stdr_gui_robot.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 {
31  CGuiRobot::CGuiRobot(const stdr_msgs::RobotIndexedMsg& msg)
32  {
33  robot_initialized_ = false;
34  started_ = false;
35  initial_pose_ = msg.robot.initialPose;
37  footprint_ = msg.robot.footprint;
38  radius_ = msg.robot.footprint.radius;
39  frame_id_ = msg.name;
40  show_label_ = true;
41  show_circles_ = false;
43  for(unsigned int i = 0 ; i < msg.robot.laserSensors.size() ; i++)
44  {
45  CGuiLaser *l = new CGuiLaser(msg.robot.laserSensors[i], frame_id_);
46  lasers_.push_back(l);
47  }
48  for(unsigned int i = 0 ; i < msg.robot.sonarSensors.size() ; i++)
49  {
50  CGuiSonar *l = new CGuiSonar(msg.robot.sonarSensors[i], frame_id_);
51  sonars_.push_back(l);
52  }
53  for(unsigned int i = 0 ; i < msg.robot.rfidSensors.size() ; i++)
54  {
55  CGuiRfid *l = new CGuiRfid(msg.robot.rfidSensors[i], frame_id_);
56  rfids_.push_back(l);
57  }
58  for(unsigned int i = 0 ; i < msg.robot.co2Sensors.size() ; i++)
59  {
60  CGuiCO2 *l = new CGuiCO2(msg.robot.co2Sensors[i], frame_id_);
61  co2_sensors_.push_back(l);
62  }
63  for(unsigned int i = 0 ; i < msg.robot.thermalSensors.size() ; i++)
64  {
65  CGuiThermal *l = new CGuiThermal(msg.robot.thermalSensors[i], frame_id_);
66  thermal_sensors_.push_back(l);
67  }
68  for(unsigned int i = 0 ; i < msg.robot.soundSensors.size() ; i++)
69  {
70  CGuiSound *l = new CGuiSound(msg.robot.soundSensors[i], frame_id_);
71  sound_sensors_.push_back(l);
72  }
73  robot_initialized_ = true;
74  }
75 
81  void CGuiRobot::speedsCallback(const geometry_msgs::Twist& msg)
82  {
83  linear_speed_ = msg.linear.x;
84  linear_speed_y_ = msg.linear.y;
85  angular_speed_ = msg.angular.z;
86  }
87 
95  void CGuiRobot::draw(QImage *m,float ocgd,tf::TransformListener *listener)
96  {
98  {
99  return;
100  }
102  {
103  ros::NodeHandle n_;
104  std::string speeds_topic = frame_id_ + std::string("/cmd_vel");
106  speeds_topic.c_str(),
107  1,
109  this);
110  }
111  started_ = true;
112  resolution_ = ocgd;
113  tf::StampedTransform transform;
114 
115  try
116  {
117  listener->waitForTransform("map_static",
118  frame_id_.c_str(),
119  ros::Time(0),
120  ros::Duration(0.2));
121  listener->lookupTransform("map_static",
122  frame_id_.c_str(), ros::Time(0), transform);
123  }
124  catch (tf::TransformException ex)
125  {
126  ROS_DEBUG("%s",ex.what());
127  }
128  tfScalar roll,pitch,yaw;
129  current_pose_.x = transform.getOrigin().x();
130  current_pose_.y = transform.getOrigin().y();
131  transform.getBasis().getRPY(roll,pitch,yaw);
132  current_pose_.theta = yaw;
133 
134  for(unsigned int i = 0 ; i < lasers_.size() ; i++)
135  {
136  lasers_[i]->paint(m,resolution_,listener);
137  }
138  for(unsigned int i = 0 ; i < sonars_.size() ; i++)
139  {
140  sonars_[i]->paint(m,resolution_,listener);
141  }
142  for(unsigned int i = 0 ; i < rfids_.size() ; i++)
143  {
144  rfids_[i]->paint(m,resolution_,listener);
145  }
146  for(unsigned int i = 0 ; i < co2_sensors_.size() ; i++)
147  {
148  co2_sensors_[i]->paint(m,resolution_,listener);
149  }
150  for(unsigned int i = 0 ; i < thermal_sensors_.size() ; i++)
151  {
152  thermal_sensors_[i]->paint(m,resolution_,listener);
153  }
154  for(unsigned int i = 0 ; i < sound_sensors_.size() ; i++)
155  {
156  sound_sensors_[i]->paint(m,resolution_,listener);
157  }
158 
159  drawSelf(m);
160  }
161 
167  void CGuiRobot::drawSelf(QImage *m)
168  {
169  QPainter painter(m);
170  painter.setRenderHint(QPainter::Antialiasing, true);
171 
172  painter.setPen(QColor(0,0,200,50 + 100 * (2 - visualization_status_)));
173 
174  if(footprint_.points.size() == 0)
175  {
176  painter.drawEllipse(
179  radius_ * 2.0 / resolution_,
180  radius_ * 2.0 / resolution_);
181 
182  painter.drawLine(
186  radius_ / resolution_ * 1.05 * cos(current_pose_.theta),
188  radius_ / resolution_ * 1.05 * sin(current_pose_.theta));
189  }
190  else
191  {
192  float max = -1;
193 
194  static QPointF *points = new QPointF[footprint_.points.size() + 1];
195 
196  for(unsigned int i = 0 ; i < footprint_.points.size() + 1; i++)
197  {
198 
199  float x = footprint_.points[i % footprint_.points.size()].x;
200  float y = footprint_.points[i % footprint_.points.size()].y;
201 
202  points[i] = QPointF(
204  x / resolution_ * cos(- current_pose_.theta)
205  + y / resolution_ * sin(- current_pose_.theta),
206 
208  x / resolution_ * sin(current_pose_.theta)
209  + y / resolution_ * cos(- current_pose_.theta));
210 
211  if(max < footprint_.points[i].y)
212  {
213  max = footprint_.points[i].y;
214  }
215  if(max < footprint_.points[i].x)
216  {
217  max = footprint_.points[i].x;
218  }
219  }
220 
221  painter.drawPolyline(points, footprint_.points.size() + 1);
222 
223  painter.drawLine(
224  QPointF( current_pose_.x / resolution_,
226  QPointF( current_pose_.x / resolution_ +
227  max / resolution_ * 1.05 * cos(current_pose_.theta),
229  max / resolution_ * 1.05 * sin(current_pose_.theta)));
230  }
231 
232  if(show_circles_)
233  {
234  painter.setPen(QColor(255,0,0,50 + 100 * (2 - visualization_status_)));
235  for(unsigned int i = 0 ; i < 5 ; i++)
236  {
237  painter.drawEllipse(
238  (current_pose_.x - (i + 1.0) / 2.0) / resolution_,
239  (current_pose_.y - (i + 1.0) / 2.0) / resolution_,
240  (i + 1.0) / resolution_,
241  (i + 1.0) / resolution_);
242  }
243  }
244  }
245 
252  {
253  float dx = p.x() * resolution_ - current_pose_.x;
254  float dy = p.y() * resolution_ - current_pose_.y;
255  float dist = sqrt( pow(dx,2) + pow(dy,2) );
256  return dist <= 0.3;
257  }
258 
264  {
265 
266  }
267 
272  void CGuiRobot::destroy(void){
273  for(unsigned int i = 0 ; i < lasers_.size() ; i++)
274  {
275  delete lasers_[i];
276  }
277  for(unsigned int i = 0 ; i < sonars_.size() ; i++)
278  {
279  delete sonars_[i];
280  }
281  }
282 
287  std::string CGuiRobot::getFrameId(void)
288  {
289  return frame_id_;
290  }
291 
298  void CGuiRobot::drawLabel(QImage *m,float ocgd)
299  {
300  QPainter painter(m);
301 
302  int text_size = frame_id_.size();
303 
304  painter.setPen(QColor(0,0,0,100 * (2 - visualization_status_)));
305 
306  painter.drawRect(
307  current_pose_.x / ocgd + 10,
308  m->height() - (current_pose_.y / ocgd) - 30,
309  3 + text_size * 9,
310  20);
311 
312  painter.setPen(QColor(255,255,255,100 * (2 - visualization_status_)));
313 
314  painter.fillRect(
315  current_pose_.x / ocgd + 10,
316  m->height() - (current_pose_.y / ocgd) - 30,
317  3 + text_size * 9,
318  20,
319  QBrush(QColor(0,0,0,100 * (2 - visualization_status_))));
320 
321  painter.setFont(QFont("Courier New"));
322  painter.drawText(
323  current_pose_.x / ocgd + 12,
324  m->height() - (current_pose_.y / ocgd) - 15,
325  QString(frame_id_.c_str()));
326  }
327 
334  {
335  show_label_ = b;
336  }
337 
343  {
344  return show_label_;
345  }
346 
352  {
354  }
355 
361  {
363  }
364 
370  {
371  return QPoint(current_pose_.x / resolution_,
373  }
374 
379  geometry_msgs::Pose2D CGuiRobot::getCurrentPoseM(void)
380  {
381  return current_pose_;
382  }
383 
389  {
390  return lasers_.size();
391  }
392 
398  {
399  return sonars_.size();
400  }
401 
406  QImage CGuiRobot::getVisualization(float ocgd)
407  {
408  float maxRange = -1;
409  for(unsigned int l = 0 ; l < lasers_.size() ; l++)
410  {
411  float t = lasers_[l]->getMaxRange();
412  if(t > maxRange)
413  {
414  maxRange = t;
415  }
416  }
417  for(unsigned int l = 0 ; l < sonars_.size() ; l++)
418  {
419  float t = sonars_[l]->getMaxRange();
420  if(t > maxRange)
421  {
422  maxRange = t;
423  }
424  }
425  visualization = QImage(310,310,QImage::Format_RGB32);
426  visualization.fill(Qt::white);
427  for(unsigned int l = 0 ; l < lasers_.size() ; l++)
428  {
429  lasers_[l]->visualizerPaint(&visualization,ocgd,maxRange);
430  }
431  for(unsigned int l = 0 ; l < sonars_.size() ; l++)
432  {
433  sonars_[l]->visualizerPaint(&visualization,ocgd,maxRange);
434  }
435  return visualization;
436  }
437 
443  char CGuiRobot::getLaserVisualizationStatus(std::string frame_id)
444  {
445  for(unsigned int i = 0 ; i < lasers_.size() ; i++)
446  {
447  if(lasers_[i]->getFrameId() == frame_id)
448  {
449  return lasers_[i]->getVisualizationStatus();
450  }
451  }
452  return 0;
453  }
454 
461  {
462  for(unsigned int i = 0 ; i < rfids_.size() ; i++)
463  {
464  if(rfids_[i]->getFrameId() == frame_id)
465  {
466  return rfids_[i]->getVisualizationStatus();
467  }
468  }
469  return 0;
470  }
471 
478  {
479  for(unsigned int i = 0 ; i < co2_sensors_.size() ; i++)
480  {
481  if(co2_sensors_[i]->getFrameId() == frame_id)
482  {
483  return co2_sensors_[i]->getVisualizationStatus();
484  }
485  }
486  return 0;
487  }
488 
495  {
496  for(unsigned int i = 0 ; i < thermal_sensors_.size() ; i++)
497  {
498  if(thermal_sensors_[i]->getFrameId() == frame_id)
499  {
500  return thermal_sensors_[i]->getVisualizationStatus();
501  }
502  }
503  return 0;
504  }
505 
512  {
513  for(unsigned int i = 0 ; i < sound_sensors_.size() ; i++)
514  {
515  if(sound_sensors_[i]->getFrameId() == frame_id)
516  {
517  return sound_sensors_[i]->getVisualizationStatus();
518  }
519  }
520  return 0;
521  }
522 
528  void CGuiRobot::toggleLaserVisualizationStatus(std::string frame_id)
529  {
530  for(unsigned int i = 0 ; i < lasers_.size() ; i++)
531  {
532  if(lasers_[i]->getFrameId() == frame_id)
533  {
534  lasers_[i]->toggleVisualizationStatus();
535  }
536  }
537  }
538 
544  char CGuiRobot::getSonarVisualizationStatus(std::string frame_id)
545  {
546  for(unsigned int i = 0 ; i < sonars_.size() ; i++)
547  {
548  if(sonars_[i]->getFrameId() == frame_id)
549  {
550  return sonars_[i]->getVisualizationStatus();
551  }
552  }
553  return 0;
554  }
555 
561  void CGuiRobot::toggleSonarVisualizationStatus(std::string frame_id)
562  {
563  for(unsigned int i = 0 ; i < sonars_.size() ; i++)
564  {
565  if(sonars_[i]->getFrameId() == frame_id)
566  {
567  sonars_[i]->toggleVisualizationStatus();
568  }
569  }
570  }
571 
578  {
579  for(unsigned int i = 0 ; i < rfids_.size() ; i++)
580  {
581  if(rfids_[i]->getFrameId() == frame_id)
582  {
583  rfids_[i]->toggleVisualizationStatus();
584  }
585  }
586  }
593  {
594  for(unsigned int i = 0 ; i < co2_sensors_.size() ; i++)
595  {
596  if(co2_sensors_[i]->getFrameId() == frame_id)
597  {
598  co2_sensors_[i]->toggleVisualizationStatus();
599  }
600  }
601  }
608  {
609  for(unsigned int i = 0 ; i < thermal_sensors_.size() ; i++)
610  {
611  if(thermal_sensors_[i]->getFrameId() == frame_id)
612  {
613  thermal_sensors_[i]->toggleVisualizationStatus();
614  }
615  }
616  }
623  {
624  for(unsigned int i = 0 ; i < sound_sensors_.size() ; i++)
625  {
626  if(sound_sensors_[i]->getFrameId() == frame_id)
627  {
628  sound_sensors_[i]->toggleVisualizationStatus();
629  }
630  }
631  }
632 
638  {
639  return visualization_status_;
640  }
641 
647  {
649  for(unsigned int i = 0 ; i < lasers_.size() ; i++)
650  {
651  lasers_[i]->setVisualizationStatus(visualization_status_);
652  }
653  for(unsigned int i = 0 ; i < sonars_.size() ; i++)
654  {
655  sonars_[i]->setVisualizationStatus(visualization_status_);
656  }
657  for(unsigned int i = 0 ; i < rfids_.size() ; i++)
658  {
659  rfids_[i]->setVisualizationStatus(visualization_status_);
660  }
661  for(unsigned int i = 0 ; i < co2_sensors_.size() ; i++)
662  {
663  co2_sensors_[i]->setVisualizationStatus(visualization_status_);
664  }
665  for(unsigned int i = 0 ; i < thermal_sensors_.size() ; i++)
666  {
667  thermal_sensors_[i]->setVisualizationStatus(visualization_status_);
668  }
669  for(unsigned int i = 0 ; i < sound_sensors_.size() ; i++)
670  {
671  sound_sensors_[i]->setVisualizationStatus(visualization_status_);
672  }
673  }
674 
679  std::vector<float> CGuiRobot::getSpeeds(void)
680  {
681  std::vector<float> speeds;
682  speeds.push_back(linear_speed_);
683  speeds.push_back(linear_speed_y_);
684  speeds.push_back(angular_speed_);
685  return speeds;
686  }
687 
693  void CGuiRobot::setEnvironmentalTags(stdr_msgs::RfidTagVector env_tags)
694  {
695  for(unsigned int i = 0 ; i < rfids_.size() ; i++)
696  {
697  rfids_[i]->setEnvironmentalTags(env_tags);
698  }
699  }
700 
704  void CGuiRobot::setEnvironmentalCO2Sources(stdr_msgs::CO2SourceVector env_)
705  {
706  for(unsigned int i = 0 ; i < co2_sensors_.size() ; i++)
707  {
708  co2_sensors_[i]->setEnvironmentalCO2Sources(env_);
709  }
710  }
714  void CGuiRobot::setEnvironmentalThermalSources(stdr_msgs::ThermalSourceVector env_)
715  {
716  for(unsigned int i = 0 ; i < thermal_sensors_.size() ; i++)
717  {
718  thermal_sensors_[i]->setEnvironmentalThermalSources(env_);
719  }
720  }
724  void CGuiRobot::setEnvironmentalSoundSources(stdr_msgs::SoundSourceVector env_)
725  {
726  for(unsigned int i = 0 ; i < sound_sensors_.size() ; i++)
727  {
728  sound_sensors_[i]->setEnvironmentalSoundSources(env_);
729  }
730  }
731 }
void setEnvironmentalTags(stdr_msgs::RfidTagVector env_tags)
Sets the tags existent in the environment.
char getThermalSensorVisualizationStatus(std::string frame_id)
Returns the rfid reader visibility status.
char getCO2SensorVisualizationStatus(std::string frame_id)
Returns the rfid reader visibility status.
bool robot_initialized_
Check for start.
void setEnvironmentalThermalSources(stdr_msgs::ThermalSourceVector env_thermal_sources)
Sets the thermal sources existent in the environment.
bool getShowLabel(void)
Gets the show_label_ flag.
float linear_speed_y_
Robot current angular speed.
geometry_msgs::Pose2D initial_pose_
Current robot pose.
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 setEnvironmentalCO2Sources(stdr_msgs::CO2SourceVector env_co2_sources)
Sets the tags existent in the environment.
void toggleVisualizationStatus(void)
Toggles the visibility status.
bool show_label_
< If true the robot&#39;s label is visible
std::vector< CGuiSonar * > sonars_
Robot Rfid antenna sensors.
void destroy(void)
Destroys the robot object.
CGuiRobot(const stdr_msgs::RobotIndexedMsg &msg)
Default contructor.
void draw(QImage *m, float ocgd, tf::TransformListener *listener)
Paints the robot and it&#39;s sensors to the image.
std::string frame_id_
Initial robot pose.
char getSoundSensorVisualizationStatus(std::string frame_id)
Returns the rfid reader visibility status.
char getRfidReaderVisualizationStatus(std::string frame_id)
Returns the rfid reader visibility status.
TFSIMD_FORCE_INLINE const tfScalar & y() const
Implements the functionalities for a sonar sensor.
float radius_
Map resolution.
std::vector< CGuiRfid * > rfids_
Robot Rfid antenna sensors.
float angular_speed_
Radius of robot if shape is circular.
std::vector< CGuiCO2 * > co2_sensors_
Robot Rfid antenna sensors.
void toggleSonarVisualizationStatus(std::string frame_id)
Toggles the sonar visibility status.
void toggleLaserVisualizationStatus(std::string frame_id)
Toggles the laser visibility status.
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
bool checkEventProximity(QPoint p)
Checks if the robot is near a specific point.
void speedsCallback(const geometry_msgs::Twist &msg)
Callback for the ros laser message.
char getVisualizationStatus(void)
Returns the visibility status.
TFSIMD_FORCE_INLINE const tfScalar & x() const
bool started_
Subscriber for the speeds twist message.
void toggleThermalSensorVisualizationStatus(std::string frame_id)
Toggles the rfid reader visibility status.
void toggleSoundSensorVisualizationStatus(std::string frame_id)
Toggles the rfid reader visibility status.
void drawLabel(QImage *m, float ocgd)
Draws the robot&#39;s label.
ros::Subscriber speeds_subscriber_
Robot current linear speed.
int getSonarsNumber(void)
Returns the sonars number.
~CGuiRobot(void)
Default destructor.
void getRPY(tfScalar &roll, tfScalar &pitch, tfScalar &yaw, unsigned int solution_number=1) const
Implements the functionalities for an RFID antenna sensor.
Definition: stdr_gui_rfid.h:39
void toggleCO2SensorVisualizationStatus(std::string frame_id)
Toggles the rfid reader visibility status.
TFSIMD_FORCE_INLINE const tfScalar & y() const
std::string getFrameId(void)
Returns the frame id of the specific robot.
int getLasersNumber(void)
Returns the lasers number.
TFSIMD_FORCE_INLINE const tfScalar & x() const
QPoint getCurrentPose(void)
Returns the current robot pose.
geometry_msgs::Pose2D current_pose_
Robot footprint if not circular.
bool show_circles_
True when robot is initialized.
std::vector< CGuiLaser * > lasers_
Robot sonar sensors.
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 toggleShowLabel(void)
Toggles the show_label_ flag.
Implements the functionalities for an RFID antenna sensor.
Definition: stdr_gui_co2.h:39
The main namespace for STDR GUI.
char getSonarVisualizationStatus(std::string frame_id)
Returns the sonar visibility status.
void toggleRfidReaderVisualizationStatus(std::string frame_id)
Toggles the rfid reader visibility status.
stdr_msgs::FootprintMsg footprint_
Visualization image.
float linear_speed_
Robot current linear speed by Y.
QImage getVisualization(float ocgd)
Returns the visualization image of the robot.
geometry_msgs::Pose2D getCurrentPoseM(void)
Returns the current robot pose in meters.
void setEnvironmentalSoundSources(stdr_msgs::SoundSourceVector env_sound_sources)
Sets the sound sources existent in the environment.
std::vector< float > getSpeeds(void)
Returns the current robot speed.
void drawSelf(QImage *m)
Draws the robot body.
std::vector< CGuiThermal * > thermal_sensors_
Robot Rfid antenna sensors.
Implements the functionalities for a laser sensor.
void setShowLabel(bool b)
Sets the show_label_ flag.
char getLaserVisualizationStatus(std::string frame_id)
Returns the laser visibility status.
float resolution_
Robot laser sensors.
void toggleShowCircles(void)
Toggles the show_circles_ flag.
std::vector< CGuiSound * > sound_sensors_
Robot frame id.
#define ROS_DEBUG(...)


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