Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reliable encoder wheel action when reversing direction #18694

Merged
merged 13 commits into from
Jul 19, 2020
25 changes: 13 additions & 12 deletions Marlin/src/lcd/ultralcd.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -902,20 +902,21 @@ void MarlinUI::update() {
// When reversing the encoder direction, a movement step can be missed because
// encoderDiff has a non-zero residual value, making the controller unresponsive.
// The fix clears the residual value when the encoder is reversed.
// Also check if passed half the threshold. This will compensate for missed single steps.
// Also check if past half the threshold to compensate for missed single steps.
static int8_t lastEncoderDiff;
int8_t prevDiff = lastEncoderDiff;
lastEncoderDiff = encoderDiff; // Store before updating encoderDiff to save actual steps

// When not past threshold, and reversing... or past half the threshold
if ((encoderDiff != 0 && abs_diff < (ENCODER_PULSES_PER_STEP)) && // Not past threshold
(
abs_diff > (ENCODER_PULSES_PER_STEP) / 2 || // Passed half the threshold
(ABS(encoderDiff - lastEncoderDiff) >= (ENCODER_PULSES_PER_STEP) && ABS(lastEncoderDiff) < (ENCODER_PULSES_PER_STEP)) // Or direction change through zero
)) {
lastEncoderDiff = encoderDiff; // Store before updating encoderDiff, we want actual steps
encoderDiff = (encoderDiff < 0 ? -1 : 1) * (ENCODER_PULSES_PER_STEP); // Treat as full step
abs_diff = ENCODER_PULSES_PER_STEP;
}
else {
lastEncoderDiff = encoderDiff;
if (WITHIN(abs_diff, 1, (ENCODER_PULSES_PER_STEP) - 1) // Not past threshold
&& (abs_diff > (ENCODER_PULSES_PER_STEP) / 2 // Passed half the threshold?
|| (ABS(encoderDiff - prevDiff) >= (ENCODER_PULSES_PER_STEP) // Or a large change...
thinkyhead marked this conversation as resolved.
Show resolved Hide resolved
&& ABS(prevDiff) < (ENCODER_PULSES_PER_STEP) // ...starting from a partial step?
)
)
) {
thinkyhead marked this conversation as resolved.
Show resolved Hide resolved
encoderDiff = (encoderDiff < 0 ? -1 : 1) * (ENCODER_PULSES_PER_STEP); // Treat as full step
abs_diff = ENCODER_PULSES_PER_STEP;
}
#endif

Expand Down