ls01b.cpp
Go to the documentation of this file.
1 /*******************************************************
2 @company: Copyright (C) 2018, Leishen Intelligent System
3 @product: LS01B
4 @filename: ls01b.cpp
5 @brief:
6 @version: date: author: comments:
7 @v1.0 18-8-21 fu new
8 *******************************************************/
9 #include "ls01b_v2/ls01b.h"
10 #include <stdio.h>
11 
12 namespace ls{
13 LS01B * LS01B::instance(std::string port, int baud_rate, double resolution)
14 {
15  static LS01B obj(port, baud_rate, resolution);
16  return &obj;
17 }
18 
19 LS01B::LS01B(std::string port, int baud_rate, double resolution)
20 {
21  scan_health_ = 0;
22  resolution_ = resolution;
23  is_shutdown_ = false;
24  is_start_ = false;
25  use_angle_ = true;
26 
27  data_len_ = 180;
28  points_size_ = 360/resolution;
29  scan_points_.resize(points_size_);
30 
31  serial_ = LSIOSR::instance(port, baud_rate);
32  serial_->init();
33  recv_thread_ = new boost::thread(boost::bind(&LS01B::recvThread, this));
34 }
35 
37 {
38  printf("start ls01b\n");
39  is_shutdown_ = true;
40  recv_thread_->interrupt();
41  recv_thread_->join();
42 
43  recv_thread_ = NULL;
44  delete recv_thread_;
45 
46  serial_->close();
47  serial_ = NULL;
48  delete serial_;
49  printf("end ls01b\n");
50 }
51 
53 {
54  return (scan_health_ < 0) ? false : true;
55 }
56 
58 {
59  scan_health_ = 0;
60 }
61 
62 int LS01B::getScan(std::vector<ScanPoint> &points, ros::Time &scan_time, float &scan_duration)
63 {
64  // boost::unique_lock<boost::mutex> lock(mutex_);
65  points.assign(scan_points_bak_.begin(), scan_points_bak_.end());
66  scan_time = pre_time_;
67 
68  scan_duration = (time_ - pre_time_).toSec();
69  // ROS_INFO("scan_duration = %f", scan_duration);
70 }
71 
72 int LS01B::getVersion(std::string &version)
73 {
74  version = "ls01b_v2_0";
75  return 0;
76 }
77 
78 double LS01B::getRPM()
79 {
80  return real_rpm_;
81 }
82 
84 {
85  printf("startScan\n");
86  char data[3];
87  data[0] = 0xa5;
88  data[1] = 0x2c;
89  data[2] = 0x0;
90  int rtn = serial_->send((const char*)data, 2);
91  if (rtn < 0)
92  {
93  printf("start scan error !\n");
94  scan_health_ = -1;
95  return rtn;
96  }
97  sleep(1.0);
98 
99  return rtn;
100 }
101 
103 {
104  printf("stopScan\n");
105  char data[2];
106  data[0] = 0xa5;
107  data[1] = 0x25;
108  int rtn = serial_->send((const char *)data, 2);
109  if (rtn < 0)
110  {
111  printf("stop scan error !\n");
112  scan_health_ = -1;
113  return rtn;
114  }
115  is_start_ = false;
116  sleep(1.0);
117  return rtn;
118 }
119 
120 int LS01B::setScanMode(bool is_continuous)
121 {
122  printf("setScanMode is_continuous = %d\n", is_continuous);
123  char data[2];
124  data[0] = 0xa5;
125  data[1] = 0x0;
126 
127  if (is_continuous){
128  data[1] = 0x20;
129  }
130  else{
131  data[1] = 0x22;
132  }
133 
134  int rtn = serial_->send((const char *)data, 2);
135  if (rtn < 0)
136  {
137  printf("setScanMode error !\n");
138  scan_health_ = -1;
139  return rtn;
140  }
141  is_start_ = true;
142  sleep(1.0);
143  return rtn;
144 }
145 
147 {
148  printf("stopRecvData\n");
149  char data[2];
150  data[0] = 0xa5;
151  data[1] = 0x21;
152 
153  int rtn = serial_->send((const char *)data, 2);
154  if (rtn < 0)
155  {
156  printf("stopRecvData error !\n");
157  scan_health_ = -1;
158  return rtn;
159  }
160  is_start_ = false;
161  sleep(1.0);
162  return rtn;
163 }
164 
165 int LS01B::switchData(bool use_angle)
166 {
167  printf("switchData use_angle = %d\n", use_angle);
168  char data[4];
169  data[0] = 0xa5;
170  data[1] = 0x0;
171  data[2] = 0x0;
172  data[3] = 0x0;
173 
174  if (use_angle){
175  data[1] = 0x5c;
176  }
177  else{
178  data[1] = 0x50;
179  }
180 
181  int rtn = serial_->send((const char *)data, 4);
182  if (rtn < 0)
183  {
184  printf("switchData error !\n");
185  scan_health_ = -1;
186  return rtn;
187  }
188  is_start_ = false;
189  use_angle_ = use_angle;
190  sleep(1.0);
191  return rtn;
192 }
193 
195 {
196  printf("setMotorSpeed rpm = %d\n", rpm);
197 
198  char data[4];
199  data[0] = 0xa5;
200  data[1] = 0x26;
201  data[3] = (rpm & 0xff);
202  data[2] = (rpm >> 8) & 0xff;
203 
204  int rtn = serial_->send((const char *)data, 4);
205  if (rtn < 0)
206  {
207  printf("setMotorSpeed error !\n");
208  scan_health_ = -1;
209  return rtn;
210  }
211  is_start_ = false;
212  sleep(1.0);
213  return rtn;
214 }
215 
216 int LS01B::setResolution(double resolution)
217 {
218  printf("setResolution resolution = %f\n", resolution);
219  char data[4];
220  data[0] = 0xa5;
221  data[1] = 0x30;
222  data[2] = 0x00;
223 
224  if (resolution == 0.25){
225  data[3] = 0x19;
226  data_len_ = 180;
227  }
228  else if (resolution == 0.5){
229  data[3] = 0x32;
230  data_len_ = 90;
231  }
232  else if (resolution == 1.0){
233  data[3] = 0x64;
234  data_len_ = 45;
235  }
236  else
237  {
238  // resolution error
239  scan_health_ = -2;
240  return scan_health_;
241  }
242 
243  resolution_ = resolution;
244  points_size_ = 360/resolution;
245  scan_points_.resize(points_size_);
246 
247 
248  int rtn = serial_->send((const char *)data, 4);
249  if (rtn < 0)
250  {
251  printf("setResolution error !\n");
252  scan_health_ = -1;
253  return rtn;
254  }
255  is_start_ = false;
256  sleep(1.0);
257 
258  return rtn;
259 }
260 
262 {
263 
264  uint8_t start_count = 0;
265  char header[6];
266  uint8_t temp_char;
267  char * packet_bytes = new char[data_len_];
268  if (packet_bytes == NULL)
269  {
270  packet_bytes = NULL;
271  // printf("new char [data_len_] error \n");
272  }
273 
274  boost::posix_time::ptime t1,t2;
275  t1 = boost::posix_time::microsec_clock::universal_time();
276  while(!is_shutdown_){
277  while(!is_start_){
278  usleep(300000);
279  }
280 
281  int count = serial_->read(&header[start_count], 1);
282  if (count<=0)
283  {
284  // printf("read header[%d] error\n", start_count);
285  start_count = 0;
286  scan_health_ = -3;
287  continue;
288  }
289 
290  if(0 == start_count){
291  if (0xA5 == (header[start_count]&0xff)){
292  start_count = 1;
293  }
294  else{
295  start_count = 0;
296  }
297  }
298  else if (1 == start_count) {
299  if (0x6A == (header[start_count]&0xff) || 0x5A == (header[start_count]&0xff))
300  {
301  if (0x6A == (header[start_count]&0xff))
302  {
303  t2 = boost::posix_time::microsec_clock::universal_time();
304  boost::posix_time::millisec_posix_time_system_config::time_duration_type t_elapse;
305  t_elapse = t2 - t1;
306  real_rpm_ = 1000000.0 / t_elapse.ticks();
307 
308  t1 = t2;
309 
310  boost::unique_lock<boost::mutex> lock(mutex_);
311  scan_points_bak_.resize(scan_points_.size());
312  scan_points_bak_.assign(scan_points_.begin(), scan_points_.end());
313  pre_time_ = time_;
314  lock.unlock();
315 
316  time_ = ros::Time::now();
317  }
318 
319  start_count = 2;
320  int count = serial_->read(&header[start_count], 4);
321  if (count != 4)
322  {
323  // printf("read header error\n");
324  start_count = 0;
325  scan_health_ = -3;
326  continue;
327  }
328 
329  rpm_ = ((header[2]&0x7f) << 8) + (header[3]&0xff);
330  int flag = ((header[2] & 0x80) == 0) ? 0 : 1;
331  int angular_resolution = (header[4]&0xff) >> 1;
332  int start_angle = ((header[4] & 0x01) << 8) + (header[5]&0xff);
333 
334 
335 
336  count = serial_->read(packet_bytes, data_len_);
337  if (count != data_len_)
338  {
339  // printf("read %d packet error\n", data_len_);
340  start_count = 0;
341  scan_health_ = -3;
342  continue;
343  }
344 
345  for (int i = 0; i < data_len_; i = i+3)
346  {
347  // printf("%02X %02X %02X \n", (packet_bytes[i]) & 0xFF, (packet_bytes[i+1]) & 0xFF, (packet_bytes[i+2]) & 0xFF);
348  int idx = start_angle / resolution_ + i/3;
349  double distance = ((packet_bytes[i+1] & 0xFF) << 8) | (packet_bytes[i+2] & 0xFF);
350  // double degree = start_angle + (packet_bytes[i] & 0xFF )* resolution_;
351  double degree = start_angle + idx * resolution_;
352 
353  boost::unique_lock<boost::mutex> lock(mutex_);
354  scan_points_[idx].degree = degree;
355  scan_points_[idx].range = distance/1000.0;
356  if(use_angle_)
357  {
358  scan_points_[idx].intensity = 0;
359  }
360  else
361  {
362  scan_points_[idx].intensity = packet_bytes[i] & 0xFF;
363  // printf("%f ", scan_points_[idx].intensity);
364  }
365  lock.unlock();
366  }
367  // printf("\n");
368 
369  start_count = 0;
370 
371  }
372  else{
373  start_count = 0;
374  }
375 
376  }
377  }
378 
379  if (packet_bytes)
380  {
381  packet_bytes = NULL;
382  delete packet_bytes;
383  }
384 }
385 }
386 
Definition: ls01b.h:17
ros::Time time_
Definition: ls01b.h:137
int close()
Definition: lsiosr.cpp:402
bool is_shutdown_
Definition: ls01b.h:124
double real_rpm_
Definition: ls01b.h:134
bool isHealth()
Definition: ls01b.cpp:52
int read(char *buffer, int length, int timeout=30)
Definition: lsiosr.cpp:127
static LS01B * instance(std::string port, int baud_rate, double resolution)
Definition: ls01b.cpp:13
bool use_angle_
Definition: ls01b.h:126
std_msgs::Header * header(M &m)
static LSIOSR * instance(std::string name, int speed, int fd=0)
Definition: lsiosr.cpp:13
int setMotorSpeed(int rpm)
Definition: ls01b.cpp:194
double getRPM()
Definition: ls01b.cpp:78
int stopScan()
Definition: ls01b.cpp:102
double resolution_
Definition: ls01b.h:129
LS01B(std::string port, int baud_rate, double resolution=0.25)
Definition: ls01b.cpp:19
int switchData(bool use_angle)
Definition: ls01b.cpp:165
bool resetHealth()
Definition: ls01b.cpp:57
int init()
Definition: lsiosr.cpp:381
ros::Time pre_time_
Definition: ls01b.h:136
int data_len_
Definition: ls01b.h:130
int getScan(std::vector< ScanPoint > &points, ros::Time &scan_time, float &scan_duration)
Definition: ls01b.cpp:62
bool is_start_
Definition: ls01b.h:125
int getVersion(std::string &version)
Definition: ls01b.cpp:72
boost::thread * recv_thread_
Definition: ls01b.h:121
int setScanMode(bool is_continuous)
Definition: ls01b.cpp:120
void recvThread()
Definition: ls01b.cpp:261
std::vector< ScanPoint > scan_points_
Definition: ls01b.h:116
LSIOSR * serial_
Definition: ls01b.h:120
int setResolution(double resolution)
Definition: ls01b.cpp:216
boost::mutex mutex_
Definition: ls01b.h:122
int stopRecvData()
Definition: ls01b.cpp:146
static Time now()
int rpm_
Definition: ls01b.h:133
std::vector< ScanPoint > scan_points_bak_
Definition: ls01b.h:117
int points_size_
Definition: ls01b.h:132
int scan_health_
Definition: ls01b.h:128
int send(const char *buffer, int length, int timeout=30)
Definition: lsiosr.cpp:299
int startScan()
Definition: ls01b.cpp:83
~LS01B()
Definition: ls01b.cpp:36


ls01b_v2
Author(s): fu
autogenerated on Sat Sep 28 2019 03:51:19