CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutSign UpSign In
Ardupilot

Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place. Commercial Alternative to JupyterHub.

GitHub Repository: Ardupilot/ardupilot
Path: blob/master/libraries/AP_Baro/AP_Baro_Backend.cpp
Views: 1798
1
#include "AP_Baro_Backend.h"
2
3
#include <stdio.h>
4
#include <AP_Math/AP_Math.h>
5
6
extern const AP_HAL::HAL& hal;
7
8
// constructor
9
AP_Baro_Backend::AP_Baro_Backend(AP_Baro &baro) :
10
_frontend(baro)
11
{
12
}
13
14
void AP_Baro_Backend::update_healthy_flag(uint8_t instance)
15
{
16
if (instance >= _frontend._num_sensors) {
17
return;
18
}
19
WITH_SEMAPHORE(_sem);
20
21
// consider a sensor as healthy if it has had an update in the
22
// last 0.5 seconds and values are non-zero and have changed within the last 2 seconds
23
const uint32_t now = AP_HAL::millis();
24
_frontend.sensors[instance].healthy =
25
(now - _frontend.sensors[instance].last_update_ms < BARO_TIMEOUT_MS) &&
26
(now - _frontend.sensors[instance].last_change_ms < BARO_DATA_CHANGE_TIMEOUT_MS) &&
27
!is_zero(_frontend.sensors[instance].pressure);
28
29
if (_frontend.sensors[instance].temperature < -200 ||
30
_frontend.sensors[instance].temperature > 200) {
31
// if temperature is way out of range then we likely have bad
32
// data from the sensor, treat is as unhealthy. This is done
33
// so SPI sensors which have no data validity checking can
34
// mark a sensor unhealthy
35
_frontend.sensors[instance].healthy = false;
36
}
37
}
38
39
void AP_Baro_Backend::backend_update(uint8_t instance)
40
{
41
update();
42
update_healthy_flag(instance);
43
}
44
45
46
/*
47
copy latest data to the frontend from a backend
48
*/
49
void AP_Baro_Backend::_copy_to_frontend(uint8_t instance, float pressure, float temperature)
50
{
51
if (instance >= _frontend._num_sensors) {
52
return;
53
}
54
uint32_t now = AP_HAL::millis();
55
56
// check for changes in data values
57
if (!is_equal(_frontend.sensors[instance].pressure, pressure) || !is_equal(_frontend.sensors[instance].temperature, temperature)) {
58
_frontend.sensors[instance].last_change_ms = now;
59
}
60
61
// update readings
62
_frontend.sensors[instance].pressure = pressure;
63
_frontend.sensors[instance].temperature = temperature;
64
_frontend.sensors[instance].last_update_ms = now;
65
}
66
67
static constexpr float FILTER_KOEF = 0.1f;
68
69
/* Check that the baro value is valid by using a mean filter. If the
70
* value is further than filter_range from mean value, it is
71
* rejected.
72
*/
73
bool AP_Baro_Backend::pressure_ok(float press)
74
{
75
76
if (isinf(press) || isnan(press)) {
77
return false;
78
}
79
80
const float range = (float)_frontend.get_filter_range();
81
if (range <= 0) {
82
return true;
83
}
84
85
bool ret = true;
86
if (is_zero(_mean_pressure)) {
87
_mean_pressure = press;
88
} else {
89
const float d = fabsf(_mean_pressure - press) / (_mean_pressure + press); // diff divide by mean value in percent ( with the * 200.0f on later line)
90
float koeff = FILTER_KOEF;
91
92
if (d * 200.0f > range) { // check the difference from mean value outside allowed range
93
// printf("\nBaro pressure error: mean %f got %f\n", (double)_mean_pressure, (double)press );
94
ret = false;
95
koeff /= (d * 10.0f); // 2.5 and more, so one bad sample never change mean more than 4%
96
_error_count++;
97
}
98
_mean_pressure = _mean_pressure * (1 - koeff) + press * koeff; // complimentary filter 1/k
99
}
100
return ret;
101
}
102
103