Revision 276
cleaned up library - dos2unix and delete-trailing-whitespace
move.c | ||
---|---|---|
1 |
/** |
|
2 |
* Copyright (c) 2007 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 move.c |
|
29 |
* @brief Functions for moving |
|
30 |
* |
|
31 |
* Implementation of functions for moving the robot. |
|
32 |
* |
|
33 |
* @author Colony Project, CMU Robotics Club |
|
34 |
**/ |
|
35 |
|
|
36 |
#include "dragonfly_lib.h" |
|
37 |
|
|
38 |
#include "move.h" |
|
39 |
#include "rangefinder.h" |
|
40 |
|
|
41 |
|
|
42 |
// Internal prototypes |
|
43 |
void translateAngulartoLinear (int velocity, int omega, int* vl, int* vr); |
|
44 |
|
|
45 |
// global varaibles for move_avoid |
|
46 |
int d1, d2, d3, d4, d5; /** |
|
47 |
* @defgroup move Movement |
|
48 |
* @brief Functions fo controlling robot motion |
|
49 |
* Higher level functions to control the movement of robots. |
|
50 |
* |
|
51 |
* @{ |
|
52 |
**/ |
|
53 |
|
|
54 |
|
|
55 |
/** |
|
56 |
* Causes the robot to move with the given translation and rotational velocities. |
|
57 |
* motors_init must be called before this function can be used. |
|
58 |
* |
|
59 |
* @param velocity the translational velocity of the robot, in the range -255 to 255. |
|
60 |
* A positive value indicates forward motion, while a negative value indicates |
|
61 |
* backwards motion. |
|
62 |
* |
|
63 |
* @param omega the rotational velocity of the robot, in the range -255 to 255. |
|
64 |
* A positive value indicates a counterclockwise velocity, while a negative |
|
65 |
* value indicates a clockwise velocity. |
|
66 |
* |
|
67 |
* @see motors_init, motor1_set, motor2_set |
|
68 |
**/ |
|
69 |
void move (int velocity, int omega) { |
|
70 |
int vl = 0; |
|
71 |
int vr = 0; |
|
72 |
translateAngulartoLinear(velocity, omega, &vl , &vr ); |
|
73 |
// |
|
74 |
|
|
75 |
if (vl < 0) { |
|
76 |
motor1_set(BACKWARD, -vl); |
|
77 |
} else { |
|
78 |
motor1_set(FORWARD, vl); |
|
79 |
} |
|
80 |
if (vr < 0) { |
|
81 |
motor2_set(BACKWARD, -vr); |
|
82 |
} else { |
|
83 |
motor2_set(FORWARD, vr); |
|
84 |
} |
|
85 |
} |
|
86 |
|
|
87 |
|
|
88 |
/** |
|
89 |
* Moves the robot with the given translational and angular velocities |
|
90 |
* while avoiding obstacles. To be effective, this function must be |
|
91 |
* called repeatedly throughout the motion. It relies on the IR |
|
92 |
* rangefinders to detect obstacles. Before calling this function, |
|
93 |
* motors_init and range_init must be called. |
|
94 |
* |
|
95 |
* @param velocity the translational velocity of the robot, in the |
|
96 |
* range -255 to 255. A positive value indicates forward motion. |
|
97 |
* |
|
98 |
* @param omega the rotational velocity of the robot, in the range |
|
99 |
* -255 to 255. A positive value indicates a counterclockwise velocity. |
|
100 |
* |
|
101 |
* @param strength the strength of the avoid behavior, in the range |
|
102 |
* 0 to 100. |
|
103 |
* |
|
104 |
* @see motors_init, range_init, move |
|
105 |
**/ |
|
106 |
void move_avoid(int velocity, int omega, int strength){ |
|
107 |
int pControl; |
|
108 |
int vl = 0; |
|
109 |
int vr = 0; |
|
110 |
int temp; |
|
111 |
|
|
112 |
temp=range_read_distance(IR1); |
|
113 |
d1 = (temp == -1) ? d1 : temp; |
|
114 |
|
|
115 |
temp=range_read_distance(IR2); |
|
116 |
d2=(temp == -1) ? d2 : temp; |
|
117 |
|
|
118 |
temp=range_read_distance(IR3); |
|
119 |
d3=(temp == -1) ? d3 : temp; |
|
120 |
|
|
121 |
temp=range_read_distance(IR4); |
|
122 |
d4=(temp == -1) ? d4 : temp; |
|
123 |
|
|
124 |
temp=range_read_distance(IR5); |
|
125 |
d5=(temp == -1) ? d5 : temp; |
|
126 |
|
|
127 |
/* Avoid obstacles ahead |
|
128 |
if(d2>170) |
|
129 |
v*=-1; |
|
130 |
|
|
131 |
Naturally slow down if there is something in the way. |
|
132 |
if(d2>150 || d1>180 || d3>180){ |
|
133 |
v>>=1; |
|
134 |
*/ |
|
135 |
|
|
136 |
//pControl= (((d3-d1) + (d4-d5))*strength)/100; |
|
137 |
pControl= (((d5-d4))*strength)/100; |
|
138 |
omega = (omega*(100-strength))/100 + pControl; |
|
139 |
translateAngulartoLinear(velocity, omega, &vl , &vr ); |
|
140 |
|
|
141 |
if (vl < 0) { |
|
142 |
motor1_set(BACKWARD, -vl); |
|
143 |
} else { |
|
144 |
motor1_set(FORWARD, vl); |
|
145 |
} |
|
146 |
if (vr < 0) { |
|
147 |
motor2_set(BACKWARD, -vr); |
|
148 |
} else { |
|
149 |
motor2_set(FORWARD, vr); |
|
150 |
} |
|
151 |
} |
|
152 |
|
|
153 |
/**@}**///end the motion group |
|
154 |
|
|
155 |
void translateAngulartoLinear (int velocity, int omega, int* vl, int* vr) { |
|
156 |
//omega: angle measure, positive couter-clockwise from front. |
|
157 |
// -180 <= omega <= 180 |
|
158 |
//velocity: -255 <= velocity <= 255 |
|
159 |
|
|
160 |
long int vltemp, vrtemp; |
|
161 |
|
|
162 |
//make sure values are in bounds |
|
163 |
if (velocity < -255 || velocity >255 || omega < -255 || omega > 255) return; |
|
164 |
|
|
165 |
//compute |
|
166 |
vrtemp = velocity + omega * 3; |
|
167 |
vltemp = velocity - omega * 3; |
|
168 |
|
|
169 |
//check to see if max linear velocities have been exceeded. |
|
170 |
if (vrtemp > 255) { |
|
171 |
vltemp = 255 * vltemp / vrtemp; |
|
172 |
vrtemp = 255; |
|
173 |
} |
|
174 |
if (vltemp > 255) { |
|
175 |
vrtemp = 255 * vrtemp / vltemp; |
|
176 |
vltemp = 255; |
|
177 |
} |
|
178 |
if (vrtemp < -255) { |
|
179 |
vltemp = -255 * vltemp / vrtemp; |
|
180 |
vrtemp = -255; |
|
181 |
} |
|
182 |
if (vltemp < -255) { |
|
183 |
vrtemp = -255 * vrtemp / vltemp; |
|
184 |
vltemp = -255; |
|
185 |
} |
|
186 |
|
|
187 |
*vr = (int)vrtemp; |
|
188 |
*vl = (int)vltemp; |
|
189 |
|
|
190 |
|
|
191 |
} |
|
192 |
|
|
193 |
|
|
1 |
/** |
|
2 |
* Copyright (c) 2007 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 move.c |
|
29 |
* @brief Functions for moving |
|
30 |
* |
|
31 |
* Implementation of functions for moving the robot. |
|
32 |
* |
|
33 |
* @author Colony Project, CMU Robotics Club |
|
34 |
**/ |
|
35 |
|
|
36 |
#include "dragonfly_lib.h" |
|
37 |
|
|
38 |
#include "move.h" |
|
39 |
#include "rangefinder.h" |
|
40 |
|
|
41 |
|
|
42 |
// Internal prototypes |
|
43 |
void translateAngulartoLinear (int velocity, int omega, int* vl, int* vr); |
|
44 |
|
|
45 |
// global varaibles for move_avoid |
|
46 |
int d1, d2, d3, d4, d5; /** |
|
47 |
* @defgroup move Movement |
|
48 |
* @brief Functions fo controlling robot motion |
|
49 |
* Higher level functions to control the movement of robots. |
|
50 |
* |
|
51 |
* @{ |
|
52 |
**/ |
|
53 |
|
|
54 |
|
|
55 |
/** |
|
56 |
* Causes the robot to move with the given translation and rotational velocities. |
|
57 |
* motors_init must be called before this function can be used. |
|
58 |
* |
|
59 |
* @param velocity the translational velocity of the robot, in the range -255 to 255. |
|
60 |
* A positive value indicates forward motion, while a negative value indicates |
|
61 |
* backwards motion. |
|
62 |
* |
|
63 |
* @param omega the rotational velocity of the robot, in the range -255 to 255. |
|
64 |
* A positive value indicates a counterclockwise velocity, while a negative |
|
65 |
* value indicates a clockwise velocity. |
|
66 |
* |
|
67 |
* @see motors_init, motor1_set, motor2_set |
|
68 |
**/ |
|
69 |
void move (int velocity, int omega) { |
|
70 |
int vl = 0; |
|
71 |
int vr = 0; |
|
72 |
translateAngulartoLinear(velocity, omega, &vl , &vr ); |
|
73 |
// |
|
74 |
|
|
75 |
if (vl < 0) { |
|
76 |
motor1_set(BACKWARD, -vl); |
|
77 |
} else { |
|
78 |
motor1_set(FORWARD, vl); |
|
79 |
} |
|
80 |
if (vr < 0) { |
|
81 |
motor2_set(BACKWARD, -vr); |
|
82 |
} else { |
|
83 |
motor2_set(FORWARD, vr); |
|
84 |
} |
|
85 |
} |
|
86 |
|
|
87 |
|
|
88 |
/** |
|
89 |
* Moves the robot with the given translational and angular velocities |
|
90 |
* while avoiding obstacles. To be effective, this function must be |
|
91 |
* called repeatedly throughout the motion. It relies on the IR |
|
92 |
* rangefinders to detect obstacles. Before calling this function, |
|
93 |
* motors_init and range_init must be called. |
|
94 |
* |
|
95 |
* @param velocity the translational velocity of the robot, in the |
|
96 |
* range -255 to 255. A positive value indicates forward motion. |
|
97 |
* |
|
98 |
* @param omega the rotational velocity of the robot, in the range |
|
99 |
* -255 to 255. A positive value indicates a counterclockwise velocity. |
|
100 |
* |
|
101 |
* @param strength the strength of the avoid behavior, in the range |
|
102 |
* 0 to 100. |
|
103 |
* |
|
104 |
* @see motors_init, range_init, move |
|
105 |
**/ |
|
106 |
void move_avoid(int velocity, int omega, int strength){ |
|
107 |
int pControl; |
|
108 |
int vl = 0; |
|
109 |
int vr = 0; |
|
110 |
int temp; |
|
111 |
|
|
112 |
temp=range_read_distance(IR1); |
|
113 |
d1 = (temp == -1) ? d1 : temp; |
|
114 |
|
|
115 |
temp=range_read_distance(IR2); |
|
116 |
d2=(temp == -1) ? d2 : temp; |
|
117 |
|
|
118 |
temp=range_read_distance(IR3); |
|
119 |
d3=(temp == -1) ? d3 : temp; |
|
120 |
|
|
121 |
temp=range_read_distance(IR4); |
|
122 |
d4=(temp == -1) ? d4 : temp; |
|
123 |
|
|
124 |
temp=range_read_distance(IR5); |
|
125 |
d5=(temp == -1) ? d5 : temp; |
|
126 |
|
|
127 |
/* Avoid obstacles ahead |
|
128 |
if(d2>170) |
|
129 |
v*=-1; |
|
130 |
|
|
131 |
Naturally slow down if there is something in the way. |
|
132 |
if(d2>150 || d1>180 || d3>180){ |
|
133 |
v>>=1; |
|
134 |
*/ |
|
135 |
|
|
136 |
//pControl= (((d3-d1) + (d4-d5))*strength)/100; |
|
137 |
pControl= (((d5-d4))*strength)/100; |
|
138 |
omega = (omega*(100-strength))/100 + pControl; |
|
139 |
translateAngulartoLinear(velocity, omega, &vl , &vr ); |
|
140 |
|
|
141 |
if (vl < 0) { |
|
142 |
motor1_set(BACKWARD, -vl); |
|
143 |
} else { |
|
144 |
motor1_set(FORWARD, vl); |
|
145 |
} |
|
146 |
if (vr < 0) { |
|
147 |
motor2_set(BACKWARD, -vr); |
|
148 |
} else { |
|
149 |
motor2_set(FORWARD, vr); |
|
150 |
} |
|
151 |
} |
|
152 |
|
|
153 |
/**@}**///end the motion group |
|
154 |
|
|
155 |
void translateAngulartoLinear (int velocity, int omega, int* vl, int* vr) { |
|
156 |
//omega: angle measure, positive couter-clockwise from front. |
|
157 |
// -180 <= omega <= 180 |
|
158 |
//velocity: -255 <= velocity <= 255 |
|
159 |
|
|
160 |
long int vltemp, vrtemp; |
|
161 |
|
|
162 |
//make sure values are in bounds |
|
163 |
if (velocity < -255 || velocity >255 || omega < -255 || omega > 255) return; |
|
164 |
|
|
165 |
//compute |
|
166 |
vrtemp = velocity + omega * 3; |
|
167 |
vltemp = velocity - omega * 3; |
|
168 |
|
|
169 |
//check to see if max linear velocities have been exceeded. |
|
170 |
if (vrtemp > 255) { |
|
171 |
vltemp = 255 * vltemp / vrtemp; |
|
172 |
vrtemp = 255; |
|
173 |
} |
|
174 |
if (vltemp > 255) { |
|
175 |
vrtemp = 255 * vrtemp / vltemp; |
|
176 |
vltemp = 255; |
|
177 |
} |
|
178 |
if (vrtemp < -255) { |
|
179 |
vltemp = -255 * vltemp / vrtemp; |
|
180 |
vrtemp = -255; |
|
181 |
} |
|
182 |
if (vltemp < -255) { |
|
183 |
vrtemp = -255 * vrtemp / vltemp; |
|
184 |
vltemp = -255; |
|
185 |
} |
|
186 |
|
|
187 |
*vr = (int)vrtemp; |
|
188 |
*vl = (int)vltemp; |
|
189 |
|
|
190 |
|
|
191 |
} |
|
192 |
|
|
193 |
|
Also available in: Unified diff