mirror of
https://github.com/betaflight/betaflight.git
synced 2025-07-16 04:45:24 +03:00
Decouple sensor alignment from all acc/gyro/mag drivers.
This resulted in the removal of duplicate logic, duplicate code and the removal of a temporary buffer per-driver. The NAZE specific sensor alignment is now contained within the core code instead of in the drivers. The sensor alignment is determines by the sensor initialisation code. The alignment of sensor readings is now performed once and only by the appropriate sensor code, see usages of alignSensors(). The acc/gyro/compass driver code is now more reusable since it has no dependencies on the main code.
This commit is contained in:
parent
0f3e4add48
commit
297609d4c3
18 changed files with 124 additions and 185 deletions
|
@ -4,6 +4,7 @@
|
|||
#include "sensors_acceleration.h"
|
||||
#include "sensors_barometer.h"
|
||||
#include "sensors_gyro.h"
|
||||
#include "sensors_compass.h"
|
||||
|
||||
#include "sensors_common.h"
|
||||
|
||||
|
@ -38,12 +39,13 @@ void sensorsAutodetect(void)
|
|||
|
||||
// Autodetect gyro hardware. We have MPU3050 or MPU6050.
|
||||
if (mpu6050Detect(&acc, &gyro, mcfg.gyro_lpf)) {
|
||||
// this filled up acc.* struct with init values
|
||||
haveMpu6k = true;
|
||||
gyroAlign = CW0_DEG; // default NAZE alignment
|
||||
} else if (l3g4200dDetect(&gyro, mcfg.gyro_lpf)) {
|
||||
// well, we found our gyro
|
||||
;
|
||||
} else if (!mpu3050Detect(&gyro, mcfg.gyro_lpf)) {
|
||||
gyroAlign = CW0_DEG;
|
||||
} else if (mpu3050Detect(&gyro, mcfg.gyro_lpf)) {
|
||||
gyroAlign = CW0_DEG;
|
||||
} else {
|
||||
// if this fails, we get a beep + blink pattern. we're doomed, no gyro or i2c error.
|
||||
failureMode(3);
|
||||
}
|
||||
|
@ -58,8 +60,10 @@ retry:
|
|||
case ACC_ADXL345: // ADXL345
|
||||
acc_params.useFifo = false;
|
||||
acc_params.dataRate = 800; // unused currently
|
||||
if (adxl345Detect(&acc_params, &acc))
|
||||
if (adxl345Detect(&acc_params, &acc)) {
|
||||
accHardware = ACC_ADXL345;
|
||||
accAlign = CW270_DEG; // default NAZE alignment
|
||||
}
|
||||
if (mcfg.acc_hardware == ACC_ADXL345)
|
||||
break;
|
||||
; // fallthrough
|
||||
|
@ -67,6 +71,7 @@ retry:
|
|||
if (haveMpu6k) {
|
||||
mpu6050Detect(&acc, &gyro, mcfg.gyro_lpf); // yes, i'm rerunning it again. re-fill acc struct
|
||||
accHardware = ACC_MPU6050;
|
||||
accAlign = CW0_DEG; // default NAZE alignment
|
||||
if (mcfg.acc_hardware == ACC_MPU6050)
|
||||
break;
|
||||
}
|
||||
|
@ -75,6 +80,7 @@ retry:
|
|||
case ACC_MMA8452: // MMA8452
|
||||
if (mma8452Detect(&acc)) {
|
||||
accHardware = ACC_MMA8452;
|
||||
accAlign = CW90_DEG; // default NAZE alignment
|
||||
if (mcfg.acc_hardware == ACC_MMA8452)
|
||||
break;
|
||||
}
|
||||
|
@ -82,6 +88,7 @@ retry:
|
|||
case ACC_BMA280: // BMA280
|
||||
if (bma280Detect(&acc)) {
|
||||
accHardware = ACC_BMA280;
|
||||
accAlign = CW0_DEG; //
|
||||
if (mcfg.acc_hardware == ACC_BMA280)
|
||||
break;
|
||||
}
|
||||
|
@ -113,22 +120,27 @@ retry:
|
|||
|
||||
// Now time to init things, acc first
|
||||
if (sensors(SENSOR_ACC))
|
||||
acc.init(mcfg.acc_align);
|
||||
acc.init();
|
||||
// this is safe because either mpu6050 or mpu3050 or lg3d20 sets it, and in case of fail, we never get here.
|
||||
gyro.init(mcfg.gyro_align);
|
||||
gyro.init();
|
||||
|
||||
#ifdef MAG
|
||||
if (!hmc5883lDetect(mcfg.mag_align))
|
||||
if (hmc5883lDetect()) {
|
||||
magAlign = CW180_DEG; // default NAZE alignment
|
||||
} else {
|
||||
sensorsClear(SENSOR_MAG);
|
||||
}
|
||||
#endif
|
||||
|
||||
if (sensors(SENSOR_MAG)) {
|
||||
// calculate magnetic declination
|
||||
deg = cfg.mag_declination / 100;
|
||||
min = cfg.mag_declination % 100;
|
||||
if (sensors(SENSOR_MAG))
|
||||
deg = cfg.mag_declination / 100;
|
||||
min = cfg.mag_declination % 100;
|
||||
|
||||
magneticDeclination = (deg + ((float)min * (1.0f / 60.0f))) * 10; // heading is in 0.1deg units
|
||||
else
|
||||
magneticDeclination = 0.0f;
|
||||
} else {
|
||||
magneticDeclination = 0.0f; // TODO investigate if this is actually needed if there is no mag sensor or if the value stored in the config should be used.
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue