2 * This file is part of Cleanflight.
4 * Cleanflight is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 3 of the License, or
7 * (at your option) any later version.
9 * Cleanflight is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with Cleanflight. If not, see <http://www.gnu.org/licenses/>.
23 #include "common/axis.h"
24 #include "common/maths.h"
26 #include "drivers/sensor.h"
27 #include "drivers/accgyro.h"
28 #include "sensors/sensors.h"
29 #include "io/statusindicator.h"
30 #include "sensors/boardalignment.h"
32 #include "sensors/gyro.h"
34 uint16_t calibratingG
= 0;
35 int16_t gyroADC
[XYZ_AXIS_COUNT
];
36 int16_t gyroZero
[FLIGHT_DYNAMICS_INDEX_COUNT
] = { 0, 0, 0 };
38 static gyroConfig_t
*gyroConfig
;
40 gyro_t gyro
; // gyro access functions
41 sensor_align_e gyroAlign
= 0;
43 void useGyroConfig(gyroConfig_t
*gyroConfigToUse
)
45 gyroConfig
= gyroConfigToUse
;
48 void gyroSetCalibrationCycles(uint16_t calibrationCyclesRequired
)
50 calibratingG
= calibrationCyclesRequired
;
53 bool isGyroCalibrationComplete(void)
55 return calibratingG
== 0;
58 bool isOnFinalGyroCalibrationCycle(void)
60 return calibratingG
== 1;
63 bool isOnFirstGyroCalibrationCycle(void)
65 return calibratingG
== CALIBRATING_GYRO_CYCLES
;
68 static void performAcclerationCalibration(uint8_t gyroMovementCalibrationThreshold
)
72 static stdev_t var
[3];
74 for (axis
= 0; axis
< 3; axis
++) {
76 // Reset g[axis] at start of calibration
77 if (isOnFirstGyroCalibrationCycle()) {
82 // Sum up CALIBRATING_GYRO_CYCLES readings
83 g
[axis
] += gyroADC
[axis
];
84 devPush(&var
[axis
], gyroADC
[axis
]);
86 // Reset global variables to prevent other code from using un-calibrated data
90 if (isOnFinalGyroCalibrationCycle()) {
91 float dev
= devStandardDeviation(&var
[axis
]);
92 // check deviation and startover in case the model was moved
93 if (gyroMovementCalibrationThreshold
&& dev
> gyroMovementCalibrationThreshold
) {
94 gyroSetCalibrationCycles(CALIBRATING_GYRO_CYCLES
);
97 gyroZero
[axis
] = (g
[axis
] + (CALIBRATING_GYRO_CYCLES
/ 2)) / CALIBRATING_GYRO_CYCLES
;
98 blinkLedAndSoundBeeper(10, 15, 1);
104 static void applyGyroZero(void)
107 for (axis
= 0; axis
< 3; axis
++) {
108 gyroADC
[axis
] -= gyroZero
[axis
];
112 void gyroUpdate(void)
114 // FIXME When gyro.read() fails due to i2c or other error gyroZero is continually re-applied to gyroADC resulting in a old reading that gets worse over time.
116 // range: +/- 8192; +/- 2000 deg/sec
118 alignSensors(gyroADC
, gyroADC
, gyroAlign
);
120 if (!isGyroCalibrationComplete()) {
121 performAcclerationCalibration(gyroConfig
->gyroMovementCalibrationThreshold
);