Revision a8480867
ID | a84808670408804bf5ce08741a3b59130351928f |
Made a lot of changes to the general structure.
Applied object-orienting techniques to the code, cleaning it up considerably. Major design changes are as follows:
- All separate node code (ie motors) are meant to only interface with hardware - doing as little logic as possible.
- Instead, the logic is moved to a class (ie MotorControl) in libscout. PID and other tools should be located here. This significantly reduces dependencies.
- Files can be included cross-package by being placed in /include/packagename, but this should be avoided to reduce dependencies.
A few formatting changes:
- Comments should end with / always, not */
- Use @ instead of \
- No double newlines.
The motors node is revamped and pretty much correct at this point.
scout/libscout/CMakeLists.txt | ||
---|---|---|
29 | 29 |
#rosbuild_add_executable(example examples/example.cpp) |
30 | 30 |
#target_link_libraries(example ${PROJECT_NAME}) |
31 | 31 |
|
32 |
rosbuild_add_executable(libscout_node src/behavior.cpp src/libscout.cpp src/libmotors.cpp src/libheadlights.cpp src/libbuttons.cpp) |
|
32 |
#rosbuild_add_executable(libscout_node src/behavior.cpp src/libscout.cpp src/libmotors.cpp src/libheadlights.cpp src/libbuttons.cpp) |
|
33 |
rosbuild_add_executable(alex_behavior src/alex_behavior.cpp src/libscout.h src/MotorControl.cpp src/HeadlightControl.cpp) |
scout/libscout/include/libscout/constants.h | ||
---|---|---|
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 |
/** |
|
28 |
* @file constants.h |
|
29 |
* @brief Contains top level scout library constants |
|
30 |
* |
|
31 |
* Contains constants for use in libscout |
|
32 |
* |
|
33 |
* @author Colony Project, CMU Robotics Club |
|
34 |
* @author Ben Wasserman |
|
35 |
**/ |
|
36 |
|
|
37 |
#ifndef _CONSTANTS_H_ |
|
38 |
#define _CONSTANTS_H_ |
|
39 |
|
|
40 |
#include "ros/ros.h" |
|
41 |
|
|
42 |
/* ROS defines */ |
|
43 |
#define QUEUE_SIZE 10 |
|
44 |
|
|
45 |
/* Libscout Defines */ |
|
46 |
|
|
47 |
/* Init defines */ |
|
48 |
#define LIB_ALL -1 |
|
49 |
#define LIB_MOTORS 0x1 |
|
50 |
#define LIB_SONAR 0x2 |
|
51 |
#define LIB_CLIFFSENSORS 0x4 |
|
52 |
#define LIB_HEADLIGHTS 0x8 |
|
53 |
#define LIB_BUTTONS 0x10 |
|
54 |
|
|
55 |
/* Status defines */ |
|
56 |
//TODO MAKE ENUMS |
|
57 |
#define LIB_ERROR -1 |
|
58 |
#define LIB_OK 0 |
|
59 |
|
|
60 |
|
|
61 |
#endif |
scout/libscout/src/HeadlightControl.cpp | ||
---|---|---|
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 HeadlightControl.cpp |
|
28 |
* @brief Contains headlights declarations and functions |
|
29 |
* |
|
30 |
* Contains functions and definitions for the use of |
|
31 |
* headlights |
|
32 |
* |
|
33 |
* @author Colony Project, CMU Robotics Club |
|
34 |
* @author Ben Wasserman |
|
35 |
* @author Alex Zirbel |
|
36 |
*/ |
|
37 |
|
|
38 |
#include "HeadlightControl.h" |
|
39 |
|
|
40 |
/** ROS publisher and client declaration */ |
|
41 |
|
|
42 |
/** |
|
43 |
* @brief Initialize the headlights module of libscout. |
|
44 |
* |
|
45 |
* Initialize the libscout node as a publisher of set_headlights. |
|
46 |
*/ |
|
47 |
HeadlightControl::HeadlightControl(const ros::NodeHandle& libscout_node) |
|
48 |
: node(libscout_node) |
|
49 |
{ |
|
50 |
set_headlights_pub = node.advertise<headlights::set_headlights> |
|
51 |
("set_headlights", QUEUE_SIZE); |
|
52 |
} |
|
53 |
|
|
54 |
/** |
|
55 |
* @brief Set headlight colors as a single RGB value |
|
56 |
* |
|
57 |
* Sets the colors of the headlights as a web color value. Can selectively |
|
58 |
* select which motors to set, and which to remain at previous color. |
|
59 |
* @param color The color the headlights should be set to expressed as 0xRRGGBB |
|
60 |
* @param which A bitmask of which headlights should be set |
|
61 |
*/ |
|
62 |
void HeadlightControl::set(int color, int which) |
|
63 |
{ |
|
64 |
int red, green, blue; |
|
65 |
|
|
66 |
red = (color & RED) >> REDSHIFT; |
|
67 |
green = (color & GREEN) >> GREENSHIFT; |
|
68 |
blue = (color & BLUE) >> BLUESHIFT; |
|
69 |
|
|
70 |
set_rgb(red, green, blue, which); |
|
71 |
} |
|
72 |
|
|
73 |
/** |
|
74 |
* @brief Set headlight colors as separate RGB values |
|
75 |
* |
|
76 |
* Sets the colors of the headlights as a web color value. Can selectively |
|
77 |
* select which motors to set, and which to remain at previous color. |
|
78 |
* @param red The red value the headlights should be set to |
|
79 |
* @param green The green value the headlights should be set to |
|
80 |
* @param blue The blue value the headlights should be set to |
|
81 |
* @param which A bitmask of which headlights should be set |
|
82 |
*/ |
|
83 |
void HeadlightControl::set_rgb(int red, int green, int blue, int which) |
|
84 |
{ |
|
85 |
headlights::set_headlights msg; |
|
86 |
|
|
87 |
if(which & HL_LEFT) |
|
88 |
{ |
|
89 |
msg.left_red = red; |
|
90 |
msg.left_green = green; |
|
91 |
msg.left_blue = blue; |
|
92 |
} |
|
93 |
else |
|
94 |
{ |
|
95 |
msg.left_red = NO_SET; |
|
96 |
msg.left_green = NO_SET; |
|
97 |
msg.left_blue = NO_SET; |
|
98 |
} |
|
99 |
if(which & HL_RIGHT) |
|
100 |
{ |
|
101 |
msg.right_red = red; |
|
102 |
msg.right_green = green; |
|
103 |
msg.right_blue = blue; |
|
104 |
} |
|
105 |
else |
|
106 |
{ |
|
107 |
msg.right_red = NO_SET; |
|
108 |
msg.right_green = NO_SET; |
|
109 |
msg.right_blue = NO_SET; |
|
110 |
} |
|
111 |
|
|
112 |
/* Publishes message to set_motors topic */ |
|
113 |
set_headlights_pub.publish(msg); |
|
114 |
ros::spinOnce(); |
|
115 |
} |
scout/libscout/src/HeadlightControl.h | ||
---|---|---|
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 HeadlightControl.h |
|
28 |
* @brief Contains headlights declarations and functions |
|
29 |
* |
|
30 |
* Contains functions and definitions for the use of |
|
31 |
* headlights |
|
32 |
* |
|
33 |
* @author Colony Project, CMU Robotics Club |
|
34 |
* @author Ben Wasserman |
|
35 |
* @author Alex Zirbel |
|
36 |
*/ |
|
37 |
|
|
38 |
#ifndef _HEADLIGHT_CONTROL_H_ |
|
39 |
#define _HEADLIGHT_CONTROL_H_ |
|
40 |
|
|
41 |
#include <ros/ros.h> |
|
42 |
#include <libscout/constants.h> |
|
43 |
#include <headlights/set_headlights.h> |
|
44 |
|
|
45 |
/* Defines */ |
|
46 |
#define WHITE 0xFFFFFF |
|
47 |
#define OFF 0x000000 |
|
48 |
#define RED 0xFF0000 |
|
49 |
#define GREEN 0x00FF00 |
|
50 |
#define BLUE 0x0000FF |
|
51 |
#define YELLOW 0xFFFF00 |
|
52 |
#define CYAN 0x00FFFF |
|
53 |
#define PINK 0xFF00FF |
|
54 |
|
|
55 |
#define HL_RIGHT 0x1 |
|
56 |
#define HL_LEFT 0x2 |
|
57 |
#define HL_BOTH 0x3 |
|
58 |
|
|
59 |
#define NO_SET -1 |
|
60 |
|
|
61 |
#define REDSHIFT 16 |
|
62 |
#define GREENSHIFT 8 |
|
63 |
#define BLUESHIFT 0 |
|
64 |
|
|
65 |
class HeadlightControl |
|
66 |
{ |
|
67 |
public: |
|
68 |
HeadlightControl(const ros::NodeHandle& libscout_node); |
|
69 |
|
|
70 |
void set(int color, int which); |
|
71 |
void set_rgb(int red, int green, int blue, int which); |
|
72 |
|
|
73 |
private: |
|
74 |
ros::NodeHandle node; |
|
75 |
|
|
76 |
ros::Publisher set_headlights_pub; |
|
77 |
}; |
|
78 |
|
|
79 |
#endif |
|
80 |
|
scout/libscout/src/MotorControl.cpp | ||
---|---|---|
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 MotorControl.cpp |
|
28 |
* @brief Contains motor declarations and functions |
|
29 |
* |
|
30 |
* Contains functions and definitions for the use of |
|
31 |
* motors |
|
32 |
* |
|
33 |
* @author Colony Project, CMU Robotics Club |
|
34 |
* @author Ben Wasserman |
|
35 |
* @author Alex Zirbel |
|
36 |
**/ |
|
37 |
|
|
38 |
#include "MotorControl.h" |
|
39 |
|
|
40 |
/** |
|
41 |
* @brief Initialize the motors module of libscout. |
|
42 |
* |
|
43 |
* Initialize the libscout node as a publisher of set_motors and a client of |
|
44 |
* query_motors. |
|
45 |
*/ |
|
46 |
MotorControl::MotorControl(const ros::NodeHandle& libscout_node) |
|
47 |
: node(libscout_node) |
|
48 |
{ |
|
49 |
set_motors_pub = |
|
50 |
node.advertise<motors::set_motors>("set_motors", QUEUE_SIZE); |
|
51 |
query_motors_client = |
|
52 |
node.serviceClient<motors::query_motors>("query_motors"); |
|
53 |
} |
|
54 |
|
|
55 |
/** |
|
56 |
* @brief Sets all the speeds according to the specificiation of which motors |
|
57 |
* to set. |
|
58 |
* |
|
59 |
* @param which A bitmask of which motors need to be set. |
|
60 |
* @param speed The value to set the motors to. |
|
61 |
* @param units The units the speed is expressed in. |
|
62 |
* @return Function status |
|
63 |
*/ |
|
64 |
void MotorControl::set(int which, float speed, char units) |
|
65 |
{ |
|
66 |
float speed_fl, speed_fr, speed_bl, speed_br; |
|
67 |
speed_fl = speed_fr = speed_bl = speed_br = speed; |
|
68 |
|
|
69 |
if(which & MOTOR_FL_REV) |
|
70 |
{ |
|
71 |
speed_fl = -speed; |
|
72 |
} |
|
73 |
if(which & MOTOR_FR_REV) |
|
74 |
{ |
|
75 |
speed_fr = -speed; |
|
76 |
} |
|
77 |
if(which & MOTOR_BL_REV) |
|
78 |
{ |
|
79 |
speed_bl = -speed; |
|
80 |
} |
|
81 |
if(which & MOTOR_BR_REV) |
|
82 |
{ |
|
83 |
speed_br = -speed; |
|
84 |
} |
|
85 |
|
|
86 |
set_each(which, speed_fl, speed_fr, speed_bl, speed_br, units); |
|
87 |
} |
|
88 |
|
|
89 |
/** |
|
90 |
* @brief Sets the left and right sides of scout to different speeds. |
|
91 |
* |
|
92 |
* @param speed_l The speed of both left motors. |
|
93 |
* @param speed_r The speed of both right motors. |
|
94 |
* @param units The units to set to. |
|
95 |
* @return Function status |
|
96 |
*/ |
|
97 |
void MotorControl::set_sides(float speed_l, float speed_r, char units) |
|
98 |
{ |
|
99 |
set_each(MOTOR_ALL, speed_l, speed_r, speed_l, speed_r, units); |
|
100 |
} |
|
101 |
|
|
102 |
/** |
|
103 |
* @brief Set motor speeds |
|
104 |
* Sets the speeds of the motors as a percentage of top speed. Can selectively |
|
105 |
* select which motors to set, and which to keep at previous speed. |
|
106 |
* |
|
107 |
* @param which A bitmask of which motors should be set. |
|
108 |
* @param speed The speed the motors should be set to. |
|
109 |
* @param units Optional units for the speeds. |
|
110 |
* @return Function status |
|
111 |
*/ |
|
112 |
void MotorControl::set_each(int which, |
|
113 |
float speed_fl, float speed_fr, |
|
114 |
float speed_bl, float speed_br, |
|
115 |
char units) |
|
116 |
{ |
|
117 |
check_which_ok(which); |
|
118 |
|
|
119 |
motors::set_motors msg; |
|
120 |
|
|
121 |
|
|
122 |
/* Tell the motors node which motors need to be updated */ |
|
123 |
msg.fl_set = msg.fr_set = msg.bl_set = msg.br_set = false; |
|
124 |
if(which & (MOTOR_FL | MOTOR_FL_REV)) |
|
125 |
{ |
|
126 |
msg.fl_set = true; |
|
127 |
motor_fl_speed = rel_to_abs(speed_fl, units); |
|
128 |
} |
|
129 |
if(which & (MOTOR_FR | MOTOR_FR_REV)) |
|
130 |
{ |
|
131 |
msg.fr_set = true; |
|
132 |
motor_fr_speed = rel_to_abs(speed_fr, units); |
|
133 |
} |
|
134 |
if(which & (MOTOR_BL | MOTOR_BL_REV)) |
|
135 |
{ |
|
136 |
msg.bl_set = true; |
|
137 |
motor_bl_speed = rel_to_abs(speed_bl, units); |
|
138 |
} |
|
139 |
if(which & (MOTOR_BR | MOTOR_BR_REV)) |
|
140 |
{ |
|
141 |
msg.br_set = true; |
|
142 |
motor_br_speed = rel_to_abs(speed_br, units); |
|
143 |
} |
|
144 |
|
|
145 |
/* Set all the speeds (the booleans in the set_motors message determine |
|
146 |
* which ones will be used) */ |
|
147 |
msg.fl_speed = (int) motor_fl_speed; |
|
148 |
msg.fr_speed = (int) motor_fr_speed; |
|
149 |
msg.bl_speed = (int) motor_bl_speed; |
|
150 |
msg.br_speed = (int) motor_br_speed; |
|
151 |
|
|
152 |
/* Publishes message to set_motors topic */ |
|
153 |
set_motors_pub.publish(msg); |
|
154 |
ros::spinOnce(); |
|
155 |
} |
|
156 |
|
|
157 |
/** |
|
158 |
* Double-checks the which variable to make sure the front and back |
|
159 |
* bits are not both set for a single motor. |
|
160 |
* |
|
161 |
* @param which The which motor specfication to check. |
|
162 |
*/ |
|
163 |
void MotorControl::check_which_ok(int which) |
|
164 |
{ |
|
165 |
ROS_ERROR_COND( ((which & MOTOR_FL) && (which & MOTOR_FL_REV)), |
|
166 |
"FL Motor set in both directions!"); |
|
167 |
ROS_ERROR_COND( ((which & MOTOR_FR) && (which & MOTOR_FR_REV)), |
|
168 |
"FR Motor set in both directions!"); |
|
169 |
ROS_ERROR_COND( ((which & MOTOR_BL) && (which & MOTOR_BL_REV)), |
|
170 |
"BL Motor set in both directions!"); |
|
171 |
ROS_ERROR_COND( ((which & MOTOR_BR) && (which & MOTOR_BR_REV)), |
|
172 |
"BR Motor set in both directions!"); |
|
173 |
} |
|
174 |
|
|
175 |
/** |
|
176 |
* @brief Query the current speeds of the motors |
|
177 |
* |
|
178 |
* Sends a request to the query_motors service which will reply with the |
|
179 |
* current speed of each motor. |
|
180 |
* |
|
181 |
* @TODO Change so we can get multiple motor speeds with one call |
|
182 |
* |
|
183 |
* @param which A bitmask that will specify which motor speed should be |
|
184 |
* returned |
|
185 |
* @return The speed of the selected motor, or LIB_ERR if no motor selected |
|
186 |
*/ |
|
187 |
float MotorControl::query(int which) |
|
188 |
{ |
|
189 |
motors::query_motors srv; |
|
190 |
if(query_motors_client.call(srv)) |
|
191 |
{ |
|
192 |
switch(which) |
|
193 |
{ |
|
194 |
case MOTOR_FL: |
|
195 |
return srv.response.fl_speed; |
|
196 |
case MOTOR_FR: |
|
197 |
return srv.response.fr_speed; |
|
198 |
case MOTOR_BL: |
|
199 |
return srv.response.bl_speed; |
|
200 |
case MOTOR_BR: |
|
201 |
return srv.response.br_speed; |
|
202 |
default: |
|
203 |
ROS_WARN("Bad WHICH in motors_query."); |
|
204 |
return LIB_ERROR; |
|
205 |
} |
|
206 |
} |
|
207 |
else |
|
208 |
{ |
|
209 |
ROS_ERROR("Failed to call service query_motors"); |
|
210 |
return LIB_ERROR; |
|
211 |
} |
|
212 |
|
|
213 |
return 0; |
|
214 |
} |
|
215 |
|
|
216 |
/** |
|
217 |
* @brief Converts set speeds (of various units) to absolute speeds. |
|
218 |
* |
|
219 |
* @param speed The speed expressed in the desired units |
|
220 |
* @param units The units the desired speed is measured in |
|
221 |
* @return The absolute speed of the motor |
|
222 |
**/ |
|
223 |
float MotorControl::rel_to_abs(float rel_speed, int units) |
|
224 |
{ |
|
225 |
switch(units) |
|
226 |
{ |
|
227 |
case MOTOR_ABSOLUTE:/* Speed given as absolute */ |
|
228 |
return rel_speed; |
|
229 |
case MOTOR_PERCENT:/* Convert from percentage */ |
|
230 |
return rel_speed * MAXSPEED / 100; |
|
231 |
case MOTOR_MMS:/* Convert from mm/s */ |
|
232 |
/** @todo Make math to do this conversion **/ |
|
233 |
return rel_speed; |
|
234 |
case MOTOR_CMS:/* Convert from cm/s */ |
|
235 |
/** @todo Make math to do this conversion **/ |
|
236 |
return rel_speed; |
|
237 |
default: /* The units aren't recognized */ |
|
238 |
/** @todo Decide on default case. Either percent or absolute. **/ |
|
239 |
return rel_speed; |
|
240 |
} |
|
241 |
} |
|
242 |
|
|
243 |
/** |
|
244 |
* @brief Convert absolute speeds to speeds of various units. |
|
245 |
* |
|
246 |
* @param speed The speed expressed in absolute units. |
|
247 |
* @param units The units the desired speed is measured in. |
|
248 |
* @return The relative speed of the motor in desired units. |
|
249 |
**/ |
|
250 |
float MotorControl::abs_to_rel(float abs_speed, int units) |
|
251 |
{ |
|
252 |
switch(units) |
|
253 |
{ |
|
254 |
case MOTOR_ABSOLUTE:/* Speed given as absolute */ |
|
255 |
return abs_speed; |
|
256 |
case MOTOR_PERCENT:/* Convert from percentage */ |
|
257 |
return abs_speed * 100 / MAXSPEED; |
|
258 |
case MOTOR_MMS:/* Convert from mm/s */ |
|
259 |
/** @todo Make math to do this conversion **/ |
|
260 |
return abs_speed; |
|
261 |
case MOTOR_CMS:/* Convert from cm/s */ |
|
262 |
/** @todo Make math to do this conversion **/ |
|
263 |
return abs_speed; |
|
264 |
default: /* The units aren't recognized */ |
|
265 |
/** @todo Decide on default case. Either percent or absolute. **/ |
|
266 |
return abs_speed; |
|
267 |
} |
|
268 |
} |
scout/libscout/src/MotorControl.h | ||
---|---|---|
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 MotorControl.h |
|
28 |
* @brief Contains motor declarations and functions |
|
29 |
* |
|
30 |
* Contains functions and definitions for the use of |
|
31 |
* motors |
|
32 |
* |
|
33 |
* @author Colony Project, CMU Robotics Club |
|
34 |
* @author Ben Wasserman |
|
35 |
* @author Alex Zirbel |
|
36 |
**/ |
|
37 |
|
|
38 |
#ifndef _MOTOR_CONTROL_H_ |
|
39 |
#define _MOTOR_CONTROL_H_ |
|
40 |
|
|
41 |
#include <ros/ros.h> |
|
42 |
/// @TODO maybe not so good! Then packages have to depend on libscout. |
|
43 |
#include <libscout/constants.h> |
|
44 |
#include <motors/query_motors.h> |
|
45 |
#include <motors/set_motors.h> |
|
46 |
|
|
47 |
/* Motor-specific defines */ |
|
48 |
#define MOTOR_ALL 0xF |
|
49 |
#define MOTOR_ALL_REV 0xF0 |
|
50 |
#define MOTOR_NONE 0x0 |
|
51 |
#define MOTOR_FL 0x8 |
|
52 |
#define MOTOR_FR 0x4 |
|
53 |
#define MOTOR_BL 0x2 |
|
54 |
#define MOTOR_BR 0x1 |
|
55 |
#define MOTOR_FL_REV 0x80 |
|
56 |
#define MOTOR_FR_REV 0x40 |
|
57 |
#define MOTOR_BL_REV 0x20 |
|
58 |
#define MOTOR_BR_REV 0x10 |
|
59 |
#define MOTOR_FRONT MOTOR_FL | MOTOR_FR |
|
60 |
#define MOTOR_BACK MOTOR_BR | MOTOR_BR |
|
61 |
#define MOTOR_LEFT MOTOR_FL | MOTOR_BL |
|
62 |
#define MOTOR_RIGHT MOTOR_FR | MOTOR_BR |
|
63 |
#define MOTOR_LEFT_REV MOTOR_FL_REV | MOTOR_BL_REV |
|
64 |
#define MOTOR_RIGHT_REV MOTOR_FR_REV | MOTOR_BR_REV |
|
65 |
#define MOTOR_LEFT_SPIN MOTOR_LEFT_REV | MOTOR_RIGHT |
|
66 |
#define MOTOR_RIGHT_SPIN MOTOR_LEFT | MOTOR_RIGHT_REV |
|
67 |
|
|
68 |
#define MAXSPEED 255 |
|
69 |
#define MOTOR_PERCENT 'p' |
|
70 |
#define MOTOR_MMS 'm' |
|
71 |
#define MOTOR_CMS 'c' |
|
72 |
#define MOTOR_ABSOLUTE 'a' |
|
73 |
#define MOTOR_DEFAULT_UNIT MOTOR_PERCENT |
|
74 |
|
|
75 |
class MotorControl |
|
76 |
{ |
|
77 |
public: |
|
78 |
/** Set up the motor node and prepare to communicate over ROS */ |
|
79 |
MotorControl(const ros::NodeHandle& libscout_node); |
|
80 |
|
|
81 |
/** Uses which to specify what to change, and sets all to same speed */ |
|
82 |
void set(int which, float speed, char units=MOTOR_DEFAULT_UNIT); |
|
83 |
|
|
84 |
/** Sets each side to a different speed */ |
|
85 |
void set_sides(float speed_l, float speed_r, |
|
86 |
char units=MOTOR_DEFAULT_UNIT); |
|
87 |
|
|
88 |
/** Sets each motor speed individually */ |
|
89 |
void set_each(int which, |
|
90 |
float speed_fl, float speed_fr, |
|
91 |
float speed_bl, float speed_br, |
|
92 |
char units=MOTOR_DEFAULT_UNIT); |
|
93 |
|
|
94 |
/** Requests the current motor speeds */ |
|
95 |
float query(int which); |
|
96 |
|
|
97 |
private: |
|
98 |
/** Error if which sets a motor to both forward and backward */ |
|
99 |
void check_which_ok(int which); |
|
100 |
|
|
101 |
float rel_to_abs(float rel_speed, int units); |
|
102 |
float abs_to_rel(float abs_speed, int units); |
|
103 |
|
|
104 |
/* Absolute speeds, but allowing fractions. Useful for PID, etc. */ |
|
105 |
float motor_fl_speed; |
|
106 |
float motor_fr_speed; |
|
107 |
float motor_bl_speed; |
|
108 |
float motor_br_speed; |
|
109 |
|
|
110 |
/** ROS publisher and client declaration */ |
|
111 |
ros::Publisher set_motors_pub; |
|
112 |
ros::ServiceClient query_motors_client; |
|
113 |
|
|
114 |
ros::NodeHandle node; |
|
115 |
|
|
116 |
}; |
|
117 |
|
|
118 |
#endif |
|
119 |
|
scout/libscout/src/alex_behavior.cpp | ||
---|---|---|
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 alex_behavior.cpp |
|
28 |
* @brief A simple robot behavior, smart-runaround style. |
|
29 |
* |
|
30 |
* @author Colony Project, CMU Robotics Club |
|
31 |
* @author Alex Zirbel |
|
32 |
*/ |
|
33 |
|
|
34 |
#include "libscout.h" |
|
35 |
|
|
36 |
/** |
|
37 |
* @brief Main. The main function for the behavior. |
|
38 |
* |
|
39 |
* This is the main function for libscout. It calls init() which initializes |
|
40 |
* the clients and publishers/subscribers for the other parts of the library. |
|
41 |
*/ |
|
42 |
int main(int argc, char **argv) |
|
43 |
{ |
|
44 |
ros::init(argc, argv, "libscout"); |
|
45 |
|
|
46 |
ros::NodeHandle node; |
|
47 |
|
|
48 |
MotorControl motors(node); |
|
49 |
|
|
50 |
ros::Rate loop_rate(10); |
|
51 |
|
|
52 |
while(ros::ok()) |
|
53 |
{ |
|
54 |
motors.set_sides(20, 80); |
|
55 |
|
|
56 |
ros::spinOnce(); |
|
57 |
loop_rate.sleep(); |
|
58 |
} |
|
59 |
|
|
60 |
return 0; |
|
61 |
} |
scout/libscout/src/behavior.cpp | ||
---|---|---|
71 | 71 |
msg.bl_speed = 125; |
72 | 72 |
msg.fr_speed = 125; |
73 | 73 |
msg.br_speed = 125; |
74 |
msg.which = 0; |
|
75 | 74 |
msg.units = MOTOR_ABSOLUTE; |
76 | 75 |
|
77 | 76 |
/* publish */ |
scout/libscout/src/constants.h | ||
---|---|---|
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 |
/** |
|
28 |
* @file constants.h |
|
29 |
* @brief Contains top level scout library constants |
|
30 |
* |
|
31 |
* Contains constants for use in libscout |
|
32 |
* |
|
33 |
* @author Colony Project, CMU Robotics Club |
|
34 |
* @author Ben Wasserman |
|
35 |
**/ |
|
36 |
|
|
37 |
#ifndef _CONSTANTS_H_ |
|
38 |
#define _CONSTANTS_H_ |
|
39 |
|
|
40 |
#include "ros/ros.h" |
|
41 |
|
|
42 |
/* ROS defines */ |
|
43 |
#define QUEUE_SIZE 10 |
|
44 |
|
|
45 |
/* Libscout Defines */ |
|
46 |
|
|
47 |
/* Init defines */ |
|
48 |
#define LIB_ALL -1 |
|
49 |
#define LIB_MOTORS 0x1 |
|
50 |
#define LIB_SONAR 0x2 |
|
51 |
#define LIB_CLIFFSENSORS 0x4 |
|
52 |
#define LIB_HEADLIGHTS 0x8 |
|
53 |
#define LIB_BUTTONS 0x10 |
|
54 |
|
|
55 |
/* Status defines */ |
|
56 |
//TODO MAKE ENUMS |
|
57 |
#define LIB_ERROR -1 |
|
58 |
#define LIB_OK 0 |
|
59 |
|
|
60 |
|
|
61 |
#endif |
scout/libscout/src/libheadlights.cpp | ||
---|---|---|
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 |
/** |
|
28 |
* @file libheadlights.cpp |
|
29 |
* @brief Contains headlights declarations and functions |
|
30 |
* |
|
31 |
* Contains functions and definitions for the use of |
|
32 |
* headlights |
|
33 |
* |
|
34 |
* @author Colony Project, CMU Robotics Club |
|
35 |
* @author Ben Wasserman |
|
36 |
**/ |
|
37 |
|
|
38 |
#include "libheadlights.h" |
|
39 |
|
|
40 |
/* ROS node created in libscout.cpp */ |
|
41 |
extern ros::NodeHandle node; |
|
42 |
|
|
43 |
/** ROS publisher and client declaration */ |
|
44 |
ros::Publisher set_headlights_publisher; |
|
45 |
|
|
46 |
/*! |
|
47 |
* \brief Initialize the headlights module of libscout. |
|
48 |
* |
|
49 |
* Initialize the libscout node as a publisher of set_headlights. |
|
50 |
*/ |
|
51 |
void libheadlights_init() |
|
52 |
{ |
|
53 |
set_headlights_publisher = |
|
54 |
node.advertise<headlights::set_headlights>("libheadlights", 10); |
|
55 |
} |
|
56 |
|
|
57 |
/*! |
|
58 |
* \brief Set headlight colors as a single RGB value |
|
59 |
* Sets the colors of the headlights as a web color value. Can selectively |
|
60 |
* select which motors to set, and which to remain at previous color. |
|
61 |
* \param color The color the headlights should be set to expressed as 0xRRGGBB |
|
62 |
* \param which A bitmask of which headlights should be set |
|
63 |
* \return Function status |
|
64 |
*/ |
|
65 |
int headlights_set(int color, int which) |
|
66 |
{ |
|
67 |
int red, green, blue; |
|
68 |
|
|
69 |
red = (color & RED) >> REDSHIFT; |
|
70 |
green = (color & GREEN) >> GREENSHIFT; |
|
71 |
blue = (color & BLUE) >> BLUESHIFT; |
|
72 |
|
|
73 |
return headlights_set_rgb(red, green, blue, which); |
|
74 |
} |
|
75 |
|
|
76 |
/*! |
|
77 |
* \brief Set headlight colors as separate RGB values |
|
78 |
* Sets the colors of the headlights as a web color value. Can selectively |
|
79 |
* select which motors to set, and which to remain at previous color. |
|
80 |
* \param red The red value the headlights should be set to |
|
81 |
* \param green The green value the headlights should be set to |
|
82 |
* \param blue The blue value the headlights should be set to |
|
83 |
* \param which A bitmask of which headlights should be set |
|
84 |
* \return Function status |
|
85 |
*/ |
|
86 |
int headlights_set_rgb(int red, int green, int blue, int which) |
|
87 |
{ |
|
88 |
/** \todo Set fields of the message based on params */ |
|
89 |
|
|
90 |
headlights::set_headlights msg; |
|
91 |
|
|
92 |
if(!ros::ok()) |
|
93 |
{ |
|
94 |
return LIB_ERROR; |
|
95 |
} |
|
96 |
|
|
97 |
if(which & HL_LEFT) |
|
98 |
{ |
|
99 |
msg.left_red = red; |
|
100 |
msg.left_green = green; |
|
101 |
msg.left_blue = blue; |
|
102 |
} |
|
103 |
else |
|
104 |
{ |
|
105 |
msg.left_red = NO_SET; |
|
106 |
msg.left_green = NO_SET; |
|
107 |
msg.left_blue = NO_SET; |
|
108 |
} |
|
109 |
if(which & HL_RIGHT) |
|
110 |
{ |
|
111 |
msg.right_red = red; |
|
112 |
msg.right_green = green; |
|
113 |
msg.right_blue = blue; |
|
114 |
} |
|
115 |
else |
|
116 |
{ |
|
117 |
msg.right_red = NO_SET; |
|
118 |
msg.right_green = NO_SET; |
|
119 |
msg.right_blue = NO_SET; |
|
120 |
} |
|
121 |
|
|
122 |
/* Publishes message to set_motors topic */ |
|
123 |
set_headlights_publisher.publish(msg); |
|
124 |
ros::spinOnce(); |
|
125 |
|
|
126 |
return LIB_OK; |
|
127 |
} |
scout/libscout/src/libheadlights.h | ||
---|---|---|
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 |
/** |
|
28 |
* @file libheadlights.h |
|
29 |
* @brief Contains headlights declarations and functions |
|
30 |
* |
|
31 |
* Contains functions and definitions for the use of |
|
32 |
* headlights |
|
33 |
* |
|
34 |
* @author Colony Project, CMU Robotics Club |
|
35 |
* @author Ben Wasserman |
|
36 |
**/ |
|
37 |
|
|
38 |
#include "ros/ros.h" |
|
39 |
#include "constants.h" |
|
40 |
#include "headlights/set_headlights.h" |
|
41 |
|
|
42 |
#ifndef _LIBheadlights_H_ |
|
43 |
#define _LIBheadlights_H_ |
|
44 |
|
|
45 |
/* Defines */ |
|
46 |
#define WHITE 0xFFFFFF |
|
47 |
#define OFF 0x000000 |
|
48 |
#define RED 0xFF0000 |
|
49 |
#define GREEN 0x00FF00 |
|
50 |
#define BLUE 0x0000FF |
|
51 |
#define YELLOW 0xFFFF00 |
|
52 |
#define CYAN 0x00FFFF |
|
53 |
#define PINK 0xFF00FF |
|
54 |
|
|
55 |
#define HL_RIGHT 0x1 |
|
56 |
#define HL_LEFT 0x2 |
|
57 |
#define HL_BOTH 0x3 |
|
58 |
|
|
59 |
#define NO_SET -1 |
|
60 |
|
|
61 |
#define REDSHIFT 16 |
|
62 |
#define GREENSHIFT 8 |
|
63 |
#define BLUESHIFT 0 |
|
64 |
|
|
65 |
void libheadlights_init(); |
|
66 |
int headlights_set(int color, int which); |
|
67 |
int headlights_set_rgb(int red, int green, int blue, int which); |
|
68 |
|
|
69 |
#endif |
|
70 |
|
scout/libscout/src/libmotors.cpp | ||
---|---|---|
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 |
/** |
|
28 |
* @file libmotors.cpp |
|
29 |
* @brief Contains motor declarations and functions |
|
30 |
* |
|
31 |
* Contains functions and definitions for the use of |
|
32 |
* motors |
|
33 |
* |
|
34 |
* @author Colony Project, CMU Robotics Club |
|
35 |
**/ |
|
36 |
|
|
37 |
#include "libmotors.h" |
|
38 |
|
|
39 |
/* ROS node created in libscout.cpp */ |
|
40 |
extern ros::NodeHandle node; |
|
41 |
|
|
42 |
/** ROS publisher and client declaration */ |
|
43 |
ros::Publisher set_motors_publisher; |
|
44 |
ros::ServiceClient query_motors_client; |
|
45 |
|
|
46 |
/** |
|
47 |
* @brief Initialize the motors module of libscout. |
|
48 |
* |
|
49 |
* Initialize the libscout node as a publisher of set_motors and a client of |
|
50 |
* query_motors. |
|
51 |
*/ |
|
52 |
void libmotors_init() |
|
53 |
{ |
|
54 |
set_motors_publisher = |
|
55 |
node.advertise<motors::set_motors>("set_motors", QUEUE_SIZE); |
|
56 |
query_motors_client = |
|
57 |
node.serviceClient<motors::query_motors>("query_motors"); |
|
58 |
} |
|
59 |
|
|
60 |
/** |
|
61 |
* @brief Sets all the speeds according to the specificiation of which motors |
|
62 |
* to set. |
|
63 |
* |
|
64 |
* @param which A bitmask of which motors need to be set. |
|
65 |
* @param speed The value to set the motors to. |
|
66 |
* @param units The units the speed is expressed in. |
|
67 |
* @return Function status |
|
68 |
*/ |
|
69 |
int motors_set(int which, float speed, char units) |
|
70 |
{ |
|
71 |
float speed_fl, speed_fr, speed_bl, speed_br; |
|
72 |
speed_fl = speed_fr = speed_bl = speed_br = speed; |
|
73 |
|
|
74 |
if(which & MOTOR_FL_REV) |
|
75 |
{ |
|
76 |
speed_fl = -speed; |
|
77 |
} |
|
78 |
if(which & MOTOR_FR_REV) |
|
79 |
{ |
|
80 |
speed_fr = -speed; |
|
81 |
} |
|
82 |
if(which & MOTOR_BL_REV) |
|
83 |
{ |
|
84 |
speed_bl = -speed; |
|
85 |
} |
|
86 |
if(which & MOTOR_BR_REV) |
|
87 |
{ |
|
88 |
speed_br = -speed; |
|
89 |
} |
|
90 |
|
|
91 |
return motors_set_each(which, speed_fl, speed_fr, speed_bl, speed_br, |
|
92 |
units); |
|
93 |
} |
|
94 |
|
|
95 |
/** |
|
96 |
* @brief Sets the left and right sides of scout to different speeds. |
|
97 |
* |
|
98 |
* @param speed_l The speed of both left motors. |
|
99 |
* @param speed_r The speed of both right motors. |
|
100 |
* @param units The units to set to. |
|
101 |
* @return Function status |
|
102 |
*/ |
|
103 |
int motors_set_sides(float speed_l, float speed_r, char units) |
|
104 |
{ |
|
105 |
return motors_set_each(MOTOR_ALL, speed_l, speed_r, speed_l, speed_r, |
|
106 |
units); |
|
107 |
} |
|
108 |
|
|
109 |
/** |
|
110 |
* @brief Set motor speeds |
|
111 |
* Sets the speeds of the motors as a percentage of top speed. Can selectively |
|
112 |
* select which motors to set, and which to remain at previous speed. |
|
113 |
* |
|
114 |
* @param which A bitmask of which motors should be set. |
|
115 |
* @param speed The speed the motors should be set to. |
|
116 |
* @param units Optional units for the speeds. |
|
117 |
* @return Function status |
|
118 |
*/ |
|
119 |
int motors_set_each(int which, |
|
120 |
float speed_fl, float speed_fr, |
|
121 |
float speed_bl, float speed_br, |
|
122 |
char units) |
|
123 |
{ |
|
124 |
check_which_ok(which); |
|
125 |
|
|
126 |
motors::set_motors msg; |
|
127 |
|
|
128 |
if(!ros::ok()) |
|
129 |
{ |
|
130 |
return LIB_ERROR; |
|
131 |
} |
|
132 |
|
|
133 |
/* Set all the speeds (the booleans in the set_motors message determine |
|
134 |
* which ones will be used) */ |
|
135 |
msg.fl_speed = speed_fl; |
|
136 |
msg.fr_speed = speed_fr; |
|
137 |
msg.bl_speed = speed_bl; |
|
138 |
msg.br_speed = speed_br; |
|
139 |
|
|
140 |
/* Tell the motors node which motors need to be updated */ |
|
141 |
msg.fl_set = msg.fr_set = msg.bl_set = msg.br_set = false; |
|
142 |
if(which & (MOTOR_FL | MOTOR_FL_REV)) |
|
143 |
{ |
|
144 |
msg.fl_set = true; |
|
145 |
} |
|
146 |
if(which & (MOTOR_FR | MOTOR_FR_REV)) |
|
147 |
{ |
|
148 |
msg.fr_set = true; |
|
149 |
} |
|
150 |
if(which & (MOTOR_BL | MOTOR_BL_REV)) |
|
151 |
{ |
|
152 |
msg.bl_set = true; |
|
153 |
} |
|
154 |
if(which & (MOTOR_BR | MOTOR_BR_REV)) |
|
155 |
{ |
|
156 |
msg.br_set = true; |
|
157 |
} |
|
158 |
|
|
159 |
/* Specify which units the speeds are given in */ |
|
160 |
msg.units = units; |
|
161 |
|
|
162 |
/* Publishes message to set_motors topic */ |
|
163 |
set_motors_publisher.publish(msg); |
|
164 |
ros::spinOnce(); |
|
165 |
|
|
166 |
return LIB_OK; |
|
167 |
} |
|
168 |
|
|
169 |
/** |
|
170 |
* Double-checks the which variable to make sure the front and back |
|
171 |
* bits are not both set for a single motor. |
|
172 |
* |
|
173 |
* @param which The which motor specfication to check. |
|
174 |
*/ |
|
175 |
void check_which_ok(int which) |
|
176 |
{ |
|
177 |
ROS_WARN_COND( ((which & MOTOR_FL) && (which & MOTOR_FL_REV)), |
|
178 |
"FL Motor set in both directions!"); |
|
179 |
ROS_WARN_COND( ((which & MOTOR_FR) && (which & MOTOR_FR_REV)), |
|
180 |
"FR Motor set in both directions!"); |
|
181 |
ROS_WARN_COND( ((which & MOTOR_BL) && (which & MOTOR_BL_REV)), |
|
182 |
"BL Motor set in both directions!"); |
|
183 |
ROS_WARN_COND( ((which & MOTOR_BR) && (which & MOTOR_BR_REV)), |
|
184 |
"BR Motor set in both directions!"); |
|
185 |
} |
|
186 |
|
|
187 |
/** |
|
188 |
* @brief Query the current speeds of the motors |
|
189 |
* |
|
190 |
* Sends a request to the query_motors service which will reply with the |
|
191 |
* current speed of each motor. |
|
192 |
* |
|
193 |
* @todo Change so we can get multiple motor speeds with one call |
|
194 |
* |
|
195 |
* @param which A bitmask that will specify which motor speed should be |
|
196 |
* returned |
|
197 |
* @return The speed of the selected motor, or LIB_ERR if no motor selected |
|
198 |
*/ |
|
199 |
float motors_query(int which) |
|
200 |
{ |
|
201 |
motors::query_motors srv; |
|
202 |
if(query_motors_client.call(srv)) |
|
203 |
{ |
|
204 |
switch(which) |
|
205 |
{ |
|
206 |
case MOTOR_FL: |
|
207 |
return srv.response.fl_speed; |
|
208 |
case MOTOR_FR: |
|
209 |
return srv.response.fr_speed; |
|
210 |
case MOTOR_BL: |
|
211 |
return srv.response.bl_speed; |
|
212 |
case MOTOR_BR: |
|
213 |
return srv.response.br_speed; |
|
214 |
default: |
|
215 |
ROS_WARN("Bad WHICH in motors_query."); |
|
216 |
return LIB_ERROR; |
|
217 |
} |
|
218 |
} |
|
219 |
else |
|
220 |
{ |
|
221 |
ROS_ERROR("Failed to call service query_motors"); |
|
222 |
return LIB_ERROR; |
|
223 |
} |
|
224 |
|
|
225 |
return 0; |
|
226 |
} |
scout/libscout/src/libmotors.h | ||
---|---|---|
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 |
/** |
|
28 |
* @file libmotors.h |
|
29 |
* @brief Contains motor declarations and functions |
|
30 |
* |
|
31 |
* Contains functions and definitions for the use of |
|
32 |
* motors |
|
33 |
* |
|
34 |
* @author Colony Project, CMU Robotics Club |
|
35 |
**/ |
|
36 |
|
|
37 |
#include "ros/ros.h" |
|
38 |
#include "constants.h" |
|
39 |
#include "motors/query_motors.h" |
|
40 |
#include "motors/set_motors.h" |
|
41 |
|
|
42 |
#ifndef _LIBMOTORS_H_ |
|
43 |
#define _LIBMOTORS_H_ |
|
44 |
|
|
45 |
/* Defines */ |
|
46 |
#define MOTOR_ALL 0xF |
|
47 |
#define MOTOR_ALL_REV 0xF0 |
|
48 |
#define MOTOR_NONE 0x0 |
|
49 |
#define MOTOR_FL 0x8 |
|
50 |
#define MOTOR_FR 0x4 |
|
51 |
#define MOTOR_BL 0x2 |
|
52 |
#define MOTOR_BR 0x1 |
|
53 |
#define MOTOR_FL_REV 0x80 |
|
54 |
#define MOTOR_FR_REV 0x40 |
|
55 |
#define MOTOR_BL_REV 0x20 |
|
56 |
#define MOTOR_BR_REV 0x10 |
|
57 |
#define MOTOR_FRONT MOTOR_FL | MOTOR_FR |
|
58 |
#define MOTOR_BACK MOTOR_BR | MOTOR_BR |
|
59 |
#define MOTOR_LEFT MOTOR_FL | MOTOR_BL |
|
60 |
#define MOTOR_RIGHT MOTOR_FR | MOTOR_BR |
|
61 |
#define MOTOR_LEFT_REV MOTOR_FL_REV | MOTOR_BL_REV |
|
62 |
#define MOTOR_RIGHT_REV MOTOR_FR_REV | MOTOR_BR_REV |
|
63 |
#define MOTOR_LEFT_SPIN MOTOR_LEFT_REV | MOTOR_RIGHT |
|
64 |
#define MOTOR_RIGHT_SPIN MOTOR_LEFT | MOTOR_RIGHT_REV |
|
65 |
|
|
66 |
#define MOTOR_PERCENT 'p' |
|
67 |
#define MOTOR_MMS 'm' |
|
68 |
#define MOTOR_CMS 'c' |
|
69 |
#define MOTOR_ABSOLUTE 'a' |
|
70 |
#define MOTOR_DEFAULT_UNIT MOTOR_PERCENT |
|
71 |
|
|
72 |
void libmotors_init(); |
|
73 |
int motors_set(int which, float speed, char units=MOTOR_DEFAULT_UNIT); |
|
74 |
int motors_set_sides(float speed_l, float speed_r, |
|
75 |
char units=MOTOR_DEFAULT_UNIT); |
|
76 |
int motors_set_each(int which, |
|
77 |
float speed_fl, float speed_fr, |
|
78 |
float speed_bl, float speed_br, |
|
79 |
char units=MOTOR_DEFAULT_UNIT); |
|
80 |
void check_which_ok(int which); |
|
81 |
float motors_query(int which); |
|
82 |
|
|
83 |
#endif |
|
84 |
|
scout/libscout/src/libscout.cpp | ||
---|---|---|
21 | 21 |
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING |
22 | 22 |
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR |
23 | 23 |
* OTHER DEALINGS IN THE SOFTWARE. |
24 |
**/ |
|
25 |
|
|
24 |
*/ |
|
26 | 25 |
|
27 | 26 |
/** |
28 | 27 |
* @file libscout.cpp |
... | ... | |
33 | 32 |
* |
34 | 33 |
* @author Colony Project, CMU Robotics Club |
35 | 34 |
* @author Ben Wasserman |
36 |
**/
|
|
35 |
*/ |
|
37 | 36 |
|
38 | 37 |
#include "libscout.h" |
39 | 38 |
|
40 |
/* Global objects */ |
|
41 |
ros::NodeHandle node; |
|
42 |
|
|
43 |
/** \todo Decide how to call user behaviors |
|
44 |
* I'm thinking that there be a function call in main that calls the "main" |
|
45 |
fn in the user behavior. However, their "main" function will not be called |
|
46 |
main. However, this has to be able to point to different functions by |
|
47 |
changing as little code in this file as possible. Also, the user behaviors |
|
48 |
should live in a different directory (possibly a different package). |
|
49 |
However, this other package should not be executed as a separate node, |
|
50 |
because that is the job of the libscout node/package to handle all the ROS |
|
51 |
stuff, and keep it under the lid. The user only has to call normal c |
|
52 |
functions. I also want to reduce the need to recompile this package when the |
|
53 |
user behavior, or any other package that works below the surface. |
|
54 |
**/ |
|
39 |
/** @todo Decide how to call user behaviors |
|
40 |
* |
|
41 |
* I'm thinking that there be a function call in main that calls the "main" |
|
42 |
fn in the user behavior. However, their "main" function will not be called |
|
43 |
main. However, this has to be able to point to different functions by |
|
44 |
changing as little code in this file as possible. Also, the user behaviors |
|
45 |
should live in a different directory (possibly a different package). |
|
46 |
However, this other package should not be executed as a separate node, |
|
47 |
because that is the job of the libscout node/package to handle all the ROS |
|
48 |
stuff, and keep it under the lid. The user only has to call normal c |
|
49 |
functions. I also want to reduce the need to recompile this package when the |
|
50 |
user behavior, or any other package that works below the surface. |
|
51 |
*/ |
|
55 | 52 |
|
56 |
/*!
|
|
57 |
* \brief Initializes modules in the libscout.
|
|
53 |
/**
|
|
54 |
* @brief Initializes modules in the libscout.
|
|
58 | 55 |
* |
59 | 56 |
* Calls init functions for each module in libscout. |
60 |
* \param modules A bitmask of the modules that will be initialized. |
|
61 |
**/ |
|
62 |
int init(int modules, int argc, char **argv){ |
|
63 |
ros::init(argc, argv, "libscout"); |
|
57 |
* @param modules A bitmask of the modules that will be initialized. |
|
58 |
*/ |
|
59 |
int init(int modules, int argc, char **argv) |
|
60 |
{ |
|
61 |
ros::init(argc, argv, "libscout"); |
|
62 |
|
|
63 |
ros::NodeHandle node; |
|
64 |
|
|
65 |
MotorControl motors = new MotorControl(node); |
|
66 |
HeadLightControl headlights = new HeadlightControl(node); |
|
64 | 67 |
|
65 |
/** \todo Copy this if for each module that gets added to the library */ |
|
66 |
if(modules & LIB_MOTORS){ |
|
67 |
libmotors_init(); |
|
68 |
} |
|
69 |
if(modules & LIB_HEADLIGHTS){ |
|
70 |
libheadlights_init(); |
|
71 |
} |
|
72 |
if(modules & LIB_BUTTONS){ |
|
73 |
libbuttons_init(); |
|
74 |
} |
|
75 |
/** \todo Add other lib inits **/ |
|
76 |
return 0; |
|
68 |
/** @todo Copy this if for each module that gets added to the library */ |
|
69 |
/*if(modules & LIB_MOTORS) |
|
70 |
{ |
|
71 |
libmotors_init(); |
|
72 |
} |
|
73 |
if(modules & LIB_HEADLIGHTS) |
|
74 |
{ |
|
75 |
libheadlights_init(); |
|
76 |
} |
|
77 |
if(modules & LIB_BUTTONS) |
|
78 |
{ |
|
79 |
libbuttons_init(); |
|
80 |
}*/ |
|
81 |
/** @todo Add other lib inits **/ |
|
82 |
return 0; |
|
77 | 83 |
} |
78 | 84 |
|
scout/libscout/src/libscout.h | ||
---|---|---|
23 | 23 |
* OTHER DEALINGS IN THE SOFTWARE. |
24 | 24 |
**/ |
25 | 25 |
|
26 |
|
|
27 | 26 |
/** |
28 | 27 |
* @file libscout.h |
29 | 28 |
* @brief Contains top level scout library functions |
... | ... | |
41 | 40 |
#ifndef _LIBSCOUT_H_ |
42 | 41 |
#define _LIBSCOUT_H_ |
43 | 42 |
|
44 |
#include "ros/ros.h" |
|
45 |
#include "constants.h" |
Also available in: Unified diff