00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030 #include "turtlesim/turtle_frame.h"
00031
00032 #include <QPointF>
00033
00034 #include <ros/package.h>
00035 #include <cstdlib>
00036 #include <ctime>
00037
00038 #define DEFAULT_BG_R 0x45
00039 #define DEFAULT_BG_G 0x56
00040 #define DEFAULT_BG_B 0xff
00041
00042 namespace turtlesim
00043 {
00044
00045 TurtleFrame::TurtleFrame(QWidget* parent, Qt::WindowFlags f)
00046 : QFrame(parent, f)
00047 , path_image_(500, 500, QImage::Format_ARGB32)
00048 , path_painter_(&path_image_)
00049 , frame_count_(0)
00050 , id_counter_(0)
00051 {
00052 setFixedSize(500, 500);
00053 setWindowTitle("TurtleSim");
00054
00055 srand(time(NULL));
00056
00057 update_timer_ = new QTimer(this);
00058 update_timer_->setInterval(16);
00059 update_timer_->start();
00060
00061 connect(update_timer_, SIGNAL(timeout()), this, SLOT(onUpdate()));
00062
00063 nh_.setParam("background_r", DEFAULT_BG_R);
00064 nh_.setParam("background_g", DEFAULT_BG_G);
00065 nh_.setParam("background_b", DEFAULT_BG_B);
00066
00067 QVector<QString> turtles;
00068 turtles.append("box-turtle.png");
00069 turtles.append("robot-turtle.png");
00070 turtles.append("sea-turtle.png");
00071 turtles.append("diamondback.png");
00072 turtles.append("electric.png");
00073 turtles.append("fuerte.png");
00074 turtles.append("groovy.png");
00075 turtles.append("hydro.svg");
00076 turtles.append("indigo.svg");
00077
00078 QString images_path = (ros::package::getPath("turtlesim") + "/images/").c_str();
00079 for (int i = 0; i < turtles.size(); ++i)
00080 {
00081 QImage img;
00082 img.load(images_path + turtles[i]);
00083 turtle_images_.append(img);
00084 }
00085
00086 meter_ = turtle_images_[0].height();
00087
00088 clear();
00089
00090 clear_srv_ = nh_.advertiseService("clear", &TurtleFrame::clearCallback, this);
00091 reset_srv_ = nh_.advertiseService("reset", &TurtleFrame::resetCallback, this);
00092 spawn_srv_ = nh_.advertiseService("spawn", &TurtleFrame::spawnCallback, this);
00093 kill_srv_ = nh_.advertiseService("kill", &TurtleFrame::killCallback, this);
00094
00095 ROS_INFO("Starting turtlesim with node name %s", ros::this_node::getName().c_str()) ;
00096
00097 width_in_meters_ = (width() - 1) / meter_;
00098 height_in_meters_ = (height() - 1) / meter_;
00099 spawnTurtle("", width_in_meters_ / 2.0, height_in_meters_ / 2.0, 0);
00100
00101
00102 if(FALSE)
00103 {
00104 for(int index = 0; index < turtles.size(); ++index)
00105 {
00106 QString name = turtles[index];
00107 name = name.split(".").first();
00108 name.replace(QString("-"), QString(""));
00109 spawnTurtle(name.toStdString(), 1.0 + 1.5 * (index % 7), 1.0 + 1.5 * (index / 7), PI / 2.0, index);
00110 }
00111 }
00112 }
00113
00114 TurtleFrame::~TurtleFrame()
00115 {
00116 delete update_timer_;
00117 }
00118
00119 bool TurtleFrame::spawnCallback(turtlesim::Spawn::Request& req, turtlesim::Spawn::Response& res)
00120 {
00121 std::string name = spawnTurtle(req.name, req.x, req.y, req.theta);
00122 if (name.empty())
00123 {
00124 ROS_ERROR("A turtled named [%s] already exists", req.name.c_str());
00125 return false;
00126 }
00127
00128 res.name = name;
00129
00130 return true;
00131 }
00132
00133 bool TurtleFrame::killCallback(turtlesim::Kill::Request& req, turtlesim::Kill::Response&)
00134 {
00135 M_Turtle::iterator it = turtles_.find(req.name);
00136 if (it == turtles_.end())
00137 {
00138 ROS_ERROR("Tried to kill turtle [%s], which does not exist", req.name.c_str());
00139 return false;
00140 }
00141
00142 turtles_.erase(it);
00143 update();
00144
00145 return true;
00146 }
00147
00148 bool TurtleFrame::hasTurtle(const std::string& name)
00149 {
00150 return turtles_.find(name) != turtles_.end();
00151 }
00152
00153 std::string TurtleFrame::spawnTurtle(const std::string& name, float x, float y, float angle)
00154 {
00155 return spawnTurtle(name, x, y, angle, rand() % turtle_images_.size());
00156 }
00157
00158 std::string TurtleFrame::spawnTurtle(const std::string& name, float x, float y, float angle, size_t index)
00159 {
00160 std::string real_name = name;
00161 if (real_name.empty())
00162 {
00163 do
00164 {
00165 std::stringstream ss;
00166 ss << "turtle" << ++id_counter_;
00167 real_name = ss.str();
00168 } while (hasTurtle(real_name));
00169 }
00170 else
00171 {
00172 if (hasTurtle(real_name))
00173 {
00174 return "";
00175 }
00176 }
00177
00178 TurtlePtr t(new Turtle(ros::NodeHandle(real_name), turtle_images_[index], QPointF(x, height_in_meters_ - y), angle));
00179 turtles_[real_name] = t;
00180 update();
00181
00182 ROS_INFO("Spawning turtle [%s] at x=[%f], y=[%f], theta=[%f]", real_name.c_str(), x, y, angle);
00183
00184 return real_name;
00185 }
00186
00187 void TurtleFrame::clear()
00188 {
00189 int r = DEFAULT_BG_R;
00190 int g = DEFAULT_BG_G;
00191 int b = DEFAULT_BG_B;
00192
00193 nh_.param("background_r", r, r);
00194 nh_.param("background_g", g, g);
00195 nh_.param("background_b", b, b);
00196
00197 path_image_.fill(qRgb(r, g, b));
00198 update();
00199 }
00200
00201 void TurtleFrame::onUpdate()
00202 {
00203 ros::spinOnce();
00204
00205 updateTurtles();
00206
00207 if (!ros::ok())
00208 {
00209 close();
00210 }
00211 }
00212
00213 void TurtleFrame::paintEvent(QPaintEvent*)
00214 {
00215 QPainter painter(this);
00216
00217 painter.drawImage(QPoint(0, 0), path_image_);
00218
00219 M_Turtle::iterator it = turtles_.begin();
00220 M_Turtle::iterator end = turtles_.end();
00221 for (; it != end; ++it)
00222 {
00223 it->second->paint(painter);
00224 }
00225 }
00226
00227 void TurtleFrame::updateTurtles()
00228 {
00229 if (last_turtle_update_.isZero())
00230 {
00231 last_turtle_update_ = ros::WallTime::now();
00232 return;
00233 }
00234
00235 bool modified = false;
00236 M_Turtle::iterator it = turtles_.begin();
00237 M_Turtle::iterator end = turtles_.end();
00238 for (; it != end; ++it)
00239 {
00240 modified |= it->second->update(0.001 * update_timer_->interval(), path_painter_, path_image_, width_in_meters_, height_in_meters_);
00241 }
00242 if (modified)
00243 {
00244 update();
00245 }
00246
00247 ++frame_count_;
00248 }
00249
00250
00251 bool TurtleFrame::clearCallback(std_srvs::Empty::Request&, std_srvs::Empty::Response&)
00252 {
00253 ROS_INFO("Clearing turtlesim.");
00254 clear();
00255 return true;
00256 }
00257
00258 bool TurtleFrame::resetCallback(std_srvs::Empty::Request&, std_srvs::Empty::Response&)
00259 {
00260 ROS_INFO("Resetting turtlesim.");
00261 turtles_.clear();
00262 id_counter_ = 0;
00263 spawnTurtle("", width_in_meters_ / 2.0, height_in_meters_ / 2.0, 0);
00264 clear();
00265 return true;
00266 }
00267
00268 }