sick_generic_laser.cpp
Go to the documentation of this file.
1 
62 #ifdef _MSC_VER
63 #define _WIN32_WINNT 0x0501
64 #pragma warning(disable: 4996)
65 #pragma warning(disable: 4267)
66 #endif
67 
68 #ifndef _MSC_VER
69 
70 
71 #endif
72 
74 
78 
79 
80 #ifdef _MSC_VER
81 #include "sick_scan/rosconsole_simu.hpp"
82 #endif
83 #define _USE_MATH_DEFINES
84 
85 #include <math.h>
86 #include <string>
87 #include <stdio.h>
88 #include <stdlib.h>
89 #include <signal.h>
90 
91 static bool isInitialized = false;
93 static std::string versionInfo = "???";
94 
95 void setVersionInfo(std::string _versionInfo)
96 {
97  versionInfo = _versionInfo;
98 }
99 
100 std::string getVersionInfo()
101 {
102 
103  return (versionInfo);
104 }
105 
107 {
109 };
110 
112 
113 
122 bool getTagVal(std::string tagVal, std::string &tag, std::string &val)
123 {
124  bool ret = false;
125  std::size_t pos;
126  pos = tagVal.find(":=");
127  tag = "";
128  val = "";
129  if (pos == std::string::npos)
130  {
131  ret = false;
132  }
133  else
134  {
135  tag = tagVal.substr(0, pos);
136  val = tagVal.substr(pos + 2);
137  ret = true;
138  }
139  return (ret);
140 }
141 
142 
143 void my_handler(int signalRecv)
144 {
145  ROS_INFO("Caught signal %d\n", signalRecv);
146  ROS_INFO("good bye");
147  ROS_INFO("You are leaving the following version of this node:");
148  ROS_INFO("%s", getVersionInfo().c_str());
149  if (s != NULL)
150  {
151  if (isInitialized)
152  {
153  s->stopScanData();
154  }
155 
157  }
158  ros::shutdown();
159 }
160 
169 int mainGenericLaser(int argc, char **argv, std::string nodeName)
170 {
171  std::string tag;
172  std::string val;
173 
174 
175  bool doInternalDebug = false;
176  bool emulSensor = false;
177  for (int i = 0; i < argc; i++)
178  {
179  std::string s = argv[i];
180  if (getTagVal(s, tag, val))
181  {
182  if (tag.compare("__internalDebug") == 0)
183  {
184  int debugState = 0;
185  sscanf(val.c_str(), "%d", &debugState);
186  if (debugState > 0)
187  {
188  doInternalDebug = true;
189  }
190  }
191  if (tag.compare("__emulSensor") == 0)
192  {
193  int dummyState = 0;
194  sscanf(val.c_str(), "%d", &dummyState);
195  if (dummyState > 0)
196  {
197  emulSensor = true;
198  }
199  }
200  }
201  }
202 
203  ros::init(argc, argv, nodeName, ros::init_options::NoSigintHandler); // scannerName holds the node-name
204  signal(SIGINT, my_handler);
205 
206  ros::NodeHandle nhPriv("~");
207 
208 
209  std::string scannerName;
210  if (false == nhPriv.getParam("scanner_type", scannerName))
211  {
212  ROS_ERROR("cannot find parameter ""scanner_type"" in the param set. Please specify scanner_type.");
213  ROS_ERROR("Try to set %s as fallback.\n", nodeName.c_str());
214  scannerName = nodeName;
215  }
216 
217 
218  if (doInternalDebug)
219  {
220 #ifdef _MSC_VER
221  nhPriv.setParam("name", scannerName);
222  rossimu_settings(nhPriv); // just for tiny simulations under Visual C++
223 #else
224  nhPriv.setParam("hostname", "192.168.0.4");
225  nhPriv.setParam("imu_enable", true);
226  nhPriv.setParam("cloud_topic", "pt_cloud");
227 #endif
228  }
229 
230 // check for TCP - use if ~hostname is set.
231  bool useTCP = false;
232  std::string hostname;
233  if (nhPriv.getParam("hostname", hostname))
234  {
235  useTCP = true;
236  }
237  bool changeIP = false;
238  std::string sNewIp;
239  if (nhPriv.getParam("new_IP_address", sNewIp))
240  {
241  changeIP = true;
242  }
243  std::string port;
244  nhPriv.param<std::string>("port", port, "2112");
245 
246  int timelimit;
247  nhPriv.param("timelimit", timelimit, 5);
248 
249  bool subscribe_datagram;
250  int device_number;
251  nhPriv.param("subscribe_datagram", subscribe_datagram, false);
252  nhPriv.param("device_number", device_number, 0);
253 
254 
256 
257  double param;
258  char colaDialectId = 'A'; // A or B (Ascii or Binary)
259 
260  if (nhPriv.getParam("range_min", param))
261  {
262  parser->set_range_min(param);
263  }
264  if (nhPriv.getParam("range_max", param))
265  {
266  parser->set_range_max(param);
267  }
268  if (nhPriv.getParam("time_increment", param))
269  {
270  parser->set_time_increment(param);
271  }
272 
273  /*
274  * Check, if parameter for protocol type is set
275  */
276  bool use_binary_protocol = true;
277  if (true == nhPriv.getParam("emul_sensor", emulSensor))
278  {
279  ROS_INFO("Found emul_sensor overwriting default settings. Emulation: %s", emulSensor ? "True" : "False");
280  }
281  if (true == nhPriv.getParam("use_binary_protocol", use_binary_protocol))
282  {
283  ROS_INFO("Found sopas_protocol_type param overwriting default protocol:");
284  if (use_binary_protocol == true)
285  {
286  ROS_INFO("Binary protocol activated");
287  }
288  else
289  {
290  if (parser->getCurrentParamPtr()->getNumberOfLayers() > 4)
291  {
292  nhPriv.setParam("sopas_protocol_type", true);
293  use_binary_protocol = true;
294  ROS_WARN("This scanner type does not support ASCII communication.\n"
295  "Binary communication has been activated.\n"
296  "The parameter \"sopas_protocol_type\" has been set to \"True\".");
297  }
298  else
299  {
300  ROS_INFO("ASCII protocol activated");
301  }
302  }
303  parser->getCurrentParamPtr()->setUseBinaryProtocol(use_binary_protocol);
304  }
305 
306 
307  if (parser->getCurrentParamPtr()->getUseBinaryProtocol())
308  {
309  colaDialectId = 'B';
310  }
311  else
312  {
313  colaDialectId = 'A';
314  }
315 
316  bool start_services = false;
317  sick_scan::SickScanServices* services = 0;
318  int result = sick_scan::ExitError;
319 
320  sick_scan::SickScanConfig cfg;
321 
322  bool run = true;
323 
324  while (ros::ok() && run)
325  {
326  switch (runState)
327  {
328  case scanner_init:
329  ROS_INFO("Start initialising scanner [Ip: %s] [Port: %s]", hostname.c_str(), port.c_str());
330  // attempt to connect/reconnect
331  delete s; // disconnect scanner
332  if (useTCP)
333  {
334  s = new sick_scan::SickScanCommonTcp(hostname, port, timelimit, parser, colaDialectId);
335  }
336  else
337  {
338  ROS_ERROR("TCP is not switched on. Probably hostname or port not set. Use roslaunch to start node.");
339  exit(-1);
340  }
341 
342 
343  if (emulSensor)
344  {
345  s->setEmulSensor(true);
346  }
347  result = s->init();
348 
349  // Start ROS services
350  if (true == nhPriv.getParam("start_services", start_services) && true == start_services)
351  {
352  services = new sick_scan::SickScanServices(&nhPriv, s, parser->getCurrentParamPtr()->getUseBinaryProtocol());
353  ROS_INFO("SickScanServices: ros services initialized");
354  }
355 
356  isInitialized = true;
357  signal(SIGINT, SIG_DFL); // change back to standard signal handler after initialising
358  if (result == sick_scan::ExitSuccess) // OK -> loop again
359  {
360  if (changeIP)
361  {
363  }
364 
365 
366  runState = scanner_run; // after initialising switch to run state
367  }
368  else
369  {
370  runState = scanner_init; // If there was an error, try to restart scanner
371 
372  }
373  break;
374 
375  case scanner_run:
376  if (result == sick_scan::ExitSuccess) // OK -> loop again
377  {
378  ros::spinOnce();
379  result = s->loopOnce();
380  }
381  else
382  {
383  runState = scanner_finalize; // interrupt
384  }
385  break;
386  case scanner_finalize:
387  run = false;
388  break; // ExitError or similiar -> interrupt while-Loop
389  default:
390  ROS_ERROR("Invalid run state in main loop");
391  run = false;
392  break;
393  }
394  }
395  if(services)
396  {
397  delete services;
398  services = 0;
399  }
400  if (s != NULL)
401  {
402  delete s; // close connnect
403  }
404  if (parser != NULL)
405  {
406  delete parser; // close parser
407  }
408  return result;
409 
410 }
ros::NodeHandle::setParam
void setParam(const std::string &key, bool b) const
sick_scan::SickScanServices
Definition: sick_scan_services.h:53
NodeRunState
NodeRunState
Definition: sick_generic_laser.cpp:106
run
void run(class_loader::ClassLoader *loader)
sick_generic_laser.h
sick_scan::ExitError
@ ExitError
Definition: abstract_parser.h:47
runState
NodeRunState runState
Definition: sick_generic_laser.cpp:111
ros::init
ROSCPP_DECL void init(const M_string &remappings, const std::string &name, uint32_t options=0)
sick_scan::SickGenericParser
Definition: sick_generic_parser.h:159
scanner_finalize
@ scanner_finalize
Definition: sick_generic_laser.cpp:108
ros::NodeHandle::getParam
bool getParam(const std::string &key, bool &b) const
sick_scan::ExitSuccess
@ ExitSuccess
Definition: abstract_parser.h:46
ros::spinOnce
ROSCPP_DECL void spinOnce()
ros::shutdown
ROSCPP_DECL void shutdown()
my_handler
void my_handler(int signalRecv)
Definition: sick_generic_laser.cpp:143
ros::ok
ROSCPP_DECL bool ok()
isInitialized
static bool isInitialized
Definition: sick_generic_laser.cpp:91
sick_scan::SickScanCommonTcp::setEmulSensor
void setEmulSensor(bool _emulFlag)
Set emulation flag (using emulation instead of "real" scanner - currently implemented for radar.
Definition: sick_scan_common_tcp.cpp:267
sick_scan::SickGenericParser::set_range_min
void set_range_min(float min)
Setting minimum range.
Definition: sick_generic_parser.cpp:1182
versionInfo
static std::string versionInfo
Definition: sick_generic_laser.cpp:93
sick_generic_parser.h
sick_scan_common_tcp.h
ROS_WARN
#define ROS_WARN(...)
getVersionInfo
std::string getVersionInfo()
Definition: sick_generic_laser.cpp:100
sick_scan::SickScanCommonTcp
Definition: sick_scan_common_tcp.h:68
sick_scan::SickScanCommonTcp::stopScanData
bool stopScanData()
Definition: sick_scan_common_tcp.cpp:589
mainGenericLaser
int mainGenericLaser(int argc, char **argv, std::string nodeName)
Internal Startup routine.
Definition: sick_generic_laser.cpp:169
scanner_run
@ scanner_run
Definition: sick_generic_laser.cpp:108
sick_scan_services.h
sick_scan::SickScanCommon::loopOnce
int loopOnce()
parsing datagram and publishing ros messages
Definition: sick_scan_common.cpp:2802
ROS_ERROR
#define ROS_ERROR(...)
sick_scan::SickGenericParser::getCurrentParamPtr
ScannerBasicParam * getCurrentParamPtr()
Gets Pointer to parameter object.
Definition: sick_generic_parser.cpp:683
ros::NodeHandle::param
T param(const std::string &param_name, const T &default_val) const
param
T param(const std::string &param_name, const T &default_val)
getTagVal
bool getTagVal(std::string tagVal, std::string &tag, std::string &val)
splitting expressions like <tag>:=into <tag> and
Definition: sick_generic_laser.cpp:122
sick_scan::SickScanCommon::init
virtual int init()
init routine of scanner
Definition: sick_scan_common.cpp:837
setVersionInfo
void setVersionInfo(std::string _versionInfo)
Definition: sick_generic_laser.cpp:95
s
static sick_scan::SickScanCommonTcp * s
Definition: sick_generic_laser.cpp:92
sick_scan::SickGenericParser::set_range_max
void set_range_max(float max)
Setting maximum range.
Definition: sick_generic_parser.cpp:1192
ROS_INFO
#define ROS_INFO(...)
sick_scan::ScannerBasicParam::getNumberOfLayers
int getNumberOfLayers(void)
Getting number of scanner layers.
Definition: sick_generic_parser.cpp:109
ros::init_options::NoSigintHandler
NoSigintHandler
sick_scan::SickGenericParser::set_time_increment
void set_time_increment(float time)
setting time increment between shots
Definition: sick_generic_parser.cpp:1223
scanner_init
@ scanner_init
Definition: sick_generic_laser.cpp:108
sick_scan::ScannerBasicParam::setUseBinaryProtocol
void setUseBinaryProtocol(bool _useBinary)
flag to decide between usage of ASCII-sopas or BINARY-sopas
Definition: sick_generic_parser.cpp:240
ros::NodeHandle
sick_scan::ScannerBasicParam::getUseBinaryProtocol
bool getUseBinaryProtocol(void)
flag to decide between usage of ASCII-sopas or BINARY-sopas
Definition: sick_generic_parser.cpp:290


sick_scan
Author(s): Michael Lehning , Jochen Sprickerhof , Martin Günther
autogenerated on Thu Sep 8 2022 02:30:19