root / branches / encoders / code / lib / src / libdragonfly / encoders.c @ 752
History | View | Annotate | Download (5.68 KB)
1 |
#include "encoders.h" |
---|---|
2 |
#include "spi.h" |
3 |
#include <dragonfly_lib.h> |
4 |
#include "ring_buffer.h" |
5 |
|
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 |
// usb_puts("[");usb_puti(left_dx);usb_puts(",");usb_puti(right_dx);usb_puts("]\r\n");
|
41 |
// usb_puts("\r\n");
|
42 |
spi_transfer(5);
|
43 |
} |
44 |
|
45 |
void put_bin(char data){ |
46 |
int i;
|
47 |
|
48 |
for(i=7;i>=0;i--) |
49 |
usb_puti((data>>i)&1);
|
50 |
usb_puts(" ");
|
51 |
} |
52 |
|
53 |
void encoders_init(void){ |
54 |
int i;
|
55 |
|
56 |
spi_init(encoder_recv/*put_bin*/, encoder_recv_complete);
|
57 |
buf_index = 0;
|
58 |
left_data_buf = 0;
|
59 |
right_data_buf= 0;
|
60 |
left_data = -1;
|
61 |
right_data = -1;
|
62 |
//RING_BUFFER_INIT(enc_buffer,BUFFER_SIZE);
|
63 |
left_data_idx = 0;
|
64 |
right_data_idx = 0;
|
65 |
for(i = 0; i < BUFFER_SIZE; i++) { |
66 |
left_data_array[i] = 0;
|
67 |
} |
68 |
for(i = 0; i < BUFFER_SIZE; i++) { |
69 |
right_data_array[i] = 0;
|
70 |
} |
71 |
spi_transfer(5);
|
72 |
} |
73 |
|
74 |
int encoder_read(char encoder){ |
75 |
if(encoder==LEFT)
|
76 |
return left_data;
|
77 |
else if(encoder==RIGHT) |
78 |
return right_data;
|
79 |
else return -1; |
80 |
} |
81 |
|
82 |
int encoder_change(char encoder){ |
83 |
return 0; |
84 |
} |
85 |
|
86 |
char encoder_direction(char encoder){ |
87 |
return 0; |
88 |
} |
89 |
|
90 |
int encoder_get_dx(char encoder) { |
91 |
if(encoder==LEFT)
|
92 |
return left_dx;
|
93 |
else if(encoder==RIGHT) |
94 |
return right_dx;
|
95 |
else return -1; |
96 |
} |
97 |
|
98 |
void encoder_rst_dx(char encoder) { |
99 |
if(encoder==LEFT)
|
100 |
left_dx = 0;
|
101 |
else if(encoder==RIGHT) |
102 |
right_dx = 0;
|
103 |
} |
104 |
|
105 |
int encoder_get_tc(void) { |
106 |
return timecount;
|
107 |
} |
108 |
|
109 |
void encoder_rst_tc(void) { |
110 |
timecount = 0;
|
111 |
} |
112 |
|
113 |
|
114 |
//Full reads occur every 40 microseconds. This function should be called
|
115 |
//every 8 microseconds.
|
116 |
void encoder_recv(char data){ |
117 |
//usb_puti(buf_index);
|
118 |
short int dx; |
119 |
|
120 |
if(buf_index == 0) |
121 |
right_data_buf |= ((short)data)<<8 & 0xff00; |
122 |
|
123 |
else if (buf_index == 1) |
124 |
right_data_buf |= ((short)data) & 0xff; |
125 |
|
126 |
else if (buf_index == 2) |
127 |
left_data_buf |= (((short)data) << 9) & (0x7F << 9); |
128 |
|
129 |
else if (buf_index == 3) |
130 |
left_data_buf |= (((short)data) << 1) & (0xFF<<1); |
131 |
|
132 |
else if (buf_index == 4) |
133 |
left_data_buf |= (((short)data)>>7) & 0x1; |
134 |
|
135 |
|
136 |
buf_index = (buf_index + 1) % 5; |
137 |
|
138 |
if(buf_index==0) { |
139 |
if(left_data_buf & (COF | LIN) || !(left_data_buf & OCF)){
|
140 |
left_data = INVALID; |
141 |
} |
142 |
|
143 |
else if(((left_data_buf & MagINCn) > 0) && ((left_data_buf & MagDECn) > 0)){ |
144 |
left_data = MAGNET_FAILURE; |
145 |
} |
146 |
else{
|
147 |
left_data = (left_data_buf>>5) & 1023; |
148 |
} |
149 |
|
150 |
if(right_data_buf & (COF | LIN) || !(right_data_buf & OCF)){
|
151 |
right_data = INVALID; |
152 |
} |
153 |
else if ( ((left_data_buf & MagINCn) > 0) && ((left_data_buf & MagDECn) > 0)){ |
154 |
left_data = MAGNET_FAILURE; |
155 |
} |
156 |
else{
|
157 |
right_data = (right_data_buf>>5) & 1023; |
158 |
} |
159 |
|
160 |
|
161 |
//if(left_data!=INVALID || right_data!=INVALID){
|
162 |
// usb_puts("{");usb_puti(left_data);usb_puts(",");usb_puti(right_data);usb_puts("}\r\n");
|
163 |
//}
|
164 |
|
165 |
left_data_buf = 0;
|
166 |
right_data_buf = 0;
|
167 |
|
168 |
|
169 |
if(left_data < INVALID) {
|
170 |
//Put new data onto data array
|
171 |
left_data_array_put(left_data); |
172 |
|
173 |
//Adjust left accumulator
|
174 |
//dx = left_data - left_data_array_prev();
|
175 |
dx = left_data_array_prev() - left_data; |
176 |
|
177 |
/*left dx was negative on robot 7, could just be that
|
178 |
the encoder is backwards, so this may need to change
|
179 |
back
|
180 |
*/
|
181 |
|
182 |
|
183 |
|
184 |
if(left_data_array_prev()==0) |
185 |
dx=0;
|
186 |
|
187 |
if(dx > 5) { //underflow |
188 |
left_dx += dx - 1023;
|
189 |
} |
190 |
else if(dx < -5) { //overflow |
191 |
left_dx += dx + 1023;
|
192 |
} |
193 |
else {
|
194 |
left_dx += dx; |
195 |
} |
196 |
} |
197 |
|
198 |
if(right_data < INVALID) {
|
199 |
//Put new data onto data array
|
200 |
right_data_array_put(right_data); |
201 |
|
202 |
//Adjust right accumulator
|
203 |
dx = right_data - right_data_array_prev(); |
204 |
|
205 |
if(right_data_array_prev()==0) |
206 |
dx=0;
|
207 |
|
208 |
if(dx > 5) { //underflow |
209 |
right_dx += dx - 1023;
|
210 |
} |
211 |
else if(dx < -5) { //overflow |
212 |
right_dx += dx + 1023;
|
213 |
} |
214 |
else {
|
215 |
right_dx += dx; |
216 |
} |
217 |
} |
218 |
} |
219 |
|
220 |
//Increment timecount accumulator
|
221 |
timecount++; |
222 |
} |
223 |
|
224 |
//Helper Functions
|
225 |
inline void left_data_array_put(unsigned short int value) { |
226 |
if(left_data_idx == BUFFER_SIZE-1) |
227 |
left_data_idx = 0;
|
228 |
else
|
229 |
left_data_idx++; |
230 |
left_data_array[left_data_idx] = value; |
231 |
} |
232 |
|
233 |
inline unsigned int left_data_array_top(void) { |
234 |
return left_data_array[left_data_idx];
|
235 |
} |
236 |
|
237 |
inline unsigned int left_data_array_prev(void) { |
238 |
if(left_data_idx == 0) |
239 |
return left_data_array[BUFFER_SIZE-1]; |
240 |
else
|
241 |
return left_data_array[left_data_idx - 1]; |
242 |
} |
243 |
|
244 |
inline unsigned int left_data_array_bottom(void) { |
245 |
if(left_data_idx == BUFFER_SIZE-1) |
246 |
return left_data_array[0]; |
247 |
else
|
248 |
return left_data_array[left_data_idx + 1]; |
249 |
} |
250 |
|
251 |
inline void right_data_array_put(unsigned short int value) { |
252 |
if(right_data_idx == BUFFER_SIZE-1) |
253 |
right_data_idx = 0;
|
254 |
else
|
255 |
right_data_idx++; |
256 |
right_data_array[right_data_idx] = value; |
257 |
} |
258 |
|
259 |
inline unsigned int right_data_array_top(void) { |
260 |
return right_data_array[right_data_idx];
|
261 |
} |
262 |
|
263 |
inline unsigned int right_data_array_prev(void) { |
264 |
if(right_data_idx == 0) |
265 |
return right_data_array[BUFFER_SIZE-1]; |
266 |
else
|
267 |
return right_data_array[right_data_idx - 1]; |
268 |
} |
269 |
|
270 |
inline unsigned int right_data_array_bottom(void) { |
271 |
if(right_data_idx == BUFFER_SIZE-1) |
272 |
return right_data_array[0]; |
273 |
else
|
274 |
return right_data_array[right_data_idx + 1]; |
275 |
} |