2012-12-14 21:54:26 -04:00
|
|
|
/*
|
|
|
|
SITL handling
|
|
|
|
|
|
|
|
This simulates a barometer
|
|
|
|
|
|
|
|
Andrew Tridgell November 2011
|
|
|
|
*/
|
|
|
|
|
2015-08-11 03:28:43 -03:00
|
|
|
#include <AP_HAL/AP_HAL.h>
|
|
|
|
#include <AP_Math/AP_Math.h>
|
2013-01-10 15:22:37 -04:00
|
|
|
|
2015-05-04 03:15:12 -03:00
|
|
|
#if CONFIG_HAL_BOARD == HAL_BOARD_SITL
|
2012-12-14 21:54:26 -04:00
|
|
|
|
2015-05-04 03:15:12 -03:00
|
|
|
#include "AP_HAL_SITL.h"
|
2012-12-14 21:54:26 -04:00
|
|
|
|
2015-05-04 03:15:12 -03:00
|
|
|
using namespace HALSITL;
|
2012-12-14 21:54:26 -04:00
|
|
|
|
|
|
|
extern const AP_HAL::HAL& hal;
|
|
|
|
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <stdint.h>
|
|
|
|
#include <math.h>
|
|
|
|
|
|
|
|
/*
|
|
|
|
setup the barometer with new input
|
|
|
|
altitude is in meters
|
|
|
|
*/
|
|
|
|
void SITL_State::_update_barometer(float altitude)
|
|
|
|
{
|
2015-04-13 03:16:22 -03:00
|
|
|
static uint32_t last_update;
|
|
|
|
|
|
|
|
float sim_alt = altitude;
|
|
|
|
|
|
|
|
if (_barometer == NULL) {
|
|
|
|
// this sketch doesn't use a barometer
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (_sitl->baro_disable) {
|
|
|
|
// barometer is disabled
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// 80Hz, to match the real APM2 barometer
|
|
|
|
uint32_t now = hal.scheduler->millis();
|
|
|
|
if ((now - last_update) < 12) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
last_update = now;
|
|
|
|
|
|
|
|
sim_alt += _sitl->baro_drift * now / 1000;
|
|
|
|
sim_alt += _sitl->baro_noise * _rand_float();
|
|
|
|
|
|
|
|
// add baro glitch
|
|
|
|
sim_alt += _sitl->baro_glitch;
|
|
|
|
|
|
|
|
// add delay
|
|
|
|
uint32_t best_time_delta_baro = 200; // initialise large time representing buffer entry closest to current time - delay.
|
|
|
|
uint8_t best_index_baro = 0; // initialise number representing the index of the entry in buffer closest to delay.
|
|
|
|
|
|
|
|
// storing data from sensor to buffer
|
|
|
|
if (now - last_store_time_baro >= 10) { // store data every 10 ms.
|
2015-05-04 21:59:07 -03:00
|
|
|
last_store_time_baro = now;
|
|
|
|
if (store_index_baro > baro_buffer_length-1) { // reset buffer index if index greater than size of buffer
|
|
|
|
store_index_baro = 0;
|
|
|
|
}
|
|
|
|
buffer_baro[store_index_baro].data = sim_alt; // add data to current index
|
|
|
|
buffer_baro[store_index_baro].time = last_store_time_baro; // add time_stamp_baro to current index
|
|
|
|
store_index_baro = store_index_baro + 1; // increment index
|
2015-04-13 03:16:22 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
// return delayed measurement
|
|
|
|
delayed_time_baro = now - _sitl->baro_delay; // get time corresponding to delay
|
|
|
|
|
|
|
|
// find data corresponding to delayed time in buffer
|
|
|
|
for (uint8_t i=0; i<=baro_buffer_length-1; i++) {
|
|
|
|
time_delta_baro = abs(delayed_time_baro - buffer_baro[i].time); // find difference between delayed time and time stamp in buffer
|
|
|
|
// if this difference is smaller than last delta, store this time
|
|
|
|
if (time_delta_baro < best_time_delta_baro) {
|
|
|
|
best_index_baro = i;
|
|
|
|
best_time_delta_baro = time_delta_baro;
|
2014-04-11 03:47:26 -03:00
|
|
|
}
|
2015-04-13 03:16:22 -03:00
|
|
|
}
|
|
|
|
if (best_time_delta_baro < 200) { // only output stored state if < 200 msec retrieval error
|
|
|
|
sim_alt = buffer_baro[best_index_baro].data;
|
|
|
|
}
|
2014-04-11 03:47:26 -03:00
|
|
|
|
2015-04-13 03:16:22 -03:00
|
|
|
_barometer->setHIL(sim_alt);
|
2012-12-14 21:54:26 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|