parser.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 #include <queue>
37 #include "parser.hpp"
38 
39 #define NUM_OF_CMD_TYPE 4
40 #define NUM_OF_CMD 7
41 
42 #define NUMBER_OF_PARAM 13
43 #define MAX_NUM_OF_PACKET_PARAM 16
44 
45 #define INTENSITY_MAX 50000.0
46 
47 
49 {"sMC",
50 "sMA",
51 "sRC",
52 "sRA",
53 };
54 
55 const char* cmd_list[NUM_OF_CMD] =
56 {"None",
57 "SetAccessLevel",
58 "SensorScanInfo",
59 "SensorStart",
60 "SensorStop",
61 "ScanData",
62 "FirstConnectDummySend"
63 };
64 
66 {
67  NONE = 0,
74 };
75 
77 {
78 
79 }
80 
81 int Parser::makeCommand(unsigned char* buf, std::string cmd)
82 {
83  std::vector<unsigned char> v_cmd_frame;
84  int cnt = 0, cmd_num = 0;
85  std::string target = " ";
86  std::string rep = ",";
87  std::string temp_str;
88 
89  // replacing " " to ","
90  for(int i = 0; cmd.find(target) != std::string::npos && i < MAX_NUM_OF_PACKET_PARAM; i++)
91  {
92  cmd.replace(cmd.find(target), target.length(), rep);
93  }
94 
95  temp_str = cmd;
96  char* ptr_tok_cmd = strtok(&temp_str[0], ",");
97 
98  // searching matched cmd
99  for(int i = 0; i < NUM_OF_CMD; i++)
100  {
101  if(strcmp(cmd_list[i], ptr_tok_cmd) == 0)
102  {
103  cmd_num = i;
104  break;
105  }
106  }
107 
108  v_cmd_frame.push_back(',');
109 
110  switch(cmd_num)
111  {
112  case SET_ACCESS_LEVEL :
113  case SENSOR_START :
114  case SENSOR_STOP :
115  v_cmd_frame.push_back('s');
116  v_cmd_frame.push_back('M');
117  v_cmd_frame.push_back('C');
118  break;
119 
120  case SENSOR_SCAN_INFO :
121  v_cmd_frame.push_back('s');
122  v_cmd_frame.push_back('R');
123  v_cmd_frame.push_back('C');
124  break;
125 
126  case NONE :
127  default :
128  std::cout << "invalid command, type correct command " << cmd_num << std::endl;
129  return -1;
130  break;
131  }
132  v_cmd_frame.push_back(',');
133 
134  for(int i = 0; i < cmd.size() + 1; i++)
135  {
136  v_cmd_frame.push_back(cmd[i]);
137  }
138 
139  memcpy(buf, &v_cmd_frame[0], v_cmd_frame.size());
140 
141  return v_cmd_frame.size() - 1;
142 }
143 
144 void Parser::parsingMsg(std::vector<unsigned char> raw_msg, sensor_msgs::LaserScan::Ptr msg, Lsc_t* lsc)
145 {
146  uint16_t msg_size = 0, index= 0, field_size = 0, index_base = 0;
147  int cmd_type = 0, cmd = 0, version = 0, status = 0;
148  float chk_intensity = 0;
149  std::vector<char *> field;
150  char* ptr;
151 
152  if(raw_msg[0] == 0x02)
153  {
154  ptr = strtok((char*) &raw_msg[1], ",");
155 
156  if(raw_msg[strtoul(ptr,NULL, 16) - 1] == 0x03)
157  {
158  raw_msg[strtoul(ptr, NULL, 16) - 1] = '\0';
159  }
160  else
161  {
162  for(int i = 0; i < raw_msg.size(); i++)
163  {
164  std::cout << raw_msg[i] <<std::endl;
165  }
166  return;
167  }
168 
169  while(ptr != NULL)
170  {
171  field.push_back(ptr);
172  ptr = strtok(NULL, ",");
173  }
174 
175  // get packet size
176  msg_size = strtoul(field[index++], NULL, 16);
177  field_size = field.size();
178 
179  // get command type
180  for(int i = 0; i < NUM_OF_CMD_TYPE; i++)
181  {
182  int temp_ret = strcmp(field[index], cmd_type_list[i]);
183 
184  if(temp_ret == 0)
185  {
186  cmd_type = i;
187  break;
188  }
189  }
190  index++;
191 
192  // get command
193  for(int i = 0; i < NUM_OF_CMD; i++)
194  {
195  int temp_ret = strcmp(field[index], cmd_list[i]);
196 
197  if(temp_ret == 0)
198  {
199  cmd = i;
200  break;
201  }
202  }
203  index++;
204 
205  switch(cmd)
206  {
207  case SENSOR_SCAN_INFO :
208  lsc->scan_info.fw_ver = strtoul(field[8], NULL, 16); // fw_ver
209  lsc->scan_info.model_name = field[13]; // model name
210 
211  #ifdef PRINT_PARSE_DATA
212  std::cout << "Command : " << cmd_list[cmd] << std::endl;
213  std::cout << "fw_ver : " << lsc->scan_info.fw_ver << std::endl;
214  std::cout << "Model_name : " << lsc->scan_info.model_name << std::endl;
215  #endif
216 
217  break;
218 
219  case SCAN_DATA :
220  for(int i = 0; i < NUMBER_OF_PARAM; i++)
221  {
222  ptr = strtok(NULL, ",");
223  field.push_back(ptr);
224  }
225  lsc->scan_mea.scan_counter = strtoul(field[7], NULL, 16); // scan counter
226  lsc->scan_mea.scan_freq = strtoul(field[10], NULL, 16); // scan freq
227  lsc->scan_mea.meas_freq = strtoul(field[11], NULL, 16); // meas freq
228  lsc->scan_mea.angle_begin = strtoul(field[12], NULL, 16); // angle_begin
229  lsc->scan_mea.angle_resol = strtoul(field[13], NULL, 16); // angle_resol
230  lsc->scan_mea.amnt_of_data = strtoul(field[14], NULL, 16); // amount of data
231  index = 16;
232 
233  #ifdef PRINT_PARSE_DATA
234  std::cout << "Cmd type : " << cmd_type_list[cmd_type] << std::endl;
235  std::cout << "Command : " << cmd_list[cmd] << std::endl;
236  std::cout << "Scan counter : " << lsc->scan_mea.scan_counter << std::endl;
237  std::cout << "Scan frequency : " << std::dec << lsc->scan_mea.scan_freq << std::endl;
238  std::cout << "Measuring frequency : " << lsc->scan_mea.meas_freq << std::endl;
239  std::cout << "Angle begin : " << lsc->scan_mea.angle_begin << std::endl;
240  std::cout << "Angle resolution : " << lsc->scan_mea.angle_resol << std::endl;
241  std::cout << "Amount of data : " << lsc->scan_mea.amnt_of_data << std::endl;
242  #endif
243 
244  msg->angle_min = lsc->scan_mea.angle_begin / 10000.0 * DEG2RAD;
245  msg->angle_max = (lsc->scan_mea.angle_begin / 10000.0 + (lsc->scan_mea.angle_resol / 10000.0 * lsc->scan_mea.amnt_of_data)) * DEG2RAD;
246  msg->angle_increment = lsc->scan_mea.angle_resol / 10000.0 * DEG2RAD;
247  msg->time_increment = (60.0 / lsc->scan_mea.meas_freq) / lsc->scan_mea.scan_counter;
248  msg->scan_time = (60.0 / lsc->scan_mea.meas_freq);
249 
250  #ifdef PRINT_PARSE_DATA
251  std::cout << "" << std::endl;
252  std::cout << "msg->angle_min = " << msg->angle_min << std::endl;
253  std::cout << "msg->angle_max = " << msg->angle_max << std::endl;
254  std::cout << "msg->angle_increment = " << msg->angle_increment << std::endl;
255  std::cout << "msg->range_max = " << msg->range_max << std::endl;
256  std::cout << "msg->range_min = " << msg->range_min << std::endl;
257  std::cout << "msg->scan_time = " << msg->scan_time << std::endl;
258  std::cout << "msg->time_increment = " << msg->time_increment << std::endl;
259  #endif
260 
261  if(strcmp(field[index++], "DIST1") == 0)
262  {
263  index_base = index;
264 
265  msg->ranges.clear();
266  for(uint16_t i = 0; i < lsc->scan_mea.amnt_of_data; i++)
267  {
268  msg->ranges.push_back(strtoul(field[index++], NULL, 16) / 1000.0);
269 
270  #ifdef PRINT_PARSE_DATA
271  std::cout << msg->ranges[i] << ", ";
272  }
273  std::cout << "" << std::endl;
274  #else
275  }
276  #endif
277  }
278 
279  msg->intensities.clear();
280  if(field[index] == field.back())
281  {
282  }
283  else
284  {
285  if(strcmp(field[index++], "RSSI1") == 0 )
286  {
287  index_base = index;
288 
289  for(uint16_t i = 0; i < lsc->scan_mea.amnt_of_data; i++)
290  {
291  chk_intensity = strtoul(field[index++], NULL, 16);
292 
293  msg->intensities.push_back(chk_intensity);
294 
295  #ifdef PRINT_PARSE_DATA
296  std::cout << std::hex << std::uppercase << msg->intensities[i] << ",";
297  }
298  std::cout << "" << std::endl;
299  #else
300  }
301  #endif
302  }
303  }
304  break;
305 
306  case SENSOR_START :
307  case SENSOR_STOP :
308  case SET_ACCESS_LEVEL :
310  for(int i = 2; i < field.size(); i++)
311  {
312  std::cout << field[i] << " ";
313  }
314  std::cout << "" << std::endl;
315  break;
316 
317  default :
318  for(int i = 2; i < field.size(); i++)
319  {
320  std::cout << field[i] << " ";
321  }
322  std::cout << "" << std::endl;
323  break;
324  }
325  }
326 }
parser.hpp
NUM_OF_CMD
#define NUM_OF_CMD
Definition: parser.cpp:40
ScanInfo::fw_ver
uint16_t fw_ver
Definition: parser.hpp:48
msg
msg
Lsc_t::scan_info
ScanInfo scan_info
Definition: parser.hpp:66
SCAN_DATA
@ SCAN_DATA
Definition: parser.cpp:72
ScanMea::amnt_of_data
uint16_t amnt_of_data
Definition: parser.hpp:59
DEG2RAD
#define DEG2RAD
Definition: parser.hpp:44
NONE
@ NONE
Definition: parser.cpp:67
ScanMea::meas_freq
uint16_t meas_freq
Definition: parser.hpp:56
ScanMea::scan_counter
uint16_t scan_counter
Definition: parser.hpp:54
NUMBER_OF_PARAM
#define NUMBER_OF_PARAM
Definition: parser.cpp:42
ScanMea::scan_freq
uint16_t scan_freq
Definition: parser.hpp:55
ScanMea::angle_begin
int32_t angle_begin
Definition: parser.hpp:57
Parser::parsingMsg
void parsingMsg(std::vector< unsigned char > raw_msg, sensor_msgs::LaserScan::Ptr msg, Lsc_t *lsc)
Definition: parser.cpp:144
ScanMea::angle_resol
uint16_t angle_resol
Definition: parser.hpp:58
MAX_NUM_OF_PACKET_PARAM
#define MAX_NUM_OF_PACKET_PARAM
Definition: parser.cpp:43
SENSOR_STOP
@ SENSOR_STOP
Definition: parser.cpp:71
cmd_type_list
const char * cmd_type_list[NUM_OF_CMD_TYPE]
Definition: parser.cpp:48
cmd_list
const char * cmd_list[NUM_OF_CMD]
Definition: parser.cpp:55
NUM_OF_CMD_TYPE
#define NUM_OF_CMD_TYPE
Definition: parser.cpp:39
Lsc_t::scan_mea
ScanMea scan_mea
Definition: parser.hpp:65
CmdListNum
CmdListNum
Definition: parser.cpp:65
Parser::Parser
Parser()
Definition: parser.cpp:76
FIRST_CONNECT_DUMMY_SEND
@ FIRST_CONNECT_DUMMY_SEND
Definition: parser.cpp:73
SENSOR_SCAN_INFO
@ SENSOR_SCAN_INFO
Definition: parser.cpp:69
Lsc_t
Definition: parser.hpp:63
Parser::makeCommand
int makeCommand(unsigned char *buf, std::string cmd)
Definition: parser.cpp:81
SET_ACCESS_LEVEL
@ SET_ACCESS_LEVEL
Definition: parser.cpp:68
SENSOR_START
@ SENSOR_START
Definition: parser.cpp:70
ScanInfo::model_name
std::string model_name
Definition: parser.hpp:49


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