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

Changes to make AccelStepper more subclassable. These changes are lar… #13

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 27 additions & 2 deletions src/AccelStepper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -106,7 +107,7 @@ void AccelStepper::computeNewSpeed()
_stepInterval = 0;
_speed = 0.0;
_n = 0;
return;
return _stepInterval;
}

if (distanceTo > 0)
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -295,6 +297,11 @@ void AccelStepper::setAcceleration(float acceleration)
}
}

float AccelStepper::acceleration()
{
return _acceleration;
}

void AccelStepper::setSpeed(float speed)
{
if (speed == _speed)
Expand Down Expand Up @@ -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]
Expand Down
28 changes: 22 additions & 6 deletions src/AccelStepper.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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]
Expand All @@ -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)
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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;

Expand Down