2012-12-14 21:54:26 -04:00
|
|
|
/*
|
|
|
|
SITL handling
|
|
|
|
|
|
|
|
This simulates a compass
|
|
|
|
|
|
|
|
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"
|
2012-12-14 21:54:26 -04:00
|
|
|
|
2015-08-11 03:28:43 -03:00
|
|
|
#include <AP_Math/AP_Math.h>
|
2015-08-15 19:51:46 -03:00
|
|
|
#include <AP_Compass/AP_Compass.h>
|
|
|
|
#include <AP_Declination/AP_Declination.h>
|
|
|
|
#include <SITL/SITL.h>
|
2012-12-14 21:54:26 -04:00
|
|
|
|
2015-04-13 03:16:22 -03:00
|
|
|
extern const AP_HAL::HAL& hal;
|
|
|
|
|
2015-05-04 03:15:12 -03:00
|
|
|
using namespace HALSITL;
|
2012-12-14 21:54:26 -04:00
|
|
|
|
|
|
|
/*
|
|
|
|
setup the compass with new input
|
|
|
|
all inputs are in degrees
|
|
|
|
*/
|
2013-05-02 02:01:47 -03:00
|
|
|
void SITL_State::_update_compass(float rollDeg, float pitchDeg, float yawDeg)
|
2012-12-14 21:54:26 -04:00
|
|
|
{
|
2015-10-15 15:48:30 -03:00
|
|
|
static uint32_t last_update;
|
|
|
|
|
2015-04-13 03:16:22 -03:00
|
|
|
if (_compass == NULL) {
|
|
|
|
// no compass in this sketch
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
yawDeg += _sitl->mag_error;
|
|
|
|
if (yawDeg > 180.0f) {
|
|
|
|
yawDeg -= 360.0f;
|
|
|
|
}
|
|
|
|
if (yawDeg < -180.0f) {
|
|
|
|
yawDeg += 360.0f;
|
|
|
|
}
|
|
|
|
|
2015-11-03 09:46:29 -04:00
|
|
|
// 100Hz
|
2015-11-19 23:11:17 -04:00
|
|
|
uint32_t now = AP_HAL::millis();
|
2015-10-15 15:48:30 -03:00
|
|
|
if ((now - last_update) < 10) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
last_update = now;
|
|
|
|
|
2016-06-17 00:48:15 -03:00
|
|
|
// calculate sensor noise and add to 'truth' field in body frame
|
|
|
|
// units are milli-Gauss
|
|
|
|
Vector3f noise = _rand_vec3f() * _sitl->mag_noise;
|
|
|
|
Vector3f new_mag_data = _sitl->state.bodyMagField + noise;
|
|
|
|
|
2015-04-13 03:16:22 -03:00
|
|
|
// add delay
|
|
|
|
uint32_t best_time_delta_mag = 1000; // initialise large time representing buffer entry closest to current time - delay.
|
|
|
|
uint8_t best_index_mag = 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_mag >= 10) { // store data every 10 ms.
|
|
|
|
last_store_time_mag = now;
|
|
|
|
if (store_index_mag > mag_buffer_length-1) { // reset buffer index if index greater than size of buffer
|
|
|
|
store_index_mag = 0;
|
|
|
|
}
|
|
|
|
buffer_mag[store_index_mag].data = new_mag_data; // add data to current index
|
|
|
|
buffer_mag[store_index_mag].time = last_store_time_mag; // add time to current index
|
|
|
|
store_index_mag = store_index_mag + 1; // increment index
|
|
|
|
}
|
|
|
|
|
|
|
|
// return delayed measurement
|
|
|
|
delayed_time_mag = now - _sitl->mag_delay; // get time corresponding to delay
|
|
|
|
// find data corresponding to delayed time in buffer
|
|
|
|
for (uint8_t i=0; i<=mag_buffer_length-1; i++) {
|
2016-05-11 18:54:16 -03:00
|
|
|
// find difference between delayed time and time stamp in buffer
|
|
|
|
time_delta_mag = abs((int32_t)(delayed_time_mag - buffer_mag[i].time));
|
2015-04-13 03:16:22 -03:00
|
|
|
// if this difference is smaller than last delta, store this time
|
|
|
|
if (time_delta_mag < best_time_delta_mag) {
|
|
|
|
best_index_mag = i;
|
|
|
|
best_time_delta_mag = time_delta_mag;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (best_time_delta_mag < 1000) { // only output stored state if < 1 sec retrieval error
|
2015-04-20 19:29:51 -03:00
|
|
|
new_mag_data = buffer_mag[best_index_mag].data;
|
2015-04-13 03:16:22 -03:00
|
|
|
}
|
|
|
|
|
2015-04-20 19:29:51 -03:00
|
|
|
new_mag_data -= _sitl->mag_ofs.get();
|
|
|
|
|
2016-05-04 04:10:38 -03:00
|
|
|
_compass->setHIL(0, new_mag_data, AP_HAL::micros());
|
|
|
|
_compass->setHIL(1, new_mag_data, AP_HAL::micros());
|
2012-12-14 21:54:26 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|