laser.cpp
Go to the documentation of this file.
1 /*
2 * Copyright (c) 2022, Autonics Co.,Ltd.
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 *
9 * * Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 *
12 * * Redistributions in binary form must reproduce the above
13 * copyright notice, this list of conditions and the following
14 * disclaimer in the documentation and/or other materials provided
15 * with the distribution.
16 *
17 * * Neither the name of the Autonics Co.,Ltd 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 HOLDER 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 
35 
36 
37 
38 #include <iostream>
39 #include <sstream>
40 
41 #include <self_test/self_test.h>
42 
43 #include "laser.h"
44 
45 AutoLaser::AutoLaser() : n(), priv_nh("~"), scan_msg(new sensor_msgs::LaserScan)
46 {
47  rcv_msg_flag_ = false;
48 }
49 
51 {
52 
53 }
54 
55 std::string AutoLaser::itostr(int i)
56 {
57  std::string rt_str;
58  std::stringstream ss;
59 
60  ss << i;
61  ss >> rt_str;
62 
63  return rt_str;
64 }
65 
66 std::string AutoLaser::ftostr(float i)
67 {
68  std::string rt_str;
69  std::stringstream ss;
70 
71  ss << i;
72  ss >> rt_str;
73 
74  return rt_str;
75 }
76 
77 unsigned char AutoLaser::hexToChar(unsigned int value)
78 {
79  if(value > 9 && value < 16)
80  {
81  return value + '0' + 7;
82  }
83  else if (value >= 16)
84  {
85  ROS_WARN("the value is out of range");
86  return value;
87  }
88  else
89  {
90  return value + '0';
91  }
92 }
93 
94 void AutoLaser::comSubCallback(const std_msgs::String::ConstPtr& msg)
95 {
96  std::string cmd = msg->data.c_str();
97  laserSendCommand(cmd);
98 }
99 
100 
101 void AutoLaser::login(std::string password)
102 {
103  std::string login_cmd = "SetAccessLevel";
104  login_cmd = login_cmd + " " + password;
105 
106  if(laserSendCommand(login_cmd) < 0);
107 }
108 
109 int AutoLaser::laserSendCommand(const std::string str)
110 {
111  uint16_t s_buffer_size = 0;
112  unsigned char s_buffer[64] = {0, };
113  int cmd_str_cnt = 0, str_cnt = 0;
114 
115  memset(s_buffer, 0x00, sizeof(s_buffer));
116 
117  s_buffer[str_cnt++] = 0x02;
118  str_cnt++;
119  str_cnt++;
120  str_cnt++;
121  str_cnt++;
122 
123  cmd_str_cnt = p.makeCommand(&s_buffer[str_cnt], str);
124 
125  if(cmd_str_cnt == -1)
126  {
127  return -1;
128  }
129  else
130  {
131  str_cnt += cmd_str_cnt;
132  }
133  s_buffer[str_cnt++] = 0x03;
134 
135  s_buffer[1] = hexToChar(str_cnt / 16 / 16 / 16);
136  s_buffer[2] = hexToChar(str_cnt / 16 / 16 % 16);
137  s_buffer[3] = hexToChar(str_cnt / 16 % 16);
138  s_buffer[4] = hexToChar(str_cnt % 16);
139 
140  s_buffer_size = sizeof(s_buffer);
141 
142  if(socket.clientWrite(s_buffer, s_buffer_size) < 0)
143  {
144  ROS_ERROR("Client write failed");
145  if(socket.tryReconnection() < 0)
146  {
147  return -1;
148  }
149  else
150  {
151  ROS_INFO("Reconnect successfully");
152  }
153  }
154  else
155  {
156  #ifdef PRINT_PARSE_DATA
157  std::cout << std::endl << "sent data : ";
158  for(int i = 0; i < str_cnt; i++)
159  {
160  std::cout << std::hex << s_buffer[i];
161  }
162  std::cout << "" << std::endl;
163  #endif
164 
165  usleep(1000*100);
166  }
167 
168  return 0;
169 }
170 
172 {
173  bool result = false;
174 
176  {
177 
178  }
179  else
180  {
181  result = true;
182  }
183 
184  return result;
185 }
186 
188 {
189  status.hardware_id = lsc.scan_info.model_name;
190 
191  if(!checkConnection())
192  {
193  status.summary(diagnostic_msgs::DiagnosticStatus::ERROR, "lost connection");
194  }
195  else
196  {
197  status.summary(diagnostic_msgs::DiagnosticStatus::OK, "connection is ok");
198  }
199 }
200 
202 {
203  if(!(socket.recvQueue.empty()))
204  {
205  rcv_msg_ = socket.recvQueue.front();
206  socket.recvQueue.pop();
207 
209  }
210  else
211  {
212  return -1;
213  }
214 
215  return 0;
216 }
217 
218 int AutoLaser::getLaserData(sensor_msgs::LaserScan::Ptr p_laser_scan)
219 {
220  if(!(socket.recvQueue.empty()))
221  {
222  rcv_msg_ = socket.recvQueue.front();
223  socket.recvQueue.pop();
224 
225  p.parsingMsg(rcv_msg_, p_laser_scan, &lsc);
226  if(p_laser_scan->ranges.empty() == false)
227  {
228  p_laser_scan->header.stamp = ros::Time::now();
229 
230  rcv_msg_flag_ = true;
231  }
232  }
233  else
234  {
235  rcv_msg_flag_ = false;
236  }
237 
238  return 0;
239 }
240 
242 {
243  if(socket.getRcvTimeout())
244  {
245  if(socket.tryReconnection() >= 0)
246  {
247  ROS_INFO("Reconnect successfully");
248  laserSendCommand("SensorStart");
249  }
250  else
251  {
252 
253  }
254  }
255 }
256 
258 {
259  std::string addr = "192.168.0.1", frame_id = "laser", port = "8000", password = "0000";
260  double range_min = 0.05, range_max = 25;
261  uint16_t port_num = 0, recnt_cnt = 0;
262  topic_name = "scan";
263 
264  priv_nh.getParam("addr", addr);
265  priv_nh.getParam("frame_id", frame_id);
266  priv_nh.getParam("port", port);
267  priv_nh.getParam("range_min", range_min);
268  priv_nh.getParam("range_max", range_max);
269  priv_nh.getParam("password", password);
270  priv_nh.getParam("topic_name", topic_name);
271 
272  scan_msg->header.frame_id = frame_id;
273  scan_msg->range_min = range_min;
274  scan_msg->range_max = range_max;
275 
276  pub_scan = n.advertise<sensor_msgs::LaserScan>(topic_name, 1000);
277  sub_cmd = n.subscribe<std_msgs::String>("command", 10, &AutoLaser::comSubCallback, this);
278 
279  if(socket.clientOpen(addr, port) < 0)
280  {
281  ROS_WARN("Failed to connect to server");
282  while(true)
283  {
284  recnt_cnt++;
285  if(socket.tryReconnection() < 0)
286  {
287  ROS_WARN("Failed to reconnect to server");
288  if(recnt_cnt >= CONNECT_ATTEMPT_CNT)
289  {
290  ROS_ERROR("Tried to connect server %d times but, Failed to connect to server", recnt_cnt);
291  return -1;
292  }
293  if(!ros::ok())
294  {
295  return -1;
296  }
297  }
298  else
299  {
300  break;
301  }
302  }
303  }
304 
305  laserSendCommand("SensorScanInfo");
306  getLscData();
307 
308  login(password);
309 
310  return 0;
311 }
312 
313 int AutoLaser::run(void)
314 {
315  ros::Rate loop_rate(150);
317 
318  self_test.setID("self_test");
319  self_test.add("Connection", this, &AutoLaser::selfTest);
320 
321  double min_freq = 0.00025; // Hz
322  double max_freq = 15.0; // Hz
325 
326  laserSendCommand("SensorStart");
327 
328  while(ros::ok())
329  {
331 
332  if(rcv_msg_flag_)
333  {
335  scan_msg->ranges.clear();
336  scan_msg->intensities.clear();
337 
338  pub_freq.tick();
340  }
341 
342  self_test.checkTest();
343 
345 
346  ros::spinOnce();
347  loop_rate.sleep();
348  }
349 
350  return 0;
351 }
352 
AutoLaser::topic_name
std::string topic_name
Definition: laser.h:93
AutoLaser::AutoLaser
AutoLaser()
Definition: laser.cpp:45
AutoLaser::laserSendCommand
int laserSendCommand(const std::string str)
Definition: laser.cpp:109
AutoLaser::ftostr
std::string ftostr(float i)
Definition: laser.cpp:66
msg
msg
Lsc_t::scan_info
ScanInfo scan_info
Definition: parser.hpp:66
AutoLaser::p
Parser p
Definition: laser.h:82
AutoLaser::rcv_msg_
std::vector< unsigned char > rcv_msg_
Definition: laser.h:91
AutoLaser::scan_msg
sensor_msgs::LaserScan::Ptr scan_msg
Definition: laser.h:87
diagnostic_updater::HeaderlessTopicDiagnostic::tick
virtual void tick()
AutoLaser::pub_scan
ros::Publisher pub_scan
Definition: laser.h:84
AutoLaser::getLaserData
int getLaserData(sensor_msgs::LaserScan::Ptr scan_msg)
Definition: laser.cpp:218
AutoLaser::n
ros::NodeHandle n
Definition: laser.h:77
AutoLaser::comSubCallback
void comSubCallback(const std_msgs::String::ConstPtr &msg)
Definition: laser.cpp:94
ros::NodeHandle::getParam
bool getParam(const std::string &key, bool &b) const
Socket::tryReconnection
int tryReconnection(void)
Definition: socket.cpp:110
AutoLaser::itostr
std::string itostr(int i)
Definition: laser.cpp:55
AutoLaser::rcv_msg_flag_
bool rcv_msg_flag_
Definition: laser.h:90
ros::spinOnce
ROSCPP_DECL void spinOnce()
AutoLaser::run
int run(void)
Definition: laser.cpp:313
AutoLaser::priv_nh
ros::NodeHandle priv_nh
Definition: laser.h:78
ros::Publisher::publish
void publish(const boost::shared_ptr< M > &message) const
ros::NodeHandle::advertise
Publisher advertise(AdvertiseOptions &ops)
self_test.h
ros::ok
ROSCPP_DECL bool ok()
self_test
laser.h
Parser::parsingMsg
void parsingMsg(std::vector< unsigned char > raw_msg, sensor_msgs::LaserScan::Ptr msg, Lsc_t *lsc)
Definition: parser.cpp:144
diagnostic_updater::DiagnosticStatusWrapper::summary
void summary(const diagnostic_msgs::DiagnosticStatus &src)
AutoLaser::hexToChar
unsigned char hexToChar(unsigned int value)
Definition: laser.cpp:77
Socket::getRcvTimeout
bool getRcvTimeout(void)
Definition: socket.cpp:88
AutoLaser::watchingDisconnection
void watchingDisconnection(void)
Definition: laser.cpp:241
CONNECT_ATTEMPT_CNT
#define CONNECT_ATTEMPT_CNT
Definition: laser.h:50
ROS_WARN
#define ROS_WARN(...)
self_test::TestRunner
ros::NodeHandle::subscribe
Subscriber subscribe(const std::string &topic, uint32_t queue_size, const boost::function< void(C)> &callback, const VoidConstPtr &tracked_object=VoidConstPtr(), const TransportHints &transport_hints=TransportHints())
Socket::getRcvError
bool getRcvError(void)
Definition: socket.cpp:93
AutoLaser::selfTest
void selfTest(diagnostic_updater::DiagnosticStatusWrapper &status)
Definition: laser.cpp:187
diagnostic_updater::HeaderlessTopicDiagnostic
AutoLaser::laserInit
int laserInit(void)
Definition: laser.cpp:257
Socket::clientWrite
int32_t clientWrite(unsigned char *buffer, uint16_t buf_size)
Definition: socket.cpp:368
diagnostic_updater::Updater::setHardwareID
void setHardwareID(const std::string &hwid)
ros::Rate::sleep
bool sleep()
AutoLaser::getLscData
int getLscData(void)
Definition: laser.cpp:201
AutoLaser::diagnostic_topic_updater
diagnostic_updater::Updater diagnostic_topic_updater
Definition: laser.h:79
diagnostic_updater::Updater::update
void update()
Parser::makeCommand
int makeCommand(unsigned char *buf, std::string cmd)
Definition: parser.cpp:81
AutoLaser::~AutoLaser
~AutoLaser()
Definition: laser.cpp:50
ROS_ERROR
#define ROS_ERROR(...)
diagnostic_updater::DiagnosticStatusWrapper
ros::Rate
Socket::recvQueue
std::queue< std::vector< unsigned char > > recvQueue
Definition: socket.hpp:79
ROS_INFO
#define ROS_INFO(...)
sensor_msgs
Socket::clientOpen
int clientOpen(std::string addr, std::string port)
Definition: socket.cpp:282
AutoLaser::sub_cmd
ros::Subscriber sub_cmd
Definition: laser.h:85
AutoLaser::lsc
Lsc_t lsc
Definition: laser.h:88
AutoLaser::socket
Socket socket
Definition: laser.h:81
ScanInfo::model_name
std::string model_name
Definition: parser.hpp:49
AutoLaser::login
void login(std::string password)
Definition: laser.cpp:101
ros::Time::now
static Time now()
diagnostic_updater::FrequencyStatusParam
AutoLaser::checkConnection
bool checkConnection()
Definition: laser.cpp:171


lsc_ros_driver
Author(s): Autonics-lidar
autogenerated on Sat Jan 14 2023 03:18:24