2015-12-01 12:07:15 -04:00
|
|
|
#pragma once
|
2011-11-05 22:11:25 -03:00
|
|
|
|
2015-08-11 03:28:42 -03:00
|
|
|
#include <AP_HAL/AP_HAL.h>
|
|
|
|
#include <AP_Param/AP_Param.h>
|
|
|
|
#include <Filter/Filter.h>
|
|
|
|
#include <Filter/DerivativeFilter.h>
|
2011-12-09 02:33:00 -04:00
|
|
|
|
2014-10-19 16:22:51 -03:00
|
|
|
// maximum number of sensor instances
|
2015-09-10 07:24:59 -03:00
|
|
|
#define BARO_MAX_INSTANCES 3
|
2014-10-19 16:22:51 -03:00
|
|
|
|
|
|
|
// maximum number of drivers. Note that a single driver can provide
|
|
|
|
// multiple sensor instances
|
2016-11-24 20:59:14 -04:00
|
|
|
#define BARO_MAX_DRIVERS 3
|
2014-10-19 16:22:51 -03:00
|
|
|
|
|
|
|
class AP_Baro_Backend;
|
|
|
|
|
2011-11-05 22:11:25 -03:00
|
|
|
class AP_Baro
|
|
|
|
{
|
2014-10-19 16:22:51 -03:00
|
|
|
friend class AP_Baro_Backend;
|
|
|
|
|
2012-08-17 03:09:24 -03:00
|
|
|
public:
|
2014-10-19 16:22:51 -03:00
|
|
|
// constructor
|
|
|
|
AP_Baro();
|
2012-12-12 17:43:00 -04:00
|
|
|
|
2014-10-19 16:22:51 -03:00
|
|
|
// initialise the barometer object, loading backend drivers
|
|
|
|
void init(void);
|
2014-08-13 09:42:14 -03:00
|
|
|
|
2014-10-19 16:22:51 -03:00
|
|
|
// update the barometer object, asking backends to push data to
|
|
|
|
// the frontend
|
|
|
|
void update(void);
|
|
|
|
|
|
|
|
// healthy - returns true if sensor and derived altitude are good
|
|
|
|
bool healthy(void) const { return healthy(_primary); }
|
2015-01-08 21:59:01 -04:00
|
|
|
bool healthy(uint8_t instance) const { return sensors[instance].healthy && sensors[instance].alt_ok && sensors[instance].calibrated; }
|
2013-09-21 08:30:41 -03:00
|
|
|
|
2015-01-06 21:45:01 -04:00
|
|
|
// check if all baros are healthy - used for SYS_STATUS report
|
|
|
|
bool all_healthy(void) const;
|
|
|
|
|
2013-09-21 08:30:41 -03:00
|
|
|
// pressure in Pascal. Divide by 100 for millibars or hectopascals
|
2014-10-19 16:22:51 -03:00
|
|
|
float get_pressure(void) const { return get_pressure(_primary); }
|
|
|
|
float get_pressure(uint8_t instance) const { return sensors[instance].pressure; }
|
2012-08-17 03:09:24 -03:00
|
|
|
|
2013-09-21 08:30:41 -03:00
|
|
|
// temperature in degrees C
|
2014-10-19 16:22:51 -03:00
|
|
|
float get_temperature(void) const { return get_temperature(_primary); }
|
|
|
|
float get_temperature(uint8_t instance) const { return sensors[instance].temperature; }
|
2012-06-19 23:25:19 -03:00
|
|
|
|
2014-10-19 16:22:51 -03:00
|
|
|
// accumulate a reading on sensors. Some backends without their
|
|
|
|
// own thread or a timer may need this.
|
|
|
|
void accumulate(void);
|
2013-01-09 08:05:17 -04:00
|
|
|
|
2012-06-19 23:25:19 -03:00
|
|
|
// calibrate the barometer. This must be called on startup if the
|
|
|
|
// altitude/climb_rate/acceleration interfaces are ever used
|
2014-10-19 16:22:51 -03:00
|
|
|
void calibrate(void);
|
2012-06-19 23:25:19 -03:00
|
|
|
|
2013-10-05 05:44:00 -03:00
|
|
|
// update the barometer calibration to the current pressure. Can
|
|
|
|
// be used for incremental preflight update of baro
|
2014-10-19 16:22:51 -03:00
|
|
|
void update_calibration(void);
|
2013-10-05 05:44:00 -03:00
|
|
|
|
2012-06-19 23:25:19 -03:00
|
|
|
// get current altitude in meters relative to altitude at the time
|
|
|
|
// of the last calibrate() call
|
2014-10-19 16:22:51 -03:00
|
|
|
float get_altitude(void) const { return get_altitude(_primary); }
|
|
|
|
float get_altitude(uint8_t instance) const { return sensors[instance].altitude; }
|
2012-06-19 23:25:19 -03:00
|
|
|
|
2014-04-11 03:34:02 -03:00
|
|
|
// get altitude difference in meters relative given a base
|
|
|
|
// pressure in Pascal
|
2014-08-13 03:21:52 -03:00
|
|
|
float get_altitude_difference(float base_pressure, float pressure) const;
|
2014-04-11 03:34:02 -03:00
|
|
|
|
2013-06-26 05:32:37 -03:00
|
|
|
// get scale factor required to convert equivalent to true airspeed
|
2014-10-19 16:22:51 -03:00
|
|
|
float get_EAS2TAS(void);
|
2012-07-05 03:26:56 -03:00
|
|
|
|
2015-04-29 02:12:41 -03:00
|
|
|
// get air density / sea level density - decreases as altitude climbs
|
2015-04-28 15:15:49 -03:00
|
|
|
float get_air_density_ratio(void);
|
|
|
|
|
2012-06-19 23:25:19 -03:00
|
|
|
// get current climb rate in meters/s. A positive number means
|
|
|
|
// going up
|
2014-10-19 16:22:51 -03:00
|
|
|
float get_climb_rate(void);
|
2012-06-19 23:25:19 -03:00
|
|
|
|
2013-09-21 08:30:41 -03:00
|
|
|
// ground temperature in degrees C
|
2012-06-19 23:25:19 -03:00
|
|
|
// the ground values are only valid after calibration
|
2014-10-19 16:22:51 -03:00
|
|
|
float get_ground_temperature(void) const { return get_ground_temperature(_primary); }
|
|
|
|
float get_ground_temperature(uint8_t i) const { return sensors[i].ground_temperature.get(); }
|
2013-09-21 08:30:41 -03:00
|
|
|
|
|
|
|
// ground pressure in Pascal
|
|
|
|
// the ground values are only valid after calibration
|
2014-10-19 16:22:51 -03:00
|
|
|
float get_ground_pressure(void) const { return get_ground_pressure(_primary); }
|
|
|
|
float get_ground_pressure(uint8_t i) const { return sensors[i].ground_pressure.get(); }
|
2012-06-19 23:25:19 -03:00
|
|
|
|
2014-11-11 20:26:28 -04:00
|
|
|
// set the temperature to be used for altitude calibration. This
|
|
|
|
// allows an external temperature source (such as a digital
|
|
|
|
// airspeed sensor) to be used as the temperature source
|
|
|
|
void set_external_temperature(float temperature);
|
|
|
|
|
2013-08-08 10:33:53 -03:00
|
|
|
// get last time sample was taken (in ms)
|
2014-10-19 16:22:51 -03:00
|
|
|
uint32_t get_last_update(void) const { return get_last_update(_primary); }
|
|
|
|
uint32_t get_last_update(uint8_t instance) const { return sensors[_primary].last_update_ms; }
|
2012-11-07 05:58:36 -04:00
|
|
|
|
2014-10-19 16:22:51 -03:00
|
|
|
// settable parameters
|
|
|
|
static const struct AP_Param::GroupInfo var_info[];
|
2012-06-27 02:59:52 -03:00
|
|
|
|
2014-10-19 16:22:51 -03:00
|
|
|
float get_calibration_temperature(void) const { return get_calibration_temperature(_primary); }
|
|
|
|
float get_calibration_temperature(uint8_t instance) const;
|
2014-08-13 09:42:14 -03:00
|
|
|
|
2014-10-19 16:22:51 -03:00
|
|
|
// HIL (and SITL) interface, setting altitude
|
|
|
|
void setHIL(float altitude_msl);
|
2014-08-13 09:42:14 -03:00
|
|
|
|
2016-05-04 21:10:04 -03:00
|
|
|
// HIL (and SITL) interface, setting pressure, temperature, altitude and climb_rate
|
|
|
|
// used by Replay
|
2016-05-04 21:36:35 -03:00
|
|
|
void setHIL(uint8_t instance, float pressure, float temperature, float altitude, float climb_rate, uint32_t last_update_ms);
|
2012-06-19 23:25:19 -03:00
|
|
|
|
2015-10-19 14:54:11 -03:00
|
|
|
// HIL variables
|
|
|
|
struct {
|
2016-05-03 19:22:20 -03:00
|
|
|
float pressure;
|
|
|
|
float temperature;
|
2016-05-04 21:10:04 -03:00
|
|
|
float altitude;
|
|
|
|
float climb_rate;
|
2016-05-04 21:36:35 -03:00
|
|
|
uint32_t last_update_ms;
|
2016-05-03 19:22:20 -03:00
|
|
|
bool updated:1;
|
2016-05-04 21:10:04 -03:00
|
|
|
bool have_alt:1;
|
2016-05-04 21:36:35 -03:00
|
|
|
bool have_last_update:1;
|
2015-10-19 14:54:11 -03:00
|
|
|
} _hil;
|
|
|
|
|
2014-10-19 16:22:51 -03:00
|
|
|
// register a new sensor, claiming a sensor slot. If we are out of
|
|
|
|
// slots it will panic
|
|
|
|
uint8_t register_sensor(void);
|
2014-11-11 20:26:28 -04:00
|
|
|
|
2015-01-05 07:29:01 -04:00
|
|
|
// return number of registered sensors
|
|
|
|
uint8_t num_instances(void) const { return _num_sensors; }
|
|
|
|
|
2015-02-09 16:39:48 -04:00
|
|
|
// enable HIL mode
|
|
|
|
void set_hil_mode(void) { _hil_mode = true; }
|
|
|
|
|
2016-01-06 19:48:31 -04:00
|
|
|
// set baro drift amount
|
|
|
|
void set_baro_drift_altitude(float alt) { _alt_offset = alt; }
|
|
|
|
|
2016-05-13 17:01:11 -03:00
|
|
|
// get baro drift amount
|
|
|
|
float get_baro_drift_offset(void) { return _alt_offset_active; }
|
|
|
|
|
2014-10-19 16:22:51 -03:00
|
|
|
private:
|
|
|
|
// how many drivers do we have?
|
2016-07-12 02:02:41 -03:00
|
|
|
uint8_t _num_drivers;
|
2014-10-19 16:22:51 -03:00
|
|
|
AP_Baro_Backend *drivers[BARO_MAX_DRIVERS];
|
|
|
|
|
|
|
|
// how many sensors do we have?
|
2016-07-12 02:02:41 -03:00
|
|
|
uint8_t _num_sensors;
|
2014-10-19 16:22:51 -03:00
|
|
|
|
|
|
|
// what is the primary sensor at the moment?
|
2016-07-12 02:02:41 -03:00
|
|
|
uint8_t _primary;
|
2014-10-19 16:22:51 -03:00
|
|
|
|
|
|
|
struct sensor {
|
|
|
|
uint32_t last_update_ms; // last update time in ms
|
|
|
|
bool healthy:1; // true if sensor is healthy
|
|
|
|
bool alt_ok:1; // true if calculated altitude is ok
|
2015-01-08 21:59:01 -04:00
|
|
|
bool calibrated:1; // true if calculated calibrated successfully
|
2014-10-19 16:22:51 -03:00
|
|
|
float pressure; // pressure in Pascal
|
|
|
|
float temperature; // temperature in degrees C
|
|
|
|
float altitude; // calculated altitude
|
|
|
|
AP_Float ground_temperature;
|
|
|
|
AP_Float ground_pressure;
|
|
|
|
} sensors[BARO_MAX_INSTANCES];
|
2014-11-11 20:26:28 -04:00
|
|
|
|
2015-08-25 15:36:58 -03:00
|
|
|
AP_Float _alt_offset;
|
2016-07-12 02:02:41 -03:00
|
|
|
float _alt_offset_active;
|
2015-09-10 07:24:59 -03:00
|
|
|
AP_Int8 _primary_baro; // primary chosen by user
|
2016-12-01 05:59:50 -04:00
|
|
|
AP_Int8 _ext_bus; // bus number for external barometer
|
2016-07-12 02:02:41 -03:00
|
|
|
float _last_altitude_EAS2TAS;
|
|
|
|
float _EAS2TAS;
|
|
|
|
float _external_temperature;
|
|
|
|
uint32_t _last_external_temperature_ms;
|
2012-08-17 03:09:24 -03:00
|
|
|
DerivativeFilterFloat_Size7 _climb_rate_filter;
|
2016-07-12 02:02:41 -03:00
|
|
|
bool _hil_mode:1;
|
2014-10-19 16:22:51 -03:00
|
|
|
|
2015-11-08 23:34:07 -04:00
|
|
|
// when did we last notify the GCS of new pressure reference?
|
2016-07-12 02:02:41 -03:00
|
|
|
uint32_t _last_notify_ms;
|
2015-12-01 12:07:15 -04:00
|
|
|
|
2014-10-19 16:22:51 -03:00
|
|
|
void SimpleAtmosphere(const float alt, float &sigma, float &delta, float &theta);
|
2016-11-24 20:59:14 -04:00
|
|
|
bool _add_backend(AP_Baro_Backend *backend);
|
2011-11-05 22:11:25 -03:00
|
|
|
};
|