2015-12-01 12:07:15 -04:00
|
|
|
#include "AP_Baro_HIL.h"
|
2011-10-13 11:22:03 -03:00
|
|
|
|
2015-08-11 03:28:42 -03:00
|
|
|
#include <AP_HAL/AP_HAL.h>
|
2014-01-05 01:36:12 -04:00
|
|
|
|
2012-10-11 14:53:21 -03:00
|
|
|
extern const AP_HAL::HAL& hal;
|
2011-10-13 11:22:03 -03:00
|
|
|
|
2014-10-19 16:22:51 -03:00
|
|
|
AP_Baro_HIL::AP_Baro_HIL(AP_Baro &baro) :
|
|
|
|
AP_Baro_Backend(baro)
|
2011-10-13 11:22:03 -03:00
|
|
|
{
|
2014-10-19 16:22:51 -03:00
|
|
|
_instance = _frontend.register_sensor();
|
2011-10-13 11:22:03 -03:00
|
|
|
}
|
|
|
|
|
2013-07-20 10:00:41 -03:00
|
|
|
// ==========================================================================
|
|
|
|
// based on tables.cpp from http://www.pdas.com/atmosdownload.html
|
|
|
|
|
|
|
|
/*
|
2014-10-19 16:22:51 -03:00
|
|
|
Compute the temperature, density, and pressure in the standard atmosphere
|
|
|
|
Correct to 20 km. Only approximate thereafter.
|
2013-07-20 10:00:41 -03:00
|
|
|
*/
|
2014-10-19 16:22:51 -03:00
|
|
|
void AP_Baro::SimpleAtmosphere(
|
2013-07-20 10:00:41 -03:00
|
|
|
const float alt, // geometric altitude, km.
|
|
|
|
float& sigma, // density/sea-level standard density
|
|
|
|
float& delta, // pressure/sea-level standard pressure
|
|
|
|
float& theta) // temperature/sea-level standard temperature
|
|
|
|
{
|
2014-10-19 16:22:51 -03:00
|
|
|
const float REARTH = 6369.0f; // radius of the Earth (km)
|
|
|
|
const float GMR = 34.163195f; // gas constant
|
|
|
|
float h=alt*REARTH/(alt+REARTH); // geometric to geopotential altitude
|
|
|
|
|
|
|
|
if (h < 11.0f) {
|
|
|
|
// Troposphere
|
2018-05-10 23:27:48 -03:00
|
|
|
theta = (SSL_AIR_TEMPERATURE - 6.5f * h) / SSL_AIR_TEMPERATURE;
|
|
|
|
delta = powf(theta, GMR / 6.5f);
|
2014-10-19 16:22:51 -03:00
|
|
|
} else {
|
|
|
|
// Stratosphere
|
2018-05-10 23:27:48 -03:00
|
|
|
theta = 216.65f / SSL_AIR_TEMPERATURE;
|
|
|
|
delta = 0.2233611f * expf(-GMR * (h - 11.0f) / 216.65f);
|
2013-07-20 10:00:41 -03:00
|
|
|
}
|
|
|
|
|
2014-10-19 16:22:51 -03:00
|
|
|
sigma = delta/theta;
|
2013-07-20 10:00:41 -03:00
|
|
|
}
|
|
|
|
|
2018-05-07 16:23:37 -03:00
|
|
|
void AP_Baro::SimpleUnderWaterAtmosphere(
|
|
|
|
float alt, // depth, km.
|
|
|
|
float& rho, // density/sea-level
|
|
|
|
float& delta, // pressure/sea-level standard pressure
|
|
|
|
float& theta) // temperature/sea-level standard temperature
|
|
|
|
{
|
|
|
|
// Values and equations based on:
|
|
|
|
// https://en.wikipedia.org/wiki/Standard_sea_level
|
|
|
|
const float seaDensity = 1.024f; // g/cm3
|
|
|
|
const float maxSeaDensity = 1.028f; // g/cm3
|
|
|
|
const float pAC = maxSeaDensity - seaDensity; // pycnocline angular coefficient
|
|
|
|
|
|
|
|
// From: https://www.windows2universe.org/earth/Water/density.html
|
|
|
|
rho = seaDensity;
|
|
|
|
if (alt < 1.0f) {
|
|
|
|
// inside pycnocline
|
|
|
|
rho += pAC*alt;
|
|
|
|
} else {
|
|
|
|
rho += pAC;
|
|
|
|
}
|
|
|
|
rho = rho/seaDensity;
|
|
|
|
|
|
|
|
// From: https://www.grc.nasa.gov/www/k-12/WindTunnel/Activities/fluid_pressure.html
|
|
|
|
// \f$P = \rho (kg) \cdot gravity (m/s2) \cdot depth (m)\f$
|
|
|
|
// \f$P_{atmosphere} = 101.325 kPa\f$
|
|
|
|
// \f$P_{total} = P_{atmosphere} + P_{fluid}\f$
|
2018-05-10 23:27:48 -03:00
|
|
|
delta = (SSL_AIR_PRESSURE + (seaDensity * 1e3) * GRAVITY_MSS * (alt * 1e3)) / SSL_AIR_PRESSURE;
|
2018-05-07 16:23:37 -03:00
|
|
|
|
|
|
|
// From: http://residualanalysis.blogspot.com.br/2010/02/temperature-of-ocean-water-at-given.html
|
|
|
|
// \f$T(D)\f$ Temperature underwater at given temperature
|
|
|
|
// \f$S\f$ Surface temperature at the surface
|
|
|
|
// \f$T(D)\approx\frac{S}{1.8 \cdot 10^{-4} \cdot S \cdot T + 1}\f$
|
|
|
|
const float seaTempSurface = 15.0f; // Celsius
|
|
|
|
const float S = seaTempSurface * 0.338f;
|
|
|
|
theta = 1.0f / ((1.8e-4) * S * (alt * 1e3) + 1.0f);
|
|
|
|
}
|
2013-07-20 10:00:41 -03:00
|
|
|
|
2014-10-19 16:22:51 -03:00
|
|
|
/*
|
|
|
|
convert an altitude in meters above sea level to a presssure and temperature
|
|
|
|
*/
|
|
|
|
void AP_Baro::setHIL(float altitude_msl)
|
2011-10-13 11:22:03 -03:00
|
|
|
{
|
2013-07-20 10:00:41 -03:00
|
|
|
float sigma, delta, theta;
|
2013-05-02 02:26:38 -03:00
|
|
|
|
2014-07-08 07:26:54 -03:00
|
|
|
SimpleAtmosphere(altitude_msl*0.001f, sigma, delta, theta);
|
2018-05-10 23:27:48 -03:00
|
|
|
float p = SSL_AIR_PRESSURE * delta;
|
2018-05-10 21:00:32 -03:00
|
|
|
float T = 303.16f * theta - C_TO_KELVIN; // Assume 30 degrees at sea level - converted to degrees Kelvin
|
2013-05-02 02:26:38 -03:00
|
|
|
|
2016-05-04 21:10:04 -03:00
|
|
|
_hil.pressure = p;
|
|
|
|
_hil.temperature = T;
|
|
|
|
_hil.updated = true;
|
2011-10-13 11:22:03 -03:00
|
|
|
}
|
2011-12-11 00:30:24 -04:00
|
|
|
|
2014-10-19 16:22:51 -03:00
|
|
|
/*
|
|
|
|
set HIL pressure and temperature for an instance
|
|
|
|
*/
|
2016-05-04 21:36:35 -03:00
|
|
|
void AP_Baro::setHIL(uint8_t instance, float pressure, float temperature, float altitude, float climb_rate, uint32_t last_update_ms)
|
2014-10-19 16:22:51 -03:00
|
|
|
{
|
|
|
|
if (instance >= _num_sensors) {
|
|
|
|
// invalid
|
|
|
|
return;
|
|
|
|
}
|
2016-05-03 19:22:20 -03:00
|
|
|
_hil.pressure = pressure;
|
|
|
|
_hil.temperature = temperature;
|
2016-05-04 21:10:04 -03:00
|
|
|
_hil.altitude = altitude;
|
|
|
|
_hil.climb_rate = climb_rate;
|
2016-05-03 19:22:20 -03:00
|
|
|
_hil.updated = true;
|
2016-05-04 21:10:04 -03:00
|
|
|
_hil.have_alt = true;
|
2016-05-04 21:36:35 -03:00
|
|
|
|
|
|
|
if (last_update_ms != 0) {
|
|
|
|
_hil.last_update_ms = last_update_ms;
|
|
|
|
_hil.have_last_update = true;
|
|
|
|
}
|
2015-10-19 14:54:11 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Read the sensor
|
|
|
|
void AP_Baro_HIL::update(void)
|
|
|
|
{
|
2016-05-03 19:22:20 -03:00
|
|
|
if (_frontend._hil.updated) {
|
|
|
|
_frontend._hil.updated = false;
|
|
|
|
_copy_to_frontend(0, _frontend._hil.pressure, _frontend._hil.temperature);
|
2015-10-19 14:54:11 -03:00
|
|
|
}
|
2011-12-11 00:30:24 -04:00
|
|
|
}
|