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
);
271 #if defined(USE_GYRO_SLEW_LIMITER)
272 void gyroInitSlewLimiter(gyroSensor_t
*gyroSensor
) {
274 for (int axis
= 0; axis
< XYZ_AXIS_COUNT
; axis
++) {
275 gyroSensor
->gyroDev
.gyroADCRawPrevious
[axis
] = 0;
280 static void gyroInitSensorFilters(gyroSensor_t
*gyroSensor
)
282 #if defined(USE_GYRO_SLEW_LIMITER)
283 gyroInitSlewLimiter(gyroSensor
);
289 void gyroInitSensor(gyroSensor_t
*gyroSensor
, const gyroDeviceConfig_t
*config
)
291 gyroSensor
->gyroDev
.gyro_high_fsr
= gyroConfig()->gyro_high_fsr
;
292 gyroSensor
->gyroDev
.gyroAlign
= config
->alignment
;
293 buildRotationMatrixFromAlignment(&config
->customAlignment
, &gyroSensor
->gyroDev
.rotationMatrix
);
294 gyroSensor
->gyroDev
.mpuIntExtiTag
= config
->extiTag
;
295 gyroSensor
->gyroDev
.hardware_lpf
= gyroConfig()->gyro_hardware_lpf
;
297 // The targetLooptime gets set later based on the active sensor's gyroSampleRateHz and pid_process_denom
298 gyroSensor
->gyroDev
.gyroSampleRateHz
= gyroSetSampleRate(&gyroSensor
->gyroDev
);
299 gyroSensor
->gyroDev
.initFn(&gyroSensor
->gyroDev
);
301 // As new gyros are supported, be sure to add them below based on whether they are subject to the overflow/inversion bug
302 // Any gyro not explicitly defined will default to not having built-in overflow protection as a safe alternative.
303 switch (gyroSensor
->gyroDev
.gyroHardware
) {
304 case GYRO_NONE
: // Won't ever actually get here, but included to account for all gyro types
317 gyroSensor
->gyroDev
.gyroHasOverflowProtection
= true;
323 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
325 gyroSensor
->gyroDev
.gyroHasOverflowProtection
= false;
329 gyroSensor
->gyroDev
.gyroHasOverflowProtection
= false; // default catch for newly added gyros until proven to be unaffected
333 gyroInitSensorFilters(gyroSensor
);
336 STATIC_UNIT_TESTED gyroHardware_e
gyroDetect(gyroDev_t
*dev
)
338 gyroHardware_e gyroHardware
= GYRO_DEFAULT
;
340 switch (gyroHardware
) {
344 #ifdef USE_GYRO_MPU6050
346 if (mpu6050GyroDetect(dev
)) {
347 gyroHardware
= GYRO_MPU6050
;
353 #ifdef USE_GYRO_L3G4200D
355 if (l3g4200dDetect(dev
)) {
356 gyroHardware
= GYRO_L3G4200D
;
362 #ifdef USE_GYRO_MPU3050
364 if (mpu3050Detect(dev
)) {
365 gyroHardware
= GYRO_MPU3050
;
371 #ifdef USE_GYRO_L3GD20
373 if (l3gd20GyroDetect(dev
)) {
374 gyroHardware
= GYRO_L3GD20
;
380 #ifdef USE_GYRO_SPI_MPU6000
382 if (mpu6000SpiGyroDetect(dev
)) {
383 gyroHardware
= GYRO_MPU6000
;
389 #if defined(USE_GYRO_MPU6500) || defined(USE_GYRO_SPI_MPU6500)
394 #ifdef USE_GYRO_SPI_MPU6500
395 if (mpu6500GyroDetect(dev
) || mpu6500SpiGyroDetect(dev
)) {
397 if (mpu6500GyroDetect(dev
)) {
399 switch (dev
->mpuDetectionResult
.sensor
) {
401 gyroHardware
= GYRO_MPU9250
;
404 gyroHardware
= GYRO_ICM20601
;
407 gyroHardware
= GYRO_ICM20602
;
410 gyroHardware
= GYRO_ICM20608G
;
413 gyroHardware
= GYRO_MPU6500
;
420 #ifdef USE_GYRO_SPI_MPU9250
422 if (mpu9250SpiGyroDetect(dev
)) {
423 gyroHardware
= GYRO_MPU9250
;
429 #ifdef USE_GYRO_SPI_ICM20649
431 if (icm20649SpiGyroDetect(dev
)) {
432 gyroHardware
= GYRO_ICM20649
;
438 #ifdef USE_GYRO_SPI_ICM20689
440 if (icm20689SpiGyroDetect(dev
)) {
441 gyroHardware
= GYRO_ICM20689
;
447 #if defined(USE_GYRO_SPI_ICM42605) || defined(USE_GYRO_SPI_ICM42688P)
450 if (icm426xxSpiGyroDetect(dev
)) {
451 switch (dev
->mpuDetectionResult
.sensor
) {
453 gyroHardware
= GYRO_ICM42605
;
456 gyroHardware
= GYRO_ICM42688P
;
459 gyroHardware
= GYRO_NONE
;
467 #ifdef USE_ACCGYRO_BMI160
469 if (bmi160SpiGyroDetect(dev
)) {
470 gyroHardware
= GYRO_BMI160
;
476 #ifdef USE_ACCGYRO_BMI270
478 if (bmi270SpiGyroDetect(dev
)) {
479 gyroHardware
= GYRO_BMI270
;
485 #ifdef USE_ACCGYRO_LSM6DSO
487 if (lsm6dsoSpiGyroDetect(dev
)) {
488 gyroHardware
= GYRO_LSM6DSO
;
496 if (fakeGyroDetect(dev
)) {
497 gyroHardware
= GYRO_FAKE
;
504 gyroHardware
= GYRO_NONE
;
507 if (gyroHardware
!= GYRO_NONE
) {
508 sensorsSet(SENSOR_GYRO
);
515 static bool gyroDetectSensor(gyroSensor_t
*gyroSensor
, const gyroDeviceConfig_t
*config
)
517 #if defined(USE_GYRO_MPU6050) || defined(USE_GYRO_MPU3050) || defined(USE_GYRO_MPU6500) || defined(USE_GYRO_SPI_MPU6500) || defined(USE_GYRO_SPI_MPU6000) \
518 || defined(USE_ACC_MPU6050) || defined(USE_GYRO_SPI_MPU9250) || defined(USE_GYRO_SPI_ICM20601) || defined(USE_GYRO_SPI_ICM20649) \
519 || 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)
521 bool gyroFound
= mpuDetect(&gyroSensor
->gyroDev
, config
);
523 #if !defined(USE_FAKE_GYRO) // Allow resorting to fake accgyro if defined
534 const gyroHardware_e gyroHardware
= gyroDetect(&gyroSensor
->gyroDev
);
535 gyroSensor
->gyroDev
.gyroHardware
= gyroHardware
;
537 return gyroHardware
!= GYRO_NONE
;
540 static void gyroPreInitSensor(const gyroDeviceConfig_t
*config
)
542 #if defined(USE_GYRO_MPU6050) || defined(USE_GYRO_MPU3050) || defined(USE_GYRO_MPU6500) || defined(USE_GYRO_SPI_MPU6500) || defined(USE_GYRO_SPI_MPU6000) \
543 || defined(USE_ACC_MPU6050) || defined(USE_GYRO_SPI_MPU9250) || defined(USE_GYRO_SPI_ICM20601) || defined(USE_GYRO_SPI_ICM20649) \
544 || defined(USE_GYRO_SPI_ICM20689) || defined(USE_ACCGYRO_BMI160) || defined(USE_ACCGYRO_BMI270) || defined(USE_ACCGRYO_LSM6DSO)
551 void gyroPreInit(void)
553 gyroPreInitSensor(gyroDeviceConfig(0));
554 #ifdef USE_MULTI_GYRO
555 gyroPreInitSensor(gyroDeviceConfig(1));
561 #ifdef USE_GYRO_OVERFLOW_CHECK
562 if (gyroConfig()->checkOverflow
== GYRO_OVERFLOW_CHECK_YAW
) {
563 gyro
.overflowAxisMask
= GYRO_OVERFLOW_Z
;
564 } else if (gyroConfig()->checkOverflow
== GYRO_OVERFLOW_CHECK_ALL_AXES
) {
565 gyro
.overflowAxisMask
= GYRO_OVERFLOW_X
| GYRO_OVERFLOW_Y
| GYRO_OVERFLOW_Z
;
567 gyro
.overflowAxisMask
= 0;
571 gyro
.gyroDebugMode
= DEBUG_NONE
;
572 gyro
.useDualGyroDebugging
= false;
573 gyro
.gyroHasOverflowProtection
= true;
579 case DEBUG_GYRO_SCALED
:
580 case DEBUG_GYRO_FILTERED
:
582 case DEBUG_GYRO_SAMPLE
:
583 gyro
.gyroDebugMode
= debugMode
;
585 case DEBUG_DUAL_GYRO_DIFF
:
586 case DEBUG_DUAL_GYRO_RAW
:
587 case DEBUG_DUAL_GYRO_SCALED
:
588 gyro
.useDualGyroDebugging
= true;
592 gyroDetectionFlags
= GYRO_NONE_MASK
;
593 uint8_t gyrosToScan
= gyroConfig()->gyrosDetected
;
595 gyro
.gyroToUse
= gyroConfig()->gyro_to_use
;
596 gyro
.gyroDebugAxis
= gyroConfig()->gyro_filter_debug_axis
;
598 if ((!gyrosToScan
|| (gyrosToScan
& GYRO_1_MASK
)) && gyroDetectSensor(&gyro
.gyroSensor1
, gyroDeviceConfig(0))) {
599 gyroDetectionFlags
|= GYRO_1_MASK
;
602 #if defined(USE_MULTI_GYRO)
603 if ((!gyrosToScan
|| (gyrosToScan
& GYRO_2_MASK
)) && gyroDetectSensor(&gyro
.gyroSensor2
, gyroDeviceConfig(1))) {
604 gyroDetectionFlags
|= GYRO_2_MASK
;
608 if (gyroDetectionFlags
== GYRO_NONE_MASK
) {
612 bool eepromWriteRequired
= false;
614 gyroConfigMutable()->gyrosDetected
= gyroDetectionFlags
;
615 eepromWriteRequired
= true;
618 #if defined(USE_MULTI_GYRO)
619 if ((gyro
.gyroToUse
== GYRO_CONFIG_USE_GYRO_BOTH
&& !((gyroDetectionFlags
& GYRO_ALL_MASK
) == GYRO_ALL_MASK
))
620 || (gyro
.gyroToUse
== GYRO_CONFIG_USE_GYRO_1
&& !(gyroDetectionFlags
& GYRO_1_MASK
))
621 || (gyro
.gyroToUse
== GYRO_CONFIG_USE_GYRO_2
&& !(gyroDetectionFlags
& GYRO_2_MASK
))) {
622 if (gyroDetectionFlags
& GYRO_1_MASK
) {
623 gyro
.gyroToUse
= GYRO_CONFIG_USE_GYRO_1
;
625 gyro
.gyroToUse
= GYRO_CONFIG_USE_GYRO_2
;
628 gyroConfigMutable()->gyro_to_use
= gyro
.gyroToUse
;
629 eepromWriteRequired
= true;
632 // Only allow using both gyros simultaneously if they are the same hardware type.
633 if (((gyroDetectionFlags
& GYRO_ALL_MASK
) == GYRO_ALL_MASK
) && gyro
.gyroSensor1
.gyroDev
.gyroHardware
== gyro
.gyroSensor2
.gyroDev
.gyroHardware
) {
634 gyroDetectionFlags
|= GYRO_IDENTICAL_MASK
;
635 } else if (gyro
.gyroToUse
== GYRO_CONFIG_USE_GYRO_BOTH
) {
636 // If the user selected "BOTH" and they are not the same type, then reset to using only the first gyro.
637 gyro
.gyroToUse
= GYRO_CONFIG_USE_GYRO_1
;
638 gyroConfigMutable()->gyro_to_use
= gyro
.gyroToUse
;
639 eepromWriteRequired
= true;
642 if (gyro
.gyroToUse
== GYRO_CONFIG_USE_GYRO_2
|| gyro
.gyroToUse
== GYRO_CONFIG_USE_GYRO_BOTH
) {
643 static DMA_DATA
uint8_t gyroBuf2
[GYRO_BUF_SIZE
];
644 // SPI DMA buffer required per device
645 gyro
.gyroSensor2
.gyroDev
.dev
.txBuf
= gyroBuf2
;
646 gyro
.gyroSensor2
.gyroDev
.dev
.rxBuf
= &gyroBuf2
[GYRO_BUF_SIZE
/ 2];
648 gyroInitSensor(&gyro
.gyroSensor2
, gyroDeviceConfig(1));
649 gyro
.gyroHasOverflowProtection
= gyro
.gyroHasOverflowProtection
&& gyro
.gyroSensor2
.gyroDev
.gyroHasOverflowProtection
;
650 detectedSensors
[SENSOR_INDEX_GYRO
] = gyro
.gyroSensor2
.gyroDev
.gyroHardware
;
654 if (eepromWriteRequired
) {
658 if (gyro
.gyroToUse
== GYRO_CONFIG_USE_GYRO_1
|| gyro
.gyroToUse
== GYRO_CONFIG_USE_GYRO_BOTH
) {
659 static DMA_DATA
uint8_t gyroBuf1
[GYRO_BUF_SIZE
];
660 // SPI DMA buffer required per device
661 gyro
.gyroSensor1
.gyroDev
.dev
.txBuf
= gyroBuf1
;
662 gyro
.gyroSensor1
.gyroDev
.dev
.rxBuf
= &gyroBuf1
[GYRO_BUF_SIZE
/ 2];
663 gyroInitSensor(&gyro
.gyroSensor1
, gyroDeviceConfig(0));
664 gyro
.gyroHasOverflowProtection
= gyro
.gyroHasOverflowProtection
&& gyro
.gyroSensor1
.gyroDev
.gyroHasOverflowProtection
;
665 detectedSensors
[SENSOR_INDEX_GYRO
] = gyro
.gyroSensor1
.gyroDev
.gyroHardware
;
668 // Copy the sensor's scale to the high-level gyro object. If running in "BOTH" mode
669 // then logic above requires both sensors to be the same so we'll use sensor1's scale.
670 // This will need to be revised if we ever allow different sensor types to be used simultaneously.
671 // Likewise determine the appropriate raw data for use in DEBUG_GYRO_RAW
672 gyro
.scale
= gyro
.gyroSensor1
.gyroDev
.scale
;
673 gyro
.rawSensorDev
= &gyro
.gyroSensor1
.gyroDev
;
674 #if defined(USE_MULTI_GYRO)
675 if (gyro
.gyroToUse
== GYRO_CONFIG_USE_GYRO_2
) {
676 gyro
.scale
= gyro
.gyroSensor2
.gyroDev
.scale
;
677 gyro
.rawSensorDev
= &gyro
.gyroSensor2
.gyroDev
;
681 if (gyro
.rawSensorDev
) {
682 gyro
.sampleRateHz
= gyro
.rawSensorDev
->gyroSampleRateHz
;
683 gyro
.accSampleRateHz
= gyro
.rawSensorDev
->accSampleRateHz
;
685 gyro
.sampleRateHz
= 0;
686 gyro
.accSampleRateHz
= 0;
692 gyroDetectionFlags_t
getGyroDetectionFlags(void)
694 return gyroDetectionFlags
;
697 void gyroSetTargetLooptime(uint8_t pidDenom
)
699 activePidLoopDenom
= pidDenom
;
700 if (gyro
.sampleRateHz
) {
701 gyro
.sampleLooptime
= 1e6
/ gyro
.sampleRateHz
;
702 gyro
.targetLooptime
= activePidLoopDenom
* 1e6
/ gyro
.sampleRateHz
;
704 gyro
.sampleLooptime
= 0;
705 gyro
.targetLooptime
= 0;
710 gyroDev_t
*gyroActiveDev(void)
712 return &ACTIVE_GYRO
->gyroDev
;
715 const mpuDetectionResult_t
*gyroMpuDetectionResult(void)
717 return &ACTIVE_GYRO
->gyroDev
.mpuDetectionResult
;
720 int16_t gyroRateDps(int axis
)
722 return lrintf(gyro
.gyroADCf
[axis
] / ACTIVE_GYRO
->gyroDev
.scale
);
725 #ifdef USE_GYRO_REGISTER_DUMP
726 static extDevice_t
*gyroSensorDevByInstance(uint8_t whichSensor
)
728 #ifdef USE_MULTI_GYRO
729 if (whichSensor
== GYRO_CONFIG_USE_GYRO_2
) {
730 return &gyro
.gyroSensor2
.gyroDev
.dev
;
735 return &gyro
.gyroSensor1
.gyroDev
.dev
;
738 uint8_t gyroReadRegister(uint8_t whichSensor
, uint8_t reg
)
740 return mpuGyroReadRegister(gyroSensorDevByInstance(whichSensor
), reg
);
742 #endif // USE_GYRO_REGISTER_DUMP