diff --git a/CHANGELOG.md b/CHANGELOG.md index bdeb275..292a744 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -9,6 +9,7 @@ TODO: pre-0.30.16: - Fix missing parenthesis in preprocessor macro (#271) - Position parameter for `forceStopAndNewPosition()` changed from `uint32_t` to `int32_t` (#268) +- Add `stepsToStop()` to predict motor stop position 0.30.15: - Fix missing initialization in `getCurrentSpeedInTicks()` (#262) diff --git a/src/FastAccelStepper.h b/src/FastAccelStepper.h index b01c713..08ec7a4 100644 --- a/src/FastAccelStepper.h +++ b/src/FastAccelStepper.h @@ -517,6 +517,21 @@ class FastAccelStepper { void stopMove(); inline bool isStopping() { return _rg.isStopping(); } + // ### stepsToStop() + // This returns the current step value of the ramp. + // This equals the number of steps for a motor to + // reach the current position and speed from standstill + // and to come to standstill with deceleration if stopped + // immediately. + // This value is valid with or without linear acceleration + // being used. + // Primary use is to forecast possible stop position. + // The stop position is: + // getCurrentPosition() + stepsToStop() + // in case of a motor running in positive direction. + uint32_t stepsToStop() { return _rg.stepsToStop(); } + + // ### forceStop() // Abruptly stop the running stepper without deceleration. // This can be called from an interrupt ! diff --git a/src/RampGenerator.h b/src/RampGenerator.h index 8c7c876..5e12cc1 100644 --- a/src/RampGenerator.h +++ b/src/RampGenerator.h @@ -82,7 +82,12 @@ class RampGenerator { return _ro.isStopInitiated() && isRampGeneratorActive(); } inline bool isRampGeneratorActive() { return rampState() != RAMP_STATE_IDLE; } - + inline uint32_t stepsToStop() { + fasDisableInterrupts(); + uint32_t v = _rw.performed_ramp_up_steps; + fasEnableInterrupts(); + return v; + } inline void stopRamp() { _rw.stopRamp(); } inline void setKeepRunning() { _ro.setKeepRunning(); } inline bool isRunningContinuously() { return _ro.isRunningContinuously(); }