lds_driver.cpp
Go to the documentation of this file.
1 /*******************************************************************************
2 * Copyright (c) 2016, Hitachi-LG Data Storage
3 * Copyright (c) 2017, ROBOTIS
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions are met:
8 *
9 * * Redistributions of source code must retain the above copyright notice, this
10 * list of conditions and the following disclaimer.
11 *
12 * * Redistributions in binary form must reproduce the above copyright notice,
13 * this list of conditions and the following disclaimer in the documentation
14 * and/or other materials provided with the distribution.
15 *
16 * * Neither the name of the copyright holder nor the names of its
17 * contributors may be used to endorse or promote products derived from
18 * this software without specific prior written permission.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
23 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
26 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
27 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
28 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 *******************************************************************************/
31 
32  /* Authors: SP Kong, JH Yang, Pyo */
33  /* maintainer: Pyo */
34 
35 #include "lds_driver.h"
36 
37 namespace lds
38 {
39 LFCDLaser::LFCDLaser(const std::string& port, uint32_t baud_rate, boost::asio::io_service& io)
40  : port_(port), baud_rate_(baud_rate), shutting_down_(false), serial_(io, port_)
41 {
42  serial_.set_option(boost::asio::serial_port_base::baud_rate(baud_rate_));
43 
44  // Below command is not required after firmware upgrade (2017.10)
45  boost::asio::write(serial_, boost::asio::buffer("b", 1)); // start motor
46 }
47 
49 {
50  boost::asio::write(serial_, boost::asio::buffer("e", 1)); // stop motor
51 }
52 
54 {
55  uint8_t temp_char;
56  uint8_t start_count = 0;
57  bool got_scan = false;
58  boost::array<uint8_t, 2520> raw_bytes;
59  uint8_t good_sets = 0;
60  uint32_t motor_speed = 0;
61  rpms=0;
62  int index;
63 
64  while (!shutting_down_ && !got_scan)
65  {
66  // Wait until first data sync of frame: 0xFA, 0xA0
67  boost::asio::read(serial_, boost::asio::buffer(&raw_bytes[start_count],1));
68 
69  if(start_count == 0)
70  {
71  if(raw_bytes[start_count] == 0xFA)
72  {
73  start_count = 1;
74  }
75  }
76  else if(start_count == 1)
77  {
78  if(raw_bytes[start_count] == 0xA0)
79  {
80  start_count = 0;
81 
82  // Now that entire start sequence has been found, read in the rest of the message
83  got_scan = true;
84 
85  boost::asio::read(serial_,boost::asio::buffer(&raw_bytes[2], 2518));
86 
87  // scan->angle_min = 0.0;
88  // scan->angle_max = 2.0*M_PI;
89  // scan->angle_increment = (2.0*M_PI/360.0);
90  // scan->range_min = 0.12;
91  // scan->range_max = 3.5;
92  // scan->ranges.resize(360);
93  // scan->intensities.resize(360);
94 
95  //read data in sets of 6
96  for(uint16_t i = 0; i < raw_bytes.size(); i=i+42)
97  {
98  if(raw_bytes[i] == 0xFA && raw_bytes[i+1] == (0xA0 + i / 42)) //&& CRC check
99  {
100  good_sets++;
101  motor_speed += (raw_bytes[i+3] << 8) + raw_bytes[i+2]; //accumulate count for avg. time increment
102  rpms=(raw_bytes[i+3]<<8|raw_bytes[i+2])/10;
103 
104  for(uint16_t j = i+4; j < i+40; j=j+6)
105  {
106  index = 6*(i/42) + (j-4-i)/6;
107 
108  // Four bytes per reading
109  uint8_t byte0 = raw_bytes[j];
110  uint8_t byte1 = raw_bytes[j+1];
111  uint8_t byte2 = raw_bytes[j+2];
112  uint8_t byte3 = raw_bytes[j+3];
113 
114  // Remaining bits are the range in mm
115  uint16_t intensity = (byte1 << 8) + byte0;
116 
117  // Last two bytes represent the uncertanty or intensity, might also be pixel area of target...
118  // uint16_t intensity = (byte3 << 8) + byte2;
119  uint16_t range = (byte3 << 8) + byte2;
120 
121  // scan->ranges[359-index] = range / 1000.0;
122  // scan->intensities[359-index] = intensity;
123  printf ("r[%d]=%f,",359-index, range / 1000.0);
124  }
125  }
126  }
127 
128  // scan->time_increment = motor_speed/good_sets/1e8;
129  }
130  else
131  {
132  start_count = 0;
133  }
134  }
135  }
136 }
137 }
138 
139 int main(int argc, char **argv)
140 {
141  std::string port;
142  int baud_rate;
143  uint16_t rpms;
144  port = "/dev/ttyUSB0";
145  baud_rate = 230400;
146  boost::asio::io_service io;
147 
148  try
149  {
150  lds::LFCDLaser laser(port, baud_rate, io);
151 
152  while (1)
153  {
154  laser.poll();
155  }
156  laser.close();
157 
158  return 0;
159  }
160  catch (boost::system::system_error ex)
161  {
162  printf("An exception was thrown: %s", ex.what());
163  return -1;
164  }
165 }
boost::asio::serial_port serial_
Actual serial port object for reading/writing to the LFCD Laser Scanner.
Definition: lds_driver.h:73
uint32_t baud_rate_
The baud rate for the serial connection.
Definition: lds_driver.h:71
int main(int argc, char **argv)
Definition: lds_driver.cpp:139
LFCDLaser(const std::string &port, uint32_t baud_rate, boost::asio::io_service &io)
Constructs a new LFCDLaser attached to the given serial port.
Definition: lds_driver.cpp:39
~LFCDLaser()
Default destructor.
Definition: lds_driver.cpp:48
bool shutting_down_
Flag for whether the driver is supposed to be shutting down or not.
Definition: lds_driver.h:72
void close()
Close the driver down and prevent the polling loop from advancing.
Definition: lds_driver.h:67
uint16_t rpms
RPMS derived from the rpm bytes in an LFCD packet.
Definition: lds_driver.h:44
void poll()
Poll the laser to get a new scan. Blocks until a complete new scan is received or close is called...
Definition: lds_driver.cpp:53


hls-lfcd-lds-driver
Author(s): Pyo , Darby Lim , Gilbert , Will Son , JH Yang, SP Kong
autogenerated on Fri Apr 16 2021 02:20:09