Support for stepper motors with acceleration etc. More...
#include <LocoStepper.h>
Public Member Functions | |
LocoStepper (uint8_t pin1, uint8_t pin2, uint8_t *inpStep2 = NULL) | |
LocoStepper (uint8_t pin1, uint8_t pin2, uint8_t pin3, uint8_t pin4, uint8_t *inpStep2 = NULL) | |
void | moveTo (long absolute) |
void | move (long relative) |
boolean | run () |
void | setSpeed (float speed) |
float | speed () |
long | distanceToGo () |
long | targetPosition () |
long | currentPosition () |
void | setCurrentPosition (long position) |
void | disableOutputs () |
void | enableOutputs () |
void | setMinPulseWidth (unsigned int minWidth) |
Protected Member Functions | |
void | step (uint8_t step) |
void | step2 (uint8_t step) |
void | step4 (uint8_t step) |
This defines a single 2 or 4 pin stepper motor, or stepper motor with fdriver chip, with optional absolute positioning commands etc. Multiple simultaneous steppers are supported, all moving at different speeds.
The fastest motor speed that can be reliably supported is 4000 steps per second (4 kHz) at a clock frequency of 16 MHz. However, any speed less than that down to very slow speeds (much less than one per second) are also supported, provided the run() function is called frequently enough to step the motor whenever required for the speed set.
Definition at line 81 of file LocoStepper.h.
LocoStepper::LocoStepper | ( | uint8_t | pin1, |
uint8_t | pin2, | ||
uint8_t * | inpStep2 = NULL |
||
) |
Constructor. You can have multiple simultaneous steppers, all moving at different speeds and accelerations, provided you call their run() functions at frequent enough intervals. Current Position is set to 0, target position is set to 0. The motor pins will be initialized to OUTPUT mode during the constructor by a call to enableOutputs().
inpSteps pointer argument gives the caller the possibility to fix himself the sequence of pins activation. If nothing is specified, a default value will be used:
/// B01, B11, B10, B00 ///
Definition at line 21 of file LocoStepper.cpp.
LocoStepper::LocoStepper | ( | uint8_t | pin1, |
uint8_t | pin2, | ||
uint8_t | pin3, | ||
uint8_t | pin4, | ||
uint8_t * | inpStep2 = NULL |
||
) |
Constructor. You can have multiple simultaneous steppers, all moving at different speeds and accelerations, provided you call their run() functions at frequent enough intervals. Current Position is set to 0, target position is set to 0. The motor pins will be initialized to OUTPUT mode during the constructor by a call to enableOutputs(). inpSteps pointer argument gives the caller the possibility to fix himself the sequence of pins activation. If nothing is specified, a default value will be used:
/// B1010, B0110, B0101, B1001 ///
Definition at line 29 of file LocoStepper.cpp.
long LocoStepper::currentPosition | ( | ) |
The currently motor position.
Definition at line 118 of file LocoStepper.cpp.
void LocoStepper::disableOutputs | ( | ) |
Disable motor pin outputs by setting them all LOW Depending on the design of your electronics this may turn off the power to the motor coils, saving power. This is useful to support Arduino low power modes: disable the outputs during sleep and then re-enable with enableOutputs() before stepping again.
Definition at line 181 of file LocoStepper.cpp.
long LocoStepper::distanceToGo | ( | ) |
The distance from the current position to the target position.
Definition at line 108 of file LocoStepper.cpp.
void LocoStepper::enableOutputs | ( | ) |
Enable motor pin outputs by setting the motor pins to OUTPUT mode. Called automatically by the constructor.
Definition at line 191 of file LocoStepper.cpp.
void LocoStepper::move | ( | long | relative | ) |
Set the target position relative to the current position
[in] | relative | The desired position relative to the current position. Negative is anticlockwise from the current position. |
Definition at line 63 of file LocoStepper.cpp.
void LocoStepper::moveTo | ( | long | absolute | ) |
Set the target position. The run() function will try to move the motor from the current position to the target position set by the most recent call to this function.
[in] | absolute | The desired absolute position. Negative is anticlockwise from the 0 position. |
Definition at line 49 of file LocoStepper.cpp.
boolean LocoStepper::run | ( | ) |
Poll the motor and step it if a step is due. You must call this as frequently as possible, but at least once per minimum step interval, preferably in your main loop.
Definition at line 72 of file LocoStepper.cpp.
void LocoStepper::setCurrentPosition | ( | long | position | ) |
Resets the current position of the motor, so that wherever the motor happens to be right now is considered to be the new position. Useful for setting a zero position on a stepper after an initial hardware positioning move.
[in] | position | The position in steps of wherever the motor happens to be right now. |
Definition at line 124 of file LocoStepper.cpp.
void LocoStepper::setMinPulseWidth | ( | unsigned int | minWidth | ) |
Sets the minimum pulse width allowed by the stepper driver.
[in] | minWidth | The minimum pulse width in microseconds. |
Definition at line 201 of file LocoStepper.cpp.
void LocoStepper::setSpeed | ( | float | speed | ) |
Sets the desired constant speed for use with run().
[in] | speed | The desired constant speed in steps per second. Positive is clockwise. Speeds of more than 1000 steps per second are unreliable. Very slow speeds may be set (eg 0.00027777 for once per hour, approximately. Speed accuracy depends on the Arduino crystal. Jitter depends on how frequently you call the runSpeed() function. |
Definition at line 129 of file LocoStepper.cpp.
float LocoStepper::speed | ( | ) |
The most recently set speed
Definition at line 135 of file LocoStepper.cpp.
|
protected |
|
protected |
Called to execute a step on a 2 pin motor. Only called when a new step is required. Subclasses may override to implement new stepping interfaces. The default sets or clears the outputs of pin1 and pin2
[in] | step | The current step phase number (0 to 3) |
Definition at line 143 of file LocoStepper.cpp.
|
protected |
Called to execute a step on a 4 pin motor. Only called when a new step is required. Subclasses may override to implement new stepping interfaces. The default sets or clears the outputs of pin1, pin2, pin3, pin4.
[in] | step | The current step phase number (0 to 3) |
Definition at line 159 of file LocoStepper.cpp.
long LocoStepper::targetPosition | ( | ) |
The most recently set target position.
Definition at line 113 of file LocoStepper.cpp.