From 5c67e8a0abaa834728e1af249785e328e969ca35 Mon Sep 17 00:00:00 2001 From: bugobliterator Date: Wed, 27 Nov 2024 15:40:39 +1100 Subject: [PATCH] AP_Periph: add support for ak09916 backup compass --- AP_Periph/AP_Periph.h | 4 ++-- AP_Periph/sensor_compass.cpp | 34 ++++++++++++++++++++-------------- Here4/hwdef.dat | 11 ++++++++++- 3 files changed, 32 insertions(+), 17 deletions(-) diff --git a/AP_Periph/AP_Periph.h b/AP_Periph/AP_Periph.h index 9bad860..f36c6fb 100644 --- a/AP_Periph/AP_Periph.h +++ b/AP_Periph/AP_Periph.h @@ -168,7 +168,7 @@ class AP_Periph_FW { static const AP_Param::Info var_info[]; - uint32_t last_mag_update_ms; + uint32_t last_mag_update_ms[COMPASS_MAX_INSTANCES]; uint32_t last_gps_update_ms; uint32_t last_baro_update_ms; uint64_t last_time_sync_usec; @@ -269,7 +269,7 @@ class AP_Periph_DroneCAN { Canard::Publisher can_stats_pub{canard_iface}; void can_mag_update(); - Canard::Publisher mag_pub{canard_iface}; + Canard::Publisher mag_pub{canard_iface}; void can_gps_init(); void can_gps_update(); diff --git a/AP_Periph/sensor_compass.cpp b/AP_Periph/sensor_compass.cpp index be8467a..b481495 100644 --- a/AP_Periph/sensor_compass.cpp +++ b/AP_Periph/sensor_compass.cpp @@ -12,22 +12,28 @@ void AP_Periph_DroneCAN::can_mag_update(void) } compass.read(); - if (periph.last_mag_update_ms == compass.last_update_ms()) { - return; - } - if (!compass.healthy()) { - return; + // estimate scale and offset from first compass + if (compass.healthy(0) && compass.healthy(1) && compass.get_offsets(1).is_zero()) { + const Vector3f &reference = compass.get_field(0); + const Vector3f &field = compass.get_field(1); + compass.set_and_save_offsets(1, reference - field); } - periph.last_mag_update_ms = compass.last_update_ms(); - const Vector3f &field = compass.get_field(); - uavcan_equipment_ahrs_MagneticFieldStrength pkt {}; - - // the canard dsdl compiler doesn't understand float16 - for (uint8_t i=0; i<3; i++) { - pkt.magnetic_field_ga[i] = field[i] * 0.001; + uavcan_equipment_ahrs_MagneticFieldStrength2 pkt {}; + for (uint8_t i=0; i