Project

General

Profile

Statistics
| Branch: | Revision:

scoutos / scout / scoutsim / src / scout.h @ 6257c97d

History | View | Annotate | Download (7.02 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
#ifndef _SCOUTSIM_SCOUT_H_
49
#define _SCOUTSIM_SCOUT_H_
50

    
51
#include <ros/ros.h>
52
#include <vector>
53
#include <boost/shared_ptr.hpp>
54

    
55
#include <motors/set_motors.h>
56
#include <encoders/query_encoders.h>
57
#include <linesensor/query_linesensor.h>
58
#include <sonar/sonar_distance.h>
59

    
60
#include <scoutsim/Pose.h>
61
#include <scoutsim/SetPen.h>
62
#include <scoutsim/Color.h>
63

    
64
#include <geometry_msgs/Pose2D.h>
65

    
66
#include <wx/wx.h>
67

    
68
#include "scoutsim_internal.h"
69
#include "scout_constants.h"
70

    
71
#define PI 3.14159265
72

    
73
#define NUM_LINESENSORS 8
74

    
75
// Distance, pixels, from center of robot to the linesensors.
76
#define LNSNSR_D 20
77

    
78
namespace scoutsim
79
{
80
    struct Vector2
81
    {
82
        Vector2()
83
            : x(0.0)
84
              , y(0.0)
85
        {}
86

    
87
        Vector2(float new_x, float new_y)
88
            : x(new_x)
89
              , y(new_y)
90
        {}
91

    
92
        bool operator==(const Vector2& rhs)
93
        {
94
            return x == rhs.x && y == rhs.y;
95
        }
96

    
97
        bool operator!=(const Vector2& rhs)
98
        {
99
            return x != rhs.x || y != rhs.y;
100
        }
101

    
102
        float x;
103
        float y;
104
    };
105

    
106
    class Scout
107
    {
108
        public:
109
            Scout(const ros::NodeHandle& nh,const wxImage& scout_image,
110
                  const Vector2& pos, wxBitmap *path_bitmap, float orient);
111

    
112
            geometry_msgs::Pose2D update(double dt, wxMemoryDC& path_dc,
113
                                         wxMemoryDC& sonar_dc,
114
                                         bool sonar_on,
115
                                         const wxImage& path_image,
116
                                         const wxImage& lines_image,
117
                                         const wxImage& walls_image,
118
                                         wxColour background_color,
119
                                          wxColour sonar_color,
120
                                         world_state state);
121
            void paint(wxDC& dc);
122

    
123
        private:
124
            float absolute_to_mps(int absolute_speed);
125
            void setMotors(const motors::set_motors::ConstPtr& msg);
126
            bool setPenCallback(scoutsim::SetPen::Request&,
127
                                scoutsim::SetPen::Response&);
128
            bool query_encoders_callback(encoders::query_encoders::Request&,
129
                                         encoders::query_encoders::Response&);
130
            bool query_linesensor_callback(linesensor::query_linesensor::Request&,
131
                                           linesensor::query_linesensor::Response&);
132
            unsigned int rgb_to_grey(unsigned char r,
133
                                     unsigned char g,
134
                                     unsigned char b);
135
            unsigned int trace_sonar(const wxImage& walls_image, int x, int y,
136
                                     double robot_theta, int sonar_pos,
137
                                     wxMemoryDC& sonar_dc, bool sonar_on);
138
            void update_sonar(const wxImage& walls_image, int x, int y,
139
                              double robot_theta, wxMemoryDC& sonar_dc,bool sonar_on);
140
            void update_linesensor(const wxImage& lines_image, int x, int y,
141
                                   double theta);
142
            int old_front_dx;
143
            int old_front_dy;
144
            int old_back_dx;
145
            int old_back_dy;
146
            bool isFront;
147

    
148
            wxBitmap *path_bitmap;
149
            bool sonar_on;
150

    
151
            ros::NodeHandle node;
152

    
153
            wxImage scout_image;
154
            wxBitmap scout;
155

    
156
            Vector2 pos;
157
            float orient;
158

    
159
            /// @todo should these be an array or something?
160

    
161
            // Keep track of the last commanded speeds sent to the sim,
162
            // converted to m/s
163
            float motor_fl_speed;
164
            float motor_fr_speed;
165
            float motor_bl_speed;
166
            float motor_br_speed;
167

    
168
            // Keep track of encoder ticks for each motor
169
            unsigned int fl_ticks;
170
            unsigned int fr_ticks;
171
            unsigned int bl_ticks;
172
            unsigned int br_ticks;
173

    
174
            int sonar_position;
175
            int sonar_stop_l;
176
            int sonar_stop_r;
177
            int sonar_direction;
178

    
179
            // The last time the sonar changed its position.
180
            ros::Time last_sonar_time;
181
            ros::Duration sonar_tick_time;
182

    
183
            // A vector of the 8 linesensor readings
184
            std::vector<unsigned int> linesensor_readings;
185

    
186
            // Each scout has a unique id number, which is also displayed on its image.
187
            int scout_id;
188

    
189
            bool pen_on;
190
            wxPen pen;
191

    
192
            ros::Subscriber motors_sub;
193
            ros::Publisher pose_pub;
194
            ros::Publisher color_pub;
195
            ros::Publisher sonar_pub;
196
            ros::ServiceServer set_pen_srv;
197
            ros::ServiceServer query_encoders_srv;
198
            ros::ServiceServer query_linesensor_srv;
199

    
200
            ros::WallTime last_command_time;
201
    };
202
    typedef boost::shared_ptr<Scout> ScoutPtr;
203
}
204

    
205
#endif