Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022 #include <stdr_robot/sensors/laser.h>
00023
00024 namespace stdr_robot {
00025
00034 Laser::Laser(const nav_msgs::OccupancyGrid& map,
00035 const stdr_msgs::LaserSensorMsg& msg,
00036 const std::string& name,
00037 ros::NodeHandle& n)
00038 :
00039 Sensor(map, name, n, msg.pose, msg.frame_id, msg.frequency)
00040 {
00041 _description = msg;
00042
00043 _publisher = n.advertise<sensor_msgs::LaserScan>
00044 ( _namespace + "/" + msg.frame_id, 1 );
00045 }
00046
00051 void Laser::updateSensorCallback()
00052 {
00053 float angle;
00054 int distance;
00055 int xMap, yMap;
00056 int divisions = 1;
00057 sensor_msgs::LaserScan _laserScan;
00058
00059 if ( _description.numRays > 1 )
00060 {
00061 divisions = _description.numRays - 1;
00062 }
00063
00064 _laserScan.angle_min = _description.minAngle;
00065 _laserScan.angle_max = _description.maxAngle;
00066 _laserScan.range_max = _description.maxRange;
00067 _laserScan.range_min = _description.minRange;
00068 _laserScan.angle_increment =
00069 ( _description.maxAngle - _description.minAngle ) / divisions;
00070
00071
00072 if ( _map.info.height == 0 || _map.info.width == 0 )
00073 {
00074 ROS_DEBUG("Outside limits\n");
00075 return;
00076 }
00077 for ( int laserScanIter = 0; laserScanIter < _description.numRays;
00078 laserScanIter++ )
00079 {
00080
00081 angle = tf::getYaw(_sensorTransform.getRotation()) +
00082 _description.minAngle + laserScanIter *
00083 ( _description.maxAngle - _description.minAngle )
00084 / divisions;
00085
00086 distance = 1;
00087
00088 while ( distance <= _description.maxRange / _map.info.resolution )
00089 {
00090 xMap = _sensorTransform.getOrigin().x() / _map.info.resolution +
00091 cos( angle ) * distance;
00092
00093 yMap = _sensorTransform.getOrigin().y() / _map.info.resolution +
00094 sin( angle ) * distance;
00095
00096 if (yMap * _map.info.width + xMap > _map.info.height*_map.info.width ||
00097 yMap * _map.info.width + xMap < 0)
00098 {
00099 distance = _description.maxRange / _map.info.resolution - 1;
00100 break;
00101 }
00102
00103 if ( _map.data[ yMap * _map.info.width + xMap ] > 70 )
00104 {
00105 break;
00106 }
00107
00108 distance ++;
00109 }
00110
00111 if ( distance * _map.info.resolution > _description.maxRange )
00112 _laserScan.ranges.push_back( std::numeric_limits<float>::infinity() );
00113 else if ( distance * _map.info.resolution < _description.minRange )
00114 _laserScan.ranges.push_back(- std::numeric_limits<float>::infinity() );
00115 else
00116 _laserScan.ranges.push_back( distance * _map.info.resolution );
00117 }
00118
00119 _laserScan.header.stamp = ros::Time::now();
00120 _laserScan.header.frame_id = _namespace + "_" + _description.frame_id;
00121 _publisher.publish( _laserScan );
00122 }
00123
00124 }