sick.cpp
Go to the documentation of this file.
00001 
00018 #include<iostream>
00019 #include "limits"
00020 #include "ros/time.h"
00021 #include "ros/duration.h"
00022 #include "ros/ros.h"
00023 #include "sensor_msgs/LaserScan.h"
00024 #include "tf/transform_broadcaster.h"
00025 #include "math.h"
00026 #include "SickPLS.hh"
00027 #include <string>
00028 
00029 #define DEG_CIRCLE 360
00030 #define DEG_TO_RAD (M_PI / (DEG_CIRCLE / 2))
00031 
00032 int main(int argc, char** argv)
00033 {
00034     //Init ROS Publisher.
00035     ros::init(argc, argv, "Sick3000");
00036     ros::NodeHandle n;
00037     std::string topic;
00038     n.getParam("topic", topic);
00039     ros::Publisher scanner_pub = n.advertise<sensor_msgs::LaserScan>(topic, 100);
00040     ros::Rate loop_rate(100);
00041     int baudrate;
00042     n.param("baudrate", baudrate, 500000);
00043     std::string serial;
00044     n.getParam("serial", serial);
00045 
00046     int init_attempts = 1;
00047     n.param("init_attempts", init_attempts, 1);
00048 
00049     //Create new SickPLS scanner class.
00050     SickToolbox::SickPLS *scanner;
00051     scanner = new SickToolbox::SickPLS(serial);
00052 
00053     //Init scanner with 500000baud.
00054     int init_counter = 0;
00055     while(init_counter < init_attempts){
00056         if(scanner -> Initialize(SickToolbox::SickPLS::IntToSickBaud(baudrate))){
00057             ROS_INFO("Init successful");
00058             break;
00059         }else{
00060             ROS_WARN("Init failed. Failed inits: %d, Start new.", init_counter+1);
00061             sleep(2);
00062             scanner = new SickToolbox::SickPLS(serial);
00063             sleep(1);
00064             init_counter++;
00065         }
00066     }
00067 
00068     //Count of measurements.
00069     int count = 0;
00070     //Min and max measured disctance
00071     double minrange = std::numeric_limits<double>::max(), maxrange = 0;
00072     //angel 180, resolution 0.5: 0.5 * 180= 360
00073     unsigned int num_measurement_values = 360;
00074 
00075     while( ros::ok() )
00076     {
00077         unsigned int scan_data[num_measurement_values];
00078         unsigned int *scan_data_pointer = scan_data;
00079         scanner -> GetSickScan(scan_data_pointer,num_measurement_values);
00080         sensor_msgs::LaserScan scan;
00081         scan.header.seq = count;
00082         scan.header.stamp = ros::Time::now();
00083         scan.header.frame_id = "base_laser";
00084         scan.angle_min = -90.0*DEG_TO_RAD;
00085         scan.angle_max = 90.0*DEG_TO_RAD;
00086         scan.scan_time = 0.1;
00087         for(unsigned int i=0; i<num_measurement_values; i++)
00088         {
00089             if(scan_data[i]>maxrange)
00090                 maxrange = scan_data[i];
00091             if(scan_data[i]<minrange)
00092                 minrange = scan_data[i];
00093             scan.ranges.push_back((double)(scan_data[i])/100.0);
00094         }
00095         //Scanner measures in cm -> convert to meter
00096         scan.range_min = minrange/100;
00097         scan.range_max = maxrange/100;
00098         //resolution
00099         scan.angle_increment = (0.5)*DEG_TO_RAD;
00100 
00101         scanner_pub.publish(scan);
00102 
00103         ros::spinOnce();
00104         loop_rate.sleep();
00105         ++count;
00106     }
00107 
00108     return 0;
00109 }


asr_mild_base_laserscanner
Author(s): Aumann Florian, Borella Jocelyn, Dehmani Souheil, Marek Felix, Reckling Reno
autogenerated on Thu Jun 6 2019 21:02:16