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>
|
|
|
|
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
|
|
|
BMP085_State=1;
|
|
|
|
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) {
|
|
|
|
result = 1;
|
2012-07-06 02:11:22 -03:00
|
|
|
Press = ((float)_pressure_sum) / _count;
|
|
|
|
Temp = ((float)_temperature_sum) / _count;
|
2012-07-05 03:26:56 -03:00
|
|
|
_pressure_samples = _count;
|
|
|
|
_count = 0;
|
|
|
|
_pressure_sum = 0;
|
|
|
|
_temperature_sum = 0;
|
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
|
|
|
}
|
|
|
|
|
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-05-02 02:26:38 -03:00
|
|
|
// approximate a barometer. This uses the typical base pressure in
|
|
|
|
// Canberra, Australia
|
2013-05-02 08:31:04 -03:00
|
|
|
const float temperature = 312;
|
2013-05-02 02:26:38 -03:00
|
|
|
|
|
|
|
float y = (altitude_msl - 584.0) / 29.271267;
|
2013-05-02 08:31:04 -03:00
|
|
|
y /= (temperature / 10.0) + 273.15;
|
2013-05-02 02:26:38 -03:00
|
|
|
y = 1.0/exp(y);
|
|
|
|
y *= 95446.0;
|
|
|
|
|
2012-07-05 03:26:56 -03:00
|
|
|
_count++;
|
2013-05-02 02:26:38 -03:00
|
|
|
_pressure_sum += y;
|
2013-05-02 08:31:04 -03:00
|
|
|
_temperature_sum += temperature;
|
2012-07-05 03:26:56 -03:00
|
|
|
if (_count == 128) {
|
|
|
|
// we have summed 128 values. This only happens
|
|
|
|
// when we stop reading the barometer for a long time
|
|
|
|
// (more than 1.2 seconds)
|
|
|
|
_count = 64;
|
|
|
|
_pressure_sum /= 2;
|
|
|
|
_temperature_sum /= 2;
|
|
|
|
}
|
|
|
|
|
2012-08-17 03:09:23 -03:00
|
|
|
healthy = true;
|
2012-10-11 14:53:21 -03:00
|
|
|
_last_update = hal.scheduler->millis();
|
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;
|
|
|
|
}
|
|
|
|
|
2013-05-02 02:08:42 -03:00
|
|
|
int32_t AP_Baro_HIL::get_raw_pressure() {
|
2011-12-11 00:30:24 -04:00
|
|
|
return Press;
|
|
|
|
}
|
|
|
|
|
2013-05-02 02:08:42 -03:00
|
|
|
int32_t AP_Baro_HIL::get_raw_temp() {
|
2011-12-11 00:30:24 -04:00
|
|
|
return Temp;
|
|
|
|
}
|