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  while (ros::ok())
323  {
324  switch (runState)
325  {
326  case scanner_init:
327  ROS_INFO("Start initialising scanner [Ip: %s] [Port: %s]", hostname.c_str(), port.c_str());
328  // attempt to connect/reconnect
329  delete s; // disconnect scanner
330  if (useTCP)
331  {
332  s = new sick_scan::SickScanCommonTcp(hostname, port, timelimit, parser, colaDialectId);
333  }
334  else
335  {
336  ROS_ERROR("TCP is not switched on. Probably hostname or port not set. Use roslaunch to start node.");
337  exit(-1);
338  }
339 
340 
341  if (emulSensor)
342  {
343  s->setEmulSensor(true);
344  }
345  result = s->init();
346 
347  // Start ROS services
348  if (true == nhPriv.getParam("start_services", start_services) && true == start_services)
349  {
350  services = new sick_scan::SickScanServices(&nhPriv, s, parser->getCurrentParamPtr()->getUseBinaryProtocol());
351  ROS_INFO("SickScanServices: ros services initialized");
352  }
353 
354  isInitialized = true;
355  signal(SIGINT, SIG_DFL); // change back to standard signal handler after initialising
356  if (result == sick_scan::ExitSuccess) // OK -> loop again
357  {
358  if (changeIP)
359  {
361  }
362 
363 
364  runState = scanner_run; // after initialising switch to run state
365  }
366  else
367  {
368  runState = scanner_init; // If there was an error, try to restart scanner
369 
370  }
371  break;
372 
373  case scanner_run:
374  if (result == sick_scan::ExitSuccess) // OK -> loop again
375  {
376  ros::spinOnce();
377  result = s->loopOnce();
378  }
379  else
380  {
381  runState = scanner_finalize; // interrupt
382  }
383  case scanner_finalize:
384  break; // ExitError or similiar -> interrupt while-Loop
385  default:
386  ROS_ERROR("Invalid run state in main loop");
387  break;
388  }
389  }
390  if(services)
391  {
392  delete services;
393  services = 0;
394  }
395  if (s != NULL)
396  {
397  delete s; // close connnect
398  }
399  if (parser != NULL)
400  {
401  delete parser; // close parser
402  }
403  return result;
404 
405 }
void set_range_max(float max)
Setting maximum range.
bool param(const std::string &param_name, T &param_val, const T &default_val)
static std::string versionInfo
std::string getVersionInfo()
static bool isInitialized
NodeRunState runState
ROSCPP_DECL void init(int &argc, char **argv, const std::string &name, uint32_t options=0)
int mainGenericLaser(int argc, char **argv, std::string nodeName)
Internal Startup routine.
void my_handler(int signalRecv)
#define ROS_WARN(...)
int getNumberOfLayers(void)
Getting number of scanner layers.
#define ROS_INFO(...)
bool param(const std::string &param_name, T &param_val, const T &default_val) const
bool getTagVal(std::string tagVal, std::string &tag, std::string &val)
splitting expressions like <tag>:=into <tag> and
ROSCPP_DECL bool ok()
bool getUseBinaryProtocol(void)
flag to decide between usage of ASCII-sopas or BINARY-sopas
ScannerBasicParam * getCurrentParamPtr()
Gets Pointer to parameter object.
bool getParam(const std::string &key, std::string &s) const
void set_time_increment(float time)
setting time increment between shots
parser
void set_range_min(float min)
Setting minimum range.
ROSCPP_DECL void shutdown()
void setUseBinaryProtocol(bool _useBinary)
flag to decide between usage of ASCII-sopas or BINARY-sopas
ROSCPP_DECL void spinOnce()
#define ROS_ERROR(...)
void setParam(const std::string &key, const XmlRpc::XmlRpcValue &v) const
static sick_scan::SickScanCommonTcp * s
void setVersionInfo(std::string _versionInfo)


sick_scan
Author(s): Michael Lehning , Jochen Sprickerhof , Martin Günther
autogenerated on Wed May 5 2021 03:05:48