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>
|
2020-09-12 16:04:38 -03:00
|
|
|
#if CONFIG_HAL_BOARD == HAL_BOARD_SITL && !defined(HAL_BUILD_AP_PERIPH)
|
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
|
|
|
|
2023-05-04 16:50:02 -03:00
|
|
|
// scaling value taken from AP_Airspeed_analog.cpp
|
|
|
|
#define VOLTS_TO_PASCAL 819
|
|
|
|
#define PASCAL_TO_VOLTS(_p) (_p/VOLTS_TO_PASCAL)
|
|
|
|
|
2012-12-14 21:54:26 -04:00
|
|
|
/*
|
|
|
|
convert airspeed in m/s to an airspeed sensor value
|
|
|
|
*/
|
2022-05-16 02:27:01 -03:00
|
|
|
void SITL_State::_update_airspeed(float true_airspeed)
|
2012-12-14 21:54:26 -04:00
|
|
|
{
|
2022-05-16 02:27:01 -03:00
|
|
|
for (uint8_t i=0; i<AIRSPEED_MAX_SENSORS; i++) {
|
|
|
|
const auto &arspd = _sitl->airspeed[i];
|
2024-04-28 20:30:55 -03:00
|
|
|
float airspeed = true_airspeed / AP_Baro::get_EAS2TAS_for_alt_amsl(_sitl->state.altitude);
|
2022-05-16 02:27:01 -03:00
|
|
|
const float diff_pressure = sq(airspeed) / arspd.ratio;
|
|
|
|
float airspeed_raw;
|
2018-03-09 04:07:50 -04:00
|
|
|
|
2022-05-16 02:27:01 -03:00
|
|
|
// apply noise to the differential pressure. This emulates the way
|
|
|
|
// airspeed noise reduces with speed
|
|
|
|
airspeed = sqrtf(fabsf(arspd.ratio*(diff_pressure + arspd.noise * rand_float())));
|
2017-08-01 17:44:32 -03:00
|
|
|
|
2022-05-16 02:27:01 -03:00
|
|
|
// check sensor failure
|
|
|
|
if (is_positive(arspd.fail)) {
|
|
|
|
airspeed = arspd.fail;
|
|
|
|
}
|
2017-09-20 00:13:49 -03:00
|
|
|
|
2022-05-16 02:27:01 -03:00
|
|
|
if (!is_zero(arspd.fail_pressure)) {
|
|
|
|
// compute a realistic pressure report given some level of trapper air pressure in the tube and our current altitude
|
|
|
|
// algorithm taken from https://en.wikipedia.org/wiki/Calibrated_airspeed#Calculation_from_impact_pressure
|
|
|
|
float tube_pressure = fabsf(arspd.fail_pressure - AP::baro().get_pressure() + arspd.fail_pitot_pressure);
|
|
|
|
airspeed = 340.29409348 * sqrt(5 * (pow((tube_pressure / SSL_AIR_PRESSURE + 1), 2.0/7.0) - 1.0));
|
|
|
|
}
|
|
|
|
float airspeed_pressure = (airspeed * airspeed) / arspd.ratio;
|
2017-09-20 00:13:49 -03:00
|
|
|
|
2022-05-16 02:27:01 -03:00
|
|
|
// flip sign here for simulating reversed pitot/static connections
|
|
|
|
if (arspd.signflip) {
|
|
|
|
airspeed_pressure *= -1;
|
|
|
|
}
|
2020-07-16 07:17:54 -03:00
|
|
|
|
2022-05-16 02:27:01 -03:00
|
|
|
// apply airspeed sensor offset in m/s
|
|
|
|
airspeed_raw = airspeed_pressure + arspd.offset;
|
2020-12-16 04:00:44 -04:00
|
|
|
|
2022-05-16 02:27:01 -03:00
|
|
|
_sitl->state.airspeed_raw_pressure[i] = airspeed_pressure;
|
2023-05-04 16:50:02 -03:00
|
|
|
|
|
|
|
airspeed_pin_voltage[i] = PASCAL_TO_VOLTS(airspeed_raw);
|
2015-04-13 03:16:22 -03:00
|
|
|
}
|
2012-12-14 21:54:26 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|