/
sensors_bmi088_bmp388.c
1020 lines (872 loc) · 29.8 KB
/
sensors_bmi088_bmp388.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
/**
* || ____ _ __
* +------+ / __ )(_) /_______________ _____ ___
* | 0xBC | / __ / / __/ ___/ ___/ __ `/_ / / _ \
* +------+ / /_/ / / /_/ /__/ / / /_/ / / /_/ __/
* || || /_____/_/\__/\___/_/ \__,_/ /___/\___/
*
* Crazyflie control firmware
*
* Copyright (C) 2011-2018 Bitcraze AB
*
* 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, in version 3.
*
* 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/>.
*
* sensors_bmi088_bmp388.c: IMU sensor driver for the *88 bosch sensors
*/
#define DEBUG_MODULE "IMU"
#include <math.h>
#include "sensors_bmi088_bmp388.h"
#include "stm32fxxx.h"
#include "imu.h"
#include "FreeRTOS.h"
#include "semphr.h"
#include "task.h"
#include "system.h"
#include "configblock.h"
#include "param.h"
#include "log.h"
#include "debug.h"
#include "nvicconf.h"
#include "ledseq.h"
#include "sound.h"
#include "filter.h"
#include "i2cdev.h"
#include "bmi088.h"
#include "bmp3.h"
#include "bstdr_types.h"
#include "static_mem.h"
#include "estimator.h"
#include "sensors_bmi088_common.h"
#include "platform_defaults.h"
#define GYRO_ADD_RAW_AND_VARIANCE_LOG_VALUES
#define SENSORS_READ_RATE_HZ 1000
#define SENSORS_STARTUP_TIME_MS 1000
#define SENSORS_READ_BARO_HZ 50
#define SENSORS_READ_MAG_HZ 20
#define SENSORS_DELAY_BARO (SENSORS_READ_RATE_HZ/SENSORS_READ_BARO_HZ)
#define SENSORS_DELAY_MAG (SENSORS_READ_RATE_HZ/SENSORS_READ_MAG_HZ)
#define SENSORS_BMI088_GYRO_FS_CFG BMI088_GYRO_RANGE_2000_DPS
#define SENSORS_BMI088_DEG_PER_LSB_CFG (2.0f *2000.0f) / 65536.0f
#define SENSORS_BMI088_ACCEL_CFG 24
#define SENSORS_BMI088_ACCEL_FS_CFG BMI088_ACCEL_RANGE_24G
#define SENSORS_BMI088_G_PER_LSB_CFG (2.0f * (float)SENSORS_BMI088_ACCEL_CFG) / 65536.0f
#define SENSORS_BMI088_1G_IN_LSB (65536 / SENSORS_BMI088_ACCEL_CFG / 2)
#define SENSORS_VARIANCE_MAN_TEST_TIMEOUT M2T(1000) // Timeout in ms
#define SENSORS_MAN_TEST_LEVEL_MAX 5.0f // Max degrees off
#define GYRO_NBR_OF_AXES 3
#define GYRO_MIN_BIAS_TIMEOUT_MS M2T(1*1000)
// Number of samples used in variance calculation. Changing this effects the threshold
#define SENSORS_NBR_OF_BIAS_SAMPLES 512
// Variance threshold to take zero bias for gyro
#define GYRO_VARIANCE_BASE 100
#define GYRO_VARIANCE_THRESHOLD_X (GYRO_VARIANCE_BASE)
#define GYRO_VARIANCE_THRESHOLD_Y (GYRO_VARIANCE_BASE)
#define GYRO_VARIANCE_THRESHOLD_Z (GYRO_VARIANCE_BASE)
#define SENSORS_ACC_SCALE_SAMPLES 200
typedef struct
{
Axis3f bias;
Axis3f variance;
Axis3f mean;
bool isBiasValueFound;
bool isBufferFilled;
Axis3i16* bufHead;
Axis3i16 buffer[SENSORS_NBR_OF_BIAS_SAMPLES];
} BiasObj;
/* initialize necessary variables */
static struct bmi088_dev bmi088Dev;
static struct bmp3_dev bmp388Dev;
static xQueueHandle accelerometerDataQueue;
STATIC_MEM_QUEUE_ALLOC(accelerometerDataQueue, 1, sizeof(Axis3f));
static xQueueHandle gyroDataQueue;
STATIC_MEM_QUEUE_ALLOC(gyroDataQueue, 1, sizeof(Axis3f));
static xQueueHandle magnetometerDataQueue;
STATIC_MEM_QUEUE_ALLOC(magnetometerDataQueue, 1, sizeof(Axis3f));
static xQueueHandle barometerDataQueue;
STATIC_MEM_QUEUE_ALLOC(barometerDataQueue, 1, sizeof(baro_t));
static xSemaphoreHandle sensorsDataReady;
static StaticSemaphore_t sensorsDataReadyBuffer;
static xSemaphoreHandle dataReady;
static StaticSemaphore_t dataReadyBuffer;
static bool isInit = false;
static sensorData_t sensorData;
static volatile uint64_t imuIntTimestamp;
static Axis3i16 gyroRaw;
static Axis3i16 accelRaw;
NO_DMA_CCM_SAFE_ZERO_INIT static BiasObj gyroBiasRunning;
static Axis3f gyroBias;
#if defined(SENSORS_GYRO_BIAS_CALCULATE_STDDEV) && defined (GYRO_BIAS_LIGHT_WEIGHT)
static Axis3f gyroBiasStdDev;
#endif
static bool gyroBiasFound = false;
static float accScaleSum = 0;
static float accScale = 1;
static bool accScaleFound = false;
static uint32_t accScaleSumCount = 0;
// Low Pass filtering
#define GYRO_LPF_CUTOFF_FREQ 80
#define ACCEL_LPF_CUTOFF_FREQ 30
static lpf2pData accLpf[3];
static lpf2pData gyroLpf[3];
static void applyAxis3fLpf(lpf2pData *data, Axis3f* in);
static bool isBarometerPresent = false;
static uint8_t baroMeasDelayMin = SENSORS_DELAY_BARO;
// IMU alignment Euler angles
static float imuPhi = IMU_PHI;
static float imuTheta = IMU_THETA;
static float imuPsi = IMU_PSI;
static float R[3][3];
// Pre-calculated values for accelerometer alignment
static float cosPitch;
static float sinPitch;
static float cosRoll;
static float sinRoll;
#ifdef GYRO_GYRO_BIAS_LIGHT_WEIGHT
static bool processGyroBiasNoBuffer(int16_t gx, int16_t gy, int16_t gz, Axis3f *gyroBiasOut);
#else
static bool processGyroBias(int16_t gx, int16_t gy, int16_t gz, Axis3f *gyroBiasOut);
#endif
static bool processAccScale(int16_t ax, int16_t ay, int16_t az);
static void sensorsBiasObjInit(BiasObj* bias);
static void sensorsCalculateVarianceAndMean(BiasObj* bias, Axis3f* varOut, Axis3f* meanOut);
static void sensorsCalculateBiasMean(BiasObj* bias, Axis3i32* meanOut);
static void sensorsAddBiasValue(BiasObj* bias, int16_t x, int16_t y, int16_t z);
static bool sensorsFindBiasValue(BiasObj* bias);
static void sensorsAlignToAirframe(Axis3f* in, Axis3f* out);
static void sensorsAccAlignToGravity(Axis3f* in, Axis3f* out);
STATIC_MEM_TASK_ALLOC(sensorsTask, SENSORS_TASK_STACKSIZE);
// Communication routines
/*!
* @brief Generic burst read
*
* @param [out] dev_id I2C address, SPI chip select or user desired identifier
*
* @return Zero if successful, otherwise an error code
*/
bstdr_ret_t bmi088_burst_read(uint8_t dev_id, uint8_t reg_addr, uint8_t *reg_data, uint16_t len)
{
/**< Burst read code comes here */
if (i2cdevReadReg8(I2C3_DEV, dev_id, reg_addr, (uint16_t) len, reg_data))
{
return BSTDR_OK;
}
else
{
return BSTDR_E_CON_ERROR;
}
}
/*!
* @brief Generic burst write
*
* @param [out] dev_id I2C address, SPI chip select or user desired identifier
*
* @return Zero if successful, otherwise an error code
*/
bstdr_ret_t bmi088_burst_write(uint8_t dev_id, uint8_t reg_addr, uint8_t *reg_data, uint16_t len)
{
/**< Burst write code comes here */
if (i2cdevWriteReg8(I2C3_DEV, dev_id,reg_addr,(uint16_t) len, reg_data))
{
return BSTDR_OK;
}
else
{
return BSTDR_E_CON_ERROR;
}
}
/*!
* @brief Generic burst read
*
* @param [in] period Delay period in milliseconds
*
* @return None
*/
void bmi088_ms_delay(uint32_t period)
{
/**< Delay code comes */
vTaskDelay(M2T(period)); // Delay a while to let the device stabilize
}
static uint16_t sensorsGyroGet(Axis3i16* dataOut)
{
return bmi088_get_gyro_data((struct bmi088_sensor_data*)dataOut, &bmi088Dev);
}
static void sensorsAccelGet(Axis3i16* dataOut)
{
bmi088_get_accel_data((struct bmi088_sensor_data*)dataOut, &bmi088Dev);
}
static void sensorsScaleBaro(baro_t* baroScaled, float pressure,
float temperature)
{
baroScaled->pressure = pressure*0.01f;
baroScaled->temperature = temperature;
baroScaled->asl = ((powf((1015.7f / baroScaled->pressure), 0.1902630958f)
- 1.0f) * (25.0f + 273.15f)) / 0.0065f;
}
bool sensorsBmi088Bmp388ReadGyro(Axis3f *gyro)
{
return (pdTRUE == xQueueReceive(gyroDataQueue, gyro, 0));
}
bool sensorsBmi088Bmp388ReadAcc(Axis3f *acc)
{
return (pdTRUE == xQueueReceive(accelerometerDataQueue, acc, 0));
}
bool sensorsBmi088Bmp388ReadMag(Axis3f *mag)
{
return (pdTRUE == xQueueReceive(magnetometerDataQueue, mag, 0));
}
bool sensorsBmi088Bmp388ReadBaro(baro_t *baro)
{
return (pdTRUE == xQueueReceive(barometerDataQueue, baro, 0));
}
void sensorsBmi088Bmp388Acquire(sensorData_t *sensors, const uint32_t tick)
{
sensorsReadGyro(&sensors->gyro);
sensorsReadAcc(&sensors->acc);
sensorsReadMag(&sensors->mag);
sensorsReadBaro(&sensors->baro);
sensors->interruptTimestamp = sensorData.interruptTimestamp;
}
bool sensorsBmi088Bmp388AreCalibrated()
{
return gyroBiasFound;
}
static void sensorsTask(void *param)
{
systemWaitStart();
Axis3f gyroScaledIMU;
Axis3f accScaledIMU;
Axis3f accScaled;
measurement_t measurement;
/* wait an additional second the keep bus free
* this is only required by the z-ranger, since the
* configuration will be done after system start-up */
//vTaskDelayUntil(&lastWakeTime, M2T(1500));
while (1)
{
if (pdTRUE == xSemaphoreTake(sensorsDataReady, portMAX_DELAY))
{
sensorData.interruptTimestamp = imuIntTimestamp;
/* get data from chosen sensors */
sensorsGyroGet(&gyroRaw);
sensorsAccelGet(&accelRaw);
/* calibrate if necessary */
#ifdef GYRO_BIAS_LIGHT_WEIGHT
gyroBiasFound = processGyroBiasNoBuffer(gyroRaw.x, gyroRaw.y, gyroRaw.z, &gyroBias);
#else
gyroBiasFound = processGyroBias(gyroRaw.x, gyroRaw.y, gyroRaw.z, &gyroBias);
#endif
if (gyroBiasFound)
{
processAccScale(accelRaw.x, accelRaw.y, accelRaw.z);
}
/* Gyro */
gyroScaledIMU.x = (gyroRaw.x - gyroBias.x) * SENSORS_BMI088_DEG_PER_LSB_CFG;
gyroScaledIMU.y = (gyroRaw.y - gyroBias.y) * SENSORS_BMI088_DEG_PER_LSB_CFG;
gyroScaledIMU.z = (gyroRaw.z - gyroBias.z) * SENSORS_BMI088_DEG_PER_LSB_CFG;
sensorsAlignToAirframe(&gyroScaledIMU, &sensorData.gyro);
applyAxis3fLpf((lpf2pData*)(&gyroLpf), &sensorData.gyro);
measurement.type = MeasurementTypeGyroscope;
measurement.data.gyroscope.gyro = sensorData.gyro;
estimatorEnqueue(&measurement);
/* Acelerometer */
accScaledIMU.x = accelRaw.x * SENSORS_BMI088_G_PER_LSB_CFG / accScale;
accScaledIMU.y = accelRaw.y * SENSORS_BMI088_G_PER_LSB_CFG / accScale;
accScaledIMU.z = accelRaw.z * SENSORS_BMI088_G_PER_LSB_CFG / accScale;
sensorsAlignToAirframe(&accScaledIMU, &accScaled);
sensorsAccAlignToGravity(&accScaled, &sensorData.acc);
applyAxis3fLpf((lpf2pData*)(&accLpf), &sensorData.acc);
measurement.type = MeasurementTypeAcceleration;
measurement.data.acceleration.acc = sensorData.acc;
estimatorEnqueue(&measurement);
}
if (isBarometerPresent)
{
static uint8_t baroMeasDelay = SENSORS_DELAY_BARO;
if (--baroMeasDelay == 0)
{
uint8_t sensor_comp = BMP3_PRESS | BMP3_TEMP;
struct bmp3_data data;
baro_t* baro388 = &sensorData.baro;
/* Temperature and Pressure data are read and stored in the bmp3_data instance */
bmp3_get_sensor_data(sensor_comp, &data, &bmp388Dev);
sensorsScaleBaro(baro388, data.pressure, data.temperature);
measurement.type = MeasurementTypeBarometer;
measurement.data.barometer.baro = sensorData.baro;
estimatorEnqueue(&measurement);
baroMeasDelay = baroMeasDelayMin;
}
}
xQueueOverwrite(accelerometerDataQueue, &sensorData.acc);
xQueueOverwrite(gyroDataQueue, &sensorData.gyro);
if (isBarometerPresent)
{
xQueueOverwrite(barometerDataQueue, &sensorData.baro);
}
xSemaphoreGive(dataReady);
}
}
void sensorsBmi088Bmp388WaitDataReady(void)
{
xSemaphoreTake(dataReady, portMAX_DELAY);
}
static void sensorsDeviceInit(void)
{
if (isInit)
return;
bstdr_ret_t rslt;
isBarometerPresent = false;
// Wait for sensors to startup
vTaskDelay(M2T(SENSORS_STARTUP_TIME_MS));
/* BMI088
* The bmi088Dev structure should have been filled in by the backend
* (i2c/spi) drivers at this point.
*/
/* BMI088 GYRO */
rslt = bmi088_gyro_init(&bmi088Dev); // initialize the device
if (rslt == BSTDR_OK)
{
struct bmi088_int_cfg intConfig;
DEBUG_PRINT("BMI088 Gyro connection [OK].\n");
/* set power mode of gyro */
bmi088Dev.gyro_cfg.power = BMI088_GYRO_PM_NORMAL;
rslt |= bmi088_set_gyro_power_mode(&bmi088Dev);
/* set bandwidth and range of gyro */
bmi088Dev.gyro_cfg.bw = BMI088_GYRO_BW_116_ODR_1000_HZ;
bmi088Dev.gyro_cfg.range = SENSORS_BMI088_GYRO_FS_CFG;
bmi088Dev.gyro_cfg.odr = BMI088_GYRO_BW_116_ODR_1000_HZ;
rslt |= bmi088_set_gyro_meas_conf(&bmi088Dev);
intConfig.gyro_int_channel = BMI088_INT_CHANNEL_3;
intConfig.gyro_int_type = BMI088_GYRO_DATA_RDY_INT;
intConfig.gyro_int_pin_3_cfg.enable_int_pin = 1;
intConfig.gyro_int_pin_3_cfg.lvl = 1;
intConfig.gyro_int_pin_3_cfg.output_mode = 0;
/* Setting the interrupt configuration */
rslt = bmi088_set_gyro_int_config(&intConfig, &bmi088Dev);
bmi088Dev.delay_ms(50);
struct bmi088_sensor_data gyr;
rslt |= bmi088_get_gyro_data(&gyr, &bmi088Dev);
}
else
{
#ifndef SENSORS_IGNORE_IMU_FAIL
DEBUG_PRINT("BMI088 Gyro connection [FAIL]\n");
isInit = false;
#endif
}
/* BMI088 ACCEL */
rslt |= bmi088_accel_switch_control(&bmi088Dev, BMI088_ACCEL_POWER_ENABLE);
bmi088Dev.delay_ms(5);
rslt = bmi088_accel_init(&bmi088Dev); // initialize the device
if (rslt == BSTDR_OK)
{
DEBUG_PRINT("BMI088 Accel connection [OK]\n");
/* set power mode of accel */
bmi088Dev.accel_cfg.power = BMI088_ACCEL_PM_ACTIVE;
rslt |= bmi088_set_accel_power_mode(&bmi088Dev);
bmi088Dev.delay_ms(10);
/* set bandwidth and range of accel */
bmi088Dev.accel_cfg.bw = BMI088_ACCEL_BW_OSR4;
bmi088Dev.accel_cfg.range = SENSORS_BMI088_ACCEL_FS_CFG;
bmi088Dev.accel_cfg.odr = BMI088_ACCEL_ODR_1600_HZ;
rslt |= bmi088_set_accel_meas_conf(&bmi088Dev);
struct bmi088_sensor_data acc;
rslt |= bmi088_get_accel_data(&acc, &bmi088Dev);
}
else
{
#ifndef SENSORS_IGNORE_IMU_FAIL
DEBUG_PRINT("BMI088 Accel connection [FAIL]\n");
isInit = false;
#endif
}
/* BMP388 */
bmp388Dev.dev_id = BMP3_I2C_ADDR_SEC;
bmp388Dev.intf = BMP3_I2C_INTF;
bmp388Dev.read = bmi088_burst_read;
bmp388Dev.write = bmi088_burst_write;
bmp388Dev.delay_ms = bmi088_ms_delay;
int i = 3;
do {
bmp388Dev.delay_ms(1);
// For some reason it often doesn't work first time
rslt = bmp3_init(&bmp388Dev);
} while (rslt != BMP3_OK && i-- > 0);
if (rslt == BMP3_OK)
{
isBarometerPresent = true;
DEBUG_PRINT("BMP388 I2C connection [OK]\n");
/* Used to select the settings user needs to change */
uint16_t settings_sel;
/* Select the pressure and temperature sensor to be enabled */
bmp388Dev.settings.press_en = BMP3_ENABLE;
bmp388Dev.settings.temp_en = BMP3_ENABLE;
/* Select the output data rate and oversampling settings for pressure and temperature */
bmp388Dev.settings.odr_filter.press_os = BMP3_OVERSAMPLING_8X;
bmp388Dev.settings.odr_filter.temp_os = BMP3_NO_OVERSAMPLING;
bmp388Dev.settings.odr_filter.odr = BMP3_ODR_50_HZ;
bmp388Dev.settings.odr_filter.iir_filter = BMP3_IIR_FILTER_COEFF_3;
/* Assign the settings which needs to be set in the sensor */
settings_sel = BMP3_PRESS_EN_SEL | BMP3_TEMP_EN_SEL | BMP3_PRESS_OS_SEL | BMP3_TEMP_OS_SEL | BMP3_ODR_SEL | BMP3_IIR_FILTER_SEL;
rslt = bmp3_set_sensor_settings(settings_sel, &bmp388Dev);
/* Set the power mode to normal mode */
bmp388Dev.settings.op_mode = BMP3_NORMAL_MODE;
rslt = bmp3_set_op_mode(&bmp388Dev);
bmp388Dev.delay_ms(20); // wait before first read out
// read out data
/* Variable used to select the sensor component */
uint8_t sensor_comp;
/* Variable used to store the compensated data */
struct bmp3_data data;
/* Sensor component selection */
sensor_comp = BMP3_PRESS | BMP3_TEMP;
/* Temperature and Pressure data are read and stored in the bmp3_data instance */
rslt = bmp3_get_sensor_data(sensor_comp, &data, &bmp388Dev);
/* Print the temperature and pressure data */
// DEBUG_PRINT("BMP388 T:%0.2f P:%0.2f\n",data.temperature, data.pressure/100.0f);
baroMeasDelayMin = SENSORS_DELAY_BARO;
}
else
{
#ifndef CONFIG_SENSORS_IGNORE_BAROMETER_FAIL
DEBUG_PRINT("BMP388 I2C connection [FAIL]\n");
isInit = false;
return;
#endif
}
// Init second order filer for accelerometer and gyro
for (uint8_t i = 0; i < 3; i++)
{
lpf2pInit(&gyroLpf[i], 1000, GYRO_LPF_CUTOFF_FREQ);
lpf2pInit(&accLpf[i], 1000, ACCEL_LPF_CUTOFF_FREQ);
}
cosPitch = cosf(configblockGetCalibPitch() * (float) M_PI / 180);
sinPitch = sinf(configblockGetCalibPitch() * (float) M_PI / 180);
cosRoll = cosf(configblockGetCalibRoll() * (float) M_PI / 180);
sinRoll = sinf(configblockGetCalibRoll() * (float) M_PI / 180);
isInit = true;
}
static void sensorsTaskInit(void)
{
accelerometerDataQueue = STATIC_MEM_QUEUE_CREATE(accelerometerDataQueue);
gyroDataQueue = STATIC_MEM_QUEUE_CREATE(gyroDataQueue);
magnetometerDataQueue = STATIC_MEM_QUEUE_CREATE(magnetometerDataQueue);
barometerDataQueue = STATIC_MEM_QUEUE_CREATE(barometerDataQueue);
STATIC_MEM_TASK_CREATE(sensorsTask, sensorsTask, SENSORS_TASK_NAME, NULL, SENSORS_TASK_PRI);
}
static void sensorsInterruptInit(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
EXTI_InitTypeDef EXTI_InitStructure;
sensorsDataReady = xSemaphoreCreateBinaryStatic(&sensorsDataReadyBuffer);
dataReady = xSemaphoreCreateBinaryStatic(&dataReadyBuffer);
// Enable the interrupt on PC14
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_14;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL; //GPIO_PuPd_DOWN;
GPIO_Init(GPIOC, &GPIO_InitStructure);
SYSCFG_EXTILineConfig(EXTI_PortSourceGPIOC, EXTI_PinSource14);
EXTI_InitStructure.EXTI_Line = EXTI_Line14;
EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt;
EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Rising;
EXTI_InitStructure.EXTI_LineCmd = ENABLE;
portDISABLE_INTERRUPTS();
EXTI_Init(&EXTI_InitStructure);
EXTI_ClearITPendingBit(EXTI_Line14);
portENABLE_INTERRUPTS();
}
static void sensorsBmi088Bmp388Init(void)
{
sensorsBiasObjInit(&gyroBiasRunning);
sensorsDeviceInit();
sensorsInterruptInit();
sensorsTaskInit();
}
void sensorsBmi088Bmp388Init_SPI(void)
{
if (isInit)
{
return;
}
DEBUG_PRINT("BMI088: Using SPI interface.\n");
sensorsBmi088_SPI_deviceInit(&bmi088Dev);
sensorsBmi088Bmp388Init();
}
void sensorsBmi088Bmp388Init_I2C(void)
{
if (isInit)
{
return;
}
DEBUG_PRINT("BMI088: Using I2C interface.\n");
sensorsBmi088_I2C_deviceInit(&bmi088Dev);
sensorsBmi088Bmp388Init();
}
static bool gyroSelftest()
{
bool testStatus = true;
int i = 3;
uint16_t readResult = BMI088_OK;
do {
// For some reason it often doesn't work first time on the Roadrunner
readResult = sensorsGyroGet(&gyroRaw);
} while (readResult != BMI088_OK && i-- > 0);
if ((readResult != BMI088_OK) || (gyroRaw.x == 0 && gyroRaw.y == 0 && gyroRaw.z == 0))
{
DEBUG_PRINT("BMI088 gyro returning x=0 y=0 z=0 [FAILED]\n");
testStatus = false;
}
int8_t gyroResult = 0;
bmi088_perform_gyro_selftest(&gyroResult, &bmi088Dev);
if (gyroResult == BMI088_SELFTEST_PASS)
{
DEBUG_PRINT("BMI088 gyro self-test [OK]\n");
}
else
{
DEBUG_PRINT("BMI088 gyro self-test [FAILED]\n");
testStatus = false;
}
return testStatus;
}
bool sensorsBmi088Bmp388Test(void)
{
bool testStatus = true;
if (!isInit)
{
DEBUG_PRINT("Uninitialized\n");
testStatus = false;
}
if (! gyroSelftest())
{
testStatus = false;
}
return testStatus;
}
/**
* Calculates accelerometer scale out of SENSORS_ACC_SCALE_SAMPLES samples. Should be called when
* platform is stable.
*/
static bool processAccScale(int16_t ax, int16_t ay, int16_t az)
{
if (!accScaleFound)
{
accScaleSum += sqrtf(powf(ax * SENSORS_BMI088_G_PER_LSB_CFG, 2) + powf(ay * SENSORS_BMI088_G_PER_LSB_CFG, 2) + powf(az * SENSORS_BMI088_G_PER_LSB_CFG, 2));
accScaleSumCount++;
if (accScaleSumCount == SENSORS_ACC_SCALE_SAMPLES)
{
accScale = accScaleSum / SENSORS_ACC_SCALE_SAMPLES;
accScaleFound = true;
}
}
return accScaleFound;
}
#ifdef GYRO_BIAS_LIGHT_WEIGHT
#define SENSORS_BIAS_SAMPLES 1000
/**
* Calculates the bias out of the first SENSORS_BIAS_SAMPLES gathered. Requires no buffer
* but needs platform to be stable during startup.
*/
static bool processGyroBiasNoBuffer(int16_t gx, int16_t gy, int16_t gz, Axis3f *gyroBiasOut)
{
static uint32_t gyroBiasSampleCount = 0;
static bool gyroBiasNoBuffFound = false;
static Axis3i64 gyroBiasSampleSum;
static Axis3i64 gyroBiasSampleSumSquares;
if (!gyroBiasNoBuffFound)
{
// If the gyro has not yet been calibrated:
// Add the current sample to the running mean and variance
gyroBiasSampleSum.x += gx;
gyroBiasSampleSum.y += gy;
gyroBiasSampleSum.z += gz;
#ifdef SENSORS_GYRO_BIAS_CALCULATE_STDDEV
gyroBiasSampleSumSquares.x += gx * gx;
gyroBiasSampleSumSquares.y += gy * gy;
gyroBiasSampleSumSquares.z += gz * gz;
#endif
gyroBiasSampleCount += 1;
// If we then have enough samples, calculate the mean and standard deviation
if (gyroBiasSampleCount == SENSORS_BIAS_SAMPLES)
{
gyroBiasOut->x = (float)(gyroBiasSampleSum.x) / SENSORS_BIAS_SAMPLES;
gyroBiasOut->y = (float)(gyroBiasSampleSum.y) / SENSORS_BIAS_SAMPLES;
gyroBiasOut->z = (float)(gyroBiasSampleSum.z) / SENSORS_BIAS_SAMPLES;
#ifdef SENSORS_GYRO_BIAS_CALCULATE_STDDEV
gyroBiasStdDev.x = sqrtf((float)(gyroBiasSampleSumSquares.x) / SENSORS_BIAS_SAMPLES - (gyroBiasOut->x * gyroBiasOut->x));
gyroBiasStdDev.y = sqrtf((float)(gyroBiasSampleSumSquares.y) / SENSORS_BIAS_SAMPLES - (gyroBiasOut->y * gyroBiasOut->y));
gyroBiasStdDev.z = sqrtf((float)(gyroBiasSampleSumSquares.z) / SENSORS_BIAS_SAMPLES - (gyroBiasOut->z * gyroBiasOut->z));
#endif
gyroBiasNoBuffFound = true;
}
}
return gyroBiasNoBuffFound;
}
#else
/**
* Calculates the bias first when the gyro variance is below threshold. Requires a buffer
* but calibrates platform first when it is stable.
*/
static bool processGyroBias(int16_t gx, int16_t gy, int16_t gz, Axis3f *gyroBiasOut)
{
sensorsAddBiasValue(&gyroBiasRunning, gx, gy, gz);
if (!gyroBiasRunning.isBiasValueFound)
{
sensorsFindBiasValue(&gyroBiasRunning);
if (gyroBiasRunning.isBiasValueFound)
{
soundSetEffect(SND_CALIB);
ledseqRun(&seq_calibrated);
}
}
gyroBiasOut->x = gyroBiasRunning.bias.x;
gyroBiasOut->y = gyroBiasRunning.bias.y;
gyroBiasOut->z = gyroBiasRunning.bias.z;
return gyroBiasRunning.isBiasValueFound;
}
#endif
static void sensorsBiasObjInit(BiasObj* bias)
{
bias->isBufferFilled = false;
bias->bufHead = bias->buffer;
}
/**
* Calculates the variance and mean for the bias buffer.
*/
static void sensorsCalculateVarianceAndMean(BiasObj* bias, Axis3f* varOut, Axis3f* meanOut)
{
uint32_t i;
int64_t sum[GYRO_NBR_OF_AXES] = {0};
int64_t sumSq[GYRO_NBR_OF_AXES] = {0};
for (i = 0; i < SENSORS_NBR_OF_BIAS_SAMPLES; i++)
{
sum[0] += bias->buffer[i].x;
sum[1] += bias->buffer[i].y;
sum[2] += bias->buffer[i].z;
sumSq[0] += bias->buffer[i].x * bias->buffer[i].x;
sumSq[1] += bias->buffer[i].y * bias->buffer[i].y;
sumSq[2] += bias->buffer[i].z * bias->buffer[i].z;
}
meanOut->x = (float) sum[0] / SENSORS_NBR_OF_BIAS_SAMPLES;
meanOut->y = (float) sum[1] / SENSORS_NBR_OF_BIAS_SAMPLES;
meanOut->z = (float) sum[2] / SENSORS_NBR_OF_BIAS_SAMPLES;
varOut->x = sumSq[0] / SENSORS_NBR_OF_BIAS_SAMPLES - meanOut->x * meanOut->x;
varOut->y = sumSq[1] / SENSORS_NBR_OF_BIAS_SAMPLES - meanOut->y * meanOut->y;
varOut->z = sumSq[2] / SENSORS_NBR_OF_BIAS_SAMPLES - meanOut->z * meanOut->z;
}
/**
* Calculates the mean for the bias buffer.
*/
static void __attribute__((used)) sensorsCalculateBiasMean(BiasObj* bias, Axis3i32* meanOut)
{
uint32_t i;
int32_t sum[GYRO_NBR_OF_AXES] = {0};
for (i = 0; i < SENSORS_NBR_OF_BIAS_SAMPLES; i++)
{
sum[0] += bias->buffer[i].x;
sum[1] += bias->buffer[i].y;
sum[2] += bias->buffer[i].z;
}
meanOut->x = sum[0] / SENSORS_NBR_OF_BIAS_SAMPLES;
meanOut->y = sum[1] / SENSORS_NBR_OF_BIAS_SAMPLES;
meanOut->z = sum[2] / SENSORS_NBR_OF_BIAS_SAMPLES;
}
/**
* Adds a new value to the variance buffer and if it is full
* replaces the oldest one. Thus a circular buffer.
*/
static void sensorsAddBiasValue(BiasObj* bias, int16_t x, int16_t y, int16_t z)
{
bias->bufHead->x = x;
bias->bufHead->y = y;
bias->bufHead->z = z;
bias->bufHead++;
if (bias->bufHead >= &bias->buffer[SENSORS_NBR_OF_BIAS_SAMPLES])
{
bias->bufHead = bias->buffer;
bias->isBufferFilled = true;
}
}
/**
* Checks if the variances is below the predefined thresholds.
* The bias value should have been added before calling this.
* @param bias The bias object
*/
static bool sensorsFindBiasValue(BiasObj* bias)
{
static int32_t varianceSampleTime;
bool foundBias = false;
if (bias->isBufferFilled)
{
sensorsCalculateVarianceAndMean(bias, &bias->variance, &bias->mean);
if (bias->variance.x < GYRO_VARIANCE_THRESHOLD_X &&
bias->variance.y < GYRO_VARIANCE_THRESHOLD_Y &&
bias->variance.z < GYRO_VARIANCE_THRESHOLD_Z &&
(varianceSampleTime + GYRO_MIN_BIAS_TIMEOUT_MS < xTaskGetTickCount()))
{
varianceSampleTime = xTaskGetTickCount();
bias->bias.x = bias->mean.x;
bias->bias.y = bias->mean.y;
bias->bias.z = bias->mean.z;
foundBias = true;
bias->isBiasValueFound = true;
}
}
return foundBias;
}
bool sensorsBmi088Bmp388ManufacturingTest(void)
{
bool testStatus = true;
if (! gyroSelftest())
{
testStatus = false;
}
int8_t accResult = 0;
bmi088_perform_accel_selftest(&accResult, &bmi088Dev);
if (accResult == BMI088_SELFTEST_PASS)
{
DEBUG_PRINT("BMI088 acc self-test [OK]\n");
}
else
{
DEBUG_PRINT("BMI088 acc self-test [FAILED]\n");
testStatus = false;
}
return testStatus;
}
/**
* Align the sensors to the Airframe axes
*/
static void sensorsAlignToAirframe(Axis3f* in, Axis3f* out)
{
// IMU alignment
static float sphi, cphi, stheta, ctheta, spsi, cpsi;
sphi = sinf(imuPhi * (float) M_PI / 180);
cphi = cosf(imuPhi * (float) M_PI / 180);
stheta = sinf(imuTheta * (float) M_PI / 180);
ctheta = cosf(imuTheta * (float) M_PI / 180);
spsi = sinf(imuPsi * (float) M_PI / 180);
cpsi = cosf(imuPsi * (float) M_PI / 180);
R[0][0] = ctheta * cpsi;
R[0][1] = ctheta * spsi;
R[0][2] = -stheta;
R[1][0] = sphi * stheta * cpsi - cphi * spsi;
R[1][1] = sphi * stheta * spsi + cphi * cpsi;
R[1][2] = sphi * ctheta;
R[2][0] = cphi * stheta * cpsi + sphi * spsi;
R[2][1] = cphi * stheta * spsi - sphi * cpsi;
R[2][2] = cphi * ctheta;
out->x = in->x*R[0][0] + in->y*R[0][1] + in->z*R[0][2];
out->y = in->x*R[1][0] + in->y*R[1][1] + in->z*R[1][2];
out->z = in->x*R[2][0] + in->y*R[2][1] + in->z*R[2][2];
}
/**
* Compensate for a miss-aligned accelerometer. It uses the trim
* data gathered from the UI and written in the config-block to
* rotate the accelerometer to be aligned with gravity.
*/
static void sensorsAccAlignToGravity(Axis3f* in, Axis3f* out)
{
Axis3f rx;
Axis3f ry;
// Rotate around x-axis
rx.x = in->x;
rx.y = in->y * cosRoll - in->z * sinRoll;
rx.z = in->y * sinRoll + in->z * cosRoll;
// Rotate around y-axis
ry.x = rx.x * cosPitch - rx.z * sinPitch;
ry.y = rx.y;
ry.z = -rx.x * sinPitch + rx.z * cosPitch;
out->x = ry.x;
out->y = ry.y;
out->z = ry.z;
}
void sensorsBmi088Bmp388SetAccMode(accModes accMode)
{
switch (accMode)
{
case ACC_MODE_PROPTEST:
// bmi088_accel_soft_reset(&bmi088Dev);
/* set bandwidth and range of accel (280Hz cut-off according to datasheet) */
bmi088Dev.accel_cfg.bw = BMI088_ACCEL_BW_NORMAL;
bmi088Dev.accel_cfg.range = SENSORS_BMI088_ACCEL_FS_CFG;
bmi088Dev.accel_cfg.odr = BMI088_ACCEL_ODR_1600_HZ;
if (bmi088_set_accel_meas_conf(&bmi088Dev) != BMI088_OK)
{
DEBUG_PRINT("ACC config [FAIL]\n");
}
for (uint8_t i = 0; i < 3; i++)
{
lpf2pInit(&accLpf[i], 1000, 500);
}
break;
case ACC_MODE_FLIGHT:
default:
/* set bandwidth and range of accel (145Hz cut-off according to datasheet) */
bmi088Dev.accel_cfg.bw = BMI088_ACCEL_BW_OSR4;
bmi088Dev.accel_cfg.range = SENSORS_BMI088_ACCEL_FS_CFG;
bmi088Dev.accel_cfg.odr = BMI088_ACCEL_ODR_1600_HZ;
if (bmi088_set_accel_meas_conf(&bmi088Dev) != BMI088_OK)
{
DEBUG_PRINT("ACC config [FAIL]\n");
}
for (uint8_t i = 0; i < 3; i++)
{
lpf2pInit(&accLpf[i], 1000, ACCEL_LPF_CUTOFF_FREQ);
}
break;
}
}
static void applyAxis3fLpf(lpf2pData *data, Axis3f* in)
{
for (uint8_t i = 0; i < 3; i++) {
in->axis[i] = lpf2pApply(&data[i], in->axis[i]);
}
}
void sensorsBmi088Bmp388DataAvailableCallback(void)
{
portBASE_TYPE xHigherPriorityTaskWoken = pdFALSE;
imuIntTimestamp = usecTimestamp();
xSemaphoreGiveFromISR(sensorsDataReady, &xHigherPriorityTaskWoken);
if (xHigherPriorityTaskWoken)
{
portYIELD();
}
}
#ifdef GYRO_ADD_RAW_AND_VARIANCE_LOG_VALUES
LOG_GROUP_START(gyro)
LOG_ADD(LOG_INT16, xRaw, &gyroRaw.x)
LOG_ADD(LOG_INT16, yRaw, &gyroRaw.y)
LOG_ADD(LOG_INT16, zRaw, &gyroRaw.z)
LOG_ADD(LOG_FLOAT, xVariance, &gyroBiasRunning.variance.x)
LOG_ADD(LOG_FLOAT, yVariance, &gyroBiasRunning.variance.y)
LOG_ADD(LOG_FLOAT, zVariance, &gyroBiasRunning.variance.z)
LOG_GROUP_STOP(gyro)
#endif
PARAM_GROUP_START(imu_sensors)
/**