mirror of
https://github.com/betaflight/betaflight.git
synced 2025-07-26 09:45:37 +03:00
Some baro cleanups to avoid using or exposing incomplete and
uninitialised baro data. imu code size reduction (treym)
This commit is contained in:
parent
2413130c0f
commit
f127847bf2
4 changed files with 43 additions and 35 deletions
|
@ -55,6 +55,10 @@ void baroSetCalibrationCycles(uint16_t calibrationCyclesRequired)
|
|||
calibratingB = calibrationCyclesRequired;
|
||||
}
|
||||
|
||||
static bool baroReady = false;
|
||||
|
||||
#define PRESSURE_SAMPLE_COUNT (barometerConfig->baro_sample_count - 1)
|
||||
|
||||
static uint32_t recalculateBarometerTotal(uint8_t baroSampleCount, uint32_t pressureTotal, int32_t newPressureReading)
|
||||
{
|
||||
static int32_t barometerSamples[BARO_SAMPLE_COUNT_MAX];
|
||||
|
@ -65,10 +69,12 @@ static uint32_t recalculateBarometerTotal(uint8_t baroSampleCount, uint32_t pres
|
|||
nextSampleIndex = (currentSampleIndex + 1);
|
||||
if (nextSampleIndex == baroSampleCount) {
|
||||
nextSampleIndex = 0;
|
||||
baroReady = true;
|
||||
}
|
||||
barometerSamples[currentSampleIndex] = newPressureReading;
|
||||
|
||||
// recalculate pressure total
|
||||
// Note, the pressure total is made up of baroSampleCount - 1 samples - See PRESSURE_SAMPLE_COUNT
|
||||
pressureTotal += barometerSamples[currentSampleIndex];
|
||||
pressureTotal -= barometerSamples[nextSampleIndex];
|
||||
|
||||
|
@ -79,10 +85,10 @@ static uint32_t recalculateBarometerTotal(uint8_t baroSampleCount, uint32_t pres
|
|||
|
||||
typedef enum {
|
||||
BAROMETER_NEEDS_SAMPLES = 0,
|
||||
BAROMETER_NEEDS_CALCULATION
|
||||
BAROMETER_NEEDS_CALCULATION,
|
||||
BAROMETER_NEEDS_PROCESSING
|
||||
} barometerState_e;
|
||||
|
||||
static bool baroReady = false;
|
||||
|
||||
bool isBaroReady(void) {
|
||||
return baroReady;
|
||||
|
@ -99,22 +105,25 @@ void baroUpdate(uint32_t currentTime)
|
|||
baroDeadline = currentTime;
|
||||
|
||||
switch (state) {
|
||||
case BAROMETER_NEEDS_SAMPLES:
|
||||
baro.get_ut();
|
||||
baro.start_up();
|
||||
state = BAROMETER_NEEDS_CALCULATION;
|
||||
baroDeadline += baro.up_delay;
|
||||
break;
|
||||
|
||||
case BAROMETER_NEEDS_CALCULATION:
|
||||
baro.get_up();
|
||||
baro.start_ut();
|
||||
baroDeadline += baro.ut_delay;
|
||||
baro.calculate(&baroPressure, &baroTemperature);
|
||||
baroReady = true;
|
||||
state = BAROMETER_NEEDS_SAMPLES;
|
||||
break;
|
||||
state = BAROMETER_NEEDS_PROCESSING;
|
||||
break;
|
||||
|
||||
case BAROMETER_NEEDS_SAMPLES:
|
||||
baro.get_ut();
|
||||
baro.start_up();
|
||||
case BAROMETER_NEEDS_PROCESSING:
|
||||
state = BAROMETER_NEEDS_SAMPLES;
|
||||
baroPressureSum = recalculateBarometerTotal(barometerConfig->baro_sample_count, baroPressureSum, baroPressure);
|
||||
state = BAROMETER_NEEDS_CALCULATION;
|
||||
baroDeadline += baro.up_delay;
|
||||
break;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -124,7 +133,7 @@ int32_t baroCalculateAltitude(void)
|
|||
|
||||
// calculates height from ground via baro readings
|
||||
// see: https://github.com/diydrones/ardupilot/blob/master/libraries/AP_Baro/AP_Baro.cpp#L140
|
||||
BaroAlt_tmp = lrintf((1.0f - powf((float)(baroPressureSum / (barometerConfig->baro_sample_count - 1)) / 101325.0f, 0.190295f)) * 4433000.0f); // in cm
|
||||
BaroAlt_tmp = lrintf((1.0f - powf((float)(baroPressureSum / PRESSURE_SAMPLE_COUNT) / 101325.0f, 0.190295f)) * 4433000.0f); // in cm
|
||||
BaroAlt_tmp -= baroGroundAltitude;
|
||||
BaroAlt = lrintf((float)BaroAlt * barometerConfig->baro_noise_lpf + (float)BaroAlt_tmp * (1.0f - barometerConfig->baro_noise_lpf)); // additional LPF to reduce baro noise
|
||||
|
||||
|
@ -134,7 +143,7 @@ int32_t baroCalculateAltitude(void)
|
|||
void performBaroCalibrationCycle(void)
|
||||
{
|
||||
baroGroundPressure -= baroGroundPressure / 8;
|
||||
baroGroundPressure += baroPressureSum / (barometerConfig->baro_sample_count - 1);
|
||||
baroGroundPressure += baroPressureSum / PRESSURE_SAMPLE_COUNT;
|
||||
baroGroundAltitude = (1.0f - powf((baroGroundPressure / 8) / 101325.0f, 0.190295f)) * 4433000.0f;
|
||||
|
||||
calibratingB--;
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue