From 76a8d494c37a3ac83d10cd1367676b022bde4d6c Mon Sep 17 00:00:00 2001 From: Andrew Tridgell Date: Fri, 12 Jul 2019 09:42:21 +1000 Subject: [PATCH] AP_IOMCU: dither heater pin to prevent 1Hz impact on mags with the simpler duty cycle code we see a 1Hz 25 mGauss cycle in the internal mags --- libraries/AP_IOMCU/iofirmware/iofirmware.cpp | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/libraries/AP_IOMCU/iofirmware/iofirmware.cpp b/libraries/AP_IOMCU/iofirmware/iofirmware.cpp index b28b0fc277..7c7050d9c7 100644 --- a/libraries/AP_IOMCU/iofirmware/iofirmware.cpp +++ b/libraries/AP_IOMCU/iofirmware/iofirmware.cpp @@ -295,9 +295,12 @@ void AP_IOMCU_FW::heater_update() // turn off the heater HEATER_SET(!heater_pwm_polarity); } else { - uint8_t cycle = ((now / 10UL) % 100U); - //Turn off heater when cycle is greater than specified duty cycle - HEATER_SET((cycle >= reg_setup.heater_duty_cycle) ? !heater_pwm_polarity : heater_pwm_polarity); + // we use a pseudo random sequence to dither the cycling as + // the heater has a significant effect on the internal + // magnetometers. The random generator dithers this so we don't get a 1Hz cycly in the magnetometer. + // The impact on the mags is about 25 mGauss. + bool heater_on = (get_random16() < uint32_t(reg_setup.heater_duty_cycle) * 0xFFFFU / 100U); + HEATER_SET(heater_on? heater_pwm_polarity : !heater_pwm_polarity); } }