Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place. Commercial Alternative to JupyterHub.
Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place. Commercial Alternative to JupyterHub.
Path: blob/master/libraries/AP_Compass/AP_Compass_SITL.cpp
Views: 1798
#include "AP_Compass_SITL.h"12#if AP_COMPASS_SITL_ENABLED34#include <AP_HAL/AP_HAL.h>56extern const AP_HAL::HAL& hal;78AP_Compass_SITL::AP_Compass_SITL()9: _sitl(AP::sitl())10{11if (_sitl != nullptr) {12for (uint8_t i=0; i<MAX_CONNECTED_MAGS; i++) {13uint32_t dev_id = _sitl->mag_devid[i];14if (dev_id == 0) {15continue;16}17uint8_t instance;18if (!register_compass(dev_id, instance)) {19continue;20} else if (_num_compass<MAX_SITL_COMPASSES) {21_compass_instance[_num_compass] = instance;22set_dev_id(_compass_instance[_num_compass], dev_id);2324if (_sitl->mag_save_ids) {25// save so the compass always comes up configured in SITL26save_dev_id(_compass_instance[_num_compass]);27}28set_rotation(instance, ROTATION_NONE);29_num_compass++;30}31}3233// Scroll through the registered compasses, and set the offsets34for (uint8_t i=0; i<_num_compass; i++) {35if (_compass.get_offsets(i).is_zero()) {36_compass.set_offsets(i, _sitl->mag_ofs[i]);37}38}3940// we want to simulate a calibrated compass by default, so set41// scale to 142AP_Param::set_default_by_name("COMPASS_SCALE", 1);43AP_Param::set_default_by_name("COMPASS_SCALE2", 1);44AP_Param::set_default_by_name("COMPASS_SCALE3", 1);4546// make first compass external47set_external(_compass_instance[0], true);4849hal.scheduler->register_timer_process(FUNCTOR_BIND(this, &AP_Compass_SITL::_timer, void));50}51}525354/*55create correction matrix for diagonals and off-diagonals56*/57void AP_Compass_SITL::_setup_eliptical_correcion(uint8_t i)58{59Vector3f diag = _sitl->mag_diag[i].get();60if (diag.is_zero()) {61diag = {1,1,1};62}63const Vector3f &diagonals = diag;64const Vector3f &offdiagonals = _sitl->mag_offdiag[i];6566if (diagonals == _last_dia && offdiagonals == _last_odi) {67return;68}6970_eliptical_corr = Matrix3f(diagonals.x, offdiagonals.x, offdiagonals.y,71offdiagonals.x, diagonals.y, offdiagonals.z,72offdiagonals.y, offdiagonals.z, diagonals.z);73if (!_eliptical_corr.invert()) {74_eliptical_corr.identity();75}76_last_dia = diag;77_last_odi = offdiagonals;78}7980void AP_Compass_SITL::_timer()81{82// TODO: Refactor delay buffer with AP_Baro_SITL.8384// Sampled at 100Hz85uint32_t now = AP_HAL::millis();86if ((now - _last_sample_time) < 10) {87return;88}89_last_sample_time = now;9091// calculate sensor noise and add to 'truth' field in body frame92// units are milli-Gauss93Vector3f noise = rand_vec3f() * _sitl->mag_noise;94Vector3f new_mag_data = _sitl->state.bodyMagField + noise;9596// add delay97uint32_t best_time_delta = 1000; // initialise large time representing buffer entry closest to current time - delay.98uint8_t best_index = 0; // initialise number representing the index of the entry in buffer closest to delay.99100// storing data from sensor to buffer101if (now - last_store_time >= 10) { // store data every 10 ms.102last_store_time = now;103if (store_index > buffer_length-1) { // reset buffer index if index greater than size of buffer104store_index = 0;105}106buffer[store_index].data = new_mag_data; // add data to current index107buffer[store_index].time = last_store_time; // add time to current index108store_index = store_index + 1; // increment index109}110111// return delayed measurement112uint32_t delayed_time = now - _sitl->mag_delay; // get time corresponding to delay113// find data corresponding to delayed time in buffer114for (uint8_t i=0; i<=buffer_length-1; i++) {115// find difference between delayed time and time stamp in buffer116uint32_t time_delta = abs((int32_t)(delayed_time - buffer[i].time));117// if this difference is smaller than last delta, store this time118if (time_delta < best_time_delta) {119best_index= i;120best_time_delta = time_delta;121}122}123if (best_time_delta < 1000) { // only output stored state if < 1 sec retrieval error124new_mag_data = buffer[best_index].data;125}126127for (uint8_t i=0; i<_num_compass; i++) {128_setup_eliptical_correcion(i);129Vector3f f = (_eliptical_corr * new_mag_data) - _sitl->mag_ofs[i].get();130// rotate compass131f.rotate_inverse((enum Rotation)_sitl->mag_orient[i].get());132f.rotate(get_board_orientation());133// scale the compass to simulate sensor scale factor errors134f *= _sitl->mag_scaling[i];135136switch (_sitl->mag_fail[i]) {137case 0:138accumulate_sample(f, _compass_instance[i], 10);139_last_data[i] = f;140break;141case 1:142// no data143break;144case 2:145// frozen compass146accumulate_sample(_last_data[i], _compass_instance[i], 10);147break;148}149}150}151152void AP_Compass_SITL::read()153{154for (uint8_t i=0; i<_num_compass; i++) {155drain_accumulated_samples(_compass_instance[i], nullptr);156}157}158#endif // AP_COMPASS_SITL_ENABLED159160161