From 5ddbb09f1e51f43d3a3dae61a891d2e52fc6b611 Mon Sep 17 00:00:00 2001 From: stijncarelsbergh Date: Thu, 8 Oct 2026 00:04:42 +0200 Subject: [PATCH] fix(hall sensor): read direction inside the critical section in getVelocity() direction is written by the interrupt handler (updateState()) and was read after interrupts() had been re-enabled, so a sector change in between mixes the old pulse timing with the new direction and produces one velocity sample with the wrong sign and magnitude. Sampled together with the pulse variables instead. --- src/sensors/HallSensor.cpp | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/sensors/HallSensor.cpp b/src/sensors/HallSensor.cpp index 64c5e48c1..77033d104 100644 --- a/src/sensors/HallSensor.cpp +++ b/src/sensors/HallSensor.cpp @@ -130,11 +130,12 @@ float HallSensor::getVelocity(){ noInterrupts(); long last_pulse_timestamp = pulse_timestamp; long last_pulse_diff = pulse_diff; + Direction last_direction = direction; interrupts(); if (last_pulse_diff == 0 || ((long)(_micros() - last_pulse_timestamp) > last_pulse_diff*2) ) { // last velocity isn't accurate if too old return 0; } else { - return direction * (_2PI / (float)cpr) / (last_pulse_diff / 1000000.0f); + return last_direction * (_2PI / (float)cpr) / (last_pulse_diff / 1000000.0f); } }