Revision 924
Odometry will temporarily not work. Working code commented out. This commit is so the blight on the world that is
colony math.h will be eradicated for all time.
trunk/code/lib/include/libdragonfly/math.h | ||
---|---|---|
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 |
* |
|
29 |
* @file math.h |
|
30 |
* @brief Contains math function(s) |
|
31 |
* |
|
32 |
* @author Colony Project, CMU Robotics Club, James Kong |
|
33 |
*/ |
|
34 |
|
|
35 |
#ifndef _MATH_H_ |
|
36 |
#define _MATH_H_ |
|
37 |
|
|
38 |
/** |
|
39 |
* @addtogroup math |
|
40 |
* @{ |
|
41 |
**/ |
|
42 |
|
|
43 |
/** @brief absolute value **/ |
|
44 |
int abs_int(int x); |
|
45 |
|
|
46 |
/**@}**/ //end group |
|
47 |
|
|
48 |
#endif |
|
49 |
|
trunk/code/lib/include/libdragonfly/dragonfly_lib.h | ||
---|---|---|
87 | 87 |
#include <bom.h> |
88 | 88 |
#include <move.h> |
89 | 89 |
#include <reset.h> |
90 |
#include <math.h> |
|
91 | 90 |
|
92 | 91 |
#endif |
93 | 92 |
|
trunk/code/projects/mapping/odometry/odometry.c | ||
---|---|---|
4 | 4 |
#include <avr/interrupt.h> |
5 | 5 |
#include <dragonfly_lib.h> |
6 | 6 |
|
7 |
long encoder_ltm1, encoder_rtm1;
|
|
7 |
int encoder_ltm1, encoder_rtm1;
|
|
8 | 8 |
|
9 | 9 |
long diff_x,diff_y; |
10 | 10 |
double angle; |
11 | 11 |
|
12 | 12 |
|
13 |
int odometry_dx(void){
|
|
13 |
long odometry_dx(void){
|
|
14 | 14 |
return diff_x; |
15 | 15 |
} |
16 | 16 |
|
17 |
int odometry_dy(void){
|
|
17 |
long odometry_dy(void){
|
|
18 | 18 |
return diff_y; |
19 | 19 |
} |
20 | 20 |
|
... | ... | |
23 | 23 |
} |
24 | 24 |
|
25 | 25 |
void odometry_init(void){ |
26 |
|
|
26 | 27 |
encoders_init(); |
28 |
|
|
27 | 29 |
odometry_reset(); |
30 |
|
|
28 | 31 |
|
29 | 32 |
//CTC Mode. CLK / 1024 |
30 | 33 |
TCCR2 = 0; // (Fully disconnected) |
... | ... | |
36 | 39 |
} |
37 | 40 |
|
38 | 41 |
void odometry_reset(void){ |
42 |
char buf[40]; |
|
39 | 43 |
diff_x = 0; |
40 | 44 |
diff_y = 0; |
45 |
encoder_ltm1 = encoder_read(LEFT); |
|
46 |
encoder_rtm1 = encoder_read(RIGHT); |
|
47 |
sprintf(buf,"(en_right,en_left) in um: (%d,%d)\n\r",encoder_ltm1,encoder_rtm1); |
|
48 |
usb_puts(buf); |
|
41 | 49 |
angle = 0.0; |
42 | 50 |
} |
43 | 51 |
|
44 |
/*Currently assumes robot only goes forward.*/
|
|
52 |
/*Currently assumes robot only goes forward. |
|
45 | 53 |
void odometry_run(void){ |
46 | 54 |
double theta; |
47 | 55 |
int encoder_left, encoder_right,dl,dr; |
... | ... | |
97 | 105 |
|
98 | 106 |
encoder_ltm1 = encoder_left; |
99 | 107 |
encoder_rtm1 = encoder_right; |
108 |
}*/ |
|
109 |
|
|
110 |
/*Generalized version of odometry.*/ |
|
111 |
void odometry_run(void){ |
|
112 |
//Angle swept through in a time step CCW- |
|
113 |
double theta, rl, dc; |
|
114 |
|
|
115 |
long dr,dl; |
|
116 |
|
|
117 |
char buf[40]; |
|
118 |
//Relative to the inner wheel, turn radius. |
|
119 |
|
|
120 |
//Get the change in wheel positions |
|
121 |
|
|
122 |
int encoder_right = encoder_read(RIGHT); |
|
123 |
int encoder_left = encoder_read(LEFT); |
|
124 |
|
|
125 |
sprintf(buf,"(en_right,en_left) in um: (%d,%d)\n\r",encoder_left,encoder_right); |
|
126 |
usb_puts(buf); |
|
127 |
|
|
128 |
dl = encoder_left - encoder_ltm1; |
|
129 |
dr = encoder_right - encoder_rtm1; |
|
130 |
|
|
131 |
if(dl >=-1 && dl <= 1 && dr >= -1 && dr <= 1) return; |
|
132 |
|
|
133 |
encoder_ltm1 = encoder_left; |
|
134 |
encoder_rtm1 = encoder_right; |
|
135 |
|
|
136 |
//sprintf(buf,"(dl,dr) in clicks: (%ld,%ld)\n\r",dl,dr); |
|
137 |
//usb_puts(buf); |
|
138 |
|
|
139 |
// Try to avoid over/underflow. |
|
140 |
dl = dl > 512 ? dl - 1024 :dl; |
|
141 |
dl = dl < -512 ? dl + 1024 :dl; |
|
142 |
dr = dr > 512 ? dr - 1024 :dr; |
|
143 |
dr = dr < -512 ? dr + 1024 :dr; |
|
144 |
|
|
145 |
sprintf(buf,"(dl,dr) in um: (%ld,%ld)\n\r",dl,dr); |
|
146 |
usb_puts(buf); |
|
147 |
//Convert "clicks" to um |
|
148 |
dl *= CLICK_DISTANCE_UM; //um |
|
149 |
dr *= CLICK_DISTANCE_UM; |
|
150 |
|
|
151 |
if(dl == dr){ |
|
152 |
//usb_putc('#'); |
|
153 |
diff_x += dl*cos(angle)/1000; |
|
154 |
diff_y += dl*sin(angle)/1000; |
|
155 |
//sprintf(buf,"Distance in um: %ld\n\r",dl); |
|
156 |
//usb_puts(buf); |
|
157 |
return; |
|
158 |
} |
|
159 |
//usb_putc('*'); |
|
160 |
//sprintf(buf,"(dl,dr) in um: (%ld,%ld)\n\r",dl,dr); |
|
161 |
//usb_puts(buf); |
|
162 |
|
|
163 |
//Call the left wheel 0, clockwise positive. |
|
164 |
|
|
165 |
rl = ((double)(ROBOT_WIDTH_UM*dl))/((double)(dl - dr)); //um |
|
166 |
|
|
167 |
//sprintf(buf,"Radius from left wheel: %ld\n\r",rl); |
|
168 |
//usb_puts(buf); |
|
169 |
|
|
170 |
theta = ((double)(dl - dr))/((double)ROBOT_WIDTH_UM); //rad |
|
171 |
|
|
172 |
dc = (rl - ROBOT_WIDTH_UM/2)*theta; //um |
|
173 |
|
|
174 |
//sprintf(buf,"Distance in um: %ld\n\r",dc); |
|
175 |
//usb_puts(buf); |
|
176 |
|
|
177 |
diff_x += ((dc * cos(angle))/1000); //mm |
|
178 |
diff_y += ((dc * sin(angle))/1000); //mm |
|
179 |
|
|
180 |
//sprintf(buf,"(dx, dy): (%ld, %ld)\n\r",diff_x, diff_y); |
|
181 |
//usb_puts(buf); |
|
182 |
|
|
183 |
//Change state variables. |
|
184 |
|
|
185 |
//angle is necessarily CCW+, so subtract. |
|
186 |
angle -= theta; |
|
187 |
|
|
100 | 188 |
} |
101 | 189 |
|
102 | 190 |
ISR(TIMER2_COMP_vect){ |
103 |
odometry_run(); |
|
191 |
odometry_run();
|
|
104 | 192 |
} |
105 | 193 |
|
106 | 194 |
|
trunk/code/projects/mapping/odometry/main.c | ||
---|---|---|
7 | 7 |
odometry_init(); |
8 | 8 |
char buf[100]; |
9 | 9 |
while(1){ |
10 |
sprintf(buf,"X: %i, Y: %i, Theta: %f\n\r",odometry_dx(),odometry_dy(),odometry_angle());
|
|
11 |
usb_puts(buf); |
|
10 |
//sprintf(buf,"X: %ld, Y: %ld, Theta: %lf\n\r", odometry_dx(), odometry_dy(), odometry_angle());
|
|
11 |
//usb_puts(buf);
|
|
12 | 12 |
delay_ms(500); |
13 | 13 |
} |
14 | 14 |
return 0; |
trunk/code/projects/mapping/odometry/odometry.h | ||
---|---|---|
16 | 16 |
|
17 | 17 |
//Standard measures will be mm and us |
18 | 18 |
|
19 |
int odometry_dx(void);
|
|
20 |
int odometry_dy(void);
|
|
19 |
long odometry_dx(void);
|
|
20 |
long odometry_dy(void);
|
|
21 | 21 |
double odometry_angle(void); |
22 | 22 |
void odometry_init(void); |
23 | 23 |
void odometry_reset(void); |
trunk/code/projects/libdragonfly/math.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 math.c |
|
29 |
* @brief Math Functions |
|
30 |
* |
|
31 |
* Useful math functions. |
|
32 |
* |
|
33 |
* @author Colony Project, CMU Robotics Club |
|
34 |
**/ |
|
35 |
|
|
36 |
#include "math.h" |
|
37 |
|
|
38 |
/** |
|
39 |
* @defgroup math Math |
|
40 |
* Function(s) for performing math operations |
|
41 |
* |
|
42 |
* @{ |
|
43 |
**/ |
|
44 |
|
|
45 |
/** |
|
46 |
* finds the absolute value of x |
|
47 |
* |
|
48 |
* @param x the int of which we want to take its absolute value |
|
49 |
* |
|
50 |
* @return the absolute value of x |
|
51 |
**/ |
|
52 |
int abs_int (int x) { |
|
53 |
|
|
54 |
if(x < 0) |
|
55 |
return -x; |
|
56 |
|
|
57 |
return x; |
|
58 |
} |
|
59 |
|
|
60 |
/**@}**/ //end defgroup |
|
61 |
|
trunk/code/projects/libdragonfly/math.h | ||
---|---|---|
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 |
* |
|
29 |
* @file math.h |
|
30 |
* @brief Contains math function(s) |
|
31 |
* |
|
32 |
* @author Colony Project, CMU Robotics Club, James Kong |
|
33 |
*/ |
|
34 |
|
|
35 |
#ifndef _MATH_H_ |
|
36 |
#define _MATH_H_ |
|
37 |
|
|
38 |
/** |
|
39 |
* @addtogroup math |
|
40 |
* @{ |
|
41 |
**/ |
|
42 |
|
|
43 |
/** @brief absolute value **/ |
|
44 |
int abs_int(int x); |
|
45 |
|
|
46 |
/**@}**/ //end group |
|
47 |
|
|
48 |
#endif |
|
49 |
|
trunk/code/projects/libdragonfly/dragonfly_lib.h | ||
---|---|---|
87 | 87 |
#include <bom.h> |
88 | 88 |
#include <move.h> |
89 | 89 |
#include <reset.h> |
90 |
#include <math.h> |
|
91 | 90 |
|
92 | 91 |
#endif |
93 | 92 |
|
Also available in: Unified diff