2014-11-15 21:58:23 -04:00
|
|
|
/// -*- tab-width: 4; Mode: C++; c-basic-offset: 4; indent-tabs-mode: nil -*-
|
|
|
|
|
|
|
|
#include <AP_HAL.h>
|
|
|
|
#include "Compass.h"
|
|
|
|
#include "AP_Compass_Backend.h"
|
|
|
|
|
2015-02-23 19:17:44 -04:00
|
|
|
extern const AP_HAL::HAL& hal;
|
|
|
|
|
2014-11-15 21:58:23 -04:00
|
|
|
AP_Compass_Backend::AP_Compass_Backend(Compass &compass) :
|
2015-02-23 19:17:44 -04:00
|
|
|
_compass(compass)
|
2015-01-19 12:58:26 -04:00
|
|
|
{}
|
|
|
|
|
|
|
|
/*
|
|
|
|
copy latest data to the frontend from a backend
|
|
|
|
*/
|
2015-02-23 19:17:44 -04:00
|
|
|
void AP_Compass_Backend::publish_field(const Vector3f &mag, uint8_t instance)
|
|
|
|
{
|
|
|
|
Compass::mag_state &state = _compass._state[instance];
|
|
|
|
|
|
|
|
state.field = mag;
|
2015-01-19 12:58:26 -04:00
|
|
|
|
|
|
|
// apply default board orientation for this compass type. This is
|
|
|
|
// a noop on most boards
|
2015-02-23 19:17:44 -04:00
|
|
|
state.field.rotate(MAG_BOARD_ORIENTATION);
|
2015-01-19 12:58:26 -04:00
|
|
|
|
2015-02-23 19:17:44 -04:00
|
|
|
if (!state.external) {
|
2015-01-19 12:58:26 -04:00
|
|
|
// and add in AHRS_ORIENTATION setting if not an external compass
|
2015-02-23 19:17:44 -04:00
|
|
|
state.field.rotate(_compass._board_orientation);
|
2015-03-13 02:43:34 -03:00
|
|
|
} else {
|
|
|
|
// add user selectable orientation
|
|
|
|
state.field.rotate((enum Rotation)state.orientation.get());
|
2015-02-23 19:17:44 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
apply_corrections(state.field, instance);
|
|
|
|
|
|
|
|
state.last_update_ms = hal.scheduler->millis();
|
2015-03-13 22:31:23 -03:00
|
|
|
_compass._last_update_usec = hal.scheduler->micros();
|
2015-02-23 19:17:44 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
register a new backend with frontend, returning instance which
|
|
|
|
should be used in publish_field()
|
|
|
|
*/
|
|
|
|
uint8_t AP_Compass_Backend::register_compass(void) const
|
|
|
|
{
|
|
|
|
return _compass.register_compass();
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
apply offset and motor compensation corrections
|
|
|
|
*/
|
|
|
|
void AP_Compass_Backend::apply_corrections(Vector3f &mag, uint8_t i)
|
|
|
|
{
|
|
|
|
Compass::mag_state &state = _compass._state[i];
|
|
|
|
const Vector3f &offsets = state.offset.get();
|
|
|
|
const Vector3f &mot = state.motor_compensation.get();
|
|
|
|
|
|
|
|
/*
|
|
|
|
note that _motor_offset[] is kept even if compensation is not
|
|
|
|
being applied so it can be logged correctly
|
|
|
|
*/
|
|
|
|
mag += offsets;
|
|
|
|
if(_compass._motor_comp_type != AP_COMPASS_MOT_COMP_DISABLED && _compass._thr_or_curr != 0.0f) {
|
|
|
|
state.motor_offset = mot * _compass._thr_or_curr;
|
|
|
|
mag += state.motor_offset;
|
|
|
|
} else {
|
|
|
|
state.motor_offset.zero();
|
2015-01-19 12:58:26 -04:00
|
|
|
}
|
2015-02-23 19:17:44 -04:00
|
|
|
}
|
2015-01-19 12:58:26 -04:00
|
|
|
|
2015-02-23 19:17:44 -04:00
|
|
|
|
|
|
|
/*
|
|
|
|
set dev_id for an instance
|
|
|
|
*/
|
|
|
|
void AP_Compass_Backend::set_dev_id(uint8_t instance, uint32_t dev_id)
|
|
|
|
{
|
|
|
|
#if COMPASS_MAX_INSTANCES > 1
|
|
|
|
_compass._state[instance].dev_id.set(dev_id);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
set external for an instance
|
|
|
|
*/
|
|
|
|
void AP_Compass_Backend::set_external(uint8_t instance, bool external)
|
|
|
|
{
|
|
|
|
_compass._state[instance].external.set(external);
|
|
|
|
}
|