2 * This file is part of Cleanflight and Betaflight.
4 * Cleanflight and Betaflight are free software. You can redistribute
5 * this software and/or modify this software under the terms of the
6 * GNU General Public License as published by the Free Software
7 * Foundation, either version 3 of the License, or (at your option)
10 * Cleanflight and Betaflight are distributed in the hope that they
11 * will be useful, but WITHOUT ANY WARRANTY; without even the implied
12 * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
13 * See the GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this software.
18 * If not, see <http://www.gnu.org/licenses/>.
29 #include "build/debug.h"
31 #include "common/axis.h"
32 #include "common/maths.h"
33 #include "common/filter.h"
35 #include "config/config.h"
37 #include "drivers/accgyro/accgyro.h"
38 #include "drivers/accgyro/accgyro_fake.h"
39 #include "drivers/accgyro/accgyro_mpu.h"
40 #include "drivers/accgyro/accgyro_mpu3050.h"
41 #include "drivers/accgyro/accgyro_mpu6050.h"
42 #include "drivers/accgyro/accgyro_mpu6500.h"
43 #include "drivers/accgyro/accgyro_spi_bmi160.h"
44 #include "drivers/accgyro/accgyro_spi_bmi270.h"
45 #include "drivers/accgyro/accgyro_spi_icm20649.h"
46 #include "drivers/accgyro/accgyro_spi_icm20689.h"
47 #include "drivers/accgyro/accgyro_spi_icm20689.h"
48 #include "drivers/accgyro/accgyro_spi_icm426xx.h"
49 #include "drivers/accgyro/accgyro_spi_lsm6dso.h"
50 #include "drivers/accgyro/accgyro_spi_mpu6000.h"
51 #include "drivers/accgyro/accgyro_spi_mpu6500.h"
52 #include "drivers/accgyro/accgyro_spi_mpu9250.h"
54 #ifdef USE_GYRO_L3GD20
55 #include "drivers/accgyro/accgyro_spi_l3gd20.h"
58 #ifdef USE_GYRO_L3G4200D
59 #include "drivers/accgyro_legacy/accgyro_l3g4200d.h"
62 #include "drivers/accgyro/gyro_sync.h"
64 #include "fc/runtime_config.h"
66 #ifdef USE_DYN_NOTCH_FILTER
67 #include "flight/dyn_notch_filter.h"
70 #include "pg/gyrodev.h"
72 #include "sensors/gyro.h"
73 #include "sensors/sensors.h"
76 #define ACTIVE_GYRO ((gyro.gyroToUse == GYRO_CONFIG_USE_GYRO_2) ? &gyro.gyroSensor2 : &gyro.gyroSensor1)
78 #define ACTIVE_GYRO (&gyro.gyroSensor1)
81 // The gyro buffer is split 50/50, the first half for the transmit buffer, the second half for the receive buffer
82 // This buffer is large enough for the gyros currently supported in accgyro_mpu.c but should be reviewed id other
83 // gyro types are supported with SPI DMA.
84 #define GYRO_BUF_SIZE 32
86 static gyroDetectionFlags_t gyroDetectionFlags
= GYRO_NONE_MASK
;
88 static uint16_t calculateNyquistAdjustedNotchHz(uint16_t notchHz
, uint16_t notchCutoffHz
)
90 const uint32_t gyroFrequencyNyquist
= 1000000 / 2 / gyro
.targetLooptime
;
91 if (notchHz
> gyroFrequencyNyquist
) {
92 if (notchCutoffHz
< gyroFrequencyNyquist
) {
93 notchHz
= gyroFrequencyNyquist
;
102 static void gyroInitFilterNotch1(uint16_t notchHz
, uint16_t notchCutoffHz
)
104 gyro
.notchFilter1ApplyFn
= nullFilterApply
;
106 notchHz
= calculateNyquistAdjustedNotchHz(notchHz
, notchCutoffHz
);
108 if (notchHz
!= 0 && notchCutoffHz
!= 0) {
109 gyro
.notchFilter1ApplyFn
= (filterApplyFnPtr
)biquadFilterApply
;
110 const float notchQ
= filterGetNotchQ(notchHz
, notchCutoffHz
);
111 for (int axis
= 0; axis
< XYZ_AXIS_COUNT
; axis
++) {
112 biquadFilterInit(&gyro
.notchFilter1
[axis
], notchHz
, gyro
.targetLooptime
, notchQ
, FILTER_NOTCH
, 1.0f
);
117 static void gyroInitFilterNotch2(uint16_t notchHz
, uint16_t notchCutoffHz
)
119 gyro
.notchFilter2ApplyFn
= nullFilterApply
;
121 notchHz
= calculateNyquistAdjustedNotchHz(notchHz
, notchCutoffHz
);
123 if (notchHz
!= 0 && notchCutoffHz
!= 0) {
124 gyro
.notchFilter2ApplyFn
= (filterApplyFnPtr
)biquadFilterApply
;
125 const float notchQ
= filterGetNotchQ(notchHz
, notchCutoffHz
);
126 for (int axis
= 0; axis
< XYZ_AXIS_COUNT
; axis
++) {
127 biquadFilterInit(&gyro
.notchFilter2
[axis
], notchHz
, gyro
.targetLooptime
, notchQ
, FILTER_NOTCH
, 1.0f
);
132 static bool gyroInitLowpassFilterLpf(int slot
, int type
, uint16_t lpfHz
, uint32_t looptime
)
134 filterApplyFnPtr
*lowpassFilterApplyFn
;
135 gyroLowpassFilter_t
*lowpassFilter
= NULL
;
139 lowpassFilterApplyFn
= &gyro
.lowpassFilterApplyFn
;
140 lowpassFilter
= gyro
.lowpassFilter
;
144 lowpassFilterApplyFn
= &gyro
.lowpass2FilterApplyFn
;
145 lowpassFilter
= gyro
.lowpass2Filter
;
154 // Establish some common constants
155 const uint32_t gyroFrequencyNyquist
= 1000000 / 2 / looptime
;
156 const float gyroDt
= looptime
* 1e-6f
;
158 // Gain could be calculated a little later as it is specific to the pt1/bqrcf2/fkf branches
159 const float gain
= pt1FilterGain(lpfHz
, gyroDt
);
161 // Dereference the pointer to null before checking valid cutoff and filter
162 // type. It will be overridden for positive cases.
163 *lowpassFilterApplyFn
= nullFilterApply
;
165 // If lowpass cutoff has been specified
169 *lowpassFilterApplyFn
= (filterApplyFnPtr
) pt1FilterApply
;
170 for (int axis
= 0; axis
< XYZ_AXIS_COUNT
; axis
++) {
171 pt1FilterInit(&lowpassFilter
[axis
].pt1FilterState
, gain
);
176 if (lpfHz
<= gyroFrequencyNyquist
) {
178 *lowpassFilterApplyFn
= (filterApplyFnPtr
) biquadFilterApplyDF1
;
180 *lowpassFilterApplyFn
= (filterApplyFnPtr
) biquadFilterApply
;
182 for (int axis
= 0; axis
< XYZ_AXIS_COUNT
; axis
++) {
183 biquadFilterInitLPF(&lowpassFilter
[axis
].biquadFilterState
, lpfHz
, looptime
);
189 *lowpassFilterApplyFn
= (filterApplyFnPtr
) pt2FilterApply
;
190 for (int axis
= 0; axis
< XYZ_AXIS_COUNT
; axis
++) {
191 pt2FilterInit(&lowpassFilter
[axis
].pt2FilterState
, gain
);
196 *lowpassFilterApplyFn
= (filterApplyFnPtr
) pt3FilterApply
;
197 for (int axis
= 0; axis
< XYZ_AXIS_COUNT
; axis
++) {
198 pt3FilterInit(&lowpassFilter
[axis
].pt3FilterState
, gain
);
208 static void dynLpfFilterInit()
210 if (gyroConfig()->gyro_lpf1_dyn_min_hz
> 0) {
211 switch (gyroConfig()->gyro_lpf1_type
) {
213 gyro
.dynLpfFilter
= DYN_LPF_PT1
;
216 gyro
.dynLpfFilter
= DYN_LPF_BIQUAD
;
219 gyro
.dynLpfFilter
= DYN_LPF_PT2
;
222 gyro
.dynLpfFilter
= DYN_LPF_PT3
;
225 gyro
.dynLpfFilter
= DYN_LPF_NONE
;
229 gyro
.dynLpfFilter
= DYN_LPF_NONE
;
231 gyro
.dynLpfMin
= gyroConfig()->gyro_lpf1_dyn_min_hz
;
232 gyro
.dynLpfMax
= gyroConfig()->gyro_lpf1_dyn_max_hz
;
233 gyro
.dynLpfCurveExpo
= gyroConfig()->gyro_lpf1_dyn_expo
;
237 void gyroInitFilters(void)
239 uint16_t gyro_lpf1_init_hz
= gyroConfig()->gyro_lpf1_static_hz
;
242 if (gyroConfig()->gyro_lpf1_dyn_min_hz
> 0) {
243 gyro_lpf1_init_hz
= gyroConfig()->gyro_lpf1_dyn_min_hz
;
247 gyroInitLowpassFilterLpf(
249 gyroConfig()->gyro_lpf1_type
,
254 gyro
.downsampleFilterEnabled
= gyroInitLowpassFilterLpf(
256 gyroConfig()->gyro_lpf2_type
,
257 gyroConfig()->gyro_lpf2_static_hz
,
261 gyroInitFilterNotch1(gyroConfig()->gyro_soft_notch_hz_1
, gyroConfig()->gyro_soft_notch_cutoff_1
);
262 gyroInitFilterNotch2(gyroConfig()->gyro_soft_notch_hz_2
, gyroConfig()->gyro_soft_notch_cutoff_2
);
266 #ifdef USE_DYN_NOTCH_FILTER
267 dynNotchInit(dynNotchConfig(), gyro
.targetLooptime
);
270 const float k
= pt1FilterGain(GYRO_IMU_DOWNSAMPLE_CUTOFF_HZ
, gyro
.targetLooptime
);
271 for (int axis
= 0; axis
< XYZ_AXIS_COUNT
; axis
++) {
272 pt1FilterInit(&gyro
.imuGyroFilter
[axis
], k
);
276 #if defined(USE_GYRO_SLEW_LIMITER)
277 void gyroInitSlewLimiter(gyroSensor_t
*gyroSensor
)
280 for (int axis
= 0; axis
< XYZ_AXIS_COUNT
; axis
++) {
281 gyroSensor
->gyroDev
.gyroADCRawPrevious
[axis
] = 0;
286 static void gyroInitSensorFilters(gyroSensor_t
*gyroSensor
)
288 #if defined(USE_GYRO_SLEW_LIMITER)
289 gyroInitSlewLimiter(gyroSensor
);
295 void gyroInitSensor(gyroSensor_t
*gyroSensor
, const gyroDeviceConfig_t
*config
)
297 gyroSensor
->gyroDev
.gyro_high_fsr
= gyroConfig()->gyro_high_fsr
;
298 gyroSensor
->gyroDev
.gyroAlign
= config
->alignment
;
299 buildRotationMatrixFromAlignment(&config
->customAlignment
, &gyroSensor
->gyroDev
.rotationMatrix
);
300 gyroSensor
->gyroDev
.mpuIntExtiTag
= config
->extiTag
;
301 gyroSensor
->gyroDev
.hardware_lpf
= gyroConfig()->gyro_hardware_lpf
;
303 // The targetLooptime gets set later based on the active sensor's gyroSampleRateHz and pid_process_denom
304 gyroSensor
->gyroDev
.gyroSampleRateHz
= gyroSetSampleRate(&gyroSensor
->gyroDev
);
305 gyroSensor
->gyroDev
.initFn(&gyroSensor
->gyroDev
);
307 // As new gyros are supported, be sure to add them below based on whether they are subject to the overflow/inversion bug
308 // Any gyro not explicitly defined will default to not having built-in overflow protection as a safe alternative.
309 switch (gyroSensor
->gyroDev
.gyroHardware
) {
310 case GYRO_NONE
: // Won't ever actually get here, but included to account for all gyro types
323 gyroSensor
->gyroDev
.gyroHasOverflowProtection
= true;
329 case GYRO_ICM20649
: // we don't actually know if this is affected, but as there are currently no flight controllers using it we err on the side of caution
331 gyroSensor
->gyroDev
.gyroHasOverflowProtection
= false;
335 gyroSensor
->gyroDev
.gyroHasOverflowProtection
= false; // default catch for newly added gyros until proven to be unaffected
339 gyroInitSensorFilters(gyroSensor
);
342 STATIC_UNIT_TESTED gyroHardware_e
gyroDetect(gyroDev_t
*dev
)
344 gyroHardware_e gyroHardware
= GYRO_DEFAULT
;
346 switch (gyroHardware
) {
350 #ifdef USE_GYRO_MPU6050
352 if (mpu6050GyroDetect(dev
)) {
353 gyroHardware
= GYRO_MPU6050
;
359 #ifdef USE_GYRO_L3G4200D
361 if (l3g4200dDetect(dev
)) {
362 gyroHardware
= GYRO_L3G4200D
;
368 #ifdef USE_GYRO_MPU3050
370 if (mpu3050Detect(dev
)) {
371 gyroHardware
= GYRO_MPU3050
;
377 #ifdef USE_GYRO_L3GD20
379 if (l3gd20GyroDetect(dev
)) {
380 gyroHardware
= GYRO_L3GD20
;
386 #ifdef USE_GYRO_SPI_MPU6000
388 if (mpu6000SpiGyroDetect(dev
)) {
389 gyroHardware
= GYRO_MPU6000
;
395 #if defined(USE_GYRO_MPU6500) || defined(USE_GYRO_SPI_MPU6500)
400 #ifdef USE_GYRO_SPI_MPU6500
401 if (mpu6500GyroDetect(dev
) || mpu6500SpiGyroDetect(dev
)) {
403 if (mpu6500GyroDetect(dev
)) {
405 switch (dev
->mpuDetectionResult
.sensor
) {
407 gyroHardware
= GYRO_MPU9250
;
410 gyroHardware
= GYRO_ICM20601
;
413 gyroHardware
= GYRO_ICM20602
;
416 gyroHardware
= GYRO_ICM20608G
;
419 gyroHardware
= GYRO_MPU6500
;
426 #ifdef USE_GYRO_SPI_MPU9250
428 if (mpu9250SpiGyroDetect(dev
)) {
429 gyroHardware
= GYRO_MPU9250
;
435 #ifdef USE_GYRO_SPI_ICM20649
437 if (icm20649SpiGyroDetect(dev
)) {
438 gyroHardware
= GYRO_ICM20649
;
444 #ifdef USE_GYRO_SPI_ICM20689
446 if (icm20689SpiGyroDetect(dev
)) {
447 gyroHardware
= GYRO_ICM20689
;
453 #if defined(USE_GYRO_SPI_ICM42605) || defined(USE_GYRO_SPI_ICM42688P)
456 if (icm426xxSpiGyroDetect(dev
)) {
457 switch (dev
->mpuDetectionResult
.sensor
) {
459 gyroHardware
= GYRO_ICM42605
;
462 gyroHardware
= GYRO_ICM42688P
;
465 gyroHardware
= GYRO_NONE
;
473 #ifdef USE_ACCGYRO_BMI160
475 if (bmi160SpiGyroDetect(dev
)) {
476 gyroHardware
= GYRO_BMI160
;
482 #ifdef USE_ACCGYRO_BMI270
484 if (bmi270SpiGyroDetect(dev
)) {
485 gyroHardware
= GYRO_BMI270
;
491 #ifdef USE_ACCGYRO_LSM6DSO
493 if (lsm6dsoSpiGyroDetect(dev
)) {
494 gyroHardware
= GYRO_LSM6DSO
;
502 if (fakeGyroDetect(dev
)) {
503 gyroHardware
= GYRO_FAKE
;
510 gyroHardware
= GYRO_NONE
;
513 if (gyroHardware
!= GYRO_NONE
) {
514 sensorsSet(SENSOR_GYRO
);
521 static bool gyroDetectSensor(gyroSensor_t
*gyroSensor
, const gyroDeviceConfig_t
*config
)
523 #if defined(USE_GYRO_MPU6050) || defined(USE_GYRO_MPU3050) || defined(USE_GYRO_MPU6500) || defined(USE_GYRO_SPI_MPU6500) || defined(USE_GYRO_SPI_MPU6000) \
524 || defined(USE_ACC_MPU6050) || defined(USE_GYRO_SPI_MPU9250) || defined(USE_GYRO_SPI_ICM20601) || defined(USE_GYRO_SPI_ICM20649) \
525 || defined(USE_GYRO_SPI_ICM20689) || defined(USE_GYRO_L3GD20) || defined(USE_ACCGYRO_BMI160) || defined(USE_ACCGYRO_BMI270) || defined(USE_ACCGYRO_LSM6DSO) || defined(USE_GYRO_SPI_ICM42605) || defined(USE_GYRO_SPI_ICM42688P)
527 bool gyroFound
= mpuDetect(&gyroSensor
->gyroDev
, config
);
529 #if !defined(USE_FAKE_GYRO) // Allow resorting to fake accgyro if defined
540 const gyroHardware_e gyroHardware
= gyroDetect(&gyroSensor
->gyroDev
);
541 gyroSensor
->gyroDev
.gyroHardware
= gyroHardware
;
543 return gyroHardware
!= GYRO_NONE
;
546 static void gyroPreInitSensor(const gyroDeviceConfig_t
*config
)
548 #if defined(USE_GYRO_MPU6050) || defined(USE_GYRO_MPU3050) || defined(USE_GYRO_MPU6500) || defined(USE_GYRO_SPI_MPU6500) || defined(USE_GYRO_SPI_MPU6000) \
549 || defined(USE_ACC_MPU6050) || defined(USE_GYRO_SPI_MPU9250) || defined(USE_GYRO_SPI_ICM20601) || defined(USE_GYRO_SPI_ICM20649) \
550 || defined(USE_GYRO_SPI_ICM20689) || defined(USE_ACCGYRO_BMI160) || defined(USE_ACCGYRO_BMI270) || defined(USE_ACCGRYO_LSM6DSO)
557 void gyroPreInit(void)
559 gyroPreInitSensor(gyroDeviceConfig(0));
560 #ifdef USE_MULTI_GYRO
561 gyroPreInitSensor(gyroDeviceConfig(1));
567 #ifdef USE_GYRO_OVERFLOW_CHECK
568 if (gyroConfig()->checkOverflow
== GYRO_OVERFLOW_CHECK_YAW
) {
569 gyro
.overflowAxisMask
= GYRO_OVERFLOW_Z
;
570 } else if (gyroConfig()->checkOverflow
== GYRO_OVERFLOW_CHECK_ALL_AXES
) {
571 gyro
.overflowAxisMask
= GYRO_OVERFLOW_X
| GYRO_OVERFLOW_Y
| GYRO_OVERFLOW_Z
;
573 gyro
.overflowAxisMask
= 0;
577 gyro
.gyroDebugMode
= DEBUG_NONE
;
578 gyro
.useDualGyroDebugging
= false;
579 gyro
.gyroHasOverflowProtection
= true;
585 case DEBUG_GYRO_SCALED
:
586 case DEBUG_GYRO_FILTERED
:
588 case DEBUG_GYRO_SAMPLE
:
589 gyro
.gyroDebugMode
= debugMode
;
591 case DEBUG_DUAL_GYRO_DIFF
:
592 case DEBUG_DUAL_GYRO_RAW
:
593 case DEBUG_DUAL_GYRO_SCALED
:
594 gyro
.useDualGyroDebugging
= true;
598 gyroDetectionFlags
= GYRO_NONE_MASK
;
599 uint8_t gyrosToScan
= gyroConfig()->gyrosDetected
;
601 gyro
.gyroToUse
= gyroConfig()->gyro_to_use
;
602 gyro
.gyroDebugAxis
= gyroConfig()->gyro_filter_debug_axis
;
604 if ((!gyrosToScan
|| (gyrosToScan
& GYRO_1_MASK
)) && gyroDetectSensor(&gyro
.gyroSensor1
, gyroDeviceConfig(0))) {
605 gyroDetectionFlags
|= GYRO_1_MASK
;
608 #if defined(USE_MULTI_GYRO)
609 if ((!gyrosToScan
|| (gyrosToScan
& GYRO_2_MASK
)) && gyroDetectSensor(&gyro
.gyroSensor2
, gyroDeviceConfig(1))) {
610 gyroDetectionFlags
|= GYRO_2_MASK
;
614 if (gyroDetectionFlags
== GYRO_NONE_MASK
) {
618 bool eepromWriteRequired
= false;
620 gyroConfigMutable()->gyrosDetected
= gyroDetectionFlags
;
621 eepromWriteRequired
= true;
624 #if defined(USE_MULTI_GYRO)
625 if ((gyro
.gyroToUse
== GYRO_CONFIG_USE_GYRO_BOTH
&& !((gyroDetectionFlags
& GYRO_ALL_MASK
) == GYRO_ALL_MASK
))
626 || (gyro
.gyroToUse
== GYRO_CONFIG_USE_GYRO_1
&& !(gyroDetectionFlags
& GYRO_1_MASK
))
627 || (gyro
.gyroToUse
== GYRO_CONFIG_USE_GYRO_2
&& !(gyroDetectionFlags
& GYRO_2_MASK
))) {
628 if (gyroDetectionFlags
& GYRO_1_MASK
) {
629 gyro
.gyroToUse
= GYRO_CONFIG_USE_GYRO_1
;
631 gyro
.gyroToUse
= GYRO_CONFIG_USE_GYRO_2
;
634 gyroConfigMutable()->gyro_to_use
= gyro
.gyroToUse
;
635 eepromWriteRequired
= true;
638 // Only allow using both gyros simultaneously if they are the same hardware type.
639 if (((gyroDetectionFlags
& GYRO_ALL_MASK
) == GYRO_ALL_MASK
) && gyro
.gyroSensor1
.gyroDev
.gyroHardware
== gyro
.gyroSensor2
.gyroDev
.gyroHardware
) {
640 gyroDetectionFlags
|= GYRO_IDENTICAL_MASK
;
641 } else if (gyro
.gyroToUse
== GYRO_CONFIG_USE_GYRO_BOTH
) {
642 // If the user selected "BOTH" and they are not the same type, then reset to using only the first gyro.
643 gyro
.gyroToUse
= GYRO_CONFIG_USE_GYRO_1
;
644 gyroConfigMutable()->gyro_to_use
= gyro
.gyroToUse
;
645 eepromWriteRequired
= true;
648 if (gyro
.gyroToUse
== GYRO_CONFIG_USE_GYRO_2
|| gyro
.gyroToUse
== GYRO_CONFIG_USE_GYRO_BOTH
) {
649 static DMA_DATA
uint8_t gyroBuf2
[GYRO_BUF_SIZE
];
650 // SPI DMA buffer required per device
651 gyro
.gyroSensor2
.gyroDev
.dev
.txBuf
= gyroBuf2
;
652 gyro
.gyroSensor2
.gyroDev
.dev
.rxBuf
= &gyroBuf2
[GYRO_BUF_SIZE
/ 2];
654 gyroInitSensor(&gyro
.gyroSensor2
, gyroDeviceConfig(1));
655 gyro
.gyroHasOverflowProtection
= gyro
.gyroHasOverflowProtection
&& gyro
.gyroSensor2
.gyroDev
.gyroHasOverflowProtection
;
656 detectedSensors
[SENSOR_INDEX_GYRO
] = gyro
.gyroSensor2
.gyroDev
.gyroHardware
;
660 if (eepromWriteRequired
) {
664 if (gyro
.gyroToUse
== GYRO_CONFIG_USE_GYRO_1
|| gyro
.gyroToUse
== GYRO_CONFIG_USE_GYRO_BOTH
) {
665 static DMA_DATA
uint8_t gyroBuf1
[GYRO_BUF_SIZE
];
666 // SPI DMA buffer required per device
667 gyro
.gyroSensor1
.gyroDev
.dev
.txBuf
= gyroBuf1
;
668 gyro
.gyroSensor1
.gyroDev
.dev
.rxBuf
= &gyroBuf1
[GYRO_BUF_SIZE
/ 2];
669 gyroInitSensor(&gyro
.gyroSensor1
, gyroDeviceConfig(0));
670 gyro
.gyroHasOverflowProtection
= gyro
.gyroHasOverflowProtection
&& gyro
.gyroSensor1
.gyroDev
.gyroHasOverflowProtection
;
671 detectedSensors
[SENSOR_INDEX_GYRO
] = gyro
.gyroSensor1
.gyroDev
.gyroHardware
;
674 // Copy the sensor's scale to the high-level gyro object. If running in "BOTH" mode
675 // then logic above requires both sensors to be the same so we'll use sensor1's scale.
676 // This will need to be revised if we ever allow different sensor types to be used simultaneously.
677 // Likewise determine the appropriate raw data for use in DEBUG_GYRO_RAW
678 gyro
.scale
= gyro
.gyroSensor1
.gyroDev
.scale
;
679 gyro
.rawSensorDev
= &gyro
.gyroSensor1
.gyroDev
;
680 #if defined(USE_MULTI_GYRO)
681 if (gyro
.gyroToUse
== GYRO_CONFIG_USE_GYRO_2
) {
682 gyro
.scale
= gyro
.gyroSensor2
.gyroDev
.scale
;
683 gyro
.rawSensorDev
= &gyro
.gyroSensor2
.gyroDev
;
687 if (gyro
.rawSensorDev
) {
688 gyro
.sampleRateHz
= gyro
.rawSensorDev
->gyroSampleRateHz
;
689 gyro
.accSampleRateHz
= gyro
.rawSensorDev
->accSampleRateHz
;
691 gyro
.sampleRateHz
= 0;
692 gyro
.accSampleRateHz
= 0;
698 gyroDetectionFlags_t
getGyroDetectionFlags(void)
700 return gyroDetectionFlags
;
703 void gyroSetTargetLooptime(uint8_t pidDenom
)
705 activePidLoopDenom
= pidDenom
;
706 if (gyro
.sampleRateHz
) {
707 gyro
.sampleLooptime
= 1e6
/ gyro
.sampleRateHz
;
708 gyro
.targetLooptime
= activePidLoopDenom
* 1e6
/ gyro
.sampleRateHz
;
710 gyro
.sampleLooptime
= 0;
711 gyro
.targetLooptime
= 0;
716 gyroDev_t
*gyroActiveDev(void)
718 return &ACTIVE_GYRO
->gyroDev
;
721 const mpuDetectionResult_t
*gyroMpuDetectionResult(void)
723 return &ACTIVE_GYRO
->gyroDev
.mpuDetectionResult
;
726 int16_t gyroRateDps(int axis
)
728 return lrintf(gyro
.gyroADCf
[axis
] / ACTIVE_GYRO
->gyroDev
.scale
);
731 #ifdef USE_GYRO_REGISTER_DUMP
732 static extDevice_t
*gyroSensorDevByInstance(uint8_t whichSensor
)
734 #ifdef USE_MULTI_GYRO
735 if (whichSensor
== GYRO_CONFIG_USE_GYRO_2
) {
736 return &gyro
.gyroSensor2
.gyroDev
.dev
;
741 return &gyro
.gyroSensor1
.gyroDev
.dev
;
744 uint8_t gyroReadRegister(uint8_t whichSensor
, uint8_t reg
)
746 return mpuGyroReadRegister(gyroSensorDevByInstance(whichSensor
), reg
);
748 #endif // USE_GYRO_REGISTER_DUMP