scoutos / scout / motors / src / motors.cpp @ c9f87aaf
History | View | Annotate | Download (6.8 KB)
1 |
/**
|
---|---|
2 |
* Copyright (c) 2011 Colony Project
|
3 |
*
|
4 |
* Permission is hereby granted, free of charge, to any person
|
5 |
* obtaining a copy of this software and associated documentation
|
6 |
* files (the "Software"), to deal in the Software without
|
7 |
* restriction, including without limitation the rights to use,
|
8 |
* copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9 |
* copies of the Software, and to permit persons to whom the
|
10 |
* Software is furnished to do so, subject to the following
|
11 |
* conditions:
|
12 |
*
|
13 |
* The above copyright notice and this permission notice shall be
|
14 |
* included in all copies or substantial portions of the Software.
|
15 |
*
|
16 |
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17 |
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
|
18 |
* OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19 |
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
|
20 |
* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
|
21 |
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
22 |
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
|
23 |
* OTHER DEALINGS IN THE SOFTWARE.
|
24 |
**/
|
25 |
|
26 |
/**
|
27 |
* @file motors.cpp
|
28 |
* @brief Contains code to control the motors.
|
29 |
*
|
30 |
* Implementation of functions for motor use.
|
31 |
*
|
32 |
* @author Colony Project, CMU Robotics Club
|
33 |
* @author Benjamin Wasserman
|
34 |
**/
|
35 |
|
36 |
#include "ros/ros.h" |
37 |
#include "motors.h" |
38 |
//#include "libscout/src/constants.h"
|
39 |
#include <cstdlib> |
40 |
|
41 |
/**
|
42 |
* @defgroup motors Motors
|
43 |
* @brief Functions for using the motors
|
44 |
*
|
45 |
* @{
|
46 |
**/
|
47 |
|
48 |
/* Motor state variables
|
49 |
* Speeds expressed as absolute speed out of max speed (0 - +-MAXSPEED)
|
50 |
* Absolute speed is the speed written to the hardware to move the motors
|
51 |
*/
|
52 |
/** @todo Fix types: static */
|
53 |
int motor_fl_speed; /**< The current speed of the front left motor. */ |
54 |
int motor_fr_speed; /**< The current speed of the front right motor. */ |
55 |
int motor_bl_speed; /**< The current speed of the back left motor. */ |
56 |
int motor_br_speed; /**< The current speed of the back right motor. */ |
57 |
|
58 |
/**
|
59 |
* @brief Sets motor speed
|
60 |
*
|
61 |
* Sets the motor speeds based on subscription to the set_motors topic.
|
62 |
*
|
63 |
* @param msg The message from the set_motors topic, containing speeds and
|
64 |
* motor configuration settings.
|
65 |
*/
|
66 |
void motors_set(const motors::set_motors::ConstPtr& msg) |
67 |
{ |
68 |
/** @todo Edit to only set requested motors, not all */
|
69 |
int which = msg->which;
|
70 |
if(which & MOTOR_FL_REV)
|
71 |
{ |
72 |
motor_fl_speed = -1 * motors_rel_to_abs(msg->fl_speed, msg->units);
|
73 |
} |
74 |
if(which & MOTOR_FR_REV)
|
75 |
{ |
76 |
motor_fr_speed = -1 * motors_rel_to_abs(msg->fr_speed, msg->units);
|
77 |
} |
78 |
if(which & MOTOR_BL_REV)
|
79 |
{ |
80 |
motor_bl_speed = -1 * motors_rel_to_abs(msg->bl_speed, msg->units);
|
81 |
} |
82 |
if(which & MOTOR_BR_REV)
|
83 |
{ |
84 |
motor_br_speed = -1 * motors_rel_to_abs(msg->br_speed, msg->units);
|
85 |
} |
86 |
if(which & MOTOR_FL)
|
87 |
{ |
88 |
motor_fl_speed = motors_rel_to_abs(msg->fl_speed, msg->units); |
89 |
} |
90 |
if(which & MOTOR_FR)
|
91 |
{ |
92 |
motor_fr_speed = motors_rel_to_abs(msg->fr_speed, msg->units); |
93 |
} |
94 |
if(which & MOTOR_BL)
|
95 |
{ |
96 |
motor_bl_speed = motors_rel_to_abs(msg->bl_speed, msg->units); |
97 |
} |
98 |
if(which & MOTOR_BR)
|
99 |
{ |
100 |
motor_br_speed = motors_rel_to_abs(msg->br_speed, msg->units); |
101 |
} |
102 |
|
103 |
/* Write speeds to hardware */
|
104 |
/** @todo Add code to write speeds to hardware */
|
105 |
} |
106 |
|
107 |
/**
|
108 |
* @brief Outputs current motor speeds
|
109 |
*
|
110 |
* Serves the service query_motors by responding to service requests with the
|
111 |
* speeds of the motors.
|
112 |
* @param req The request. The only field is the units requested.
|
113 |
* @param res The response. The fields will be filled with values.
|
114 |
*/
|
115 |
bool motors_query(motors::query_motors::Request &req,
|
116 |
motors::query_motors::Response &res) |
117 |
{ |
118 |
int units = req.units;
|
119 |
res.fl_speed = motors_abs_to_rel(motor_fl_speed, units); |
120 |
res.fr_speed = motors_abs_to_rel(motor_fr_speed, units); |
121 |
res.bl_speed = motors_abs_to_rel(motor_bl_speed, units); |
122 |
res.br_speed = motors_abs_to_rel(motor_br_speed, units); |
123 |
|
124 |
ROS_DEBUG("Motor speeds queried");
|
125 |
return true; |
126 |
} |
127 |
|
128 |
/**
|
129 |
* @brief Converts set speeds (of various units) to absolute speeds.
|
130 |
*
|
131 |
* @param speed The speed expressed in the desired units
|
132 |
* @param units The units the desired speed is measured in
|
133 |
* @return The absolute speed of the motor
|
134 |
**/
|
135 |
int motors_rel_to_abs(int rel_speed, int units) |
136 |
{ |
137 |
switch(units)
|
138 |
{ |
139 |
case MOTOR_ABSOLUTE:/* Speed given as absolute */ |
140 |
return rel_speed;
|
141 |
case MOTOR_PERCENT:/* Convert from percentage */ |
142 |
return rel_speed * MAXSPEED / 100; |
143 |
case MOTOR_MMS:/* Convert from mm/s */ |
144 |
/** @todo Make math to do this conversion **/
|
145 |
return rel_speed;
|
146 |
case MOTOR_CMS:/* Convert from cm/s */ |
147 |
/** @todo Make math to do this conversion **/
|
148 |
return rel_speed;
|
149 |
default: /* The units aren't recognized */ |
150 |
/** @todo Decide on default case. Either percent or absolute. **/
|
151 |
return rel_speed;
|
152 |
} |
153 |
} |
154 |
|
155 |
/**
|
156 |
* @brief Convert absolute speeds to speeds of various units.
|
157 |
*
|
158 |
* @param speed The speed expressed in absolute units.
|
159 |
* @param units The units the desired speed is measured in.
|
160 |
* @return The relative speed of the motor in desired units.
|
161 |
**/
|
162 |
int motors_abs_to_rel(int abs_speed, int units) |
163 |
{ |
164 |
switch(units)
|
165 |
{ |
166 |
case MOTOR_ABSOLUTE:/* Speed given as absolute */ |
167 |
return abs_speed;
|
168 |
case MOTOR_PERCENT:/* Convert from percentage */ |
169 |
return abs_speed * 100 / MAXSPEED; |
170 |
case MOTOR_MMS:/* Convert from mm/s */ |
171 |
/** @todo Make math to do this conversion **/
|
172 |
return abs_speed;
|
173 |
case MOTOR_CMS:/* Convert from cm/s */ |
174 |
/** @todo Make math to do this conversion **/
|
175 |
return abs_speed;
|
176 |
default: /* The units aren't recognized */ |
177 |
/** @todo Decide on default case. Either percent or absolute. **/
|
178 |
return abs_speed;
|
179 |
} |
180 |
} |
181 |
|
182 |
/**
|
183 |
* @brief Motors driver. This is a ROS node that controls motor speeds.
|
184 |
*
|
185 |
* This is the main function for the motors node. It is run when the node
|
186 |
* starts and initializes the motors. It then subscribes to the
|
187 |
* set_motors, and set_motor_speeds topics, and advertises the
|
188 |
* query_motors service.
|
189 |
*
|
190 |
* @param argc The number of command line arguments (should be 1)
|
191 |
* @param argv The array of command line arguments
|
192 |
**/
|
193 |
int main(int argc, char **argv) |
194 |
{ |
195 |
/* Initialize in ROS the motors driver node */
|
196 |
ros::init(argc, argv, "motors_driver");
|
197 |
|
198 |
/* Advertise that this serves the query_motors service */
|
199 |
ros::NodeHandle n; |
200 |
ros::ServiceServer service = n.advertiseService("query_motors",
|
201 |
motors_query); |
202 |
|
203 |
/* Subscribe to the set_motors topic */
|
204 |
ros::Subscriber sub0 = n.subscribe("set_motors", 4, motors_set); |
205 |
|
206 |
/* Initialize hardware for motors */
|
207 |
// Hardware init functions here
|
208 |
|
209 |
ROS_INFO("Ready to set motors.");
|
210 |
ros::spin(); |
211 |
|
212 |
return 0; |
213 |
} |
214 |
|
215 |
/** @} **/
|