LMS1xx_node.cpp
Go to the documentation of this file.
00001 /*
00002  * LMS1xx.cpp
00003  *
00004  *  Created on: 09-08-2010
00005  *  Author: Konrad Banachowicz
00006  ***************************************************************************
00007  *   This library is free software; you can redistribute it and/or         *
00008  *   modify it under the terms of the GNU Lesser General Public            *
00009  *   License as published by the Free Software Foundation; either          *
00010  *   version 2.1 of the License, or (at your option) any later version.    *
00011  *                                                                         *
00012  *   This library is distributed in the hope that it will be useful,       *
00013  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
00014  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU     *
00015  *   Lesser General Public License for more details.                       *
00016  *                                                                         *
00017  *   You should have received a copy of the GNU Lesser General Public      *
00018  *   License along with this library; if not, write to the Free Software   *
00019  *   Foundation, Inc., 59 Temple Place,                                    *
00020  *   Suite 330, Boston, MA  02111-1307  USA                                *
00021  *                                                                         *
00022  ***************************************************************************/
00023 
00024 #include <csignal>
00025 #include <cstdio>
00026 #include <LMS1xx/LMS1xx.h>
00027 #include "ros/ros.h"
00028 #include "sensor_msgs/LaserScan.h"
00029 #include <limits>
00030 #include <string>
00031 
00032 #define DEG2RAD M_PI/180.0
00033 
00034 int main(int argc, char **argv)
00035 {
00036   // laser data
00037   LMS1xx laser;
00038   scanCfg cfg;
00039   scanOutputRange outputRange;
00040   scanDataCfg dataCfg;
00041   sensor_msgs::LaserScan scan_msg;
00042 
00043   // parameters
00044   std::string host;
00045   std::string frame_id;
00046   bool inf_range;
00047   int port;
00048 
00049   ros::init(argc, argv, "lms1xx");
00050   ros::NodeHandle nh;
00051   ros::NodeHandle n("~");
00052   ros::Publisher scan_pub = nh.advertise<sensor_msgs::LaserScan>("scan", 1);
00053 
00054   n.param<std::string>("host", host, "192.168.1.2");
00055   n.param<std::string>("frame_id", frame_id, "laser");
00056   n.param<bool>("publish_min_range_as_inf", inf_range, false);
00057   n.param<int>("port", port, 2111);
00058 
00059   while (ros::ok())
00060   {
00061     ROS_INFO_STREAM("Connecting to laser at " << host);
00062     laser.connect(host, port);
00063     if (!laser.isConnected())
00064     {
00065       ROS_WARN("Unable to connect, retrying.");
00066       ros::Duration(1).sleep();
00067       continue;
00068     }
00069 
00070     ROS_DEBUG("Logging in to laser.");
00071     laser.login();
00072     cfg = laser.getScanCfg();
00073     outputRange = laser.getScanOutputRange();
00074 
00075     if (cfg.scaningFrequency != 5000)
00076     {
00077       laser.disconnect();
00078       ROS_WARN("Unable to get laser output range. Retrying.");
00079       ros::Duration(1).sleep();
00080       continue;
00081     }
00082 
00083     ROS_INFO("Connected to laser.");
00084 
00085     ROS_DEBUG("Laser configuration: scaningFrequency %d, angleResolution %d, startAngle %d, stopAngle %d",
00086               cfg.scaningFrequency, cfg.angleResolution, cfg.startAngle, cfg.stopAngle);
00087     ROS_DEBUG("Laser output range:angleResolution %d, startAngle %d, stopAngle %d",
00088               outputRange.angleResolution, outputRange.startAngle, outputRange.stopAngle);
00089 
00090     scan_msg.header.frame_id = frame_id;
00091     scan_msg.range_min = 0.01;
00092     scan_msg.range_max = 20.0;
00093     scan_msg.scan_time = 100.0 / cfg.scaningFrequency;
00094     scan_msg.angle_increment = static_cast<double>(outputRange.angleResolution / 10000.0 * DEG2RAD);
00095     scan_msg.angle_min = static_cast<double>(outputRange.startAngle / 10000.0 * DEG2RAD - M_PI / 2);
00096     scan_msg.angle_max = static_cast<double>(outputRange.stopAngle / 10000.0 * DEG2RAD - M_PI / 2);
00097 
00098     ROS_DEBUG_STREAM("Device resolution is " << (double)outputRange.angleResolution / 10000.0 << " degrees.");
00099     ROS_DEBUG_STREAM("Device frequency is " << (double)cfg.scaningFrequency / 100.0 << " Hz");
00100 
00101     int angle_range = outputRange.stopAngle - outputRange.startAngle;
00102     int num_values = angle_range / outputRange.angleResolution;
00103     if (angle_range % outputRange.angleResolution == 0)
00104     {
00105       // Include endpoint
00106       ++num_values;
00107     }
00108     scan_msg.ranges.resize(num_values);
00109     scan_msg.intensities.resize(num_values);
00110 
00111     scan_msg.time_increment =
00112       (outputRange.angleResolution / 10000.0)
00113       / 360.0
00114       / (cfg.scaningFrequency / 100.0);
00115 
00116     ROS_DEBUG_STREAM("Time increment is " << static_cast<int>(scan_msg.time_increment * 1000000) << " microseconds");
00117 
00118     dataCfg.outputChannel = 1;
00119     dataCfg.remission = true;
00120     dataCfg.resolution = 1;
00121     dataCfg.encoder = 0;
00122     dataCfg.position = false;
00123     dataCfg.deviceName = false;
00124     dataCfg.outputInterval = 1;
00125 
00126     ROS_DEBUG("Setting scan data configuration.");
00127     laser.setScanDataCfg(dataCfg);
00128 
00129     ROS_DEBUG("Starting measurements.");
00130     laser.startMeas();
00131 
00132     ROS_DEBUG("Waiting for ready status.");
00133     ros::Time ready_status_timeout = ros::Time::now() + ros::Duration(5);
00134 
00135     // while(1)
00136     // {
00137     status_t stat = laser.queryStatus();
00138     ros::Duration(1.0).sleep();
00139     if (stat != ready_for_measurement)
00140     {
00141       ROS_WARN("Laser not ready. Retrying initialization.");
00142       laser.disconnect();
00143       ros::Duration(1).sleep();
00144       continue;
00145     }
00146     /*if (stat == ready_for_measurement)
00147     {
00148       ROS_DEBUG("Ready status achieved.");
00149       break;
00150     }
00151 
00152       if (ros::Time::now() > ready_status_timeout)
00153       {
00154         ROS_WARN("Timed out waiting for ready status. Trying again.");
00155         laser.disconnect();
00156         continue;
00157       }
00158 
00159       if (!ros::ok())
00160       {
00161         laser.disconnect();
00162         return 1;
00163       }
00164     }*/
00165 
00166     ROS_DEBUG("Starting device.");
00167     laser.startDevice();  // Log out to properly re-enable system after config
00168 
00169     ROS_DEBUG("Commanding continuous measurements.");
00170     laser.scanContinous(1);
00171 
00172     while (ros::ok())
00173     {
00174       ros::Time start = ros::Time::now();
00175 
00176       scan_msg.header.stamp = start;
00177       ++scan_msg.header.seq;
00178 
00179       scanData data;
00180       ROS_DEBUG("Reading scan data.");
00181       if (laser.getScanData(&data))
00182       {
00183         for (int i = 0; i < data.dist_len1; i++)
00184         {
00185           float range_data = data.dist1[i] * 0.001;
00186 
00187           if (inf_range && range_data < scan_msg.range_min)
00188           {
00189             scan_msg.ranges[i] = std::numeric_limits<float>::infinity();
00190           }
00191           else
00192           {
00193             scan_msg.ranges[i] = range_data;
00194           }
00195         }
00196 
00197         for (int i = 0; i < data.rssi_len1; i++)
00198         {
00199           scan_msg.intensities[i] = data.rssi1[i];
00200         }
00201 
00202         ROS_DEBUG("Publishing scan data.");
00203         scan_pub.publish(scan_msg);
00204       }
00205       else
00206       {
00207         ROS_ERROR("Laser timed out on delivering scan, attempting to reinitialize.");
00208         break;
00209       }
00210 
00211       ros::spinOnce();
00212     }
00213 
00214     laser.scanContinous(0);
00215     laser.stopMeas();
00216     laser.disconnect();
00217   }
00218 
00219   return 0;
00220 }


lms1xx
Author(s): Konrad Banachowicz
autogenerated on Thu Jun 6 2019 18:52:14