diff --git a/libraries/AP_Compass/AP_Compass_AK09916.cpp b/libraries/AP_Compass/AP_Compass_AK09916.cpp index e846fceb7c..7cddd0a3d1 100644 --- a/libraries/AP_Compass/AP_Compass_AK09916.cpp +++ b/libraries/AP_Compass/AP_Compass_AK09916.cpp @@ -24,6 +24,7 @@ #include #include #include +#include extern const AP_HAL::HAL &hal; @@ -288,6 +289,19 @@ void AP_Compass_AK09916::_update() _make_adc_sensitivity_adjustment(raw_field); raw_field *= AK09916_MILLIGAUSS_SCALE; +#ifdef HAL_AK09916_HEATER_OFFSET + /* + the internal AK09916 can be impacted by the magnetic field from + a heater. We use the heater duty cycle to correct for the error + */ + if (AP_HAL::Device::devid_get_bus_type(_bus->get_bus_id()) == AP_HAL::Device::BUS_TYPE_SPI) { + auto *bc = AP::boardConfig(); + if (bc) { + raw_field += HAL_AK09916_HEATER_OFFSET * bc->get_heater_duty_cycle() * 0.01; + } + } +#endif + accumulate_sample(raw_field, _compass_instance, 10); }