-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSensors.c
1092 lines (973 loc) · 38.8 KB
/
Sensors.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
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
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// ************************************************************************************************************
// board orientation and setup
// ************************************************************************************************************
//default board orientation
#if !defined(ACC_ORIENTATION)
#define ACC_ORIENTATION(X, Y, Z) {accADC[ROLL] = X; accADC[PITCH] = Y; accADC[YAW] = Z;}
#endif
#if !defined(GYRO_ORIENTATION)
#define GYRO_ORIENTATION(X, Y, Z) {gyroADC[ROLL] = X; gyroADC[PITCH] = Y; gyroADC[YAW] = Z;}
#endif
#if !defined(MAG_ORIENTATION)
#define MAG_ORIENTATION(X, Y, Z) {magADC[ROLL] = X; magADC[PITCH] = Y; magADC[YAW] = Z;}
#endif
/*** I2C address ***/
#if !defined(MMA7455_ADDRESS)
#define MMA7455_ADDRESS 0x1D
#endif
#if !defined(ADXL345_ADDRESS)
#define ADXL345_ADDRESS 0x1D
//#define ADXL345_ADDRESS 0x53 //WARNING: Conflicts with a Wii Motion plus!
#endif
#if !defined(BMA180_ADDRESS)
#define BMA180_ADDRESS 0x40
//#define BMA180_ADDRESS 0x41
#endif
#if !defined(ITG3200_ADDRESS)
#define ITG3200_ADDRESS 0X68
//#define ITG3200_ADDRESS 0X69
#endif
#if !defined(MPU6050_ADDRESS)
#define MPU6050_ADDRESS 0x68 // address pin AD0 low (GND), default for FreeIMU v0.4 and InvenSense evaluation board
//#define MPU6050_ADDRESS 0x69 // address pin AD0 high (VCC)
#endif
//MPU6050 Gyro LPF setting
#if defined(MPU6050_LPF_256HZ) || defined(MPU6050_LPF_188HZ) || defined(MPU6050_LPF_98HZ) || defined(MPU6050_LPF_42HZ) || defined(MPU6050_LPF_20HZ) || defined(MPU6050_LPF_10HZ) || defined(MPU6050_LPF_5HZ)
#if defined(MPU6050_LPF_256HZ)
#define MPU6050_DLPF_CFG 0
#endif
#if defined(MPU6050_LPF_188HZ)
#define MPU6050_DLPF_CFG 1
#endif
#if defined(MPU6050_LPF_98HZ)
#define MPU6050_DLPF_CFG 2
#endif
#if defined(MPU6050_LPF_42HZ)
#define MPU6050_DLPF_CFG 3
#endif
#if defined(MPU6050_LPF_20HZ)
#define MPU6050_DLPF_CFG 4
#endif
#if defined(MPU6050_LPF_10HZ)
#define MPU6050_DLPF_CFG 5
#endif
#if defined(MPU6050_LPF_5HZ)
#define MPU6050_DLPF_CFG 6
#endif
#else
//Default settings LPF 256Hz/8000Hz sample
#define MPU6050_DLPF_CFG 0
#endif
#if defined(TINY_GPS) | defined(TINY_GPS_SONAR)
#define TINY_GPS_TWI_ADD 0x11
#include "tinygps.h"
#endif
uint8_t rawADC[6];
static uint32_t neutralizeTime = 0;
// ************************************************************************************************************
// I2C general functions
// ************************************************************************************************************
void i2c_init(void) {
#if defined(INTERNAL_I2C_PULLUPS)
I2C_PULLUPS_ENABLE
#else
I2C_PULLUPS_DISABLE
#endif
TWSR = 0; // no prescaler => prescaler = 1
TWBR = ((F_CPU / I2C_SPEED) - 16) / 2; // change the I2C clock rate
TWCR = 1<<TWEN; // enable twi module, no interrupt
}
// I2C repeat start
void i2c_rep_start(uint8_t address) {
TWCR = (1<<TWINT) | (1<<TWSTA) | (1<<TWEN) ; // send REPEAT START condition
waitTransmissionI2C(); // wait until transmission completed
TWDR = address; // send device address
TWCR = (1<<TWINT) | (1<<TWEN);
waitTransmissionI2C(); // wail until transmission completed
}
void i2c_stop(void) {
TWCR = (1 << TWINT) | (1 << TWEN) | (1 << TWSTO);
// while(TWCR & (1<<TWSTO)); // <- can produce a blocking state with some WMP clones
}
void i2c_write(uint8_t data ) {
TWDR = data; // send data to the previously addressed device
TWCR = (1<<TWINT) | (1<<TWEN);
waitTransmissionI2C();
}
uint8_t i2c_read(uint8_t ack) {
TWCR = (1<<TWINT) | (1<<TWEN) | (ack? (1<<TWEA) : 0);
waitTransmissionI2C();
uint8_t r = TWDR;
if (!ack) i2c_stop();
return r;
}
uint8_t i2c_readAck() {
return i2c_read(1);
}
uint8_t i2c_readNak(void) {
return i2c_read(0);
}
// spin here to wait i2c transmission completed
void waitTransmissionI2C() {
uint16_t count = 255;
while (!(TWCR & (1<<TWINT))) {
count--;
if (count==0) { //we are in a blocking state => we don't insist
TWCR = 0; //and we force a reset on TWINT register
neutralizeTime = micros(); //we take a timestamp here to neutralize the value during a short delay
i2c_errors_count++;
break;
}
}
}
//read 'size' bytes to buf
size_t i2c_read_to_buf(uint8_t add, void *buf, size_t size) {
i2c_rep_start((add<<1) | 1); // I2C read direction
size_t bytes_read = 0;
uint8_t *b = (uint8_t*)buf;
while (size--) {
/* acknowledge all but the final byte */
*b++ = i2c_read(size > 0);
/* TODO catch I2C errors here and abort */
bytes_read++;
}
return bytes_read;
}
size_t i2c_read_reg_to_buf(uint8_t add, uint8_t reg, void *buf, size_t size) {
i2c_rep_start(add<<1); // I2C write direction
i2c_write(reg); // register selection
return i2c_read_to_buf(add, buf, size);
}
/* transform a series of bytes from big endian to little
endian and vice versa. */
void swap_endianness(void *buf, size_t size) {
/* we swap in-place, so we only have to
* place _one_ element on a temporary tray
*/
uint8_t tray;
uint8_t *from;
uint8_t *to;
/* keep swapping until the pointers have assed each other */
for (from = (uint8_t*)buf, to = &from[size-1]; from < to; from++, to--) {
tray = *from;
*from = *to;
*to = tray;
}
}
void i2c_getSixRawADC(uint8_t add, uint8_t reg) {
i2c_read_reg_to_buf(add, reg, &rawADC, 6);
}
void i2c_writeReg(uint8_t add, uint8_t reg, uint8_t val) {
i2c_rep_start(add<<1); // I2C write direction
i2c_write(reg); // register selection
i2c_write(val); // value to write in register
i2c_stop();
}
uint8_t i2c_readReg(uint8_t add, uint8_t reg) {
uint8_t val;
i2c_read_reg_to_buf(add, reg, &val, 1);
return val;
}
// ****************
// GYRO common part
// ****************
void GYRO_Common() {
static int16_t previousGyroADC[3] = {0,0,0};
static int32_t g[3];
uint8_t axis, tilt=0;
#if defined MMGYRO
// Moving Average Gyros by Magnetron1
//---------------------------------------------------
static int16_t mediaMobileGyroADC[3][MMGYROVECTORLENGTH];
static int32_t mediaMobileGyroADCSum[3];
static uint8_t mediaMobileGyroIDX;
//---------------------------------------------------
#endif
if (calibratingG>0) {
for (axis = 0; axis < 3; axis++) {
// Reset g[axis] at start of calibration
if (calibratingG == 512) {
g[axis]=0;
#if defined(GYROCALIBRATIONFAILSAFE)
previousGyroADC[axis] = gyroADC[axis];
}
if (calibratingG % 10 == 0) {
if(abs(gyroADC[axis] - previousGyroADC[axis]) > 8) tilt=1;
previousGyroADC[axis] = gyroADC[axis];
#endif
}
// Sum up 512 readings
g[axis] +=gyroADC[axis];
// Clear global variables for next reading
gyroADC[axis]=0;
gyroZero[axis]=0;
if (calibratingG == 1) {
gyroZero[axis]=g[axis]>>9;
#if defined(BUZZER)
alarmArray[7] = 4;
#else
blinkLED(10,15,1); //the delay causes to beep the buzzer really long
#endif
}
}
#if defined(GYROCALIBRATIONFAILSAFE)
if(tilt) {
calibratingG=1000;
LEDPIN_ON;
} else {
calibratingG--;
LEDPIN_OFF;
}
return;
#else
calibratingG--;
#endif
}
#ifdef MMGYRO
mediaMobileGyroIDX = ++mediaMobileGyroIDX % conf.mmgyro;
for (axis = 0; axis < 3; axis++) {
gyroADC[axis] -= gyroZero[axis];
mediaMobileGyroADCSum[axis] -= mediaMobileGyroADC[axis][mediaMobileGyroIDX];
//anti gyro glitch, limit the variation between two consecutive readings
mediaMobileGyroADC[axis][mediaMobileGyroIDX] = constrain(gyroADC[axis],previousGyroADC[axis]-800,previousGyroADC[axis]+800);
mediaMobileGyroADCSum[axis] += mediaMobileGyroADC[axis][mediaMobileGyroIDX];
gyroADC[axis] = mediaMobileGyroADCSum[axis] / conf.mmgyro;
#else
for (axis = 0; axis < 3; axis++) {
gyroADC[axis] -= gyroZero[axis];
//anti gyro glitch, limit the variation between two consecutive readings
gyroADC[axis] = constrain(gyroADC[axis],previousGyroADC[axis]-800,previousGyroADC[axis]+800);
#endif
previousGyroADC[axis] = gyroADC[axis];
}
#if defined(SENSORS_TILT_45DEG_LEFT)
int16_t temp = ((gyroADC[PITCH] - gyroADC[ROLL] )*7)/10;
gyroADC[ROLL] = ((gyroADC[ROLL] + gyroADC[PITCH])*7)/10;
gyroADC[PITCH]= temp;
#endif
#if defined(SENSORS_TILT_45DEG_RIGHT)
int16_t temp = ((gyroADC[PITCH] + gyroADC[ROLL] )*7)/10;
gyroADC[ROLL] = ((gyroADC[ROLL] - gyroADC[PITCH])*7)/10;
gyroADC[PITCH]= temp;
#endif
}
// ****************
// ACC common part
// ****************
void ACC_Common() {
static int32_t a[3];
if (calibratingA>0) {
for (uint8_t axis = 0; axis < 3; axis++) {
// Reset a[axis] at start of calibration
if (calibratingA == 512) a[axis]=0;
// Sum up 512 readings
a[axis] +=accADC[axis];
// Clear global variables for next reading
accADC[axis]=0;
global_conf.accZero[axis]=0;
}
// Calculate average, shift Z down by acc_1G and store values in EEPROM at end of calibration
if (calibratingA == 1) {
global_conf.accZero[ROLL] = a[ROLL]>>9;
global_conf.accZero[PITCH] = a[PITCH]>>9;
global_conf.accZero[YAW] = (a[YAW]>>9)-acc_1G; // for nunchuk 200=1G
conf.angleTrim[ROLL] = 0;
conf.angleTrim[PITCH] = 0;
writeGlobalSet(1); // write accZero in EEPROM
}
calibratingA--;
}
#if defined(INFLIGHT_ACC_CALIBRATION)
static int32_t b[3];
static int16_t accZero_saved[3] = {0,0,0};
static int16_t angleTrim_saved[2] = {0, 0};
//Saving old zeropoints before measurement
if (InflightcalibratingA==50) {
accZero_saved[ROLL] = global_conf.accZero[ROLL] ;
accZero_saved[PITCH] = global_conf.accZero[PITCH];
accZero_saved[YAW] = global_conf.accZero[YAW] ;
angleTrim_saved[ROLL] = conf.angleTrim[ROLL] ;
angleTrim_saved[PITCH] = conf.angleTrim[PITCH] ;
}
if (InflightcalibratingA>0) {
for (uint8_t axis = 0; axis < 3; axis++) {
// Reset a[axis] at start of calibration
if (InflightcalibratingA == 50) b[axis]=0;
// Sum up 50 readings
b[axis] +=accADC[axis];
// Clear global variables for next reading
accADC[axis]=0;
global_conf.accZero[axis]=0;
}
//all values are measured
if (InflightcalibratingA == 1) {
AccInflightCalibrationActive = 0;
AccInflightCalibrationMeasurementDone = 1;
#if defined(BUZZER)
alarmArray[7] = 1; //buzzer for indicatiing the end of calibration
#endif
// recover saved values to maintain current flight behavior until new values are transferred
global_conf.accZero[ROLL] = accZero_saved[ROLL] ;
global_conf.accZero[PITCH] = accZero_saved[PITCH];
global_conf.accZero[YAW] = accZero_saved[YAW] ;
conf.angleTrim[ROLL] = angleTrim_saved[ROLL] ;
conf.angleTrim[PITCH] = angleTrim_saved[PITCH] ;
}
InflightcalibratingA--;
}
// Calculate average, shift Z down by acc_1G and store values in EEPROM at end of calibration
if (AccInflightCalibrationSavetoEEProm == 1){ //the copter is landed, disarmed and the combo has been done again
AccInflightCalibrationSavetoEEProm = 0;
global_conf.accZero[ROLL] = b[ROLL]/50;
global_conf.accZero[PITCH] = b[PITCH]/50;
global_conf.accZero[YAW] = b[YAW]/50-acc_1G; // for nunchuk 200=1G
conf.angleTrim[ROLL] = 0;
conf.angleTrim[PITCH] = 0;
writeGlobalSet(1); // write accZero in EEPROM
}
#endif
accADC[ROLL] -= global_conf.accZero[ROLL] ;
accADC[PITCH] -= global_conf.accZero[PITCH];
accADC[YAW] -= global_conf.accZero[YAW] ;
#if defined(SENSORS_TILT_45DEG_LEFT)
int16_t temp = ((accADC[PITCH] - accADC[ROLL] )*7)/10;
accADC[ROLL] = ((accADC[ROLL] + accADC[PITCH])*7)/10;
accADC[PITCH] = temp;
#endif
#if defined(SENSORS_TILT_45DEG_RIGHT)
int16_t temp = ((accADC[PITCH] + accADC[ROLL] )*7)/10;
accADC[ROLL] = ((accADC[ROLL] - accADC[PITCH])*7)/10;
accADC[PITCH] = temp;
#endif
}
// ************************************************************************************************************
// I2C Barometer MS561101BA
// ************************************************************************************************************
//
// specs are here: http://www.meas-spec.com/downloads/MS5611-01BA03.pdf
// useful info on pages 7 -> 12
#if defined(MS561101BA)
// registers of the device
#define MS561101BA_PRESSURE 0x40
#define MS561101BA_TEMPERATURE 0x50
#define MS561101BA_RESET 0x1E
// OSR (Over Sampling Ratio) constants
#define MS561101BA_OSR_256 0x00
#define MS561101BA_OSR_512 0x02
#define MS561101BA_OSR_1024 0x04
#define MS561101BA_OSR_2048 0x06
#define MS561101BA_OSR_4096 0x08
#define OSR MS561101BA_OSR_4096
static struct {
// sensor registers from the MS561101BA datasheet
uint16_t c[7];
union {uint32_t val; uint8_t raw[4]; } ut; //uncompensated T
union {uint32_t val; uint8_t raw[4]; } up; //uncompensated P
uint8_t state;
uint32_t deadline;
} ms561101ba_ctx;
void i2c_MS561101BA_reset(){
i2c_writeReg(MS561101BA_ADDRESS, MS561101BA_RESET, 0);
}
void i2c_MS561101BA_readCalibration(){
union {uint16_t val; uint8_t raw[2]; } data;
for(uint8_t i=0;i<6;i++) {
i2c_rep_start(MS561101BA_ADDRESS<<1);
i2c_write(0xA2+2*i);
delay(10);
i2c_rep_start((MS561101BA_ADDRESS<<1) | 1);//I2C read direction => 1
delay(10);
data.raw[1] = i2c_readAck(); // read a 16 bit register
data.raw[0] = i2c_readNak();
ms561101ba_ctx.c[i+1] = data.val;
}
}
void Baro_init() {
delay(10);
i2c_MS561101BA_reset();
delay(100);
i2c_MS561101BA_readCalibration();
delay(10);
i2c_MS561101BA_UT_Start();
ms561101ba_ctx.deadline = currentTime+10000;
}
// read uncompensated temperature value: send command first
void i2c_MS561101BA_UT_Start() {
i2c_rep_start(MS561101BA_ADDRESS<<1); // I2C write direction
i2c_write(MS561101BA_TEMPERATURE + OSR); // register selection
i2c_stop();
}
// read uncompensated pressure value: send command first
void i2c_MS561101BA_UP_Start () {
i2c_rep_start(MS561101BA_ADDRESS<<1); // I2C write direction
i2c_write(MS561101BA_PRESSURE + OSR); // register selection
i2c_stop();
}
// read uncompensated pressure value: read result bytes
void i2c_MS561101BA_UP_Read () {
i2c_rep_start(MS561101BA_ADDRESS<<1);
i2c_write(0);
i2c_rep_start((MS561101BA_ADDRESS<<1) | 1);
ms561101ba_ctx.up.raw[2] = i2c_readAck();
ms561101ba_ctx.up.raw[1] = i2c_readAck();
ms561101ba_ctx.up.raw[0] = i2c_readNak();
}
// read uncompensated temperature value: read result bytes
void i2c_MS561101BA_UT_Read() {
i2c_rep_start(MS561101BA_ADDRESS<<1);
i2c_write(0);
i2c_rep_start((MS561101BA_ADDRESS<<1) | 1);
ms561101ba_ctx.ut.raw[2] = i2c_readAck();
ms561101ba_ctx.ut.raw[1] = i2c_readAck();
ms561101ba_ctx.ut.raw[0] = i2c_readNak();
}
void i2c_MS561101BA_Calculate() {
int32_t off2,sens2,delt;
int64_t dT = (int32_t)ms561101ba_ctx.ut.val - ((int32_t)ms561101ba_ctx.c[5] << 8);
baroTemperature = 2000 + ((dT * ms561101ba_ctx.c[6])>>23);
int64_t off = ((uint32_t)ms561101ba_ctx.c[2] <<16) + ((dT * ms561101ba_ctx.c[4]) >> 7);
int64_t sens = ((uint32_t)ms561101ba_ctx.c[1] <<15) + ((dT * ms561101ba_ctx.c[3]) >> 8);
if (baroTemperature < 2000) { // temperature lower than 20st.C
delt = baroTemperature-2000;
delt = 5*delt*delt;
off2 = delt>>1;
sens2 = delt>>2;
if (baroTemperature < -1500) { // temperature lower than -15st.C
delt = baroTemperature+1500;
delt = delt*delt;
off2 += 7 * delt;
sens2 += (11 * delt)>>1;
}
off -= off2;
sens -= sens2;
}
baroPressure = (( (ms561101ba_ctx.up.val * sens ) >> 21) - off) >> 15;
}
//return 0: no data available, no computation ; 1: new value available ; 2: no new value, but computation time
uint8_t Baro_update() { // first UT conversion is started in init procedure
if (currentTime < ms561101ba_ctx.deadline) return 0;
ms561101ba_ctx.deadline = currentTime+10000; // UT and UP conversion take 8.5ms so we do next reading after 10ms
TWBR = ((F_CPU / 400000L) - 16) / 2; // change the I2C clock rate to 400kHz, MS5611 is ok with this speed
if (ms561101ba_ctx.state == 0) {
i2c_MS561101BA_UT_Read();
i2c_MS561101BA_UP_Start();
Baro_Common(); // moved here for less timecycle spike
ms561101ba_ctx.state = 1;
return 1;
} else {
i2c_MS561101BA_UP_Read();
i2c_MS561101BA_UT_Start();
i2c_MS561101BA_Calculate();
ms561101ba_ctx.state = 0;
return 2;
}
}
#endif
#if BARO
void Baro_Common() {
static int32_t baroHistTab[BARO_TAB_SIZE];
static uint8_t baroHistIdx;
uint8_t indexplus1 = (baroHistIdx + 1);
if (indexplus1 == BARO_TAB_SIZE) indexplus1 = 0;
baroHistTab[baroHistIdx] = baroPressure;
baroPressureSum += baroHistTab[baroHistIdx];
baroPressureSum -= baroHistTab[indexplus1];
baroHistIdx = indexplus1;
}
#endif
// ************************************************************************************************************
// standalone I2C Nunchuk
// ************************************************************************************************************
#if defined(NUNCHACK)
#define NUNCHACK_ADDRESS 0x52
void ACC_init() {
i2c_writeReg(NUNCHACK_ADDRESS ,0xF0 ,0x55 );
i2c_writeReg(NUNCHACK_ADDRESS ,0xFB ,0x00 );
delay(250);
acc_1G = 200;
}
void ACC_getADC() {
TWBR = ((F_CPU / I2C_SPEED) - 16) / 2; // change the I2C clock rate. !! you must check if the nunchuk is ok with this freq
i2c_getSixRawADC(NUNCHACK_ADDRESS,0x00);
ACC_ORIENTATION( ( (rawADC[3]<<2) + ((rawADC[5]>>4)&0x2) ) ,
- ( (rawADC[2]<<2) + ((rawADC[5]>>3)&0x2) ) ,
( ((rawADC[4]&0xFE)<<2) + ((rawADC[5]>>5)&0x6) ));
ACC_Common();
}
#endif
// ************************************************************************************************************
// I2C Compass common function
// ************************************************************************************************************
#if MAG
static float magGain[3] = {1.0,1.0,1.0}; // gain for each axis, populated at sensor init
static uint8_t magInit = 0;
uint8_t Mag_getADC() { // return 1 when news values are available, 0 otherwise
static uint32_t t,tCal = 0;
static int16_t magZeroTempMin[3];
static int16_t magZeroTempMax[3];
uint8_t axis;
if ( currentTime < t ) return 0; //each read is spaced by 100ms
t = currentTime + 100000;
TWBR = ((F_CPU / 400000L) - 16) / 2; // change the I2C clock rate to 400kHz
Device_Mag_getADC();
magADC[ROLL] = magADC[ROLL] * magGain[ROLL];
magADC[PITCH] = magADC[PITCH] * magGain[PITCH];
magADC[YAW] = magADC[YAW] * magGain[YAW];
if (f.CALIBRATE_MAG) {
tCal = t;
for(axis=0;axis<3;axis++) {
global_conf.magZero[axis] = 0;
magZeroTempMin[axis] = magADC[axis];
magZeroTempMax[axis] = magADC[axis];
}
f.CALIBRATE_MAG = 0;
}
if (magInit) { // we apply offset only once mag calibration is done
magADC[ROLL] -= global_conf.magZero[ROLL];
magADC[PITCH] -= global_conf.magZero[PITCH];
magADC[YAW] -= global_conf.magZero[YAW];
}
if (tCal != 0) {
if ((t - tCal) < 30000000) { // 30s: you have 30s to turn the multi in all directions
LEDPIN_TOGGLE;
for(axis=0;axis<3;axis++) {
if (magADC[axis] < magZeroTempMin[axis]) magZeroTempMin[axis] = magADC[axis];
if (magADC[axis] > magZeroTempMax[axis]) magZeroTempMax[axis] = magADC[axis];
}
} else {
tCal = 0;
for(axis=0;axis<3;axis++)
global_conf.magZero[axis] = (magZeroTempMin[axis] + magZeroTempMax[axis])>>1;
writeGlobalSet(1);
}
} else {
#if defined(SENSORS_TILT_45DEG_LEFT)
int16_t temp = ((magADC[PITCH] - magADC[ROLL] )*7)/10;
magADC[ROLL] = ((magADC[ROLL] + magADC[PITCH])*7)/10;
magADC[PITCH] = temp;
#endif
#if defined(SENSORS_TILT_45DEG_RIGHT)
int16_t temp = ((magADC[PITCH] + magADC[ROLL] )*7)/10;
magADC[ROLL] = ((magADC[ROLL] - magADC[PITCH])*7)/10;
magADC[PITCH] = temp;
#endif
}
return 1;
}
#endif
// ************************************************************************************************************
// I2C Compass HMC5883
// ************************************************************************************************************
// I2C adress: 0x3C (8bit) 0x1E (7bit)
// ************************************************************************************************************
#if defined(HMC5883)
#define HMC58X3_R_CONFA 0
#define HMC58X3_R_CONFB 1
#define HMC58X3_R_MODE 2
#define HMC58X3_X_SELF_TEST_GAUSS (+1.16) //!< X axis level when bias current is applied.
#define HMC58X3_Y_SELF_TEST_GAUSS (+1.16) //!< Y axis level when bias current is applied.
#define HMC58X3_Z_SELF_TEST_GAUSS (+1.08) //!< Y axis level when bias current is applied.
#define SELF_TEST_LOW_LIMIT (243.0/390.0) //!< Low limit when gain is 5.
#define SELF_TEST_HIGH_LIMIT (575.0/390.0) //!< High limit when gain is 5.
#define HMC_POS_BIAS 1
#define HMC_NEG_BIAS 2
#define MAG_ADDRESS 0x1E
#define MAG_DATA_REGISTER 0x03
void Mag_init() {
int32_t xyz_total[3]={0,0,0}; // 32 bit totals so they won't overflow.
bool bret=true; // Error indicator
delay(50); //Wait before start
i2c_writeReg(MAG_ADDRESS, HMC58X3_R_CONFA, 0x010 + HMC_POS_BIAS); // Reg A DOR=0x010 + MS1,MS0 set to pos bias
// Note that the very first measurement after a gain change maintains the same gain as the previous setting.
// The new gain setting is effective from the second measurement and on.
i2c_writeReg(MAG_ADDRESS, HMC58X3_R_CONFB, 2 << 5); //Set the Gain
i2c_writeReg(MAG_ADDRESS,HMC58X3_R_MODE, 1);
delay(100);
getADC(); //Get one sample, and discard it
for (uint8_t i=0; i<10; i++) { //Collect 10 samples
i2c_writeReg(MAG_ADDRESS,HMC58X3_R_MODE, 1);
delay(100);
getADC(); // Get the raw values in case the scales have already been changed.
// Since the measurements are noisy, they should be averaged rather than taking the max.
xyz_total[0]+=magADC[0];
xyz_total[1]+=magADC[1];
xyz_total[2]+=magADC[2];
// Detect saturation.
if (-(1<<12) >= min(magADC[0],min(magADC[1],magADC[2]))) {
bret=false;
break; // Breaks out of the for loop. No sense in continuing if we saturated.
}
}
// Apply the negative bias. (Same gain)
i2c_writeReg(MAG_ADDRESS,HMC58X3_R_CONFA, 0x010 + HMC_NEG_BIAS); // Reg A DOR=0x010 + MS1,MS0 set to negative bias.
for (uint8_t i=0; i<10; i++) {
i2c_writeReg(MAG_ADDRESS,HMC58X3_R_MODE, 1);
delay(100);
getADC(); // Get the raw values in case the scales have already been changed.
// Since the measurements are noisy, they should be averaged.
xyz_total[0]-=magADC[0];
xyz_total[1]-=magADC[1];
xyz_total[2]-=magADC[2];
// Detect saturation.
if (-(1<<12) >= min(magADC[0],min(magADC[1],magADC[2]))) {
bret=false;
break; // Breaks out of the for loop. No sense in continuing if we saturated.
}
}
magGain[0]=fabs(820.0*HMC58X3_X_SELF_TEST_GAUSS*2.0*10.0/xyz_total[0]);
magGain[1]=fabs(820.0*HMC58X3_Y_SELF_TEST_GAUSS*2.0*10.0/xyz_total[1]);
magGain[2]=fabs(820.0*HMC58X3_Z_SELF_TEST_GAUSS*2.0*10.0/xyz_total[2]);
// leave test mode
i2c_writeReg(MAG_ADDRESS ,HMC58X3_R_CONFA ,0x70 ); //Configuration Register A -- 0 11 100 00 num samples: 8 ; output rate: 15Hz ; normal measurement mode
i2c_writeReg(MAG_ADDRESS ,HMC58X3_R_CONFB ,0x20 ); //Configuration Register B -- 001 00000 configuration gain 1.3Ga
i2c_writeReg(MAG_ADDRESS ,HMC58X3_R_MODE ,0x00 ); //Mode register -- 000000 00 continuous Conversion Mode
delay(100);
magInit = 1;
if (!bret) { //Something went wrong so get a best guess
magGain[0] = 1.0;
magGain[1] = 1.0;
magGain[2] = 1.0;
}
} // Mag_init().
#endif
// ************************************************************************************************************
// I2C Gyroscope and Accelerometer MPU6050
// ************************************************************************************************************
#if defined(MPU6050)
void Gyro_init() {
TWBR = ((F_CPU / 400000L) - 16) / 2; // change the I2C clock rate to 400kHz
i2c_writeReg(MPU6050_ADDRESS, 0x6B, 0x80); //PWR_MGMT_1 -- DEVICE_RESET 1
delay(5);
i2c_writeReg(MPU6050_ADDRESS, 0x6B, 0x03); //PWR_MGMT_1 -- SLEEP 0; CYCLE 0; TEMP_DIS 0; CLKSEL 3 (PLL with Z Gyro reference)
i2c_writeReg(MPU6050_ADDRESS, 0x1A, MPU6050_DLPF_CFG); //CONFIG -- EXT_SYNC_SET 0 (disable input pin for data sync) ; default DLPF_CFG = 0 => ACC bandwidth = 260Hz GYRO bandwidth = 256Hz)
i2c_writeReg(MPU6050_ADDRESS, 0x1B, 0x18); //GYRO_CONFIG -- FS_SEL = 3: Full scale set to 2000 deg/sec
// enable I2C bypass for AUX I2C
#if defined(MAG)
i2c_writeReg(MPU6050_ADDRESS, 0x37, 0x02); //INT_PIN_CFG -- INT_LEVEL=0 ; INT_OPEN=0 ; LATCH_INT_EN=0 ; INT_RD_CLEAR=0 ; FSYNC_INT_LEVEL=0 ; FSYNC_INT_EN=0 ; I2C_BYPASS_EN=1 ; CLKOUT_EN=0
#endif
}
void Gyro_getADC () {
i2c_getSixRawADC(MPU6050_ADDRESS, 0x43);
GYRO_ORIENTATION( ((rawADC[0]<<8) | rawADC[1])>>2 , // range: +/- 8192; +/- 2000 deg/sec
((rawADC[2]<<8) | rawADC[3])>>2 ,
((rawADC[4]<<8) | rawADC[5])>>2 );
GYRO_Common();
}
void ACC_init () {
i2c_writeReg(MPU6050_ADDRESS, 0x1C, 0x10); //ACCEL_CONFIG -- AFS_SEL=2 (Full Scale = +/-8G) ; ACCELL_HPF=0 //note something is wrong in the spec.
//note: something seems to be wrong in the spec here. With AFS=2 1G = 4096 but according to my measurement: 1G=2048 (and 2048/8 = 256)
//confirmed here: http://www.multiwii.com/forum/viewtopic.php?f=8&t=1080&start=10#p7480
#if defined(FREEIMUv04)
acc_1G = 255;
#else
acc_1G = 512;
#endif
#if defined(MPU6050_I2C_AUX_MASTER)
//at this stage, the MAG is configured via the original MAG init function in I2C bypass mode
//now we configure MPU as a I2C Master device to handle the MAG via the I2C AUX port (done here for HMC5883)
i2c_writeReg(MPU6050_ADDRESS, 0x6A, 0b00100000); //USER_CTRL -- DMP_EN=0 ; FIFO_EN=0 ; I2C_MST_EN=1 (I2C master mode) ; I2C_IF_DIS=0 ; FIFO_RESET=0 ; I2C_MST_RESET=0 ; SIG_COND_RESET=0
i2c_writeReg(MPU6050_ADDRESS, 0x37, 0x00); //INT_PIN_CFG -- INT_LEVEL=0 ; INT_OPEN=0 ; LATCH_INT_EN=0 ; INT_RD_CLEAR=0 ; FSYNC_INT_LEVEL=0 ; FSYNC_INT_EN=0 ; I2C_BYPASS_EN=0 ; CLKOUT_EN=0
i2c_writeReg(MPU6050_ADDRESS, 0x24, 0x0D); //I2C_MST_CTRL -- MULT_MST_EN=0 ; WAIT_FOR_ES=0 ; SLV_3_FIFO_EN=0 ; I2C_MST_P_NSR=0 ; I2C_MST_CLK=13 (I2C slave speed bus = 400kHz)
i2c_writeReg(MPU6050_ADDRESS, 0x25, 0x80|MAG_ADDRESS);//I2C_SLV0_ADDR -- I2C_SLV4_RW=1 (read operation) ; I2C_SLV4_ADDR=MAG_ADDRESS
i2c_writeReg(MPU6050_ADDRESS, 0x26, MAG_DATA_REGISTER);//I2C_SLV0_REG -- 6 data bytes of MAG are stored in 6 registers. First register address is MAG_DATA_REGISTER
i2c_writeReg(MPU6050_ADDRESS, 0x27, 0x86); //I2C_SLV0_CTRL -- I2C_SLV0_EN=1 ; I2C_SLV0_BYTE_SW=0 ; I2C_SLV0_REG_DIS=0 ; I2C_SLV0_GRP=0 ; I2C_SLV0_LEN=3 (3x2 bytes)
#endif
}
void ACC_getADC () {
i2c_getSixRawADC(MPU6050_ADDRESS, 0x3B);
ACC_ORIENTATION( ((rawADC[0]<<8) | rawADC[1])>>3 ,
((rawADC[2]<<8) | rawADC[3])>>3 ,
((rawADC[4]<<8) | rawADC[5])>>3 );
ACC_Common();
}
//The MAG acquisition function must be replaced because we now talk to the MPU device
#if defined(MPU6050_I2C_AUX_MASTER)
void Device_Mag_getADC() {
i2c_getSixRawADC(MPU6050_ADDRESS, 0x49); //0x49 is the first memory room for EXT_SENS_DATA
#if defined(HMC5843)
MAG_ORIENTATION( ((rawADC[0]<<8) | rawADC[1]) ,
((rawADC[2]<<8) | rawADC[3]) ,
((rawADC[4]<<8) | rawADC[5]) );
#endif
#if defined (HMC5883)
MAG_ORIENTATION( ((rawADC[0]<<8) | rawADC[1]) ,
((rawADC[4]<<8) | rawADC[5]) ,
((rawADC[2]<<8) | rawADC[3]) );
#endif
#if defined (MAG3110)
MAG_ORIENTATION( ((rawADC[0]<<8) | rawADC[1]) ,
((rawADC[2]<<8) | rawADC[3]) ,
((rawADC[4]<<8) | rawADC[5]) );
#endif
}
#endif
#endif
#if defined(WMP) || defined(NUNCHUCK)
// ************************************************************************************************************
// I2C Wii Motion Plus + optional Nunchuk
// ************************************************************************************************************
// I2C adress 1: 0x53 (7bit)
// I2C adress 2: 0x52 (7bit)
// ************************************************************************************************************
#define WMP_ADDRESS_1 0x53
#define WMP_ADDRESS_2 0x52
void Gyro_init() {
delay(250);
i2c_writeReg(WMP_ADDRESS_1, 0xF0, 0x55); // Initialize Extension
delay(250);
i2c_writeReg(WMP_ADDRESS_1, 0xFE, 0x05); // Activate Nunchuck pass-through mode
delay(250);
}
void Gyro_getADC() {
uint8_t axis;
TWBR = ((F_CPU / I2C_SPEED) - 16) / 2; // change the I2C clock rate
i2c_getSixRawADC(WMP_ADDRESS_2,0x00);
if (micros() < (neutralizeTime + NEUTRALIZE_DELAY)) {//we neutralize data in case of blocking+hard reset state
for (axis = 0; axis < 3; axis++) {gyroADC[axis]=0;accADC[axis]=0;}
accADC[YAW] = acc_1G;
f.NUNCHUKDATA = 0;
}
// Wii Motion Plus Data
if ( (rawADC[5]&0x03) == 0x02 ) {
// Assemble 14bit data
gyroADC[ROLL] = - ( ((rawADC[5]>>2)<<8) | rawADC[2] ); //range: +/- 8192
gyroADC[PITCH] = - ( ((rawADC[4]>>2)<<8) | rawADC[1] );
gyroADC[YAW] = - ( ((rawADC[3]>>2)<<8) | rawADC[0] );
GYRO_Common();
// Check if slow bit is set and normalize to fast mode range
gyroADC[ROLL] = (rawADC[3]&0x01) ? gyroADC[ROLL]/5 : gyroADC[ROLL]; //the ratio 1/5 is not exactly the IDG600 or ISZ650 specification
gyroADC[PITCH] = (rawADC[4]&0x02)>>1 ? gyroADC[PITCH]/5 : gyroADC[PITCH]; //we detect here the slow of fast mode WMP gyros values (see wiibrew for more details)
gyroADC[YAW] = (rawADC[3]&0x02)>>1 ? gyroADC[YAW]/5 : gyroADC[YAW]; // this step must be done after zero compensation
f.NUNCHUKDATA = 0;
#if defined(NUNCHUCK)
} else if ( (rawADC[5]&0x03) == 0x00 ) { // Nunchuk Data
ACC_ORIENTATION( ( (rawADC[3]<<2) | ((rawADC[5]>>4)&0x02) ) ,
- ( (rawADC[2]<<2) | ((rawADC[5]>>3)&0x02) ) ,
( ((rawADC[4]>>1)<<3) | ((rawADC[5]>>5)&0x06) ) );
ACC_Common();
f.NUNCHUKDATA = 1;
#endif
}
}
#if defined(NUNCHUCK)
void ACC_init () {
// We need to set acc_1G for the Nunchuk beforehand
// If a different accelerometer is used, it will be overwritten by its ACC_init() later.
acc_1G = 200;
}
void ACC_getADC () { // it's done ine the WMP gyro part
Gyro_getADC();
}
#endif
#endif
#if defined(TINY_GPS) | defined(TINY_GPS_SONAR)
void tinygps_query(void) {
struct nav_data_t navi;
int16_t i2c_errors = i2c_errors_count;
/* copy GPS data to local struct */
i2c_read_to_buf(TINY_GPS_TWI_ADD, &navi, sizeof(navi));
/* did we generate any errors? */
if (i2c_errors == i2c_errors_count) {
#if defined(TINY_GPS)
GPS_numSat = navi.gps.sats;
f.GPS_FIX = (navi.gps.quality > 0);
GPS_coord[LAT] = (navi.gps.flags & 1<<NMEA_RMC_FLAGS_LAT_NORTH ? 1 : -1) * GPS_coord_to_decimal(&navi.gps.lat);
GPS_coord[LON] = (navi.gps.flags & 1<<NMEA_RMC_FLAGS_LON_EAST ? 1 : -1) * GPS_coord_to_decimal(&navi.gps.lon);
GPS_altitude = navi.gps.alt.m;
#endif
#if defined(TINY_GPS_SONAR)
sonarAlt = navi.sonar.distance;
#endif
}
}
#endif
// ************************************************************************************************************
// I2C Sonar SRF08
// ************************************************************************************************************
// first contribution from guru_florida (02-25-2012)
//
// specs are here: http://www.meas-spec.com/downloads/MS5611-01BA03.pdf
// useful info on pages 7 -> 12
#if defined(SRF02) || defined(SRF08) || defined(SRF10) || defined(SRC235)
// the default address for any new sensor found on the bus
// the code will move new sonars to the next available sonar address in range of F0-FE so that another
// sonar sensor can be added again.
// Thus, add only 1 sonar sensor at a time, poweroff, then wire the next, power on, wait for flashing light and repeat
#if !defined(SRF08_DEFAULT_ADDRESS)
#define SRF08_DEFAULT_ADDRESS 0x70
#endif
#if !defined(SRF08_RANGE_WAIT)
#define SRF08_RANGE_WAIT 80000 // delay between Ping and Range Read commands
#endif
#if !defined(SRF08_RANGE_SLEEP)
#define SRF08_RANGE_SLEEP 35000 // sleep this long before starting another Ping
#endif
#if !defined(SRF08_SENSOR_FIRST)
#define SRF08_SENSOR_FIRST 0xF0 // the first sensor i2c address (after it has been moved)
#endif
#if !defined(SRF08_MAX_SENSORS)
#define SRF08_MAX_SENSORS 4 // maximum number of sensors we'll allow (can go up to 8)
#endif
#define SONAR_MULTICAST_PING
// registers of the device
#define SRF08_REV_COMMAND 0
#define SRF08_LIGHT_GAIN 1
#define SRF08_ECHO_RANGE 2
static struct {
// sensor registers from the MS561101BA datasheet
int32_t range[SRF08_MAX_SENSORS];
int8_t sensors; // the number of sensors present
int8_t current; // the current sensor being read
uint8_t state;
uint32_t deadline;
} srf08_ctx;
// read uncompensated temperature value: send command first
void Sonar_init() {
memset(&srf08_ctx, 0, sizeof(srf08_ctx));
srf08_ctx.deadline = 4000000;
}
// this function works like readReg accept a failed read is a normal expectation
// use for testing the existence of sensors on the i2c bus
// a 0xffff code is returned if the read failed
uint16_t i2c_try_readReg(uint8_t add, uint8_t reg) {
uint16_t count = 255;
i2c_rep_start(add<<1); // I2C write direction
i2c_write(reg); // register selection
i2c_rep_start((add<<1)|1); // I2C read direction
TWCR = (1<<TWINT) | (1<<TWEN);
while (!(TWCR & (1<<TWINT))) {
count--;
if (count==0) { //we are in a blocking state => we don't insist
TWCR = 0; //and we force a reset on TWINT register
return 0xffff; // return failure to read
}
}
uint8_t r = TWDR;
i2c_stop();
return r;
}
// read a 16bit unsigned int from the i2c bus
uint16_t i2c_readReg16(int8_t addr, int8_t reg) {
uint8_t b[2];
i2c_read_reg_to_buf(addr, reg, &b, sizeof(b));
return (b[0]<<8) | b[1];
}
void i2c_srf08_change_addr(int8_t current, int8_t moveto) {
// to change a srf08 address, we must write the following sequence to the command register
// this sequence must occur as 4 seperate i2c transactions!!
// A0 AA A5 [addr]
i2c_writeReg(current, SRF08_REV_COMMAND, 0xA0); delay(30);
i2c_writeReg(current, SRF08_REV_COMMAND, 0xAA); delay(30);
i2c_writeReg(current, SRF08_REV_COMMAND, 0xA5); delay(30);
i2c_writeReg(current, SRF08_REV_COMMAND, moveto); delay(30); // now change i2c address
blinkLED(5,1,2);
#if defined(BUZZER)
alarmArray[7] = 2;
#endif
}
// discover previously known sensors and any new sensor (move new sensors to assigned area)
void i2c_srf08_discover() {
uint8_t addr;
uint16_t x;
// determine how many sensors are plugged in
srf08_ctx.sensors=0;
addr = SRF08_SENSOR_FIRST;
for(int i=0; i<SRF08_MAX_SENSORS && x!=0xff; i++) {
// read the revision as a way to check if sensor exists at this location
x = i2c_try_readReg(addr, SRF08_REV_COMMAND);
if(x!=0xffff) {
// detected a sensor at this address
srf08_ctx.sensors++;
addr += 2;
}
}
// do not add sensors if we are already maxed
if(srf08_ctx.sensors < SRF08_MAX_SENSORS) {