- Main Page
- Classes
- Files

# AccelStepper.h

    00001 // AccelStepper.h
    00002 //
    00003 /// \mainpage AccelStepper library for Arduino
    00004 ///
    00005 /// This is the Arduino AccelStepper 1.2 library.
    00006 /// It provides an object-oriented interface for 2 or 4 pin stepper motors.
    00007 ///
    00008 /// The standard Arduino IDE includes the Stepper library
    00009 /// (http://arduino.cc/en/Reference/Stepper) for stepper motors. It is
    00010 /// perfectly adequate for simple, single motor applications.
    00011 ///
    00012 /// AccelStepper significantly improves on the standard Arduino Stepper library in several ways:
    00013 /// \li Supports acceleration and deceleration
    00014 /// \li Supports multiple simultaneous steppers, with independent concurrent stepping on each stepper
    00015 /// \li API functions never delay() or block
    00016 /// \li Supports 2 and 4 wire steppers
    00017 /// \li Supports stepper drivers such as the Sparkfun EasyDriver (based on 3967 driver chip)
    00018 /// \li Very slow speeds are supported
    00019 /// \li Extensive API
    00020 /// \li Subclass support
    00021 ///
    00022 /// The latest version of this documentation  can be downloaded from
    00023 /// http://www.open.com.au/mikem/arduino/AccelStepper
    00024 ///
    00025 /// Example Arduino programs are included to show the main modes of use.
    00026 ///
    00027 /// The version of the package that this documentation refers to can be downloaded
    00028 /// from http://www.open.com.au/mikem/arduino/AccelStepper/AccelStepper-1.3.zip
    00029 /// You can find the latest version at http://www.open.com.au/mikem/arduino/AccelStepper
    00030 ///
    00031 /// Tested on Arduino Diecimila and Mega with arduino-0018 on OpenSuSE 11.1 and avr-libc-1.6.1-1.15,
    00032 /// cross-avr-binutils-2.19-9.1, cross-avr-gcc-4.1.3_20080612-26.5.
    00033 ///
    00034 /// \par Installation
    00035 /// Install in the usual way: unzip the distribution zip file to the libraries
    00036 /// sub-folder of your sketchbook.
    00037 ///
    00038 /// This software is Copyright (C) 2010 Mike McCauley. Use is subject to license
    00039 /// conditions. The main licensing options available are GPL V2 or Commercial:
    00040 ///
    00041 /// \par Open Source Licensing GPL V2
    00042 /// This is the appropriate option if you want to share the source code of your
    00043 /// application with everyone you distribute it to, and you also want to give them
    00044 /// the right to share who uses it. If you wish to use this software under Open
    00045 /// Source Licensing, you must contribute all your source code to the open source
    00046 /// community in accordance with the GPL Version 2 when your application is
    00047 /// distributed. See http://www.gnu.org/copyleft/gpl.html
    00048 ///
    00049 /// \par Commercial Licensing
    00050 /// This is the appropriate option if you are creating proprietary applications
    00051 /// and you are not prepared to distribute and share the source code of your
    00052 /// application. Contact info@open.com.au for details.
    00053 ///
    00054 /// \par Revision History
    00055 /// \version 1.0 Initial release
    00056 ///
    00057 /// \version 1.1 Added speed() function to get the current speed.
    00058 /// \version 1.2 Added runSpeedToPosition() submitted by Gunnar Arndt.
    00059 /// \version 1.3 Added support for stepper drivers (ie with Step and Direction inputs) with _pins == 1
    00060 ///
    00061 ///
    00062 /// \author  Mike McCauley (mikem@open.com.au)
    00063 // Copyright (C) 2009 Mike McCauley
    00064 // $Id: AccelStepper.h,v 1.2 2010/10/24 07:46:18 mikem Exp mikem $
    00065
    00066 #ifndef AccelStepper_h
    00067 #define AccelStepper_h
    00068
    00069 #include <stdlib.h>
    00070 #include <wiring.h>
    00071
    00072 // These defs cause trouble on some versions of Arduino
    00073 #undef round
    00074
    00075 /////////////////////////////////////////////////////////////////////
    00076 /// \class AccelStepper AccelStepper.h <AccelStepper.h>
    00077 /// \brief Support for stepper motors with acceleration etc.
    00078 ///
    00079 /// This defines a single 2 or 4 pin stepper motor, or stepper moter with fdriver chip, with optional
    00080 /// acceleration, deceleration, absolute positioning commands etc. Multiple
    00081 /// simultaneous steppers are supported, all moving
    00082 /// at different speeds and accelerations.
    00083 ///
    00084 /// \par Operation
    00085 /// This module operates by computing a step time in milliseconds. The step
    00086 /// time is recomputed after each step and after speed and acceleration
    00087 /// parameters are changed by the caller. The time of each step is recorded in
    00088 /// milliseconds. The run() function steps the motor if a new step is due.
    00089 /// The run() function must be called frequently until the motor is in the
    00090 /// desired position, after which time run() will do nothing.
    00091 ///
    00092 /// \par Positioning
    00093 /// Positions are specified by a signed long integer. At
    00094 /// construction time, the current position of the motor is consider to be 0. Positive
    00095 /// positions are clockwise from the initial position; negative positions are
    00096 /// anticlockwise. The curent position can be altered for instance after
    00097 /// initialization positioning.
    00098 ///
    00099 /// \par Caveats
    00100 /// This is an open loop controller: If the motor stalls or is oversped,
    00101 /// AccelStepper will not have a correct
    00102 /// idea of where the motor really is (since there is no feedback of the motor's
    00103 /// real position. We only know where we _think_ it is, relative to the
    00104 /// initial starting point).
    00105 ///
    00106 /// The fastest motor speed that can be reliably supported is 1000 steps per
    00107 /// second (1 step every millisecond). However any speed less than that down
    00108 /// to very slow speeds (much less than one per second) are supported,
    00109 /// provided the run() function is called frequently enough to step the
    00110 /// motor whenever required.
    00111 class AccelStepper
    00112 {
    00113 public:
    00114     /// Constructor. You can have multiple simultaneous steppers, all moving
    00115     /// at different speeds and accelerations, provided you call their run()
    00116     /// functions at frequent enough intervals. Current Position is set to 0, target
    00117     /// position is set to 0. MaxSpeed and Acceleration default to 1.0.
    00118     /// The motor pins will be initialised to OUTPUT mode during the
    00119     /// constructor by a call to enableOutputs().
    00120     /// \param[in] pins Number of pins to interface to. 1, 2 or 4 are
    00121     /// supported. 1 means a stepper driver (with Step and Direction pins)
    00122     /// 2 means a 2 wire stepper. 4 means a 4 wire stepper.
    00123     /// Defaults to 4 pins.
    00124     /// \param[in] pin1 Arduino digital pin number for motor pin 1. Defaults
    00125     /// to pin 2. For a driver (pins==1), this is the Step input to the driver. Low to high transition means to step)
    00126     /// \param[in] pin2 Arduino digital pin number for motor pin 2. Defaults
    00127     /// to pin 3. For a driver (pins==1), this is the Direction input the driver. High means forward.
    00128     /// \param[in] pin3 Arduino digital pin number for motor pin 3. Defaults
    00129     /// to pin 4.
    00130     /// \param[in] pin4 Arduino digital pin number for motor pin 4. Defaults
    00131     /// to pin 5.
    00132     AccelStepper(uint8_t pins = 4, uint8_t pin1 = 2, uint8_t pin2 = 3, uint8_t pin3 = 4, uint8_t pin4 = 5);
    00133
    00134     /// Set the target position. The run() function will try to move the motor
    00135     /// from the current position to the target position set by the most
    00136     /// recent call to this function.
    00137     /// \param[in] absolute The desired absolute position. Negative is
    00138     /// anticlockwise from the 0 position.
    00139     void    moveTo(long absolute);
    00140
    00141     /// Set the target position relative to the current position
    00142     /// \param[in] relative The desired position relative to the current position. Negative is
    00143     /// anticlockwise from the current position.
    00144     void    move(long relative);
    00145
    00146     /// Poll the motor and step it if a step is due, implementing
    00147     /// accelerations and decelerations to achive the ratget position. You must call this as
    00148     /// fequently as possible, but at least once per minimum step interval,
    00149     /// preferably in your main loop.
    00150     /// \return true if the motor is at the target position.
    00151     boolean run();
    00152
    00153     /// Poll the motor and step it if a step is due, implmenting a constant
    00154     /// speed as set by the most recent call to setSpeed().
    00155     /// \return true if the motor was stepped.
    00156     boolean runSpeed();
    00157
    00158     /// Sets the maximum permitted speed. the run() function will accelerate
    00159     /// up to the speed set by this function.
    00160     /// \param[in] speed The desired maximum speed in steps per second. Must
    00161     /// be > 0. Speeds of more than 1000 steps per second are unreliable.
    00162     void    setMaxSpeed(float speed);
    00163
    00164     /// Sets the acceleration and deceleration parameter.
    00165     /// \param[in] acceleration The desired acceleration in steps per second
    00166     /// per second. Must be > 0.
    00167     void    setAcceleration(float acceleration);
    00168
    00169     /// Sets the desired constant speed for use with runSpeed().
    00170     /// \param[in] speed The desired constant speed in steps per
    00171     /// second. Positive is clockwise. Speeds of more than 1000 steps per
    00172     /// second are unreliable. Very slow speeds may be set (eg 0.00027777 for
    00173     /// once per hour, approximately. Speed accuracy depends on the Arduino
    00174     /// crystal. Jitter depends on how frequently you call the runSpeed() function.
    00175     void    setSpeed(float speed);
    00176
    00177     /// The most recently set speed
    00178     /// \return the most recent speed in steps per second
    00179     float   speed();
    00180
    00181     /// The distance from the current position to the target position.
    00182     /// \return the distance from the current position to the target position
    00183     /// in steps. Positive is clockwise from the current position.
    00184     long    distanceToGo();
    00185
    00186     /// The most recently set target position.
    00187     /// \return the target position
    00188     /// in steps. Positive is clockwise from the 0 position.
    00189     long    targetPosition();
    00190
    00191
    00192     /// The currently motor position.
    00193     /// \return the current motor position
    00194     /// in steps. Positive is clockwise from the 0 position.
    00195     long    currentPosition();
    00196
    00197     /// Resets the current position of the motor, so that wherever the mottor
    00198     /// happens to be right now is considered to be the new position. Useful
    00199     /// for setting a zero position on a stepper after an initial hardware
    00200     /// positioning move.
    00201     /// \param[in] position The position in steps of wherever the motor
    00202     /// happens to be right now.
    00203     void    setCurrentPosition(long position);
    00204
    00205     /// Moves the motor to the target position and blocks until it is at
    00206     /// position. Dont use this in event loops, since it blocks.
    00207     void    runToPosition();
    00208
    00209     /// Runs at the currently selected speed until the target position is reached
    00210     /// Does not implement accelerations.
    00211     boolean runSpeedToPosition();
    00212
    00213     /// Moves the motor to the new target position and blocks until it is at
    00214     /// position. Dont use this in event loops, since it blocks.
    00215     /// \param[in] position The new target position.
    00216     void    runToNewPosition(long position);
    00217
    00218     /// Disable motor pin outputs by setting them all LOW
    00219     /// Depending on the design of your electronics this may turn off
    00220     /// the power to the motor coils, saving power.
    00221     /// This is useful to support Arduino low power modes: disable the outputs
    00222     /// during sleep and then reenable with enableOutputs() before stepping
    00223     /// again.
    00224     void    disableOutputs();
    00225
    00226     /// Enable motor pin outputs by setting the motor pins to OUTPUT
    00227     /// mode. Called automatically by the constructor.
    00228     void    enableOutputs();
    00229
    00230 protected:
    00231
    00232     /// Forces the library to compute a new instantaneous speed and set that as
    00233     /// the current speed. Calls
    00234     /// desiredSpeed(), which can be overridden by subclasses. It is called by
    00235     /// the library:
    00236     /// \li  after each step
    00237     /// \li  after change to maxSpeed through setMaxSpeed()
    00238     /// \li  after change to acceleration through setAcceleration()
    00239     /// \li  after change to target position (relative or absolute) through
    00240     /// move() or moveTo()
    00241     void           computeNewSpeed();
    00242
    00243     /// Called to execute a step. Only called when a new step is
    00244     /// required. Subclasses may override to implement new stepping
    00245     /// interfaces. The default calls step1(), step2() or step4() depending on the
    00246     /// number of pins defined for the stepper.
    00247     /// \param[in] step The current step phase number (0 to 3)
    00248     virtual void   step(uint8_t step);
    00249
    00250
    00251     /// Called to execute a step on a stepper drover (ie where pins == 1). Only called when a new step is
    00252     /// required. Subclasses may override to implement new stepping
    00253     /// interfaces. The default sets or clears the outputs of Step pin1 to step,
    00254     /// and sets the output of _pin2 to the desired direction. The Step pin (_pin1) is pulsed for 1 microsecond
    00255     /// which is the minimum STEP pulse width for the 3967 driver.
    00256     /// \param[in] step The current step phase number (0 to 3)
    00257     virtual void   step1(uint8_t step);
    00258
    00259     /// Called to execute a step on a 2 pin motor. Only called when a new step is
    00260     /// required. Subclasses may override to implement new stepping
    00261     /// interfaces. The default sets or clears the outputs of pin1 and pin2
    00262     /// \param[in] step The current step phase number (0 to 3)
    00263     virtual void   step2(uint8_t step);
    00264
    00265     /// Called to execute a step on a 4 pin motor. Only called when a new step is
    00266     /// required. Subclasses may override to implement new stepping
    00267     /// interfaces. The default sets or clears the outputs of pin1, pin2,
    00268     /// pin3, pin4.
    00269     /// \param[in] step The current step phase number (0 to 3)
    00270     virtual void   step4(uint8_t step);
    00271
    00272     /// Compute and return the desired speed. The default algorithm uses
    00273     /// maxSpeed, acceleration and the current speed to set a new speed to
    00274     /// move the motor from teh current position to the target
    00275     /// position. Subclasses may override this to provide an alternate
    00276     /// algorithm (but do not block). Called by computeNewSpeed whenever a new speed neds to be
    00277     /// computed.
    00278     virtual float  desiredSpeed();
    00279
    00280 private:
    00281     /// Number of pins on the stepper motor. Permits 2 or 4. 2 pins is a
    00282     /// bipolar, and 4 pins is a unipolar.
    00283     uint8_t        _pins;          // 2 or 4
    00284
    00285     /// Arduino pin number for the 2 or 4 pins required to interface to the
    00286     /// stepper motor.
    00287     uint8_t        _pin1, _pin2, _pin3, _pin4;
    00288
    00289     /// The current absolution position in steps.
    00290     long           _currentPos;    // Steps
    00291
    00292     /// The target position in steps. The AccelStepper library will move the
    00293     /// motor from teh _currentPos to the _targetPos, taking into account the
    00294     /// max speed, acceleration and deceleration
    00295     long           _targetPos;     // Steps
    00296
    00297     /// The current motos speed in steps per second
    00298     /// Positive is clockwise
    00299     float          _speed;         // Steps per second
    00300
    00301     /// The maximum permitted speed in steps per second. Must be > 0.
    00302     float          _maxSpeed;
    00303
    00304     /// The acceleration to use to accelerate or decelerate the motor in steps
    00305     /// per second per second. Must be > 0
    00306     float          _acceleration;
    00307
    00308     /// The current interval between steps in milliseconds.
    00309     unsigned long  _stepInterval;
    00310
    00311     /// The last step time in milliseconds
    00312     unsigned long  _lastStepTime;
    00313 };
    00314
    00315 #endif