Revision 2814387f
ID | 2814387f48599dab8a99852093cc51629a14fb89 |
Updated motors code.
Changed the definitions for the set_motors message, which forced a few
implementation changes as well.
Changed "libscout_node" to "node" and "n" to "node" in other packages.
scout/motors/src/motors.cpp | ||
---|---|---|
34 | 34 |
**/ |
35 | 35 |
|
36 | 36 |
#include "ros/ros.h" |
37 |
#include "motors_util.cpp" |
|
37 | 38 |
#include "motors.h" |
38 | 39 |
//#include "libscout/src/constants.h" |
39 | 40 |
#include <cstdlib> |
... | ... | |
45 | 46 |
* @{ |
46 | 47 |
**/ |
47 | 48 |
|
49 |
|
|
50 |
|
|
48 | 51 |
/* Motor state variables |
49 | 52 |
* Speeds expressed as absolute speed out of max speed (0 - +-MAXSPEED) |
50 | 53 |
* Absolute speed is the speed written to the hardware to move the motors |
51 | 54 |
*/ |
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. */ |
|
55 |
static int motor_fl_speed; /**< The current speed of the front left motor. */ |
|
56 |
static int motor_fr_speed; /**< The current speed of the front right motor. */ |
|
57 |
static int motor_bl_speed; /**< The current speed of the back left motor. */ |
|
58 |
static int motor_br_speed; /**< The current speed of the back right motor. */ |
|
57 | 59 |
|
58 | 60 |
/** |
59 | 61 |
* @brief Sets motor speed |
... | ... | |
65 | 67 |
*/ |
66 | 68 |
void motors_set(const motors::set_motors::ConstPtr& msg) |
67 | 69 |
{ |
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) |
|
70 |
if(msg->fl_set) |
|
87 | 71 |
{ |
88 | 72 |
motor_fl_speed = motors_rel_to_abs(msg->fl_speed, msg->units); |
89 | 73 |
} |
90 |
if(which & MOTOR_FR)
|
|
74 |
if(msg->fr_set)
|
|
91 | 75 |
{ |
92 | 76 |
motor_fr_speed = motors_rel_to_abs(msg->fr_speed, msg->units); |
93 | 77 |
} |
94 |
if(which & MOTOR_BL)
|
|
78 |
if(msg->bl_set)
|
|
95 | 79 |
{ |
96 | 80 |
motor_bl_speed = motors_rel_to_abs(msg->bl_speed, msg->units); |
97 | 81 |
} |
98 |
if(which & MOTOR_BR)
|
|
82 |
if(msg->br_set)
|
|
99 | 83 |
{ |
100 | 84 |
motor_br_speed = motors_rel_to_abs(msg->br_speed, msg->units); |
101 | 85 |
} |
102 | 86 |
|
103 |
/* Write speeds to hardware */ |
|
104 | 87 |
/** @todo Add code to write speeds to hardware */ |
88 |
ROS_DEBUG("Motor speeds set"); |
|
105 | 89 |
} |
106 | 90 |
|
107 | 91 |
/** |
... | ... | |
126 | 110 |
} |
127 | 111 |
|
128 | 112 |
/** |
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 | 113 |
* @brief Motors driver. This is a ROS node that controls motor speeds. |
184 | 114 |
* |
185 | 115 |
* This is the main function for the motors node. It is run when the node |
... | ... | |
196 | 126 |
ros::init(argc, argv, "motors_driver"); |
197 | 127 |
|
198 | 128 |
/* Advertise that this serves the query_motors service */ |
199 |
ros::NodeHandle n; |
|
200 |
ros::ServiceServer service = n.advertiseService("query_motors", |
|
201 |
motors_query); |
|
129 |
ros::NodeHandle node;
|
|
130 |
ros::ServiceServer service = node.advertiseService("query_motors",
|
|
131 |
motors_query);
|
|
202 | 132 |
|
203 | 133 |
/* Subscribe to the set_motors topic */ |
204 |
ros::Subscriber sub0 = n.subscribe("set_motors", QUEUE_SIZE, motors_set); |
|
134 |
ros::Subscriber sub0 = node.subscribe("set_motors", QUEUE_SIZE, motors_set);
|
|
205 | 135 |
|
206 | 136 |
/* Initialize hardware for motors */ |
207 | 137 |
// Hardware init functions here |
Also available in: Unified diff