stdr_gui_robot.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_gui/stdr_gui_sensors/stdr_gui_robot.h"
00023 
00024 namespace stdr_gui
00025 {
00031   CGuiRobot::CGuiRobot(const stdr_msgs::RobotIndexedMsg& msg)
00032   {
00033     robot_initialized_ = false;
00034     started_ = false;
00035     initial_pose_ = msg.robot.initialPose;
00036     current_pose_ = initial_pose_;
00037     footprint_ = msg.robot.footprint;
00038     radius_ = msg.robot.footprint.radius;
00039     frame_id_ = msg.name;
00040     show_label_ = true;
00041     show_circles_ = false;
00042     visualization_status_ = 0;
00043     for(unsigned int i = 0 ; i < msg.robot.laserSensors.size() ; i++)
00044     {
00045       CGuiLaser *l = new CGuiLaser(msg.robot.laserSensors[i], frame_id_);
00046       lasers_.push_back(l);
00047     }
00048     for(unsigned int i = 0 ; i < msg.robot.sonarSensors.size() ; i++)
00049     {
00050       CGuiSonar *l = new CGuiSonar(msg.robot.sonarSensors[i], frame_id_);
00051       sonars_.push_back(l);
00052     }
00053     for(unsigned int i = 0 ; i < msg.robot.rfidSensors.size() ; i++)
00054     {
00055       CGuiRfid *l = new CGuiRfid(msg.robot.rfidSensors[i], frame_id_);
00056       rfids_.push_back(l);
00057     }
00058     for(unsigned int i = 0 ; i < msg.robot.co2Sensors.size() ; i++)
00059     {
00060       CGuiCO2 *l = new CGuiCO2(msg.robot.co2Sensors[i], frame_id_);
00061       co2_sensors_.push_back(l);
00062     }
00063     for(unsigned int i = 0 ; i < msg.robot.thermalSensors.size() ; i++)
00064     {
00065       CGuiThermal *l = new CGuiThermal(msg.robot.thermalSensors[i], frame_id_);
00066       thermal_sensors_.push_back(l);
00067     }
00068     for(unsigned int i = 0 ; i < msg.robot.soundSensors.size() ; i++)
00069     {
00070       CGuiSound *l = new CGuiSound(msg.robot.soundSensors[i], frame_id_);
00071       sound_sensors_.push_back(l);
00072     }
00073     robot_initialized_ = true;
00074   }
00075   
00081   void CGuiRobot::speedsCallback(const geometry_msgs::Twist& msg)
00082   {
00083     linear_speed_ = msg.linear.x;
00084     linear_speed_y_ = msg.linear.y;
00085     angular_speed_ = msg.angular.z;
00086   }
00087   
00095   void CGuiRobot::draw(QImage *m,float ocgd,tf::TransformListener *listener)
00096   {
00097     if(!robot_initialized_)
00098     {
00099       return;
00100     }
00101     if(robot_initialized_ && !started_)
00102     {
00103       ros::NodeHandle n_;
00104       std::string speeds_topic = frame_id_ + std::string("/cmd_vel");
00105       speeds_subscriber_ = n_.subscribe(
00106         speeds_topic.c_str(), 
00107         1, 
00108         &CGuiRobot::speedsCallback,
00109         this);
00110     }
00111     started_ = true;
00112     resolution_ = ocgd;
00113     tf::StampedTransform transform;
00114       
00115     try
00116     {
00117       listener->waitForTransform("map_static",
00118                                   frame_id_.c_str(),
00119                                   ros::Time(0),
00120                                   ros::Duration(0.2));
00121       listener->lookupTransform("map_static", 
00122         frame_id_.c_str(), ros::Time(0), transform);
00123     }
00124     catch (tf::TransformException ex)
00125     {
00126       ROS_DEBUG("%s",ex.what());
00127     }
00128     tfScalar roll,pitch,yaw;
00129     current_pose_.x = transform.getOrigin().x();
00130     current_pose_.y = transform.getOrigin().y();
00131     transform.getBasis().getRPY(roll,pitch,yaw);
00132     current_pose_.theta = yaw;
00133     
00134     for(unsigned int i = 0 ; i < lasers_.size() ; i++)
00135     {
00136       lasers_[i]->paint(m,resolution_,listener);
00137     }
00138     for(unsigned int i = 0 ; i < sonars_.size() ; i++)
00139     {
00140       sonars_[i]->paint(m,resolution_,listener);
00141     }
00142     for(unsigned int i = 0 ; i < rfids_.size() ; i++)
00143     {
00144       rfids_[i]->paint(m,resolution_,listener);
00145     }
00146     for(unsigned int i = 0 ; i < co2_sensors_.size() ; i++)
00147     {
00148       co2_sensors_[i]->paint(m,resolution_,listener);
00149     }
00150     for(unsigned int i = 0 ; i < thermal_sensors_.size() ; i++)
00151     {
00152       thermal_sensors_[i]->paint(m,resolution_,listener);
00153     }
00154     for(unsigned int i = 0 ; i < sound_sensors_.size() ; i++)
00155     {
00156       sound_sensors_[i]->paint(m,resolution_,listener);
00157     }
00158     
00159     drawSelf(m);
00160   }
00161   
00167   void CGuiRobot::drawSelf(QImage *m)
00168   {
00169     QPainter painter(m);
00170     painter.setRenderHint(QPainter::Antialiasing, true);
00171     
00172     painter.setPen(QColor(0,0,200,50 + 100 * (2 - visualization_status_)));
00173     
00174     if(footprint_.points.size() == 0)
00175     {
00176       painter.drawEllipse(
00177         (current_pose_.x - radius_) / resolution_,
00178         (current_pose_.y - radius_) / resolution_,
00179         radius_ * 2.0 / resolution_,
00180         radius_ * 2.0 / resolution_);
00181       
00182       painter.drawLine( 
00183         current_pose_.x / resolution_,
00184         current_pose_.y / resolution_,
00185         current_pose_.x / resolution_ + 
00186           radius_ / resolution_ * 1.05 * cos(current_pose_.theta),
00187         current_pose_.y / resolution_ + 
00188           radius_ / resolution_ * 1.05 * sin(current_pose_.theta));
00189     }
00190     else
00191     {
00192       float max = -1;
00193       
00194       static QPointF *points = new QPointF[footprint_.points.size() + 1];
00195       
00196       for(unsigned int i = 0 ; i < footprint_.points.size() + 1; i++)
00197       {
00198         
00199         float x = footprint_.points[i % footprint_.points.size()].x;
00200         float y = footprint_.points[i % footprint_.points.size()].y;
00201         
00202         points[i] = QPointF(
00203           current_pose_.x / resolution_ + 
00204             x / resolution_ * cos(- current_pose_.theta) 
00205             + y / resolution_ * sin(- current_pose_.theta),
00206               
00207           current_pose_.y / resolution_ + 
00208             x / resolution_ * sin(current_pose_.theta) 
00209             + y / resolution_ * cos(- current_pose_.theta));
00210     
00211         if(max < footprint_.points[i].y)
00212         {
00213           max = footprint_.points[i].y;
00214         }
00215         if(max < footprint_.points[i].x)
00216         {
00217           max = footprint_.points[i].x;
00218         }
00219       }
00220       
00221       painter.drawPolyline(points, footprint_.points.size() + 1);
00222       
00223       painter.drawLine(
00224         QPointF(  current_pose_.x / resolution_,
00225                   current_pose_.y / resolution_),
00226         QPointF(  current_pose_.x / resolution_ + 
00227                     max / resolution_ * 1.05 * cos(current_pose_.theta),
00228                   current_pose_.y / resolution_ + 
00229                     max / resolution_ * 1.05 * sin(current_pose_.theta)));
00230     }
00231     
00232     if(show_circles_)
00233     {
00234       painter.setPen(QColor(255,0,0,50 + 100 * (2 - visualization_status_)));
00235       for(unsigned int i = 0 ; i < 5 ; i++)
00236       {
00237         painter.drawEllipse(
00238           (current_pose_.x - (i + 1.0) / 2.0) / resolution_,
00239           (current_pose_.y - (i + 1.0) / 2.0) / resolution_,
00240           (i + 1.0) / resolution_,
00241           (i + 1.0) / resolution_);
00242       }
00243     }
00244   }
00245   
00251   bool CGuiRobot::checkEventProximity(QPoint p)
00252   {
00253     float dx = p.x() * resolution_ - current_pose_.x;
00254     float dy = p.y() * resolution_ - current_pose_.y;
00255     float dist = sqrt( pow(dx,2) + pow(dy,2) );
00256     return dist <= 0.3;
00257   }
00258   
00263   CGuiRobot::~CGuiRobot(void)
00264   {
00265     
00266   }
00267   
00272   void CGuiRobot::destroy(void){
00273     for(unsigned int i = 0 ; i < lasers_.size() ; i++)
00274     {
00275       delete lasers_[i];
00276     }
00277     for(unsigned int i = 0 ; i < sonars_.size() ; i++)
00278     {
00279       delete sonars_[i];
00280     }
00281   }
00282 
00287   std::string CGuiRobot::getFrameId(void)
00288   {
00289     return frame_id_;
00290   }
00291   
00298   void CGuiRobot::drawLabel(QImage *m,float ocgd)
00299   {
00300     QPainter painter(m);
00301     
00302     int text_size = frame_id_.size();
00303     
00304     painter.setPen(QColor(0,0,0,100 * (2 - visualization_status_)));
00305     
00306     painter.drawRect(
00307       current_pose_.x / ocgd + 10,
00308       m->height() - (current_pose_.y / ocgd) - 30,
00309       3 + text_size * 9,
00310       20);
00311     
00312     painter.setPen(QColor(255,255,255,100 * (2 - visualization_status_)));
00313     
00314     painter.fillRect(
00315       current_pose_.x / ocgd + 10,
00316       m->height() - (current_pose_.y / ocgd) - 30,
00317       3 + text_size * 9,
00318       20,
00319       QBrush(QColor(0,0,0,100 * (2 - visualization_status_))));
00320     
00321     painter.setFont(QFont("Courier New"));
00322     painter.drawText(
00323       current_pose_.x / ocgd + 12,
00324       m->height() - (current_pose_.y / ocgd) - 15,
00325       QString(frame_id_.c_str()));
00326   }
00327   
00333   void CGuiRobot::setShowLabel(bool b)
00334   {
00335     show_label_ = b;
00336   }
00337   
00342   bool CGuiRobot::getShowLabel(void)
00343   {
00344     return show_label_;
00345   }
00346   
00351   void CGuiRobot::toggleShowLabel(void)
00352   {
00353     show_label_ =! show_label_;
00354   }
00355   
00360   void CGuiRobot::toggleShowCircles(void)
00361   {
00362     show_circles_ =! show_circles_;
00363   }
00364   
00369   QPoint CGuiRobot::getCurrentPose(void)
00370   {
00371     return QPoint(current_pose_.x / resolution_, 
00372       current_pose_.y / resolution_);
00373   }
00374   
00379   geometry_msgs::Pose2D CGuiRobot::getCurrentPoseM(void)
00380   {
00381     return current_pose_;
00382   }
00383   
00388   int CGuiRobot::getLasersNumber(void)
00389   {
00390     return lasers_.size();
00391   }
00392   
00397   int CGuiRobot::getSonarsNumber(void)
00398   {
00399     return sonars_.size();
00400   }
00401   
00406   QImage CGuiRobot::getVisualization(float ocgd)
00407   {
00408     float maxRange = -1;
00409     for(unsigned int l = 0 ; l < lasers_.size() ; l++)
00410     {
00411       float t = lasers_[l]->getMaxRange();
00412       if(t > maxRange)
00413       {
00414         maxRange = t;
00415       }
00416     }
00417     for(unsigned int l = 0 ; l < sonars_.size() ; l++)
00418     {
00419       float t = sonars_[l]->getMaxRange();
00420       if(t > maxRange)
00421       {
00422         maxRange = t;
00423       }
00424     }
00425     visualization = QImage(310,310,QImage::Format_RGB32);
00426     visualization.fill(Qt::white);
00427     for(unsigned int l = 0 ; l < lasers_.size() ; l++)
00428     {
00429       lasers_[l]->visualizerPaint(&visualization,ocgd,maxRange);
00430     }
00431     for(unsigned int l = 0 ; l < sonars_.size() ; l++)
00432     {
00433       sonars_[l]->visualizerPaint(&visualization,ocgd,maxRange);
00434     }
00435     return visualization;
00436   }
00437   
00443   char CGuiRobot::getLaserVisualizationStatus(std::string frame_id)
00444   {
00445     for(unsigned int i = 0 ; i < lasers_.size() ; i++)
00446     {
00447       if(lasers_[i]->getFrameId() == frame_id)
00448       {
00449         return lasers_[i]->getVisualizationStatus();
00450       }
00451     }
00452     return 0;
00453   }
00454   
00460   char CGuiRobot::getRfidReaderVisualizationStatus(std::string frame_id)
00461   {
00462     for(unsigned int i = 0 ; i < rfids_.size() ; i++)
00463     {
00464       if(rfids_[i]->getFrameId() == frame_id)
00465       {
00466         return rfids_[i]->getVisualizationStatus();
00467       }
00468     }
00469     return 0;
00470   }
00471   
00477   char CGuiRobot::getCO2SensorVisualizationStatus(std::string frame_id)
00478   {
00479     for(unsigned int i = 0 ; i < co2_sensors_.size() ; i++)
00480     {
00481       if(co2_sensors_[i]->getFrameId() == frame_id)
00482       {
00483         return co2_sensors_[i]->getVisualizationStatus();
00484       }
00485     }
00486     return 0;
00487   }
00488   
00494   char CGuiRobot::getThermalSensorVisualizationStatus(std::string frame_id)
00495   {
00496     for(unsigned int i = 0 ; i < thermal_sensors_.size() ; i++)
00497     {
00498       if(thermal_sensors_[i]->getFrameId() == frame_id)
00499       {
00500         return thermal_sensors_[i]->getVisualizationStatus();
00501       }
00502     }
00503     return 0;
00504   }
00505   
00511   char CGuiRobot::getSoundSensorVisualizationStatus(std::string frame_id)
00512   {
00513     for(unsigned int i = 0 ; i < sound_sensors_.size() ; i++)
00514     {
00515       if(sound_sensors_[i]->getFrameId() == frame_id)
00516       {
00517         return sound_sensors_[i]->getVisualizationStatus();
00518       }
00519     }
00520     return 0;
00521   }
00522     
00528   void CGuiRobot::toggleLaserVisualizationStatus(std::string frame_id)
00529   {
00530     for(unsigned int i = 0 ; i < lasers_.size() ; i++)
00531     {
00532       if(lasers_[i]->getFrameId() == frame_id)
00533       {
00534         lasers_[i]->toggleVisualizationStatus();
00535       }
00536     }
00537   }
00538   
00544   char CGuiRobot::getSonarVisualizationStatus(std::string frame_id)
00545   {
00546     for(unsigned int i = 0 ; i < sonars_.size() ; i++)
00547     {
00548       if(sonars_[i]->getFrameId() == frame_id)
00549       {
00550         return sonars_[i]->getVisualizationStatus();
00551       }
00552     }
00553     return 0;
00554   }
00555   
00561   void CGuiRobot::toggleSonarVisualizationStatus(std::string frame_id)
00562   {
00563     for(unsigned int i = 0 ; i < sonars_.size() ; i++)
00564     {
00565       if(sonars_[i]->getFrameId() == frame_id)
00566       {
00567         sonars_[i]->toggleVisualizationStatus();
00568       }
00569     }
00570   }
00571   
00577   void CGuiRobot::toggleRfidReaderVisualizationStatus(std::string frame_id)
00578   {
00579     for(unsigned int i = 0 ; i < rfids_.size() ; i++)
00580     {
00581       if(rfids_[i]->getFrameId() == frame_id)
00582       {
00583         rfids_[i]->toggleVisualizationStatus();
00584       }
00585     }
00586   }
00592   void CGuiRobot::toggleCO2SensorVisualizationStatus(std::string frame_id)
00593   {
00594     for(unsigned int i = 0 ; i < co2_sensors_.size() ; i++)
00595     {
00596       if(co2_sensors_[i]->getFrameId() == frame_id)
00597       {
00598         co2_sensors_[i]->toggleVisualizationStatus();
00599       }
00600     }
00601   }
00607   void CGuiRobot::toggleThermalSensorVisualizationStatus(std::string frame_id)
00608   {
00609     for(unsigned int i = 0 ; i < thermal_sensors_.size() ; i++)
00610     {
00611       if(thermal_sensors_[i]->getFrameId() == frame_id)
00612       {
00613         thermal_sensors_[i]->toggleVisualizationStatus();
00614       }
00615     }
00616   }
00622   void CGuiRobot::toggleSoundSensorVisualizationStatus(std::string frame_id)
00623   {
00624     for(unsigned int i = 0 ; i < sound_sensors_.size() ; i++)
00625     {
00626       if(sound_sensors_[i]->getFrameId() == frame_id)
00627       {
00628         sound_sensors_[i]->toggleVisualizationStatus();
00629       }
00630     }
00631   }
00632   
00637   char CGuiRobot::getVisualizationStatus(void)
00638   {
00639     return visualization_status_;
00640   }
00641   
00646   void CGuiRobot::toggleVisualizationStatus(void)
00647   {
00648     visualization_status_ = (visualization_status_ + 1) % 3;
00649     for(unsigned int i = 0 ; i < lasers_.size() ; i++)
00650     {
00651       lasers_[i]->setVisualizationStatus(visualization_status_);
00652     }
00653     for(unsigned int i = 0 ; i < sonars_.size() ; i++)
00654     {
00655       sonars_[i]->setVisualizationStatus(visualization_status_);
00656     }
00657     for(unsigned int i = 0 ; i < rfids_.size() ; i++)
00658     {
00659       rfids_[i]->setVisualizationStatus(visualization_status_);
00660     }
00661     for(unsigned int i = 0 ; i < co2_sensors_.size() ; i++)
00662     {
00663       co2_sensors_[i]->setVisualizationStatus(visualization_status_);
00664     }
00665     for(unsigned int i = 0 ; i < thermal_sensors_.size() ; i++)
00666     {
00667       thermal_sensors_[i]->setVisualizationStatus(visualization_status_);
00668     }
00669     for(unsigned int i = 0 ; i < sound_sensors_.size() ; i++)
00670     {
00671       sound_sensors_[i]->setVisualizationStatus(visualization_status_);
00672     }
00673   }
00674   
00679   std::vector<float> CGuiRobot::getSpeeds(void)
00680   {
00681     std::vector<float> speeds;
00682     speeds.push_back(linear_speed_);
00683     speeds.push_back(linear_speed_y_);
00684     speeds.push_back(angular_speed_);
00685     return speeds; 
00686   }
00687   
00693   void CGuiRobot::setEnvironmentalTags(stdr_msgs::RfidTagVector env_tags)
00694   {
00695     for(unsigned int i = 0 ; i < rfids_.size() ; i++)
00696     {
00697       rfids_[i]->setEnvironmentalTags(env_tags);
00698     }
00699   }
00700   
00704   void CGuiRobot::setEnvironmentalCO2Sources(stdr_msgs::CO2SourceVector env_)
00705   {
00706     for(unsigned int i = 0 ; i < co2_sensors_.size() ; i++)
00707     {
00708       co2_sensors_[i]->setEnvironmentalCO2Sources(env_);
00709     }
00710   }
00714   void CGuiRobot::setEnvironmentalThermalSources(stdr_msgs::ThermalSourceVector env_)
00715   {
00716     for(unsigned int i = 0 ; i < thermal_sensors_.size() ; i++)
00717     {
00718       thermal_sensors_[i]->setEnvironmentalThermalSources(env_);
00719     }
00720   }
00724   void CGuiRobot::setEnvironmentalSoundSources(stdr_msgs::SoundSourceVector env_)
00725   {
00726     for(unsigned int i = 0 ; i < sound_sensors_.size() ; i++)
00727     {
00728       sound_sensors_[i]->setEnvironmentalSoundSources(env_);
00729     }
00730   }
00731 }


stdr_gui
Author(s): Manos Tsardoulias
autogenerated on Thu Jun 6 2019 18:57:38