Project

General

Profile

Statistics
| Branch: | Revision:

robobuggy / buggysim / src / buggy.cpp @ 1efba872

History | View | Annotate | Download (5.03 KB)

1
/*
2
 * Copyright (c) 2009, Willow Garage, Inc.
3
 * All rights reserved.
4
 *
5
 * Redistribution and use in source and binary forms, with or without
6
 * modification, are permitted provided that the following conditions are met:
7
 *
8
 *     * Redistributions of source code must retain the above copyright
9
 *       notice, this list of conditions and the following disclaimer.
10
 *     * Redistributions in binary form must reproduce the above copyright
11
 *       notice, this list of conditions and the following disclaimer in the
12
 *       documentation and/or other materials provided with the distribution.
13
 *     * Neither the name of the Willow Garage, Inc. nor the names of its
14
 *       contributors may be used to endorse or promote products derived from
15
 *       this software without specific prior written permission.
16
 *
17
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
18
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20
 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
21
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
25
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27
 * POSSIBILITY OF SUCH DAMAGE.
28
 */
29

    
30
#include "buggysim/buggy.h"
31

    
32
#include <QColor>
33
#include <QRgb>
34

    
35
#define DEFAULT_PEN_R 0xb3
36
#define DEFAULT_PEN_G 0xb8
37
#define DEFAULT_PEN_B 0xff
38

    
39
namespace buggysim
40
{
41

    
42
   AbstractBuggy::AbstractBuggy(const ros::NodeHandle& nh,
43
                                const QImage& turtle_image,
44
                                const QPointF& pos,
45
                                float orient)
46
      : nh_(nh)
47
      , turtle_image_(turtle_image)
48
      , pos_(pos)
49
      , orient_(orient)
50
      //, lin_vel_(2.0)
51
      //, ang_vel_(0.0)
52
      //, turnAngle_(0.0)
53
      , pen_on_(true)
54
      , pen_(QColor(DEFAULT_PEN_R, DEFAULT_PEN_G, DEFAULT_PEN_B))
55
   {
56
      pen_.setWidth(3);
57
      ROS_INFO("Namespace in abstract buggy: %s", nh.getNamespace().data());
58
      ROS_INFO("Namespace in abstract buggy, stored: %s", nh_.getNamespace().data());
59
      //angle_sub_ = nh_.subscribe
60
      //             ("command_turnAngle", 1, &AbstractBuggy::turnAngleCallback, this);
61
      //pose_pub_ = nh_.advertise<buggymsgs::Pose>("pose", 1);
62

    
63
      //color_pub_ = nh_.advertise<Color>("color_sensor", 1);
64
      set_pen_srv_ = nh_.advertiseService
65
                     ("set_pen", &AbstractBuggy::setPenCallback, this);
66
      teleport_relative_srv_ = nh_.advertiseService
67
                               ("teleport_relative", &AbstractBuggy::teleportRelativeCallback, this);
68
      teleport_absolute_srv_ = nh_.advertiseService
69
                               ("teleport_absolute", &AbstractBuggy::teleportAbsoluteCallback, this);
70

    
71

    
72
      meter_ = turtle_image_.height();
73
      rotateImage();
74
   }
75

    
76
   // This is a virtual method, to be implemented by the subclass
77
   /*void AbstractBuggy::turnAngleCallback(const buggymsgs::DirectionConstPtr& dir)
78
   {
79
      last_command_time_ = ros::WallTime::now();
80
      turnAngle_ = dir->turnAngle;
81
      //lin_vel_ = vel->linear;
82
      //ang_vel_ = vel->angular;
83
   }*/
84

    
85
   bool AbstractBuggy::setPenCallback(buggysim::SetPen::Request& req,
86
                               buggysim::SetPen::Response&)
87
   {
88
      pen_on_ = !req.off;
89
      if (req.off) {
90
         return true;
91
      }
92

    
93
      QPen pen(QColor(req.r, req.g, req.b));
94
      if (req.width != 0) {
95
         pen.setWidth(req.width);
96
      }
97

    
98
      pen_ = pen;
99
      return true;
100
   }
101

    
102
   bool AbstractBuggy::teleportRelativeCallback(buggysim::TeleportRelative::Request& req,
103
                                         buggysim::TeleportRelative::Response&)
104
   {
105
      teleport_requests_.push_back(TeleportRequest(0, 0, req.angular, req.linear,
106
                                   true));
107
      return true;
108
   }
109

    
110
   bool AbstractBuggy::teleportAbsoluteCallback(buggysim::TeleportAbsolute::Request& req,
111
                                         buggysim::TeleportAbsolute::Response&)
112
   {
113
      teleport_requests_.push_back(TeleportRequest(req.x, req.y, req.theta, 0,
114
                                   false));
115
      return true;
116
   }
117

    
118
   void AbstractBuggy::rotateImage()
119
   {
120
      QTransform transform;
121
      transform.rotate(-orient_ * 180.0 / PI + 90.0);
122
      turtle_rotated_image_ = turtle_image_.transformed(transform);
123
   }
124

    
125
   bool AbstractBuggy::update(double dt, QPainter& path_painter, const QImage& path_image,
126
                       qreal canvas_width, qreal canvas_height)
127
   {
128

    
129
       return false;
130

    
131
   }
132

    
133
   void AbstractBuggy::paint(QPainter& painter)
134
   {
135
      QPointF p = pos_ * meter_;
136
      p.rx() -= 0.5 * turtle_rotated_image_.width();
137
      p.ry() -= 0.5 * turtle_rotated_image_.height();
138
      painter.drawImage(p, turtle_rotated_image_);
139
   }
140

    
141
}