YDLIDAR SDK  V1.4.5
ydlidar_driver.h
1 /*********************************************************************
2 * Software License Agreement (BSD License)
3 *
4 * Copyright (c) 2018, EAIBOT, Inc.
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 *
11 * * Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * * Redistributions in binary form must reproduce the above
14 * copyright notice, this list of conditions and the following
15 * disclaimer in the documentation and/or other materials provided
16 * with the distribution.
17 * * Neither the name of the Willow Garage nor the names of its
18 * contributors may be used to endorse or promote products derived
19 * from this software without specific prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
25 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
27 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
29 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
31 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
32 * POSSIBILITY OF SUCH DAMAGE.
33 *********************************************************************/
34 
49 #ifndef YDLIDAR_DRIVER_H
50 #define YDLIDAR_DRIVER_H
51 #include <stdlib.h>
52 #include <atomic>
53 #include <map>
54 #include "serial.h"
55 #include "locker.h"
56 #include "thread.h"
57 #include "ydlidar_protocol.h"
58 #include "help_info.h"
59 
60 #if !defined(__cplusplus)
61 #ifndef __cplusplus
62 #error "The YDLIDAR SDK requires a C++ compiler to be built"
63 #endif
64 #endif
65 
66 
67 using namespace std;
68 using namespace serial;
69 
70 namespace ydlidar {
71 
76  public:
77  PropertyBuilderByName(bool, SingleChannel,
78  private)
79  PropertyBuilderByName(int, LidarType,
80  private)
81  PropertyBuilderByName(uint32_t, PointTime,
82  private)
83 
87  YDlidarDriver();
88 
93  virtual ~YDlidarDriver();
94 
107  result_t connect(const char *port_path, uint32_t baudrate);
108 
112  void disconnect();
113 
119  static std::string getSDKVersion();
120 
125  static std::map<std::string, std::string> lidarPortList();
126 
127 
134  bool isscanning() const;
135 
142  bool isconnected() const;
143 
152  void setIntensities(const bool &isintensities);
153 
160  void setAutoReconnect(const bool &enable);
161 
168  result_t getHealth(device_health &health, uint32_t timeout = DEFAULT_TIMEOUT);
169 
178  result_t getDeviceInfo(device_info &info, uint32_t timeout = DEFAULT_TIMEOUT);
179 
189  result_t startScan(bool force = false, uint32_t timeout = DEFAULT_TIMEOUT) ;
190 
197  result_t stop();
198 
199 
210  result_t grabScanData(node_info *nodebuffer, size_t &count,
211  uint32_t timeout = DEFAULT_TIMEOUT) ;
212 
213 
224  result_t ascendScanData(node_info *nodebuffer, size_t count);
225 
234  result_t reset(uint32_t timeout = DEFAULT_TIMEOUT);
235 
242  result_t startMotor();
243 
250  result_t stopMotor();
251 
261  result_t getScanFrequency(scan_frequency &frequency,
262  uint32_t timeout = DEFAULT_TIMEOUT);
263 
273  result_t setScanFrequencyAdd(scan_frequency &frequency,
274  uint32_t timeout = DEFAULT_TIMEOUT);
275 
285  result_t setScanFrequencyDis(scan_frequency &frequency,
286  uint32_t timeout = DEFAULT_TIMEOUT);
287 
297  result_t setScanFrequencyAddMic(scan_frequency &frequency,
298  uint32_t timeout = DEFAULT_TIMEOUT);
299 
309  result_t setScanFrequencyDisMic(scan_frequency &frequency,
310  uint32_t timeout = DEFAULT_TIMEOUT);
311 
321  result_t getSamplingRate(sampling_rate &rate,
322  uint32_t timeout = DEFAULT_TIMEOUT);
323 
333  result_t setSamplingRate(sampling_rate &rate,
334  uint32_t timeout = DEFAULT_TIMEOUT);
335 
345  result_t getZeroOffsetAngle(offset_angle &angle,
346  uint32_t timeout = DEFAULT_TIMEOUT);
347 
348  protected:
349 
354  result_t createThread();
355 
356 
366  result_t startAutoScan(bool force = false, uint32_t timeout = DEFAULT_TIMEOUT) ;
367 
373  result_t stopScan(uint32_t timeout = DEFAULT_TIMEOUT);
374 
380  result_t checkDeviceInfo(uint8_t *recvBuffer, uint8_t byte, int recvPos,
381  int recvSize, int pos);
382 
388  result_t waitDevicePackage(uint32_t timeout = DEFAULT_TIMEOUT);
394  result_t waitPackage(node_info *node, uint32_t timeout = DEFAULT_TIMEOUT);
395 
406  result_t waitScanData(node_info *nodebuffer, size_t &count,
407  uint32_t timeout = DEFAULT_TIMEOUT);
408 
412  int cacheScanData();
413 
423  result_t sendCommand(uint8_t cmd, const void *payload = NULL,
424  size_t payloadsize = 0);
425 
436  result_t waitResponseHeader(lidar_ans_header *header,
437  uint32_t timeout = DEFAULT_TIMEOUT);
438 
450  result_t waitForData(size_t data_count, uint32_t timeout = DEFAULT_TIMEOUT,
451  size_t *returned_size = NULL);
452 
461  result_t getData(uint8_t *data, size_t size);
462 
471  result_t sendData(const uint8_t *data, size_t size);
472 
473 
477  void checkTransDelay();
478 
482  void disableDataGrabbing();
483 
487  void setDTR();
488 
492  void clearDTR();
493 
497  void flushSerial();
498 
502  result_t checkAutoConnecting();
503 
504 
505  public:
506  std::atomic<bool> isConnected;
507  std::atomic<bool> isScanning;
508  std::atomic<bool> isAutoReconnect;
509  std::atomic<bool> isAutoconnting;
510 
511 
512  enum {
513  DEFAULT_TIMEOUT = 2000,
514  DEFAULT_HEART_BEAT = 1000,
515  MAX_SCAN_NODES = 3600,
516  DEFAULT_TIMEOUT_COUNT = 1,
517  };
518 
525 
526  private:
527  int PackageSampleBytes;
528  serial::Serial *_serial;
529  bool m_intensities;
530  uint32_t m_baudrate;
531  bool isSupportMotorDtrCtrl;
532  uint32_t trans_delay;
533  int m_sampling_rate;
534  int model;
535  int sample_rate;
536 
537  node_package package;
538  node_packages packages;
539 
540  uint16_t package_Sample_Index;
541  float IntervalSampleAngle;
542  float IntervalSampleAngle_LastPackage;
543  uint16_t FirstSampleAngle;
544  uint16_t LastSampleAngle;
545  uint16_t CheckSum;
546  uint8_t scan_frequence;
547 
548  uint16_t CheckSumCal;
549  uint16_t SampleNumlAndCTCal;
550  uint16_t LastSampleAngleCal;
551  bool CheckSumResult;
552  uint16_t Valu8Tou16;
553 
554  std::string serial_port;
555  uint8_t *globalRecvBuffer;
556  int retryCount;
557  bool has_device_header;
558  uint8_t last_device_byte;
559  int asyncRecvPos;
560  uint16_t async_size;
561 
562  //singleChannel
563  device_info info_;
564  device_health health_;
565  lidar_ans_header header_;
566  uint8_t *headerBuffer;
567  uint8_t *infoBuffer;
568  uint8_t *healthBuffer;
569  bool get_device_info_success;
570  bool get_device_health_success;
571 
572  int package_index;
573  bool has_package_error;
574 
575 };
576 
577 }// namespace ydlidar
578 
579 #endif // YDLIDAR_DRIVER_H
std::atomic< bool > isScanning
扫图状态
Definition: ydlidar_driver.h:507
size_t scan_node_count
激光点数
Definition: ydlidar_driver.h:520
node_info * scan_node_buf
激光点信息
Definition: ydlidar_driver.h:519
Definition: help_info.h:39
Event _dataEvent
数据同步事件
Definition: ydlidar_driver.h:521
Definition: ydlidar_protocol.h:179
Definition: locker.h:184
Thread _thread
线程id
Definition: ydlidar_driver.h:524
std::atomic< bool > isAutoconnting
是否正在自动连接中
Definition: ydlidar_driver.h:509
Definition: ydlidar_protocol.h:223
Locker _lock
线程锁
Definition: ydlidar_driver.h:522
Locker _serial_lock
串口锁
Definition: ydlidar_driver.h:523
std::atomic< bool > isConnected
串口连接状体
Definition: ydlidar_driver.h:506
Definition: thread.h:22
Definition: ydlidar_protocol.h:151
Definition: serial.h:11
std::atomic< bool > isAutoReconnect
异常自动从新连接
Definition: ydlidar_driver.h:508
Definition: ydlidar_protocol.h:172
Definition: ydlidar_protocol.h:184
Definition: ydlidar_protocol.h:212
Definition: ydlidar_driver.h:75
Definition: ydlidar_protocol.h:135
Definition: ydlidar_protocol.h:188
Definition: serial.h:109
Definition: locker.h:20
Definition: ydlidar_protocol.h:161