Revision 749
added another function pointer to encoders which gets called after all bytes are recv'd.
working on a behavior to drive in a straight line, but encoders are giving invalid on robot 5
encoders.c | ||
---|---|---|
1 |
#include "encoders.h" |
|
2 |
#include "spi.h" |
|
3 |
#include <dragonfly_lib.h> |
|
4 |
#include "ring_buffer.h" |
|
1 | 5 |
|
2 |
//Dummy file for now. |
|
6 |
unsigned int left_data_buf; |
|
7 |
unsigned int right_data_buf; |
|
8 |
char buf_index; |
|
9 |
|
|
10 |
unsigned int left_data; |
|
11 |
unsigned int right_data; |
|
12 |
|
|
13 |
unsigned int left_data_array[BUFFER_SIZE]; |
|
14 |
unsigned int right_data_array[BUFFER_SIZE]; |
|
15 |
int left_data_idx; |
|
16 |
int right_data_idx; |
|
17 |
|
|
18 |
int left_dx; |
|
19 |
int right_dx; |
|
20 |
long int timecount; |
|
21 |
|
|
22 |
void encoder_recv(char data); |
|
23 |
|
|
24 |
//Helper Function Prototypes |
|
25 |
inline void left_data_array_put(unsigned short int value); |
|
26 |
inline unsigned int left_data_array_top(void); |
|
27 |
inline unsigned int left_data_array_prev(void); |
|
28 |
inline unsigned int left_data_array_bottom(void); |
|
29 |
|
|
30 |
inline void right_data_array_put(unsigned short int value); |
|
31 |
inline unsigned int right_data_array_top(void); |
|
32 |
inline unsigned int right_data_array_prev(void); |
|
33 |
inline unsigned int right_data_array_bottom(void); |
|
34 |
|
|
35 |
//RING_BUFFER_NEW(enc_buffer, BUFFER_SIZE, short int); |
|
36 |
|
|
37 |
void encoder_recv_complete(void); |
|
38 |
|
|
39 |
void encoder_recv_complete(){ |
|
40 |
spi_transfer(5); |
|
41 |
} |
|
42 |
|
|
43 |
|
|
44 |
void encoders_init(void){ |
|
45 |
int i; |
|
46 |
|
|
47 |
spi_init(encoder_recv, encoder_recv_complete); |
|
48 |
buf_index = 0; |
|
49 |
left_data_buf = 0; |
|
50 |
right_data_buf= 0; |
|
51 |
left_data = -1; |
|
52 |
right_data = -1; |
|
53 |
//RING_BUFFER_INIT(enc_buffer,BUFFER_SIZE); |
|
54 |
left_data_idx = 0; |
|
55 |
right_data_idx = 0; |
|
56 |
for(i = 0; i < BUFFER_SIZE; i++) { |
|
57 |
left_data_array[i] = 0; |
|
58 |
} |
|
59 |
for(i = 0; i < BUFFER_SIZE; i++) { |
|
60 |
right_data_array[i] = 0; |
|
61 |
} |
|
62 |
spi_transfer(5); |
|
63 |
} |
|
64 |
|
|
65 |
int encoder_read(char encoder){ |
|
66 |
if(encoder==LEFT) |
|
67 |
return left_data; |
|
68 |
else if(encoder==RIGHT) |
|
69 |
return right_data; |
|
70 |
else return -1; |
|
71 |
} |
|
72 |
|
|
73 |
int encoder_change(char encoder){ |
|
74 |
return 0; |
|
75 |
} |
|
76 |
|
|
77 |
char encoder_direction(char encoder){ |
|
78 |
return 0; |
|
79 |
} |
|
80 |
|
|
81 |
int encoder_get_dx(char encoder) { |
|
82 |
if(encoder==LEFT) |
|
83 |
return left_dx; |
|
84 |
else if(encoder==RIGHT) |
|
85 |
return right_dx; |
|
86 |
else return -1; |
|
87 |
} |
|
88 |
|
|
89 |
void encoder_rst_dx(char encoder) { |
|
90 |
if(encoder==LEFT) |
|
91 |
left_dx = 0; |
|
92 |
else if(encoder==RIGHT) |
|
93 |
right_dx = 0; |
|
94 |
} |
|
95 |
|
|
96 |
int encoder_get_tc(void) { |
|
97 |
return timecount; |
|
98 |
} |
|
99 |
|
|
100 |
void encoder_rst_tc(void) { |
|
101 |
timecount = 0; |
|
102 |
} |
|
103 |
|
|
104 |
|
|
105 |
//Full reads occur every 40 microseconds. This function should be called |
|
106 |
//every 8 microseconds. |
|
107 |
void encoder_recv(char data){ |
|
108 |
//usb_puti(buf_index); |
|
109 |
short int dx; |
|
110 |
|
|
111 |
if(buf_index < 2) |
|
112 |
right_data_buf |= (((short)data) << ((1-buf_index)*8)) & (0xFF<<(1-buf_index)); |
|
113 |
|
|
114 |
else if (buf_index == 2) |
|
115 |
left_data_buf |= (((short)data) << 9) & (0x7F << 9); |
|
116 |
|
|
117 |
else if (buf_index == 3) |
|
118 |
left_data_buf |= (((short)data) << 1) & (0xFF<<1); |
|
119 |
|
|
120 |
else if (buf_index == 4) |
|
121 |
left_data_buf |= (((short)data)>>7) & 0x1; |
|
122 |
|
|
123 |
|
|
124 |
buf_index = (buf_index + 1) % 5; |
|
125 |
|
|
126 |
if(buf_index==0){ |
|
127 |
//if(left_data_buf & (OCF | COF | LIN)) |
|
128 |
// left_data = INVALID; |
|
129 |
|
|
130 |
/*else*/ if(((left_data_buf & MagINCn) > 0) && ((left_data_buf & MagDECn) > 0)) |
|
131 |
left_data = MAGNET_FAILURE; |
|
132 |
|
|
133 |
else |
|
134 |
left_data = (left_data_buf>>5) & 1023; |
|
135 |
|
|
136 |
if(right_data_buf & (OCF | COF | LIN)) |
|
137 |
right_data = INVALID; |
|
138 |
|
|
139 |
else if ( ((left_data_buf & MagINCn) > 0) && ((left_data_buf & MagDECn) > 0)) |
|
140 |
left_data = MAGNET_FAILURE; |
|
141 |
|
|
142 |
else right_data = (right_data_buf>>5) & 1023; |
|
143 |
|
|
144 |
usb_puts("[");usb_puti(left_data);usb_puts(",");usb_puti(right_data);usb_puts("]\r\n"); |
|
145 |
|
|
146 |
left_data_buf = 0; |
|
147 |
right_data_buf = 0; |
|
148 |
|
|
149 |
} |
|
150 |
|
|
151 |
|
|
152 |
if(left_data < INVALID) { |
|
153 |
//Put new data onto data array |
|
154 |
left_data_array_put(left_data); |
|
155 |
|
|
156 |
//Adjust left accumulator |
|
157 |
dx = left_data - left_data_array_prev(); |
|
158 |
if(dx > 5) { //underflow |
|
159 |
left_dx += dx - 1023; |
|
160 |
} |
|
161 |
else if(dx < -5) { //overflow |
|
162 |
left_dx += dx + 1023; |
|
163 |
} |
|
164 |
else { |
|
165 |
left_dx += dx; |
|
166 |
} |
|
167 |
} |
|
168 |
|
|
169 |
if(right_data < INVALID) { |
|
170 |
//Put new data onto data array |
|
171 |
right_data_array_put(right_data); |
|
172 |
|
|
173 |
//Adjust right accumulator |
|
174 |
dx = right_data - right_data_array_prev(); |
|
175 |
if(dx > 5) { //underflow |
|
176 |
right_dx += dx - 1023; |
|
177 |
} |
|
178 |
else if(dx < -5) { //overflow |
|
179 |
right_dx += dx + 1023; |
|
180 |
} |
|
181 |
else { |
|
182 |
right_dx += dx; |
|
183 |
} |
|
184 |
} |
|
185 |
|
|
186 |
//Increment timecount accumulator |
|
187 |
timecount++; |
|
188 |
} |
|
189 |
|
|
190 |
//Helper Functions |
|
191 |
inline void left_data_array_put(unsigned short int value) { |
|
192 |
if(left_data_idx == BUFFER_SIZE-1) |
|
193 |
left_data_idx = 0; |
|
194 |
else |
|
195 |
left_data_idx++; |
|
196 |
left_data_array[left_data_idx] = value; |
|
197 |
} |
|
198 |
|
|
199 |
inline unsigned int left_data_array_top(void) { |
|
200 |
return left_data_array[left_data_idx]; |
|
201 |
} |
|
202 |
|
|
203 |
inline unsigned int left_data_array_prev(void) { |
|
204 |
if(left_data_idx == 0) |
|
205 |
return left_data_array[BUFFER_SIZE-1]; |
|
206 |
else |
|
207 |
return left_data_array[left_data_idx - 1]; |
|
208 |
} |
|
209 |
|
|
210 |
inline unsigned int left_data_array_bottom(void) { |
|
211 |
if(left_data_idx == BUFFER_SIZE-1) |
|
212 |
return left_data_array[0]; |
|
213 |
else |
|
214 |
return left_data_array[left_data_idx + 1]; |
|
215 |
} |
|
216 |
|
|
217 |
inline void right_data_array_put(unsigned short int value) { |
|
218 |
if(right_data_idx == BUFFER_SIZE-1) |
|
219 |
right_data_idx = 0; |
|
220 |
else |
|
221 |
right_data_idx++; |
|
222 |
right_data_array[right_data_idx] = value; |
|
223 |
} |
|
224 |
|
|
225 |
inline unsigned int right_data_array_top(void) { |
|
226 |
return right_data_array[right_data_idx]; |
|
227 |
} |
|
228 |
|
|
229 |
inline unsigned int right_data_array_prev(void) { |
|
230 |
if(right_data_idx == 0) |
|
231 |
return right_data_array[BUFFER_SIZE-1]; |
|
232 |
else |
|
233 |
return right_data_array[right_data_idx - 1]; |
|
234 |
} |
|
235 |
|
|
236 |
inline unsigned int right_data_array_bottom(void) { |
|
237 |
if(right_data_idx == BUFFER_SIZE-1) |
|
238 |
return right_data_array[0]; |
|
239 |
else |
|
240 |
return right_data_array[right_data_idx + 1]; |
|
241 |
} |
Also available in: Unified diff