00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055
00056
00057
00058
00059
00060
00061 #include <ros/ros.h>
00062
00063
00064 #include <sensor_msgs/LaserScan.h>
00065
00066
00067
00068
00069
00070 #include <cob_sick_s300/ScannerSickS300.h>
00071
00072
00073
00074 class NodeClass
00075 {
00076
00077 public:
00078
00079 ros::NodeHandle nodeHandle;
00080
00081 ros::Publisher topicPub_LaserScan;
00082
00083
00084
00085
00086
00087
00088
00089
00090
00091
00092
00093 std::string port;
00094 int baud, start_scan, stop_scan, scan_id;
00095 bool inverted;
00096 std::string frame_id;
00097
00098
00099
00100 NodeClass()
00101 {
00102
00103 ros::NodeHandle private_nh_("~");
00104
00105 private_nh_.param<std::string>("port", port, "/dev/ttyUSB0");
00106 private_nh_.param<int>("baud", baud, 500000);
00107 private_nh_.param<int>("scan_id", scan_id, 7);
00108 private_nh_.param<bool>("inverted", inverted, false);
00109 private_nh_.param<std::string>("frame_id", frame_id, "/base_laser_link");
00110 private_nh_.param<int>("start_scan", start_scan, 0);
00111 private_nh_.param<int>("stop_scan", stop_scan, 541);
00112
00113
00114 topicPub_LaserScan = nodeHandle.advertise<sensor_msgs::LaserScan>("scan", 1);
00115
00116
00117
00118
00119
00120
00121 }
00122
00123
00124 ~NodeClass()
00125 {
00126 }
00127
00128
00129
00130
00131
00132
00133
00134
00135
00136
00137 void publishLaserScan(std::vector<double> vdDistM, std::vector<double> vdAngRAD, std::vector<double> vdIntensAU)
00138 {
00139
00140 int num_readings = vdDistM.size();
00141 start_scan = 0;
00142 stop_scan = vdDistM.size();
00143 double laser_frequency = 10;
00144
00145
00146 sensor_msgs::LaserScan laserScan;
00147 laserScan.header.stamp = ros::Time::now();
00148
00149
00150 laserScan.header.frame_id = frame_id;
00151 laserScan.angle_increment = vdAngRAD[start_scan + 1] - vdAngRAD[start_scan];
00152 laserScan.range_min = -135.0/180.0*3.14;
00153 laserScan.range_max = 135.0/180.0*3.14;
00154 laserScan.time_increment = (1 / laser_frequency) / (vdDistM.size());
00155
00156
00157 num_readings = stop_scan - start_scan;
00158 laserScan.angle_min = vdAngRAD[start_scan];
00159 laserScan.angle_max = vdAngRAD[stop_scan - 1];
00160 laserScan.set_ranges_size(num_readings);
00161 laserScan.set_intensities_size(num_readings);
00162
00163
00164
00165 for(int i = 0; i < (stop_scan - start_scan); i++)
00166 {
00167 if(inverted)
00168 {
00169 laserScan.ranges[i] = vdDistM[stop_scan-1-i];
00170 laserScan.intensities[i] = vdIntensAU[stop_scan-1-i];
00171 }
00172 else
00173 {
00174 laserScan.ranges[i] = vdDistM[start_scan + i];
00175 laserScan.intensities[i] = vdIntensAU[start_scan + i];
00176 }
00177 }
00178
00179
00180 topicPub_LaserScan.publish(laserScan);
00181 }
00182 };
00183
00184
00185
00186 int main(int argc, char** argv)
00187 {
00188
00189 ros::init(argc, argv, "sick_s300");
00190
00191 NodeClass nodeClass;
00192 ScannerSickS300 SickS300;
00193
00194
00195
00196
00197
00198 int iBaudRate = nodeClass.baud;
00199 int iScanId = nodeClass.scan_id;
00200 bool bOpenScan = false, bRecScan = false;
00201 bool firstTry = true;
00202 std::vector<double> vdDistM, vdAngRAD, vdIntensAU;
00203
00204 while (!bOpenScan)
00205 {
00206 ROS_INFO("Opening scanner... (port:%s)",nodeClass.port.c_str());
00207 bOpenScan = SickS300.open(nodeClass.port.c_str(), iBaudRate, iScanId);
00208
00209
00210 if(!bOpenScan)
00211 {
00212 ROS_ERROR("...scanner not available on port %s. Will retry every second.",nodeClass.port.c_str());
00213 firstTry = false;
00214 }
00215 sleep(1);
00216 }
00217 ROS_INFO("...scanner opened successfully on port %s",nodeClass.port.c_str());
00218
00219
00220 ros::Rate loop_rate(5);
00221 while(nodeClass.nodeHandle.ok())
00222 {
00223
00224 ROS_DEBUG("Reading scanner...");
00225 bRecScan = SickS300.getScan(vdDistM, vdAngRAD, vdIntensAU);
00226 ROS_DEBUG("...read %d points from scanner successfully",vdDistM.size());
00227
00228 if(bRecScan)
00229 {
00230 ROS_DEBUG("...publishing LaserScan message");
00231 nodeClass.publishLaserScan(vdDistM, vdAngRAD, vdIntensAU);
00232 }
00233 else
00234 {
00235 ROS_WARN("...no Scan available");
00236 }
00237
00238
00239 ros::spinOnce();
00240 loop_rate.sleep();
00241 }
00242 return 0;
00243 }
00244
00245
00246
00247