Arduino Project Libraries  0.00
Stock and random Arduino Libraries
AccelStepper Class Reference

Support for stepper motors with acceleration etc. More...

#include <AccelStepper.h>

Collaboration diagram for AccelStepper:
Collaboration graph

Public Types

enum  MotorInterfaceType {
  FUNCTION = 0, DRIVER = 1, FULL2WIRE = 2, FULL3WIRE = 3,
  FULL4WIRE = 4, HALF3WIRE = 6, HALF4WIRE = 8
}
 Symbolic names for number of pins. Use this in the pins argument the AccelStepper constructor to provide a symbolic name for the number of pins to use. More...
 

Public Member Functions

 AccelStepper (uint8_t interface=AccelStepper::FULL4WIRE, uint8_t pin1=2, uint8_t pin2=3, uint8_t pin3=4, uint8_t pin4=5, bool enable=true)
 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(). More...
 
 AccelStepper (void(*forward)(), void(*backward)())
 Alternate Constructor which will call your own functions for forward and backward steps. 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. Any motor initialization should happen before hand, no pins are used or initialized. More...
 
void moveTo (long absolute)
 Set the target position. The run() function will try to move the motor (at most one step per call) from the current position to the target position set by the most recent call to this function. Caution: moveTo() also recalculates the speed for the next step. If you are trying to use constant speed movements, you should call setSpeed() after calling moveTo(). More...
 
void move (long relative)
 Set the target position relative to the current position. More...
 
boolean run ()
 Poll the motor and step it if a step is due, implementing accelerations and decelerations to acheive the target position. You must call this as frequently as possible, but at least once per minimum step time interval, preferably in your main loop. Note that each call to run() will make at most one step, and then only when a step is due, based on the current speed and the time since the last step. More...
 
boolean runSpeed ()
 Poll the motor and step it if a step is due, implementing a constant speed as set by the most recent call to setSpeed(). You must call this as frequently as possible, but at least once per step interval,. More...
 
void setMaxSpeed (float speed)
 Sets the maximum permitted speed. The run() function will accelerate up to the speed set by this function. Caution: the maximum speed achievable depends on your processor and clock speed. More...
 
float maxSpeed ()
 returns the maximum speed configured for this stepper that was previously set by setMaxSpeed(); More...
 
void setAcceleration (float acceleration)
 Sets the acceleration/deceleration rate. More...
 
void setSpeed (float speed)
 Sets the desired constant speed for use with runSpeed(). More...
 
float speed ()
 The most recently set speed. More...
 
long distanceToGo ()
 The distance from the current position to the target position. More...
 
long targetPosition ()
 The most recently set target position. More...
 
long currentPosition ()
 The currently motor position. More...
 
void 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 0 position. Useful for setting a zero position on a stepper after an initial hardware positioning move. Has the side effect of setting the current motor speed to 0. More...
 
void runToPosition ()
 Moves the motor (with acceleration/deceleration) to the target position and blocks until it is at position. Dont use this in event loops, since it blocks. More...
 
boolean runSpeedToPosition ()
 Runs at the currently selected speed until the target position is reached Does not implement accelerations. More...
 
void runToNewPosition (long position)
 Moves the motor (with acceleration/deceleration) to the new target position and blocks until it is at position. Dont use this in event loops, since it blocks. More...
 
void stop ()
 Sets a new target position that causes the stepper to stop as quickly as possible, using the current speed and acceleration parameters. More...
 
virtual void 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. If the enable Pin is defined, sets it to OUTPUT mode and clears the pin to disabled. More...
 
virtual void enableOutputs ()
 Enable motor pin outputs by setting the motor pins to OUTPUT mode. Called automatically by the constructor. If the enable Pin is defined, sets it to OUTPUT mode and sets the pin to enabled. More...
 
void setMinPulseWidth (unsigned int minWidth)
 Sets the minimum pulse width allowed by the stepper driver. The minimum practical pulse width is approximately 20 microseconds. Times less than 20 microseconds will usually result in 20 microseconds or so. More...
 
void setEnablePin (uint8_t enablePin=0xff)
 Sets the enable pin number for stepper drivers. 0xFF indicates unused (default). Otherwise, if a pin is set, the pin will be turned on when enableOutputs() is called and switched off when disableOutputs() is called. More...
 
void setPinsInverted (bool directionInvert=false, bool stepInvert=false, bool enableInvert=false)
 Sets the inversion for stepper driver pins. More...
 
void setPinsInverted (bool pin1Invert, bool pin2Invert, bool pin3Invert, bool pin4Invert, bool enableInvert)
 Sets the inversion for 2, 3 and 4 wire stepper pins. More...
 
bool isRunning ()
 Checks to see if the motor is currently running to a target. More...
 

Protected Types

enum  Direction { DIRECTION_CCW = 0, DIRECTION_CW = 1 }
 Direction indicator Symbolic names for the direction the motor is turning. More...
 

Protected Member Functions

void computeNewSpeed ()
 Forces the library to compute a new instantaneous speed and set that as the current speed. It is called by the library: More...
 
virtual void setOutputPins (uint8_t mask)
 Low level function to set the motor output pins bit 0 of the mask corresponds to _pin[0] bit 1 of the mask corresponds to _pin[1] You can override this to impment, for example serial chip output insted of using the output pins directly. More...
 
virtual void step (long step)
 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(), step4() or step8() depending on the number of pins defined for the stepper. More...
 
virtual void step0 (long step)
 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. More...
 
