turtle_frame.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2009, Willow Garage, Inc.
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are met:
7  *
8  * * Redistributions of source code must retain the above copyright
9  * notice, this list of conditions and the following disclaimer.
10  * * Redistributions in binary form must reproduce the above copyright
11  * notice, this list of conditions and the following disclaimer in the
12  * documentation and/or other materials provided with the distribution.
13  * * Neither the name of the Willow Garage, Inc. nor the names of its
14  * contributors may be used to endorse or promote products derived from
15  * this software without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
18  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
21  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
25  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27  * POSSIBILITY OF SUCH DAMAGE.
28  */
29 
30 #include "turtlesim/turtle_frame.h"
31 
32 #include <QPointF>
33 
34 #include <ros/package.h>
35 #include <cstdlib>
36 #include <ctime>
37 
38 #define DEFAULT_BG_R 0x45
39 #define DEFAULT_BG_G 0x56
40 #define DEFAULT_BG_B 0xff
41 
42 namespace turtlesim
43 {
44 
45 TurtleFrame::TurtleFrame(QWidget* parent, Qt::WindowFlags f)
46 : QFrame(parent, f)
47 , path_image_(500, 500, QImage::Format_ARGB32)
48 , path_painter_(&path_image_)
49 , frame_count_(0)
50 , id_counter_(0)
51 , private_nh_("~")
52 {
53  setFixedSize(500, 500);
54  setWindowTitle("TurtleSim");
55 
56  srand(time(NULL));
57 
58  update_timer_ = new QTimer(this);
59  update_timer_->setInterval(16);
60  update_timer_->start();
61 
62  connect(update_timer_, SIGNAL(timeout()), this, SLOT(onUpdate()));
63 
64  if (!private_nh_.hasParam("background_r"))
65  {
66  private_nh_.setParam("background_r", DEFAULT_BG_R);
67  }
68  if (!private_nh_.hasParam("background_g"))
69  {
70  private_nh_.setParam("background_g", DEFAULT_BG_G);
71  }
72  if (!private_nh_.hasParam("background_b"))
73  {
74  private_nh_.setParam("background_b", DEFAULT_BG_B);
75  }
76 
77  QVector<QString> turtles;
78  turtles.append("box-turtle.png");
79  turtles.append("robot-turtle.png");
80  turtles.append("sea-turtle.png");
81  turtles.append("diamondback.png");
82  turtles.append("electric.png");
83  turtles.append("fuerte.png");
84  turtles.append("groovy.png");
85  turtles.append("hydro.svg");
86  turtles.append("indigo.svg");
87  turtles.append("jade.png");
88  turtles.append("kinetic.png");
89  turtles.append("lunar.png");
90  turtles.append("melodic.png");
91 
92  QString images_path = (ros::package::getPath("turtlesim") + "/images/").c_str();
93  for (int i = 0; i < turtles.size(); ++i)
94  {
95  QImage img;
96  img.load(images_path + turtles[i]);
97  turtle_images_.append(img);
98  }
99 
100  meter_ = turtle_images_[0].height();
101 
102  clear();
103 
108 
109  ROS_INFO("Starting turtlesim with node name %s", ros::this_node::getName().c_str()) ;
110 
111  width_in_meters_ = (width() - 1) / meter_;
112  height_in_meters_ = (height() - 1) / meter_;
113  spawnTurtle("", width_in_meters_ / 2.0, height_in_meters_ / 2.0, 0);
114 
115  // spawn all available turtle types
116  if(false)
117  {
118  for(int index = 0; index < turtles.size(); ++index)
119  {
120  QString name = turtles[index];
121  name = name.split(".").first();
122  name.replace(QString("-"), QString(""));
123  spawnTurtle(name.toStdString(), 1.0 + 1.5 * (index % 7), 1.0 + 1.5 * (index / 7), PI / 2.0, index);
124  }
125  }
126 }
127 
129 {
130  delete update_timer_;
131 }
132 
133 bool TurtleFrame::spawnCallback(turtlesim::Spawn::Request& req, turtlesim::Spawn::Response& res)
134 {
135  std::string name = spawnTurtle(req.name, req.x, req.y, req.theta);
136  if (name.empty())
137  {
138  ROS_ERROR("A turtled named [%s] already exists", req.name.c_str());
139  return false;
140  }
141 
142  res.name = name;
143 
144  return true;
145 }
146 
147 bool TurtleFrame::killCallback(turtlesim::Kill::Request& req, turtlesim::Kill::Response&)
148 {
149  M_Turtle::iterator it = turtles_.find(req.name);
150  if (it == turtles_.end())
151  {
152  ROS_ERROR("Tried to kill turtle [%s], which does not exist", req.name.c_str());
153  return false;
154  }
155 
156  turtles_.erase(it);
157  update();
158 
159  return true;
160 }
161 
162 bool TurtleFrame::hasTurtle(const std::string& name)
163 {
164  return turtles_.find(name) != turtles_.end();
165 }
166 
167 std::string TurtleFrame::spawnTurtle(const std::string& name, float x, float y, float angle)
168 {
169  return spawnTurtle(name, x, y, angle, rand() % turtle_images_.size());
170 }
171 
172 std::string TurtleFrame::spawnTurtle(const std::string& name, float x, float y, float angle, size_t index)
173 {
174  std::string real_name = name;
175  if (real_name.empty())
176  {
177  do
178  {
179  std::stringstream ss;
180  ss << "turtle" << ++id_counter_;
181  real_name = ss.str();
182  } while (hasTurtle(real_name));
183  }
184  else
185  {
186  if (hasTurtle(real_name))
187  {
188  return "";
189  }
190  }
191 
192  TurtlePtr t(new Turtle(ros::NodeHandle(real_name), turtle_images_[index], QPointF(x, height_in_meters_ - y), angle));
193  turtles_[real_name] = t;
194  update();
195 
196  ROS_INFO("Spawning turtle [%s] at x=[%f], y=[%f], theta=[%f]", real_name.c_str(), x, y, angle);
197 
198  return real_name;
199 }
200 
202 {
203  int r = DEFAULT_BG_R;
204  int g = DEFAULT_BG_G;
205  int b = DEFAULT_BG_B;
206 
207  private_nh_.param("background_r", r, r);
208  private_nh_.param("background_g", g, g);
209  private_nh_.param("background_b", b, b);
210 
211  path_image_.fill(qRgb(r, g, b));
212  update();
213 }
214 
216 {
217  ros::spinOnce();
218 
219  updateTurtles();
220 
221  if (!ros::ok())
222  {
223  close();
224  }
225 }
226 
227 void TurtleFrame::paintEvent(QPaintEvent*)
228 {
229  QPainter painter(this);
230 
231  painter.drawImage(QPoint(0, 0), path_image_);
232 
233  M_Turtle::iterator it = turtles_.begin();
234  M_Turtle::iterator end = turtles_.end();
235  for (; it != end; ++it)
236  {
237  it->second->paint(painter);
238  }
239 }
240 
242 {
244  {
246  return;
247  }
248 
249  bool modified = false;
250  M_Turtle::iterator it = turtles_.begin();
251  M_Turtle::iterator end = turtles_.end();
252  for (; it != end; ++it)
253  {
254  modified |= it->second->update(0.001 * update_timer_->interval(), path_painter_, path_image_, width_in_meters_, height_in_meters_);
255  }
256  if (modified)
257  {
258  update();
259  }
260 
261  ++frame_count_;
262 }
263 
264 
265 bool TurtleFrame::clearCallback(std_srvs::Empty::Request&, std_srvs::Empty::Response&)
266 {
267  ROS_INFO("Clearing turtlesim.");
268  clear();
269  return true;
270 }
271 
272 bool TurtleFrame::resetCallback(std_srvs::Empty::Request&, std_srvs::Empty::Response&)
273 {
274  ROS_INFO("Resetting turtlesim.");
275  turtles_.clear();
276  id_counter_ = 0;
277  spawnTurtle("", width_in_meters_ / 2.0, height_in_meters_ / 2.0, 0);
278  clear();
279  return true;
280 }
281 
282 }
#define DEFAULT_BG_R
bool hasTurtle(const std::string &name)
bool spawnCallback(turtlesim::Spawn::Request &, turtlesim::Spawn::Response &)
ros::ServiceServer kill_srv_
Definition: turtle_frame.h:91
bool clearCallback(std_srvs::Empty::Request &, std_srvs::Empty::Response &)
void paintEvent(QPaintEvent *event)
ROSCPP_DECL const std::string & getName()
ServiceServer advertiseService(const std::string &service, bool(T::*srv_func)(MReq &, MRes &), T *obj)
ros::ServiceServer clear_srv_
Definition: turtle_frame.h:88
void update(const std::string &key, const XmlRpc::XmlRpcValue &v)
ros::NodeHandle nh_
Definition: turtle_frame.h:78
ros::NodeHandle private_nh_
Definition: turtle_frame.h:79
bool param(const std::string &param_name, T &param_val, const T &default_val) const
#define PI
Definition: turtle.h:51
#define ROS_INFO(...)
TurtleFrame(QWidget *parent=0, Qt::WindowFlags f=0)
ROSCPP_DECL bool ok()
bool hasParam(const std::string &key) const
ROSLIB_DECL std::string getPath(const std::string &package_name)
bool killCallback(turtlesim::Kill::Request &, turtlesim::Kill::Response &)
bool resetCallback(std_srvs::Empty::Request &, std_srvs::Empty::Response &)
static WallTime now()
void setParam(const std::string &key, const XmlRpc::XmlRpcValue &v) const
#define DEFAULT_BG_G
ros::ServiceServer spawn_srv_
Definition: turtle_frame.h:90
ros::ServiceServer reset_srv_
Definition: turtle_frame.h:89
std::string spawnTurtle(const std::string &name, float x, float y, float angle)
ROSCPP_DECL void spinOnce()
#define ROS_ERROR(...)
ros::WallTime last_turtle_update_
Definition: turtle_frame.h:86
#define DEFAULT_BG_B
QVector< QImage > turtle_images_
Definition: turtle_frame.h:97


turtlesim
Author(s): Josh Faust, Dirk Thomas
autogenerated on Mon Feb 28 2022 23:31:12