2015-08-11 03:28:42 -03:00
|
|
|
#include <AP_HAL/AP_HAL.h>
|
|
|
|
#include <AP_Notify/AP_Notify.h>
|
2019-11-26 18:45:02 -04:00
|
|
|
#include <AP_GPS/AP_GPS.h>
|
2015-09-06 19:58:11 -03:00
|
|
|
#include <GCS_MAVLink/GCS.h>
|
2019-11-20 23:53:43 -04:00
|
|
|
#include <AP_AHRS/AP_AHRS.h>
|
2021-09-17 06:12:44 -03:00
|
|
|
#include <AP_InternalError/AP_InternalError.h>
|
2015-03-18 20:42:30 -03:00
|
|
|
|
2016-03-10 20:41:18 -04:00
|
|
|
#include "AP_Compass.h"
|
|
|
|
|
2019-10-11 11:57:44 -03:00
|
|
|
const extern AP_HAL::HAL& hal;
|
2015-08-28 18:03:09 -03:00
|
|
|
|
2019-05-26 22:49:12 -03:00
|
|
|
#if COMPASS_CAL_ENABLED
|
|
|
|
|
2019-11-14 22:14:24 -04:00
|
|
|
void Compass::cal_update()
|
2015-03-18 20:42:30 -03:00
|
|
|
{
|
2019-03-25 06:06:34 -03:00
|
|
|
if (hal.util->get_soft_armed()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-09-02 15:19:15 -03:00
|
|
|
bool running = false;
|
2015-03-18 20:42:30 -03:00
|
|
|
|
2019-11-20 03:18:10 -04:00
|
|
|
for (Priority i(0); i<COMPASS_MAX_INSTANCES; i++) {
|
2020-04-14 11:09:12 -03:00
|
|
|
if (_calibrator[i] == nullptr) {
|
|
|
|
continue;
|
2015-03-18 21:49:08 -03:00
|
|
|
}
|
2020-04-14 11:09:12 -03:00
|
|
|
if (_calibrator[i]->failed()) {
|
2015-03-18 21:49:08 -03:00
|
|
|
AP_Notify::events.compass_cal_failed = 1;
|
2015-03-18 20:42:30 -03:00
|
|
|
}
|
|
|
|
|
2020-04-14 11:09:12 -03:00
|
|
|
if (_calibrator[i]->running()) {
|
2015-09-02 15:19:15 -03:00
|
|
|
running = true;
|
2020-04-14 11:09:12 -03:00
|
|
|
} else if (_cal_autosave && !_cal_saved[i] && _calibrator[i]->get_state().status == CompassCalibrator::Status::SUCCESS) {
|
2019-11-20 03:18:10 -04:00
|
|
|
_accept_calibration(uint8_t(i));
|
2015-03-18 20:42:30 -03:00
|
|
|
}
|
|
|
|
}
|
2015-09-02 15:19:15 -03:00
|
|
|
|
|
|
|
AP_Notify::flags.compass_cal_running = running;
|
|
|
|
|
2015-08-27 23:44:33 -03:00
|
|
|
if (is_calibrating()) {
|
|
|
|
_cal_has_run = true;
|
|
|
|
return;
|
2016-10-25 02:16:11 -03:00
|
|
|
} else if (_cal_has_run && _auto_reboot()) {
|
2015-08-27 23:44:33 -03:00
|
|
|
hal.scheduler->delay(1000);
|
|
|
|
hal.scheduler->reboot(false);
|
|
|
|
}
|
2015-03-18 20:42:30 -03:00
|
|
|
}
|
|
|
|
|
2019-11-14 22:14:24 -04:00
|
|
|
bool Compass::_start_calibration(uint8_t i, bool retry, float delay)
|
2015-03-18 20:42:30 -03:00
|
|
|
{
|
2015-09-02 18:58:05 -03:00
|
|
|
if (!healthy(i)) {
|
2015-03-18 20:42:30 -03:00
|
|
|
return false;
|
|
|
|
}
|
2016-10-25 02:16:11 -03:00
|
|
|
if (!use_for_yaw(i)) {
|
|
|
|
return false;
|
|
|
|
}
|
2019-11-20 03:18:10 -04:00
|
|
|
Priority prio = Priority(i);
|
2021-05-18 06:55:43 -03:00
|
|
|
|
|
|
|
#if COMPASS_MAX_INSTANCES > 1
|
2019-11-20 03:18:10 -04:00
|
|
|
if (_priority_did_list[prio] != _priority_did_stored_list[prio]) {
|
|
|
|
gcs().send_text(MAV_SEVERITY_ERROR, "Compass cal requires reboot after priority change");
|
|
|
|
return false;
|
|
|
|
}
|
2021-05-18 06:55:43 -03:00
|
|
|
#endif
|
2020-04-14 11:09:12 -03:00
|
|
|
|
|
|
|
if (_calibrator[prio] == nullptr) {
|
|
|
|
_calibrator[prio] = new CompassCalibrator();
|
|
|
|
if (_calibrator[prio] == nullptr) {
|
|
|
|
gcs().send_text(MAV_SEVERITY_ERROR, "Compass cal object not initialised");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-26 18:45:02 -04:00
|
|
|
if (_options.get() & uint16_t(Option::CAL_REQUIRE_GPS)) {
|
|
|
|
if (AP::gps().status() < AP_GPS::GPS_OK_FIX_2D) {
|
|
|
|
gcs().send_text(MAV_SEVERITY_ERROR, "Compass cal requires GPS lock");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
2016-10-25 02:16:11 -03:00
|
|
|
if (!is_calibrating()) {
|
2015-09-02 18:58:05 -03:00
|
|
|
AP_Notify::events.initiated_compass_cal = 1;
|
|
|
|
}
|
2020-04-14 11:09:12 -03:00
|
|
|
|
|
|
|
if (_rotate_auto) {
|
2021-12-04 00:22:56 -04:00
|
|
|
enum Rotation r = _get_state(prio).external?(enum Rotation)_get_state(prio).orientation.get():ROTATION_NONE;
|
2020-04-14 11:09:12 -03:00
|
|
|
if (r != ROTATION_CUSTOM) {
|
2021-12-04 00:22:56 -04:00
|
|
|
_calibrator[prio]->set_orientation(r, _get_state(prio).external, _rotate_auto>=2, _rotate_auto>=3);
|
2020-04-14 11:09:12 -03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
_cal_saved[prio] = false;
|
2021-12-04 00:22:56 -04:00
|
|
|
if (i == 0 && _get_state(prio).external != 0) {
|
2020-04-14 11:09:12 -03:00
|
|
|
_calibrator[prio]->start(retry, delay, get_offsets_max(), i, _calibration_threshold);
|
2015-09-02 18:58:05 -03:00
|
|
|
} else {
|
2016-05-18 03:32:32 -03:00
|
|
|
// internal compasses or secondary compasses get twice the
|
|
|
|
// threshold. This is because internal compasses tend to be a
|
|
|
|
// lot noisier
|
2020-04-14 11:09:12 -03:00
|
|
|
_calibrator[prio]->start(retry, delay, get_offsets_max(), i, _calibration_threshold*2);
|
2015-09-02 18:58:05 -03:00
|
|
|
}
|
2020-04-14 11:09:12 -03:00
|
|
|
if (!_cal_thread_started) {
|
|
|
|
_cal_requires_reboot = true;
|
|
|
|
if (!hal.scheduler->thread_create(FUNCTOR_BIND(this, &Compass::_update_calibration_trampoline, void), "compasscal", 2048, AP_HAL::Scheduler::PRIORITY_IO, 0)) {
|
|
|
|
gcs().send_text(MAV_SEVERITY_CRITICAL, "CompassCalibrator: Cannot start compass thread.");
|
|
|
|
return false;
|
2018-07-17 23:07:52 -03:00
|
|
|
}
|
2020-04-14 11:09:12 -03:00
|
|
|
_cal_thread_started = true;
|
2018-07-16 05:21:53 -03:00
|
|
|
}
|
2015-09-06 19:07:07 -03:00
|
|
|
|
|
|
|
// disable compass learning both for calibration and after completion
|
|
|
|
_learn.set_and_save(0);
|
|
|
|
|
2015-09-02 18:58:05 -03:00
|
|
|
return true;
|
2015-03-18 20:42:30 -03:00
|
|
|
}
|
|
|
|
|
2020-04-14 11:09:12 -03:00
|
|
|
void Compass::_update_calibration_trampoline() {
|
|
|
|
while(true) {
|
|
|
|
for (Priority i(0); i<COMPASS_MAX_INSTANCES; i++) {
|
|
|
|
if (_calibrator[i] == nullptr) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
_calibrator[i]->update();
|
|
|
|
}
|
|
|
|
hal.scheduler->delay(1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-14 22:14:24 -04:00
|
|
|
bool Compass::_start_calibration_mask(uint8_t mask, bool retry, bool autosave, float delay, bool autoreboot)
|
2015-03-18 20:42:30 -03:00
|
|
|
{
|
2016-10-25 02:16:11 -03:00
|
|
|
_cal_autosave = autosave;
|
|
|
|
_compass_cal_autoreboot = autoreboot;
|
|
|
|
|
2021-08-19 02:18:41 -03:00
|
|
|
bool at_least_one_started = false;
|
2015-03-18 21:49:39 -03:00
|
|
|
for (uint8_t i=0; i<COMPASS_MAX_INSTANCES; i++) {
|
|
|
|
if ((1<<i) & mask) {
|
2016-10-25 02:16:11 -03:00
|
|
|
if (!_start_calibration(i,retry,delay)) {
|
|
|
|
_cancel_calibration_mask(mask);
|
2015-03-18 20:42:30 -03:00
|
|
|
return false;
|
|
|
|
}
|
2021-08-19 02:18:41 -03:00
|
|
|
at_least_one_started = true;
|
2015-03-18 20:42:30 -03:00
|
|
|
}
|
|
|
|
}
|
2021-08-19 02:18:41 -03:00
|
|
|
return at_least_one_started;
|
2015-03-18 20:42:30 -03:00
|
|
|
}
|
|
|
|
|
2021-08-19 02:18:41 -03:00
|
|
|
bool Compass::start_calibration_all(bool retry, bool autosave, float delay, bool autoreboot)
|
2015-03-18 20:42:30 -03:00
|
|
|
{
|
2016-10-25 02:16:11 -03:00
|
|
|
_cal_autosave = autosave;
|
|
|
|
_compass_cal_autoreboot = autoreboot;
|
|
|
|
|
2021-08-19 02:18:41 -03:00
|
|
|
bool at_least_one_started = false;
|
2015-03-18 21:49:39 -03:00
|
|
|
for (uint8_t i=0; i<COMPASS_MAX_INSTANCES; i++) {
|
2016-10-25 02:16:11 -03:00
|
|
|
// ignore any compasses that fail to start calibrating
|
|
|
|
// start all should only calibrate compasses that are being used
|
2021-08-19 02:18:41 -03:00
|
|
|
if (_start_calibration(i,retry,delay)) {
|
|
|
|
at_least_one_started = true;
|
|
|
|
}
|
2015-03-18 20:42:30 -03:00
|
|
|
}
|
2021-08-19 02:18:41 -03:00
|
|
|
return at_least_one_started;
|
2015-03-18 20:42:30 -03:00
|
|
|
}
|
|
|
|
|
2019-11-14 22:14:24 -04:00
|
|
|
void Compass::_cancel_calibration(uint8_t i)
|
2015-03-18 20:42:30 -03:00
|
|
|
{
|
|
|
|
AP_Notify::events.initiated_compass_cal = 0;
|
2019-11-20 03:18:10 -04:00
|
|
|
Priority prio = Priority(i);
|
2020-04-14 11:09:12 -03:00
|
|
|
if (_calibrator[prio] == nullptr) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (_calibrator[prio]->running() || _calibrator[prio]->get_state().status == CompassCalibrator::Status::WAITING_TO_START) {
|
2015-09-02 15:20:15 -03:00
|
|
|
AP_Notify::events.compass_cal_canceled = 1;
|
|
|
|
}
|
2019-11-20 03:18:10 -04:00
|
|
|
_cal_saved[prio] = false;
|
2020-04-14 11:09:12 -03:00
|
|
|
_calibrator[prio]->stop();
|
2015-03-18 20:42:30 -03:00
|
|
|
}
|
|
|
|
|
2019-11-14 22:14:24 -04:00
|
|
|
void Compass::_cancel_calibration_mask(uint8_t mask)
|
2015-03-18 20:42:30 -03:00
|
|
|
{
|
2019-11-14 22:14:24 -04:00
|
|
|
for (uint8_t i=0; i<COMPASS_MAX_INSTANCES; i++) {
|
|
|
|
if ((1<<i) & mask) {
|
2016-10-25 02:16:11 -03:00
|
|
|
_cancel_calibration(i);
|
2015-03-18 20:42:30 -03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-14 22:14:24 -04:00
|
|
|
void Compass::cancel_calibration_all()
|
2015-03-18 20:42:30 -03:00
|
|
|
{
|
2016-10-25 02:16:11 -03:00
|
|
|
_cancel_calibration_mask(0xFF);
|
2015-03-18 20:42:30 -03:00
|
|
|
}
|
|
|
|
|
2019-11-14 22:14:24 -04:00
|
|
|
bool Compass::_accept_calibration(uint8_t i)
|
2015-03-18 20:42:30 -03:00
|
|
|
{
|
2019-11-20 03:18:10 -04:00
|
|
|
Priority prio = Priority(i);
|
|
|
|
|
2020-04-14 11:09:12 -03:00
|
|
|
CompassCalibrator* cal = _calibrator[prio];
|
|
|
|
if (cal == nullptr) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
const CompassCalibrator::Report cal_report = cal->get_report();
|
2015-03-18 20:42:30 -03:00
|
|
|
|
2020-04-14 11:09:12 -03:00
|
|
|
if (_cal_saved[prio] || cal_report.status == CompassCalibrator::Status::NOT_STARTED) {
|
2016-10-25 02:16:11 -03:00
|
|
|
return true;
|
2020-04-14 11:09:12 -03:00
|
|
|
} else if (cal_report.status == CompassCalibrator::Status::SUCCESS) {
|
2019-11-20 03:18:10 -04:00
|
|
|
_cal_saved[prio] = true;
|
2016-10-25 02:16:11 -03:00
|
|
|
|
2020-04-14 11:09:12 -03:00
|
|
|
Vector3f ofs(cal_report.ofs), diag(cal_report.diag), offdiag(cal_report.offdiag);
|
|
|
|
float scale_factor = cal_report.scale_factor;
|
2015-03-18 20:42:30 -03:00
|
|
|
|
|
|
|
set_and_save_offsets(i, ofs);
|
|
|
|
set_and_save_diagonals(i,diag);
|
|
|
|
set_and_save_offdiagonals(i,offdiag);
|
2019-11-26 18:11:54 -04:00
|
|
|
set_and_save_scale_factor(i,scale_factor);
|
2015-03-18 20:42:30 -03:00
|
|
|
|
2021-12-04 00:22:56 -04:00
|
|
|
if (cal_report.check_orientation && _get_state(prio).external && _rotate_auto >= 2) {
|
2020-04-14 11:09:12 -03:00
|
|
|
set_and_save_orientation(i, cal_report.orientation);
|
2018-07-16 05:21:53 -03:00
|
|
|
}
|
|
|
|
|
2015-03-18 21:49:39 -03:00
|
|
|
if (!is_calibrating()) {
|
2015-03-18 20:42:30 -03:00
|
|
|
AP_Notify::events.compass_cal_saved = 1;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
} else {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-14 22:14:24 -04:00
|
|
|
bool Compass::_accept_calibration_mask(uint8_t mask)
|
2015-03-18 20:42:30 -03:00
|
|
|
{
|
|
|
|
bool success = true;
|
2019-11-20 03:18:10 -04:00
|
|
|
for (Priority i(0); i<COMPASS_MAX_INSTANCES; i++) {
|
2020-04-14 11:09:12 -03:00
|
|
|
if (_calibrator[i] == nullptr) {
|
|
|
|
continue;
|
|
|
|
}
|
2019-11-20 03:18:10 -04:00
|
|
|
if ((1<<uint8_t(i)) & mask) {
|
|
|
|
if (!_accept_calibration(uint8_t(i))) {
|
2015-03-18 20:42:30 -03:00
|
|
|
success = false;
|
|
|
|
}
|
2020-04-14 11:09:12 -03:00
|
|
|
_calibrator[i]->stop();
|
2015-03-18 20:42:30 -03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return success;
|
|
|
|
}
|
|
|
|
|
2019-07-31 22:05:22 -03:00
|
|
|
bool Compass::send_mag_cal_progress(const GCS_MAVLINK& link)
|
2015-03-18 20:42:30 -03:00
|
|
|
{
|
2020-06-11 21:35:32 -03:00
|
|
|
const mavlink_channel_t chan = link.get_chan();
|
|
|
|
|
|
|
|
for (uint8_t i = 0; i < COMPASS_MAX_INSTANCES; i++) {
|
|
|
|
const Priority compass_id = (next_cal_progress_idx[chan] + 1) % COMPASS_MAX_INSTANCES;
|
2020-04-14 11:09:12 -03:00
|
|
|
|
2016-04-22 19:21:18 -03:00
|
|
|
auto& calibrator = _calibrator[compass_id];
|
2020-04-14 11:09:12 -03:00
|
|
|
if (calibrator == nullptr) {
|
2020-06-11 21:35:32 -03:00
|
|
|
next_cal_progress_idx[chan] = compass_id;
|
2020-04-14 11:09:12 -03:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
const CompassCalibrator::State cal_state = calibrator->get_state();
|
2015-03-18 20:42:30 -03:00
|
|
|
|
2020-04-14 11:09:12 -03:00
|
|
|
if (cal_state.status == CompassCalibrator::Status::WAITING_TO_START ||
|
|
|
|
cal_state.status == CompassCalibrator::Status::RUNNING_STEP_ONE ||
|
|
|
|
cal_state.status == CompassCalibrator::Status::RUNNING_STEP_TWO) {
|
2020-06-11 21:35:32 -03:00
|
|
|
// ensure we don't try to send with no space available
|
|
|
|
if (!HAVE_PAYLOAD_SPACE(chan, MAG_CAL_PROGRESS)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
next_cal_progress_idx[chan] = compass_id;
|
|
|
|
|
2015-03-18 20:42:30 -03:00
|
|
|
mavlink_msg_mag_cal_progress_send(
|
2019-07-31 22:05:22 -03:00
|
|
|
link.get_chan(),
|
2020-06-11 21:35:32 -03:00
|
|
|
uint8_t(compass_id),
|
|
|
|
_get_cal_mask(),
|
2020-04-14 11:09:12 -03:00
|
|
|
(uint8_t)cal_state.status, cal_state.attempt, cal_state.completion_pct, cal_state.completion_mask,
|
|
|
|
0.0f, 0.0f, 0.0f
|
2015-03-18 20:42:30 -03:00
|
|
|
);
|
2020-06-11 21:35:32 -03:00
|
|
|
} else {
|
|
|
|
next_cal_progress_idx[chan] = compass_id;
|
2015-03-18 20:42:30 -03:00
|
|
|
}
|
|
|
|
}
|
2019-07-31 22:05:22 -03:00
|
|
|
|
|
|
|
return true;
|
2015-03-18 20:42:30 -03:00
|
|
|
}
|
|
|
|
|
2019-07-31 22:05:22 -03:00
|
|
|
bool Compass::send_mag_cal_report(const GCS_MAVLINK& link)
|
2015-03-18 20:42:30 -03:00
|
|
|
{
|
2020-06-11 21:35:32 -03:00
|
|
|
const mavlink_channel_t chan = link.get_chan();
|
|
|
|
|
|
|
|
for (uint8_t i = 0; i < COMPASS_MAX_INSTANCES; i++) {
|
|
|
|
const Priority compass_id = (next_cal_report_idx[chan] + 1) % COMPASS_MAX_INSTANCES;
|
|
|
|
|
2020-04-14 11:09:12 -03:00
|
|
|
if (_calibrator[compass_id] == nullptr) {
|
2020-06-11 21:35:32 -03:00
|
|
|
next_cal_report_idx[chan] = compass_id;
|
2020-04-14 11:09:12 -03:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
const CompassCalibrator::Report cal_report = _calibrator[compass_id]->get_report();
|
|
|
|
if (cal_report.status == CompassCalibrator::Status::SUCCESS ||
|
|
|
|
cal_report.status == CompassCalibrator::Status::FAILED ||
|
|
|
|
cal_report.status == CompassCalibrator::Status::BAD_ORIENTATION) {
|
2020-06-11 21:35:32 -03:00
|
|
|
|
|
|
|
// ensure we don't try to send with no space available
|
|
|
|
if (!HAVE_PAYLOAD_SPACE(chan, MAG_CAL_REPORT)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
next_cal_report_idx[chan] = compass_id;
|
|
|
|
|
2015-03-18 20:42:30 -03:00
|
|
|
mavlink_msg_mag_cal_report_send(
|
2019-07-31 22:05:22 -03:00
|
|
|
link.get_chan(),
|
2020-06-11 21:35:32 -03:00
|
|
|
uint8_t(compass_id),
|
|
|
|
_get_cal_mask(),
|
|
|
|
(uint8_t)cal_report.status,
|
|
|
|
_cal_saved[compass_id],
|
2020-04-14 11:09:12 -03:00
|
|
|
cal_report.fitness,
|
|
|
|
cal_report.ofs.x, cal_report.ofs.y, cal_report.ofs.z,
|
|
|
|
cal_report.diag.x, cal_report.diag.y, cal_report.diag.z,
|
|
|
|
cal_report.offdiag.x, cal_report.offdiag.y, cal_report.offdiag.z,
|
|
|
|
cal_report.orientation_confidence,
|
|
|
|
cal_report.original_orientation,
|
|
|
|
cal_report.orientation,
|
|
|
|
cal_report.scale_factor
|
2015-03-18 20:42:30 -03:00
|
|
|
);
|
2020-06-11 21:35:32 -03:00
|
|
|
} else {
|
|
|
|
next_cal_report_idx[chan] = compass_id;
|
2015-03-18 20:42:30 -03:00
|
|
|
}
|
|
|
|
}
|
2019-07-31 22:05:22 -03:00
|
|
|
return true;
|
2015-03-18 20:42:30 -03:00
|
|
|
}
|
|
|
|
|
2021-07-30 01:41:20 -03:00
|
|
|
bool Compass::is_calibrating() const
|
2015-03-18 20:42:30 -03:00
|
|
|
{
|
2019-11-20 03:18:10 -04:00
|
|
|
for (Priority i(0); i<COMPASS_MAX_INSTANCES; i++) {
|
2020-04-14 11:09:12 -03:00
|
|
|
if (_calibrator[i] == nullptr) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
switch(_calibrator[i]->get_state().status) {
|
2019-11-21 23:57:17 -04:00
|
|
|
case CompassCalibrator::Status::NOT_STARTED:
|
|
|
|
case CompassCalibrator::Status::SUCCESS:
|
|
|
|
case CompassCalibrator::Status::FAILED:
|
|
|
|
case CompassCalibrator::Status::BAD_ORIENTATION:
|
2016-10-25 02:16:11 -03:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
2015-03-18 20:42:30 -03:00
|
|
|
}
|
|
|
|
|
2020-04-14 11:09:12 -03:00
|
|
|
uint8_t Compass::_get_cal_mask()
|
2015-03-18 20:42:30 -03:00
|
|
|
{
|
|
|
|
uint8_t cal_mask = 0;
|
2019-11-20 03:18:10 -04:00
|
|
|
for (Priority i(0); i<COMPASS_MAX_INSTANCES; i++) {
|
2020-04-14 11:09:12 -03:00
|
|
|
if (_calibrator[i] == nullptr) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (_calibrator[i]->get_state().status != CompassCalibrator::Status::NOT_STARTED) {
|
2019-11-20 03:18:10 -04:00
|
|
|
cal_mask |= 1 << uint8_t(i);
|
2015-03-18 20:42:30 -03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return cal_mask;
|
|
|
|
}
|
2015-08-23 07:23:38 -03:00
|
|
|
|
|
|
|
/*
|
|
|
|
handle an incoming MAG_CAL command
|
|
|
|
*/
|
2017-07-16 21:45:40 -03:00
|
|
|
MAV_RESULT Compass::handle_mag_cal_command(const mavlink_command_long_t &packet)
|
2015-08-23 07:23:38 -03:00
|
|
|
{
|
2017-07-16 21:45:40 -03:00
|
|
|
MAV_RESULT result = MAV_RESULT_FAILED;
|
2015-08-23 07:23:38 -03:00
|
|
|
|
|
|
|
switch (packet.command) {
|
|
|
|
case MAV_CMD_DO_START_MAG_CAL: {
|
|
|
|
result = MAV_RESULT_ACCEPTED;
|
2015-09-03 03:50:56 -03:00
|
|
|
if (hal.util->get_soft_armed()) {
|
2020-02-25 10:19:04 -04:00
|
|
|
gcs().send_text(MAV_SEVERITY_NOTICE, "Disarm to allow compass calibration");
|
2015-09-03 03:50:56 -03:00
|
|
|
result = MAV_RESULT_FAILED;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (packet.param1 < 0 || packet.param1 > 255) {
|
2015-08-23 07:23:38 -03:00
|
|
|
result = MAV_RESULT_FAILED;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint8_t mag_mask = packet.param1;
|
2015-11-24 14:57:42 -04:00
|
|
|
bool retry = !is_zero(packet.param2);
|
|
|
|
bool autosave = !is_zero(packet.param3);
|
2015-08-23 07:23:38 -03:00
|
|
|
float delay = packet.param4;
|
2015-11-24 14:57:42 -04:00
|
|
|
bool autoreboot = !is_zero(packet.param5);
|
2015-08-23 07:23:38 -03:00
|
|
|
|
|
|
|
if (mag_mask == 0) { // 0 means all
|
2020-07-20 18:25:08 -03:00
|
|
|
_reset_compass_id();
|
2021-08-19 02:18:41 -03:00
|
|
|
if (!start_calibration_all(retry, autosave, delay, autoreboot)) {
|
|
|
|
result = MAV_RESULT_FAILED;
|
|
|
|
}
|
2015-08-23 07:23:38 -03:00
|
|
|
} else {
|
2016-10-25 02:16:11 -03:00
|
|
|
if (!_start_calibration_mask(mag_mask, retry, autosave, delay, autoreboot)) {
|
2015-08-23 07:23:38 -03:00
|
|
|
result = MAV_RESULT_FAILED;
|
|
|
|
}
|
|
|
|
}
|
2019-11-14 22:14:24 -04:00
|
|
|
|
2015-08-23 07:23:38 -03:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
case MAV_CMD_DO_ACCEPT_MAG_CAL: {
|
|
|
|
result = MAV_RESULT_ACCEPTED;
|
2019-11-14 22:14:24 -04:00
|
|
|
if (packet.param1 < 0 || packet.param1 > 255) {
|
2015-08-23 07:23:38 -03:00
|
|
|
result = MAV_RESULT_FAILED;
|
|
|
|
break;
|
|
|
|
}
|
2019-11-14 22:14:24 -04:00
|
|
|
|
2015-08-23 07:23:38 -03:00
|
|
|
uint8_t mag_mask = packet.param1;
|
2019-11-14 22:14:24 -04:00
|
|
|
|
2015-08-23 07:23:38 -03:00
|
|
|
if (mag_mask == 0) { // 0 means all
|
2016-10-25 02:16:11 -03:00
|
|
|
mag_mask = 0xFF;
|
2015-08-23 07:23:38 -03:00
|
|
|
}
|
2019-11-14 22:14:24 -04:00
|
|
|
|
|
|
|
if (!_accept_calibration_mask(mag_mask)) {
|
2015-08-23 07:23:38 -03:00
|
|
|
result = MAV_RESULT_FAILED;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
2019-11-14 22:14:24 -04:00
|
|
|
|
2015-08-23 07:23:38 -03:00
|
|
|
case MAV_CMD_DO_CANCEL_MAG_CAL: {
|
|
|
|
result = MAV_RESULT_ACCEPTED;
|
2019-11-14 22:14:24 -04:00
|
|
|
if (packet.param1 < 0 || packet.param1 > 255) {
|
2015-08-23 07:23:38 -03:00
|
|
|
result = MAV_RESULT_FAILED;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint8_t mag_mask = packet.param1;
|
|
|
|
|
|
|
|
if (mag_mask == 0) { // 0 means all
|
|
|
|
cancel_calibration_all();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2016-10-25 02:16:11 -03:00
|
|
|
_cancel_calibration_mask(mag_mask);
|
2015-08-23 07:23:38 -03:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
2019-05-26 22:49:12 -03:00
|
|
|
|
2019-11-20 23:53:43 -04:00
|
|
|
/*
|
|
|
|
get mag field with the effects of offsets, diagonals and
|
|
|
|
off-diagonals removed
|
|
|
|
*/
|
2021-02-01 12:26:27 -04:00
|
|
|
bool Compass::get_uncorrected_field(uint8_t instance, Vector3f &field) const
|
2019-11-20 23:53:43 -04:00
|
|
|
{
|
|
|
|
// form eliptical correction matrix and invert it. This is
|
|
|
|
// needed to remove the effects of the eliptical correction
|
|
|
|
// when calculating new offsets
|
|
|
|
const Vector3f &diagonals = get_diagonals(instance);
|
|
|
|
const Vector3f &offdiagonals = get_offdiagonals(instance);
|
|
|
|
Matrix3f mat {
|
|
|
|
diagonals.x, offdiagonals.x, offdiagonals.y,
|
|
|
|
offdiagonals.x, diagonals.y, offdiagonals.z,
|
|
|
|
offdiagonals.y, offdiagonals.z, diagonals.z
|
|
|
|
};
|
|
|
|
if (!mat.invert()) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// get corrected field
|
|
|
|
field = get_field(instance);
|
|
|
|
|
|
|
|
// remove impact of diagonals and off-diagonals
|
|
|
|
field = mat * field;
|
|
|
|
|
|
|
|
// remove impact of offsets
|
|
|
|
field -= get_offsets(instance);
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
fast compass calibration given vehicle position and yaw. This
|
|
|
|
results in zero diagonal and off-diagonal elements, so is only
|
|
|
|
suitable for vehicles where the field is close to spherical. It is
|
|
|
|
useful for large vehicles where moving the vehicle to calibrate it
|
|
|
|
is difficult.
|
|
|
|
|
|
|
|
The offsets of the selected compasses are set to values to bring
|
|
|
|
them into consistency with the WMM tables at the given latitude and
|
|
|
|
longitude. If compass_mask is zero then all enabled compasses are
|
|
|
|
calibrated.
|
|
|
|
|
|
|
|
This assumes that the compass is correctly scaled in milliGauss
|
|
|
|
*/
|
|
|
|
MAV_RESULT Compass::mag_cal_fixed_yaw(float yaw_deg, uint8_t compass_mask,
|
2021-10-14 05:48:47 -03:00
|
|
|
float lat_deg, float lon_deg, bool force_use)
|
2019-11-20 23:53:43 -04:00
|
|
|
{
|
2020-07-20 18:25:08 -03:00
|
|
|
_reset_compass_id();
|
2019-11-20 23:53:43 -04:00
|
|
|
if (is_zero(lat_deg) && is_zero(lon_deg)) {
|
|
|
|
Location loc;
|
|
|
|
// get AHRS position. If unavailable then try GPS location
|
2022-01-20 19:42:40 -04:00
|
|
|
if (!AP::ahrs().get_location(loc)) {
|
2019-11-20 23:53:43 -04:00
|
|
|
if (AP::gps().status() < AP_GPS::GPS_OK_FIX_3D) {
|
|
|
|
gcs().send_text(MAV_SEVERITY_ERROR, "Mag: no position available");
|
|
|
|
return MAV_RESULT_FAILED;
|
|
|
|
}
|
|
|
|
loc = AP::gps().location();
|
|
|
|
}
|
|
|
|
lat_deg = loc.lat * 1.0e-7;
|
|
|
|
lon_deg = loc.lng * 1.0e-7;
|
|
|
|
}
|
|
|
|
|
|
|
|
// get the magnetic field intensity and orientation
|
|
|
|
float intensity;
|
|
|
|
float declination;
|
|
|
|
float inclination;
|
|
|
|
if (!AP_Declination::get_mag_field_ef(lat_deg, lon_deg, intensity, declination, inclination)) {
|
|
|
|
gcs().send_text(MAV_SEVERITY_ERROR, "Mag: WMM table error");
|
|
|
|
return MAV_RESULT_FAILED;
|
|
|
|
}
|
|
|
|
|
|
|
|
// create a field vector and rotate to the required orientation
|
|
|
|
Vector3f field(1e3f * intensity, 0.0f, 0.0f);
|
|
|
|
Matrix3f R;
|
|
|
|
R.from_euler(0.0f, -ToRad(inclination), ToRad(declination));
|
|
|
|
field = R * field;
|
|
|
|
|
|
|
|
Matrix3f dcm;
|
|
|
|
dcm.from_euler(AP::ahrs().roll, AP::ahrs().pitch, radians(yaw_deg));
|
|
|
|
|
|
|
|
// Rotate into body frame using provided yaw
|
|
|
|
field = dcm.transposed() * field;
|
|
|
|
|
2022-01-16 16:55:36 -04:00
|
|
|
for (uint8_t i=0; i<get_count(); i++) {
|
2019-11-20 23:53:43 -04:00
|
|
|
if (compass_mask != 0 && ((1U<<i) & compass_mask) == 0) {
|
|
|
|
// skip this compass
|
|
|
|
continue;
|
|
|
|
}
|
2022-01-16 16:55:36 -04:00
|
|
|
if (_use_for_yaw[Priority(i)] == 0 || (!force_use && !use_for_yaw(i))) {
|
2019-11-20 23:53:43 -04:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (!healthy(i)) {
|
|
|
|
gcs().send_text(MAV_SEVERITY_ERROR, "Mag[%u]: unhealthy\n", i);
|
|
|
|
return MAV_RESULT_FAILED;
|
|
|
|
}
|
|
|
|
|
|
|
|
Vector3f measurement;
|
|
|
|
if (!get_uncorrected_field(i, measurement)) {
|
|
|
|
gcs().send_text(MAV_SEVERITY_ERROR, "Mag[%u]: bad uncorrected field", i);
|
|
|
|
return MAV_RESULT_FAILED;
|
|
|
|
}
|
|
|
|
|
|
|
|
Vector3f offsets = field - measurement;
|
|
|
|
set_and_save_offsets(i, offsets);
|
|
|
|
Vector3f one{1,1,1};
|
|
|
|
set_and_save_diagonals(i, one);
|
|
|
|
Vector3f zero{0,0,0};
|
|
|
|
set_and_save_offdiagonals(i, zero);
|
|
|
|
}
|
|
|
|
|
|
|
|
return MAV_RESULT_ACCEPTED;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-05-26 22:49:12 -03:00
|
|
|
#endif // COMPASS_CAL_ENABLED
|