YARP
Yet Another Robot Platform
rpLidar.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2006-2020 Istituto Italiano di Tecnologia (IIT)
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17  */
18 
19 #ifndef RPLIDAR_H
20 #define RPLIDAR_H
21 
22 
23 #include <yarp/os/LogComponent.h>
24 #include <yarp/os/PeriodicThread.h>
25 #include <yarp/os/Semaphore.h>
28 #include <yarp/dev/PolyDriver.h>
29 #include <yarp/sig/Vector.h>
30 #include <yarp/dev/ISerialDevice.h>
31 
32 #include <mutex>
33 #include <string>
34 #include <vector>
35 
36 using namespace yarp::os;
37 using namespace yarp::dev;
38 
39 typedef unsigned char byte;
40 
41 
43 
44 
46 {
47  int maxsize;
48  int start;
49  int end;
50  byte *elems;
51 
52 public:
53  inline bool isFull()
54  {
55  return (end + 1) % maxsize == start;
56  }
57 
58  inline const byte* getRawData()
59  {
60  return elems;
61  }
62 
63  inline bool isEmpty()
64  {
65  return end == start;
66  }
67 
68  inline bool write_elem(byte elem)
69  {
70  elems[end] = elem;
71  end = (end + 1) % maxsize;
72  if (end == start)
73  {
74  yCError(RPLIDAR, "rpLidar buffer overrun!");
75  start = (start + 1) % maxsize; // full, overwrite
76  return false;
77  }
78  return true;
79  }
80 
81  inline bool write_elems(byte* elems, int size)
82  {
83  for (int i = 0; i < size; i++)
84  {
85  if (write_elem(elems[i]) == false) return false;
86  }
87  return true;
88  }
89 
90  inline int size()
91  {
92  int i;
93  if (end>start)
94  i = end - start;
95  else if (end == start)
96  i = 0;
97  else
98  i = maxsize - start + end;
99  return i;
100  }
101 
102  inline bool read_elem(byte* elem)
103  {
104  if (end == start)
105  {
106  yCError(RPLIDAR, "rpLidar buffer underrun!");
107  return false;
108  }
109  *elem = elems[start];
110  start = (start + 1) % maxsize;
111  return true;
112  }
113 
114  inline void throw_away_elem()
115  {
116  start = (start + 1) % maxsize;
117  }
118 
119  inline void throw_away_elems(int size)
120  {
121  start = (start + size) % maxsize;
122  }
123 
124  inline byte select_elem(int offset)
125  {
126  return elems[(start+offset) % maxsize];
127  }
128 
129  inline void select_elems(byte* elems, int size)
130  {
131  for (int i = 0; i < size; i++)
132  {
133  elems[i] = select_elem(i);
134  }
135  }
136 
137  inline bool read_elems(byte* elems, int size)
138  {
139  for (int i = 0; i < size; i++)
140  {
141  if (read_elem(&elems[i]) == false) return false;
142  }
143  return true;
144  }
145 
146  inline unsigned int getMaxSize()
147  {
148  return maxsize;
149  }
150 
151  inline void clear()
152  {
153  start = 0;
154  end = 0;
155  }
156 
157  inline unsigned int get_start()
158  {
159  return start;
160  }
161 
162  inline unsigned int get_end()
163  {
164  return end;
165  }
166 
167  rpLidarCircularBuffer(int bufferSize);
169 };
170 
171 //---------------------------------------------------------------------------------------------------------------
172 struct Range_t
173 {
174  double min;
175  double max;
176 };
177 
178 //---------------------------------------------------------------------------------------------------------------
185 {
186 protected:
189 
190  std::mutex mutex;
192 
194 
195  double min_angle;
196  double max_angle;
197  double min_distance;
198  double max_distance;
199  double resolution;
203  std::vector <Range_t> range_skip_vector;
204 
205  std::string info;
206  Device_status device_status;
207 
209 
210 public:
211  RpLidar(double period = 0.01) : PeriodicThread(period),
212  pSerial(nullptr),
213  sensorsNum(0),
214  min_angle(0.0),
215  max_angle(0.0),
216  min_distance(0.0),
217  max_distance(0.0),
218  resolution(0.0),
219  clip_max_enable(false),
220  clip_min_enable(false),
221  do_not_clip_infinity_enable(false),
222  device_status(Device_status::DEVICE_OK_STANBY)
223  {
224  buffer = new rpLidarCircularBuffer(20000);
225  }
226 
227 
229  {
230  if (buffer)
231  {
232  delete buffer;
233  buffer = 0;
234  }
235  }
236 
237  bool open(yarp::os::Searchable& config) override;
238  bool close() override;
239  bool threadInit() override;
240  void threadRelease() override;
241  void run() override;
242 
243 public:
244  //IRangefinder2D interface
245  bool getRawData(yarp::sig::Vector &data) override;
246  bool getLaserMeasurement(std::vector<LaserMeasurementData> &data) override;
247  bool getDeviceStatus (Device_status &status) override;
248  bool getDeviceInfo (std::string &device_info) override;
249  bool getDistanceRange (double& min, double& max) override;
250  bool setDistanceRange (double min, double max) override;
251  bool getScanLimits (double& min, double& max) override;
252  bool setScanLimits (double min, double max) override;
253  bool getHorizontalResolution (double& step) override;
254  bool setHorizontalResolution (double step) override;
255  bool getScanRate (double& rate) override;
256  bool setScanRate (double rate) override;
257 
258 private:
259  bool HW_getHealth();
260  bool HW_reset();
261  bool HW_start();
262  bool HW_stop();
263  bool HW_getInfo(std::string& s_info);
264 
265 };
266 
267 #endif
rpLidarCircularBuffer::read_elems
bool read_elems(byte *elems, int size)
Definition: rpLidar.h:137
RpLidar::sensorsNum
int sensorsNum
Definition: rpLidar.h:193
RPLIDAR
const yarp::os::LogComponent & RPLIDAR()
Definition: rpLidar.cpp:45
YARP_DECLARE_LOG_COMPONENT
#define YARP_DECLARE_LOG_COMPONENT(name)
Definition: LogComponent.h:77
RpLidar::clip_min_enable
bool clip_min_enable
Definition: rpLidar.h:201
rpLidarCircularBuffer::throw_away_elem
void throw_away_elem()
Definition: rpLidar.h:114
yarp::os::Searchable
A base class for nested structures that can be searched.
Definition: Searchable.h:69
IRangefinder2D.h
Vector.h
contains the definition of a Vector type
rpLidarCircularBuffer::get_end
unsigned int get_end()
Definition: rpLidar.h:162
RpLidar
rpLidar: Documentation to be added
Definition: rpLidar.h:185
rpLidarCircularBuffer::select_elem
byte select_elem(int offset)
Definition: rpLidar.h:124
RpLidar::mutex
std::mutex mutex
Definition: rpLidar.h:190
RpLidar::pSerial
ISerialDevice * pSerial
Definition: rpLidar.h:188
yarp::dev::DeviceDriver
Interface implemented by all device drivers.
Definition: DeviceDriver.h:38
rpLidarCircularBuffer
Definition: rpLidar.h:46
yarp::dev::IRangefinder2D
A generic interface for planar laser range finders.
Definition: IRangefinder2D.h:38
RpLidar::max_distance
double max_distance
Definition: rpLidar.h:198
yarp::dev::ISerialDevice
A generic interface to serial port devices.
Definition: ISerialDevice.h:28
ControlBoardInterfaces.h
define control board standard interfaces
rpLidarCircularBuffer::throw_away_elems
void throw_away_elems(int size)
Definition: rpLidar.h:119
yarp::dev
An interface for the device drivers.
Definition: audioBufferSizeData.cpp:17
RpLidar::range_skip_vector
std::vector< Range_t > range_skip_vector
Definition: rpLidar.h:203
RpLidar::laser_data
yarp::sig::Vector laser_data
Definition: rpLidar.h:208
RpLidar::~RpLidar
~RpLidar()
Definition: rpLidar.h:228
rpLidarCircularBuffer::clear
void clear()
Definition: rpLidar.h:151
yarp::sig::VectorOf< double >
rpLidarCircularBuffer::write_elem
bool write_elem(byte elem)
Definition: rpLidar.h:68
RpLidar::clip_max_enable
bool clip_max_enable
Definition: rpLidar.h:200
rpLidarCircularBuffer::getRawData
const byte * getRawData()
Definition: rpLidar.h:58
RpLidar::do_not_clip_infinity_enable
bool do_not_clip_infinity_enable
Definition: rpLidar.h:202
RpLidar::info
std::string info
Definition: rpLidar.h:205
RpLidar::min_distance
double min_distance
Definition: rpLidar.h:197
rpLidarCircularBuffer::size
int size()
Definition: rpLidar.h:90
PolyDriver.h
Range_t::min
double min
Definition: rpLidar.h:174
yarp::dev::Range_t
Definition: Lidar2DDeviceBase.h:23
RpLidar::buffer
rpLidarCircularBuffer * buffer
Definition: rpLidar.h:191
rpLidarCircularBuffer::select_elems
void select_elems(byte *elems, int size)
Definition: rpLidar.h:129
RpLidar::device_status
Device_status device_status
Definition: rpLidar.h:206
yarp::dev::PolyDriver
A container for a device driver.
Definition: PolyDriver.h:27
RpLidar::resolution
double resolution
Definition: rpLidar.h:199
rpLidarCircularBuffer::isEmpty
bool isEmpty()
Definition: rpLidar.h:63
buffer
Definition: V4L_camera.h:75
rpLidarCircularBuffer::getMaxSize
unsigned int getMaxSize()
Definition: rpLidar.h:146
ISerialDevice.h
Semaphore.h
yarp::os::PeriodicThread
An abstraction for a periodic thread.
Definition: PeriodicThread.h:25
LogComponent.h
PeriodicThread.h
rpLidarCircularBuffer::read_elem
bool read_elem(byte *elem)
Definition: rpLidar.h:102
rpLidarCircularBuffer::write_elems
bool write_elems(byte *elems, int size)
Definition: rpLidar.h:81
yCError
#define yCError(component,...)
Definition: LogComponent.h:157
RpLidar::min_angle
double min_angle
Definition: rpLidar.h:195
yarp::os
An interface to the operating system, including Port based communication.
Definition: AbstractCarrier.h:17
rpLidarCircularBuffer::isFull
bool isFull()
Definition: rpLidar.h:53
byte
unsigned char byte
Definition: rpLidar.h:39
RpLidar::driver
PolyDriver driver
Definition: rpLidar.h:187
RpLidar::RpLidar
RpLidar(double period=0.01)
Definition: rpLidar.h:211
rpLidarCircularBuffer::get_start
unsigned int get_start()
Definition: rpLidar.h:157
RpLidar::max_angle
double max_angle
Definition: rpLidar.h:196
Range_t::max
double max
Definition: rpLidar.h:175