#include <AccelStepper.h>
Public Member Functions | |
AccelStepper (uint8_t pins=4, uint8_t pin1=2, uint8_t pin2=3, uint8_t pin3=4, uint8_t pin4=5) | |
void | moveTo (long absolute) |
void | move (long relative) |
boolean | run () |
boolean | runSpeed () |
void | setMaxSpeed (float speed) |
void | setAcceleration (float acceleration) |
void | setSpeed (float speed) |
float | speed () |
long | distanceToGo () |
long | targetPosition () |
long | currentPosition () |
void | setCurrentPosition (long position) |
void | runToPosition () |
boolean | runSpeedToPosition () |
void | runToNewPosition (long position) |
void | disableOutputs () |
void | enableOutputs () |
Protected Member Functions | |
void | computeNewSpeed () |
virtual void | step (uint8_t step) |
virtual void | step1 (uint8_t step) |
virtual void | step2 (uint8_t step) |
virtual void | step4 (uint8_t step) |
virtual float | desiredSpeed () |
This defines a single 2 or 4 pin stepper motor, or stepper moter with fdriver chip, with optional acceleration, deceleration, absolute positioning commands etc. Multiple simultaneous steppers are supported, all moving at different speeds and accelerations.
AccelStepper::AccelStepper | ( | uint8_t | pins = 4 , |
|
uint8_t | pin1 = 2 , |
|||
uint8_t | pin2 = 3 , |
|||
uint8_t | pin3 = 4 , |
|||
uint8_t | pin4 = 5 | |||
) |
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. MaxSpeed and Acceleration default to 1.0. The motor pins will be initialised to OUTPUT mode during the constructor by a call to enableOutputs().
[in] | pins | Number of pins to interface to. 1, 2 or 4 are supported. 1 means a stepper driver (with Step and Direction pins) 2 means a 2 wire stepper. 4 means a 4 wire stepper. Defaults to 4 pins. |
[in] | pin1 | Arduino digital pin number for motor pin 1. Defaults to pin 2. For a driver (pins==1), this is the Step input to the driver. Low to high transition means to step) |
[in] | pin2 | Arduino digital pin number for motor pin 2. Defaults to pin 3. For a driver (pins==1), this is the Direction input the driver. High means forward. |
[in] | pin3 | Arduino digital pin number for motor pin 3. Defaults to pin 4. |
[in] | pin4 | Arduino digital pin number for motor pin 4. Defaults to pin 5. |
References enableOutputs().
void AccelStepper::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. |
References computeNewSpeed().
Referenced by move(), and runToNewPosition().
void AccelStepper::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. |
References moveTo().
boolean AccelStepper::run | ( | ) |
Poll the motor and step it if a step is due, implementing accelerations and decelerations to achive the ratget position. You must call this as fequently as possible, but at least once per minimum step interval, preferably in your main loop.
References computeNewSpeed(), and runSpeed().
Referenced by runToPosition().
boolean AccelStepper::runSpeed | ( | ) |
Poll the motor and step it if a step is due, implmenting a constant speed as set by the most recent call to setSpeed().
References step().
Referenced by run(), and runSpeedToPosition().
void AccelStepper::setMaxSpeed | ( | float | speed | ) |
Sets the maximum permitted speed. the run() function will accelerate up to the speed set by this function.
[in] | speed | The desired maximum speed in steps per second. Must be > 0. Speeds of more than 1000 steps per second are unreliable. |
References computeNewSpeed().
void AccelStepper::setAcceleration | ( | float | acceleration | ) |
Sets the acceleration and deceleration parameter.
[in] | acceleration | The desired acceleration in steps per second per second. Must be > 0. |
References computeNewSpeed().
void AccelStepper::setSpeed | ( | float | speed | ) |
Sets the desired constant speed for use with runSpeed().
[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. |
Referenced by computeNewSpeed().
float AccelStepper::speed | ( | ) |
The most recently set speed
long AccelStepper::distanceToGo | ( | ) |
The distance from the current position to the target position.
Referenced by desiredSpeed().
long AccelStepper::targetPosition | ( | ) |
The most recently set target position.
long AccelStepper::currentPosition | ( | ) |
The currently motor position.
void AccelStepper::setCurrentPosition | ( | long | position | ) |
Resets the current position of the motor, so that wherever the mottor 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. |
void AccelStepper::runToPosition | ( | ) |
Moves the motor to the target position and blocks until it is at position. Dont use this in event loops, since it blocks.
References run().
Referenced by runToNewPosition().
boolean AccelStepper::runSpeedToPosition | ( | ) |
Runs at the currently selected speed until the target position is reached Does not implement accelerations.
References runSpeed().
void AccelStepper::runToNewPosition | ( | long | position | ) |
Moves the motor to the new target position and blocks until it is at position. Dont use this in event loops, since it blocks.
[in] | position | The new target position. |
References moveTo(), and runToPosition().
void AccelStepper::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 reenable with enableOutputs() before stepping again.
void AccelStepper::enableOutputs | ( | ) |
Enable motor pin outputs by setting the motor pins to OUTPUT mode. Called automatically by the constructor.
Referenced by AccelStepper().
void AccelStepper::computeNewSpeed | ( | ) | [protected] |
Forces the library to compute a new instantaneous speed and set that as the current speed. Calls desiredSpeed(), which can be overridden by subclasses. It is called by the library:
References desiredSpeed(), and setSpeed().
Referenced by moveTo(), run(), setAcceleration(), and setMaxSpeed().
void AccelStepper::step | ( | uint8_t | step | ) | [protected, virtual] |
Called to execute a step. Only called when a new step is required. Subclasses may override to implement new stepping interfaces. The default calls step1(), step2() or step4() depending on the number of pins defined for the stepper.
[in] | step | The current step phase number (0 to 3) |
References step1(), step2(), and step4().
Referenced by runSpeed().
void AccelStepper::step1 | ( | uint8_t | step | ) | [protected, virtual] |
Called to execute a step on a stepper drover (ie where pins == 1). Only called when a new step is required. Subclasses may override to implement new stepping interfaces. The default sets or clears the outputs of Step pin1 to step, and sets the output of _pin2 to the desired direction. The Step pin (_pin1) is pulsed for 1 microsecond which is the minimum STEP pulse width for the 3967 driver.
[in] | step | The current step phase number (0 to 3) |
Referenced by step().
void AccelStepper::step2 | ( | uint8_t | step | ) | [protected, virtual] |
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) |
Referenced by step().
void AccelStepper::step4 | ( | uint8_t | step | ) | [protected, virtual] |
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) |
Referenced by step().
float AccelStepper::desiredSpeed | ( | ) | [protected, virtual] |
Compute and return the desired speed. The default algorithm uses maxSpeed, acceleration and the current speed to set a new speed to move the motor from teh current position to the target position. Subclasses may override this to provide an alternate algorithm (but do not block). Called by computeNewSpeed whenever a new speed neds to be computed.
References distanceToGo().
Referenced by computeNewSpeed().