Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a function to check the health of the Barometer #8233

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 25 additions & 7 deletions src/main/sensors/barometer.c
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
#include "common/calibration.h"
#include "common/log.h"
#include "common/maths.h"
#include "common/time.h"
#include "common/utils.h"

#include "config/parameter_group.h"
Expand Down Expand Up @@ -58,13 +57,22 @@ baro_t baro; // barometer access functions

#ifdef USE_BARO

// Timeouts for health reporting
#define BARO_TIMEOUT_MS 500 // Timeout in ms since last successful read
#define BARO_DATA_CHANGE_TIMEOUT_MS 2000 // Timeout in ms since last successful read that involved temperature of pressure changing

PG_REGISTER_WITH_RESET_TEMPLATE(barometerConfig_t, barometerConfig, PG_BAROMETER_CONFIG, 4);

PG_RESET_TEMPLATE(barometerConfig_t, barometerConfig,
.baro_hardware = SETTING_BARO_HARDWARE_DEFAULT,
.baro_calibration_tolerance = SETTING_BARO_CAL_TOLERANCE_DEFAULT
);

typedef enum {
BAROMETER_NEEDS_SAMPLES = 0,
BAROMETER_NEEDS_CALCULATION
} barometerState_e;

static zeroCalibrationScalar_t zeroCalibration;
static float baroGroundAltitude = 0;
static float baroGroundPressure = 101325.0f; // 101325 pascal, 1 standard atmosphere
Expand Down Expand Up @@ -245,11 +253,6 @@ bool baroInit(void)
return true;
}

typedef enum {
BAROMETER_NEEDS_SAMPLES = 0,
BAROMETER_NEEDS_CALCULATION
} barometerState_e;

uint32_t baroUpdate(void)
{
static barometerState_e state = BAROMETER_NEEDS_SAMPLES;
Expand All @@ -274,6 +277,7 @@ uint32_t baroUpdate(void)
if (baro.dev.start_ut) {
baro.dev.start_ut(&baro.dev);
}

#ifdef USE_SIMULATOR
if (!ARMING_FLAG(SIMULATOR_MODE)) {
//output: baro.baroPressure, baro.baroTemperature
Expand All @@ -282,6 +286,17 @@ uint32_t baroUpdate(void)
#else
baro.dev.calculate(&baro.dev, &baro.baroPressure, &baro.baroTemperature);
#endif

const timeMs_t now = millis();

// Check for changes in data values
if ((baro.baroLastPressure != baro.baroPressure) || (baro.baroLastTemperature != baro.baroTemperature)) {
baro.lastChangeMs = now;
}

baro.baroLastPressure = baro.baroPressure;
baro.baroLastTemperature = baro.baroTemperature;
baro.lastUpdateMs = now;
state = BAROMETER_NEEDS_SAMPLES;
return baro.dev.ut_delay;
break;
Expand Down Expand Up @@ -342,7 +357,10 @@ int16_t baroGetTemperature(void)

bool baroIsHealthy(void)
{
return true;
// Consider a sensor as healthy if it has had an update in the last 0.5 seconds and values are non-zero and have changed within the last 2 seconds
const timeMs_t now = millis();

return (now - baro.lastUpdateMs < BARO_TIMEOUT_MS) && (now - baro.lastChangeMs < BARO_DATA_CHANGE_TIMEOUT_MS) && (baro.baroLastPressure != 0);
}

#endif /* BARO */
10 changes: 7 additions & 3 deletions src/main/sensors/barometer.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
#include "config/parameter_group.h"

#include "drivers/barometer/barometer.h"
#include "common/time.h"

typedef enum {
BARO_NONE = 0,
Expand All @@ -41,8 +42,12 @@ typedef enum {
typedef struct baro_s {
baroDev_t dev;
int32_t BaroAlt;
int32_t baroTemperature; // Use temperature for telemetry
int32_t baroPressure; // Use pressure for telemetry
int32_t baroTemperature;
int32_t baroPressure;
int32_t baroLastTemperature;
int32_t baroLastPressure;
timeMs_t lastChangeMs;
timeMs_t lastUpdateMs;
} baro_t;

extern baro_t baro;
Expand All @@ -56,7 +61,6 @@ typedef struct barometerConfig_s {

PG_DECLARE(barometerConfig_t, barometerConfig);


bool baroInit(void);
bool baroIsCalibrationComplete(void);
void baroStartCalibration(void);
Expand Down