From d94341e1c0472e7a56233a034200b129c5a9daa4 Mon Sep 17 00:00:00 2001 From: smathis Date: Tue, 9 Jun 2026 13:36:51 +0200 Subject: [PATCH] Guard against race condition with targetReached --- src/masterMacsAxis.cpp | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/src/masterMacsAxis.cpp b/src/masterMacsAxis.cpp index ca53089..24b3820 100644 --- a/src/masterMacsAxis.cpp +++ b/src/masterMacsAxis.cpp @@ -532,9 +532,9 @@ asynStatus masterMacsAxis::doPoll(bool *moving) { } else { - // If we wait for a handshake, but the motor was moving in its last poll - // cycle and has reached its target, it is not moving. Otherwise it is - // considered moving, even if we're still waiting for the handshake. + // If the motor hasn't moved after being enabled, we cannot trust + // targetReached. Hence, the targetReachedUninitialized flag guards + // against invalid detection of the "moving" status. if (pMasterMacsA_->targetReachedUninitialized) { *moving = false; } else { @@ -545,7 +545,13 @@ asynStatus masterMacsAxis::doPoll(bool *moving) { } } - if (targetReached()) { + // If the motor is not switched on, the targetReached bit is set to + // true. targetReachedUninitialized is set to true in the enable + // function, which is in a separate thread than doPoll. Therefore, + // doPoll can reset targetReachedUninitialized, which then can lead to + // a false "moving" status. Therefore, we only trust targetReached if + // the motor is switched on. + if (targetReached() && switchedOn()) { pMasterMacsA_->targetReachedUninitialized = false; } }