| 1 | /* |
| 2 | * Slamtec LIDAR SDK |
| 3 | * |
| 4 | * Copyright (c) 2014 - 2020 Shanghai Slamtec Co., Ltd. |
| 5 | * http://www.slamtec.com |
| 6 | * |
| 7 | */ |
| 8 | /* |
| 9 | * Redistribution and use in source and binary forms, with or without |
| 10 | * modification, are permitted provided that the following conditions are met: |
| 11 | * |
| 12 | * 1. Redistributions of source code must retain the above copyright notice, |
| 13 | * this list of conditions and the following disclaimer. |
| 14 | * |
| 15 | * 2. Redistributions in binary form must reproduce the above copyright notice, |
| 16 | * this list of conditions and the following disclaimer in the documentation |
| 17 | * and/or other materials provided with the distribution. |
| 18 | * |
| 19 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
| 20 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, |
| 21 | * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
| 22 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR |
| 23 | * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, |
| 24 | * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
| 25 | * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; |
| 26 | * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, |
| 27 | * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR |
| 28 | * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, |
| 29 | * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 30 | * |
| 31 | */ |
| 32 | |
| 33 | #include "sdkcommon.h" |
| 34 | #include "hal/abs_rxtx.h" |
| 35 | #include "hal/thread.h" |
| 36 | #include "hal/types.h" |
| 37 | #include "hal/assert.h" |
| 38 | #include "hal/locker.h" |
| 39 | #include "hal/socket.h" |
| 40 | #include "hal/event.h" |
| 41 | #include "rplidar_driver.h" |
| 42 | #include "sl_crc.h" |
| 43 | #include <algorithm> |
| 44 | |
| 45 | namespace rp { namespace standalone{ namespace rplidar { |
| 46 | |
| 47 | RPlidarDriver::RPlidarDriver(){} |
| 48 | |
| 49 | RPlidarDriver::RPlidarDriver(sl_u32 channelType) |
| 50 | :_channelType(channelType) |
| 51 | { |
| 52 | } |
| 53 | |
| 54 | RPlidarDriver::~RPlidarDriver() {} |
| 55 | |
| 56 | RPlidarDriver * RPlidarDriver::CreateDriver(_u32 drivertype) |
| 57 | { |
| 58 | //_channelType = drivertype; |
| 59 | return new RPlidarDriver(drivertype); |
| 60 | } |
| 61 | |
| 62 | void RPlidarDriver::DisposeDriver(RPlidarDriver * drv) |
| 63 | { |
| 64 | delete drv; |
| 65 | } |
| 66 | |
| 67 | u_result RPlidarDriver::connect(const char *path, _u32 portOrBaud, _u32 flag) |
| 68 | { |
| 69 | switch (_channelType) |
| 70 | { |
| 71 | case CHANNEL_TYPE_SERIALPORT: |
| 72 | _channel = (*createSerialPortChannel(path, portOrBaud)); |
| 73 | break; |
| 74 | case CHANNEL_TYPE_TCP: |
| 75 | _channel = *createTcpChannel(path, portOrBaud); |
| 76 | break; |
| 77 | case CHANNEL_TYPE_UDP: |
| 78 | _channel = *createUdpChannel(path, portOrBaud); |
| 79 | break; |
| 80 | } |
| 81 | if (!(bool)_channel) return SL_RESULT_OPERATION_FAIL; |
| 82 | |
| 83 | _lidarDrv = *createLidarDriver(); |
| 84 | |
| 85 | if (!(bool)_lidarDrv) return SL_RESULT_OPERATION_FAIL; |
| 86 | |
| 87 | sl_result ans =(_lidarDrv)->connect(_channel); |
| 88 | return ans; |
| 89 | } |
| 90 | |
| 91 | void RPlidarDriver::disconnect() |
| 92 | { |
| 93 | (_lidarDrv)->disconnect(); |
| 94 | } |
| 95 | |
| 96 | bool RPlidarDriver::isConnected() |
| 97 | { |
| 98 | return (_lidarDrv)->isConnected(); |
| 99 | } |
| 100 | |
| 101 | u_result RPlidarDriver::reset(_u32 timeout) |
| 102 | { |
| 103 | return (_lidarDrv)->reset(); |
| 104 | } |
| 105 | |
| 106 | u_result RPlidarDriver::getAllSupportedScanModes(std::vector<RplidarScanMode>& outModes, _u32 timeoutInMs) |
| 107 | { |
| 108 | return (_lidarDrv)->getAllSupportedScanModes(outModes, timeoutInMs); |
| 109 | } |
| 110 | |
| 111 | u_result RPlidarDriver::getTypicalScanMode(_u16& outMode, _u32 timeoutInMs) |
| 112 | { |
| 113 | return (_lidarDrv)->getTypicalScanMode(outMode, timeoutInMs); |
| 114 | } |
| 115 | |
| 116 | u_result RPlidarDriver::startScan(bool force, bool useTypicalScan, _u32 options, RplidarScanMode* outUsedScanMode) |
| 117 | { |
| 118 | return (_lidarDrv)->startScan(force, useTypicalScan, options, outUsedScanMode); |
| 119 | } |
| 120 | |
| 121 | u_result RPlidarDriver::startScanExpress(bool force, _u16 scanMode, _u32 options, RplidarScanMode* outUsedScanMode, _u32 timeout) |
| 122 | { |
| 123 | return (_lidarDrv)->startScanExpress(force, scanMode, options, outUsedScanMode, timeout); |
| 124 | } |
| 125 | |
| 126 | u_result RPlidarDriver::getHealth(rplidar_response_device_health_t & health, _u32 timeout) |
| 127 | { |
| 128 | return (_lidarDrv)->getHealth(health, timeout); |
| 129 | } |
| 130 | |
| 131 | u_result RPlidarDriver::getDeviceInfo(rplidar_response_device_info_t & info, _u32 timeout) |
| 132 | { |
| 133 | return (_lidarDrv)->getDeviceInfo(info, timeout); |
| 134 | } |
| 135 | |
| 136 | u_result RPlidarDriver::setMotorPWM(_u16 pwm) |
| 137 | { |
| 138 | return (_lidarDrv)->setMotorSpeed(pwm); |
| 139 | } |
| 140 | |
| 141 | u_result RPlidarDriver::checkMotorCtrlSupport(bool & support, _u32 timeout) |
| 142 | { |
| 143 | MotorCtrlSupport motorSupport; |
| 144 | u_result ans = (_lidarDrv)->checkMotorCtrlSupport(motorSupport, timeout); |
| 145 | if (motorSupport == MotorCtrlSupportNone) |
| 146 | support = false; |
| 147 | return ans; |
| 148 | } |
| 149 | |
| 150 | u_result RPlidarDriver::setLidarIpConf(const rplidar_ip_conf_t& conf, _u32 timeout) |
| 151 | { |
| 152 | return (_lidarDrv)->setLidarIpConf(conf, timeout); |
| 153 | } |
| 154 | |
| 155 | u_result RPlidarDriver::getLidarIpConf(rplidar_ip_conf_t& conf, _u32 timeout) |
| 156 | { |
| 157 | return (_lidarDrv)->getLidarIpConf(conf, timeout); |
| 158 | } |
| 159 | |
| 160 | u_result RPlidarDriver::getDeviceMacAddr(_u8* macAddrArray, _u32 timeoutInMs) |
| 161 | { |
| 162 | return (_lidarDrv)->getDeviceMacAddr(macAddrArray, timeoutInMs); |
| 163 | } |
| 164 | |
| 165 | u_result RPlidarDriver::stop(_u32 timeout) |
| 166 | { |
| 167 | return (_lidarDrv)->stop(timeout); |
| 168 | } |
| 169 | |
| 170 | u_result RPlidarDriver::grabScanDataHq(rplidar_response_measurement_node_hq_t * nodebuffer, size_t & count, _u32 timeout) |
| 171 | { |
| 172 | return (_lidarDrv)->grabScanDataHq(nodebuffer, count, timeout); |
| 173 | } |
| 174 | |
| 175 | u_result RPlidarDriver::ascendScanData(rplidar_response_measurement_node_hq_t * nodebuffer, size_t count) |
| 176 | { |
| 177 | return (_lidarDrv)->ascendScanData(nodebuffer, count); |
| 178 | } |
| 179 | |
| 180 | u_result RPlidarDriver::getScanDataWithInterval(rplidar_response_measurement_node_t * nodebuffer, size_t & count) |
| 181 | { |
| 182 | return RESULT_OPERATION_NOT_SUPPORT; |
| 183 | } |
| 184 | |
| 185 | u_result RPlidarDriver::getScanDataWithIntervalHq(rplidar_response_measurement_node_hq_t * nodebuffer, size_t & count) |
| 186 | { |
| 187 | return (_lidarDrv)->getScanDataWithIntervalHq(nodebuffer, count); |
| 188 | } |
| 189 | |
| 190 | u_result RPlidarDriver::startMotor() |
| 191 | { |
| 192 | return (_lidarDrv)->setMotorSpeed(DEFAULT_MOTOR_SPEED); |
| 193 | } |
| 194 | u_result RPlidarDriver::stopMotor() |
| 195 | { |
| 196 | return (_lidarDrv)->setMotorSpeed(0); |
| 197 | } |
| 198 | |
| 199 | }}} |