mirror of
https://github.com/betaflight/betaflight.git
synced 2025-07-26 01:35:41 +03:00
Fix to acc gyro accumulation code
This commit is contained in:
parent
94d3549299
commit
c0b1572175
3 changed files with 29 additions and 30 deletions
|
@ -77,9 +77,9 @@
|
|||
gyro_t gyro;
|
||||
static uint8_t gyroDebugMode;
|
||||
|
||||
static float accumulator[XYZ_AXIS_COUNT];
|
||||
static timeUs_t accumulatedTimeUs;
|
||||
static timeUs_t lastTimeSampledUs;
|
||||
static float accumulatedMeasurements[XYZ_AXIS_COUNT];
|
||||
static timeUs_t accumulatedMeasurementTimeUs;
|
||||
static timeUs_t accumulationLastTimeSampledUs;
|
||||
|
||||
typedef struct gyroCalibration_s {
|
||||
int32_t sum[XYZ_AXIS_COUNT];
|
||||
|
@ -649,9 +649,9 @@ static void gyroUpdateSensor(gyroSensor_t *gyroSensor, timeUs_t currentTimeUs)
|
|||
gyroDataAnalyse(&gyroSensor->gyroDev, gyroSensor->notchFilterDyn);
|
||||
#endif
|
||||
|
||||
const timeDelta_t sampleDeltaUs = currentTimeUs - lastTimeSampledUs;
|
||||
lastTimeSampledUs = currentTimeUs;
|
||||
accumulatedTimeUs += sampleDeltaUs;
|
||||
const timeDelta_t sampleDeltaUs = currentTimeUs - accumulationLastTimeSampledUs;
|
||||
accumulationLastTimeSampledUs = currentTimeUs;
|
||||
accumulatedMeasurementTimeUs += sampleDeltaUs;
|
||||
|
||||
if (gyroDebugMode == DEBUG_NONE) {
|
||||
for (int axis = 0; axis < XYZ_AXIS_COUNT; axis++) {
|
||||
|
@ -664,7 +664,7 @@ static void gyroUpdateSensor(gyroSensor_t *gyroSensor, timeUs_t currentTimeUs)
|
|||
gyroADCf = gyroSensor->notchFilter2ApplyFn(&gyroSensor->notchFilter2[axis], gyroADCf);
|
||||
gyroADCf = gyroSensor->softLpfFilterApplyFn(gyroSensor->softLpfFilterPtr[axis], gyroADCf);
|
||||
gyro.gyroADCf[axis] = gyroADCf;
|
||||
accumulator[axis] += gyroADCf * sampleDeltaUs;
|
||||
accumulatedMeasurements[axis] += gyroADCf * sampleDeltaUs;
|
||||
}
|
||||
} else {
|
||||
for (int axis = 0; axis < XYZ_AXIS_COUNT; axis++) {
|
||||
|
@ -696,7 +696,7 @@ static void gyroUpdateSensor(gyroSensor_t *gyroSensor, timeUs_t currentTimeUs)
|
|||
gyroADCf = gyroSensor->softLpfFilterApplyFn(gyroSensor->softLpfFilterPtr[axis], gyroADCf);
|
||||
|
||||
gyro.gyroADCf[axis] = gyroADCf;
|
||||
accumulator[axis] += gyroADCf * sampleDeltaUs;
|
||||
accumulatedMeasurements[axis] += gyroADCf * sampleDeltaUs;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -706,20 +706,19 @@ void gyroUpdate(timeUs_t currentTimeUs)
|
|||
gyroUpdateSensor(&gyroSensor1, currentTimeUs);
|
||||
}
|
||||
|
||||
bool gyroGetAccumulationAverage(float *accumulation)
|
||||
bool gyroGetAccumulationAverage(float *accumulationAverage)
|
||||
{
|
||||
if (accumulatedTimeUs > 0) {
|
||||
const float accumulatedTimeS = accumulatedTimeUs * 1e-6;
|
||||
if (accumulatedMeasurementTimeUs > 0) {
|
||||
// If we have gyro data accumulated, calculate average rate that will yield the same rotation
|
||||
for (int axis = 0; axis < XYZ_AXIS_COUNT; axis++) {
|
||||
accumulation[axis] = accumulator[axis] / accumulatedTimeS;
|
||||
accumulator[axis] = 0.0f;
|
||||
accumulationAverage[axis] = accumulatedMeasurements[axis] / accumulatedMeasurementTimeUs;
|
||||
accumulatedMeasurements[axis] = 0.0f;
|
||||
}
|
||||
accumulatedTimeUs = 0;
|
||||
accumulatedMeasurementTimeUs = 0;
|
||||
return true;
|
||||
} else {
|
||||
for (int axis = 0; axis < XYZ_AXIS_COUNT; axis++) {
|
||||
accumulation[axis] = 0.0f;
|
||||
accumulationAverage[axis] = 0.0f;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue