scoutos / scout / scoutsim / src / sim_frame.cpp @ a692ef82
History | View | Annotate | Download (27.7 KB)
1 | c492be62 | Alex Zirbel | /**
|
---|---|---|---|
2 | * The code in this package was developed using the structure of Willow
|
||
3 | * Garage's turtlesim package. It was modified by the CMU Robotics Club
|
||
4 | * to be used as a simulator for the Colony Scout robot.
|
||
5 | 266ae7f2 | Alex Zirbel | *
|
6 | c492be62 | Alex Zirbel | * All redistribution of this code is limited to the terms of Willow Garage's
|
7 | * licensing terms, as well as under permission from the CMU Robotics Club.
|
||
8 | *
|
||
9 | * Copyright (c) 2011 Colony Project
|
||
10 | *
|
||
11 | * Permission is hereby granted, free of charge, to any person
|
||
12 | * obtaining a copy of this software and associated documentation
|
||
13 | * files (the "Software"), to deal in the Software without
|
||
14 | * restriction, including without limitation the rights to use,
|
||
15 | * copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||
16 | * copies of the Software, and to permit persons to whom the
|
||
17 | * Software is furnished to do so, subject to the following
|
||
18 | * conditions:
|
||
19 | *
|
||
20 | * The above copyright notice and this permission notice shall be
|
||
21 | * included in all copies or substantial portions of the Software.
|
||
22 | *
|
||
23 | * Copyright (c) 2009, Willow Garage, Inc.
|
||
24 | 266ae7f2 | Alex Zirbel | * All rights reserved.
|
25 | c492be62 | Alex Zirbel | *
|
26 | * Redistribution and use in source and binary forms, with or without
|
||
27 | * modification, are permitted provided that the following conditions are met:
|
||
28 | *
|
||
29 | * Redistributions of source code must retain the above copyright
|
||
30 | * notice, this list of conditions and the following disclaimer.
|
||
31 | * Redistributions in binary form must reproduce the above copyright
|
||
32 | * notice, this list of conditions and the following disclaimer in the
|
||
33 | * documentation and/or other materials provided with the distribution.
|
||
34 | * Neither the name of the Willow Garage, Inc. nor the names of its
|
||
35 | * contributors may be used to endorse or promote products derived from
|
||
36 | * this software without specific prior written permission.
|
||
37 | *
|
||
38 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
||
39 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
|
||
40 | * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
||
41 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
|
||
42 | * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
|
||
43 | * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
||
44 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
|
||
45 | * OTHER DEALINGS IN THE SOFTWARE.
|
||
46 | 266ae7f2 | Alex Zirbel | */
|
47 | |||
48 | 3a73516c | Alex | /**
|
49 | * @file sim_frame.cpp
|
||
50 | *
|
||
51 | * @ingroup scoutsim
|
||
52 | * @{
|
||
53 | */
|
||
54 | |||
55 | a8480867 | Alex Zirbel | #include "sim_frame.h" |
56 | 266ae7f2 | Alex Zirbel | |
57 | 2eaafff2 | Alex | #include <stdio.h> |
58 | |||
59 | 266ae7f2 | Alex Zirbel | #include <ros/package.h> |
60 | #include <cstdlib> |
||
61 | #include <ctime> |
||
62 | |||
63 | 2eaafff2 | Alex | using namespace std; |
64 | 266ae7f2 | Alex Zirbel | |
65 | namespace scoutsim
|
||
66 | { |
||
67 | 2eaafff2 | Alex | SimFrame::SimFrame(wxWindow* parent, string map_name)
|
68 | 266ae7f2 | Alex Zirbel | : wxFrame(parent, wxID_ANY, wxT("ScoutSim"), wxDefaultPosition,
|
69 | wxSize(500, 500), wxDEFAULT_FRAME_STYLE & ~wxRESIZE_BORDER) |
||
70 | 144137a1 | Alex Zirbel | , frame_count(0)
|
71 | , id_counter(0)
|
||
72 | 46ed9b9b | Yuyang (Misty) Guo | , BOM_counter(0)
|
73 | 266ae7f2 | Alex Zirbel | { |
74 | 2eaafff2 | Alex | std::cout << "Constructing sim frame." << std::endl;
|
75 | |||
76 | 266ae7f2 | Alex Zirbel | srand(time(NULL));
|
77 | |||
78 | 144137a1 | Alex Zirbel | update_timer = new wxTimer(this); |
79 | 7f095440 | Priya | update_timer->Start(REAL_TIME_REFRESH_RATE * 1000);
|
80 | 266ae7f2 | Alex Zirbel | |
81 | 144137a1 | Alex Zirbel | Connect(update_timer->GetId(), wxEVT_TIMER, |
82 | 266ae7f2 | Alex Zirbel | wxTimerEventHandler(SimFrame::onUpdate), NULL, this); |
83 | Connect(wxEVT_PAINT, wxPaintEventHandler(SimFrame::onPaint), |
||
84 | NULL, this); |
||
85 | |||
86 | 594c3bb9 | Alex | images_path = ros::package::getPath("scoutsim") + "/images/"; |
87 | 266ae7f2 | Alex Zirbel | |
88 | ade1b7f9 | Alex | map_base_name = ros::package::getPath("scoutsim") + "/maps/" + |
89 | map_name + ".bmp";
|
||
90 | map_lines_name = ros::package::getPath("scoutsim") + "/maps/" + |
||
91 | map_name + "_lines.bmp";
|
||
92 | 7ffad595 | Alex | map_walls_name = ros::package::getPath("scoutsim") + "/maps/" + |
93 | map_name + "_walls.bmp";
|
||
94 | ade1b7f9 | Alex | display_map_name = map_base_name; |
95 | |||
96 | wxBitmap lines_bitmap; |
||
97 | e2770306 | Alex | wxBitmap walls_bitmap; |
98 | 4c9fb6ba | viki | ROS_INFO("Loading map: %s", display_map_name.c_str());
|
99 | e2770306 | Alex | path_bitmap.LoadFile(wxString::FromAscii(display_map_name.c_str())); |
100 | |||
101 | // Try to load the file; if it fails, make a new blank file
|
||
102 | if (!lines_bitmap.LoadFile(wxString::FromAscii(map_lines_name.c_str())))
|
||
103 | { |
||
104 | lines_bitmap = wxBitmap(path_bitmap.GetWidth(), path_bitmap.GetHeight(), 3);
|
||
105 | } |
||
106 | ade1b7f9 | Alex | lines_image = lines_bitmap.ConvertToImage(); |
107 | |||
108 | e2770306 | Alex | // Try to load the file; if it fails, make a new blank file
|
109 | if (!walls_bitmap.LoadFile(wxString::FromAscii(map_walls_name.c_str())))
|
||
110 | { |
||
111 | walls_bitmap = wxBitmap(path_bitmap.GetWidth(), path_bitmap.GetHeight(), 3);
|
||
112 | } |
||
113 | 7ffad595 | Alex | walls_image = walls_bitmap.ConvertToImage(); |
114 | |||
115 | 266ae7f2 | Alex Zirbel | clear(); |
116 | |||
117 | 144137a1 | Alex Zirbel | clear_srv = nh.advertiseService("clear",
|
118 | 4612f7e4 | Alex Zirbel | &SimFrame::clearCallback, this);
|
119 | 144137a1 | Alex Zirbel | reset_srv = nh.advertiseService("reset",
|
120 | 4612f7e4 | Alex Zirbel | &SimFrame::resetCallback, this);
|
121 | 144137a1 | Alex Zirbel | spawn_srv = nh.advertiseService("spawn",
|
122 | 4612f7e4 | Alex Zirbel | &SimFrame::spawnCallback, this);
|
123 | 0e143737 | Hui Jun Tay | spawn_em_srv = nh.advertiseService("spawn_em",
|
124 | &SimFrame::spawnEmCallback, this);
|
||
125 | 144137a1 | Alex Zirbel | kill_srv = nh.advertiseService("kill",
|
126 | 43811241 | Alex | &SimFrame::killCallback, this);
|
127 | set_sonar_viz_srv = nh.advertiseService("set_sonar_viz",
|
||
128 | &SimFrame::setSonarVizCallback, this);
|
||
129 | set_ghost_srv = nh.advertiseService("set_ghost",
|
||
130 | &SimFrame::setGhostCallback, this);
|
||
131 | set_teleop_srv = nh.advertiseService("set_teleop",
|
||
132 | &SimFrame::setTeleopCallback, this);
|
||
133 | 266ae7f2 | Alex Zirbel | |
134 | 82f3f746 | Priya | // Subscribe and publisher wirless from robots
|
135 | 7ffad595 | Alex | wireless_receive = nh.advertise< ::messages::WirelessPacket>( |
136 | "/wireless/receive", 1000); |
||
137 | wireless_send = nh.subscribe("/wireless/send", 1000, |
||
138 | &SimFrame::wirelessCallback, this);
|
||
139 | 82f3f746 | Priya | |
140 | 0e77683c | Alex | // Teleop
|
141 | 43811241 | Alex | teleop_type = TELEOP_OFF; |
142 | 0e77683c | Alex | teleop_l_speed = 0;
|
143 | teleop_r_speed = 0;
|
||
144 | 60a90290 | Hui Jun Tay | teleop_scoutname = "scout1";
|
145 | |||
146 | 11bb78c2 | Hui Jun Tay | //Emitter default values
|
147 | em_aperture = PI / 3.0; |
||
148 | em_distance = 1.0; |
||
149 | |||
150 | |||
151 | 6ebee82c | Alex | teleop_pub = nh.advertise< ::messages::set_motors>("/scout1/set_motors", 1000); |
152 | 0e77683c | Alex | |
153 | 266ae7f2 | Alex Zirbel | ROS_INFO("Starting scoutsim with node name %s",
|
154 | ros::this_node::getName().c_str()) ; |
||
155 | |||
156 | 2eaafff2 | Alex | wxMenu *menuFile = new wxMenu;
|
157 | menuFile->Append(ID_ABOUT, _("&About"));
|
||
158 | menuFile->AppendSeparator(); |
||
159 | menuFile->Append(ID_QUIT, _("E&xit"));
|
||
160 | |||
161 | wxMenu *menuSim = new wxMenu;
|
||
162 | menuSim->Append(ID_CLEAR, _("&Clear"));
|
||
163 | |||
164 | wxMenu *menuView = new wxMenu;
|
||
165 | menuView->Append(ID_MAP, _("&Map"));
|
||
166 | menuView->Append(ID_LINES, _("&Lines"));
|
||
167 | 7ffad595 | Alex | menuView->Append(ID_WALLS, _("&Walls"));
|
168 | 2eaafff2 | Alex | |
169 | 0e77683c | Alex | wxMenu *menuTeleop = new wxMenu;
|
170 | menuTeleop->Append(ID_TELEOP_NONE, _("&None"));
|
||
171 | menuTeleop->Append(ID_TELEOP_PRECISE, _("&Precise"));
|
||
172 | menuTeleop->Append(ID_TELEOP_FLUID, _("&Fluid"));
|
||
173 | |||
174 | 2eaafff2 | Alex | wxMenuBar *menuBar = new wxMenuBar;
|
175 | menuBar->Append(menuFile, _("&File"));
|
||
176 | menuBar->Append(menuSim, _("&Sim"));
|
||
177 | menuBar->Append(menuView, _("&View"));
|
||
178 | 0e77683c | Alex | menuBar->Append(menuTeleop, _("&Teleop"));
|
179 | 2eaafff2 | Alex | |
180 | SetMenuBar(menuBar); |
||
181 | |||
182 | c63c9752 | Alex | width_in_meters = GetSize().GetWidth() / PIX_PER_METER; |
183 | height_in_meters = GetSize().GetHeight() / PIX_PER_METER; |
||
184 | 25694a03 | Priya | |
185 | spawnScout("scout1", 1.4, .78, 0); |
||
186 | 266ae7f2 | Alex Zirbel | } |
187 | |||
188 | SimFrame::~SimFrame() |
||
189 | { |
||
190 | 144137a1 | Alex Zirbel | delete update_timer;
|
191 | 266ae7f2 | Alex Zirbel | } |
192 | |||
193 | 4612f7e4 | Alex Zirbel | bool SimFrame::spawnCallback(scoutsim::Spawn::Request &req,
|
194 | scoutsim::Spawn::Response &res) |
||
195 | 266ae7f2 | Alex Zirbel | { |
196 | std::string name = spawnScout(req.name, req.x, req.y, req.theta);
|
||
197 | if (name.empty())
|
||
198 | { |
||
199 | 43811241 | Alex | ROS_WARN("A scout named [%s] already exists", req.name.c_str());
|
200 | 266ae7f2 | Alex Zirbel | return false; |
201 | } |
||
202 | |||
203 | res.name = name; |
||
204 | return true; |
||
205 | } |
||
206 | |||
207 | 0e143737 | Hui Jun Tay | |
208 | bool SimFrame::spawnEmCallback(scoutsim::Spawn::Request &req,
|
||
209 | scoutsim::Spawn::Response &res) |
||
210 | { |
||
211 | std::string name = spawnEmitter(req.name, req.x, req.y, req.theta);
|
||
212 | if (name.empty())
|
||
213 | { |
||
214 | ROS_WARN("An emitter named [%s] already exists", req.name.c_str());
|
||
215 | return false; |
||
216 | } |
||
217 | res.name = name; |
||
218 | return true; |
||
219 | } |
||
220 | |||
221 | 266ae7f2 | Alex Zirbel | bool SimFrame::killCallback(scoutsim::Kill::Request& req,
|
222 | scoutsim::Kill::Response&) |
||
223 | { |
||
224 | 144137a1 | Alex Zirbel | M_Scout::iterator it = scouts.find(req.name); |
225 | 98ed4757 | Hui Jun Tay | M_Emitter::iterator m_it = emitters.find(req.name); |
226 | 144137a1 | Alex Zirbel | if (it == scouts.end())
|
227 | 266ae7f2 | Alex Zirbel | { |
228 | 98ed4757 | Hui Jun Tay | if (m_it == emitters.end()) {
|
229 | ROS_WARN("Tried to kill scout/emitter [%s], which does not exist",
|
||
230 | req.name.c_str()); |
||
231 | return false; |
||
232 | } |
||
233 | emitters.erase(m_it); |
||
234 | return true; |
||
235 | 266ae7f2 | Alex Zirbel | } |
236 | |||
237 | 144137a1 | Alex Zirbel | scouts.erase(it); |
238 | 266ae7f2 | Alex Zirbel | |
239 | return true; |
||
240 | } |
||
241 | |||
242 | 43811241 | Alex | bool SimFrame::setSonarVizCallback(SetSonarViz::Request& req,
|
243 | SetSonarViz::Response&) |
||
244 | { |
||
245 | M_Scout::iterator it = scouts.find(req.scout_name); |
||
246 | if (it == scouts.end())
|
||
247 | { |
||
248 | ROS_WARN("Tried to set sonar on scout [%s], which does not exist",
|
||
249 | req.scout_name.c_str()); |
||
250 | return false; |
||
251 | } |
||
252 | |||
253 | it->second->set_sonar_visual(req.on); |
||
254 | return true; |
||
255 | } |
||
256 | |||
257 | bool SimFrame::setGhostCallback(SetGhost::Request& req,
|
||
258 | SetGhost::Response&) |
||
259 | { |
||
260 | for (unsigned int i=0; i < ghost_scouts.size(); ++i) |
||
261 | { |
||
262 | if (ghost_scouts.at(i)->get_name() == req.scout_name)
|
||
263 | { |
||
264 | ghost_scouts.at(i)->set_visible(req.on); |
||
265 | return true; |
||
266 | } |
||
267 | } |
||
268 | |||
269 | ROS_WARN("Tried to set ghost on scout [%s], which does not exist",
|
||
270 | req.scout_name.c_str()); |
||
271 | return false; |
||
272 | } |
||
273 | |||
274 | bool SimFrame::setTeleopCallback(SetTeleop::Request& req,
|
||
275 | SetTeleop::Response&) |
||
276 | { |
||
277 | std::stringstream ss; |
||
278 | ss << "/" << req.scout_name << "/set_motors"; |
||
279 | 6ebee82c | Alex | teleop_pub = nh.advertise< ::messages::set_motors>(ss.str(), 1000);
|
280 | 60a90290 | Hui Jun Tay | teleop_scoutname = req.scout_name.c_str(); |
281 | ROS_INFO("Teleoping %s...",teleop_scoutname.c_str()); //debug statement |
||
282 | 43811241 | Alex | |
283 | return true; |
||
284 | } |
||
285 | |||
286 | 266ae7f2 | Alex Zirbel | bool SimFrame::hasScout(const std::string& name) |
287 | { |
||
288 | 144137a1 | Alex Zirbel | return scouts.find(name) != scouts.end();
|
289 | 266ae7f2 | Alex Zirbel | } |
290 | |||
291 | 0e143737 | Hui Jun Tay | bool SimFrame::hasEmitter(const std::string& name) |
292 | { |
||
293 | return emitters.find(name) != emitters.end();
|
||
294 | } |
||
295 | |||
296 | 9b3564f3 | Alex Zirbel | std::string SimFrame::spawnScout(const std::string& name, |
297 | float x, float y, float angle) |
||
298 | 266ae7f2 | Alex Zirbel | { |
299 | std::string real_name = name;
|
||
300 | if (real_name.empty())
|
||
301 | { |
||
302 | 43811241 | Alex | // Generate the name scoutX, where X is an increasing number.
|
303 | 266ae7f2 | Alex Zirbel | do
|
304 | { |
||
305 | std::stringstream ss; |
||
306 | 144137a1 | Alex Zirbel | ss << "scout" << ++id_counter;
|
307 | 266ae7f2 | Alex Zirbel | real_name = ss.str(); |
308 | 43811241 | Alex | } |
309 | while (hasScout(real_name));
|
||
310 | 266ae7f2 | Alex Zirbel | } |
311 | else
|
||
312 | { |
||
313 | if (hasScout(real_name))
|
||
314 | { |
||
315 | return ""; |
||
316 | } |
||
317 | } |
||
318 | |||
319 | 594c3bb9 | Alex | wxImage scout_image; |
320 | |||
321 | // Try to load a name-specific image; if not, load the default scout
|
||
322 | string specific_name = images_path + name + ".png"; |
||
323 | if (fileExists(specific_name))
|
||
324 | { |
||
325 | scout_image.LoadFile(wxString::FromAscii(specific_name.c_str())); |
||
326 | scout_image.SetMask(true);
|
||
327 | scout_image.SetMaskColour(255, 255, 255); |
||
328 | } |
||
329 | else
|
||
330 | { |
||
331 | scout_image.LoadFile( |
||
332 | wxString::FromAscii((images_path + "scout.png").c_str()));
|
||
333 | scout_image.SetMask(true);
|
||
334 | scout_image.SetMaskColour(255, 255, 255); |
||
335 | } |
||
336 | |||
337 | 266ae7f2 | Alex Zirbel | ScoutPtr t(new Scout(ros::NodeHandle(real_name),
|
338 | 594c3bb9 | Alex | scout_image, Vector2(x, y), &path_bitmap, angle)); |
339 | 144137a1 | Alex Zirbel | scouts[real_name] = t; |
340 | 266ae7f2 | Alex Zirbel | |
341 | 0e0ef125 | Priya | ghost_scouts.push_back(new GhostScout(ros::NodeHandle(real_name),
|
342 | scout_image, Vector2(x, y), &path_bitmap, angle, name)); |
||
343 | |||
344 | 266ae7f2 | Alex Zirbel | ROS_INFO("Spawning scout [%s] at x=[%f], y=[%f], theta=[%f]",
|
345 |