root / branches / encoders / code / projects / libdragonfly / encoders.c @ 1345
History | View | Annotate | Download (7.99 KB)
1 | 674 | justin | #include "encoders.h" |
---|---|---|---|
2 | #include "spi.h" |
||
3 | #include <dragonfly_lib.h> |
||
4 | 675 | justin | #include "ring_buffer.h" |
5 | 495 | kwoo | |
6 | 749 | bneuman | unsigned int left_data_buf; |
7 | unsigned int right_data_buf; |
||
8 | 844 | justin | char encoder_buf_index;
|
9 | 674 | justin | |
10 | 749 | bneuman | unsigned int left_data; |
11 | unsigned int right_data; |
||
12 | 674 | justin | |
13 | 749 | bneuman | unsigned int left_data_array[BUFFER_SIZE]; |
14 | unsigned int right_data_array[BUFFER_SIZE]; |
||
15 | 677 | jykong | int left_data_idx;
|
16 | int right_data_idx;
|
||
17 | 675 | justin | |
18 | 677 | jykong | int left_dx;
|
19 | int right_dx;
|
||
20 | long int timecount; |
||
21 | |||
22 | 771 | bneuman | volatile short int data_ready; |
23 | |||
24 | 677 | jykong | void encoder_recv(char data); |
25 | |||
26 | //Helper Function Prototypes
|
||
27 | inline void left_data_array_put(unsigned short int value); |
||
28 | 749 | bneuman | inline unsigned int left_data_array_top(void); |
29 | inline unsigned int left_data_array_prev(void); |
||
30 | inline unsigned int left_data_array_bottom(void); |
||
31 | 677 | jykong | |
32 | inline void right_data_array_put(unsigned short int value); |
||
33 | 749 | bneuman | inline unsigned int right_data_array_top(void); |
34 | inline unsigned int right_data_array_prev(void); |
||
35 | inline unsigned int right_data_array_bottom(void); |
||
36 | 677 | jykong | |
37 | |||
38 | 749 | bneuman | void encoder_recv_complete(){
|
39 | 854 | justin | encoder_buf_index = 0;
|
40 | data_ready++; |
||
41 | 771 | bneuman | |
42 | 854 | justin | spi_transfer(5);
|
43 | 749 | bneuman | } |
44 | |||
45 | 854 | justin | /**
|
46 | * @brief Initializes encoder variables and the hardware interface.
|
||
47 | */
|
||
48 | 674 | justin | void encoders_init(void){ |
49 | 677 | jykong | int i;
|
50 | 771 | bneuman | |
51 | data_ready=0;
|
||
52 | 677 | jykong | |
53 | 854 | justin | spi_init(encoder_recv, encoder_recv_complete); |
54 | 844 | justin | encoder_buf_index = 0;
|
55 | 674 | justin | left_data_buf = 0;
|
56 | right_data_buf= 0;
|
||
57 | 1345 | chihsiuh | |
58 | 674 | justin | left_data = -1;
|
59 | right_data = -1;
|
||
60 | 771 | bneuman | |
61 | 677 | jykong | //RING_BUFFER_INIT(enc_buffer,BUFFER_SIZE);
|
62 | left_data_idx = 0;
|
||
63 | right_data_idx = 0;
|
||
64 | for(i = 0; i < BUFFER_SIZE; i++) { |
||
65 | left_data_array[i] = 0;
|
||
66 | } |
||
67 | for(i = 0; i < BUFFER_SIZE; i++) { |
||
68 | right_data_array[i] = 0;
|
||
69 | } |
||
70 | 749 | bneuman | spi_transfer(5);
|
71 | 674 | justin | } |
72 | |||
73 | 773 | kwoo | /**
|
74 | 854 | justin | * @brief Returns the specified encoders value
|
75 | 773 | kwoo | *
|
76 | * @param encoder this is the encoder that you want to read. Valid arguments
|
||
77 | * are LEFT and RIGHT
|
||
78 | *
|
||
79 | * @return the value of the specified encoder
|
||
80 | **/
|
||
81 | 674 | justin | int encoder_read(char encoder){ |
82 | 854 | justin | |
83 | if(encoder==LEFT) return left_data; |
||
84 | else if(encoder==RIGHT) return right_data; |
||
85 | else return -1; |
||
86 | 674 | justin | } |
87 | |||
88 | |||
89 | 854 | justin | /**
|
90 | * @brief Outputs encoder direction as FORWARD OR BACK
|
||
91 | * A STUB! DO NOT use.
|
||
92 | *
|
||
93 | * @param encoder The encoder you want the direction of.
|
||
94 | * Valid arguments are right and left.
|
||
95 | *
|
||
96 | * @return FORWARD or BACK (the constants)
|
||
97 | */
|
||
98 | 674 | justin | char encoder_direction(char encoder){ |
99 | return 0; |
||
100 | } |
||
101 | 854 | justin | |
102 | 773 | kwoo | /**
|
103 | 854 | justin | * Gets the total distance covered by the specified encoder (in encoder clicks)
|
104 | 773 | kwoo | *
|
105 | * @param encoder the encoder that you want to read, use LEFT or RIGHT
|
||
106 | *
|
||
107 | 854 | justin | * @return The distance covered by the specified encoder.
|
108 | 773 | kwoo | **/
|
109 | 677 | jykong | int encoder_get_dx(char encoder) { |
110 | 854 | justin | |
111 | if(encoder==LEFT) return left_dx; |
||
112 | else if(encoder==RIGHT) return right_dx; |
||
113 | 677 | jykong | else return -1; |
114 | } |
||
115 | |||
116 | 773 | kwoo | /**
|
117 | 854 | justin | * Resets the distance accumulator for the specified
|
118 | 773 | kwoo | * encoder.
|
119 | *
|
||
120 | 854 | justin | * @param encoder the encoder that you want to reset distance for
|
121 | 773 | kwoo | **/
|
122 | 677 | jykong | void encoder_rst_dx(char encoder) { |
123 | 854 | justin | |
124 | if(encoder==LEFT) left_dx = 0; |
||
125 | else if(encoder==RIGHT) right_dx = 0; |
||
126 | 677 | jykong | } |
127 | |||
128 | 854 | justin | /**
|
129 | * @brief Returns the number of encoder reads that have occurred.
|
||
130 | *
|
||
131 | * @return The time count.
|
||
132 | */
|
||
133 | 677 | jykong | int encoder_get_tc(void) { |
134 | return timecount;
|
||
135 | } |
||
136 | |||
137 | 854 | justin | /**
|
138 | * @brief Resets the encoder read counter.
|
||
139 | */
|
||
140 | 677 | jykong | void encoder_rst_tc(void) { |
141 | timecount = 0;
|
||
142 | } |
||
143 | |||
144 | 1345 | chihsiuh | ///////////////////////////////////////////////////////////////////////
|
145 | int left_data_at(int index) { |
||
146 | int tmp_idx = left_data_idx - index;
|
||
147 | if (tmp_idx < 0) |
||
148 | tmp_idx += BUFFER_SIZE; |
||
149 | return left_data_array[tmp_idx];
|
||
150 | } |
||
151 | 677 | jykong | |
152 | 1345 | chihsiuh | int right_data_at(int index) { |
153 | int tmp_idx = right_data_idx - index;
|
||
154 | if (tmp_idx < 0) |
||
155 | tmp_idx += BUFFER_SIZE; |
||
156 | return right_data_array[tmp_idx];
|
||
157 | } |
||
158 | |||
159 | |||
160 | int get_dx(char encoder, int index) { |
||
161 | int dx, ctr;
|
||
162 | ctr = 0;
|
||
163 | dx = 1024;
|
||
164 | do {
|
||
165 | if (encoder == LEFT)
|
||
166 | dx = left_data_at(index+ctr+38) - left_data_at(index+ctr);
|
||
167 | else
|
||
168 | dx = right_data_at(index+ctr) - right_data_at(index+ctr+38);
|
||
169 | ctr++; |
||
170 | } while ((dx > 30 || dx < -30) && ctr < 3); |
||
171 | if (dx > 30 || dx < -30) |
||
172 | return ERR_VEL;
|
||
173 | return dx;
|
||
174 | } |
||
175 | 854 | justin | /**
|
176 | * @brief Returns the approximated instantaneous velocity of the robot
|
||
177 | * in terms of encoder clicks.
|
||
178 | *
|
||
179 | * @param encoder RIGHT or LEFT - the wheel you want the velocity for.
|
||
180 | *
|
||
181 | * @return The instantaneous velocity for the given wheel.
|
||
182 | */
|
||
183 | 771 | bneuman | int encoder_get_v(char encoder){ |
184 | 1345 | chihsiuh | /*
|
185 | 1191 | chihsiuh | if (encoder == LEFT)
|
186 | return left_data_array_bottom() - left_data_array_top();
|
||
187 | 771 | bneuman | |
188 | 1191 | chihsiuh | if (encoder == RIGHT)
|
189 | return right_data_array_top() - right_data_array_bottom();
|
||
190 | 771 | bneuman | |
191 | 1345 | chihsiuh | return -1;
|
192 | */
|
||
193 | int vel1, vel2, tmp;
|
||
194 | vel1 = get_dx(encoder, 0);
|
||
195 | vel2 = get_dx(encoder, 1);
|
||
196 | |||
197 | if (vel1 == ERR_VEL && vel2 == ERR_VEL)
|
||
198 | return ERR_VEL << 1; |
||
199 | else if (vel2 == ERR_VEL) |
||
200 | return vel1 << 1; |
||
201 | else if (vel1 == ERR_VEL) |
||
202 | return vel2 << 1; |
||
203 | else
|
||
204 | return vel1 + vel2;
|
||
205 | 771 | bneuman | } |
206 | 1345 | chihsiuh | /////////////////////////////////////////////////////////////////////////
|
207 | 771 | bneuman | |
208 | 854 | justin | /**
|
209 | * @brief Waits until n encoder reads have occurred.
|
||
210 | * Counter is reset on functions exit.
|
||
211 | *
|
||
212 | * @param n
|
||
213 | */
|
||
214 | 771 | bneuman | void encoder_wait(int n){ |
215 | while(data_ready<n);
|
||
216 | data_ready=0;
|
||
217 | } |
||
218 | |||
219 | |||
220 | 674 | justin | //Full reads occur every 40 microseconds. This function should be called
|
221 | //every 8 microseconds.
|
||
222 | void encoder_recv(char data){ |
||
223 | 677 | jykong | short int dx; |
224 | |||
225 | 808 | justin | //Parse the encoder data, comes in over 5 bytes 16 bits per encoder,
|
226 | // second is offset by 1 bit.
|
||
227 | 844 | justin | switch(encoder_buf_index){
|
228 | 808 | justin | case 0: |
229 | right_data_buf |= ((short)data)<<8 & 0xff00; |
||
230 | break;
|
||
231 | case 1: |
||
232 | right_data_buf |= ((short)data) & 0xff; |
||
233 | break;
|
||
234 | case 2: |
||
235 | left_data_buf |= (((short)data) << 9) & (0x7F << 9); |
||
236 | break;
|
||
237 | case 3: |
||
238 | left_data_buf |= (((short)data) << 1) & (0xFF<<1); |
||
239 | break;
|
||
240 | case 4: left_data_buf |= (((short)data)>>7) & 0x1; |
||
241 | } |
||
242 | 771 | bneuman | |
243 | 844 | justin | encoder_buf_index = (encoder_buf_index + 1) % 5; |
244 | 677 | jykong | |
245 | 844 | justin | if(encoder_buf_index==0) { |
246 | 854 | justin | |
247 | /*Error handling for the left encoder*/
|
||
248 | if(!(left_data_buf & OCF))
|
||
249 | left_data = ENCODER_DATA_NOT_READY; |
||
250 | if(left_data_buf & (COF | LIN))
|
||
251 | left_data = ENCODER_MISALIGNED; |
||
252 | else if((left_data_buf & MagINCn) && (left_data_buf & MagDECn)) |
||
253 | left_data = ENCODER_MAGNET_FAILURE; |
||
254 | 808 | justin | else left_data = (left_data_buf>>5) & 1023; |
255 | 771 | bneuman | |
256 | 854 | justin | /*Error handling for the right encoder*/
|
257 | if(!(right_data_buf & OCF))
|
||
258 | right_data = ENCODER_DATA_NOT_READY; |
||
259 | if(right_data_buf & (COF | LIN))
|
||
260 | right_data = ENCODER_MISALIGNED; |
||
261 | else if ((right_data_buf & MagINCn) && (right_data_buf & MagDECn)) |
||
262 | right_data = ENCODER_MAGNET_FAILURE; |
||
263 | 808 | justin | else right_data = (right_data_buf>>5) & 1023; |
264 | 771 | bneuman | |
265 | 808 | justin | left_data_buf = 0;
|
266 | right_data_buf = 0;
|
||
267 | 752 | bneuman | |
268 | 854 | justin | /*Above 1023 is invalid data*/
|
269 | if(!(left_data > 1023)) { |
||
270 | 771 | bneuman | left_data_array_put(left_data); |
271 | 752 | bneuman | |
272 | 771 | bneuman | //Adjust left accumulator
|
273 | 1191 | chihsiuh | dx = - left_data + left_data_array_prev(); |
274 | 771 | bneuman | |
275 | 808 | justin | if(left_data_array_prev()==0) dx=0; |
276 | 752 | bneuman | |
277 | 808 | justin | if(dx > 512) left_dx += dx - 1023; //Underflow |
278 | else if(dx < -512) left_dx += dx + 1023; //Overflow |
||
279 | else left_dx += dx;
|
||
280 | 677 | jykong | } |
281 | |||
282 | 854 | justin | /*Above 1023 is invalid data*/
|
283 | if(!(right_data > 1023)) { |
||
284 | 771 | bneuman | right_data_array_put(right_data); |
285 | 677 | jykong | |
286 | 771 | bneuman | //Adjust right accumulator
|
287 | dx = right_data - right_data_array_prev(); |
||
288 | 752 | bneuman | |
289 | 808 | justin | if(right_data_array_prev()==0) dx=0; |
290 | |||
291 | if(dx > 512) right_dx += dx - 1023; //underflow |
||
292 | else if(dx < -512) right_dx += dx + 1023; //overflow |
||
293 | else right_dx += dx;
|
||
294 | 677 | jykong | } |
295 | 808 | justin | } |
296 | 677 | jykong | |
297 | 808 | justin | //Increment timecount accumulator
|
298 | timecount++; |
||
299 | 674 | justin | } |
300 | 677 | jykong | |
301 | 844 | justin | |
302 | 677 | jykong | //Helper Functions
|
303 | inline void left_data_array_put(unsigned short int value) { |
||
304 | if(left_data_idx == BUFFER_SIZE-1) |
||
305 | left_data_idx = 0;
|
||
306 | else
|
||
307 | left_data_idx++; |
||
308 | left_data_array[left_data_idx] = value; |
||
309 | } |
||
310 | |||
311 | 749 | bneuman | inline unsigned int left_data_array_top(void) { |
312 | 677 | jykong | return left_data_array[left_data_idx];
|
313 | } |
||
314 | |||
315 | 749 | bneuman | inline unsigned int left_data_array_prev(void) { |
316 | 677 | jykong | if(left_data_idx == 0) |
317 | return left_data_array[BUFFER_SIZE-1]; |
||
318 | else
|
||
319 | return left_data_array[left_data_idx - 1]; |
||
320 | } |
||
321 | |||
322 | 749 | bneuman | inline unsigned int left_data_array_bottom(void) { |
323 | 677 | jykong | if(left_data_idx == BUFFER_SIZE-1) |
324 | return left_data_array[0]; |
||
325 | else
|
||
326 | return left_data_array[left_data_idx + 1]; |
||
327 | } |
||
328 | |||
329 | inline void right_data_array_put(unsigned short int value) { |
||
330 | if(right_data_idx == BUFFER_SIZE-1) |
||
331 | right_data_idx = 0;
|
||
332 | else
|
||
333 | right_data_idx++; |
||
334 | right_data_array[right_data_idx] = value; |
||
335 | } |
||
336 | |||
337 | 749 | bneuman | inline unsigned int right_data_array_top(void) { |
338 | 677 | jykong | return right_data_array[right_data_idx];
|
339 | } |
||
340 | |||
341 | 749 | bneuman | inline unsigned int right_data_array_prev(void) { |
342 | 677 | jykong | if(right_data_idx == 0) |
343 | return right_data_array[BUFFER_SIZE-1]; |
||
344 | else
|
||
345 | return right_data_array[right_data_idx - 1]; |
||
346 | } |
||
347 | |||
348 | 749 | bneuman | inline unsigned int right_data_array_bottom(void) { |
349 | 677 | jykong | if(right_data_idx == BUFFER_SIZE-1) |
350 | return right_data_array[0]; |
||
351 | else
|
||
352 | return right_data_array[right_data_idx + 1]; |
||
353 | } |