2011-12-28 05:32:21 -04:00
|
|
|
/// -*- tab-width: 4; Mode: C++; c-basic-offset: 4; indent-tabs-mode: nil -*-
|
2011-10-13 11:22:03 -03:00
|
|
|
|
2012-07-05 03:26:56 -03:00
|
|
|
#include <AP_Baro.h>
|
2013-05-02 02:08:42 -03:00
|
|
|
#include "AP_Baro_HIL.h"
|
2012-10-11 14:53:21 -03:00
|
|
|
#include <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
|
|
|
|
|
|
|
// Public Methods //////////////////////////////////////////////////////////////
|
2013-05-02 02:08:42 -03:00
|
|
|
bool AP_Baro_HIL::init()
|
2011-10-13 11:22:03 -03:00
|
|
|
{
|
2012-08-17 03:09:23 -03:00
|
|
|
return true;
|
2011-10-13 11:22:03 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Read the sensor. This is a state machine
|
|
|
|
// We read one time Temperature (state = 1) and then 4 times Pressure (states 2-5)
|
2013-05-02 02:08:42 -03:00
|
|
|
uint8_t AP_Baro_HIL::read()
|
2011-10-13 11:22:03 -03:00
|
|
|
{
|
2012-08-17 03:09:23 -03:00
|
|
|
uint8_t result = 0;
|
2011-10-13 11:22:03 -03:00
|
|
|
|
2012-07-05 03:26:56 -03:00
|
|
|
if (_count != 0) {
|
2013-10-17 03:21:36 -03:00
|
|
|
hal.scheduler->suspend_timer_procs();
|
2012-07-05 03:26:56 -03:00
|
|
|
result = 1;
|
2014-01-05 01:36:12 -04:00
|
|
|
Press = _pressure_sum / _count;
|
|
|
|
Temp = _temperature_sum / _count;
|
2012-07-05 03:26:56 -03:00
|
|
|
_pressure_samples = _count;
|
|
|
|
_count = 0;
|
|
|
|
_pressure_sum = 0;
|
|
|
|
_temperature_sum = 0;
|
2013-10-17 03:21:36 -03:00
|
|
|
hal.scheduler->resume_timer_procs();
|
2012-08-17 03:09:23 -03:00
|
|
|
}
|
2012-07-05 03:26:56 -03:00
|
|
|
|
2012-08-17 03:09:23 -03:00
|
|
|
return result;
|
2011-10-13 11:22:03 -03:00
|
|
|
}
|
|
|
|
|
2014-02-17 18:12:20 -04:00
|
|
|
void AP_Baro_HIL::setHIL(float pressure, float temperature)
|
|
|
|
{
|
|
|
|
_count = 1;
|
|
|
|
_pressure_sum = pressure;
|
|
|
|
_temperature_sum = temperature;
|
|
|
|
_last_update = hal.scheduler->millis();
|
|
|
|
healthy = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-07-20 10:00:41 -03:00
|
|
|
// ==========================================================================
|
|
|
|
// based on tables.cpp from http://www.pdas.com/atmosdownload.html
|
|
|
|
|
|
|
|
/*
|
|
|
|
Compute the temperature, density, and pressure in the standard atmosphere
|
|
|
|
Correct to 20 km. Only approximate thereafter.
|
|
|
|
*/
|
|
|
|
static void SimpleAtmosphere(
|
|
|
|
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
|
|
|
|
{
|
|
|
|
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.0)
|
|
|
|
{ // Troposphere
|
|
|
|
theta=(288.15f-6.5f*h)/288.15f;
|
|
|
|
delta=powf(theta, GMR/6.5f);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{ // Stratosphere
|
|
|
|
theta=216.65f/288.15f;
|
|
|
|
delta=0.2233611f*expf(-GMR*(h-11.0f)/216.65f);
|
|
|
|
}
|
|
|
|
|
|
|
|
sigma=delta/theta;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-05-02 02:26:38 -03:00
|
|
|
void AP_Baro_HIL::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-07-20 01:37:47 -03:00
|
|
|
const float p0 = 101325;
|
2013-05-02 02:26:38 -03:00
|
|
|
|
2013-07-20 10:00:41 -03:00
|
|
|
SimpleAtmosphere(altitude_msl*0.001, sigma, delta, theta);
|
|
|
|
float p = p0 * delta;
|
2013-07-20 18:41:10 -03:00
|
|
|
float T = 303.16 * theta - 273.16; // Assume 30 degrees at sea level - converted to degrees Kelvin
|
2013-05-02 02:26:38 -03:00
|
|
|
|
2014-02-17 18:12:20 -04:00
|
|
|
setHIL(p, T);
|
2011-10-13 11:22:03 -03:00
|
|
|
}
|
2011-12-11 00:30:24 -04:00
|
|
|
|
2013-05-02 02:08:42 -03:00
|
|
|
float AP_Baro_HIL::get_pressure() {
|
2011-12-11 00:30:24 -04:00
|
|
|
return Press;
|
|
|
|
}
|
|
|
|
|
2013-05-02 02:08:42 -03:00
|
|
|
float AP_Baro_HIL::get_temperature() {
|
2011-12-11 00:30:24 -04:00
|
|
|
return Temp;
|
|
|
|
}
|