laser.cpp
Go to the documentation of this file.
00001 /******************************************************************************
00002    STDR Simulator - Simple Two DImensional Robot Simulator
00003    Copyright (C) 2013 STDR Simulator
00004    This program is free software; you can redistribute it and/or modify
00005    it under the terms of the GNU General Public License as published by
00006    the Free Software Foundation; either version 3 of the License, or
00007    (at your option) any later version.
00008    This program is distributed in the hope that it will be useful,
00009    but WITHOUT ANY WARRANTY; without even the implied warranty of
00010    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00011    GNU General Public License for more details.
00012    You should have received a copy of the GNU General Public License
00013    along with this program; if not, write to the Free Software Foundation,
00014    Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301  USA
00015    
00016    Authors : 
00017    * Manos Tsardoulias, etsardou@gmail.com
00018    * Aris Thallas, aris.thallas@gmail.com
00019    * Chris Zalidis, zalidis@gmail.com 
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 }  // namespace stdr_robot


stdr_robot
Author(s): Manos Tsardoulias, Chris Zalidis, Aris Thallas
autogenerated on Thu Jun 6 2019 18:57:28