Statistics
| Branch: | Revision:

root / scout / scoutsim / src / ghost_scout.cpp @ 3a73516c

History | View | Annotate | Download (4.84 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
/**
49
 * @file ghost_scout.cpp
50
 *
51
 * @ingroup scoutsim
52
 * @{
53
 */
54

    
55
#include "ghost_scout.h"
56

    
57
#include <wx/wx.h>
58

    
59
using namespace std;
60

    
61
namespace scoutsim
62
{
63
  GhostScout::GhostScout(const ros::NodeHandle& nh,
64
      const wxImage& scout_image,
65
      const Vector2& pos,
66
      wxBitmap *path_bitmap,
67
      float orient,
68
      string scoutname)
69
    : path_bitmap(path_bitmap)
70
      , node (nh)
71
      , scout_image(scout_image)
72
      , pos(pos)
73
      , start_x(pos.x)
74
      , start_y(pos.y)
75
      , orient(orient)
76
      , name(scoutname)
77
      , is_visible(false)
78
  {
79
    scout = wxBitmap(scout_image);
80

    
81
    position = node.subscribe("position", 1, &GhostScout::setPosition, this);
82

    
83
  }
84

    
85
  void GhostScout::setPosition(const ::messages::ScoutPosition& msg)
86
  {
87
    pos.x = start_x + msg.x;
88
    pos.y = start_y - msg.y;
89
    orient = msg.theta;
90
  }
91

    
92
  // Scale to linesensor value
93
  unsigned int GhostScout::rgb_to_grey(unsigned char r,
94
      unsigned char g,
95
      unsigned char b)
96
  {
97
    // Should be 0 to 255
98
    unsigned int grey = ((unsigned int) r + (unsigned int) g + (unsigned int) b) / 3;
99

    
100
    /// @todo Convert to the proper range
101
    return 255 - grey;
102
  }
103

    
104
  /// Sends back the position of this scout so scoutsim can save
105
  /// the world state
106
  geometry_msgs::Pose2D GhostScout::update(double dt,
107
      wxMemoryDC& path_dc,
108
      wxMemoryDC& sonar_dc,
109
      wxColour background_color,
110
      world_state state)
111
  {
112

    
113
    wxImage rotated_image = scout_image.Rotate(orient - M_PI/2.0,
114
        wxPoint(scout_image.GetWidth() / 2,
115
          scout_image.GetHeight() / 2),
116
        false);
117

    
118
    for (int y = 0; y < rotated_image.GetHeight(); ++y)
119
    {
120
      for (int x = 0; x < rotated_image.GetWidth(); ++x)
121
      {
122
        if (rotated_image.GetRed(x, y) == 255
123
            && rotated_image.GetBlue(x, y) == 255
124
            && rotated_image.GetGreen(x, y) == 255)
125
        {
126
          rotated_image.SetAlpha(x, y, 0);
127
        }
128
        else
129
        {
130
          rotated_image.SetAlpha(x, y, 125);
131
        }
132
      }
133
    }
134

    
135
    scout = wxBitmap(rotated_image);
136

    
137
    geometry_msgs::Pose2D my_pose;
138
    my_pose.x = pos.x;
139
    my_pose.y = pos.y;
140
    my_pose.theta = orient;
141

    
142
    return my_pose;
143
  }
144

    
145
  void GhostScout::paint(wxDC& dc)
146
  {
147
      if (!is_visible)
148
      {
149
          return;
150
      }
151

    
152
    wxSize scout_size = wxSize(scout.GetWidth(), scout.GetHeight());
153
    dc.DrawBitmap(scout, pos.x * PIX_PER_METER - (scout_size.GetWidth() / 2),
154
        pos.y * PIX_PER_METER - (scout_size.GetHeight() / 2), true);
155
  }
156

    
157
  string GhostScout::get_name()
158
  {
159
    return name;
160
  }
161

    
162
  void GhostScout::set_visible(bool vis)
163
  {
164
    is_visible = vis;
165
  }
166
}
167

    
168
/** @} */