sick.cpp
Go to the documentation of this file.
1 
18 #include<iostream>
19 #include "limits"
20 #include "ros/time.h"
21 #include "ros/duration.h"
22 #include "ros/ros.h"
23 #include "sensor_msgs/LaserScan.h"
25 #include "math.h"
26 #include "SickPLS.hh"
27 #include <string>
28 
29 #define DEG_CIRCLE 360
30 #define DEG_TO_RAD (M_PI / (DEG_CIRCLE / 2))
31 
32 int main(int argc, char** argv)
33 {
34  //Init ROS Publisher.
35  ros::init(argc, argv, "Sick3000");
37  std::string topic;
38  n.getParam("topic", topic);
39  ros::Publisher scanner_pub = n.advertise<sensor_msgs::LaserScan>(topic, 100);
40  ros::Rate loop_rate(100);
41  int baudrate;
42  n.param("baudrate", baudrate, 500000);
43  std::string serial;
44  n.getParam("serial", serial);
45 
46  int init_attempts = 1;
47  n.param("init_attempts", init_attempts, 1);
48 
49  //Create new SickPLS scanner class.
50  SickToolbox::SickPLS *scanner;
51  scanner = new SickToolbox::SickPLS(serial);
52 
53  //Init scanner with 500000baud.
54  int init_counter = 0;
55  while(init_counter < init_attempts){
56  if(scanner -> Initialize(SickToolbox::SickPLS::IntToSickBaud(baudrate))){
57  ROS_INFO("Init successful");
58  break;
59  }else{
60  ROS_WARN("Init failed. Failed inits: %d, Start new.", init_counter+1);
61  sleep(2);
62  scanner = new SickToolbox::SickPLS(serial);
63  sleep(1);
64  init_counter++;
65  }
66  }
67 
68  //Count of measurements.
69  int count = 0;
70  //Min and max measured disctance
71  double minrange = std::numeric_limits<double>::max(), maxrange = 0;
72  //angel 180, resolution 0.5: 0.5 * 180= 360
73  unsigned int num_measurement_values = 360;
74 
75  while( ros::ok() )
76  {
77  unsigned int scan_data[num_measurement_values];
78  unsigned int *scan_data_pointer = scan_data;
79  scanner -> GetSickScan(scan_data_pointer,num_measurement_values);
80  sensor_msgs::LaserScan scan;
81  scan.header.seq = count;
82  scan.header.stamp = ros::Time::now();
83  scan.header.frame_id = "base_laser";
84  scan.angle_min = -90.0*DEG_TO_RAD;
85  scan.angle_max = 90.0*DEG_TO_RAD;
86  scan.scan_time = 0.1;
87  for(unsigned int i=0; i<num_measurement_values; i++)
88  {
89  if(scan_data[i]>maxrange)
90  maxrange = scan_data[i];
91  if(scan_data[i]<minrange)
92  minrange = scan_data[i];
93  scan.ranges.push_back((double)(scan_data[i])/100.0);
94  }
95  //Scanner measures in cm -> convert to meter
96  scan.range_min = minrange/100;
97  scan.range_max = maxrange/100;
98  //resolution
99  scan.angle_increment = (0.5)*DEG_TO_RAD;
100 
101  scanner_pub.publish(scan);
102 
103  ros::spinOnce();
104  loop_rate.sleep();
105  ++count;
106  }
107 
108  return 0;
109 }
void publish(const boost::shared_ptr< M > &message) const
ROSCPP_DECL void init(int &argc, char **argv, const std::string &name, uint32_t options=0)
A general class for interfacing w/ SickPLS laser range finders.
Definition: SickPLS.hh:50
#define ROS_WARN(...)
#define ROS_INFO(...)
bool param(const std::string &param_name, T &param_val, const T &default_val) const
ROSCPP_DECL bool ok()
Publisher advertise(const std::string &topic, uint32_t queue_size, bool latch=false)
#define DEG_TO_RAD
Definition: sick.cpp:30
int main(int argc, char **argv)
Definition: sick.cpp:32
bool getParam(const std::string &key, std::string &s) const
static Time now()
Definition of class SickPLS. Code by Jason C. Derenick and Thomas H. Miller. Contact derenick(at)lehi...
ROSCPP_DECL void spinOnce()
static sick_pls_baud_t IntToSickBaud(const int baud_int)
Converts integer to corresponding Sick PLS baud.
Definition: SickPLS.cc:715


asr_mild_base_laserscanner
Author(s): Aumann Florian, Borella Jocelyn, Dehmani Souheil, Marek Felix, Reckling Reno
autogenerated on Mon Jun 10 2019 12:41:37