forked from AeroQuad/AeroQuad
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Accel.h
580 lines (491 loc) · 18.9 KB
/
Accel.h
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
/*
AeroQuad v2.3 - March 2011
www.AeroQuad.com
Copyright (c) 2011 Ted Carancho. All rights reserved.
An Open Source Arduino based multicopter.
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
class Accel {
public:
float accelScaleFactor;
float smoothFactor;
float rawAltitude;
int accelChannel[3];
#if defined(AeroQuadMega_CHR6DM) || defined(APM_OP_CHR6DM)
float accelZero[3];
#else
int accelZero[3];
#endif
float accelData[3];
int accelADC[3];
int sign[3];
float accelOneG, zAxis;
byte rollChannel, pitchChannel, zAxisChannel;
Accel(void) {
sign[ROLL] = 1;
sign[PITCH] = 1;
sign[YAW] = 1;
zAxis = 0;
}
// ******************************************************************
// The following function calls must be defined in any new subclasses
// ******************************************************************
virtual void initialize(void) {
this->_initialize(rollChannel, pitchChannel, zAxisChannel);
}
virtual void measure(void);
virtual void calibrate(void);
virtual const int getFlightData(byte);
// **************************************************************
// The following functions are common between all Gyro subclasses
// **************************************************************
void _initialize(byte rollChannel, byte pitchChannel, byte zAxisChannel) {
accelChannel[ROLL] = rollChannel;
accelChannel[PITCH] = pitchChannel;
accelChannel[ZAXIS] = zAxisChannel;
accelOneG = readFloat(ACCEL1G_ADR);
accelZero[XAXIS] = readFloat(LEVELPITCHCAL_ADR);
accelZero[YAXIS] = readFloat(LEVELROLLCAL_ADR);
accelZero[ZAXIS] = readFloat(LEVELZCAL_ADR);
}
// return the raw ADC value from the accel, with sign change if need, not smoothed or scaled to SI units
const int getRaw(byte axis) {
return accelADC[axis] * sign[axis];
}
// return the smoothed and scaled to SI units value of the accel with sign change if needed
const float getData(byte axis) {
return accelData[axis] * sign[axis];
}
// invert the sign for a specifica accel axis
const int invert(byte axis) {
sign[axis] = -sign[axis];
return sign[axis];
}
const int getZero(byte axis) {
return accelZero[axis];
}
void setZero(byte axis, int value) {
accelZero[axis] = value;
}
// returns the SI scale factor
const float getScaleFactor(void) {
return accelScaleFactor;
}
// returns the smoothfactor
const float getSmoothFactor() {
return smoothFactor;
}
void setSmoothFactor(float value) {
smoothFactor = value;
}
void setOneG(float value) {
accelOneG = value;
}
const float getOneG(void) {
return accelOneG;
}
const float getZaxis() {
return accelOneG - getData(ZAXIS);
}
};
/******************************************************/
/************ AeroQuad v1 Accelerometer ***************/
/******************************************************/
#if defined(AeroQuad_v1) || defined(AeroQuad_v1_IDG) || defined(AeroQuadMega_v1)
class Accel_AeroQuad_v1 : public Accel {
private:
public:
Accel_AeroQuad_v1() : Accel(){
}
void initialize(void) {
// rollChannel = 1
// pitchChannel = 0
// zAxisChannel = 2
this->_initialize(1, 0, 2);
smoothFactor = readFloat(ACCSMOOTH_ADR);
accelScaleFactor = G_2_MPS2((aref/1024.0) / 0.300);
//accelScaleFactor = G_2_MPS2(((aref*1000)/1024)/(aref*100));
}
void measure(void) {
accelADC[XAXIS] = analogRead(accelChannel[PITCH]) - accelZero[PITCH];
accelADC[YAXIS] = analogRead(accelChannel[ROLL]) - accelZero[ROLL];
accelADC[ZAXIS] = accelZero[ZAXIS] - analogRead(accelChannel[ZAXIS]);
for (byte axis = XAXIS; axis < LASTAXIS; axis++) {
accelData[axis] = filterSmooth(accelADC[axis] * accelScaleFactor, accelData[axis], smoothFactor);
}
}
const int getFlightData(byte axis) {
return getRaw(axis);
}
// Allows user to zero accelerometers on command
void calibrate(void) {
int findZero[FINDZERO];
for (byte calAxis = XAXIS; calAxis < LASTAXIS; calAxis++) {
for (int i=0; i<FINDZERO; i++) {
findZero[i] = analogRead(accelChannel[calAxis]);
}
accelZero[calAxis] = findMedian(findZero, FINDZERO);
}
// store accel value that represents 1g
measure();
accelOneG = -accelData[ZAXIS];
// replace with estimated Z axis 0g value
accelZero[ZAXIS] = (accelZero[XAXIS] + accelZero[YAXIS]) / 2;
writeFloat(accelOneG, ACCEL1G_ADR);
writeFloat(accelZero[YAXIS], LEVELROLLCAL_ADR);
writeFloat(accelZero[XAXIS], LEVELPITCHCAL_ADR);
writeFloat(accelZero[ZAXIS], LEVELZCAL_ADR);
}
};
#endif
/******************************************************/
/********* AeroQuad Mega v2 Accelerometer *************/
/******************************************************/
#if defined(AeroQuad_v18) || defined(AeroQuadMega_v2)
class Accel_AeroQuadMega_v2 : public Accel {
private:
int accelAddress;
public:
Accel_AeroQuadMega_v2() : Accel(){
accelAddress = 0x40; // page 54 and 61 of datasheet
accelScaleFactor = G_2_MPS2(1.0/4096.0); // g per LSB @ +/- 2g range
}
void initialize(void) {
byte data;
this->_initialize(0,1,2); // AKA added for consistency
accelOneG = readFloat(ACCEL1G_ADR);
accelZero[XAXIS] = readFloat(LEVELPITCHCAL_ADR);
accelZero[YAXIS] = readFloat(LEVELROLLCAL_ADR);
accelZero[ZAXIS] = readFloat(LEVELZCAL_ADR);
smoothFactor = readFloat(ACCSMOOTH_ADR);
// Check if accel is connected
if (readWhoI2C(accelAddress) != 0x03) // page 52 of datasheet
Serial.println("Accelerometer not found!");
// Thanks to SwiftingSpeed for updates on these settings
// http://aeroquad.com/showthread.php?991-AeroQuad-Flight-Software-v2.0&p=11207&viewfull=1#post11207
updateRegisterI2C(accelAddress, 0x10, 0xB6); //reset device
delay(10); //sleep 10 ms after reset (page 25)
// In datasheet, summary register map is page 21
// Low pass filter settings is page 27
// Range settings is page 28
updateRegisterI2C(accelAddress, 0x0D, 0x10); //enable writing to control registers
sendByteI2C(accelAddress, 0x20); // register bw_tcs (bits 4-7)
data = readByteI2C(accelAddress); // get current register value
updateRegisterI2C(accelAddress, 0x20, data & 0x0F); // set low pass filter to 10Hz (value = 0000xxxx)
// From page 27 of BMA180 Datasheet
// 1.0g = 0.13 mg/LSB
// 1.5g = 0.19 mg/LSB
// 2.0g = 0.25 mg/LSB
// 3.0g = 0.38 mg/LSB
// 4.0g = 0.50 mg/LSB
// 8.0g = 0.99 mg/LSB
// 16.0g = 1.98 mg/LSB
sendByteI2C(accelAddress, 0x35); // register offset_lsb1 (bits 1-3)
data = readByteI2C(accelAddress);
data &= 0xF1;
data |= 0x04; // Set range select bits for +/-2g
updateRegisterI2C(accelAddress, 0x35, data);
}
void measure(void) {
//int rawData[3];
Wire.beginTransmission(accelAddress);
Wire.send(0x02);
Wire.endTransmission();
Wire.requestFrom(accelAddress, 6);
for (byte axis = XAXIS; axis < LASTAXIS; axis++) {
if (axis == XAXIS)
accelADC[axis] = ((Wire.receive()|(Wire.receive() << 8)) >> 2) - accelZero[axis];
else
accelADC[axis] = accelZero[axis] - ((Wire.receive()|(Wire.receive() << 8)) >> 2);
accelData[axis] = filterSmooth(accelADC[axis] * accelScaleFactor, accelData[axis], smoothFactor);
}
}
const int getFlightData(byte axis) {
return getRaw(axis) >> 3;
}
// Allows user to zero accelerometers on command
void calibrate(void) {
int findZero[FINDZERO];
int dataAddress;
for (byte calAxis = XAXIS; calAxis < ZAXIS; calAxis++) {
if (calAxis == XAXIS) dataAddress = 0x02;
if (calAxis == YAXIS) dataAddress = 0x04;
if (calAxis == ZAXIS) dataAddress = 0x06;
for (int i=0; i<FINDZERO; i++) {
sendByteI2C(accelAddress, dataAddress);
findZero[i] = readReverseWordI2C(accelAddress) >> 2; // last two bits are not part of measurement
delay(10);
}
accelZero[calAxis] = findMedian(findZero, FINDZERO);
}
// replace with estimated Z axis 0g value
accelZero[ZAXIS] = (accelZero[XAXIS] + accelZero[PITCH]) / 2;
// store accel value that represents 1g
measure();
accelOneG = -accelData[ZAXIS];
writeFloat(accelOneG, ACCEL1G_ADR);
writeFloat(accelZero[XAXIS], LEVELPITCHCAL_ADR);
writeFloat(accelZero[YAXIS], LEVELROLLCAL_ADR);
writeFloat(accelZero[ZAXIS], LEVELZCAL_ADR);
}
};
#endif
/******************************************************/
/*********** ArduCopter ADC Accelerometer *************/
/******************************************************/
#ifdef ArduCopter
class Accel_ArduCopter : public Accel {
private:
int findZero[FINDZERO];
int rawADC;
public:
Accel_ArduCopter() : Accel(){
accelScaleFactor = G_2_MPS2((3.3/4096) / 0.330);
}
void initialize(void) {
// old AQ way
// rollChannel = 5
// pitchChannel = 4
// zAxisChannel = 6
// new way in 2.3
// rollChannel = 3
// pitchChannel = 4
// zAxisChannel = 5
this->_initialize(3, 4, 5);
smoothFactor = readFloat(ACCSMOOTH_ADR);
}
void measure(void) {
for (byte axis = ROLL; axis < LASTAXIS; axis++) {
rawADC = analogRead_ArduCopter_ADC(accelChannel[axis]);
if (rawADC > 500) // Check if measurement good
if (axis == ROLL)
accelADC[axis] = rawADC - accelZero[axis];
else
accelADC[axis] = accelZero[axis] - rawADC;
accelData[axis] = filterSmooth(accelADC[axis] * accelScaleFactor, accelData[axis], smoothFactor);
}
}
const int getFlightData(byte axis) {
return getRaw(axis);
}
// Allows user to zero accelerometers on command
void calibrate(void) {
for(byte calAxis = XAXIS; calAxis < LASTAXIS; calAxis++) {
for (int i=0; i<FINDZERO; i++) {
findZero[i] = analogRead_ArduCopter_ADC(accelChannel[calAxis]);
delay(2);
}
accelZero[calAxis] = findMedian(findZero, FINDZERO);
}
//accelOneG = 486; // tested value with the configurator at flat level
// replace with estimated Z axis 0g value
accelZero[ZAXIS] = (accelZero[ROLL] + accelZero[PITCH]) / 2;
// store accel value that represents 1g
measure();
accelOneG = -accelData[ZAXIS];
writeFloat(accelOneG, ACCEL1G_ADR);
writeFloat(accelZero[XAXIS], LEVELPITCHCAL_ADR);
writeFloat(accelZero[YAXIS], LEVELROLLCAL_ADR);
writeFloat(accelZero[ZAXIS], LEVELZCAL_ADR);
}
};
#endif
/******************************************************/
/****************** Wii Accelerometer *****************/
/******************************************************/
#if defined(AeroQuad_Wii) || defined(AeroQuadMega_Wii)
class Accel_Wii : public Accel {
public:
Accel_Wii() : Accel(){
accelScaleFactor = 0.09165093; // Experimentally derived to produce meters/s^2
}
void initialize(void) {
accelOneG = readFloat(ACCEL1G_ADR);
accelZero[XAXIS] = readFloat(LEVELPITCHCAL_ADR);
accelZero[YAXIS] = readFloat(LEVELROLLCAL_ADR);
accelZero[ZAXIS] = readFloat(LEVELZCAL_ADR);
smoothFactor = readFloat(ACCSMOOTH_ADR);
}
void measure(void) {
// Actual measurement performed in gyro class
// We just update the appropriate variables here
// Depending on how your accel is mounted, you can change X/Y axis to pitch/roll mapping here
accelADC[XAXIS] = NWMP_acc[PITCH] - accelZero[PITCH];
accelADC[YAXIS] = NWMP_acc[ROLL] - accelZero[ROLL];
accelADC[ZAXIS] = accelZero[ZAXIS] - NWMP_acc[ZAXIS];
for (byte axis = XAXIS; axis < LASTAXIS; axis++) {
accelData[axis] = filterSmooth(accelADC[axis] * accelScaleFactor, accelData[axis], smoothFactor);
}
}
const int getFlightData(byte axis) {
return getRaw(axis);
}
// Allows user to zero accelerometers on command
void calibrate(void) {
int findZero[FINDZERO];
for(byte calAxis = XAXIS; calAxis < LASTAXIS; calAxis++) {
for (int i=0; i<FINDZERO; i++) {
updateControls();
findZero[i] = NWMP_acc[calAxis];
}
accelZero[calAxis] = findMedian(findZero, FINDZERO);
}
// store accel value that represents 1g
accelOneG = -accelData[ZAXIS];
// replace with estimated Z axis 0g value
accelZero[ZAXIS] = (accelZero[XAXIS] + accelZero[YAXIS]) / 2;
writeFloat(accelOneG, ACCEL1G_ADR);
writeFloat(accelZero[YAXIS], LEVELROLLCAL_ADR);
writeFloat(accelZero[XAXIS], LEVELPITCHCAL_ADR);
writeFloat(accelZero[ZAXIS], LEVELZCAL_ADR);
}
};
#endif
/******************************************************/
/****************** CHR6DM Accelerometer **************/
/******************************************************/
#if defined(AeroQuadMega_CHR6DM) || defined(APM_OP_CHR6DM)
class Accel_CHR6DM : public Accel {
public:
Accel_CHR6DM() : Accel() {
accelScaleFactor = 0;
}
void initialize(void) {
smoothFactor = readFloat(ACCSMOOTH_ADR);
accelZero[ROLL] = readFloat(LEVELROLLCAL_ADR);
accelZero[PITCH] = readFloat(LEVELPITCHCAL_ADR);
accelZero[ZAXIS] = readFloat(LEVELZCAL_ADR);
accelOneG = readFloat(ACCEL1G_ADR);
calibrate();
}
void measure(void) {
//currentTime = micros(); // AKA removed as a result of Honks original work, not needed further
accelADC[XAXIS] = chr6dm.data.ax - accelZero[XAXIS];
accelADC[YAXIS] = chr6dm.data.ay - accelZero[YAXIS];
accelADC[ZAXIS] = chr6dm.data.az - accelOneG;
//accelData[XAXIS] = filterSmoothWithTime(accelADC[XAXIS], accelData[XAXIS], smoothFactor, ((currentTime - previousTime) / 5000.0)); //to get around 1
//accelData[YAXIS] = filterSmoothWithTime(accelADC[YAXIS], accelData[YAXIS], smoothFactor, ((currentTime - previousTime) / 5000.0));
//accelData[ZAXIS] = filterSmoothWithTime(accelADC[ZAXIS], accelData[ZAXIS], smoothFactor, ((currentTime - previousTime) / 5000.0));
accelData[XAXIS] = filterSmooth(accelADC[XAXIS], accelData[XAXIS], smoothFactor); //to get around 1
accelData[YAXIS] = filterSmooth(accelADC[YAXIS], accelData[YAXIS], smoothFactor);
accelData[ZAXIS] = filterSmooth(accelADC[ZAXIS], accelData[ZAXIS], smoothFactor);
//previousTime = currentTime; // AKA removed as a result of Honks original work, not needed further
}
const int getFlightData(byte axis) {
return getRaw(axis);
}
// Allows user to zero accelerometers on command
void calibrate(void) {
float zeroXreads[FINDZERO];
float zeroYreads[FINDZERO];
float zeroZreads[FINDZERO];
for (int i=0; i<FINDZERO; i++) {
chr6dm.requestAndReadPacket();
zeroXreads[i] = chr6dm.data.ax;
zeroYreads[i] = chr6dm.data.ay;
zeroZreads[i] = chr6dm.data.az;
}
accelZero[XAXIS] = findMedian(zeroXreads, FINDZERO);
accelZero[YAXIS] = findMedian(zeroYreads, FINDZERO);
accelZero[ZAXIS] = findMedian(zeroZreads, FINDZERO);
// store accel value that represents 1g
accelOneG = accelZero[ZAXIS];
// replace with estimated Z axis 0g value
//accelZero[ZAXIS] = (accelZero[ROLL] + accelZero[PITCH]) / 2;
writeFloat(accelOneG, ACCEL1G_ADR);
writeFloat(accelZero[XAXIS], LEVELROLLCAL_ADR);
writeFloat(accelZero[YAXIS], LEVELPITCHCAL_ADR);
writeFloat(accelZero[ZAXIS], LEVELZCAL_ADR);
}
/* AKA - NOT USED
void calculateAltitude() {
currentTime = micros();
if ((abs(CHR_RollAngle) < 5) && (abs(CHR_PitchAngle) < 5))
rawAltitude += (getZaxis()) * ((currentTime - previousTime) / 1000000.0);
previousTime = currentTime;
}
*/
};
#endif
/********************************************/
/******** CHR6DM Fake Accelerometer *********/
/********************************************/
#ifdef CHR6DM_FAKE_ACCEL
class Accel_CHR6DM_Fake : public Accel {
public:
float fakeAccelRoll;
float fakeAccelPitch;
float fakeAccelYaw;
Accel_CHR6DM_Fake() : Accel() {
accelScaleFactor = 0;
}
void initialize(void) {
smoothFactor = readFloat(ACCSMOOTH_ADR);
accelZero[ROLL] = readFloat(LEVELROLLCAL_ADR);
accelZero[PITCH] = readFloat(LEVELPITCHCAL_ADR);
accelZero[ZAXIS] = readFloat(LEVELZCAL_ADR);
accelZero[ROLL] = 0;
accelZero[PITCH] = 0;
accelZero[ZAXIS] = 0;
accelOneG = readFloat(ACCEL1G_ADR);
calibrate();
}
void measure(void) {
//currentTime = micros(); // AKA removed as a result of Honks original work, not needed further
//read done in gyro //TODO
accelADC[XAXIS] = fakeAccelRoll - accelZero[XAXIS];
accelADC[YAXIS] = fakeAccelPitch - accelZero[YAXIS];
accelADC[ZAXIS] = fakeAccelYaw - accelOneG;
//accelData[XAXIS] = smoothWithTime(accelADC[XAXIS], accelData[XAXIS], smoothFactor, ((currentTime - previousTime) / 5000.0));
//accelData[YAXIS] = smoothWithTime(accelADC[YAXIS], accelData[YAXIS], smoothFactor, ((currentTime - previousTime) / 5000.0));
//accelData[ZAXIS] = smoothWithTime(accelADC[ZAXIS], accelData[ZAXIS], smoothFactor, ((currentTime - previousTime) / 5000.0));
accelData[XAXIS] = smooth(accelADC[XAXIS], accelData[XAXIS], smoothFactor);
accelData[YAXIS] = smooth(accelADC[YAXIS], accelData[YAXIS], smoothFactor);
accelData[ZAXIS] = smooth(accelADC[ZAXIS], accelData[ZAXIS], smoothFactor);
//previousTime = currentTime; // AKA removed as a result of Honks original work, not needed further
}
const int getFlightData(byte axis) {
return getRaw(axis);
}
// Allows user to zero accelerometers on command
void calibrate(void) {
float zeroXreads[FINDZERO];
float zeroYreads[FINDZERO];
float zeroZreads[FINDZERO];
for (int i=0; i<FINDZERO; i++) {
chr6dm.requestAndReadPacket();
zeroXreads[i] = 0;
zeroYreads[i] = 0;
zeroZreads[i] = 0;
}
accelZero[XAXIS] = findMedian(zeroXreads, FINDZERO);
accelZero[YAXIS] = findMedian(zeroYreads, FINDZERO);
accelZero[ZAXIS] = findMedian(zeroZreads, FINDZERO);
// store accel value that represents 1g
accelOneG = accelZero[ZAXIS];
// replace with estimated Z axis 0g value
//accelZero[ZAXIS] = (accelZero[ROLL] + accelZero[PITCH]) / 2;
writeFloat(accelOneG, ACCEL1G_ADR);
writeFloat(accelZero[XAXIS], LEVELROLLCAL_ADR);
writeFloat(accelZero[YAXIS], LEVELPITCHCAL_ADR);
writeFloat(accelZero[ZAXIS], LEVELZCAL_ADR);
}
/* AKA - NOT USED
void calculateAltitude() {
currentTime = micros();
if ((abs(CHR_RollAngle) < 5) && (abs(CHR_PitchAngle) < 5))
rawAltitude += (getZaxis()) * ((currentTime - previousTime) / 1000000.0);
previousTime = currentTime;
}
*/
};
#endif