Debian Codegraph @

3rdparty/rplidar_sdk/sdk/include/sl_lidar_driver.h

raw symbols SCIP
1 /*
2 * Slamtec LIDAR SDK
3 *
4 * Copyright (c) 2020 Shanghai Slamtec Co., Ltd.
5 * http://www.slamtec.com
6 */
7 /*
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions are met:
10 *
11 * 1. Redistributions of source code must retain the above copyright notice,
12 * this list of conditions and the following disclaimer.
13 *
14 * 2. Redistributions in binary form must reproduce the above copyright notice,
15 * this list of conditions and the following disclaimer in the documentation
16 * and/or other materials provided with the distribution.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
20 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
21 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
22 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
23 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
24 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
25 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
26 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
27 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
28 * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 *
30 */
31
32 #pragma once
33
34 #ifndef __cplusplus
35 #error "The Slamtec LIDAR SDK requires a C++ compiler to be built"
36 #endif
37
38 #include <vector>
39 #include <map>
40 #include <string>
41
42 #ifndef DEPRECATED
43 #ifdef __GNUC__
44 #define DEPRECATED(func) func __attribute__ ((deprecated))
45 #elif defined(_MSC_VER)
46 #define DEPRECATED(func) __declspec(deprecated) func
47 #else
48 #pragma message("WARNING: You need to implement DEPRECATED for this compiler")
49 #define DEPRECATED(func) func
50 #endif
51 #endif
52
53
54 #include "sl_lidar_cmd.h"
55
56 #include <string>
57
58 namespace sl {
59
60 #ifdef DEPRECATED
61 #define DEPRECATED_WARN(fn, replacement) do { \
62 static bool __shown__ = false; \
63 if (!__shown__) { \
64 printDeprecationWarn(fn, replacement); \
65 __shown__ = true; \
66 } \
67 } while (0)
68 #endif
69
70 /**
71 * Lidar scan mode
72 */
73 struct LidarScanMode
74 {
75 // Mode id
76 sl_u16 id;
77
78 // Time cost for one measurement (in microseconds)
79 float us_per_sample;
80
81 // Max distance in this scan mode (in meters)
82 float max_distance;
83
84 // The answer command code for this scan mode
85 sl_u8 ans_type;
86
87 // The name of scan mode (padding with 0 if less than 64 characters)
88 char scan_mode[64];
89 };
90
91 template <typename T>
92 struct Result
93 {
94 sl_result err;
95 T value;
96 Result(const T& value)
97 : err(SL_RESULT_OK)
98 , value(value)
99 {
100 }
101
102 Result(sl_result err)
103 : err(err)
104 , value()
105 {
106 }
107
108 operator sl_result() const
109 {
110 return err;
111 }
112
113 operator bool() const
114 {
115 return SL_IS_OK(err);
116 }
117
118 T& operator* ()
119 {
120 return value;
121 }
122
123 T* operator-> ()
124 {
125 return &value;
126 }
127 };
128
129 enum LIDARTechnologyType {
130 LIDAR_TECHNOLOGY_UNKNOWN = 0,
131 LIDAR_TECHNOLOGY_TRIANGULATION = 1,
132 LIDAR_TECHNOLOGY_DTOF = 2,
133 LIDAR_TECHNOLOGY_ETOF = 3,
134 LIDAR_TECHNOLOGY_FMCW = 4,
135 };
136
137 enum LIDARMajorType {
138 LIDAR_MAJOR_TYPE_UNKNOWN = 0,
139 LIDAR_MAJOR_TYPE_A_SERIES = 1,
140 LIDAR_MAJOR_TYPE_S_SERIES = 2,
141 LIDAR_MAJOR_TYPE_T_SERIES = 3,
142 LIDAR_MAJOR_TYPE_M_SERIES = 4,
143 LIDAR_MAJOR_TYPE_C_SERIES = 6,
144 };
145
146 enum LIDARInterfaceType {
147 LIDAR_INTERFACE_UART = 0,
148 LIDAR_INTERFACE_ETHERNET = 1,
149 LIDAR_INTERFACE_USB = 2,
150 LIDAR_INTERFACE_CANBUS = 5,
151
152
153 LIDAR_INTERFACE_UNKNOWN = 0xFFFF,
154 };
155
156 struct SlamtecLidarTimingDesc {
157
158 sl_u32 sample_duration_uS;
159 sl_u32 native_baudrate;
160
161 sl_u32 linkage_delay_uS;
162
163 LIDARInterfaceType native_interface_type;
164
165 bool native_timestamp_support;
166 };
167
168 /**
169 * Abstract interface of communication channel
170 */
171 class IChannel
172 {
173 public:
174 virtual ~IChannel() {}
175
176 public:
177 /**
178 * Open communication channel (return true if succeed)
179 */
180 virtual bool open() = 0;
181
182 /**
183 * Close communication channel
184 */
185 virtual void close() = 0;
186
187 /**
188 * Flush all written data to remote endpoint
189 */
190 virtual void flush() = 0;
191
192 /**
193 * Wait for some data
194 * \param size Bytes to wait
195 * \param timeoutInMs Wait timeout (in microseconds, -1 for forever)
196 * \param actualReady [out] actual ready bytes
197 * \return true for data ready
198 */
199 virtual bool waitForData(size_t size, sl_u32 timeoutInMs = -1, size_t* actualReady = nullptr) = 0;
200
201
202 /**
203 * Wait for some data
204 * \param size_hint Byte count may available to retrieve without beening blocked
205 * \param timeoutInMs Wait timeout (in microseconds, -1 for forever)
206 * \return RESULT_OK if there is data available for receiving
207 * RESULT_OPERATION_TIMEOUT if the given timeout duration is exceed
208 * RESULT_OPERATION_FAIL if there is something wrong with the channel
209 */
210 virtual sl_result waitForDataExt(size_t& size_hint, sl_u32 timeoutInMs = 1000) = 0;
211
212
213 /**
214 * Send data to remote endpoint
215 * \param data The data buffer
216 * \param size The size of data buffer (in bytes)
217 * \return Bytes written (negative for write failure)
218 */
219 virtual int write(const void* data, size_t size) = 0;
220
221 /**
222 * Read data from the chanel
223 * \param buffer The buffer to receive data
224 * \param size The size of the read buffer
225 * \return Bytes read (negative for read failure)
226 */
227 virtual int read(void* buffer, size_t size) = 0;
228
229 /**
230 * Clear read cache
231 */
232 virtual void clearReadCache() = 0;
233
234 virtual int getChannelType() = 0;
235
236 private:
237
238 };
239
240 /**
241 * Abstract interface of serial port channel
242 */
243 class ISerialPortChannel : public IChannel
244 {
245 public:
246 virtual ~ISerialPortChannel() {}
247
248 public:
249 virtual void setDTR(bool dtr) = 0;
250 };
251
252 /**
253 * Create a serial channel
254 * \param device Serial port device
255 * e.g. on Windows, it may be com3 or \\.\com10
256 * on Unix-Like OS, it may be /dev/ttyS1, /dev/ttyUSB2, etc
257 * \param baudrate Baudrate
258 * Please refer to the datasheet for the baudrate (maybe 115200 or 256000)
259 */
260 Result<IChannel*> createSerialPortChannel(const std::string& device, int baudrate);
261
262 /**
263 * Create a TCP channel
264 * \param ip IP address of the device
265 * \param port TCP port
266 */
267 Result<IChannel*> createTcpChannel(const std::string& ip, int port);
268
269 /**
270 * Create a UDP channel
271 * \param ip IP address of the device
272 * \param port UDP port
273 */
274 Result<IChannel*> createUdpChannel(const std::string& ip, int port);
275
276 enum MotorCtrlSupport
277 {
278 MotorCtrlSupportNone = 0,
279 MotorCtrlSupportPwm = 1,
280 MotorCtrlSupportRpm = 2,
281 };
282
283 enum ChannelType{
284 CHANNEL_TYPE_SERIALPORT = 0x0,
285 CHANNEL_TYPE_TCP = 0x1,
286 CHANNEL_TYPE_UDP = 0x2,
287 };
288
289 /**
290 * Lidar motor info
291 */
292 struct LidarMotorInfo
293 {
294 MotorCtrlSupport motorCtrlSupport;
295
296 // Desire speed
297 sl_u16 desired_speed;
298
299 // Max speed
300 sl_u16 max_speed;
301
302 // Min speed
303 sl_u16 min_speed;
304 };
305
306 class ILidarDriver
307 {
308 public:
309 virtual ~ILidarDriver() {}
310
311 public:
312 /**
313 * Connect to LIDAR via channel
314 * \param channel The communication channel
315 * Note: you should manage the lifecycle of the channel object, make sure it is alive during lidar driver's lifecycle
316 */
317 virtual sl_result connect(IChannel* channel) = 0;
318
319 /**
320 * Disconnect from the LIDAR
321 */
322 virtual void disconnect() = 0;
323
324 /**
325 * Check if the connection is established
326 */
327 virtual bool isConnected() = 0;
328
329 public:
330 enum
331 {
332 DEFAULT_TIMEOUT = 2000
333 };
334
335 public:
336 /// Ask the LIDAR core system to reset it self
337 /// The host system can use the Reset operation to help LIDAR escape the self-protection mode.
338 ///
339 /// \param timeout The operation timeout value (in millisecond)
340 virtual sl_result reset(sl_u32 timeoutInMs = DEFAULT_TIMEOUT) = 0;
341
342 /// Get all scan modes that supported by lidar
343 virtual sl_result getAllSupportedScanModes(std::vector<LidarScanMode>& outModes, sl_u32 timeoutInMs = DEFAULT_TIMEOUT) = 0;
344
345 /// Get typical scan mode of lidar
346 virtual sl_result getTypicalScanMode(sl_u16& outMode, sl_u32 timeoutInMs = DEFAULT_TIMEOUT) = 0;
347
348 /// Start scan
349 ///
350 /// \param force Force the core system to output scan data regardless whether the scanning motor is rotating or not.
351 /// \param useTypicalScan Use lidar's typical scan mode or use the compatibility mode (2k sps)
352 /// \param options Scan options (please use 0)
353 /// \param outUsedScanMode The scan mode selected by lidar
354 virtual sl_result startScan(bool force, bool useTypicalScan, sl_u32 options = 0, LidarScanMode* outUsedScanMode = nullptr) = 0;
355
356 /// Start scan in specific mode
357 ///
358 /// \param force Force the core system to output scan data regardless whether the scanning motor is rotating or not.
359 /// \param scanMode The scan mode id (use getAllSupportedScanModes to get supported modes)
360 /// \param options Scan options (please use 0)
361 /// \param outUsedScanMode The scan mode selected by lidar
362 virtual sl_result startScanExpress(bool force, sl_u16 scanMode, sl_u32 options = 0, LidarScanMode* outUsedScanMode = nullptr, sl_u32 timeout = DEFAULT_TIMEOUT) = 0;
363
364 /// Retrieve the health status of the RPLIDAR
365 /// The host system can use this operation to check whether RPLIDAR is in the self-protection mode.
366 ///
367 /// \param health The health status info returned from the RPLIDAR
368 ///
369 /// \param timeout The operation timeout value (in millisecond) for the serial port communication
370 virtual sl_result getHealth(sl_lidar_response_device_health_t& health, sl_u32 timeout = DEFAULT_TIMEOUT) = 0;
371
372 /// Get the device information of the RPLIDAR include the serial number, firmware version, device model etc.
373 ///
374 /// \param info The device information returned from the RPLIDAR
375 /// \param timeout The operation timeout value (in millisecond) for the serial port communication
376 virtual sl_result getDeviceInfo(sl_lidar_response_device_info_t& info, sl_u32 timeout = DEFAULT_TIMEOUT) = 0;
377
378 /// Check whether the device support motor control
379 /// Note: this API will disable grab.
380 ///
381 /// \param motorCtrlSupport Return the result.
382 /// \param timeout The operation timeout value (in millisecond) for the serial port communication.
383 virtual sl_result checkMotorCtrlSupport(MotorCtrlSupport& motorCtrlSupport, sl_u32 timeout = DEFAULT_TIMEOUT) = 0;
384
385 /// Calculate LIDAR's current scanning frequency from the given scan data
386 /// Please refer to the application note doc for details
387 /// Remark: the calcuation will be incorrect if the specified scan data doesn't contains enough data
388 ///
389 /// \param scanMode Lidar's current scan mode
390 /// \param nodes Current scan's measurements
391 /// \param count The number of sample nodes inside the given buffer
392 virtual sl_result getFrequency(const LidarScanMode& scanMode, const sl_lidar_response_measurement_node_hq_t* nodes, size_t count, float& frequency) = 0;
393
394 ///Set LPX and S2E series lidar's static IP address
395 ///
396 /// \param conf Network parameter that LPX series lidar owned
397 /// \param timeout The operation timeout value (in millisecond) for the ethernet udp communication
398 virtual sl_result setLidarIpConf(const sl_lidar_ip_conf_t& conf, sl_u32 timeout = DEFAULT_TIMEOUT) = 0;
399
400 ///Get LPX and S2E series lidar's static IP address
401 ///
402 /// \param conf Network parameter that LPX series lidar owned
403 /// \param timeout The operation timeout value (in millisecond) for the ethernet udp communication
404 virtual sl_result getLidarIpConf( sl_lidar_ip_conf_t& conf, sl_u32 timeout = DEFAULT_TIMEOUT) = 0;
405 //
406 /////Get LPX series lidar's MAC address
407 ///
408 /// \param macAddrArray The device MAC information returned from the LPX series lidar
409 /// Notice: the macAddrArray must point to a valid buffer with at least 6 bytes length
410 /// Otherwise, buffer overwrite will occur
411 virtual sl_result getDeviceMacAddr(sl_u8* macAddrArray, sl_u32 timeoutInMs = DEFAULT_TIMEOUT) = 0;
412
413 /// Ask the LIDAR core system to stop the current scan operation and enter idle state. The background thread will be terminated
414 ///
415 /// \param timeout The operation timeout value (in millisecond) for the serial port communication
416 virtual sl_result stop(sl_u32 timeout = DEFAULT_TIMEOUT) = 0;
417
418 /// Wait and grab a complete 0-360 degree scan data previously received.
419 /// The grabbed scan data returned by this interface always has the following charactistics:
420 ///
421 /// 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
422 /// 2) All data nodes are belong to exactly ONE complete 360-degrees's scan
423 /// 3) Note, the angle data in one scan may not be ascending. You can use API ascendScanData to reorder the nodebuffer.
424 ///
425 /// \param nodebuffer Buffer provided by the caller application to store the scan data
426 ///
427 /// \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).
428 /// Once the interface returns, this parameter will store the actual received data count.
429 ///
430 /// \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.
431 ///
432 /// The interface will return SL_RESULT_OPERATION_TIMEOUT to indicate that no complete 360-degrees' scan can be retrieved withing the given timeout duration.
433 ///
434 /// \The caller application can set the timeout value to Zero(0) to make this interface always returns immediately to achieve non-block operation.
435 virtual sl_result grabScanDataHq(sl_lidar_response_measurement_node_hq_t* nodebuffer, size_t& count, sl_u32 timeout = DEFAULT_TIMEOUT) = 0;
436
437
438
439 /// Wait and grab a complete 0-360 degree scan data previously received with timestamp support.
440 ///
441 /// The returned timestamp belongs to the first data point of the scan data (begining of the scan).
442 /// Its value is represented based on the current machine's time domain with the unit of microseconds (uS).
443 ///
444 /// If the currently connected LIDAR supports hardware timestamp mechanism, this timestamp will use
445 /// the actual data emitted by the LIDAR device and remap it to the current machine's time domain.
446 ///
447 /// For other models that do not support hardware timestamps, this data will be deducted through estimation,
448 /// and there may be a slight deviation from the actual situation.
449 ///
450 /// The grabbed scan data returned by this interface always has the following charactistics:
451 ///
452 /// 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
453 /// 2) All data nodes are belong to exactly ONE complete 360-degrees's scan
454 /// 3) Note, the angle data in one scan may not be ascending. You can use API ascendScanData to reorder the nodebuffer.
455 ///
456 /// \param nodebuffer Buffer provided by the caller application to store the scan data
457 ///
458 /// \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).
459 /// Once the interface returns, this parameter will store the actual received data count.
460 ///
461 /// \param timestamp_uS The reference used to store the timestamp value.
462 /// \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.
463 ///
464 /// The interface will return SL_RESULT_OPERATION_TIMEOUT to indicate that no complete 360-degrees' scan can be retrieved withing the given timeout duration.
465 ///
466 /// \The caller application can set the timeout value to Zero(0) to make this interface always returns immediately to achieve non-block operation.
467 virtual sl_result grabScanDataHqWithTimeStamp(sl_lidar_response_measurement_node_hq_t* nodebuffer, size_t& count, sl_u64 & timestamp_uS, sl_u32 timeout = DEFAULT_TIMEOUT) = 0;
468
469
470 /// Ascending the scan data according to the angle value in the scan.
471 ///
472 /// \param nodebuffer Buffer provided by the caller application to do the reorder. Should be retrived from the grabScanData
473 ///
474 /// \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).
475 /// Once the interface returns, this parameter will store the actual received data count.
476 /// The interface will return SL_RESULT_OPERATION_FAIL when all the scan data is invalid.
477 virtual sl_result ascendScanData(sl_lidar_response_measurement_node_hq_t* nodebuffer, size_t count) = 0;
478
479 /// Return received scan points even if it's not complete scan
480 ///
481 /// \param nodebuffer Buffer provided by the caller application to store the scan data
482 ///
483 /// \param count Once the interface returns, this parameter will store the actual received data count.
484 ///
485 /// The interface will return SL_RESULT_OPERATION_TIMEOUT to indicate that not even a single node can be retrieved since last call.
486 virtual sl_result getScanDataWithIntervalHq(sl_lidar_response_measurement_node_hq_t* nodebuffer, size_t& count) = 0;
487 /// Set lidar motor speed
488 /// The host system can use this operation to set lidar motor speed.
489 ///
490 /// \param speed The speed value set to lidar
491 ///
492 ///Note: The function will stop scan if speed is DEFAULT_MOTOR_SPEED.
493 virtual sl_result setMotorSpeed(sl_u16 speed = DEFAULT_MOTOR_SPEED) = 0;
494
495 /// Get the motor information of the RPLIDAR include the max speed, min speed, desired speed.
496 ///
497 /// \param motorInfo The motor information returned from the RPLIDAR
498 virtual sl_result getMotorInfo(LidarMotorInfo &motorInfo, sl_u32 timeoutInMs = DEFAULT_TIMEOUT) = 0;
499
500
501 /// Ask the LIDAR to use a new baudrate for serial communication
502 /// The target LIDAR system must support such feature to work.
503 /// This function does NOT check whether the target LIDAR works with the requiredBaudRate or not.
504 /// In order to verifiy the result, use getDeviceInfo or other getXXXX functions instead.
505 ///
506 /// \param requiredBaudRate The new baudrate required to be used. It MUST matches with the baudrate of the binded channel.
507 /// \param baudRateDetected The actual baudrate detected by the LIDAR system
508 virtual sl_result negotiateSerialBaudRate(sl_u32 requiredBaudRate, sl_u32* baudRateDetected = NULL) = 0;
509
510
511
512 /// Get the technology of the LIDAR's measurement system
513 ///
514 ///
515 /// \param devInfo The device info used to deduct the result
516 /// If NULL is specified, a driver cached version of the connected LIDAR will be used
517 virtual LIDARTechnologyType getLIDARTechnologyType(const sl_lidar_response_device_info_t* devInfo = nullptr) = 0;
518
519
520 /// Get the Major Type (Series Info) of the LIDAR
521 ///
522 ///
523 /// \param devInfo The device info used to deduct the result
524 /// If NULL is specified, a driver cached version of the connected LIDAR will be used
525 virtual LIDARMajorType getLIDARMajorType(const sl_lidar_response_device_info_t* devInfo = nullptr) = 0;
526
527
528 /// Get the Model Name of the LIDAR
529 /// The result will be somthing like: "A1M8" or "S1M1" or "A3M1-R1"
530 ///
531 /// \param out_description The output string that contains the generated model name
532 ///
533 /// \param fetchAliasName If set to true, a communication will be taken to ask if there is any Alias name availabe
534 /// \param devInfo The device info used to deduct the result
535 /// If NULL is specified, a driver cached version of the connected LIDAR will be used
536 /// \param timeout The timeout value used by potential data communication
537 virtual sl_result getModelNameDescriptionString(std::string& out_description, bool fetchAliasName = true, const sl_lidar_response_device_info_t* devInfo = nullptr, sl_u32 timeout = DEFAULT_TIMEOUT) = 0;
538
539 };
540
541 /**
542 * Create a LIDAR driver instance
543 *
544 * Example
545 * Result<ISerialChannel*> channel = createSerialPortChannel("/dev/ttyUSB0", 115200);
546 * assert((bool)channel);
547 * assert(*channel);
548 *
549 * auto lidar = createLidarDriver();
550 * assert((bool)lidar);
551 * assert(*lidar);
552 *
553 * auto res = (*lidar)->connect(*channel);
554 * assert(SL_IS_OK(res));
555 *
556 * sl_lidar_response_device_info_t deviceInfo;
557 * res = (*lidar)->getDeviceInfo(deviceInfo);
558 * assert(SL_IS_OK(res));
559 *
560 * printf("Model: %d, Firmware Version: %d.%d, Hardware Version: %d\n",
561 * deviceInfo.model,
562 * deviceInfo.firmware_version >> 8, deviceInfo.firmware_version & 0xffu,
563 * deviceInfo.hardware_version);
564 *
565 * delete *lidar;
566 * delete *channel;
567 */
568 Result<ILidarDriver*> createLidarDriver();
569 }