sonar.cpp
Go to the documentation of this file.
1 /******************************************************************************
2  STDR Simulator - Simple Two DImensional Robot Simulator
3  Copyright (C) 2013 STDR Simulator
4  This program is free software; you can redistribute it and/or modify
5  it under the terms of the GNU General Public License as published by
6  the Free Software Foundation; either version 3 of the License, or
7  (at your option) any later version.
8  This program is distributed in the hope that it will be useful,
9  but WITHOUT ANY WARRANTY; without even the implied warranty of
10  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11  GNU General Public License for more details.
12  You should have received a copy of the GNU General Public License
13  along with this program; if not, write to the Free Software Foundation,
14  Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
15 
16  Authors :
17  * Manos Tsardoulias, etsardou@gmail.com
18  * Aris Thallas, aris.thallas@gmail.com
19  * Chris Zalidis, zalidis@gmail.com
20 ******************************************************************************/
21 
23 
24 namespace stdr_robot {
25 
34  Sonar::Sonar(const nav_msgs::OccupancyGrid& map,
35  const stdr_msgs::SonarSensorMsg& msg,
36  const std::string& name,
37  ros::NodeHandle& n)
38  :
39  Sensor(map, name, n, msg.pose, msg.frame_id, msg.frequency)
40  {
41  _description = msg;
42 
43  _publisher = n.advertise<sensor_msgs::Range>
44  ( _namespace + "/" + msg.frame_id, 1 );
45  }
46 
52  {
53 
54  }
55 
61  {
62  float angle;
63  int distance;
64  int xMap, yMap;
65  sensor_msgs::Range sonarRangeMsg;
66 
67  sonarRangeMsg.max_range = _description.maxRange;
68  sonarRangeMsg.min_range = _description.minRange;
69  sonarRangeMsg.radiation_type = 0;
70  sonarRangeMsg.field_of_view = _description.coneAngle;
71 
72  if ( _map.info.height == 0 || _map.info.width == 0 )
73  {
74  ROS_DEBUG("Outside limits\n");
75  return;
76  }
77 
78  sonarRangeMsg.range = _description.maxRange;
79 
80  float angleStep = 3.14159 / 180.0;
81  float angleMin = - ( _description.coneAngle / 2.0 );
82  float angleMax = _description.coneAngle / 2.0 ;
84  //float angleMin = -( _description.coneAngle * 180.0 / 3.14159 / 2.0 );
85  //float angleMax = _description.coneAngle * 180.0 / 3.14159 / 2.0 ;
86 
87  for ( float sonarIter = angleMin; sonarIter < angleMax;
88  sonarIter += angleStep )
89  {
90 
91  distance = 1;
92 
93  while ( distance <= _description.maxRange / _map.info.resolution )
94  {
95  xMap = _sensorTransform.getOrigin().x() / _map.info.resolution +
96  cos( sonarIter + tf::getYaw(_sensorTransform.getRotation()) )
97  * distance;
98  yMap = _sensorTransform.getOrigin().y() / _map.info.resolution +
99  sin( sonarIter + tf::getYaw(_sensorTransform.getRotation()) )
100  * distance;
101 
102  if (yMap * _map.info.width + xMap > _map.info.height*_map.info.width ||
103  yMap * _map.info.width + xMap < 0)
104  {
105  distance = _description.maxRange / _map.info.resolution - 1;
106  break;
107  }
108 
110  if ( _map.data[ yMap * _map.info.width + xMap ] > 70 )
111  {
112  break;
113  }
114  distance ++;
115  }
116 
117  if ( distance * _map.info.resolution < sonarRangeMsg.range )
118  {
119  sonarRangeMsg.range = distance * _map.info.resolution;
120  }
121  }
122 
123  if ( sonarRangeMsg.range < _description.minRange )
124  {
125  sonarRangeMsg.range = -std::numeric_limits<float>::infinity();
126  }
127  else if ( sonarRangeMsg.range >= _description.maxRange )
128  {
129  sonarRangeMsg.range = std::numeric_limits<float>::infinity();
130  }
131 
132  sonarRangeMsg.header.stamp = ros::Time::now();
133  sonarRangeMsg.header.frame_id = _namespace + "_" + _description.frame_id;
134  _publisher.publish( sonarRangeMsg );
135  }
136 
137 } // namespace stdr_robot
const std::string & _namespace
< The base for the sensor frame_id
Definition: sensor_base.h:114
void publish(const boost::shared_ptr< M > &message) const
The main namespace for STDR Robot.
Definition: exceptions.h:27
static double getYaw(const Quaternion &bt_q)
A class that provides sensor abstraction.
Definition: sensor_base.h:40
TFSIMD_FORCE_INLINE tfScalar distance(const Vector3 &v) const
stdr_msgs::SonarSensorMsg _description
< Sonar sensor description
Definition: sonar.h:70
TFSIMD_FORCE_INLINE tfScalar angle(const Quaternion &q1, const Quaternion &q2)
TFSIMD_FORCE_INLINE const tfScalar & x() const
virtual void updateSensorCallback()
Updates the sensor measurements.
Definition: sonar.cpp:60
Sonar(const nav_msgs::OccupancyGrid &map, const stdr_msgs::SonarSensorMsg &msg, const std::string &name, ros::NodeHandle &n)
Default constructor.
Definition: sonar.cpp:34
tf::StampedTransform _sensorTransform
True if sensor got the _sensorTransform.
Definition: sensor_base.h:135
TFSIMD_FORCE_INLINE const tfScalar & y() const
Publisher advertise(const std::string &topic, uint32_t queue_size, bool latch=false)
TFSIMD_FORCE_INLINE Vector3 & getOrigin()
const nav_msgs::OccupancyGrid & _map
Sensor pose relative to robot.
Definition: sensor_base.h:116
~Sonar(void)
Default destructor.
Definition: sonar.cpp:51
Quaternion getRotation() const
static Time now()
ros::Publisher _publisher
ROS tf listener.
Definition: sensor_base.h:131
#define ROS_DEBUG(...)


stdr_robot
Author(s): Manos Tsardoulias, Chris Zalidis, Aris Thallas
autogenerated on Mon Jun 10 2019 15:15:10