-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathFreeBoardIMU.ino
595 lines (507 loc) · 18.4 KB
/
FreeBoardIMU.ino
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
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
/**
* FreeIMU library serial communication protocol
*
*/
#include <ADXL345.h>
#include <bma180.h>
#include <HMC58X3.h>
#include <ITG3200.h>
#include <MS561101BA.h>
#include <I2Cdev.h>
#include <MPU60X0.h>
#include <EEPROM.h>
#include <FlexiTimer2.h>
#include <AverageList.h>
#include <PString.h>
//#define DEBUG
#include "DebugUtils.h"
#include "CommunicationUtils.h"
#include "FreeIMU.h"
#include <Wire.h>
#include <SPI.h>
/**mag declination*/
#define DECL "DEC"
#define RAW_OUT "#RAW"
#define IMU_OUT "#IMU"
#define CAL_OUT "CAL"
#define CAL_SAVE "#CAL"
#define NMEA_ON "#NMX"
#define FREEBOARD_ON "#FBX"
#define NMEA_OFF "#NMO"
#define FREEBOARD_OFF "#FBO"
volatile boolean execute = false;
volatile int interval = 0;
float q[4];
float qm[7];
//float m[3];
int raw_values[11];
float ypr[3]; // yaw pitch roll
float yprm[4]; // yaw, pitch, roll, mag heading
char str[256];
float val[9];
float declination=0.0;
bool raw_out=false;
bool nmea_out=false;
bool freeboard_out=true;
String inputSerial = ""; // a string to hold incoming data
typedef volatile float rval; //change float to the datatype you want to use
const byte MAX_NUMBER_OF_READINGS = 5;
rval mghStorage[MAX_NUMBER_OF_READINGS] = {0.0};
AverageList<rval> mghList = AverageList<rval>( mghStorage, MAX_NUMBER_OF_READINGS );
// rate of turn average
rval rotStorage[MAX_NUMBER_OF_READINGS] = {0.0};
AverageList<rval> rotList = AverageList<rval>( rotStorage, MAX_NUMBER_OF_READINGS);
// Set the FreeIMU object
FreeIMU my3IMU = FreeIMU();
//The command from the PC
char cmd;
/*
* Timer interrupt driven method to do time sensitive calculations
* The calc flag causes the main loop to execute other less sensitive calls.
*/
void calculate() {
//we create 100ms pings here
execute = true;
//we record the ping count out to 2 secs
interval++;
interval = interval % 20;
}
void setup() {
inputSerial.reserve(40);
Serial.begin(38400);
//Serial.begin(115200);
Wire.begin();
my3IMU.init(true);
//setup timers
FlexiTimer2::set(100, calculate); // 100ms period
FlexiTimer2::start();
// LED
pinMode(13, OUTPUT);
mghList.reset();
}
/*
SerialEvent occurs whenever a new data comes in the
hardware serial RX. This routine is run between each
time loop() runs, so using delay inside loop can delay
response. Multiple bytes of data may be available.
*/
void serialEvent() {
while (Serial.available()) {
// get the new byte:
char inChar = (char) Serial.read();
// add it to the inputString:
inputSerial += inChar;
if (inChar == '\n') {
//inputSerialComplete = true;
char carray[inputSerial.length() + 1]; //determine size of the array
inputSerial.toCharArray(carray, sizeof(carray));
process(carray, ',');
inputSerial = "";
//inputSerialComplete = false;
}
}
}
void loop() {
if(raw_out){
outputRaw();
}else if (execute) {
//do these every 100ms
//quarternary, updates AHRS
my3IMU.getQ(q);
if (interval % 2 == 0) {
// do every 200ms
//mag
my3IMU.getValues(val);
//convert to YPR
my3IMU.getYawPitchRollRad(yprm);
//convert to magnetic heading
calcMagHeading();
updateROT();
mghList.addValue(yprm[3]);
}
if (interval % 5 == 0) {
//do every 500ms
float h = degrees(mghList.getTotalAverage());
if(h<0.0){
h=(360.0+h);
}
//ArduIMU output format
printIMU(h);
//now do the NMEA version
if(nmea_out){
printNmeaMag(h);
printRateOfTurn();
printPitchRoll();
}
}
}
execute = false;
}
void outputRaw(){
#if HAS_ITG3200()
my3IMU.acc.readAccel(&raw_values[0], &raw_values[1], &raw_values[2]);
//my3IMU.gyro.readGyroRaw(&raw_values[3], &raw_values[4], &raw_values[5]);
#else // MPU6050
my3IMU.accgyro.getMotion6(&raw_values[0], &raw_values[1], &raw_values[2], &raw_values[3], &raw_values[4], &raw_values[5]);
#endif
Serial.print(raw_values[0]);
Serial.print(",");
Serial.print(raw_values[1]);
Serial.print(",");
Serial.print(raw_values[2]);
Serial.print(",");
#if IS_9DOM()
my3IMU.magn.getValues(&raw_values[0], &raw_values[1], &raw_values[2]);
Serial.print(raw_values[0]);
Serial.print(",");
Serial.print(raw_values[1]);
Serial.print(",");
Serial.print(raw_values[2]);
#endif
Serial.println();
//}
}
void process(char * s, char parser) {
//if (DEBUG) Serial.print("Process str:");
//if (DEBUG) Serial.println(s);
char *cmd = strtok(s, ",");
while (cmd != NULL && strlen(cmd) > 3) {
//starts with # its a command
//if (DEBUG) Serial.print("Process incoming..l=");
//if (DEBUG) Serial.print(strlen(cmd));
//if (DEBUG) Serial.print(", ");
//if (DEBUG) Serial.println(cmd);
char key[5];
int l = strlen(cmd);
if (cmd[0] == '#') {
//
strncpy(key, cmd, 4);
key[4] = '\0';
char valArray[l - 4];
memcpy(valArray, &cmd[5], l - 5);
valArray[l - 5] = '\0';
//if (DEBUG) Serial.print(key);
//if (DEBUG) Serial.print(" = ");
//if (DEBUG) Serial.println(valArray);
//used for calibration
if (strcmp(key, RAW_OUT) == 0) {
raw_out=true;
}
if (strcmp(key, IMU_OUT) == 0) {
raw_out=false;
}
if (strcmp(key, FREEBOARD_ON) == 0) {
freeboard_out=true;
}
if (strcmp(key, FREEBOARD_OFF) == 0) {
freeboard_out=false;
}
if (strcmp(key, NMEA_ON) == 0) {
nmea_out=true;
}
if (strcmp(key, NMEA_OFF) == 0) {
nmea_out=false;
}
//save calibration
if (strcmp(key, CAL_SAVE) == 0) {
const uint8_t eepromsize = sizeof(float) * 6 + sizeof(int) * 6;
while(Serial.available() < eepromsize){
Serial.print(".") ; // wait until all calibration data are received
}
EEPROM.write(FREEIMU_EEPROM_BASE, FREEIMU_EEPROM_SIGNATURE);
for(uint8_t i = 1; i<(eepromsize + 1); i++) {
EEPROM.write(FREEIMU_EEPROM_BASE + i, (char) Serial.read());
}
//my3IMU.calLoad(); // reload calibration
// toggle LED after calibration store.
digitalWrite(13, HIGH);
delay(1000);
digitalWrite(13, LOW);
Serial.println("Saved calibration to EEPROM");
}
} else {
strncpy(key, cmd, 3);
key[3] = '\0';
char valArray[l - 3];
memcpy(valArray, &cmd[4], l - 4);
valArray[l - 4] = '\0';
//if (DEBUG) Serial.print(key);
//if (DEBUG) Serial.print(" = ");
//if (DEBUG) Serial.println(valArray);
if (strcmp(key, DECL) == 0) {
declination= atof(valArray);
}
//get calibration
if(strcmp(key, CAL_OUT) == 0){
//my3IMU.magn.calibrate(1,75);
Serial.print("acc offset: ");
Serial.print(my3IMU.acc_off_x);
Serial.print(",");
Serial.print(my3IMU.acc_off_y);
Serial.print(",");
Serial.print(my3IMU.acc_off_z);
Serial.print("\n");
Serial.print("magn offset: ");
Serial.print(my3IMU.magn_off_x);
Serial.print(",");
Serial.print(my3IMU.magn_off_y);
Serial.print(",");
Serial.print(my3IMU.magn_off_z);
Serial.print("\n");
Serial.print("acc scale: ");
Serial.print(my3IMU.acc_scale_x);
Serial.print(",");
Serial.print(my3IMU.acc_scale_y);
Serial.print(",");
Serial.print(my3IMU.acc_scale_z);
Serial.print("\n");
Serial.print("magn scale: ");
Serial.print(my3IMU.magn_scale_x);
Serial.print(",");
Serial.print(my3IMU.magn_scale_y);
Serial.print(",");
Serial.print(my3IMU.magn_scale_z);
Serial.print("\n");
}
}
//next token
cmd = strtok(NULL, ",");
}
//if (DEBUG) Serial.println("Process str exit");
}
void printIMU(float h){
//!!VER:1.9,RLL:-0.52,PCH:0.06,YAW:80.24,IMUH:253,MGX:44,MGY:-254,MGZ:-257,MGH:80.11,LAT:-412937350,LON:1732472000,ALT:14,COG:116,SOG:0,FIX:1,SAT:5,TOW:22504700***
if(freeboard_out){
Serial.print("!!VER:1.9,UID:IMU,");
Serial.print("MGH:");
Serial.print(h);
Serial.print(",YAW:");
Serial.print(degrees(yprm[0]));
Serial.print(",PCH:");
Serial.print(degrees(yprm[1]));
Serial.print(",RLL:");
Serial.print(degrees(yprm[2]));
Serial.println(",");
}
}
/*
1 2 3
| | |
$--ROT,x.x,A*hh<CR><LF>
Field Number:
Rate Of Turn, degrees per minute, "-" means bow turns to port
Status, A means data is valid
Checksum
*/
void printRateOfTurn(){
char rotSentence [25];
PString str(rotSentence, sizeof(rotSentence));
str.print("$TIROT,");
float r =rotList.getTotalAverage();
char rBuf[7];
dtostrf(r, 3, 1, rBuf);
str.print(rBuf); //prints 1 decimal, but round to degree, should be good enough
str.print(",A*");
//calculate the checksum
int cs = 0; //clear any old checksum
for (unsigned int n = 1; n < strlen(rotSentence) - 1; n++) {
cs ^= rotSentence[n]; //calculates the checksum
}
//bug - arduino prints 0x007 as 7, 0x02B as 2B, so we add it now
if (cs < 0x10) str.print('0');
str.print(cs, HEX); // Assemble the final message and send it out the serial port
Serial.println(rotSentence);
}
/*
Print out the NMEA string for mag heading
$HC = mag compass
=== HDM - Heading - Magnetic ===
Vessel heading in degrees with respect to magnetic north produced by
any device or system producing magnetic heading.
------------------------------------------------------------------------------
1 2 3
| | |
$--HDM,x.x,M*hh<CR><LF>
------------------------------------------------------------------------------
Field Number:
1. Heading Degrees, magnetic
2. M = magnetic
3. Checksum
*/
void printNmeaMag(float h){
//Assemble a sentence of the various parts so that we can calculate the proper checksum
char magSentence [25];
PString str(magSentence, sizeof(magSentence));
str.print("$HCHDM,");
str.print((int)h); //prints 1 decimal, but round to degree, should be good enough
str.print(".0,M*");
//calculate the checksum
int cs = 0; //clear any old checksum
for (unsigned int n = 1; n < strlen(magSentence) - 1; n++) {
cs ^= magSentence[n]; //calculates the checksum
}
//bug - arduino prints 0x007 as 7, 0x02B as 2B, so we add it now
if (cs < 0x10) str.print('0');
str.print(cs, HEX); // Assemble the final message and send it out the serial port
Serial.println(magSentence);
//do HDT if we have a declination
if(declination!=0.0){
//print HDT
char hdtSentence [45];
PString str(hdtSentence, sizeof(hdtSentence));
str.print("$HCHDT,");
float d = h-declination;
char dBuf[7];
dtostrf(d, 3, 1, dBuf);
str.print(dBuf);
str.print(",T*");
//calculate the checksum
int cs = 0; //clear any old checksum
for (unsigned int n = 1; n < strlen(hdtSentence) - 1; n++) {
cs ^= hdtSentence[n]; //calculates the checksum
}
//bug - arduino prints 0x007 as 7, 0x02B as 2B, so we add it now
if (cs < 0x10) str.print('0');
str.print(cs, HEX); // Assemble the final message and send it out the serial port
Serial.println(hdtSentence);
//and do HDG (Heading, Deviation and Variation)
/*
------------------------------------------------------------------------------
1 2 3 4 5 6
| | | | | |
$--HDG,x.x,x.x,a,x.x,a*hh<CR><LF>
------------------------------------------------------------------------------
Field Number:
1. Magnetic Sensor heading in degrees
2. Magnetic Deviation, degrees
3. Magnetic Deviation direction, E = Easterly, W = Westerly, direction of magnetic north from true north.
4. Magnetic Variation degrees
5. Magnetic Variation direction, E = Easterly, W = Westerly
6. Checksum
*/
char hdgSentence [35];
float x = (abs(declination));
char xBuf[7];
dtostrf(x, 3, 1, xBuf);
//x=((int)(x*10))*0.1;
PString hdgStr(hdgSentence, sizeof(hdgSentence));
hdgStr.print("$HCHDG,");
hdgStr.print(h);//mag heading
hdgStr.print(",");
hdgStr.print(xBuf);//declination
if(declination<0.0){
hdgStr.print(",E,");
}else{
hdgStr.print(",W,");
}
hdgStr.print(xBuf);//declination
if(declination<0.0){
hdgStr.print(",E*");
}else{
hdgStr.print(",W*");
}
//calculate the checksum
cs = 0; //clear any old checksum
for (unsigned int n = 1; n < strlen(hdgSentence) - 1; n++) {
cs ^= hdgSentence[n]; //calculates the checksum
}
//bug - arduino prints 0x007 as 7, 0x02B as 2B, so we add it now
if (cs < 0x10) str.print('0');
hdgStr.print(cs, HEX); // Assemble the final message and send it out the serial port
Serial.println(hdgSentence);
}
}
/*
$YXXDR (Transducer Measurements: Vessel Attitude) Used for various secondary transducers?!
or $PTNTHPR (Heading, Pitch, Roll) Looks like a new specific attitude one from the digital camera world as apparently EXIF tags also contain NMEA strings.
$YXXDR,<1>, <2>, <3>, <4>,<5>, <6>, <7>, <8>,<9>, <10>,<11>,<12>,<13>,<14>,<15>,<16>*hh<CR><LF>
eg $YXXDR,A,5.2,D,PTCH,A,7.4,D,ROLL,,,,,,,,*hh<CR><LF>
The fields in the B version of the XDR sentence are as follows:
<1>A = angular displacement
<2>Pitch: oscillation of vessel about its latitudinal axis. Bow moving up ispositive. Value reported to the nearest 0.1 degree.
<3>D = degrees
<4>PTCH (ID indicating pitch of vessel)
<5>A = angular displacement
<6>Roll: oscillation of vessel about its longitudinal axis. Roll to the starboard is positive. Value reported to the nearest 0.1 degree.
<7>D = degrees
<8>ROLL (ID indicating roll of vessel)
<9>+Not used
*/
void printPitchRoll(){
char xdrSentence [65];
PString str(xdrSentence, sizeof(xdrSentence));
str.print("$YXXDR,A,");
//pitch
char pBuf[7];
dtostrf(degrees(yprm[1]), 3, 1, pBuf);
str.print(pBuf); //prints 1 decimal, but round to degree, should be good enough
str.print(",D,PTCH,A,");
//roll
char rBuf[7];
dtostrf(degrees(yprm[2]), 3, 1, rBuf);
str.print(rBuf);
str.print(",D,ROLL,A,");
//yaw
char yBuf[7];
dtostrf(degrees(yprm[3]), 3, 1, yBuf);
str.print(yBuf);
str.print(",D,YAW*");
//calculate the checksum
int cs = 0; //clear any old checksum
for (unsigned int n = 1; n < strlen(xdrSentence) - 1; n++) {
cs ^= xdrSentence[n]; //calculates the checksum
}
//bug - arduino prints 0x007 as 7, 0x02B as 2B, so we add it now
if (cs < 0x10) str.print('0');
str.print(cs, HEX); // Assemble the final message and send it out the serial port
Serial.println(xdrSentence);
}
void updateROT(){
//executed every 0.2 secs
//value is deg/s
float rot=(val[5]*0.06097);
rotList.addValue(-degrees(rot));
}
void calcMagHeading(){
float Head_X;
float Head_Y;
float cos_roll;
float sin_roll;
float cos_pitch;
float sin_pitch;
cos_roll = cos(-yprm[2]);
sin_roll = sin(-yprm[2]);
cos_pitch = cos(yprm[1]);
sin_pitch = sin(yprm[1]);
//Example calc
//Xh = bx * cos(theta) + by * sin(phi) * sin(theta) + bz * cos(phi) * sin(theta)
//Yh = by * cos(phi) - bz * sin(phi)
//return wrap((atan2(-Yh, Xh) + variation))
// Tilt compensated Magnetic field X component:
Head_X = val[6]*cos_pitch+val[7]*sin_roll*sin_pitch+val[8]*cos_roll*sin_pitch;
// Tilt compensated Magnetic field Y component:
Head_Y = val[7]*cos_roll-val[8]*sin_roll;
// Magnetic Heading
yprm[3] = atan2(-Head_Y,-Head_X);
}
char serial_busy_wait() {
while(!Serial.available()) {
; // do nothing until ready
}
return Serial.read();
}
const int EEPROM_MIN_ADDR = 0;
const int EEPROM_MAX_ADDR = 511;
void eeprom_serial_dump_column() {
// counter
int i;
// byte read from eeprom
byte b;
// buffer used by sprintf
char buf[10];
for (i = EEPROM_MIN_ADDR; i <= EEPROM_MAX_ADDR; i++) {
b = EEPROM.read(i);
sprintf(buf, "%03X: %02X", i, b);
Serial.println(buf);
}
}