From de2a883115fb736c7b75417f55a1c800718b087c Mon Sep 17 00:00:00 2001 From: "A. Craig West" Date: Mon, 28 Feb 2022 21:08:57 -0500 Subject: [PATCH] Changes to make AccelStepper more subclassable. These changes are largely oriented to implementing new step-scheduling algorithms. --- src/AccelStepper.cpp | 29 +++++++++++++++++++++++++++-- src/AccelStepper.h | 28 ++++++++++++++++++++++------ 2 files changed, 49 insertions(+), 8 deletions(-) diff --git a/src/AccelStepper.cpp b/src/AccelStepper.cpp index 093d56d..a093b73 100644 --- a/src/AccelStepper.cpp +++ b/src/AccelStepper.cpp @@ -94,7 +94,8 @@ void AccelStepper::setCurrentPosition(long position) _speed = 0.0; } -void AccelStepper::computeNewSpeed() +// Subclasses can override +unsigned long AccelStepper::computeNewSpeed() { long distanceTo = distanceToGo(); // +ve is clockwise from curent location @@ -106,7 +107,7 @@ void AccelStepper::computeNewSpeed() _stepInterval = 0; _speed = 0.0; _n = 0; - return; + return _stepInterval; } if (distanceTo > 0) @@ -174,6 +175,7 @@ void AccelStepper::computeNewSpeed() Serial.println(stepsToStop); Serial.println("-----"); #endif + return _stepInterval; } // Run the motor to implement speed and acceleration in order to proceed to the target position @@ -295,6 +297,11 @@ void AccelStepper::setAcceleration(float acceleration) } } +float AccelStepper::acceleration() +{ + return _acceleration; +} + void AccelStepper::setSpeed(float speed) { if (speed == _speed) @@ -350,6 +357,24 @@ void AccelStepper::step(long step) } } +long AccelStepper::stepForward() +{ + // Clockwise + _currentPos += 1; + step(_currentPos); + _lastStepTime = micros(); + return _currentPos; +} + +long AccelStepper::stepBackward() +{ + // Counter-clockwise + _currentPos -= 1; + step(_currentPos); + _lastStepTime = micros(); + return _currentPos; +} + // You might want to override this to implement eg serial output // bit 0 of the mask corresponds to _pin[0] // bit 1 of the mask corresponds to _pin[1] diff --git a/src/AccelStepper.h b/src/AccelStepper.h index 5ab5aba..f63fd79 100644 --- a/src/AccelStepper.h +++ b/src/AccelStepper.h @@ -429,6 +429,11 @@ class AccelStepper /// root to be calculated. Dont call more ofthen than needed void setAcceleration(float acceleration); + /// Returns the acceleration/deceleration rate configured for this stepper + /// that was previously set by setAcceleration(); + /// \return The currently configured acceleration/deceleration + float acceleration(); + /// Sets the desired constant speed for use with runSpeed(). /// \param[in] speed The desired constant speed in steps per /// second. Positive is clockwise. Speeds of more than 1000 steps per @@ -471,7 +476,7 @@ class AccelStepper /// position. Dont use this in event loops, since it blocks. void runToPosition(); - /// Runs at the currently selected speed until the target position is reached. + /// Runs at the currently selected speed unless the target position is reached. /// Does not implement accelerations. /// \return true if it stepped boolean runSpeedToPosition(); @@ -551,7 +556,8 @@ class AccelStepper /// \li after change to acceleration through setAcceleration() /// \li after change to target position (relative or absolute) through /// move() or moveTo() - void computeNewSpeed(); + /// \return the new step interval + virtual unsigned long computeNewSpeed(); /// Low level function to set the motor output pins /// bit 0 of the mask corresponds to _pin[0] @@ -567,6 +573,16 @@ class AccelStepper /// \param[in] step The current step phase number (0 to 7) virtual void step(long step); + /// Called to execute a clockwise(+) step. Only called when a new step is + /// required. This increments the _currentPos and calls step() + /// \return the updated current position + long stepForward(); + + /// Called to execute a counter-clockwise(-) step. Only called when a new step is + /// required. This decrements the _currentPos and calls step() + /// \return the updated current position + long stepBackward(); + /// Called to execute a step using stepper functions (pins = 0) Only called when a new step is /// required. Calls _forward() or _backward() to perform the step /// \param[in] step The current step phase number (0 to 7) @@ -618,6 +634,10 @@ class AccelStepper /// Protected because some peoples subclasses need it to be so boolean _direction; // 1 == CW + /// The current interval between steps in microseconds. + /// 0 means the motor is currently stopped with _speed == 0 + unsigned long _stepInterval; + private: /// Number of pins on the stepper motor. Permits 2 or 4. 2 pins is a /// bipolar, and 4 pins is a unipolar. @@ -650,10 +670,6 @@ class AccelStepper float _acceleration; float _sqrt_twoa; // Precomputed sqrt(2*_acceleration) - /// The current interval between steps in microseconds. - /// 0 means the motor is currently stopped with _speed == 0 - unsigned long _stepInterval; - /// The last step time in microseconds unsigned long _lastStepTime;