/
mIMUtest.c
335 lines (300 loc) · 6.19 KB
/
mIMUtest.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
/*
* mIMUtest.c
*
* Created: 2/2/2013 3:29:45 PM
* Author: Nick
*/
#include <avr/io.h>
#include "m_imu.h"
#include "m_usb.h"
#include "m_bus.h"
#include "m_general.h"
void selectIMU(int select);
void calibrate(int select);
void measure(int select);
unsigned long overflow = 0;
unsigned long micros;
int raw_data_buffer[3];
int ax, ay, az, gx, gy, gz, mx, my, mz;
int i = 0;
int j = 0;
int sel;
int bar0[6];
int bar1[6];
int bar2[6];
int bar3[6];
int bar4[6];
int bar5[6];
ISR(TIMER1_COMPB_vect){
set(TIFR1,OCF1B);
OCR1B += 521;
}
ISR(TIMER1_OVF_vect){
set(TIFR1,TOV1);
TCNT1 = 0;
}
ISR(TIMER3_OVF_vect){
set(TIFR3,TOV3);
//sel = overflow % 2;
measure(0);
overflow++;
}
void selectIMU(int select){
switch(select){
case 0:
clear(PORTF,0);
clear(PORTF,1);
clear(PORTB,1);
break;
case 1:
set(PORTF,0);
clear(PORTF,1);
clear(PORTB,1);
break;
case 2:
clear(PORTF,0);
set(PORTF,1);
clear(PORTB,1);
break;
case 3:
set(PORTF,0);
set(PORTF,1);
clear(PORTB,1);
break;
case 4:
clear(PORTF,0);
clear(PORTF,1);
set(PORTB,1);
break;
case 5:
set(PORTF,0);
clear(PORTF,1);
set(PORTB,1);
break;
}
}
void calibrate(int select){
long axbar = 0;
long aybar = 0;
long azbar = 0;
long gxbar = 0;
long gybar = 0;
long gzbar = 0;
int bar[6];
selectIMU(select);
for(i = 0; i < 100; i++){
m_wait(10);
m_green(TOGGLE);
toggle(PORTB,2);
/*
m_imu_accel(raw_data_buffer);
axbar = axbar + (long)raw_data_buffer[0];
aybar = aybar + (long)raw_data_buffer[1];
azbar = azbar + (long)raw_data_buffer[2];
*/
m_imu_gyro(raw_data_buffer);
gxbar = gxbar + (long)raw_data_buffer[0];
gybar = gybar + (long)raw_data_buffer[1];
gzbar = gzbar + (long)raw_data_buffer[2];
}
bar[0] = (int)axbar/i;
bar[1] = (int)aybar/i;
bar[2] = (int)azbar/i;
bar[3] = (int)gxbar/i;
bar[4] = (int)gybar/i;
bar[5] = (int)gzbar/i;
switch(select){
case 0:
for(i = 0; i < 6; i++){
bar0[i] = bar[i];
}
break;
case 1:
for(i = 0; i < 6; i++){
bar1[i] = bar[i];
}
break;
case 2:
for(i = 0; i < 6; i++){
bar2[i] = bar[i];
}
break;
case 3:
for(i = 0; i < 6; i++){
bar3[i] = bar[i];
}
break;
case 4:
for(i = 0; i < 6; i++){
bar4[i] = bar[i];
}
break;
case 5:
for(i = 0; i < 6; i++){
bar5[i] = bar[i];
}
break;
}
}
void measure(int select){
//m_green(TOGGLE);
toggle(PORTB,2);
int* bar;
selectIMU(select);
switch(select){
case 0:
bar = bar0;
break;
case 1:
bar = bar1;
break;
case 2:
bar = bar2;
break;
case 3:
bar = bar3;
break;
case 4:
bar = bar4;
break;
case 5:
bar = bar5;
break;
}
//m_usb_tx_int(select);
//m_usb_tx_string("\t");
micros = 4096 * overflow + (unsigned long)((float)(((unsigned long)(TCNT3H) << 8) | TCNT3L) * 4096 / 65536);
m_usb_tx_ulong(micros);
m_usb_tx_string("\t");
m_imu_accel(raw_data_buffer);
ax = raw_data_buffer[0] - bar[0];
ay = raw_data_buffer[1] - bar[1];
az = raw_data_buffer[2] - bar[2];
m_usb_tx_int(ax);
m_usb_tx_string("\t");
m_usb_tx_int(ay);
m_usb_tx_string("\t");
m_usb_tx_int(az);
m_usb_tx_string("\t");
m_imu_gyro(raw_data_buffer);
gx = raw_data_buffer[0] - bar[3];
gy = raw_data_buffer[1] - bar[4];
gz = raw_data_buffer[2] - bar[5];
m_usb_tx_int(gx);
m_usb_tx_string("\t");
m_usb_tx_int(gy);
m_usb_tx_string("\t");
m_usb_tx_int(gz);
m_usb_tx_string("\t");
m_imu_mag(raw_data_buffer);
mx = raw_data_buffer[0];
my = raw_data_buffer[2];
mz = raw_data_buffer[1];
m_usb_tx_int(mx);
m_usb_tx_string("\t");
m_usb_tx_int(my);
m_usb_tx_string("\t");
m_usb_tx_int(mz);
m_usb_tx_string("\n");
}
int main(void)
{
unsigned long runtime = 0;
m_clockdivide(0); // 16 MHz
OCR1B = 521; // initialize output compare register (interrupt every 521 cycles)
set(DDRB,6); // set B6 as output (output compare pin)
set(DDRB,4); // set B4 as output (green LED for data synchronization)
set(PORTB,4); // turn on green LED on B4
set(DDRF,0); // set F0 as output
set(DDRF,1); // set F1 as output
set(DDRB,1); // set B1 as output
m_usb_init();
while(!m_usb_isconnected()){
m_green(ON);
}
m_green(OFF);
set(DDRB,2);
while(!m_usb_rx_available()); // wait for runtime argument from Python script
m_wait(5);
while(m_usb_rx_available()){
runtime = runtime * 10 + (m_usb_rx_char() - '0'); // build number of seconds
}
runtime = runtime * 1000000; // convert to microseconds
clear(PORTF,0); // S0
clear(PORTF,1); // S1
clear(PORTB,1); // S2
m_wait(10);
if(!m_imu_init(0,1)){
m_red(ON); // RED LED turns on if there's a problem
m_usb_tx_string("IMU0 could not connect");
}
/*
set(PORTF,0);
m_wait(10);
if(!m_imu_init(0,1)){
m_red(ON); // RED LED turns on if there's a problem
m_usb_tx_string("IMU1 could not connect");
}
clear(PORTF,0);
set(PORTF,1);
m_wait(10);
if(!m_imu_init(0,1)){
m_red(ON); // RED LED turns on if there's a problem
m_usb_tx_string("IMU2 could not connect");
}
set(PORTF,0);
m_wait(10);
if(!m_imu_init(0,1)){
m_red(ON); // RED LED turns on if there's a problem
m_usb_tx_string("IMU3 could not connect");
}
set(PORTB,1);
clear(PORTF,0);
clear(PORTF,1);
m_wait(10);
if(!m_imu_init(0,1)){
m_red(ON); // RED LED turns on if there's a problem
m_usb_tx_string("IMU4 could not connect");
}
set(PORTF,0);
m_wait(10);
if(!m_imu_init(0,1)){
m_red(ON); // RED LED turns on if there's a problem
m_usb_tx_string("IMU5 could not connect");
}
*/
//calibrate(0);
//calibrate(1);
//calibrate(2);
//calibrate(3);
//calibrate(4);
//calibrate(5);
// Initialize Timer 1 output compare interrupt
set(TIMSK1,OCIE1B);
set(TCCR1B,CS12);
clear(TCCR1B,CS11);
set(TCCR1B,CS10);
clear(TCCR1B,WGM13);
set(TCCR1B,WGM12);
clear(TCCR1A,WGM11);
clear(TCCR1A,WGM10);
clear(TCCR1A,COM1B1);
set(TCCR1A,COM1B0);
// Initialize Timer 1 overflow interrupt
set(TIMSK1,TOIE1);
// Initialize Timer 3 overflow interrupt
overflow = 0;
set(TIMSK3,TOIE3);
clear(TCCR3B,CS32);
clear(TCCR3B,CS31);
set(TCCR3B,CS30);
sei(); // enable global interrupts
clear(PORTB,4); // turn off green LED --> signal to camera
while(overflow * 4096 < runtime){
m_green(TOGGLE);
}
m_usb_tx_string(" ");
m_green(OFF);
//cli();
while(1);
}