2016-03-10 20:41:18 -04:00
|
|
|
#include <AP_Math/AP_Math.h>
|
2017-08-18 05:58:08 -03:00
|
|
|
#include <AP_AHRS/AP_AHRS.h>
|
2016-03-10 20:41:18 -04:00
|
|
|
|
2017-08-18 05:58:08 -03:00
|
|
|
#include <AP_Compass/AP_Compass.h>
|
|
|
|
#include <AP_Declination/AP_Declination.h>
|
2019-01-18 00:23:42 -04:00
|
|
|
#include <AP_Logger/AP_Logger.h>
|
2014-02-15 22:21:06 -04:00
|
|
|
|
2017-08-18 05:58:08 -03:00
|
|
|
#include "Compass_learn.h"
|
2018-10-19 02:07:53 -03:00
|
|
|
#include <GCS_MAVLink/GCS.h>
|
2017-08-18 05:58:08 -03:00
|
|
|
|
|
|
|
#include <stdio.h>
|
2020-01-06 19:06:54 -04:00
|
|
|
#include <AP_Vehicle/AP_Vehicle.h>
|
2017-08-18 05:58:08 -03:00
|
|
|
|
2019-05-26 22:49:12 -03:00
|
|
|
#if COMPASS_LEARN_ENABLED
|
|
|
|
|
2017-08-18 05:58:08 -03:00
|
|
|
extern const AP_HAL::HAL &hal;
|
|
|
|
|
2021-03-03 02:30:24 -04:00
|
|
|
static const uint8_t COMPASS_LEARN_NUM_SAMPLES = 30;
|
|
|
|
static const uint8_t COMPASS_LEARN_BEST_ERROR_THRESHOLD = 50;
|
|
|
|
static const uint8_t COMPASS_LEARN_WORST_ERROR_THRESHOLD = 65;
|
|
|
|
|
2017-08-18 05:58:08 -03:00
|
|
|
// constructor
|
2018-10-19 02:07:53 -03:00
|
|
|
CompassLearn::CompassLearn(Compass &_compass) :
|
2017-08-18 05:58:08 -03:00
|
|
|
compass(_compass)
|
|
|
|
{
|
2018-10-19 02:07:53 -03:00
|
|
|
gcs().send_text(MAV_SEVERITY_INFO, "CompassLearn: Initialised");
|
2019-11-20 03:18:10 -04:00
|
|
|
for (Compass::Priority i(0); i<compass.get_count(); i++) {
|
2020-02-23 20:34:32 -04:00
|
|
|
if (compass._use_for_yaw[Compass::Priority(i)]) {
|
2019-11-26 18:11:54 -04:00
|
|
|
// reset scale factors, we can't learn scale factors in
|
|
|
|
// flight
|
2019-11-20 03:18:10 -04:00
|
|
|
compass.set_and_save_scale_factor(uint8_t(i), 0.0);
|
2019-11-26 18:11:54 -04:00
|
|
|
}
|
|
|
|
}
|
2017-08-18 05:58:08 -03:00
|
|
|
}
|
2014-02-15 22:33:41 -04:00
|
|
|
|
2014-02-15 22:21:06 -04:00
|
|
|
/*
|
2017-08-18 05:58:08 -03:00
|
|
|
update when new compass sample available
|
2014-02-15 22:21:06 -04:00
|
|
|
*/
|
2017-08-18 05:58:08 -03:00
|
|
|
void CompassLearn::update(void)
|
2014-02-15 22:21:06 -04:00
|
|
|
{
|
2020-01-06 19:06:54 -04:00
|
|
|
const AP_Vehicle *vehicle = AP::vehicle();
|
2017-08-18 05:58:08 -03:00
|
|
|
if (converged || compass.get_learn_type() != Compass::LEARN_INFLIGHT ||
|
2020-01-06 19:06:54 -04:00
|
|
|
!hal.util->get_soft_armed() || vehicle->get_time_flying_ms() < 3000) {
|
2017-08-18 05:58:08 -03:00
|
|
|
// only learn when flying and with enough time to be clear of
|
|
|
|
// the ground
|
2014-02-15 22:21:06 -04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-01-06 19:06:54 -04:00
|
|
|
const AP_AHRS &ahrs = AP::ahrs();
|
2017-08-18 05:58:08 -03:00
|
|
|
if (!have_earth_field) {
|
|
|
|
Location loc;
|
|
|
|
if (!ahrs.get_position(loc)) {
|
|
|
|
// need to wait till we have a global position
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-06-01 08:01:43 -03:00
|
|
|
// setup the expected earth field in mGauss at this location
|
|
|
|
mag_ef = AP_Declination::get_earth_field_ga(loc) * 1000;
|
2017-08-18 05:58:08 -03:00
|
|
|
have_earth_field = true;
|
|
|
|
|
|
|
|
// form eliptical correction matrix and invert it. This is
|
|
|
|
// needed to remove the effects of the eliptical correction
|
|
|
|
// when calculating new offsets
|
2019-11-20 03:18:10 -04:00
|
|
|
const Vector3f &diagonals = compass.get_diagonals(0);
|
|
|
|
const Vector3f &offdiagonals = compass.get_offdiagonals(0);
|
2017-08-18 05:58:08 -03:00
|
|
|
mat = Matrix3f(
|
|
|
|
diagonals.x, offdiagonals.x, offdiagonals.y,
|
|
|
|
offdiagonals.x, diagonals.y, offdiagonals.z,
|
|
|
|
offdiagonals.y, offdiagonals.z, diagonals.z
|
|
|
|
);
|
|
|
|
if (!mat.invert()) {
|
|
|
|
// if we can't invert, use the identity matrix
|
|
|
|
mat.identity();
|
|
|
|
}
|
|
|
|
|
|
|
|
// set initial error to field intensity
|
2019-06-01 08:01:43 -03:00
|
|
|
float intensity = mag_ef.length();
|
2017-08-18 05:58:08 -03:00
|
|
|
for (uint16_t i=0; i<num_sectors; i++) {
|
2019-06-01 08:01:43 -03:00
|
|
|
errors[i] = intensity;
|
2014-02-15 22:21:06 -04:00
|
|
|
}
|
2021-03-03 02:30:24 -04:00
|
|
|
|
2018-10-19 02:07:53 -03:00
|
|
|
gcs().send_text(MAV_SEVERITY_INFO, "CompassLearn: have earth field");
|
2017-08-18 05:58:08 -03:00
|
|
|
hal.scheduler->register_io_process(FUNCTOR_BIND_MEMBER(&CompassLearn::io_timer, void));
|
|
|
|
}
|
|
|
|
|
2018-10-20 07:46:36 -03:00
|
|
|
AP_Notify::flags.compass_cal_running = true;
|
|
|
|
|
2017-08-18 05:58:08 -03:00
|
|
|
if (sample_available) {
|
|
|
|
// last sample still being processed by IO thread
|
2014-02-15 22:21:06 -04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-11-20 03:18:10 -04:00
|
|
|
Vector3f field = compass.get_field(0);
|
2017-08-18 05:58:08 -03:00
|
|
|
Vector3f field_change = field - last_field;
|
|
|
|
if (field_change.length() < min_field_change) {
|
|
|
|
return;
|
|
|
|
}
|
2018-10-11 20:35:03 -03:00
|
|
|
|
|
|
|
{
|
|
|
|
WITH_SEMAPHORE(sem);
|
2017-08-18 05:58:08 -03:00
|
|
|
// give a sample to the backend to process
|
|
|
|
new_sample.field = field;
|
2019-11-20 03:18:10 -04:00
|
|
|
new_sample.offsets = compass.get_offsets(0);
|
2017-08-18 05:58:08 -03:00
|
|
|
new_sample.attitude = Vector3f(ahrs.roll, ahrs.pitch, ahrs.yaw);
|
|
|
|
sample_available = true;
|
|
|
|
last_field = field;
|
|
|
|
num_samples++;
|
|
|
|
}
|
2014-02-15 22:21:06 -04:00
|
|
|
|
2017-08-18 05:58:08 -03:00
|
|
|
if (sample_available) {
|
2021-03-03 02:30:24 -04:00
|
|
|
// @LoggerMessage: COFS
|
|
|
|
// @Description: Current compass learn offsets
|
|
|
|
// @Field: TimeUS: Time since system startup
|
|
|
|
// @Field: OfsX: best learnt offset, x-axis
|
|
|
|
// @Field: OfsY: best learnt offset, y-axis
|
|
|
|
// @Field: OfsZ: best learnt offset, z-axis
|
|
|
|
// @Field: Var: error of best offset vector
|
|
|
|
// @Field: Yaw: best learnt yaw
|
|
|
|
// @Field: WVar: error of best learn yaw
|
|
|
|
// @Field: N: number of samples used
|
2019-01-18 00:24:08 -04:00
|
|
|
AP::logger().Write("COFS", "TimeUS,OfsX,OfsY,OfsZ,Var,Yaw,WVar,N", "QffffffI",
|
2017-08-18 05:58:08 -03:00
|
|
|
AP_HAL::micros64(),
|
2018-02-09 19:52:31 -04:00
|
|
|
(double)best_offsets.x,
|
|
|
|
(double)best_offsets.y,
|
|
|
|
(double)best_offsets.z,
|
|
|
|
(double)best_error,
|
|
|
|
(double)best_yaw_deg,
|
|
|
|
(double)worst_error,
|
2017-08-18 05:58:08 -03:00
|
|
|
num_samples);
|
|
|
|
}
|
2014-02-15 22:33:41 -04:00
|
|
|
|
2018-10-11 20:35:03 -03:00
|
|
|
if (!converged) {
|
|
|
|
WITH_SEMAPHORE(sem);
|
|
|
|
|
2018-10-19 02:20:18 -03:00
|
|
|
// set offsets to current best guess
|
2019-11-20 03:18:10 -04:00
|
|
|
compass.set_offsets(0, best_offsets);
|
2018-10-19 02:20:18 -03:00
|
|
|
|
|
|
|
// set non-primary offsets to match primary
|
2019-11-20 03:18:10 -04:00
|
|
|
Vector3f field_primary = compass.get_field(0);
|
|
|
|
for (uint8_t i=1; i<compass.get_count(); i++) {
|
2020-02-23 20:34:32 -04:00
|
|
|
if (!compass._use_for_yaw[Compass::Priority(i)]) {
|
2018-10-19 02:20:18 -03:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
Vector3f field2 = compass.get_field(i);
|
|
|
|
Vector3f new_offsets = compass.get_offsets(i) + (field_primary - field2);
|
|
|
|
compass.set_offsets(i, new_offsets);
|
|
|
|
}
|
|
|
|
|
2017-08-18 05:58:08 -03:00
|
|
|
// stop updating the offsets once converged
|
2021-03-03 02:30:24 -04:00
|
|
|
if (num_samples > COMPASS_LEARN_NUM_SAMPLES &&
|
|
|
|
best_error < COMPASS_LEARN_BEST_ERROR_THRESHOLD &&
|
|
|
|
worst_error > COMPASS_LEARN_WORST_ERROR_THRESHOLD) {
|
2017-08-18 05:58:08 -03:00
|
|
|
// set the offsets and enable compass for EKF use. Let the
|
|
|
|
// EKF learn the remaining compass offset error
|
2018-10-19 02:20:18 -03:00
|
|
|
for (uint8_t i=0; i<compass.get_count(); i++) {
|
2020-02-23 20:34:32 -04:00
|
|
|
if (compass._use_for_yaw[Compass::Priority(i)]) {
|
2018-10-19 02:20:18 -03:00
|
|
|
compass.save_offsets(i);
|
2019-11-26 18:11:54 -04:00
|
|
|
compass.set_and_save_scale_factor(i, 0.0);
|
2018-10-19 02:20:18 -03:00
|
|
|
}
|
|
|
|
}
|
2018-10-19 04:36:13 -03:00
|
|
|
compass.set_learn_type(Compass::LEARN_NONE, true);
|
2018-10-19 20:43:49 -03:00
|
|
|
// setup so use can trigger it again
|
|
|
|
converged = false;
|
|
|
|
sample_available = false;
|
|
|
|
num_samples = 0;
|
|
|
|
have_earth_field = false;
|
|
|
|
memset(predicted_offsets, 0, sizeof(predicted_offsets));
|
|
|
|
worst_error = 0;
|
|
|
|
best_error = 0;
|
|
|
|
best_yaw_deg = 0;
|
|
|
|
best_offsets.zero();
|
2018-10-19 02:07:53 -03:00
|
|
|
gcs().send_text(MAV_SEVERITY_INFO, "CompassLearn: finished");
|
2018-10-20 07:46:36 -03:00
|
|
|
AP_Notify::flags.compass_cal_running = false;
|
|
|
|
AP_Notify::events.compass_cal_saved = true;
|
2014-02-15 22:21:06 -04:00
|
|
|
}
|
2017-08-18 05:58:08 -03:00
|
|
|
}
|
|
|
|
}
|
2014-02-15 22:21:06 -04:00
|
|
|
|
2017-08-18 05:58:08 -03:00
|
|
|
/*
|
|
|
|
we run the math intensive calculations in the IO thread
|
|
|
|
*/
|
|
|
|
void CompassLearn::io_timer(void)
|
|
|
|
{
|
|
|
|
if (!sample_available) {
|
|
|
|
return;
|
|
|
|
}
|
2018-10-11 20:35:03 -03:00
|
|
|
|
2017-08-18 05:58:08 -03:00
|
|
|
struct sample s;
|
2018-10-11 20:35:03 -03:00
|
|
|
|
|
|
|
{
|
|
|
|
WITH_SEMAPHORE(sem);
|
|
|
|
s = new_sample;
|
|
|
|
sample_available = false;
|
2017-08-18 05:58:08 -03:00
|
|
|
}
|
2014-02-15 22:21:06 -04:00
|
|
|
|
2017-08-18 05:58:08 -03:00
|
|
|
process_sample(s);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
process a new compass sample
|
|
|
|
*/
|
|
|
|
void CompassLearn::process_sample(const struct sample &s)
|
|
|
|
{
|
|
|
|
uint16_t besti = 0;
|
|
|
|
float bestv = 0, worstv=0;
|
|
|
|
|
|
|
|
/*
|
|
|
|
we run through the 72 possible yaw error values, and for each
|
|
|
|
one we calculate a value for the compass offsets if that yaw
|
|
|
|
error is correct.
|
|
|
|
*/
|
|
|
|
for (uint16_t i=0; i<num_sectors; i++) {
|
|
|
|
float yaw_err_deg = i*(360/num_sectors);
|
|
|
|
|
|
|
|
// form rotation matrix for the euler attitude
|
|
|
|
Matrix3f dcm;
|
|
|
|
dcm.from_euler(s.attitude.x, s.attitude.y, wrap_2PI(s.attitude.z + radians(yaw_err_deg)));
|
|
|
|
|
|
|
|
// calculate the field we would expect to get if this yaw error is correct
|
|
|
|
Vector3f expected_field = dcm.transposed() * mag_ef;
|
|
|
|
|
|
|
|
// calculate a value for the compass offsets for this yaw error
|
|
|
|
Vector3f v1 = mat * s.field;
|
|
|
|
Vector3f v2 = mat * expected_field;
|
|
|
|
Vector3f offsets = (v2 - v1) + s.offsets;
|
|
|
|
float delta = (offsets - predicted_offsets[i]).length();
|
|
|
|
|
|
|
|
if (num_samples == 1) {
|
|
|
|
predicted_offsets[i] = offsets;
|
|
|
|
} else {
|
|
|
|
// lowpass the predicted offsets and the error
|
2019-04-03 21:36:58 -03:00
|
|
|
const float learn_rate = 0.92f;
|
2017-08-18 05:58:08 -03:00
|
|
|
predicted_offsets[i] = predicted_offsets[i] * learn_rate + offsets * (1-learn_rate);
|
|
|
|
errors[i] = errors[i] * learn_rate + delta * (1-learn_rate);
|
|
|
|
}
|
2014-02-15 22:33:41 -04:00
|
|
|
|
2017-08-18 05:58:08 -03:00
|
|
|
// keep track of the current best prediction
|
|
|
|
if (i == 0 || errors[i] < bestv) {
|
|
|
|
besti = i;
|
|
|
|
bestv = errors[i];
|
2014-02-15 22:33:41 -04:00
|
|
|
}
|
2017-08-18 05:58:08 -03:00
|
|
|
// also keep the worst error. This is used as part of the convergence test
|
|
|
|
if (i == 0 || errors[i] > worstv) {
|
|
|
|
worstv = errors[i];
|
|
|
|
}
|
|
|
|
}
|
2014-02-15 22:33:41 -04:00
|
|
|
|
2018-10-11 20:35:03 -03:00
|
|
|
WITH_SEMAPHORE(sem);
|
|
|
|
|
|
|
|
// pass the current estimate to the front-end
|
|
|
|
best_offsets = predicted_offsets[besti];
|
|
|
|
best_error = bestv;
|
|
|
|
worst_error = worstv;
|
|
|
|
best_yaw_deg = wrap_360(degrees(s.attitude.z) + besti * (360/num_sectors));
|
2021-03-03 02:30:24 -04:00
|
|
|
|
|
|
|
// send current learn state to gcs
|
|
|
|
const uint32_t now = AP_HAL::millis();
|
|
|
|
if (!converged && now - last_learn_progress_sent_ms >= 5000) {
|
|
|
|
float percent = (MIN(num_samples / COMPASS_LEARN_NUM_SAMPLES, 1.0f) +
|
|
|
|
MIN(COMPASS_LEARN_BEST_ERROR_THRESHOLD / (best_error + 1.0f), 1.0f) +
|
|
|
|
MIN(worst_error / COMPASS_LEARN_WORST_ERROR_THRESHOLD, 1.0f)) / 3.0f * 100.f;
|
|
|
|
gcs().send_text(MAV_SEVERITY_INFO, "CompassLearn: %d%%", (int) percent);
|
|
|
|
last_learn_progress_sent_ms = now;
|
|
|
|
}
|
2014-02-15 22:21:06 -04:00
|
|
|
}
|
2019-05-26 22:49:12 -03:00
|
|
|
|
|
|
|
#endif // COMPASS_LEARN_ENABLED
|