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>
|
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>
|
2020-01-06 19:06:54 -04:00
|
|
|
#include <AP_Vehicle/AP_Vehicle.h>
|
2021-10-14 05:48:47 -03:00
|
|
|
#include <AP_NavEKF/EKFGSF_yaw.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;
|
|
|
|
|
|
|
|
// constructor
|
2018-10-19 02:07:53 -03:00
|
|
|
CompassLearn::CompassLearn(Compass &_compass) :
|
2017-08-18 05:58:08 -03:00
|
|
|
compass(_compass)
|
|
|
|
{
|
2023-09-02 02:21:34 -03:00
|
|
|
GCS_SEND_TEXT(MAV_SEVERITY_INFO, "CompassLearn: Initialised");
|
2017-08-18 05:58:08 -03:00
|
|
|
}
|
2014-02-15 22:33:41 -04:00
|
|
|
|
2021-10-14 05:48:47 -03:00
|
|
|
// accuracy threshold applied for GSF yaw estimate
|
|
|
|
#define YAW_ACCURACY_THRESHOLD_DEG 5.0
|
|
|
|
|
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();
|
2021-10-14 05:48:47 -03:00
|
|
|
if (compass.get_learn_type() != Compass::LEARN_INFLIGHT ||
|
|
|
|
!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;
|
|
|
|
}
|
|
|
|
|
2021-10-14 05:48:47 -03:00
|
|
|
const auto &ahrs = AP::ahrs();
|
|
|
|
const auto *gsf = ahrs.get_yaw_estimator();
|
|
|
|
if (gsf == nullptr) {
|
|
|
|
// no GSF available
|
2014-02-15 22:21:06 -04:00
|
|
|
return;
|
|
|
|
}
|
2021-10-14 05:48:47 -03:00
|
|
|
if (degrees(fabsf(ahrs.get_pitch())) > 50) {
|
|
|
|
// we don't want to be too close to nose up, or yaw gets
|
|
|
|
// problematic. Tailsitters need to wait till they are in
|
|
|
|
// forward flight
|
2017-08-18 05:58:08 -03:00
|
|
|
return;
|
|
|
|
}
|
2018-10-11 20:35:03 -03:00
|
|
|
|
2021-10-14 05:48:47 -03:00
|
|
|
AP_Notify::flags.compass_cal_running = true;
|
2014-02-15 22:21:06 -04:00
|
|
|
|
2021-10-14 05:48:47 -03:00
|
|
|
ftype yaw_rad, yaw_variance;
|
2022-05-09 22:28:33 -03:00
|
|
|
uint8_t n_clips;
|
|
|
|
if (!gsf->getYawData(yaw_rad, yaw_variance, &n_clips) ||
|
2021-10-14 05:48:47 -03:00
|
|
|
!is_positive(yaw_variance) ||
|
2022-05-09 22:28:33 -03:00
|
|
|
n_clips > 1 ||
|
2021-10-14 05:48:47 -03:00
|
|
|
yaw_variance >= sq(radians(YAW_ACCURACY_THRESHOLD_DEG))) {
|
|
|
|
// not converged
|
2017-08-18 05:58:08 -03:00
|
|
|
return;
|
|
|
|
}
|
2018-10-11 20:35:03 -03:00
|
|
|
|
2024-02-01 20:12:03 -04:00
|
|
|
const bool result = compass.mag_cal_fixed_yaw(degrees(yaw_rad), (1U<<HAL_COMPASS_MAX_SENSORS)-1, 0, 0, true);
|
|
|
|
if (result) {
|
2021-10-14 05:48:47 -03:00
|
|
|
AP_Notify::flags.compass_cal_running = false;
|
|
|
|
compass.set_learn_type(Compass::LEARN_NONE, true);
|
2023-09-02 02:21:34 -03:00
|
|
|
GCS_SEND_TEXT(MAV_SEVERITY_INFO, "CompassLearn: Finished");
|
2021-03-03 02:30:24 -04:00
|
|
|
}
|
2014-02-15 22:21:06 -04:00
|
|
|
}
|
2019-05-26 22:49:12 -03:00
|
|
|
|
|
|
|
#endif // COMPASS_LEARN_ENABLED
|