rplidar_driver.h 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340
  1. /*
  2. * RPLIDAR SDK
  3. *
  4. * Copyright (c) 2009 - 2014 RoboPeak Team
  5. * http://www.robopeak.com
  6. * Copyright (c) 2014 - 2019 Shanghai Slamtec Co., Ltd.
  7. * http://www.slamtec.com
  8. *
  9. */
  10. /*
  11. * Redistribution and use in source and binary forms, with or without
  12. * modification, are permitted provided that the following conditions are met:
  13. *
  14. * 1. Redistributions of source code must retain the above copyright notice,
  15. * this list of conditions and the following disclaimer.
  16. *
  17. * 2. Redistributions in binary form must reproduce the above copyright notice,
  18. * this list of conditions and the following disclaimer in the documentation
  19. * and/or other materials provided with the distribution.
  20. *
  21. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  22. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
  23. * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  24. * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
  25. * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  26. * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  27. * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
  28. * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
  29. * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
  30. * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
  31. * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  32. *
  33. */
  34. #pragma once
  35. #ifndef __cplusplus
  36. #error "The RPlidar SDK requires a C++ compiler to be built"
  37. #endif
  38. #ifndef DEPRECATED
  39. #ifdef __GNUC__
  40. #define DEPRECATED(func) func __attribute__ ((deprecated))
  41. #elif defined(_MSC_VER)
  42. #define DEPRECATED(func) __declspec(deprecated) func
  43. #else
  44. #pragma message("WARNING: You need to implement DEPRECATED for this compiler")
  45. #define DEPRECATED(func) func
  46. #endif
  47. #endif
  48. namespace rp { namespace standalone{ namespace rplidar {
  49. struct RplidarScanMode {
  50. _u16 id;
  51. float us_per_sample; // microseconds per sample
  52. float max_distance; // max distance
  53. _u8 ans_type; // the answer type of the scam mode, its value should be RPLIDAR_ANS_TYPE_MEASUREMENT*
  54. char scan_mode[64]; // name of scan mode, max 63 characters
  55. };
  56. enum {
  57. DRIVER_TYPE_SERIALPORT = 0x0,
  58. DRIVER_TYPE_TCP = 0x1,
  59. };
  60. class ChannelDevice
  61. {
  62. public:
  63. virtual bool bind(const char*, uint32_t ) = 0;
  64. virtual bool open() {return true;}
  65. virtual void close() = 0;
  66. virtual void flush() {return;}
  67. virtual bool waitfordata(size_t data_count,_u32 timeout = -1, size_t * returned_size = NULL) = 0;
  68. virtual int senddata(const _u8 * data, size_t size) = 0;
  69. virtual int recvdata(unsigned char * data, size_t size) = 0;
  70. virtual void setDTR() {return;}
  71. virtual void clearDTR() {return;}
  72. virtual void ReleaseRxTx() {return;}
  73. };
  74. class RPlidarDriver {
  75. public:
  76. enum {
  77. DEFAULT_TIMEOUT = 2000, //2000 ms
  78. };
  79. enum {
  80. MAX_SCAN_NODES = 8192,
  81. };
  82. enum {
  83. LEGACY_SAMPLE_DURATION = 476,
  84. };
  85. public:
  86. /// Create an RPLIDAR Driver Instance
  87. /// This interface should be invoked first before any other operations
  88. ///
  89. /// \param drivertype the connection type used by the driver.
  90. static RPlidarDriver * CreateDriver(_u32 drivertype = DRIVER_TYPE_SERIALPORT);
  91. /// Dispose the RPLIDAR Driver Instance specified by the drv parameter
  92. /// Applications should invoke this interface when the driver instance is no longer used in order to free memory
  93. static void DisposeDriver(RPlidarDriver * drv);
  94. /// Open the specified serial port and connect to a target RPLIDAR device
  95. ///
  96. /// \param port_path the device path of the serial port
  97. /// e.g. on Windows, it may be com3 or \\.\com10
  98. /// on Unix-Like OS, it may be /dev/ttyS1, /dev/ttyUSB2, etc
  99. ///
  100. /// \param baudrate the baudrate used
  101. /// For most RPLIDAR models, the baudrate should be set to 115200
  102. ///
  103. /// \param flag other flags
  104. /// Reserved for future use, always set to Zero
  105. virtual u_result connect(const char *, _u32, _u32 flag = 0) = 0;
  106. /// Disconnect with the RPLIDAR and close the serial port
  107. virtual void disconnect() = 0;
  108. /// Returns TRUE when the connection has been established
  109. virtual bool isConnected() = 0;
  110. /// Ask the RPLIDAR core system to reset it self
  111. /// The host system can use the Reset operation to help RPLIDAR escape the self-protection mode.
  112. ///
  113. /// \param timeout The operation timeout value (in millisecond) for the serial port communication
  114. virtual u_result reset(_u32 timeout = DEFAULT_TIMEOUT) = 0;
  115. virtual u_result clearNetSerialRxCache() = 0;
  116. // FW1.24
  117. /// Get all scan modes that supported by lidar
  118. virtual u_result getAllSupportedScanModes(std::vector<RplidarScanMode>& outModes, _u32 timeoutInMs = DEFAULT_TIMEOUT) = 0;
  119. /// Get typical scan mode of lidar
  120. virtual u_result getTypicalScanMode(_u16& outMode, _u32 timeoutInMs = DEFAULT_TIMEOUT) = 0;
  121. /// Start scan
  122. ///
  123. /// \param force Force the core system to output scan data regardless whether the scanning motor is rotating or not.
  124. /// \param useTypicalScan Use lidar's typical scan mode or use the compatibility mode (2k sps)
  125. /// \param options Scan options (please use 0)
  126. /// \param outUsedScanMode The scan mode selected by lidar
  127. virtual u_result startScan(bool force, bool useTypicalScan, _u32 options = 0, RplidarScanMode* outUsedScanMode = NULL) = 0;
  128. /// Start scan in specific mode
  129. ///
  130. /// \param force Force the core system to output scan data regardless whether the scanning motor is rotating or not.
  131. /// \param scanMode The scan mode id (use getAllSupportedScanModes to get supported modes)
  132. /// \param options Scan options (please use 0)
  133. /// \param outUsedScanMode The scan mode selected by lidar
  134. virtual u_result startScanExpress(bool force, _u16 scanMode, _u32 options = 0, RplidarScanMode* outUsedScanMode = NULL, _u32 timeout = DEFAULT_TIMEOUT) = 0;
  135. /// Retrieve the health status of the RPLIDAR
  136. /// The host system can use this operation to check whether RPLIDAR is in the self-protection mode.
  137. ///
  138. /// \param health The health status info returned from the RPLIDAR
  139. ///
  140. /// \param timeout The operation timeout value (in millisecond) for the serial port communication
  141. virtual u_result getHealth(rplidar_response_device_health_t & health, _u32 timeout = DEFAULT_TIMEOUT) = 0;
  142. /// Get the device information of the RPLIDAR include the serial number, firmware version, device model etc.
  143. ///
  144. /// \param info The device information returned from the RPLIDAR
  145. /// \param timeout The operation timeout value (in millisecond) for the serial port communication
  146. virtual u_result getDeviceInfo(rplidar_response_device_info_t & info, _u32 timeout = DEFAULT_TIMEOUT) = 0;
  147. /// Get the sample duration information of the RPLIDAR.
  148. /// DEPRECATED, please use RplidarScanMode::us_per_sample
  149. ///
  150. /// \param rateInfo The sample duration information returned from the RPLIDAR
  151. /// \param timeout The operation timeout value (in millisecond) for the serial port communication
  152. DEPRECATED(virtual u_result getSampleDuration_uS(rplidar_response_sample_rate_t & rateInfo, _u32 timeout = DEFAULT_TIMEOUT)) = 0;
  153. /// Set the RPLIDAR's motor pwm when using accessory board, currently valid for A2 and A3 only.
  154. ///
  155. /// \param pwm The motor pwm value would like to set
  156. virtual u_result setMotorPWM(_u16 pwm) = 0;
  157. /// Set the RPLIDAR's motor rpm, currently valid for tof lidar only.
  158. ///
  159. /// \param rpm The motor rpm value would like to set
  160. virtual u_result setLidarSpinSpeed(_u16 rpm, _u32 timeout = DEFAULT_TIMEOUT) = 0;
  161. /// Start RPLIDAR's motor when using accessory board
  162. virtual u_result startMotor() = 0;
  163. /// Stop RPLIDAR's motor when using accessory board
  164. virtual u_result stopMotor() = 0;
  165. /// Check whether the device support motor control.
  166. /// Note: this API will disable grab.
  167. ///
  168. /// \param support Return the result.
  169. /// \param timeout The operation timeout value (in millisecond) for the serial port communication.
  170. virtual u_result checkMotorCtrlSupport(bool & support, _u32 timeout = DEFAULT_TIMEOUT) = 0;
  171. /// Check if the device is Tof lidar.
  172. /// Note: this API is effective if and only if getDeviceInfo has been called.
  173. ///
  174. /// \param support Return the result.
  175. /// \param timeout The operation timeout value (in millisecond) for the serial port communication.
  176. virtual u_result checkIfTofLidar(bool & isTofLidar, _u32 timeout = DEFAULT_TIMEOUT) = 0;
  177. /// Calculate RPLIDAR's current scanning frequency from the given scan data
  178. /// DEPRECATED, please use getFrequency(RplidarScanMode, size_t)
  179. ///
  180. /// Please refer to the application note doc for details
  181. /// Remark: the calcuation will be incorrect if the specified scan data doesn't contains enough data
  182. ///
  183. /// \param inExpressMode Indicate whether the RPLIDAR is in express mode
  184. /// \param count The number of sample nodes inside the given buffer
  185. /// \param frequency The scanning frequency (in HZ) calcuated by the interface.
  186. /// \param is4kmode Return whether the RPLIDAR is working on 4k sample rate mode.
  187. DEPRECATED(virtual u_result getFrequency(bool inExpressMode, size_t count, float & frequency, bool & is4kmode)) = 0;
  188. /// Calculate RPLIDAR's current scanning frequency from the given scan data
  189. /// Please refer to the application note doc for details
  190. /// Remark: the calcuation will be incorrect if the specified scan data doesn't contains enough data
  191. ///
  192. /// \param scanMode Lidar's current scan mode
  193. /// \param count The number of sample nodes inside the given buffer
  194. virtual u_result getFrequency(const RplidarScanMode& scanMode, size_t count, float & frequency) = 0;
  195. /// Ask the RPLIDAR core system to enter the scan mode(Normal/Express, Express mode is 4k mode)
  196. /// A background thread will be created by the RPLIDAR driver to fetch the scan data continuously.
  197. /// User Application can use the grabScanData() interface to retrieved the scan data cached previous by this background thread.
  198. ///
  199. /// \param force Force the core system to output scan data regardless whether the scanning motor is rotating or not.
  200. /// \param timeout The operation timeout value (in millisecond) for the serial port communication.
  201. virtual u_result startScanNormal(bool force, _u32 timeout = DEFAULT_TIMEOUT) = 0;
  202. /// Check whether the device support express mode.
  203. /// DEPRECATED, please use getAllSupportedScanModes
  204. ///
  205. /// \param support Return the result.
  206. /// \param timeout The operation timeout value (in millisecond) for the serial port communication.
  207. DEPRECATED(virtual u_result checkExpressScanSupported(bool & support, _u32 timeout = DEFAULT_TIMEOUT)) = 0;
  208. /// Ask the RPLIDAR core system to stop the current scan operation and enter idle state. The background thread will be terminated
  209. ///
  210. /// \param timeout The operation timeout value (in millisecond) for the serial port communication
  211. virtual u_result stop(_u32 timeout = DEFAULT_TIMEOUT) = 0;
  212. /// Wait and grab a complete 0-360 degree scan data previously received.
  213. /// NOTE: This method only support distance less than 16.38 meters, for longer distance, please use grabScanDataHq
  214. /// The grabbed scan data returned by this interface always has the following charactistics:
  215. ///
  216. /// 1) The first node of the grabbed data array (nodebuffer[0]) must be the first sample of a scan, i.e. the start_bit == 1
  217. /// 2) All data nodes are belong to exactly ONE complete 360-degrees's scan
  218. /// 3) Note, the angle data in one scan may not be ascending. You can use API ascendScanData to reorder the nodebuffer.
  219. ///
  220. /// \param nodebuffer Buffer provided by the caller application to store the scan data
  221. ///
  222. /// \param count The caller must initialize this parameter to set the max data count of the provided buffer (in unit of rplidar_response_measurement_node_t).
  223. /// Once the interface returns, this parameter will store the actual received data count.
  224. ///
  225. /// \param timeout Max duration allowed to wait for a complete scan data, nothing will be stored to the nodebuffer if a complete 360-degrees' scan data cannot to be ready timely.
  226. ///
  227. /// The interface will return RESULT_OPERATION_TIMEOUT to indicate that no complete 360-degrees' scan can be retrieved withing the given timeout duration.
  228. ///
  229. /// \The caller application can set the timeout value to Zero(0) to make this interface always returns immediately to achieve non-block operation.
  230. DEPRECATED(virtual u_result grabScanData(rplidar_response_measurement_node_t * nodebuffer, size_t & count, _u32 timeout = DEFAULT_TIMEOUT)) = 0;
  231. /// Wait and grab a complete 0-360 degree scan data previously received.
  232. /// The grabbed scan data returned by this interface always has the following charactistics:
  233. ///
  234. /// 1) The first node of the grabbed data array (nodebuffer[0]) must be the first sample of a scan, i.e. the start_bit == 1
  235. /// 2) All data nodes are belong to exactly ONE complete 360-degrees's scan
  236. /// 3) Note, the angle data in one scan may not be ascending. You can use API ascendScanData to reorder the nodebuffer.
  237. ///
  238. /// \param nodebuffer Buffer provided by the caller application to store the scan data
  239. ///
  240. /// \param count The caller must initialize this parameter to set the max data count of the provided buffer (in unit of rplidar_response_measurement_node_t).
  241. /// Once the interface returns, this parameter will store the actual received data count.
  242. ///
  243. /// \param timeout Max duration allowed to wait for a complete scan data, nothing will be stored to the nodebuffer if a complete 360-degrees' scan data cannot to be ready timely.
  244. ///
  245. /// The interface will return RESULT_OPERATION_TIMEOUT to indicate that no complete 360-degrees' scan can be retrieved withing the given timeout duration.
  246. ///
  247. /// \The caller application can set the timeout value to Zero(0) to make this interface always returns immediately to achieve non-block operation.
  248. virtual u_result grabScanDataHq(rplidar_response_measurement_node_hq_t * nodebuffer, size_t & count, _u32 timeout = DEFAULT_TIMEOUT) = 0;
  249. /// Ascending the scan data according to the angle value in the scan.
  250. ///
  251. /// \param nodebuffer Buffer provided by the caller application to do the reorder. Should be retrived from the grabScanData
  252. ///
  253. /// \param count The caller must initialize this parameter to set the max data count of the provided buffer (in unit of rplidar_response_measurement_node_t).
  254. /// Once the interface returns, this parameter will store the actual received data count.
  255. /// The interface will return RESULT_OPERATION_FAIL when all the scan data is invalid.
  256. DEPRECATED(virtual u_result ascendScanData(rplidar_response_measurement_node_t * nodebuffer, size_t count)) = 0;
  257. /// Ascending the scan data according to the angle value in the scan.
  258. ///
  259. /// \param nodebuffer Buffer provided by the caller application to do the reorder. Should be retrived from the grabScanData
  260. ///
  261. /// \param count The caller must initialize this parameter to set the max data count of the provided buffer (in unit of rplidar_response_measurement_node_t).
  262. /// Once the interface returns, this parameter will store the actual received data count.
  263. /// The interface will return RESULT_OPERATION_FAIL when all the scan data is invalid.
  264. virtual u_result ascendScanData(rplidar_response_measurement_node_hq_t * nodebuffer, size_t count) = 0;
  265. /// Return received scan points even if it's not complete scan
  266. ///
  267. /// \param nodebuffer Buffer provided by the caller application to store the scan data
  268. ///
  269. /// \param count Once the interface returns, this parameter will store the actual received data count.
  270. ///
  271. /// The interface will return RESULT_OPERATION_TIMEOUT to indicate that not even a single node can be retrieved since last call.
  272. DEPRECATED(virtual u_result getScanDataWithInterval(rplidar_response_measurement_node_t * nodebuffer, size_t & count)) = 0;
  273. /// Return received scan points even if it's not complete scan.
  274. ///
  275. /// \param nodebuffer Buffer provided by the caller application to store the scan data. This buffer must be initialized by
  276. /// the caller.
  277. ///
  278. /// \param count The caller must initialize this parameter to set the max data count of the provided buffer (in unit of rplidar_response_measurement_node_t).
  279. /// Once the interface returns, this parameter will store the actual received data count.
  280. ///
  281. /// The interface will return RESULT_OPERATION_TIMEOUT to indicate that not even a single node can be retrieved since last call.
  282. /// The interface will return RESULT_REMAINING_DATA to indicate that the given buffer is full, but that there remains data to be read.
  283. virtual u_result getScanDataWithIntervalHq(rplidar_response_measurement_node_hq_t * nodebuffer, size_t & count) = 0;
  284. virtual ~RPlidarDriver() {}
  285. protected:
  286. RPlidarDriver(){}
  287. public:
  288. ChannelDevice* _chanDev;
  289. };
  290. }}}