virtual void step1 (long step)
 Called to execute a step on a stepper driver (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. More...
 
virtual void step2 (long step)
 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. More...
 
virtual void step3 (long step)
 Called to execute a step on a 3 pin motor, such as HDD spindle. 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. More...
 
virtual void step4 (long step)
 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. More...
 
virtual void step6 (long step)
 Called to execute a step on a 3 pin motor, such as HDD spindle. 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. More...
 
virtual void step8 (long step)
 Called to execute a step on a 4 pin half-steper 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. More...
 

Protected Attributes

boolean _direction
 Current direction motor is spinning in Protected because some peoples subclasses need it to be so. More...
 

Private Attributes

uint8_t _interface
 Number of pins on the stepper motor. Permits 2 or 4. 2 pins is a bipolar, and 4 pins is a unipolar. More...
 
uint8_t _pin [4]
 Arduino pin number assignments for the 2 or 4 pins required to interface to the stepper motor or driver. More...
 
uint8_t _pinInverted [4]
 Whether the _pins is inverted or not. More...
 
long _currentPos
 The current absolution position in steps. More...
 
long _targetPos
 The target position in steps. The AccelStepper library will move the motor from the _currentPos to the _targetPos, taking into account the max speed, acceleration and deceleration. More...
 
float _speed
 The current motos speed in steps per second Positive is clockwise. More...
 
float _maxSpeed
 The maximum permitted speed in steps per second. Must be > 0. More...
 
float _acceleration
 The acceleration to use to accelerate or decelerate the motor in steps per second per second. Must be > 0. More...
 
float _sqrt_twoa
 
unsigned long _stepInterval
 The current interval between steps in microseconds. 0 means the motor is currently stopped with _speed == 0. More...
 
unsigned long _lastStepTime
 The last step time in microseconds. More...
 
unsigned int _minPulseWidth
 The minimum allowed pulse width in microseconds. More...
 
bool _enableInverted
 Is the direction pin inverted? bool _dirInverted; /// Moved to _pinInverted[1]. More...
 
uint8_t _enablePin
 Enable pin for stepper driver, or 0xFF if unused. More...
 
void(* _forward )()
 The pointer to a forward-step procedure. More...
 
void(* _backward )()
 The pointer to a backward-step procedure. More...
 
long _n
 The step counter for speed calculations. More...
 
float _c0
 Initial step size in microseconds. More...
 
float _cn
 Last step size in microseconds. More...
 
float _cmin
 Min step size in microseconds based on maxSpeed. More...
 

Detailed Description

Support for stepper motors with acceleration etc.

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.

Operation
This module operates by computing a step time in microseconds. The step time is recomputed after each step and after speed and acceleration parameters are changed by the caller. The time of each step is recorded in microseconds. The run() function steps the motor once if a new step is due. The run() function must be called frequently until the motor is in the desired position, after which time run() will do nothing.
Positioning
Positions are specified by a signed long integer. At construction time, the current position of the motor is consider to be 0. Positive positions are clockwise from the initial position; negative positions are anticlockwise. The current position can be altered for instance after initialization positioning.
Caveats
This is an open loop controller: If the motor stalls or is oversped, AccelStepper will not have a correct idea of where the motor really is (since there is no feedback of the motor's real position. We only know where we think it is, relative to the initial starting point).
Performance
The fastest motor speed that can be reliably supported is about 4000 steps per second at a clock frequency of 16 MHz on Arduino such as Uno etc. Faster processors can support faster stepping speeds. 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. Calling setAcceleration() is expensive, since it requires a square root to be calculated.

Gregor Christandl reports that with an Arduino Due and a simple test program, he measured 43163 steps per second using runSpeed(), and 16214 steps per second using run();

Definition at line 309 of file AccelStepper.h.

Member Enumeration Documentation

◆ Direction

enum AccelStepper::Direction
protected

Direction indicator Symbolic names for the direction the motor is turning.

Enumerator
DIRECTION_CCW 

Counter-Clockwise.

DIRECTION_CW 

Clockwise.

Definition at line 521 of file AccelStepper.h.

522  {
523  DIRECTION_CCW = 0, ///< Counter-Clockwise
524  DIRECTION_CW = 1 ///< Clockwise
525  } Direction;
Direction
Direction indicator Symbolic names for the direction the motor is turning.
Definition: AccelStepper.h:521
Counter-Clockwise.
Definition: AccelStepper.h:523

◆ MotorInterfaceType

Symbolic names for number of pins. Use this in the pins argument the AccelStepper constructor to provide a symbolic name for the number of pins to use.

Enumerator
FUNCTION 

Use the functional interface, implementing your own driver functions (internal use only)

DRIVER 

Stepper Driver, 2 driver pins required.

FULL2WIRE 

2 wire stepper, 2 motor pins required

FULL3WIRE 

3 wire stepper, such as HDD spindle, 3 motor pins required

FULL4WIRE 

4 wire full stepper, 4 motor pins required

HALF3WIRE 

3 wire half stepper, such as HDD spindle, 3 motor pins required

HALF4WIRE 

4 wire half stepper, 4 motor pins required

Definition at line 316 of file AccelStepper.h.

317  {
318  FUNCTION = 0, ///< Use the functional interface, implementing your own driver functions (internal use only)
319  DRIVER = 1, ///< Stepper Driver, 2 driver pins required
320  FULL2WIRE = 2, ///< 2 wire stepper, 2 motor pins required
321  FULL3WIRE = 3, ///< 3 wire stepper, such as HDD spindle, 3 motor pins required
322  FULL4WIRE = 4, ///< 4 wire full stepper, 4 motor pins required
323  HALF3WIRE = 6, ///< 3 wire half stepper, such as HDD spindle, 3 motor pins required
324  HALF4WIRE = 8 ///< 4 wire half stepper, 4 motor pins required
4 wire full stepper, 4 motor pins required
Definition: AccelStepper.h:322
MotorInterfaceType
Symbolic names for number of pins. Use this in the pins argument the AccelStepper constructor to prov...
Definition: AccelStepper.h:316
2 wire stepper, 2 motor pins required
Definition: AccelStepper.h:320
3 wire half stepper, such as HDD spindle, 3 motor pins required
Definition: AccelStepper.h:323
4 wire half stepper, 4 motor pins required
Definition: AccelStepper.h:324
3 wire stepper, such as HDD spindle, 3 motor pins required
Definition: AccelStepper.h:321
Stepper Driver, 2 driver pins required.
Definition: AccelStepper.h:319
Use the functional interface, implementing your own driver functions (internal use only) ...
Definition: AccelStepper.h:318

Constructor & Destructor Documentation

◆ AccelStepper() [1/2]

AccelStepper::AccelStepper ( uint8_t  interface = AccelStepper::FULL4WIRE,
uint8_t  pin1 = 2,
uint8_t  pin2 = 3,
uint8_t  pin3 = 4,
uint8_t  pin4 = 5,
bool  enable = true 
)

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().

Parameters
[in]interfaceNumber of pins to interface to. Integer values are supported, but it is preferred to use the MotorInterfaceType symbolic names. AccelStepper::DRIVER (1) means a stepper driver (with Step and Direction pins). If an enable line is also needed, call setEnablePin() after construction. You may also invert the pins using setPinsInverted(). AccelStepper::FULL2WIRE (2) means a 2 wire stepper (2 pins required). AccelStepper::FULL3WIRE (3) means a 3 wire stepper, such as HDD spindle (3 pins required). AccelStepper::FULL4WIRE (4) means a 4 wire stepper (4 pins required). AccelStepper::HALF3WIRE (6) means a 3 wire half stepper, such as HDD spindle (3 pins required) AccelStepper::HALF4WIRE (8) means a 4 wire half stepper (4 pins required) Defaults to AccelStepper::FULL4WIRE (4) pins.
[in]pin1Arduino digital pin number for motor pin 1. Defaults to pin 2. For a AccelStepper::DRIVER (interface==1), this is the Step input to the driver. Low to high transition means to step)
[in]pin2Arduino digital pin number for motor pin 2. Defaults to pin 3. For a AccelStepper::DRIVER (interface==1), this is the Direction input the driver. High means forward.
[in]pin3Arduino digital pin number for motor pin 3. Defaults to pin 4.
[in]pin4Arduino digital pin number for motor pin 4. Defaults to pin 5.
[in]enableIf this is true (the default), enableOutputs() will be called to enable the output pins at construction time.

Definition at line 190 of file AccelStepper.cpp.

References _acceleration, _c0, _cmin, _cn, _currentPos, _enableInverted, _lastStepTime, _maxSpeed, _minPulseWidth, _n, _speed, _sqrt_twoa, _stepInterval, _targetPos, enableOutputs(), and setAcceleration().

191 {
193  _currentPos = 0;
194  _targetPos = 0;
195  _speed = 0.0;
196  _maxSpeed = 1.0;
197  _acceleration = 0.0;
198  _sqrt_twoa = 1.0;
199  _stepInterval = 0;
200  _minPulseWidth = 1;
201  _enablePin = 0xff;
202  _lastStepTime = 0;
203  _pin[0] = pin1;
204  _pin[1] = pin2;
205  _pin[2] = pin3;
206  _pin[3] = pin4;
207  _enableInverted = false;
208 
209  // NEW
210  _n = 0;
211  _c0 = 0.0;
212  _cn = 0.0;
213  _cmin = 1.0;
215 
216  int i;
217  for (i = 0; i < 4; i++)
218  _pinInverted[i] = 0;
219  if (enable)
220  enableOutputs();
221  // Some reasonable default
222  setAcceleration(1);
223 }
float _cn
Last step size in microseconds.
Definition: AccelStepper.h:669
unsigned int _minPulseWidth
The minimum allowed pulse width in microseconds.
Definition: AccelStepper.h:642
void setAcceleration(float acceleration)
Sets the acceleration/deceleration rate.
long _n
The step counter for speed calculations.
Definition: AccelStepper.h:663
float _speed
The current motos speed in steps per second Positive is clockwise.
Definition: AccelStepper.h:624
long _targetPos
The target position in steps. The AccelStepper library will move the motor from the _currentPos to th...
Definition: AccelStepper.h:620
boolean _direction
Current direction motor is spinning in Protected because some peoples subclasses need it to be so...
Definition: AccelStepper.h:600
unsigned long _lastStepTime
The last step time in microseconds.
Definition: AccelStepper.h:639
float _acceleration
The acceleration to use to accelerate or decelerate the motor in steps per second per second...
Definition: AccelStepper.h:631
uint8_t _pinInverted[4]
Whether the _pins is inverted or not.
Definition: AccelStepper.h:612
virtual void enableOutputs()
Enable motor pin outputs by setting the motor pins to OUTPUT mode. Called automatically by the constr...
#define interface
uint8_t _interface
Number of pins on the stepper motor. Permits 2 or 4. 2 pins is a bipolar, and 4 pins is a unipolar...
Definition: AccelStepper.h:605
long _currentPos
The current absolution position in steps.
Definition: AccelStepper.h:615
float _cmin
Min step size in microseconds based on maxSpeed.
Definition: AccelStepper.h:672
unsigned long _stepInterval
The current interval between steps in microseconds. 0 means the motor is currently stopped with _spee...
Definition: AccelStepper.h:636
Counter-Clockwise.
Definition: AccelStepper.h:523
uint8_t _pin[4]
Arduino pin number assignments for the 2 or 4 pins required to interface to the stepper motor or driv...
Definition: AccelStepper.h:609
bool _enableInverted
Is the direction pin inverted? bool _dirInverted; /// Moved to _pinInverted[1].
Definition: AccelStepper.h:651
uint8_t _enablePin
Enable pin for stepper driver, or 0xFF if unused.
Definition: AccelStepper.h:654
float _sqrt_twoa
Definition: AccelStepper.h:632
float _c0
Initial step size in microseconds.
Definition: AccelStepper.h:666
float _maxSpeed
The maximum permitted speed in steps per second. Must be > 0.
Definition: AccelStepper.h:627
Here is the call graph for this function:

◆ AccelStepper() [2/2]

AccelStepper::AccelStepper ( void(*)()  forward,
void(*)()  backward 
)

Alternate Constructor which will call your own functions for forward and backward steps. 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. Any motor initialization should happen before hand, no pins are used or initialized.

Parameters
[in]forwardvoid-returning procedure that will make a forward step
[in]backwardvoid-returning procedure that will make a backward step

Definition at line 225 of file AccelStepper.cpp.

References _acceleration, _backward, _c0, _cmin, _cn, _currentPos, _forward, _lastStepTime, _maxSpeed, _minPulseWidth, _n, _speed, _sqrt_twoa, _stepInterval, _targetPos, and setAcceleration().

226 {
227  _interface = 0;
228  _currentPos = 0;
229  _targetPos = 0;
230  _speed = 0.0;
231  _maxSpeed = 1.0;
232  _acceleration = 0.0;
233  _sqrt_twoa = 1.0;
234  _stepInterval = 0;
235  _minPulseWidth = 1;
236  _enablePin = 0xff;
237  _lastStepTime = 0;
238  _pin[0] = 0;
239  _pin[1] = 0;
240  _pin[2] = 0;
241  _pin[3] = 0;
242  _forward = forward;
243  _backward = backward;
244 
245  // NEW
246  _n = 0;
247  _c0 = 0.0;
248  _cn = 0.0;
249  _cmin = 1.0;
251 
252  int i;
253  for (i = 0; i < 4; i++)
254  _pinInverted[i] = 0;
255  // Some reasonable default
256  setAcceleration(1);
257 }
float _cn
Last step size in microseconds.
Definition: AccelStepper.h:669
void(* _backward)()
The pointer to a backward-step procedure.
Definition: AccelStepper.h:660
unsigned int _minPulseWidth
The minimum allowed pulse width in microseconds.
Definition: AccelStepper.h:642
void(* _forward)()
The pointer to a forward-step procedure.
Definition: AccelStepper.h:657
void setAcceleration(float acceleration)
Sets the acceleration/deceleration rate.
long _n
The step counter for speed calculations.
Definition: AccelStepper.h:663
float _speed
The current motos speed in steps per second Positive is clockwise.
Definition: AccelStepper.h:624
long _targetPos
The target position in steps. The AccelStepper library will move the motor from the _currentPos to th...
Definition: AccelStepper.h:620
boolean _direction
Current direction motor is spinning in Protected because some peoples subclasses need it to be so...
Definition: AccelStepper.h:600
unsigned long _lastStepTime
The last step time in microseconds.
Definition: AccelStepper.h:639
float _acceleration
The acceleration to use to accelerate or decelerate the motor in steps per second per second...
Definition: AccelStepper.h:631
uint8_t _pinInverted[4]
Whether the _pins is inverted or not.
Definition: AccelStepper.h:612
template DEFAULT_T_CLAUSE CONSTEXPR T && forward(typename remove_reference< T >::type &arg)
uint8_t _interface
Number of pins on the stepper motor. Permits 2 or 4. 2 pins is a bipolar, and 4 pins is a unipolar...
Definition: AccelStepper.h:605
long _currentPos
The current absolution position in steps.
Definition: AccelStepper.h:615
float _cmin
Min step size in microseconds based on maxSpeed.
Definition: AccelStepper.h:672
unsigned long _stepInterval
The current interval between steps in microseconds. 0 means the motor is currently stopped with _spee...
Definition: AccelStepper.h:636
Counter-Clockwise.
Definition: AccelStepper.h:523
uint8_t _pin[4]
Arduino pin number assignments for the 2 or 4 pins required to interface to the stepper motor or driv...
Definition: AccelStepper.h:609
uint8_t _enablePin
Enable pin for stepper driver, or 0xFF if unused.
Definition: AccelStepper.h:654
float _sqrt_twoa
Definition: AccelStepper.h:632
float _c0
Initial step size in microseconds.
Definition: AccelStepper.h:666
float _maxSpeed
The maximum permitted speed in steps per second. Must be > 0.
Definition: AccelStepper.h:627
Here is the call graph for this function:

Member Function Documentation

◆ computeNewSpeed()

void AccelStepper::computeNewSpeed ( )
protected

Forces the library to compute a new instantaneous speed and set that as the current speed. It is called by the library:

Definition at line 97 of file AccelStepper.cpp.

References _acceleration, _c0, _cn, _n, _speed, _stepInterval, and distanceToGo().

Referenced by moveTo(), run(), setAcceleration(), and setMaxSpeed().

98 {
99  long distanceTo = distanceToGo(); // +ve is clockwise from curent location
100 
101  long stepsToStop = (long)((_speed * _speed) / (2.0 * _acceleration)); // Equation 16
102 
103  if (distanceTo == 0 && stepsToStop <= 1)
104  {
105  // We are at the target and its time to stop
106  _stepInterval = 0;
107  _speed = 0.0;
108  _n = 0;
109  return;
110  }
111 
112  if (distanceTo > 0)
113  {
114  // We are anticlockwise from the target
115  // Need to go clockwise from here, maybe decelerate now
116  if (_n > 0)
117  {
118  // Currently accelerating, need to decel now? Or maybe going the wrong way?
119  if ((stepsToStop >= distanceTo) || _direction == DIRECTION_CCW)
120  _n = -stepsToStop; // Start deceleration
121  }
122  else if (_n < 0)
123  {
124  // Currently decelerating, need to accel again?
125  if ((stepsToStop < distanceTo) && _direction == DIRECTION_CW)
126  _n = -_n; // Start accceleration
127  }
128  }
129  else if (distanceTo < 0)
130  {
131  // We are clockwise from the target
132  // Need to go anticlockwise from here, maybe decelerate
133  if (_n > 0)
134  {
135  // Currently accelerating, need to decel now? Or maybe going the wrong way?
136  if ((stepsToStop >= -distanceTo) || _direction == DIRECTION_CW)
137  _n = -stepsToStop; // Start deceleration
138  }
139  else if (_n < 0)
140  {
141  // Currently decelerating, need to accel again?
142  if ((stepsToStop < -distanceTo) && _direction == DIRECTION_CCW)
143  _n = -_n; // Start accceleration
144  }
145  }
146 
147  // Need to accelerate or decelerate
148  if (_n == 0)
149  {
150  // First step from stopped
151  _cn = _c0;
152  _direction = (distanceTo > 0) ? DIRECTION_CW : DIRECTION_CCW;
153  }
154  else
155  {
156  // Subsequent step. Works for accel (n is +_ve) and decel (n is -ve).
157  _cn = _cn - ((2.0 * _cn) / ((4.0 * _n) + 1)); // Equation 13
158  _cn = max(_cn, _cmin);
159  }
160  _n++;
161  _stepInterval = _cn;
162  _speed = 1000000.0 / _cn;
163  if (_direction == DIRECTION_CCW)
164  _speed = -_speed;
165 
166 #if 0
167  Serial.println(_speed);
168  Serial.println(_acceleration);
169  Serial.println(_cn);
170  Serial.println(_c0);
171  Serial.println(_n);
172  Serial.println(_stepInterval);
173  Serial.println(distanceTo);
174  Serial.println(stepsToStop);
175  Serial.println("-----");
176 #endif
177 }
float _cn
Last step size in microseconds.
Definition: AccelStepper.h:669
long distanceToGo()
The distance from the current position to the target position.
long _n
The step counter for speed calculations.
Definition: AccelStepper.h:663
float _speed
The current motos speed in steps per second Positive is clockwise.
Definition: AccelStepper.h:624
boolean _direction
Current direction motor is spinning in Protected because some peoples subclasses need it to be so...
Definition: AccelStepper.h:600
float _acceleration
The acceleration to use to accelerate or decelerate the motor in steps per second per second...
Definition: AccelStepper.h:631
float _cmin
Min step size in microseconds based on maxSpeed.
Definition: AccelStepper.h:672
unsigned long _stepInterval
The current interval between steps in microseconds. 0 means the motor is currently stopped with _spee...
Definition: AccelStepper.h:636
Counter-Clockwise.
Definition: AccelStepper.h:523
#define max(a, b)
float _c0
Initial step size in microseconds.
Definition: AccelStepper.h:666
Here is the call graph for this function:
Here is the caller graph for this function:

◆ currentPosition()

long AccelStepper::currentPosition ( )

The currently motor position.

Returns
the current motor position in steps. Positive is clockwise from the 0 position.

Definition at line 82 of file AccelStepper.cpp.

References _currentPos.

83 {
84  return _currentPos;
85 }
long _currentPos
The current absolution position in steps.
Definition: AccelStepper.h:615

◆ disableOutputs()

void AccelStepper::disableOutputs ( )
virtual

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. If the enable Pin is defined, sets it to OUTPUT mode and clears the pin to disabled.

Definition at line 543 of file AccelStepper.cpp.

544 {
545  if (! _interface) return;
546 
547  setOutputPins(0); // Handles inversion automatically
548  if (_enablePin != 0xff)
549  {
552  }
553 }
#define digitalWrite(pin, value)
#define LOW
Definition: io.h:42
virtual void setOutputPins(uint8_t mask)
Low level function to set the motor output pins bit 0 of the mask corresponds to _pin[0] bit 1 of the...
#define pinMode(pin, value)
uint8_t _interface
Number of pins on the stepper motor. Permits 2 or 4. 2 pins is a bipolar, and 4 pins is a unipolar...
Definition: AccelStepper.h:605
bool _enableInverted
Is the direction pin inverted? bool _dirInverted; /// Moved to _pinInverted[1].
Definition: AccelStepper.h:651
uint8_t _enablePin
Enable pin for stepper driver, or 0xFF if unused.
Definition: AccelStepper.h:654
#define OUTPUT
Definition: io.h:45

◆ distanceToGo()

long AccelStepper::distanceToGo ( )

The distance from the current position to the target position.

Returns
the distance from the current position to the target position in steps. Positive is clockwise from the current position.

Definition at line 72 of file AccelStepper.cpp.

References _currentPos, and _targetPos.

Referenced by computeNewSpeed(), and run().

73 {
74  return _targetPos - _currentPos;
75 }
long _targetPos
The target position in steps. The AccelStepper library will move the motor from the _currentPos to th...
Definition: AccelStepper.h:620
long _currentPos
The current absolution position in steps.
Definition: AccelStepper.h:615
Here is the caller graph for this function:

◆ enableOutputs()

void AccelStepper::enableOutputs ( )
virtual

Enable motor pin outputs by setting the motor pins to OUTPUT mode. Called automatically by the constructor. If the enable Pin is defined, sets it to OUTPUT mode and sets the pin to enabled.

Definition at line 555 of file AccelStepper.cpp.

Referenced by AccelStepper().

556 {
557  if (! _interface)
558  return;
559 
560  pinMode(_pin[0], OUTPUT);
561  pinMode(_pin[1], OUTPUT);
563  {
564  pinMode(_pin[2], OUTPUT);
565  pinMode(_pin[3], OUTPUT);
566  }
567  else if (_interface == FULL3WIRE || _interface == HALF3WIRE)
568  {
569  pinMode(_pin[2], OUTPUT);
570  }
571 
572  if (_enablePin != 0xff)
573  {
576  }
577 }
4 wire full stepper, 4 motor pins required
Definition: AccelStepper.h:322
#define digitalWrite(pin, value)
3 wire half stepper, such as HDD spindle, 3 motor pins required
Definition: AccelStepper.h:323
#define HIGH
Definition: io.h:43
4 wire half stepper, 4 motor pins required
Definition: AccelStepper.h:324
3 wire stepper, such as HDD spindle, 3 motor pins required
Definition: AccelStepper.h:321
#define pinMode(pin, value)
uint8_t _interface
Number of pins on the stepper motor. Permits 2 or 4. 2 pins is a bipolar, and 4 pins is a unipolar...
Definition: AccelStepper.h:605
uint8_t _pin[4]
Arduino pin number assignments for the 2 or 4 pins required to interface to the stepper motor or driv...
Definition: AccelStepper.h:609
bool _enableInverted
Is the direction pin inverted? bool _dirInverted; /// Moved to _pinInverted[1].
Definition: AccelStepper.h:651
uint8_t _enablePin
Enable pin for stepper driver, or 0xFF if unused.
Definition: AccelStepper.h:654
#define OUTPUT
Definition: io.h:45
Here is the caller graph for this function:

◆ isRunning()

bool AccelStepper::isRunning ( )

Checks to see if the motor is currently running to a target.

Returns
true if the speed is not zero or not at the target position

Definition at line 649 of file AccelStepper.cpp.

References _currentPos, _speed, and _targetPos.

650 {
651  return !(_speed == 0.0 && _targetPos == _currentPos);
652 }
float _speed
The current motos speed in steps per second Positive is clockwise.
Definition: AccelStepper.h:624
long _targetPos
The target position in steps. The AccelStepper library will move the motor from the _currentPos to th...
Definition: AccelStepper.h:620
long _currentPos
The current absolution position in steps.
Definition: AccelStepper.h:615

◆ maxSpeed()

float AccelStepper::maxSpeed ( )

returns the maximum speed configured for this stepper that was previously set by setMaxSpeed();

Returns
The currently configured maximum speed

Definition at line 276 of file AccelStepper.cpp.

References _maxSpeed.

277 {
278  return _maxSpeed;
279 }
float _maxSpeed
The maximum permitted speed in steps per second. Must be > 0.
Definition: AccelStepper.h:627

◆ move()

void AccelStepper::move ( long  relative)

Set the target position relative to the current position.

Parameters
[in]relativeThe desired position relative to the current position. Negative is anticlockwise from the current position.

Definition at line 33 of file AccelStepper.cpp.

References _currentPos, and moveTo().

Referenced by stop().

34 {
35  moveTo(_currentPos + relative);
36 }
void moveTo(long absolute)
Set the target position. The run() function will try to move the motor (at most one step per call) fr...
long _currentPos
The current absolution position in steps.
Definition: AccelStepper.h:615
Here is the call graph for this function:
Here is the caller graph for this function:

◆ moveTo()

void AccelStepper::moveTo ( long  absolute)

Set the target position. The run() function will try to move the motor (at most one step per call) from the current position to the target position set by the most recent call to this function. Caution: moveTo() also recalculates the speed for the next step. If you are trying to use constant speed movements, you should call setSpeed() after calling moveTo().

Parameters
[in]absoluteThe desired absolute position. Negative is anticlockwise from the 0 position.

Definition at line 23 of file AccelStepper.cpp.

References _targetPos, and computeNewSpeed().

Referenced by move(), and runToNewPosition().

24 {
25  if (_targetPos != absolute)
26  {
27  _targetPos = absolute;
29  // compute new n?
30  }
31 }
void computeNewSpeed()
Forces the library to compute a new instantaneous speed and set that as the current speed...
long _targetPos
The target position in steps. The AccelStepper library will move the motor from the _currentPos to th...
Definition: AccelStepper.h:620
Here is the call graph for this function:
Here is the caller graph for this function:

◆ run()

boolean AccelStepper::run ( )

Poll the motor and step it if a step is due, implementing accelerations and decelerations to acheive the target position. You must call this as frequently as possible, but at least once per minimum step time interval, preferably in your main loop. Note that each call to run() will make at most one step, and then only when a step is due, based on the current speed and the time since the last step.

Returns
true if the motor is still running to the target position.

Definition at line 183 of file AccelStepper.cpp.

References _speed, computeNewSpeed(), and distanceToGo().

184 {
185  if (runSpeed())
186  computeNewSpeed();
187  return _speed != 0.0 || distanceToGo() != 0;
188 }
long distanceToGo()
The distance from the current position to the target position.
float _speed
The current motos speed in steps per second Positive is clockwise.
Definition: AccelStepper.h:624
void computeNewSpeed()
Forces the library to compute a new instantaneous speed and set that as the current speed...
boolean runSpeed()
Poll the motor and step it if a step is due, implementing a constant speed as set by the most recent ...
Here is the call graph for this function:

◆ runSpeed()

boolean AccelStepper::runSpeed ( )

Poll the motor and step it if a step is due, implementing a constant speed as set by the most recent call to setSpeed(). You must call this as frequently as possible, but at least once per step interval,.

Returns
true if the motor was stepped.

Definition at line 41 of file AccelStepper.cpp.

References _currentPos, _lastStepTime, _stepInterval, and step().

42 {
43  // Dont do anything unless we actually have a step interval
44  if (!_stepInterval)
45  return false;
46 
47  unsigned long time = micros();
48  if (time - _lastStepTime >= _stepInterval)
49  {
50  if (_direction == DIRECTION_CW)
51  {
52  // Clockwise
53  _currentPos += 1;
54  }
55  else
56  {
57  // Anticlockwise
58  _currentPos -= 1;
59  }
61 
62  _lastStepTime = time; // Caution: does not account for costs in step()
63 
64  return true;
65  }
66  else
67  {
68  return false;
69  }
70 }
time_t time(time_t *timer)
Definition: time.c:42
boolean _direction
Current direction motor is spinning in Protected because some peoples subclasses need it to be so...
Definition: AccelStepper.h:600
unsigned long _lastStepTime
The last step time in microseconds.
Definition: AccelStepper.h:639
long _currentPos
The current absolution position in steps.
Definition: AccelStepper.h:615
unsigned long _stepInterval
The current interval between steps in microseconds. 0 means the motor is currently stopped with _spee...
Definition: AccelStepper.h:636
static uint32_t micros(void)
Definition: io.h:77
virtual void step(long step)
Called to execute a step. Only called when a new step is required. Subclasses may override to impleme...
Here is the call graph for this function:

◆ runSpeedToPosition()

boolean AccelStepper::runSpeedToPosition ( )

Runs at the currently selected speed until the target position is reached Does not implement accelerations.

Returns
true if it stepped

Definition at line 619 of file AccelStepper.cpp.

References _currentPos, and _targetPos.

620 {
621  if (_targetPos == _currentPos)
622  return false;
623  if (_targetPos >_currentPos)
625  else
627  return runSpeed();
628 }
long _targetPos
The target position in steps. The AccelStepper library will move the motor from the _currentPos to th...
Definition: AccelStepper.h:620
boolean _direction
Current direction motor is spinning in Protected because some peoples subclasses need it to be so...
Definition: AccelStepper.h:600
boolean runSpeed()
Poll the motor and step it if a step is due, implementing a constant speed as set by the most recent ...
long _currentPos
The current absolution position in steps.
Definition: AccelStepper.h:615
Counter-Clockwise.
Definition: AccelStepper.h:523

◆ runToNewPosition()

void AccelStepper::runToNewPosition ( long  position)

Moves the motor (with acceleration/deceleration) to the new target position and blocks until it is at position. Dont use this in event loops, since it blocks.

Parameters
[in]positionThe new target position.

Definition at line 631 of file AccelStepper.cpp.

References moveTo(), and runToPosition().

632 {
633  moveTo(position);
634  runToPosition();
635 }
void moveTo(long absolute)
Set the target position. The run() function will try to move the motor (at most one step per call) fr...
void runToPosition()
Moves the motor (with acceleration/deceleration) to the target position and blocks until it is at pos...
Here is the call graph for this function:

◆ runToPosition()

void AccelStepper::runToPosition ( )

Moves the motor (with acceleration/deceleration) to the target position and blocks until it is at position. Dont use this in event loops, since it blocks.

Definition at line 613 of file AccelStepper.cpp.

Referenced by runToNewPosition().

614 {
615  while (run())
616  ;
617 }
boolean run()
Poll the motor and step it if a step is due, implementing accelerations and decelerations to acheive ...
Here is the caller graph for this function:

◆ setAcceleration()

void AccelStepper::setAcceleration ( float  acceleration)

Sets the acceleration/deceleration rate.

Parameters
[in]accelerationThe desired acceleration in steps per second per second. Must be > 0.0. This is an expensive call since it requires a square root to be calculated. Dont call more ofthen than needed

Definition at line 281 of file AccelStepper.cpp.

References _acceleration, _n, and computeNewSpeed().

Referenced by AccelStepper().

282 {
283  if (acceleration == 0.0)
284  return;
285  if (acceleration < 0.0)
286  acceleration = -acceleration;
287  if (_acceleration != acceleration)
288  {
289  // Recompute _n per Equation 17
290  _n = _n * (_acceleration / acceleration);
291  // New c0 per Equation 7, with correction per Equation 15
292  _c0 = 0.676 * sqrt(2.0 / acceleration) * 1000000.0; // Equation 15
293  _acceleration = acceleration;
294  computeNewSpeed();
295  }
296 }
long _n
The step counter for speed calculations.
Definition: AccelStepper.h:663
void computeNewSpeed()
Forces the library to compute a new instantaneous speed and set that as the current speed...
float _acceleration
The acceleration to use to accelerate or decelerate the motor in steps per second per second...
Definition: AccelStepper.h:631
float _c0
Initial step size in microseconds.
Definition: AccelStepper.h:666
Here is the call graph for this function:
Here is the caller graph for this function:

◆ setCurrentPosition()

void AccelStepper::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 0 position. Useful for setting a zero position on a stepper after an initial hardware positioning move. Has the side effect of setting the current motor speed to 0.

Parameters
[in]positionThe position in steps of wherever the motor happens to be right now.

Definition at line 89 of file AccelStepper.cpp.

References _currentPos, _n, _speed, _stepInterval, and _targetPos.

90 {
91  _targetPos = _currentPos = position;
92  _n = 0;
93  _stepInterval = 0;
94  _speed = 0.0;
95 }
long _n
The step counter for speed calculations.
Definition: AccelStepper.h:663
float _speed
The current motos speed in steps per second Positive is clockwise.
Definition: AccelStepper.h:624
long _targetPos
The target position in steps. The AccelStepper library will move the motor from the _currentPos to th...
Definition: AccelStepper.h:620
long _currentPos
The current absolution position in steps.
Definition: AccelStepper.h:615
unsigned long _stepInterval
The current interval between steps in microseconds. 0 means the motor is currently stopped with _spee...
Definition: AccelStepper.h:636

◆ setEnablePin()

void AccelStepper::setEnablePin ( uint8_t  enablePin = 0xff)

Sets the enable pin number for stepper drivers. 0xFF indicates unused (default). Otherwise, if a pin is set, the pin will be turned on when enableOutputs() is called and switched off when disableOutputs() is called.

Parameters
[in]enablePinArduino digital pin number for motor enable
See also
setPinsInverted

Definition at line 584 of file AccelStepper.cpp.

585 {
586  _enablePin = enablePin;
587 
588  // This happens after construction, so init pin now.
589  if (_enablePin != 0xff)
590  {
593  }
594 }
#define digitalWrite(pin, value)
#define HIGH
Definition: io.h:43
#define pinMode(pin, value)
bool _enableInverted
Is the direction pin inverted? bool _dirInverted; /// Moved to _pinInverted[1].
Definition: AccelStepper.h:651
uint8_t _enablePin
Enable pin for stepper driver, or 0xFF if unused.
Definition: AccelStepper.h:654
#define OUTPUT
Definition: io.h:45

◆ setMaxSpeed()

void AccelStepper::setMaxSpeed ( float  speed)

Sets the maximum permitted speed. The run() function will accelerate up to the speed set by this function. Caution: the maximum speed achievable depends on your processor and clock speed.

Parameters
[in]speedThe desired maximum speed in steps per second. Must be > 0. Caution: Speeds that exceed the maximum speed supported by the processor may Result in non-linear accelerations and decelerations.

Definition at line 259 of file AccelStepper.cpp.

References _acceleration, _cmin, _maxSpeed, _n, _speed, and computeNewSpeed().

260 {
261  if (speed < 0.0)
262  speed = -speed;
263  if (_maxSpeed != speed)
264  {
265  _maxSpeed = speed;
266  _cmin = 1000000.0 / speed;
267  // Recompute _n from current speed and adjust speed if accelerating or cruising
268  if (_n > 0)
269  {
270  _n = (long)((_speed * _speed) / (2.0 * _acceleration)); // Equation 16
271  computeNewSpeed();
272  }
273  }
274 }
float speed()
The most recently set speed.
long _n
The step counter for speed calculations.
Definition: AccelStepper.h:663
float _speed
The current motos speed in steps per second Positive is clockwise.
Definition: AccelStepper.h:624
void computeNewSpeed()
Forces the library to compute a new instantaneous speed and set that as the current speed...
float _acceleration
The acceleration to use to accelerate or decelerate the motor in steps per second per second...
Definition: AccelStepper.h:631
float _cmin
Min step size in microseconds based on maxSpeed.
Definition: AccelStepper.h:672
float _maxSpeed
The maximum permitted speed in steps per second. Must be > 0.
Definition: AccelStepper.h:627
Here is the call graph for this function:

◆ setMinPulseWidth()

void AccelStepper::setMinPulseWidth ( unsigned int  minWidth)

Sets the minimum pulse width allowed by the stepper driver. The minimum practical pulse width is approximately 20 microseconds. Times less than 20 microseconds will usually result in 20 microseconds or so.

Parameters
[in]minWidthThe minimum pulse width in microseconds.

Definition at line 579 of file AccelStepper.cpp.

References _minPulseWidth.

580 {
581  _minPulseWidth = minWidth;
582 }
unsigned int _minPulseWidth
The minimum allowed pulse width in microseconds.
Definition: AccelStepper.h:642

◆ setOutputPins()

void AccelStepper::setOutputPins ( uint8_t  mask)
protectedvirtual

Low level function to set the motor output pins bit 0 of the mask corresponds to _pin[0] bit 1 of the mask corresponds to _pin[1] You can override this to impment, for example serial chip output insted of using the output pins directly.

Definition at line 357 of file AccelStepper.cpp.

358 {
359  uint8_t numpins = 2;
361  numpins = 4;
362  else if (_interface == FULL3WIRE || _interface == HALF3WIRE)
363  numpins = 3;
364  uint8_t i;
365  for (i = 0; i < numpins; i++)
366  digitalWrite(_pin[i], (mask & (1 << i)) ? (HIGH ^ _pinInverted[i]) : (LOW ^ _pinInverted[i]));
367 }
4 wire full stepper, 4 motor pins required
Definition: AccelStepper.h:322
#define digitalWrite(pin, value)
#define LOW
Definition: io.h:42
3 wire half stepper, such as HDD spindle, 3 motor pins required
Definition: AccelStepper.h:323
#define HIGH
Definition: io.h:43
4 wire half stepper, 4 motor pins required
Definition: AccelStepper.h:324
3 wire stepper, such as HDD spindle, 3 motor pins required
Definition: AccelStepper.h:321
uint8_t _pinInverted[4]
Whether the _pins is inverted or not.
Definition: AccelStepper.h:612
uint8_t _interface
Number of pins on the stepper motor. Permits 2 or 4. 2 pins is a bipolar, and 4 pins is a unipolar...
Definition: AccelStepper.h:605
uint8_t _pin[4]
Arduino pin number assignments for the 2 or 4 pins required to interface to the stepper motor or driv...
Definition: AccelStepper.h:609

◆ setPinsInverted() [1/2]

void AccelStepper::setPinsInverted ( bool  directionInvert = false,
bool  stepInvert = false,
bool  enableInvert = false 
)

Sets the inversion for stepper driver pins.

Parameters
[in]directionInvertTrue for inverted direction pin, false for non-inverted
[in]stepInvertTrue for inverted step pin, false for non-inverted
[in]enableInvertTrue for inverted enable pin, false (default) for non-inverted

Definition at line 596 of file AccelStepper.cpp.

References _enableInverted.

597 {
598  _pinInverted[0] = stepInvert;
599  _pinInverted[1] = directionInvert;
600  _enableInverted = enableInvert;
601 }
uint8_t _pinInverted[4]
Whether the _pins is inverted or not.
Definition: AccelStepper.h:612
bool _enableInverted
Is the direction pin inverted? bool _dirInverted; /// Moved to _pinInverted[1].
Definition: AccelStepper.h:651

◆ setPinsInverted() [2/2]

void AccelStepper::setPinsInverted ( bool  pin1Invert,
bool  pin2Invert,
bool  pin3Invert,
bool  pin4Invert,
bool  enableInvert 
)

Sets the inversion for 2, 3 and 4 wire stepper pins.

Parameters
[in]pin1InvertTrue for inverted pin1, false for non-inverted
[in]pin2InvertTrue for inverted pin2, false for non-inverted
[in]pin3InvertTrue for inverted pin3, false for non-inverted
[in]pin4InvertTrue for inverted pin4, false for non-inverted
[in]enableInvertTrue for inverted enable pin, false (default) for non-inverted

Definition at line 603 of file AccelStepper.cpp.

References _enableInverted.

604 {
605  _pinInverted[0] = pin1Invert;
606  _pinInverted[1] = pin2Invert;
607  _pinInverted[2] = pin3Invert;
608  _pinInverted[3] = pin4Invert;
609  _enableInverted = enableInvert;
610 }
uint8_t _pinInverted[4]
Whether the _pins is inverted or not.
Definition: AccelStepper.h:612
bool _enableInverted
Is the direction pin inverted? bool _dirInverted; /// Moved to _pinInverted[1].
Definition: AccelStepper.h:651

◆ setSpeed()

void AccelStepper::setSpeed ( float  speed)

Sets the desired constant speed for use with runSpeed().

Parameters
[in]speedThe 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 298 of file AccelStepper.cpp.

References _speed, and _stepInterval.

299 {
300  if (speed == _speed)
301  return;
302  speed = constrain(speed, -_maxSpeed, _maxSpeed);
303  if (speed == 0.0)
304  _stepInterval = 0;
305  else
306  {
307  _stepInterval = fabs(1000000.0 / speed);
309  }
310  _speed = speed;
311 }
float speed()
The most recently set speed.
float _speed
The current motos speed in steps per second Positive is clockwise.
Definition: AccelStepper.h:624
boolean _direction
Current direction motor is spinning in Protected because some peoples subclasses need it to be so...
Definition: AccelStepper.h:600
unsigned long _stepInterval
The current interval between steps in microseconds. 0 means the motor is currently stopped with _spee...
Definition: AccelStepper.h:636
Counter-Clockwise.
Definition: AccelStepper.h:523
float _maxSpeed
The maximum permitted speed in steps per second. Must be > 0.
Definition: AccelStepper.h:627

◆ speed()

float AccelStepper::speed ( )

The most recently set speed.

Returns
the most recent speed in steps per second

Definition at line 313 of file AccelStepper.cpp.

References _speed.

314 {
315  return _speed;
316 }
float _speed
The current motos speed in steps per second Positive is clockwise.
Definition: AccelStepper.h:624

◆ step()

void AccelStepper::step ( long  step)
protectedvirtual

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(), step4() or step8() depending on the number of pins defined for the stepper.

Parameters
[in]stepThe current step phase number (0 to 7)

Definition at line 319 of file AccelStepper.cpp.

Referenced by runSpeed().

320 {
321  switch (_interface)
322  {
323  case FUNCTION:
324  step0(step);
325  break;
326 
327  case DRIVER:
328  step1(step);
329  break;
330 
331  case FULL2WIRE:
332  step2(step);
333  break;
334 
335  case FULL3WIRE:
336  step3(step);
337  break;
338 
339  case FULL4WIRE:
340  step4(step);
341  break;
342 
343  case HALF3WIRE:
344  step6(step);
345  break;
346 
347  case HALF4WIRE:
348  step8(step);
349  break;
350  }
351 }
4 wire full stepper, 4 motor pins required
Definition: AccelStepper.h:322
2 wire stepper, 2 motor pins required
Definition: AccelStepper.h:320
virtual void step1(long step)
Called to execute a step on a stepper driver (ie where pins == 1). Only called when a new step is req...
virtual void step4(long step)
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.
virtual void step2(long step)
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.
3 wire half stepper, such as HDD spindle, 3 motor pins required
Definition: AccelStepper.h:323
4 wire half stepper, 4 motor pins required
Definition: AccelStepper.h:324
virtual void step0(long step)
Called to execute a step using stepper functions (pins = 0) Only called when a new step is required...
3 wire stepper, such as HDD spindle, 3 motor pins required
Definition: AccelStepper.h:321
Stepper Driver, 2 driver pins required.
Definition: AccelStepper.h:319
uint8_t _interface
Number of pins on the stepper motor. Permits 2 or 4. 2 pins is a bipolar, and 4 pins is a unipolar...
Definition: AccelStepper.h:605
virtual void step8(long step)
Called to execute a step on a 4 pin half-steper motor. Only called when a new step is required...
Use the functional interface, implementing your own driver functions (internal use only) ...
Definition: AccelStepper.h:318
virtual void step(long step)
Called to execute a step. Only called when a new step is required. Subclasses may override to impleme...
virtual void step3(long step)
Called to execute a step on a 3 pin motor, such as HDD spindle. Only called when a new step is requir...
virtual void step6(long step)
Called to execute a step on a 3 pin motor, such as HDD spindle. Only called when a new step is requir...
Here is the caller graph for this function:

◆ step0()

void AccelStepper::step0 ( long  step)
protectedvirtual

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.

Parameters
[in]stepThe current step phase number (0 to 7)

Definition at line 370 of file AccelStepper.cpp.

References _backward, _forward, and _speed.

371 {
372  (void)(step); // Unused
373  if (_speed > 0)
374  _forward();
375  else
376  _backward();
377 }
void(* _backward)()
The pointer to a backward-step procedure.
Definition: AccelStepper.h:660
void(* _forward)()
The pointer to a forward-step procedure.
Definition: AccelStepper.h:657
float _speed
The current motos speed in steps per second Positive is clockwise.
Definition: AccelStepper.h:624
virtual void step(long step)
Called to execute a step. Only called when a new step is required. Subclasses may override to impleme...

◆ step1()

void AccelStepper::step1 ( long  step)
protectedvirtual

Called to execute a step on a stepper driver (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.

Parameters
[in]stepThe current step phase number (0 to 7)

Definition at line 382 of file AccelStepper.cpp.

383 {
384  (void)(step); // Unused
385 
386  // _pin[0] is step, _pin[1] is direction
387  setOutputPins(_direction ? 0b10 : 0b00); // Set direction first else get rogue pulses
388  setOutputPins(_direction ? 0b11 : 0b01); // step HIGH
389  // Caution 200ns setup time
390  // Delay the minimum allowed pulse width
392  setOutputPins(_direction ? 0b10 : 0b00); // step LOW
393 }
unsigned int _minPulseWidth
The minimum allowed pulse width in microseconds.
Definition: AccelStepper.h:642
#define delayMicroseconds(us)
Definition: io.h:94
boolean _direction
Current direction motor is spinning in Protected because some peoples subclasses need it to be so...
Definition: AccelStepper.h:600
virtual void setOutputPins(uint8_t mask)
Low level function to set the motor output pins bit 0 of the mask corresponds to _pin[0] bit 1 of the...
virtual void step(long step)
Called to execute a step. Only called when a new step is required. Subclasses may override to impleme...

◆ step2()

void AccelStepper::step2 ( long  step)
protectedvirtual

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.

Parameters
[in]stepThe current step phase number (0 to 7)

Definition at line 399 of file AccelStepper.cpp.

400 {
401  switch (step & 0x3)
402  {
403  case 0: /* 01 */
404  setOutputPins(0b10);
405  break;
406 
407  case 1: /* 11 */
408  setOutputPins(0b11);
409  break;
410 
411  case 2: /* 10 */
412  setOutputPins(0b01);
413  break;
414 
415  case 3: /* 00 */
416  setOutputPins(0b00);
417  break;
418  }
419 }
virtual void setOutputPins(uint8_t mask)
Low level function to set the motor output pins bit 0 of the mask corresponds to _pin[0] bit 1 of the...
virtual void step(long step)
Called to execute a step. Only called when a new step is required. Subclasses may override to impleme...

◆ step3()

void AccelStepper::step3 ( long  step)
protectedvirtual

Called to execute a step on a 3 pin motor, such as HDD spindle. 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.

Parameters
[in]stepThe current step phase number (0 to 7)

Definition at line 423 of file AccelStepper.cpp.

424 {
425  switch (step % 3)
426  {
427  case 0: // 100
428  setOutputPins(0b100);
429  break;
430 
431  case 1: // 001
432  setOutputPins(0b001);
433  break;
434 
435  case 2: //010
436  setOutputPins(0b010);
437  break;
438 
439  }
440 }
virtual void setOutputPins(uint8_t mask)
Low level function to set the motor output pins bit 0 of the mask corresponds to _pin[0] bit 1 of the...
virtual void step(long step)
Called to execute a step. Only called when a new step is required. Subclasses may override to impleme...

◆ step4()

void AccelStepper::step4 ( long  step)
protectedvirtual

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.

Parameters
[in]stepThe current step phase number (0 to 7)

Definition at line 445 of file AccelStepper.cpp.

446 {
447  switch (step & 0x3)
448  {
449  case 0: // 1010
450  setOutputPins(0b0101);
451  break;
452 
453  case 1: // 0110
454  setOutputPins(0b0110);
455  break;
456 
457  case 2: //0101
458  setOutputPins(0b1010);
459  break;
460 
461  case 3: //1001
462  setOutputPins(0b1001);
463  break;
464  }
465 }
virtual void setOutputPins(uint8_t mask)
Low level function to set the motor output pins bit 0 of the mask corresponds to _pin[0] bit 1 of the...
virtual void step(long step)
Called to execute a step. Only called when a new step is required. Subclasses may override to impleme...

◆ step6()

void AccelStepper::step6 ( long  step)
protectedvirtual

Called to execute a step on a 3 pin motor, such as HDD spindle. 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.

Parameters
[in]stepThe current step phase number (0 to 7)

Definition at line 470 of file AccelStepper.cpp.

471 {
472  switch (step % 6)
473  {
474  case 0: // 100
475  setOutputPins(0b100);
476  break;
477 
478  case 1: // 101
479  setOutputPins(0b101);
480  break;
481 
482  case 2: // 001
483  setOutputPins(0b001);
484  break;
485 
486  case 3: // 011
487  setOutputPins(0b011);
488  break;
489 
490  case 4: // 010
491  setOutputPins(0b010);
492  break;
493 
494  case 5: // 011
495  setOutputPins(0b110);
496  break;
497 
498  }
499 }
virtual void setOutputPins(uint8_t mask)
Low level function to set the motor output pins bit 0 of the mask corresponds to _pin[0] bit 1 of the...
virtual void step(long step)
Called to execute a step. Only called when a new step is required. Subclasses may override to impleme...

◆ step8()

void AccelStepper::step8 ( long  step)
protectedvirtual

Called to execute a step on a 4 pin half-steper 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.

Parameters
[in]stepThe current step phase number (0 to 7)

Definition at line 504 of file AccelStepper.cpp.

505 {
506  switch (step & 0x7)
507  {
508  case 0: // 1000
509  setOutputPins(0b0001);
510  break;
511 
512  case 1: // 1010
513  setOutputPins(0b0101);
514  break;
515 
516  case 2: // 0010
517  setOutputPins(0b0100);
518  break;
519 
520  case 3: // 0110
521  setOutputPins(0b0110);
522  break;
523 
524  case 4: // 0100
525  setOutputPins(0b0010);
526  break;
527 
528  case 5: //0101
529  setOutputPins(0b1010);
530  break;
531 
532  case 6: // 0001
533  setOutputPins(0b1000);
534  break;
535 
536  case 7: //1001
537  setOutputPins(0b1001);
538  break;
539  }
540 }
virtual void setOutputPins(uint8_t mask)
Low level function to set the motor output pins bit 0 of the mask corresponds to _pin[0] bit 1 of the...
virtual void step(long step)
Called to execute a step. Only called when a new step is required. Subclasses may override to impleme...

◆ stop()

void AccelStepper::stop ( )

Sets a new target position that causes the stepper to stop as quickly as possible, using the current speed and acceleration parameters.

Definition at line 637 of file AccelStepper.cpp.

References _acceleration, _speed, and move().

638 {
639  if (_speed != 0.0)
640  {
641  long stepsToStop = (long)((_speed * _speed) / (2.0 * _acceleration)) + 1; // Equation 16 (+integer rounding)
642  if (_speed > 0)
643  move(stepsToStop);
644  else
645  move(-stepsToStop);
646  }
647 }
float _speed
The current motos speed in steps per second Positive is clockwise.
Definition: AccelStepper.h:624
void move(long relative)
Set the target position relative to the current position.
float _acceleration
The acceleration to use to accelerate or decelerate the motor in steps per second per second...
Definition: AccelStepper.h:631
Here is the call graph for this function:

◆ targetPosition()

long AccelStepper::targetPosition ( )

The most recently set target position.

Returns
the target position in steps. Positive is clockwise from the 0 position.

Definition at line 77 of file AccelStepper.cpp.

References _targetPos.

78 {
79  return _targetPos;
80 }
long _targetPos
The target position in steps. The AccelStepper library will move the motor from the _currentPos to th...
Definition: AccelStepper.h:620

Field Documentation

◆ _acceleration

float AccelStepper::_acceleration
private

The acceleration to use to accelerate or decelerate the motor in steps per second per second. Must be > 0.

Definition at line 631 of file AccelStepper.h.

Referenced by AccelStepper(), computeNewSpeed(), setAcceleration(), setMaxSpeed(), and stop().

◆ _backward

void(* AccelStepper::_backward) ()
private

The pointer to a backward-step procedure.

Definition at line 660 of file AccelStepper.h.

Referenced by AccelStepper(), and step0().

◆ _c0

float AccelStepper::_c0
private

Initial step size in microseconds.

Definition at line 666 of file AccelStepper.h.

Referenced by AccelStepper(), and computeNewSpeed().

◆ _cmin

float AccelStepper::_cmin
private

Min step size in microseconds based on maxSpeed.

Definition at line 672 of file AccelStepper.h.

Referenced by AccelStepper(), and setMaxSpeed().

◆ _cn

float AccelStepper::_cn
private

Last step size in microseconds.

Definition at line 669 of file AccelStepper.h.

Referenced by AccelStepper(), and computeNewSpeed().

◆ _currentPos

long AccelStepper::_currentPos
private

The current absolution position in steps.

Definition at line 615 of file AccelStepper.h.

Referenced by AccelStepper(), currentPosition(), distanceToGo(), isRunning(), move(), runSpeed(), runSpeedToPosition(), and setCurrentPosition().

◆ _direction

boolean AccelStepper::_direction
protected

Current direction motor is spinning in Protected because some peoples subclasses need it to be so.

Definition at line 600 of file AccelStepper.h.

◆ _enableInverted

bool AccelStepper::_enableInverted
private

Is the direction pin inverted? bool _dirInverted; /// Moved to _pinInverted[1].

Is the step pin inverted? bool _stepInverted; /// Moved to _pinInverted[0] Is the enable pin inverted?

Definition at line 651 of file AccelStepper.h.

Referenced by AccelStepper(), and setPinsInverted().

◆ _enablePin

uint8_t AccelStepper::_enablePin
private

Enable pin for stepper driver, or 0xFF if unused.

Definition at line 654 of file AccelStepper.h.

◆ _forward

void(* AccelStepper::_forward) ()
private

The pointer to a forward-step procedure.

Definition at line 657 of file AccelStepper.h.

Referenced by AccelStepper(), and step0().

◆ _interface

uint8_t AccelStepper::_interface
private

Number of pins on the stepper motor. Permits 2 or 4. 2 pins is a bipolar, and 4 pins is a unipolar.

Definition at line 605 of file AccelStepper.h.

◆ _lastStepTime

unsigned long AccelStepper::_lastStepTime
private

The last step time in microseconds.

Definition at line 639 of file AccelStepper.h.

Referenced by AccelStepper(), and runSpeed().

◆ _maxSpeed

float AccelStepper::_maxSpeed
private

The maximum permitted speed in steps per second. Must be > 0.

Definition at line 627 of file AccelStepper.h.

Referenced by AccelStepper(), maxSpeed(), and setMaxSpeed().

◆ _minPulseWidth

unsigned int AccelStepper::_minPulseWidth
private

The minimum allowed pulse width in microseconds.

Definition at line 642 of file AccelStepper.h.

Referenced by AccelStepper(), and setMinPulseWidth().

◆ _n

long AccelStepper::_n
private

The step counter for speed calculations.

Definition at line 663 of file AccelStepper.h.

Referenced by AccelStepper(), computeNewSpeed(), setAcceleration(), setCurrentPosition(), and setMaxSpeed().

◆ _pin

uint8_t AccelStepper::_pin[4]
private

Arduino pin number assignments for the 2 or 4 pins required to interface to the stepper motor or driver.

Definition at line 609 of file AccelStepper.h.

◆ _pinInverted

uint8_t AccelStepper::_pinInverted[4]
private

Whether the _pins is inverted or not.

Definition at line 612 of file AccelStepper.h.

◆ _speed

float AccelStepper::_speed
private

The current motos speed in steps per second Positive is clockwise.

Definition at line 624 of file AccelStepper.h.

Referenced by AccelStepper(), computeNewSpeed(), isRunning(), run(), setCurrentPosition(), setMaxSpeed(), setSpeed(), speed(), step0(), and stop().

◆ _sqrt_twoa

float AccelStepper::_sqrt_twoa
private

Definition at line 632 of file AccelStepper.h.

Referenced by AccelStepper().

◆ _stepInterval

unsigned long AccelStepper::_stepInterval
private

The current interval between steps in microseconds. 0 means the motor is currently stopped with _speed == 0.

Definition at line 636 of file AccelStepper.h.

Referenced by AccelStepper(), computeNewSpeed(), runSpeed(), setCurrentPosition(), and setSpeed().

◆ _targetPos

long AccelStepper::_targetPos
private

The target position in steps. The AccelStepper library will move the motor from the _currentPos to the _targetPos, taking into account the max speed, acceleration and deceleration.

Definition at line 620 of file AccelStepper.h.

Referenced by AccelStepper(), distanceToGo(), isRunning(), moveTo(), runSpeedToPosition(), setCurrentPosition(), and targetPosition().


The documentation for this class was generated from the following files: