Go to the documentation of this file.00001 #include <csignal>
00002 #include <cstdio>
00003 #include <lms1xx.h>
00004 #include "ros/ros.h"
00005 #include "sensor_msgs/LaserScan.h"
00006
00007 #define DEG2RAD M_PI/180.0
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 int main(int argc, char **argv)
00033 {
00034
00035 LMS1xx laser;
00036 scanCfg cfg;
00037 scanDataCfg dataCfg;
00038 scanData data;
00039
00040 sensor_msgs::LaserScan scan_msg;
00041
00042 std::string host;
00043 std::string frame_id;
00044 bool inverted;
00045 double resolution;
00046 double frequency;
00047 bool set_config;
00048
00049 ros::init(argc, argv, "lms1xx");
00050 ros::NodeHandle n("~");
00051 ros::Publisher scan_pub = n.advertise<sensor_msgs::LaserScan>("scan", 1);
00052
00053 if(!n.hasParam("host")) ROS_WARN("Used default parameter for host");
00054 n.param<std::string>("host", host, "192.168.1.2");
00055 if(!n.hasParam("frame_id")) ROS_WARN("Used default parameter for frame_id");
00056 n.param<std::string>("frame_id", frame_id, "/base_laser_link");
00057 if(!n.hasParam("inverted")) ROS_WARN("Used default parameter for inverted");
00058 n.param<bool>("inverted", inverted, false);
00059 if(!n.hasParam("angle_resolution")) ROS_WARN("Used default parameter for resolution");
00060 n.param<double>("angle_resolution", resolution, 0.5);
00061 if(!n.hasParam("scan_frequency")) ROS_WARN("Used default parameter for frequency");
00062 n.param<double>("scan_frequency", frequency, 25);
00063 if(!n.hasParam("set_config")) ROS_WARN("Used default parameter for set_config");
00064 n.param<bool>("set_config", set_config, false);
00065
00066 ROS_INFO("connecting to laser at : %s", host.c_str());
00067 ROS_INFO("using frame_id : %s", frame_id.c_str());
00068 ROS_INFO("inverted : %s", (inverted)?"true":"false");
00069 ROS_INFO("using res : %f", resolution);
00070 ROS_INFO("using freq : %f", frequency);
00071
00072 laser.connect(host);
00073
00074 if (laser.isConnected())
00075 {
00076 ROS_INFO("Connected to laser.");
00077
00078
00079 laser.login();
00080 cfg = laser.getScanCfg();
00081
00082 if(set_config)
00083 {
00084 ROS_DEBUG("Set angle resolution to %f deg",resolution);
00085 cfg.angleResolution = (int)(resolution * 10000);
00086 ROS_DEBUG("Set scan frequency to %f hz",frequency);
00087 cfg.scaningFrequency = (int)(frequency * 100);
00088
00089 laser.setScanCfg(cfg);
00090 laser.saveConfig();
00091 }
00092
00093 cfg = laser.getScanCfg();
00094
00095 if(cfg.angleResolution != (int)(resolution * 10000))
00096 ROS_ERROR("Setting angle resolution failed");
00097 if(cfg.scaningFrequency != (int)(frequency * 100))
00098 ROS_ERROR("Setting scan frequency failed");
00099
00100
00101 scan_msg.header.frame_id = frame_id;
00102
00103 scan_msg.range_min = 0.01;
00104 scan_msg.range_max = 20.0;
00105
00106 scan_msg.scan_time = 100.0/cfg.scaningFrequency;
00107
00108 scan_msg.angle_increment = (double)cfg.angleResolution/10000.0 * DEG2RAD;
00109 scan_msg.angle_min = (double)cfg.startAngle/10000.0 * DEG2RAD - M_PI/2;
00110 scan_msg.angle_max = (double)cfg.stopAngle/10000.0 * DEG2RAD - M_PI/2;
00111
00112 int num_values;
00113 if (cfg.angleResolution == 2500)
00114 {
00115 num_values = 1081;
00116 }
00117 else if (cfg.angleResolution == 5000)
00118 {
00119 num_values = 541;
00120 }
00121 else
00122 {
00123 ROS_ERROR("Unsupported resolution");
00124 return 0;
00125 }
00126
00127 scan_msg.time_increment = scan_msg.scan_time/num_values;
00128
00129 scan_msg.ranges.resize(num_values);
00130 scan_msg.intensities.resize(num_values);
00131
00132 if(not inverted)
00133 scan_msg.time_increment *= -1.;
00134
00135
00136 dataCfg.outputChannel = 1;
00137 dataCfg.remission = true;
00138 dataCfg.resolution = 1;
00139 dataCfg.encoder = 0;
00140 dataCfg.position = false;
00141 dataCfg.deviceName = false;
00142 dataCfg.outputInterval = 1;
00143
00144 laser.setScanDataCfg(dataCfg);
00145 ROS_DEBUG("setScanDataCfg");
00146
00147 laser.startMeas();
00148 ROS_DEBUG("startMeas");
00149
00150 status_t stat;
00151 do
00152 {
00153 stat = laser.queryStatus();
00154 ros::Duration(1.0).sleep();
00155 }
00156 while (stat != ready_for_measurement);
00157
00158 laser.startDevice();
00159 ROS_DEBUG("startDevice");
00160
00161 laser.scanContinous(1);
00162 ROS_DEBUG("scanContinous true");
00163
00164 while (ros::ok())
00165 {
00166 ros::Time start = ros::Time::now();
00167
00168 scan_msg.header.stamp = start;
00169 ++scan_msg.header.seq;
00170
00171 if(laser.getData(data))
00172 {
00173 for (int i = 0; i < data.dist_len1; i++)
00174 {
00175 if(not inverted) {
00176 scan_msg.ranges[i] = data.dist1[data.dist_len1-1-i] * 0.001;
00177 scan_msg.intensities[i] = data.rssi1[data.rssi_len1-1-i];
00178 } else {
00179 scan_msg.ranges[i] = data.dist1[i] * 0.001;
00180 scan_msg.intensities[i] = data.rssi1[i];
00181 }
00182 }
00183
00184 scan_pub.publish(scan_msg);
00185 }
00186
00187 ros::spinOnce();
00188 }
00189
00190 laser.scanContinous(0);
00191 ROS_DEBUG("scanContinous false");
00192 laser.stopMeas();
00193 ROS_DEBUG("stopMeas");
00194 laser.disconnect();
00195 ROS_DEBUG("disconnect");
00196 }
00197 else
00198 {
00199 ROS_ERROR("Connection to device failed");
00200 }
00201 return 0;
00202 }