2012-12-14 21:54:26 -04:00
|
|
|
/*
|
|
|
|
SITL handling
|
|
|
|
|
2017-05-11 10:38:32 -03:00
|
|
|
This simulates an analog airspeed sensor
|
2012-12-14 21:54:26 -04:00
|
|
|
|
|
|
|
Andrew Tridgell November 2011
|
|
|
|
*/
|
|
|
|
|
2015-08-11 03:28:43 -03:00
|
|
|
#include <AP_HAL/AP_HAL.h>
|
2015-05-04 03:15:12 -03:00
|
|
|
#if CONFIG_HAL_BOARD == HAL_BOARD_SITL
|
2012-12-14 21:54:26 -04:00
|
|
|
|
2015-08-11 03:28:43 -03:00
|
|
|
#include "AP_HAL_SITL.h"
|
2015-05-04 03:15:12 -03:00
|
|
|
#include "AP_HAL_SITL_Namespace.h"
|
|
|
|
#include "HAL_SITL_Class.h"
|
2017-05-11 10:52:29 -03:00
|
|
|
#include "SITL_State.h"
|
2015-08-15 19:51:46 -03:00
|
|
|
#include <SITL/SITL.h>
|
2015-08-11 03:28:43 -03:00
|
|
|
#include <AP_Math/AP_Math.h>
|
2012-12-14 21:54:26 -04:00
|
|
|
|
2015-04-13 03:16:22 -03:00
|
|
|
extern const AP_HAL::HAL& hal;
|
2013-11-26 01:42:43 -04:00
|
|
|
|
2015-05-04 03:15:12 -03:00
|
|
|
using namespace HALSITL;
|
2012-12-14 21:54:26 -04:00
|
|
|
|
|
|
|
/*
|
|
|
|
convert airspeed in m/s to an airspeed sensor value
|
|
|
|
*/
|
2017-05-11 10:38:32 -03:00
|
|
|
void SITL_State::_update_airspeed(float airspeed)
|
2012-12-14 21:54:26 -04:00
|
|
|
{
|
2015-05-04 21:59:07 -03:00
|
|
|
const float airspeed_ratio = 1.9936f;
|
2017-05-11 10:48:20 -03:00
|
|
|
const float airspeed_offset = 2013.0f;
|
2015-05-04 21:59:07 -03:00
|
|
|
|
2017-05-11 10:38:32 -03:00
|
|
|
// Check sensor failure
|
|
|
|
airspeed = is_zero(_sitl->arspd_fail) ? airspeed : _sitl->arspd_fail;
|
|
|
|
// Add noise
|
|
|
|
airspeed = airspeed + (_sitl->arspd_noise * rand_float());
|
|
|
|
|
2017-08-01 17:44:32 -03:00
|
|
|
if (!is_zero(_sitl->arspd_fail_pressure)) {
|
|
|
|
// compute a realistic pressure report given some level of trapper air pressure in the tube abd our current altitude
|
|
|
|
// algorithim taken from https://en.wikipedia.org/wiki/Calibrated_airspeed#Calculation_from_impact_pressure
|
|
|
|
float tube_pressure = abs(_sitl->arspd_fail_pressure - _barometer->get_pressure() + _sitl->arspd_fail_pitot_pressure);
|
|
|
|
airspeed = 340.29409348 * sqrt(5 * (pow((tube_pressure / 101325.01576 + 1), 2.0/7.0) - 1.0));
|
|
|
|
}
|
|
|
|
|
2017-05-11 10:48:20 -03:00
|
|
|
const float airspeed_pressure = (airspeed * airspeed) / airspeed_ratio;
|
|
|
|
float airspeed_raw = airspeed_pressure + airspeed_offset;
|
|
|
|
if (airspeed_raw / 4 > 0xFFFF) {
|
2017-05-11 10:38:32 -03:00
|
|
|
airspeed_pin_value = 0xFFFF;
|
|
|
|
return;
|
2015-05-04 21:59:07 -03:00
|
|
|
}
|
2015-04-13 03:16:22 -03:00
|
|
|
// add delay
|
2017-05-11 10:48:20 -03:00
|
|
|
const uint32_t now = AP_HAL::millis();
|
|
|
|
uint32_t best_time_delta_wind = 200; // initialise large time representing buffer entry closest to current time - delay.
|
|
|
|
uint8_t best_index_wind = 0; // initialise number representing the index of the entry in buffer closest to delay.
|
2015-04-13 03:16:22 -03:00
|
|
|
|
|
|
|
// storing data from sensor to buffer
|
2017-05-11 10:48:20 -03:00
|
|
|
if (now - last_store_time_wind >= 10) { // store data every 10 ms.
|
2015-04-13 03:16:22 -03:00
|
|
|
last_store_time_wind = now;
|
2017-05-11 10:48:20 -03:00
|
|
|
if (store_index_wind > wind_buffer_length - 1) { // reset buffer index if index greater than size of buffer
|
2015-04-13 03:16:22 -03:00
|
|
|
store_index_wind = 0;
|
|
|
|
}
|
2017-05-11 10:48:20 -03:00
|
|
|
buffer_wind[store_index_wind].data = airspeed_raw; // add data to current index
|
|
|
|
buffer_wind[store_index_wind].time = last_store_time_wind; // add time to current index
|
|
|
|
store_index_wind = store_index_wind + 1; // increment index
|
2015-04-13 03:16:22 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
// return delayed measurement
|
2017-05-11 10:48:20 -03:00
|
|
|
delayed_time_wind = now - _sitl->wind_delay; // get time corresponding to delay
|
2015-04-13 03:16:22 -03:00
|
|
|
// find data corresponding to delayed time in buffer
|
2017-05-11 10:48:20 -03:00
|
|
|
for (uint8_t i = 0; i <= wind_buffer_length - 1; i++) {
|
2016-05-11 18:54:16 -03:00
|
|
|
// find difference between delayed time and time stamp in buffer
|
|
|
|
time_delta_wind = abs(
|
|
|
|
(int32_t)(delayed_time_wind - buffer_wind[i].time));
|
2015-04-13 03:16:22 -03:00
|
|
|
// if this difference is smaller than last delta, store this time
|
|
|
|
if (time_delta_wind < best_time_delta_wind) {
|
|
|
|
best_index_wind = i;
|
|
|
|
best_time_delta_wind = time_delta_wind;
|
|
|
|
}
|
|
|
|
}
|
2017-05-11 10:48:20 -03:00
|
|
|
if (best_time_delta_wind < 200) { // only output stored state if < 200 msec retrieval error
|
2015-04-13 03:16:22 -03:00
|
|
|
airspeed_raw = buffer_wind[best_index_wind].data;
|
|
|
|
}
|
|
|
|
|
2017-05-11 10:48:20 -03:00
|
|
|
airspeed_pin_value = airspeed_raw / 4;
|
2012-12-14 21:54:26 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|