2021-10-29 22:15:47 -03:00
|
|
|
#include "AP_Baro_SITL.h"
|
2017-04-12 08:17:19 -03:00
|
|
|
|
2021-10-29 22:15:47 -03:00
|
|
|
#if AP_SIM_BARO_ENABLED
|
2017-04-12 08:17:19 -03:00
|
|
|
|
2021-10-29 22:15:47 -03:00
|
|
|
#include <AP_HAL/AP_HAL.h>
|
|
|
|
#include <AP_Vehicle/AP_Vehicle_Type.h>
|
2017-04-12 08:17:19 -03:00
|
|
|
|
|
|
|
extern const AP_HAL::HAL& hal;
|
|
|
|
|
|
|
|
/*
|
|
|
|
constructor - registers instance at top Baro driver
|
|
|
|
*/
|
|
|
|
AP_Baro_SITL::AP_Baro_SITL(AP_Baro &baro) :
|
2024-09-21 02:21:57 -03:00
|
|
|
AP_Baro_Backend(baro),
|
2018-05-10 20:32:23 -03:00
|
|
|
_sitl(AP::sitl()),
|
2024-09-21 02:21:57 -03:00
|
|
|
_has_sample(false)
|
2017-04-12 08:17:19 -03:00
|
|
|
{
|
2017-07-24 08:00:36 -03:00
|
|
|
if (_sitl != nullptr) {
|
2017-07-24 05:57:05 -03:00
|
|
|
_instance = _frontend.register_sensor();
|
2018-05-07 16:24:57 -03:00
|
|
|
#if APM_BUILD_TYPE(APM_BUILD_ArduSub)
|
|
|
|
_frontend.set_type(_instance, AP_Baro::BARO_TYPE_WATER);
|
|
|
|
#endif
|
2020-07-18 04:12:41 -03:00
|
|
|
set_bus_id(_instance, AP_HAL::Device::make_bus_id(AP_HAL::Device::BUS_TYPE_SITL, 0, _instance, DEVTYPE_BARO_SITL));
|
2017-06-21 11:58:39 -03:00
|
|
|
hal.scheduler->register_timer_process(FUNCTOR_BIND(this, &AP_Baro_SITL::_timer, void));
|
2017-04-12 08:17:19 -03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-08-16 04:00:20 -03:00
|
|
|
// adjust for board temperature warmup on start-up
|
2017-04-12 19:49:08 -03:00
|
|
|
void AP_Baro_SITL::temperature_adjustment(float &p, float &T)
|
|
|
|
{
|
2017-07-24 05:57:21 -03:00
|
|
|
const float tsec = AP_HAL::millis() * 0.001f;
|
2023-01-03 21:44:30 -04:00
|
|
|
const float T_sensor = T + AP::sitl()->temp_board_offset;
|
|
|
|
const float tconst = AP::sitl()->temp_tconst;
|
2021-08-16 04:00:20 -03:00
|
|
|
if (tsec < 23 * tconst) { // time which past the equation below equals T_sensor within approx. 1E-9
|
2023-01-03 21:44:30 -04:00
|
|
|
const float T0 = AP::sitl()->temp_start;
|
2021-08-16 04:00:20 -03:00
|
|
|
T = T_sensor - (T_sensor - T0) * expf(-tsec / tconst);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
T = T_sensor;
|
|
|
|
}
|
|
|
|
|
2023-01-03 21:44:30 -04:00
|
|
|
const float baro_factor = AP::sitl()->temp_baro_factor;
|
2017-07-24 05:57:21 -03:00
|
|
|
const float Tzero = 30.0f; // start baro adjustment at 30C
|
|
|
|
if (is_positive(baro_factor)) {
|
2017-04-12 19:49:08 -03:00
|
|
|
// this produces a pressure change with temperature that
|
|
|
|
// closely matches what has been observed with a ICM-20789
|
|
|
|
// barometer. A typical factor is 1.2.
|
|
|
|
p -= powf(MAX(T - Tzero, 0), baro_factor);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-06-21 11:58:39 -03:00
|
|
|
void AP_Baro_SITL::_timer()
|
2017-04-12 08:17:19 -03:00
|
|
|
{
|
2017-06-21 11:58:39 -03:00
|
|
|
|
|
|
|
// 100Hz
|
2017-07-24 05:57:21 -03:00
|
|
|
const uint32_t now = AP_HAL::millis();
|
2017-06-21 11:58:39 -03:00
|
|
|
if ((now - _last_sample_time) < 10) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
_last_sample_time = now;
|
|
|
|
|
2017-07-24 08:00:36 -03:00
|
|
|
float sim_alt = _sitl->state.altitude;
|
2017-04-12 08:17:19 -03:00
|
|
|
|
2020-11-24 21:28:11 -04:00
|
|
|
if (_sitl->baro[_instance].disable) {
|
2017-04-12 08:17:19 -03:00
|
|
|
// barometer is disabled
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2024-04-29 01:30:27 -03:00
|
|
|
const auto drift_delta_t_ms = now - last_drift_delta_t_ms;
|
|
|
|
last_drift_delta_t_ms = now;
|
|
|
|
total_alt_drift += _sitl->baro[_instance].drift * drift_delta_t_ms * 0.001f;
|
|
|
|
|
|
|
|
sim_alt += total_alt_drift;
|
2020-11-24 21:28:11 -04:00
|
|
|
sim_alt += _sitl->baro[_instance].noise * rand_float();
|
2017-04-12 08:17:19 -03:00
|
|
|
|
|
|
|
// add baro glitch
|
2020-11-24 21:28:11 -04:00
|
|
|
sim_alt += _sitl->baro[_instance].glitch;
|
2017-04-12 08:17:19 -03:00
|
|
|
|
|
|
|
// add delay
|
2017-07-24 05:57:21 -03:00
|
|
|
uint32_t best_time_delta = 200; // initialise large time representing buffer entry closest to current time - delay.
|
|
|
|
uint8_t best_index = 0; // initialise number representing the index of the entry in buffer closest to delay.
|
2017-04-12 08:17:19 -03:00
|
|
|
|
|
|
|
// storing data from sensor to buffer
|
2017-07-24 08:00:36 -03:00
|
|
|
if (now - _last_store_time >= 10) { // store data every 10 ms.
|
|
|
|
_last_store_time = now;
|
|
|
|
if (_store_index > _buffer_length - 1) { // reset buffer index if index greater than size of buffer
|
|
|
|
_store_index = 0;
|
2017-04-12 08:17:19 -03:00
|
|
|
}
|
2020-08-21 16:15:40 -03:00
|
|
|
|
|
|
|
// if freezed barometer, report altitude to last recorded altitude
|
2020-11-24 21:28:11 -04:00
|
|
|
if (_sitl->baro[_instance].freeze == 1) {
|
2020-08-21 16:15:40 -03:00
|
|
|
sim_alt = _last_altitude;
|
|
|
|
} else {
|
|
|
|
_last_altitude = sim_alt;
|
|
|
|
}
|
|
|
|
|
2017-07-24 08:00:36 -03:00
|
|
|
_buffer[_store_index].data = sim_alt; // add data to current index
|
|
|
|
_buffer[_store_index].time = _last_store_time; // add time_stamp to current index
|
|
|
|
_store_index = _store_index + 1; // increment index
|
2017-04-12 08:17:19 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
// return delayed measurement
|
2020-11-24 21:28:11 -04:00
|
|
|
const uint32_t delayed_time = now - _sitl->baro[_instance].delay; // get time corresponding to delay
|
2017-04-12 08:17:19 -03:00
|
|
|
|
|
|
|
// find data corresponding to delayed time in buffer
|
2017-07-24 08:00:36 -03:00
|
|
|
for (uint8_t i = 0; i <= _buffer_length - 1; i++) {
|
2017-04-12 08:17:19 -03:00
|
|
|
// find difference between delayed time and time stamp in buffer
|
|
|
|
uint32_t time_delta = abs(
|
2017-07-24 08:00:36 -03:00
|
|
|
(int32_t)(delayed_time - _buffer[i].time));
|
2017-04-12 08:17:19 -03:00
|
|
|
// if this difference is smaller than last delta, store this time
|
|
|
|
if (time_delta < best_time_delta) {
|
|
|
|
best_index = i;
|
|
|
|
best_time_delta = time_delta;
|
|
|
|
}
|
|
|
|
}
|
2017-07-24 05:57:21 -03:00
|
|
|
if (best_time_delta < 200) { // only output stored state if < 200 msec retrieval error
|
2017-07-24 08:00:36 -03:00
|
|
|
sim_alt = _buffer[best_index].data;
|
2017-04-12 08:17:19 -03:00
|
|
|
}
|
|
|
|
|
2018-05-07 16:24:57 -03:00
|
|
|
#if !APM_BUILD_TYPE(APM_BUILD_ArduSub)
|
2024-04-28 03:53:12 -03:00
|
|
|
float p, T_K;
|
|
|
|
AP_Baro::get_pressure_temperature_for_alt_amsl(sim_alt, p, T_K);
|
|
|
|
float T = KELVIN_TO_C(T_K);
|
2017-04-12 19:49:08 -03:00
|
|
|
temperature_adjustment(p, T);
|
2018-05-07 16:24:57 -03:00
|
|
|
#else
|
|
|
|
float rho, delta, theta;
|
|
|
|
AP_Baro::SimpleUnderWaterAtmosphere(-sim_alt * 0.001f, rho, delta, theta);
|
2018-05-10 23:27:48 -03:00
|
|
|
float p = SSL_AIR_PRESSURE * delta;
|
2022-01-12 08:03:24 -04:00
|
|
|
float T = KELVIN_TO_C(SSL_AIR_TEMPERATURE * theta);
|
2018-05-07 16:24:57 -03:00
|
|
|
#endif
|
2017-06-21 11:58:39 -03:00
|
|
|
|
2020-11-24 22:44:22 -04:00
|
|
|
// add in correction for wind effects
|
2023-01-03 21:44:30 -04:00
|
|
|
p += wind_pressure_correction(_instance);
|
2020-11-24 22:44:22 -04:00
|
|
|
|
2017-06-21 11:58:39 -03:00
|
|
|
_recent_press = p;
|
|
|
|
_recent_temp = T;
|
|
|
|
_has_sample = true;
|
|
|
|
}
|
|
|
|
|
2020-06-30 04:03:24 -03:00
|
|
|
// unhealthy if baro is turned off or beyond supported instances
|
|
|
|
bool AP_Baro_SITL::healthy(uint8_t instance)
|
|
|
|
{
|
2024-04-28 03:53:12 -03:00
|
|
|
return _last_sample_time != 0 && !_sitl->baro[instance].disable;
|
2020-06-30 04:03:24 -03:00
|
|
|
}
|
|
|
|
|
2017-06-21 11:58:39 -03:00
|
|
|
// Read the sensor
|
|
|
|
void AP_Baro_SITL::update(void)
|
|
|
|
{
|
2018-10-11 20:35:03 -03:00
|
|
|
if (!_has_sample) {
|
|
|
|
return;
|
2017-06-21 11:58:39 -03:00
|
|
|
}
|
2018-10-11 20:35:03 -03:00
|
|
|
|
|
|
|
WITH_SEMAPHORE(_sem);
|
|
|
|
_copy_to_frontend(_instance, _recent_press, _recent_temp);
|
|
|
|
_has_sample = false;
|
2017-04-12 08:17:19 -03:00
|
|
|
}
|
|
|
|
|
2020-11-24 22:44:22 -04:00
|
|
|
/*
|
|
|
|
return pressure correction for wind based on SIM_BARO_WCF parameters
|
|
|
|
*/
|
2023-01-03 21:44:30 -04:00
|
|
|
float AP_Baro_SITL::wind_pressure_correction(uint8_t instance)
|
2020-11-24 22:44:22 -04:00
|
|
|
{
|
2023-01-03 21:44:30 -04:00
|
|
|
const auto &bp = AP::sitl()->baro[instance];
|
2020-11-24 22:44:22 -04:00
|
|
|
|
|
|
|
// correct for static pressure position errors
|
2023-01-03 21:44:30 -04:00
|
|
|
const Vector3f &airspeed_vec_bf = AP::sitl()->state.velocity_air_bf;
|
2020-11-24 22:44:22 -04:00
|
|
|
|
|
|
|
float error = 0.0;
|
|
|
|
const float sqx = sq(airspeed_vec_bf.x);
|
|
|
|
const float sqy = sq(airspeed_vec_bf.y);
|
2022-11-22 16:57:25 -04:00
|
|
|
const float sqz = sq(airspeed_vec_bf.z);
|
2020-11-24 22:44:22 -04:00
|
|
|
|
|
|
|
if (is_positive(airspeed_vec_bf.x)) {
|
|
|
|
error += bp.wcof_xp * sqx;
|
|
|
|
} else {
|
|
|
|
error += bp.wcof_xn * sqx;
|
|
|
|
}
|
|
|
|
if (is_positive(airspeed_vec_bf.y)) {
|
|
|
|
error += bp.wcof_yp * sqy;
|
|
|
|
} else {
|
|
|
|
error += bp.wcof_yn * sqy;
|
|
|
|
}
|
2022-11-22 16:57:25 -04:00
|
|
|
if (is_positive(airspeed_vec_bf.z)) {
|
|
|
|
error += bp.wcof_zp * sqz;
|
|
|
|
} else {
|
|
|
|
error += bp.wcof_zn * sqz;
|
|
|
|
}
|
2020-11-24 22:44:22 -04:00
|
|
|
|
2024-04-28 03:53:12 -03:00
|
|
|
return error * 0.5 * SSL_AIR_DENSITY * AP::baro()._get_air_density_ratio();
|
2020-11-24 22:44:22 -04:00
|
|
|
}
|
|
|
|
|
2021-10-29 22:15:47 -03:00
|
|
|
#endif // AP_SIM_BARO_ENABLED
|