123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130 |
- #include "AP_Compass_SITL.h"
- #include <AP_HAL/AP_HAL.h>
- #if CONFIG_HAL_BOARD == HAL_BOARD_SITL
- extern const AP_HAL::HAL& hal;
- AP_Compass_SITL::AP_Compass_SITL()
- : _sitl(AP::sitl())
- {
- if (_sitl != nullptr) {
- _compass._setup_earth_field();
- for (uint8_t i=0; i<SITL_NUM_COMPASSES; i++) {
-
- if (_compass.get_offsets(i).is_zero()) {
- _compass.set_offsets(i, _sitl->mag_ofs);
- }
-
- _compass_instance[i] = register_compass();
- set_dev_id(_compass_instance[i], AP_HAL::Device::make_bus_id(AP_HAL::Device::BUS_TYPE_SITL, i, 0, DEVTYPE_SITL));
-
- save_dev_id(_compass_instance[i]);
- }
-
-
- set_external(_compass_instance[0], true);
- hal.scheduler->register_timer_process(FUNCTOR_BIND(this, &AP_Compass_SITL::_timer, void));
- }
- }
- void AP_Compass_SITL::_setup_eliptical_correcion(void)
- {
- Vector3f diag = _sitl->mag_diag.get();
- if (diag.is_zero()) {
- diag(1,1,1);
- }
- const Vector3f &diagonals = diag;
- const Vector3f &offdiagonals = _sitl->mag_offdiag;
-
- if (diagonals == _last_dia && offdiagonals == _last_odi) {
- return;
- }
-
- _eliptical_corr = Matrix3f(diagonals.x, offdiagonals.x, offdiagonals.y,
- offdiagonals.x, diagonals.y, offdiagonals.z,
- offdiagonals.y, offdiagonals.z, diagonals.z);
- if (!_eliptical_corr.invert()) {
- _eliptical_corr.identity();
- }
- _last_dia = diag;
- _last_odi = offdiagonals;
- }
- void AP_Compass_SITL::_timer()
- {
-
-
- uint32_t now = AP_HAL::millis();
- if ((now - _last_sample_time) < 10) {
- return;
- }
- _last_sample_time = now;
-
-
- Vector3f noise = rand_vec3f() * _sitl->mag_noise;
- Vector3f new_mag_data = _sitl->state.bodyMagField + noise;
-
- uint32_t best_time_delta = 1000;
- uint8_t best_index = 0;
-
- if (now - last_store_time >= 10) {
- last_store_time = now;
- if (store_index > buffer_length-1) {
- store_index = 0;
- }
- buffer[store_index].data = new_mag_data;
- buffer[store_index].time = last_store_time;
- store_index = store_index + 1;
- }
-
- uint32_t delayed_time = now - _sitl->mag_delay;
-
- for (uint8_t i=0; i<=buffer_length-1; i++) {
-
- uint32_t time_delta = abs((int32_t)(delayed_time - buffer[i].time));
-
- if (time_delta < best_time_delta) {
- best_index= i;
- best_time_delta = time_delta;
- }
- }
- if (best_time_delta < 1000) {
- new_mag_data = buffer[best_index].data;
- }
- _setup_eliptical_correcion();
-
- new_mag_data = _eliptical_corr * new_mag_data;
- new_mag_data -= _sitl->mag_ofs.get();
- for (uint8_t i=0; i<SITL_NUM_COMPASSES; i++) {
- Vector3f f = new_mag_data;
- if (i == 0) {
-
- f.rotate_inverse((enum Rotation)_sitl->mag_orient.get());
- f.rotate(get_board_orientation());
- }
-
- accumulate_sample(f, _compass_instance[i], 10);
- }
- }
- void AP_Compass_SITL::read()
- {
- for (uint8_t i=0; i<SITL_NUM_COMPASSES; i++) {
- drain_accumulated_samples(_compass_instance[i], nullptr);
- }
- }
- #endif
|