scoutos / scout / scoutsim / src / ghost_scout.cpp @ 28999371
History | View | Annotate | Download (4.62 KB)
1 | 0ccfc42d | Priya | /**
|
---|---|---|---|
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 | { |
||
71 | scout = wxBitmap(scout_image); |
||
72 | |||
73 | position = node.subscribe("position", 1, &GhostScout::setPosition, this); |
||
74 | |||
75 | } |
||
76 | |||
77 | void GhostScout::setPosition(const ::messages::ScoutPosition& msg) |
||
78 | { |
||
79 | pos.x = start_x + msg.x; |
||
80 | pos.y = start_y - msg.y; |
||
81 | orient = msg.theta; |
||
82 | } |
||
83 | |||
84 | // Scale to linesensor value
|
||
85 | unsigned int GhostScout::rgb_to_grey(unsigned char r, |
||
86 | unsigned char g, |
||
87 | unsigned char b) |
||
88 | { |
||
89 | // Should be 0 to 255
|
||
90 | unsigned int grey = ((unsigned int) r + (unsigned int) g + (unsigned int) b) / 3; |
||
91 | |||
92 | /// @todo Convert to the proper range
|
||
93 | return 255 - grey; |
||
94 | } |
||
95 | |||
96 | /// Sends back the position of this scout so scoutsim can save
|
||
97 | /// the world state
|
||
98 | geometry_msgs::Pose2D GhostScout::update(double dt,
|
||
99 | wxMemoryDC& path_dc, |
||
100 | wxMemoryDC& sonar_dc, |
||
101 | wxColour background_color, |
||
102 | world_state state) |
||
103 | { |
||
104 | |||
105 | ddfeb111 | Priya | wxImage rotated_image = scout_image.Rotate(orient - M_PI/2.0, |
106 | wxPoint(scout_image.GetWidth() / 2,
|
||
107 | scout_image.GetHeight() / 2),
|
||
108 | false);
|
||
109 | |||
110 | for (int y = 0; y < rotated_image.GetHeight(); ++y) |
||
111 | { |
||
112 | for (int x = 0; x < rotated_image.GetWidth(); ++x) |
||
113 | { |
||
114 | if (rotated_image.GetRed(x, y) == 255 |
||
115 | && rotated_image.GetBlue(x, y) == 255
|
||
116 | && rotated_image.GetGreen(x, y) == 255)
|
||
117 | { |
||
118 | rotated_image.SetAlpha(x, y, 0);
|
||
119 | } |
||
120 | else
|
||
121 | { |
||
122 | rotated_image.SetAlpha(x, y, 125);
|
||
123 | } |
||
124 | } |
||
125 | } |
||
126 | |||
127 | scout = wxBitmap(rotated_image); |
||
128 | |||
129 | 0ccfc42d | Priya | geometry_msgs::Pose2D my_pose; |
130 | my_pose.x = pos.x; |
||
131 | my_pose.y = pos.y; |
||
132 | my_pose.theta = orient; |
||
133 | |||
134 | 28999371 | Alex | //ROS_INFO("Scout position %f %f %f", pos.x, pos.y, orient);
|
135 | 0ccfc42d | Priya | |
136 | return my_pose;
|
||
137 | } |
||
138 | |||
139 | void GhostScout::paint(wxDC& dc)
|
||
140 | { |
||
141 | wxSize scout_size = wxSize(scout.GetWidth(), scout.GetHeight()); |
||
142 | dc.DrawBitmap(scout, pos.x * PIX_PER_METER - (scout_size.GetWidth() / 2),
|
||
143 | pos.y * PIX_PER_METER - (scout_size.GetHeight() / 2), true); |
||
144 | } |
||
145 | } |