scoutos / scout / scoutsim / src / ghost_scout.cpp @ 6ebee82c
History | View | Annotate | Download (4.77 KB)
1 |
/**
|
---|---|
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 |
*
|
6 |
* 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 |
* All rights reserved.
|
25 |
*
|
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 |
*/
|
47 |
|
48 |
#include "ghost_scout.h" |
49 |
|
50 |
#include <wx/wx.h> |
51 |
|
52 |
using namespace std; |
53 |
|
54 |
namespace scoutsim
|
55 |
{ |
56 |
GhostScout::GhostScout(const ros::NodeHandle& nh,
|
57 |
const wxImage& scout_image,
|
58 |
const Vector2& pos,
|
59 |
wxBitmap *path_bitmap, |
60 |
float orient,
|
61 |
string scoutname)
|
62 |
: path_bitmap(path_bitmap) |
63 |
, node (nh) |
64 |
, scout_image(scout_image) |
65 |
, pos(pos) |
66 |
, start_x(pos.x) |
67 |
, start_y(pos.y) |
68 |
, orient(orient) |
69 |
, name(scoutname) |
70 |
, is_visible(false)
|
71 |
{ |
72 |
scout = wxBitmap(scout_image); |
73 |
|
74 |
position = node.subscribe("position", 1, &GhostScout::setPosition, this); |
75 |
|
76 |
} |
77 |
|
78 |
void GhostScout::setPosition(const ::messages::ScoutPosition& msg) |
79 |
{ |
80 |
pos.x = start_x + msg.x; |
81 |
pos.y = start_y - msg.y; |
82 |
orient = msg.theta; |
83 |
} |
84 |
|
85 |
// Scale to linesensor value
|
86 |
unsigned int GhostScout::rgb_to_grey(unsigned char r, |
87 |
unsigned char g, |
88 |
unsigned char b) |
89 |
{ |
90 |
// Should be 0 to 255
|
91 |
unsigned int grey = ((unsigned int) r + (unsigned int) g + (unsigned int) b) / 3; |
92 |
|
93 |
/// @todo Convert to the proper range
|
94 |
return 255 - grey; |
95 |
} |
96 |
|
97 |
/// Sends back the position of this scout so scoutsim can save
|
98 |
/// the world state
|
99 |
geometry_msgs::Pose2D GhostScout::update(double dt,
|
100 |
wxMemoryDC& path_dc, |
101 |
wxMemoryDC& sonar_dc, |
102 |
wxColour background_color, |
103 |
world_state state) |
104 |
{ |
105 |
|
106 |
wxImage rotated_image = scout_image.Rotate(orient - M_PI/2.0, |
107 |
wxPoint(scout_image.GetWidth() / 2,
|
108 |
scout_image.GetHeight() / 2),
|
109 |
false);
|
110 |
|
111 |
for (int y = 0; y < rotated_image.GetHeight(); ++y) |
112 |
{ |
113 |
for (int x = 0; x < rotated_image.GetWidth(); ++x) |
114 |
{ |
115 |
if (rotated_image.GetRed(x, y) == 255 |
116 |
&& rotated_image.GetBlue(x, y) == 255
|
117 |
&& rotated_image.GetGreen(x, y) == 255)
|
118 |
{ |
119 |
rotated_image.SetAlpha(x, y, 0);
|
120 |
} |
121 |
else
|
122 |
{ |
123 |
rotated_image.SetAlpha(x, y, 125);
|
124 |
} |
125 |
} |
126 |
} |
127 |
|
128 |
scout = wxBitmap(rotated_image); |
129 |
|
130 |
geometry_msgs::Pose2D my_pose; |
131 |
my_pose.x = pos.x; |
132 |
my_pose.y = pos.y; |
133 |
my_pose.theta = orient; |
134 |
|
135 |
return my_pose;
|
136 |
} |
137 |
|
138 |
void GhostScout::paint(wxDC& dc)
|
139 |
{ |
140 |
if (!is_visible)
|
141 |
{ |
142 |
return;
|
143 |
} |
144 |
|
145 |
wxSize scout_size = wxSize(scout.GetWidth(), scout.GetHeight()); |
146 |
dc.DrawBitmap(scout, pos.x * PIX_PER_METER - (scout_size.GetWidth() / 2),
|
147 |
pos.y * PIX_PER_METER - (scout_size.GetHeight() / 2), true); |
148 |
} |
149 |
|
150 |
string GhostScout::get_name()
|
151 |
{ |
152 |
return name;
|
153 |
} |
154 |
|
155 |
void GhostScout::set_visible(bool vis) |
156 |
{ |
157 |
is_visible = vis; |
158 |
} |
159 |
} |