stdr_gui_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_gui{
25 
32  CGuiSonar::CGuiSonar(stdr_msgs::SonarSensorMsg msg,std::string baseTopic):
33  msg_(msg)
34  {
35  topic_ = baseTopic + "/" + msg_.frame_id;
36  tf_frame_ = baseTopic + "_" + msg_.frame_id;
38  lock_ = false;
39  subscriber_ = n.subscribe(topic_.c_str(), 1, &CGuiSonar::callback,this);
41  }
42 
48  {
49 
50  }
51 
57  void CGuiSonar::callback(const sensor_msgs::Range& msg)
58  {
59  if(lock_)
60  {
61  return;
62  }
63  range_ = msg;
64  }
65 
74  QImage *m,
75  float ocgd,
76  tf::TransformListener *listener)
77  {
78  lock_ = true;
79  QPainter painter(m);
80 
82  tf::StampedTransform transform;
83 
84  try
85  {
86  listener->waitForTransform("map_static",
87  tf_frame_.c_str(),
88  ros::Time(0),
89  ros::Duration(0.2));
90  listener->lookupTransform("map_static",
91  tf_frame_.c_str(), ros::Time(0), transform);
92  }
93  catch (tf::TransformException ex)
94  {
95  ROS_DEBUG("%s",ex.what());
96  }
97  tfScalar roll,pitch,yaw;
98  float pose_x = transform.getOrigin().x();
99  float pose_y = transform.getOrigin().y();
100  transform.getBasis().getRPY(roll,pitch,yaw);
101  float pose_theta = yaw;
102 
104 
105  float real_dist = range_.range;
106  if(real_dist > msg_.maxRange)
107  {
108  real_dist = msg_.maxRange;
109  QBrush brush(QColor(100,100,100,75 * (2 - visualization_status_)));
110  painter.setBrush(brush);
111  QPen pen(QColor(0,0,0,0));
112  painter.setPen(pen);
113  }
114  else if(real_dist < msg_.minRange)
115  {
116  real_dist = msg_.minRange;
117  QBrush brush(QColor(100,100,100,75 * (2 - visualization_status_)));
118  painter.setBrush(brush);
119  QPen pen(QColor(0,0,0,0));
120  painter.setPen(pen);
121  }
122  else
123  {
124  QBrush brush(QColor(0,200,0,75 * (2 - visualization_status_)));
125  painter.setBrush(brush);
126  QPen pen(QColor(0,0,0,0));
127  painter.setPen(pen);
128  }
129 
130  painter.drawPie(
131  pose_x / ocgd - real_dist / ocgd,
132 
133  pose_y / ocgd - real_dist / ocgd,
134 
135  real_dist / ocgd * 2,
136  real_dist / ocgd * 2,
137 
138  - (pose_theta - msg_.coneAngle / 2.0)
139  * 180.0 / STDR_PI * 16,
140 
141  - msg_.coneAngle * 180.0 / STDR_PI * 16);
142 
143  lock_ = false;
144  }
145 
154  QImage *m,
155  float ocgd,
156  float maxRange)
157  {
158  float size = m->width();
159  float climax = size / maxRange * ocgd / 2.1;
160  lock_ = true;
161  QPainter painter(m);
162 
163  float real_dist = range_.range;
164  if(real_dist > msg_.maxRange)
165  {
166  real_dist = msg_.maxRange;
167  QBrush brush(QColor(100,100,100,100));
168  painter.setBrush(brush);
169  }
170  else if(real_dist < msg_.minRange)
171  {
172  real_dist = msg_.minRange;
173  QBrush brush(QColor(100,100,100,100));
174  painter.setBrush(brush);
175  }
176  else
177  {
178  QBrush brush(QColor(0,200,0,100));
179  painter.setBrush(brush);
180  }
181 
182  painter.drawPie(
183  size / 2 + (msg_.pose.x / ocgd - real_dist / ocgd) * climax,
184  size / 2 + (msg_.pose.y / ocgd - real_dist / ocgd) * climax,
185 
186  real_dist / ocgd * 2 * climax,
187  real_dist / ocgd * 2 * climax,
188 
189  -(msg_.pose.theta - msg_.coneAngle / 2.0) * 180.0 / STDR_PI * 16,
190 
191  -(msg_.coneAngle * 180.0 / STDR_PI) * 16);
192 
193  lock_ = false;
194  }
195 
201  {
202  return msg_.maxRange;
203  }
204 
210  {
211  return visualization_status_;
212  }
213 
219  {
221  }
222 
227  std::string CGuiSonar::getFrameId(void)
228  {
229  return msg_.frame_id;
230  }
231 
238  {
240  }
241 }
242 
~CGuiSonar(void)
Default destructor.
stdr_msgs::SonarSensorMsg msg_
Subscriber for the ros sensor msg.
char getVisualizationStatus(void)
Returns the visibility status of the specific sonar sensor.
double tfScalar
Subscriber subscribe(const std::string &topic, uint32_t queue_size, void(T::*fp)(M), T *obj, const TransportHints &transport_hints=TransportHints())
TFSIMD_FORCE_INLINE Matrix3x3 & getBasis()
void toggleVisualizationStatus(void)
Toggles the visibility status of the specific sonar sensor.
std::string topic_
The ROS tf frame.
void callback(const sensor_msgs::Range &msg)
Callback for the ros sonar message.
sensor_msgs::Range range_
Visualization status of the specific sonar.
bool lock_
< Used to avoid drawing when a new sonar message arives
bool waitForTransform(const std::string &target_frame, const std::string &source_frame, const ros::Time &time, const ros::Duration &timeout, const ros::Duration &polling_sleep_duration=ros::Duration(0.01), std::string *error_msg=NULL) const
TFSIMD_FORCE_INLINE const tfScalar & x() const
CGuiSonar(stdr_msgs::SonarSensorMsg msg, std::string baseTopic)
Default contructor.
std::string tf_frame_
A sonar sensor message : Depscription of a sonar sensor.
void getRPY(tfScalar &roll, tfScalar &pitch, tfScalar &yaw, unsigned int solution_number=1) const
TFSIMD_FORCE_INLINE const tfScalar & y() const
#define STDR_PI
Definition: stdr_tools.h:100
std::string getFrameId(void)
Returns the frame id of the specific sonar sensor.
TFSIMD_FORCE_INLINE Vector3 & getOrigin()
void lookupTransform(const std::string &target_frame, const std::string &source_frame, const ros::Time &time, StampedTransform &transform) const
void paint(QImage *m, float ocgd, tf::TransformListener *listener)
Paints the sonar range in the map image.
The main namespace for STDR GUI.
float getMaxRange(void)
Returns the max range of the specific sonar sensor.
void setVisualizationStatus(char vs)
Sets the visibility status of the specific sonar sensor.
void visualizerPaint(QImage *m, float ocgd, float maxRange)
Paints the sonar range in it&#39;s own visualizer.
#define ROS_DEBUG(...)
ros::Subscriber subscriber_
The ros sonar range msg.


stdr_gui
Author(s): Manos Tsardoulias
autogenerated on Mon Jun 10 2019 15:15:16