From 80836f85321ffd0c54a0d2899fc56fba9c5923e1 Mon Sep 17 00:00:00 2001 From: stijncarelsbergh Date: Thu, 8 Oct 2026 00:04:37 +0200 Subject: [PATCH] fix(magnetic sensor pwm): scale the pulseIn() timeout with the PWM frequency The frequency-aware constructor documents the AS5600 PWM modes (115/230/460/920 Hz) and computes the raw counts from them, but left the read timeout at the default 1200 us. One period at 115 Hz is ~8.7 ms and the high pulse can be ~8.4 ms, so pulseIn() times out, returns 0 and the reported angle sticks at min_raw_count: a silently dead sensor for an officially supported configuration. Only 920 Hz (max pulse ~1.05 ms) fitted into the old timeout. The timeout is now 1.2 periods, i.e. ~1.2 ms at 920 Hz (compatible with the behaviour before) and ~10 ms at 115 Hz. --- src/sensors/MagneticSensorPWM.cpp | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/sensors/MagneticSensorPWM.cpp b/src/sensors/MagneticSensorPWM.cpp index 04b7cc75d..e53c46c10 100644 --- a/src/sensors/MagneticSensorPWM.cpp +++ b/src/sensors/MagneticSensorPWM.cpp @@ -45,6 +45,11 @@ MagneticSensorPWM::MagneticSensorPWM(uint8_t _pinPWM, int freqHz, int _total_pwm min_elapsed_time = 1.0f/freqHz; // set the minimum time between two readings + // scale the blocking read timeout with the PWM frequency: the high pulse can + // be almost a full period long (e.g. ~8.4ms at 115Hz) while the 1200us + // default only covers the fastest supported frequency (920Hz) + timeout_us = (unsigned int)(1.2f * 1000000.0f / freqHz); + // define as not set last_call_us = _micros(); }