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 //---------------------------------------------------------------------------------------------------------------
179 
181 {
182 protected:
185 
186  std::mutex mutex;
188 
190 
191  double min_angle;
192  double max_angle;
193  double min_distance;
194  double max_distance;
195  double resolution;
199  std::vector <Range_t> range_skip_vector;
200 
201  std::string info;
203 
205 
206 public:
207  RpLidar(double period = 0.01) : PeriodicThread(period),
208  pSerial(nullptr),
209  sensorsNum(0),
210  min_angle(0.0),
211  max_angle(0.0),
212  min_distance(0.0),
213  max_distance(0.0),
214  resolution(0.0),
215  clip_max_enable(false),
216  clip_min_enable(false),
217  do_not_clip_infinity_enable(false),
218  device_status(Device_status::DEVICE_OK_STANBY)
219  {
220  buffer = new rpLidarCircularBuffer(20000);
221  }
222 
223 
225  {
226  if (buffer)
227  {
228  delete buffer;
229  buffer = 0;
230  }
231  }
232 
233  bool open(yarp::os::Searchable& config) override;
234  bool close() override;
235  bool threadInit() override;
236  void threadRelease() override;
237  void run() override;
238 
239 public:
240  //IRangefinder2D interface
241  bool getRawData(yarp::sig::Vector &data) override;
242  bool getLaserMeasurement(std::vector<LaserMeasurementData> &data) override;
243  bool getDeviceStatus (Device_status &status) override;
244  bool getDeviceInfo (std::string &device_info) override;
245  bool getDistanceRange (double& min, double& max) override;
246  bool setDistanceRange (double min, double max) override;
247  bool getScanLimits (double& min, double& max) override;
248  bool setScanLimits (double min, double max) override;
249  bool getHorizontalResolution (double& step) override;
250  bool setHorizontalResolution (double step) override;
251  bool getScanRate (double& rate) override;
252  bool setScanRate (double rate) override;
253 
254 private:
255  bool HW_getHealth();
256  bool HW_reset();
257  bool HW_start();
258  bool HW_stop();
259  bool HW_getInfo(std::string& s_info);
260 
261 };
262 
263 #endif
define control board standard interfaces
contains the definition of a Vector type
double max_distance
Definition: rpLidar.h:194
yarp::sig::Vector laser_data
Definition: rpLidar.h:204
RpLidar(double period=0.01)
Definition: rpLidar.h:207
std::string info
Definition: rpLidar.h:201
bool do_not_clip_infinity_enable
Definition: rpLidar.h:198
std::mutex mutex
Definition: rpLidar.h:186
bool clip_max_enable
Definition: rpLidar.h:196
std::vector< Range_t > range_skip_vector
Definition: rpLidar.h:199
~RpLidar()
Definition: rpLidar.h:224
PolyDriver driver
Definition: rpLidar.h:183
Device_status device_status
Definition: rpLidar.h:202
bool clip_min_enable
Definition: rpLidar.h:197
int sensorsNum
Definition: rpLidar.h:189
double resolution
Definition: rpLidar.h:195
ISerialDevice * pSerial
Definition: rpLidar.h:184
rpLidarCircularBuffer * buffer
Definition: rpLidar.h:187
double min_angle
Definition: rpLidar.h:191
double min_distance
Definition: rpLidar.h:193
double max_angle
Definition: rpLidar.h:192
byte select_elem(int offset)
Definition: rpLidar.h:124
void throw_away_elem()
Definition: rpLidar.h:114
bool read_elem(byte *elem)
Definition: rpLidar.h:102
bool write_elems(byte *elems, int size)
Definition: rpLidar.h:81
unsigned int get_start()
Definition: rpLidar.h:157
void throw_away_elems(int size)
Definition: rpLidar.h:119
bool write_elem(byte elem)
Definition: rpLidar.h:68
bool read_elems(byte *elems, int size)
Definition: rpLidar.h:137
unsigned int getMaxSize()
Definition: rpLidar.h:146
unsigned int get_end()
Definition: rpLidar.h:162
void select_elems(byte *elems, int size)
Definition: rpLidar.h:129
const byte * getRawData()
Definition: rpLidar.h:58
Interface implemented by all device drivers.
Definition: DeviceDriver.h:38
A generic interface for planar laser range finders.
A generic interface to serial port devices.
Definition: ISerialDevice.h:28
A container for a device driver.
Definition: PolyDriver.h:27
An abstraction for a periodic thread.
A base class for nested structures that can be searched.
Definition: Searchable.h:69
#define yCError(component,...)
Definition: LogComponent.h:157
#define YARP_DECLARE_LOG_COMPONENT(name)
Definition: LogComponent.h:77
An interface for the device drivers.
An interface to the operating system, including Port based communication.
unsigned char byte
Definition: rpLidar.h:39
const yarp::os::LogComponent & RPLIDAR()
Definition: rpLidar.cpp:45
double min
Definition: rpLidar.h:174
double max
Definition: rpLidar.h:175