Библиотека для работы с шаговым двигателем

Dependents:   Delta_Robot_2018 Speed_Control

Committer:
yuliyasm
Date:
Mon May 07 11:39:53 2018 +0000
Revision:
0:deef8c36d17a
4

Who changed what in which revision?

UserRevisionLine numberNew contents of line
yuliyasm 0:deef8c36d17a 1 // AccelStepper.h
yuliyasm 0:deef8c36d17a 2 //
yuliyasm 0:deef8c36d17a 3 /// \mainpage AccelStepper library for MBED
yuliyasm 0:deef8c36d17a 4 ///
yuliyasm 0:deef8c36d17a 5 /// This is the MBED AccelStepper library.
yuliyasm 0:deef8c36d17a 6 /// It provides an object-oriented interface for 2, 3 or 4 pin stepper motors.
yuliyasm 0:deef8c36d17a 7 /// Based on the Arduino AccelStepper library by Airspayce.com
yuliyasm 0:deef8c36d17a 8 /// Translated for MBED by Jaap Vermaas <jaap@tuxic.nl>, 03-2015
yuliyasm 0:deef8c36d17a 9 ///
yuliyasm 0:deef8c36d17a 10 /// The standard Arduino IDE includes the Stepper library
yuliyasm 0:deef8c36d17a 11 /// (http://arduino.cc/en/Reference/Stepper) for stepper motors. It is
yuliyasm 0:deef8c36d17a 12 /// perfectly adequate for simple, single motor applications.
yuliyasm 0:deef8c36d17a 13 ///
yuliyasm 0:deef8c36d17a 14 /// AccelStepper significantly improves on the standard Arduino Stepper library in several ways:
yuliyasm 0:deef8c36d17a 15 /// \li Supports acceleration and deceleration
yuliyasm 0:deef8c36d17a 16 /// \li Supports multiple simultaneous steppers, with independent concurrent stepping on each stepper
yuliyasm 0:deef8c36d17a 17 /// \li API functions never delay() or block
yuliyasm 0:deef8c36d17a 18 /// \li Supports 2, 3 and 4 wire steppers, plus 3 and 4 wire half steppers.
yuliyasm 0:deef8c36d17a 19 /// \li Supports alternate stepping functions to enable support of AFMotor (https://github.com/adafruit/Adafruit-Motor-Shield-library)
yuliyasm 0:deef8c36d17a 20 /// \li Supports stepper drivers such as the Sparkfun EasyDriver (based on 3967 driver chip)
yuliyasm 0:deef8c36d17a 21 /// \li Very slow speeds are supported
yuliyasm 0:deef8c36d17a 22 /// \li Extensive API
yuliyasm 0:deef8c36d17a 23 /// \li Subclass support
yuliyasm 0:deef8c36d17a 24 ///
yuliyasm 0:deef8c36d17a 25 /// The latest version of this documentation can be downloaded from
yuliyasm 0:deef8c36d17a 26 /// http://www.airspayce.com/mikem/arduino/AccelStepper
yuliyasm 0:deef8c36d17a 27 /// The version of the package that this documentation refers to can be downloaded
yuliyasm 0:deef8c36d17a 28 /// from http://www.airspayce.com/mikem/arduino/AccelStepper/AccelStepper-1.47.zip
yuliyasm 0:deef8c36d17a 29 ///
yuliyasm 0:deef8c36d17a 30 /// Example Arduino programs are included to show the main modes of use.
yuliyasm 0:deef8c36d17a 31 ///
yuliyasm 0:deef8c36d17a 32 /// You can also find online help and discussion at http://groups.google.com/group/accelstepper
yuliyasm 0:deef8c36d17a 33 /// Please use that group for all questions and discussions on this topic.
yuliyasm 0:deef8c36d17a 34 /// Do not contact the author directly, unless it is to discuss commercial licensing.
yuliyasm 0:deef8c36d17a 35 /// Before asking a question or reporting a bug, please read http://www.catb.org/esr/faqs/smart-questions.html
yuliyasm 0:deef8c36d17a 36 ///
yuliyasm 0:deef8c36d17a 37 /// Tested on Arduino Diecimila and Mega with arduino-0018 & arduino-0021
yuliyasm 0:deef8c36d17a 38 /// on OpenSuSE 11.1 and avr-libc-1.6.1-1.15,
yuliyasm 0:deef8c36d17a 39 /// cross-avr-binutils-2.19-9.1, cross-avr-gcc-4.1.3_20080612-26.5.
yuliyasm 0:deef8c36d17a 40 /// Tested on Teensy http://www.pjrc.com/teensy including Teensy 3.1 built using Arduino IDE 1.0.5 with
yuliyasm 0:deef8c36d17a 41 /// teensyduino addon 1.18 and later.
yuliyasm 0:deef8c36d17a 42 ///
yuliyasm 0:deef8c36d17a 43 /// \par Installation
yuliyasm 0:deef8c36d17a 44 ///
yuliyasm 0:deef8c36d17a 45 /// Install in the usual way: unzip the distribution zip file to the libraries
yuliyasm 0:deef8c36d17a 46 /// sub-folder of your sketchbook.
yuliyasm 0:deef8c36d17a 47 ///
yuliyasm 0:deef8c36d17a 48 /// \par Theory
yuliyasm 0:deef8c36d17a 49 ///
yuliyasm 0:deef8c36d17a 50 /// This code uses speed calculations as described in
yuliyasm 0:deef8c36d17a 51 /// "Generate stepper-motor speed profiles in real time" by David Austin
yuliyasm 0:deef8c36d17a 52 /// http://fab.cba.mit.edu/classes/MIT/961.09/projects/i0/Stepper_Motor_Speed_Profile.pdf
yuliyasm 0:deef8c36d17a 53 /// with the exception that AccelStepper uses steps per second rather than radians per second
yuliyasm 0:deef8c36d17a 54 /// (because we dont know the step angle of the motor)
yuliyasm 0:deef8c36d17a 55 /// An initial step interval is calculated for the first step, based on the desired acceleration
yuliyasm 0:deef8c36d17a 56 /// On subsequent steps, shorter step intervals are calculated based
yuliyasm 0:deef8c36d17a 57 /// on the previous step until max speed is achieved.
yuliyasm 0:deef8c36d17a 58 ///
yuliyasm 0:deef8c36d17a 59 /// \par Donations
yuliyasm 0:deef8c36d17a 60 ///
yuliyasm 0:deef8c36d17a 61 /// This library is offered under a free GPL license for those who want to use it that way.
yuliyasm 0:deef8c36d17a 62 /// We try hard to keep it up to date, fix bugs
yuliyasm 0:deef8c36d17a 63 /// and to provide free support. If this library has helped you save time or money, please consider donating at
yuliyasm 0:deef8c36d17a 64 /// http://www.airspayce.com or here:
yuliyasm 0:deef8c36d17a 65 ///
yuliyasm 0:deef8c36d17a 66 /// \htmlonly <form action="https://www.paypal.com/cgi-bin/webscr" method="post"><input type="hidden" name="cmd" value="_donations" /> <input type="hidden" name="business" value="mikem@airspayce.com" /> <input type="hidden" name="lc" value="AU" /> <input type="hidden" name="item_name" value="Airspayce" /> <input type="hidden" name="item_number" value="AccelStepper" /> <input type="hidden" name="currency_code" value="USD" /> <input type="hidden" name="bn" value="PP-DonationsBF:btn_donateCC_LG.gif:NonHosted" /> <input type="image" alt="PayPal — The safer, easier way to pay online." name="submit" src="https://www.paypalobjects.com/en_AU/i/btn/btn_donateCC_LG.gif" /> <img alt="" src="https://www.paypalobjects.com/en_AU/i/scr/pixel.gif" width="1" height="1" border="0" /></form> \endhtmlonly
yuliyasm 0:deef8c36d17a 67 ///
yuliyasm 0:deef8c36d17a 68 /// \par Trademarks
yuliyasm 0:deef8c36d17a 69 ///
yuliyasm 0:deef8c36d17a 70 /// AccelStepper is a trademark of AirSpayce Pty Ltd. The AccelStepper mark was first used on April 26 2010 for
yuliyasm 0:deef8c36d17a 71 /// international trade, and is used only in relation to motor control hardware and software.
yuliyasm 0:deef8c36d17a 72 /// It is not to be confused with any other similar marks covering other goods and services.
yuliyasm 0:deef8c36d17a 73 ///
yuliyasm 0:deef8c36d17a 74 /// \par Copyright
yuliyasm 0:deef8c36d17a 75 ///
yuliyasm 0:deef8c36d17a 76 /// This software is Copyright (C) 2010 Mike McCauley. Use is subject to license
yuliyasm 0:deef8c36d17a 77 /// conditions. The main licensing options available are GPL V2 or Commercial:
yuliyasm 0:deef8c36d17a 78 ///
yuliyasm 0:deef8c36d17a 79 /// \par Open Source Licensing GPL V2
yuliyasm 0:deef8c36d17a 80 /// This is the appropriate option if you want to share the source code of your
yuliyasm 0:deef8c36d17a 81 /// application with everyone you distribute it to, and you also want to give them
yuliyasm 0:deef8c36d17a 82 /// the right to share who uses it. If you wish to use this software under Open
yuliyasm 0:deef8c36d17a 83 /// Source Licensing, you must contribute all your source code to the open source
yuliyasm 0:deef8c36d17a 84 /// community in accordance with the GPL Version 2 when your application is
yuliyasm 0:deef8c36d17a 85 /// distributed. See http://www.gnu.org/copyleft/gpl.html
yuliyasm 0:deef8c36d17a 86 ///
yuliyasm 0:deef8c36d17a 87 /// \par Commercial Licensing
yuliyasm 0:deef8c36d17a 88 /// This is the appropriate option if you are creating proprietary applications
yuliyasm 0:deef8c36d17a 89 /// and you are not prepared to distribute and share the source code of your
yuliyasm 0:deef8c36d17a 90 /// application. Contact info@airspayce.com for details.
yuliyasm 0:deef8c36d17a 91 ///
yuliyasm 0:deef8c36d17a 92 /// \par Revision History
yuliyasm 0:deef8c36d17a 93 /// \version 1.0 Initial release
yuliyasm 0:deef8c36d17a 94 ///
yuliyasm 0:deef8c36d17a 95 /// \version 1.1 Added speed() function to get the current speed.
yuliyasm 0:deef8c36d17a 96 /// \version 1.2 Added runSpeedToPosition() submitted by Gunnar Arndt.
yuliyasm 0:deef8c36d17a 97 /// \version 1.3 Added support for stepper drivers (ie with Step and Direction inputs) with _pins == 1
yuliyasm 0:deef8c36d17a 98 /// \version 1.4 Added functional contructor to support AFMotor, contributed by Limor, with example sketches.
yuliyasm 0:deef8c36d17a 99 /// \version 1.5 Improvements contributed by Peter Mousley: Use of microsecond steps and other speed improvements
yuliyasm 0:deef8c36d17a 100 /// to increase max stepping speed to about 4kHz. New option for user to set the min allowed pulse width.
yuliyasm 0:deef8c36d17a 101 /// Added checks for already running at max speed and skip further calcs if so.
yuliyasm 0:deef8c36d17a 102 /// \version 1.6 Fixed a problem with wrapping of microsecond stepping that could cause stepping to hang.
yuliyasm 0:deef8c36d17a 103 /// Reported by Sandy Noble.
yuliyasm 0:deef8c36d17a 104 /// Removed redundant _lastRunTime member.
yuliyasm 0:deef8c36d17a 105 /// \version 1.7 Fixed a bug where setCurrentPosition() did not always work as expected.
yuliyasm 0:deef8c36d17a 106 /// Reported by Peter Linhart.
yuliyasm 0:deef8c36d17a 107 /// \version 1.8 Added support for 4 pin half-steppers, requested by Harvey Moon
yuliyasm 0:deef8c36d17a 108 /// \version 1.9 setCurrentPosition() now also sets motor speed to 0.
yuliyasm 0:deef8c36d17a 109 /// \version 1.10 Builds on Arduino 1.0
yuliyasm 0:deef8c36d17a 110 /// \version 1.11 Improvments from Michael Ellison:
yuliyasm 0:deef8c36d17a 111 /// Added optional enable line support for stepper drivers
yuliyasm 0:deef8c36d17a 112 /// Added inversion for step/direction/enable lines for stepper drivers
yuliyasm 0:deef8c36d17a 113 /// \version 1.12 Announce Google Group
yuliyasm 0:deef8c36d17a 114 /// \version 1.13 Improvements to speed calculation. Cost of calculation is now less in the worst case,
yuliyasm 0:deef8c36d17a 115 /// and more or less constant in all cases. This should result in slightly beter high speed performance, and
yuliyasm 0:deef8c36d17a 116 /// reduce anomalous speed glitches when other steppers are accelerating.
yuliyasm 0:deef8c36d17a 117 /// However, its hard to see how to replace the sqrt() required at the very first step from 0 speed.
yuliyasm 0:deef8c36d17a 118 /// \version 1.14 Fixed a problem with compiling under arduino 0021 reported by EmbeddedMan
yuliyasm 0:deef8c36d17a 119 /// \version 1.15 Fixed a problem with runSpeedToPosition which did not correctly handle
yuliyasm 0:deef8c36d17a 120 /// running backwards to a smaller target position. Added examples
yuliyasm 0:deef8c36d17a 121 /// \version 1.16 Fixed some cases in the code where abs() was used instead of fabs().
yuliyasm 0:deef8c36d17a 122 /// \version 1.17 Added example ProportionalControl
yuliyasm 0:deef8c36d17a 123 /// \version 1.18 Fixed a problem: If one calls the funcion runSpeed() when Speed is zero, it makes steps
yuliyasm 0:deef8c36d17a 124 /// without counting. reported by Friedrich, Klappenbach.
yuliyasm 0:deef8c36d17a 125 /// \version 1.19 Added MotorInterfaceType and symbolic names for the number of pins to use
yuliyasm 0:deef8c36d17a 126 /// for the motor interface. Updated examples to suit.
yuliyasm 0:deef8c36d17a 127 /// Replaced individual pin assignment variables _pin1, _pin2 etc with array _pin[4].
yuliyasm 0:deef8c36d17a 128 /// _pins member changed to _interface.
yuliyasm 0:deef8c36d17a 129 /// Added _pinInverted array to simplify pin inversion operations.
yuliyasm 0:deef8c36d17a 130 /// Added new function setOutputPins() which sets the motor output pins.
yuliyasm 0:deef8c36d17a 131 /// It can be overridden in order to provide, say, serial output instead of parallel output
yuliyasm 0:deef8c36d17a 132 /// Some refactoring and code size reduction.
yuliyasm 0:deef8c36d17a 133 /// \version 1.20 Improved documentation and examples to show need for correctly
yuliyasm 0:deef8c36d17a 134 /// specifying AccelStepper::FULL4WIRE and friends.
yuliyasm 0:deef8c36d17a 135 /// \version 1.21 Fixed a problem where desiredSpeed could compute the wrong step acceleration
yuliyasm 0:deef8c36d17a 136 /// when _speed was small but non-zero. Reported by Brian Schmalz.
yuliyasm 0:deef8c36d17a 137 /// Precompute sqrt_twoa to improve performance and max possible stepping speed
yuliyasm 0:deef8c36d17a 138 /// \version 1.22 Added Bounce.pde example
yuliyasm 0:deef8c36d17a 139 /// Fixed a problem where calling moveTo(), setMaxSpeed(), setAcceleration() more
yuliyasm 0:deef8c36d17a 140 /// frequently than the step time, even
yuliyasm 0:deef8c36d17a 141 /// with the same values, would interfere with speed calcs. Now a new speed is computed
yuliyasm 0:deef8c36d17a 142 /// only if there was a change in the set value. Reported by Brian Schmalz.
yuliyasm 0:deef8c36d17a 143 /// \version 1.23 Rewrite of the speed algorithms in line with
yuliyasm 0:deef8c36d17a 144 /// http://fab.cba.mit.edu/classes/MIT/961.09/projects/i0/Stepper_Motor_Speed_Profile.pdf
yuliyasm 0:deef8c36d17a 145 /// Now expect smoother and more linear accelerations and decelerations. The desiredSpeed()
yuliyasm 0:deef8c36d17a 146 /// function was removed.
yuliyasm 0:deef8c36d17a 147 /// \version 1.24 Fixed a problem introduced in 1.23: with runToPosition, which did never returned
yuliyasm 0:deef8c36d17a 148 /// \version 1.25 Now ignore attempts to set acceleration to 0.0
yuliyasm 0:deef8c36d17a 149 /// \version 1.26 Fixed a problem where certina combinations of speed and accelration could cause
yuliyasm 0:deef8c36d17a 150 /// oscillation about the target position.
yuliyasm 0:deef8c36d17a 151 /// \version 1.27 Added stop() function to stop as fast as possible with current acceleration parameters.
yuliyasm 0:deef8c36d17a 152 /// Also added new Quickstop example showing its use.
yuliyasm 0:deef8c36d17a 153 /// \version 1.28 Fixed another problem where certain combinations of speed and accelration could cause
yuliyasm 0:deef8c36d17a 154 /// oscillation about the target position.
yuliyasm 0:deef8c36d17a 155 /// Added support for 3 wire full and half steppers such as Hard Disk Drive spindle.
yuliyasm 0:deef8c36d17a 156 /// Contributed by Yuri Ivatchkovitch.
yuliyasm 0:deef8c36d17a 157 /// \version 1.29 Fixed a problem that could cause a DRIVER stepper to continually step
yuliyasm 0:deef8c36d17a 158 /// with some sketches. Reported by Vadim.
yuliyasm 0:deef8c36d17a 159 /// \version 1.30 Fixed a problem that could cause stepper to back up a few steps at the end of
yuliyasm 0:deef8c36d17a 160 /// accelerated travel with certain speeds. Reported and patched by jolo.
yuliyasm 0:deef8c36d17a 161 /// \version 1.31 Updated author and distribution location details to airspayce.com
yuliyasm 0:deef8c36d17a 162 /// \version 1.32 Fixed a problem with enableOutputs() and setEnablePin on Arduino Due that
yuliyasm 0:deef8c36d17a 163 /// prevented the enable pin changing stae correctly. Reported by Duane Bishop.
yuliyasm 0:deef8c36d17a 164 /// \version 1.33 Fixed an error in example AFMotor_ConstantSpeed.pde did not setMaxSpeed();
yuliyasm 0:deef8c36d17a 165 /// Fixed a problem that caused incorrect pin sequencing of FULL3WIRE and HALF3WIRE.
yuliyasm 0:deef8c36d17a 166 /// Unfortunately this meant changing the signature for all step*() functions.
yuliyasm 0:deef8c36d17a 167 /// Added example MotorShield, showing how to use AdaFruit Motor Shield to control
yuliyasm 0:deef8c36d17a 168 /// a 3 phase motor such as a HDD spindle motor (and without using the AFMotor library.
yuliyasm 0:deef8c36d17a 169 /// \version 1.34 Added setPinsInverted(bool pin1Invert, bool pin2Invert, bool pin3Invert, bool pin4Invert, bool enableInvert)
yuliyasm 0:deef8c36d17a 170 /// to allow inversion of 2, 3 and 4 wire stepper pins. Requested by Oleg.
yuliyasm 0:deef8c36d17a 171 /// \version 1.35 Removed default args from setPinsInverted(bool, bool, bool, bool, bool) to prevent ambiguity with
yuliyasm 0:deef8c36d17a 172 /// setPinsInverted(bool, bool, bool). Reported by Mac Mac.
yuliyasm 0:deef8c36d17a 173 /// \version 1.36 Changed enableOutputs() and disableOutputs() to be virtual so can be overridden.
yuliyasm 0:deef8c36d17a 174 /// Added new optional argument 'enable' to constructor, which allows you toi disable the
yuliyasm 0:deef8c36d17a 175 /// automatic enabling of outputs at construction time. Suggested by Guido.
yuliyasm 0:deef8c36d17a 176 /// \version 1.37 Fixed a problem with step1 that could cause a rogue step in the
yuliyasm 0:deef8c36d17a 177 /// wrong direction (or not,
yuliyasm 0:deef8c36d17a 178 /// depending on the setup-time requirements of the connected hardware).
yuliyasm 0:deef8c36d17a 179 /// Reported by Mark Tillotson.
yuliyasm 0:deef8c36d17a 180 /// \version 1.38 run() function incorrectly always returned true. Updated function and doc so it returns true
yuliyasm 0:deef8c36d17a 181 /// if the motor is still running to the target position.
yuliyasm 0:deef8c36d17a 182 /// \version 1.39 Updated typos in keywords.txt, courtesey Jon Magill.
yuliyasm 0:deef8c36d17a 183 /// \version 1.40 Updated documentation, including testing on Teensy 3.1
yuliyasm 0:deef8c36d17a 184 /// \version 1.41 Fixed an error in the acceleration calculations, resulting in acceleration of haldf the intended value
yuliyasm 0:deef8c36d17a 185 /// \version 1.42 Improved support for FULL3WIRE and HALF3WIRE output pins. These changes were in Yuri's original
yuliyasm 0:deef8c36d17a 186 /// contribution but did not make it into production.<br>
yuliyasm 0:deef8c36d17a 187 /// \version 1.43 Added DualMotorShield example. Shows how to use AccelStepper to control 2 x 2 phase steppers using the
yuliyasm 0:deef8c36d17a 188 /// Itead Studio Arduino Dual Stepper Motor Driver Shield model IM120417015.<br>
yuliyasm 0:deef8c36d17a 189 /// \version 1.44 examples/DualMotorShield/DualMotorShield.ino examples/DualMotorShield/DualMotorShield.pde
yuliyasm 0:deef8c36d17a 190 /// was missing from the distribution.<br>
yuliyasm 0:deef8c36d17a 191 /// \version 1.45 Fixed a problem where if setAcceleration was not called, there was no default
yuliyasm 0:deef8c36d17a 192 /// acceleration. Reported by Michael Newman.<br>
yuliyasm 0:deef8c36d17a 193 /// \version 1.45 Fixed inaccuracy in acceleration rate by using Equation 15, suggested by Sebastian Gracki.<br>
yuliyasm 0:deef8c36d17a 194 /// Performance improvements in runSpeed suggested by Jaakko Fagerlund.<br>
yuliyasm 0:deef8c36d17a 195 /// \version 1.46 Fixed error in documentation for runToPosition().
yuliyasm 0:deef8c36d17a 196 /// Reinstated time calculations in runSpeed() since new version is reported
yuliyasm 0:deef8c36d17a 197 /// not to work correctly under some circumstances. Reported by Oleg V Gavva.<br>
yuliyasm 0:deef8c36d17a 198
yuliyasm 0:deef8c36d17a 199 ///
yuliyasm 0:deef8c36d17a 200 /// \author Mike McCauley (mikem@airspayce.com) DO NOT CONTACT THE AUTHOR DIRECTLY: USE THE LISTS
yuliyasm 0:deef8c36d17a 201 // Copyright (C) 2009-2013 Mike McCauley
yuliyasm 0:deef8c36d17a 202 // $Id: AccelStepper.h,v 1.21 2014/10/31 06:05:30 mikem Exp mikem $
yuliyasm 0:deef8c36d17a 203
yuliyasm 0:deef8c36d17a 204 #ifndef AccelStepper_h
yuliyasm 0:deef8c36d17a 205 #define AccelStepper_h
yuliyasm 0:deef8c36d17a 206
yuliyasm 0:deef8c36d17a 207 #include <stdlib.h>
yuliyasm 0:deef8c36d17a 208 #include <mbed.h>
yuliyasm 0:deef8c36d17a 209 #define max(a,b) (((a) > (b)) ? (a) : (b))
yuliyasm 0:deef8c36d17a 210 #define constrain(amt,low,high) ((amt)<(low)?(low):((amt)>(high)?(high):(amt)))
yuliyasm 0:deef8c36d17a 211 #define LOW false
yuliyasm 0:deef8c36d17a 212 #define HIGH true
yuliyasm 0:deef8c36d17a 213 //#if ARDUINO >= 100
yuliyasm 0:deef8c36d17a 214 //#include <Arduino.h>
yuliyasm 0:deef8c36d17a 215 //#else
yuliyasm 0:deef8c36d17a 216 //#include <WProgram.h>
yuliyasm 0:deef8c36d17a 217 //#include <wiring.h>
yuliyasm 0:deef8c36d17a 218 //#endif
yuliyasm 0:deef8c36d17a 219
yuliyasm 0:deef8c36d17a 220 // These defs cause trouble on some versions of Arduino
yuliyasm 0:deef8c36d17a 221 #undef round
yuliyasm 0:deef8c36d17a 222
yuliyasm 0:deef8c36d17a 223 /////////////////////////////////////////////////////////////////////
yuliyasm 0:deef8c36d17a 224 /// \class AccelStepper AccelStepper.h <AccelStepper.h>
yuliyasm 0:deef8c36d17a 225 /// \brief Support for stepper motors with acceleration etc.
yuliyasm 0:deef8c36d17a 226 ///
yuliyasm 0:deef8c36d17a 227 /// This defines a single 2 or 4 pin stepper motor, or stepper moter with fdriver chip, with optional
yuliyasm 0:deef8c36d17a 228 /// acceleration, deceleration, absolute positioning commands etc. Multiple
yuliyasm 0:deef8c36d17a 229 /// simultaneous steppers are supported, all moving
yuliyasm 0:deef8c36d17a 230 /// at different speeds and accelerations.
yuliyasm 0:deef8c36d17a 231 ///
yuliyasm 0:deef8c36d17a 232 /// \par Operation
yuliyasm 0:deef8c36d17a 233 /// This module operates by computing a step time in microseconds. The step
yuliyasm 0:deef8c36d17a 234 /// time is recomputed after each step and after speed and acceleration
yuliyasm 0:deef8c36d17a 235 /// parameters are changed by the caller. The time of each step is recorded in
yuliyasm 0:deef8c36d17a 236 /// microseconds. The run() function steps the motor once if a new step is due.
yuliyasm 0:deef8c36d17a 237 /// The run() function must be called frequently until the motor is in the
yuliyasm 0:deef8c36d17a 238 /// desired position, after which time run() will do nothing.
yuliyasm 0:deef8c36d17a 239 ///
yuliyasm 0:deef8c36d17a 240 /// \par Positioning
yuliyasm 0:deef8c36d17a 241 /// Positions are specified by a signed long integer. At
yuliyasm 0:deef8c36d17a 242 /// construction time, the current position of the motor is consider to be 0. Positive
yuliyasm 0:deef8c36d17a 243 /// positions are clockwise from the initial position; negative positions are
yuliyasm 0:deef8c36d17a 244 /// anticlockwise. The current position can be altered for instance after
yuliyasm 0:deef8c36d17a 245 /// initialization positioning.
yuliyasm 0:deef8c36d17a 246 ///
yuliyasm 0:deef8c36d17a 247 /// \par Caveats
yuliyasm 0:deef8c36d17a 248 /// This is an open loop controller: If the motor stalls or is oversped,
yuliyasm 0:deef8c36d17a 249 /// AccelStepper will not have a correct
yuliyasm 0:deef8c36d17a 250 /// idea of where the motor really is (since there is no feedback of the motor's
yuliyasm 0:deef8c36d17a 251 /// real position. We only know where we _think_ it is, relative to the
yuliyasm 0:deef8c36d17a 252 /// initial starting point).
yuliyasm 0:deef8c36d17a 253 ///
yuliyasm 0:deef8c36d17a 254 /// \par Performance
yuliyasm 0:deef8c36d17a 255 /// The fastest motor speed that can be reliably supported is about 4000 steps per
yuliyasm 0:deef8c36d17a 256 /// second at a clock frequency of 16 MHz on Arduino such as Uno etc.
yuliyasm 0:deef8c36d17a 257 /// Faster processors can support faster stepping speeds.
yuliyasm 0:deef8c36d17a 258 /// However, any speed less than that
yuliyasm 0:deef8c36d17a 259 /// down to very slow speeds (much less than one per second) are also supported,
yuliyasm 0:deef8c36d17a 260 /// provided the run() function is called frequently enough to step the motor
yuliyasm 0:deef8c36d17a 261 /// whenever required for the speed set.
yuliyasm 0:deef8c36d17a 262 /// Calling setAcceleration() is expensive,
yuliyasm 0:deef8c36d17a 263 /// since it requires a square root to be calculated.
yuliyasm 0:deef8c36d17a 264 class AccelStepper
yuliyasm 0:deef8c36d17a 265 {
yuliyasm 0:deef8c36d17a 266 public:
yuliyasm 0:deef8c36d17a 267 /// \brief Symbolic names for number of pins.
yuliyasm 0:deef8c36d17a 268 /// Use this in the pins argument the AccelStepper constructor to
yuliyasm 0:deef8c36d17a 269 /// provide a symbolic name for the number of pins
yuliyasm 0:deef8c36d17a 270 /// to use.
yuliyasm 0:deef8c36d17a 271 typedef enum
yuliyasm 0:deef8c36d17a 272 {
yuliyasm 0:deef8c36d17a 273 FUNCTION = 0, ///< Use the functional interface, implementing your own driver functions (internal use only)
yuliyasm 0:deef8c36d17a 274 DRIVER = 1, ///< Stepper Driver, 2 driver pins required
yuliyasm 0:deef8c36d17a 275 FULL2WIRE = 2, ///< 2 wire stepper, 2 motor pins required
yuliyasm 0:deef8c36d17a 276 FULL3WIRE = 3, ///< 3 wire stepper, such as HDD spindle, 3 motor pins required
yuliyasm 0:deef8c36d17a 277 FULL4WIRE = 4, ///< 4 wire full stepper, 4 motor pins required
yuliyasm 0:deef8c36d17a 278 HALF3WIRE = 6, ///< 3 wire half stepper, such as HDD spindle, 3 motor pins required
yuliyasm 0:deef8c36d17a 279 HALF4WIRE = 8 ///< 4 wire half stepper, 4 motor pins required
yuliyasm 0:deef8c36d17a 280 } MotorInterfaceType;
yuliyasm 0:deef8c36d17a 281
yuliyasm 0:deef8c36d17a 282 /// Constructor. You can have multiple simultaneous steppers, all moving
yuliyasm 0:deef8c36d17a 283 /// at different speeds and accelerations, provided you call their run()
yuliyasm 0:deef8c36d17a 284 /// functions at frequent enough intervals. Current Position is set to 0, target
yuliyasm 0:deef8c36d17a 285 /// position is set to 0. MaxSpeed and Acceleration default to 1.0.
yuliyasm 0:deef8c36d17a 286 /// The motor pins will be initialised to OUTPUT mode during the
yuliyasm 0:deef8c36d17a 287 /// constructor by a call to enableOutputs().
yuliyasm 0:deef8c36d17a 288 /// \param[in] interface Number of pins to interface to. 1, 2, 4 or 8 are
yuliyasm 0:deef8c36d17a 289 /// supported, but it is preferred to use the \ref MotorInterfaceType symbolic names.
yuliyasm 0:deef8c36d17a 290 /// AccelStepper::DRIVER (1) means a stepper driver (with Step and Direction pins).
yuliyasm 0:deef8c36d17a 291 /// If an enable line is also needed, call setEnablePin() after construction.
yuliyasm 0:deef8c36d17a 292 /// You may also invert the pins using setPinsInverted().
yuliyasm 0:deef8c36d17a 293 /// AccelStepper::FULL2WIRE (2) means a 2 wire stepper (2 pins required).
yuliyasm 0:deef8c36d17a 294 /// AccelStepper::FULL3WIRE (3) means a 3 wire stepper, such as HDD spindle (3 pins required).
yuliyasm 0:deef8c36d17a 295 /// AccelStepper::FULL4WIRE (4) means a 4 wire stepper (4 pins required).
yuliyasm 0:deef8c36d17a 296 /// AccelStepper::HALF3WIRE (6) means a 3 wire half stepper, such as HDD spindle (3 pins required)
yuliyasm 0:deef8c36d17a 297 /// AccelStepper::HALF4WIRE (8) means a 4 wire half stepper (4 pins required)
yuliyasm 0:deef8c36d17a 298 /// Defaults to AccelStepper::FULL4WIRE (4) pins.
yuliyasm 0:deef8c36d17a 299 /// \param[in] pin1 Arduino digital pin number for motor pin 1. Defaults
yuliyasm 0:deef8c36d17a 300 /// to pin 2. For a AccelStepper::DRIVER (interface==1),
yuliyasm 0:deef8c36d17a 301 /// this is the Step input to the driver. Low to high transition means to step)
yuliyasm 0:deef8c36d17a 302 /// \param[in] pin2 Arduino digital pin number for motor pin 2. Defaults
yuliyasm 0:deef8c36d17a 303 /// to pin 3. For a AccelStepper::DRIVER (interface==1),
yuliyasm 0:deef8c36d17a 304 /// this is the Direction input the driver. High means forward.
yuliyasm 0:deef8c36d17a 305 /// \param[in] pin3 Arduino digital pin number for motor pin 3. Defaults
yuliyasm 0:deef8c36d17a 306 /// to pin 4.
yuliyasm 0:deef8c36d17a 307 /// \param[in] pin4 Arduino digital pin number for motor pin 4. Defaults
yuliyasm 0:deef8c36d17a 308 /// to pin 5.
yuliyasm 0:deef8c36d17a 309 /// \param[in] enable If this is true (the default), enableOutputs() will be called to enable
yuliyasm 0:deef8c36d17a 310 /// the output pins at construction time.
yuliyasm 0:deef8c36d17a 311 // 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);
yuliyasm 0:deef8c36d17a 312 AccelStepper(uint8_t interface, PinName pin1 = LED1, PinName pin2 = LED2, PinName pin3 = LED3, PinName pin4 = LED4, bool enable = true);
yuliyasm 0:deef8c36d17a 313
yuliyasm 0:deef8c36d17a 314 /// Alternate Constructor which will call your own functions for forward and backward steps.
yuliyasm 0:deef8c36d17a 315 /// You can have multiple simultaneous steppers, all moving
yuliyasm 0:deef8c36d17a 316 /// at different speeds and accelerations, provided you call their run()
yuliyasm 0:deef8c36d17a 317 /// functions at frequent enough intervals. Current Position is set to 0, target
yuliyasm 0:deef8c36d17a 318 /// position is set to 0. MaxSpeed and Acceleration default to 1.0.
yuliyasm 0:deef8c36d17a 319 /// Any motor initialization should happen before hand, no pins are used or initialized.
yuliyasm 0:deef8c36d17a 320 /// \param[in] forward void-returning procedure that will make a forward step
yuliyasm 0:deef8c36d17a 321 /// \param[in] backward void-returning procedure that will make a backward step
yuliyasm 0:deef8c36d17a 322 AccelStepper(void (*forward)(), void (*backward)());
yuliyasm 0:deef8c36d17a 323
yuliyasm 0:deef8c36d17a 324 /// Set the target position. The run() function will try to move the motor (at most one step per call)
yuliyasm 0:deef8c36d17a 325 /// from the current position to the target position set by the most
yuliyasm 0:deef8c36d17a 326 /// recent call to this function. Caution: moveTo() also recalculates the speed for the next step.
yuliyasm 0:deef8c36d17a 327 /// If you are trying to use constant speed movements, you should call setSpeed() after calling moveTo().
yuliyasm 0:deef8c36d17a 328 /// \param[in] absolute The desired absolute position. Negative is
yuliyasm 0:deef8c36d17a 329 /// anticlockwise from the 0 position.
yuliyasm 0:deef8c36d17a 330 void moveTo(long absolute);
yuliyasm 0:deef8c36d17a 331
yuliyasm 0:deef8c36d17a 332 /// Set the target position relative to the current position
yuliyasm 0:deef8c36d17a 333 /// \param[in] relative The desired position relative to the current position. Negative is
yuliyasm 0:deef8c36d17a 334 /// anticlockwise from the current position.
yuliyasm 0:deef8c36d17a 335 void move(long relative);
yuliyasm 0:deef8c36d17a 336
yuliyasm 0:deef8c36d17a 337 /// Poll the motor and step it if a step is due, implementing
yuliyasm 0:deef8c36d17a 338 /// accelerations and decelerations to acheive the target position. You must call this as
yuliyasm 0:deef8c36d17a 339 /// frequently as possible, but at least once per minimum step time interval,
yuliyasm 0:deef8c36d17a 340 /// 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,
yuliyasm 0:deef8c36d17a 341 /// based on the current speed and the time since the last step.
yuliyasm 0:deef8c36d17a 342 /// \return true if the motor is still running to the target position.
yuliyasm 0:deef8c36d17a 343 bool run();
yuliyasm 0:deef8c36d17a 344
yuliyasm 0:deef8c36d17a 345 /// Poll the motor and step it if a step is due, implementing a constant
yuliyasm 0:deef8c36d17a 346 /// speed as set by the most recent call to setSpeed(). You must call this as
yuliyasm 0:deef8c36d17a 347 /// frequently as possible, but at least once per step interval,
yuliyasm 0:deef8c36d17a 348 /// \return true if the motor was stepped.
yuliyasm 0:deef8c36d17a 349 bool runSpeed();
yuliyasm 0:deef8c36d17a 350
yuliyasm 0:deef8c36d17a 351 /// Sets the maximum permitted speed. The run() function will accelerate
yuliyasm 0:deef8c36d17a 352 /// up to the speed set by this function.
yuliyasm 0:deef8c36d17a 353 /// Caution: the maximum speed achievable depends on your processor and clock speed.
yuliyasm 0:deef8c36d17a 354 /// \param[in] speed The desired maximum speed in steps per second. Must
yuliyasm 0:deef8c36d17a 355 /// be > 0. Caution: Speeds that exceed the maximum speed supported by the processor may
yuliyasm 0:deef8c36d17a 356 /// Result in non-linear accelerations and decelerations.
yuliyasm 0:deef8c36d17a 357 void setMaxSpeed(float speed);
yuliyasm 0:deef8c36d17a 358
yuliyasm 0:deef8c36d17a 359 /// Sets the acceleration/deceleration rate.
yuliyasm 0:deef8c36d17a 360 /// \param[in] acceleration The desired acceleration in steps per second
yuliyasm 0:deef8c36d17a 361 /// per second. Must be > 0.0. This is an expensive call since it requires a square
yuliyasm 0:deef8c36d17a 362 /// root to be calculated. Dont call more ofthen than needed
yuliyasm 0:deef8c36d17a 363 void setAcceleration(float acceleration);
yuliyasm 0:deef8c36d17a 364
yuliyasm 0:deef8c36d17a 365 /// Sets the desired constant speed for use with runSpeed().
yuliyasm 0:deef8c36d17a 366 /// \param[in] speed The desired constant speed in steps per
yuliyasm 0:deef8c36d17a 367 /// second. Positive is clockwise. Speeds of more than 1000 steps per
yuliyasm 0:deef8c36d17a 368 /// second are unreliable. Very slow speeds may be set (eg 0.00027777 for
yuliyasm 0:deef8c36d17a 369 /// once per hour, approximately. Speed accuracy depends on the Arduino
yuliyasm 0:deef8c36d17a 370 /// crystal. Jitter depends on how frequently you call the runSpeed() function.
yuliyasm 0:deef8c36d17a 371 void setSpeed(float speed);
yuliyasm 0:deef8c36d17a 372
yuliyasm 0:deef8c36d17a 373 /// The most recently set speed
yuliyasm 0:deef8c36d17a 374 /// \return the most recent speed in steps per second
yuliyasm 0:deef8c36d17a 375 float speed();
yuliyasm 0:deef8c36d17a 376
yuliyasm 0:deef8c36d17a 377 /// The distance from the current position to the target position.
yuliyasm 0:deef8c36d17a 378 /// \return the distance from the current position to the target position
yuliyasm 0:deef8c36d17a 379 /// in steps. Positive is clockwise from the current position.
yuliyasm 0:deef8c36d17a 380 long distanceToGo();
yuliyasm 0:deef8c36d17a 381
yuliyasm 0:deef8c36d17a 382 /// The most recently set target position.
yuliyasm 0:deef8c36d17a 383 /// \return the target position
yuliyasm 0:deef8c36d17a 384 /// in steps. Positive is clockwise from the 0 position.
yuliyasm 0:deef8c36d17a 385 long targetPosition();
yuliyasm 0:deef8c36d17a 386
yuliyasm 0:deef8c36d17a 387 /// The currently motor position.
yuliyasm 0:deef8c36d17a 388 /// \return the current motor position
yuliyasm 0:deef8c36d17a 389 /// in steps. Positive is clockwise from the 0 position.
yuliyasm 0:deef8c36d17a 390 long currentPosition();
yuliyasm 0:deef8c36d17a 391
yuliyasm 0:deef8c36d17a 392 /// Resets the current position of the motor, so that wherever the motor
yuliyasm 0:deef8c36d17a 393 /// happens to be right now is considered to be the new 0 position. Useful
yuliyasm 0:deef8c36d17a 394 /// for setting a zero position on a stepper after an initial hardware
yuliyasm 0:deef8c36d17a 395 /// positioning move.
yuliyasm 0:deef8c36d17a 396 /// Has the side effect of setting the current motor speed to 0.
yuliyasm 0:deef8c36d17a 397 /// \param[in] position The position in steps of wherever the motor
yuliyasm 0:deef8c36d17a 398 /// happens to be right now.
yuliyasm 0:deef8c36d17a 399 void setCurrentPosition(long position);
yuliyasm 0:deef8c36d17a 400
yuliyasm 0:deef8c36d17a 401 /// Moves the motor (with acceleration/deceleration)
yuliyasm 0:deef8c36d17a 402 /// to the target position and blocks until it is at
yuliyasm 0:deef8c36d17a 403 /// position. Dont use this in event loops, since it blocks.
yuliyasm 0:deef8c36d17a 404 void runToPosition();
yuliyasm 0:deef8c36d17a 405
yuliyasm 0:deef8c36d17a 406 /// Runs at the currently selected speed until the target position is reached
yuliyasm 0:deef8c36d17a 407 /// Does not implement accelerations.
yuliyasm 0:deef8c36d17a 408 /// \return true if it stepped
yuliyasm 0:deef8c36d17a 409 bool runSpeedToPosition();
yuliyasm 0:deef8c36d17a 410
yuliyasm 0:deef8c36d17a 411 /// Moves the motor (with acceleration/deceleration)
yuliyasm 0:deef8c36d17a 412 /// to the new target position and blocks until it is at
yuliyasm 0:deef8c36d17a 413 /// position. Dont use this in event loops, since it blocks.
yuliyasm 0:deef8c36d17a 414 /// \param[in] position The new target position.
yuliyasm 0:deef8c36d17a 415 void runToNewPosition(long position);
yuliyasm 0:deef8c36d17a 416
yuliyasm 0:deef8c36d17a 417 /// Sets a new target position that causes the stepper
yuliyasm 0:deef8c36d17a 418 /// to stop as quickly as possible, using the current speed and acceleration parameters.
yuliyasm 0:deef8c36d17a 419 void stop();
yuliyasm 0:deef8c36d17a 420
yuliyasm 0:deef8c36d17a 421 /// Disable motor pin outputs by setting them all LOW
yuliyasm 0:deef8c36d17a 422 /// Depending on the design of your electronics this may turn off
yuliyasm 0:deef8c36d17a 423 /// the power to the motor coils, saving power.
yuliyasm 0:deef8c36d17a 424 /// This is useful to support Arduino low power modes: disable the outputs
yuliyasm 0:deef8c36d17a 425 /// during sleep and then reenable with enableOutputs() before stepping
yuliyasm 0:deef8c36d17a 426 /// again.
yuliyasm 0:deef8c36d17a 427 virtual void disableOutputs();
yuliyasm 0:deef8c36d17a 428
yuliyasm 0:deef8c36d17a 429 /// Enable motor pin outputs by setting the motor pins to OUTPUT
yuliyasm 0:deef8c36d17a 430 /// mode. Called automatically by the constructor.
yuliyasm 0:deef8c36d17a 431 virtual void enableOutputs();
yuliyasm 0:deef8c36d17a 432
yuliyasm 0:deef8c36d17a 433 /// Sets the minimum pulse width allowed by the stepper driver. The minimum practical pulse width is
yuliyasm 0:deef8c36d17a 434 /// approximately 20 microseconds. Times less than 20 microseconds
yuliyasm 0:deef8c36d17a 435 /// will usually result in 20 microseconds or so.
yuliyasm 0:deef8c36d17a 436 /// \param[in] minWidth The minimum pulse width in microseconds.
yuliyasm 0:deef8c36d17a 437 void setMinPulseWidth(unsigned int minWidth);
yuliyasm 0:deef8c36d17a 438
yuliyasm 0:deef8c36d17a 439 /// Sets the enable pin number for stepper drivers.
yuliyasm 0:deef8c36d17a 440 /// 0xFF indicates unused (default).
yuliyasm 0:deef8c36d17a 441 /// Otherwise, if a pin is set, the pin will be turned on when
yuliyasm 0:deef8c36d17a 442 /// enableOutputs() is called and switched off when disableOutputs()
yuliyasm 0:deef8c36d17a 443 /// is called.
yuliyasm 0:deef8c36d17a 444 /// \param[in] enablePin Arduino digital pin number for motor enable
yuliyasm 0:deef8c36d17a 445 /// \sa setPinsInverted
yuliyasm 0:deef8c36d17a 446 void setEnablePin(PinName enablePin);
yuliyasm 0:deef8c36d17a 447
yuliyasm 0:deef8c36d17a 448 /// Sets the inversion for stepper driver pins
yuliyasm 0:deef8c36d17a 449 /// \param[in] directionInvert True for inverted direction pin, false for non-inverted
yuliyasm 0:deef8c36d17a 450 /// \param[in] stepInvert True for inverted step pin, false for non-inverted
yuliyasm 0:deef8c36d17a 451 /// \param[in] enableInvert True for inverted enable pin, false (default) for non-inverted
yuliyasm 0:deef8c36d17a 452 void setPinsInverted(bool directionInvert = false, bool stepInvert = false, bool enableInvert = false);
yuliyasm 0:deef8c36d17a 453
yuliyasm 0:deef8c36d17a 454 /// Sets the inversion for 2, 3 and 4 wire stepper pins
yuliyasm 0:deef8c36d17a 455 /// \param[in] pin1Invert True for inverted pin1, false for non-inverted
yuliyasm 0:deef8c36d17a 456 /// \param[in] pin2Invert True for inverted pin2, false for non-inverted
yuliyasm 0:deef8c36d17a 457 /// \param[in] pin3Invert True for inverted pin3, false for non-inverted
yuliyasm 0:deef8c36d17a 458 /// \param[in] pin4Invert True for inverted pin4, false for non-inverted
yuliyasm 0:deef8c36d17a 459 /// \param[in] enableInvert True for inverted enable pin, false (default) for non-inverted
yuliyasm 0:deef8c36d17a 460 void setPinsInverted(bool pin1Invert, bool pin2Invert, bool pin3Invert, bool pin4Invert, bool enableInvert);
yuliyasm 0:deef8c36d17a 461
yuliyasm 0:deef8c36d17a 462 protected:
yuliyasm 0:deef8c36d17a 463 DigitalOut *_pin0;
yuliyasm 0:deef8c36d17a 464 DigitalOut *_pin1;
yuliyasm 0:deef8c36d17a 465 DigitalOut *_pin2;
yuliyasm 0:deef8c36d17a 466 DigitalOut *_pin3;
yuliyasm 0:deef8c36d17a 467
yuliyasm 0:deef8c36d17a 468 /// \brief Direction indicator
yuliyasm 0:deef8c36d17a 469 /// Symbolic names for the direction the motor is turning
yuliyasm 0:deef8c36d17a 470 typedef enum
yuliyasm 0:deef8c36d17a 471 {
yuliyasm 0:deef8c36d17a 472 DIRECTION_CCW = 0, ///< Clockwise
yuliyasm 0:deef8c36d17a 473 DIRECTION_CW = 1 ///< Counter-Clockwise
yuliyasm 0:deef8c36d17a 474 } Direction;
yuliyasm 0:deef8c36d17a 475
yuliyasm 0:deef8c36d17a 476 /// Forces the library to compute a new instantaneous speed and set that as
yuliyasm 0:deef8c36d17a 477 /// the current speed. It is called by
yuliyasm 0:deef8c36d17a 478 /// the library:
yuliyasm 0:deef8c36d17a 479 /// \li after each step
yuliyasm 0:deef8c36d17a 480 /// \li after change to maxSpeed through setMaxSpeed()
yuliyasm 0:deef8c36d17a 481 /// \li after change to acceleration through setAcceleration()
yuliyasm 0:deef8c36d17a 482 /// \li after change to target position (relative or absolute) through
yuliyasm 0:deef8c36d17a 483 /// move() or moveTo()
yuliyasm 0:deef8c36d17a 484 void computeNewSpeed();
yuliyasm 0:deef8c36d17a 485
yuliyasm 0:deef8c36d17a 486 /// Low level function to set the motor output pins
yuliyasm 0:deef8c36d17a 487 /// bit 0 of the mask corresponds to _pin[0]
yuliyasm 0:deef8c36d17a 488 /// bit 1 of the mask corresponds to _pin[1]
yuliyasm 0:deef8c36d17a 489 /// You can override this to impment, for example serial chip output insted of using the
yuliyasm 0:deef8c36d17a 490 /// output pins directly
yuliyasm 0:deef8c36d17a 491 virtual void setOutputPins(uint8_t mask);
yuliyasm 0:deef8c36d17a 492
yuliyasm 0:deef8c36d17a 493 /// Called to execute a step. Only called when a new step is
yuliyasm 0:deef8c36d17a 494 /// required. Subclasses may override to implement new stepping
yuliyasm 0:deef8c36d17a 495 /// interfaces. The default calls step1(), step2(), step4() or step8() depending on the
yuliyasm 0:deef8c36d17a 496 /// number of pins defined for the stepper.
yuliyasm 0:deef8c36d17a 497 /// \param[in] step The current step phase number (0 to 7)
yuliyasm 0:deef8c36d17a 498 virtual void step(long step);
yuliyasm 0:deef8c36d17a 499
yuliyasm 0:deef8c36d17a 500 /// Called to execute a step using stepper functions (pins = 0) Only called when a new step is
yuliyasm 0:deef8c36d17a 501 /// required. Calls _forward() or _backward() to perform the step
yuliyasm 0:deef8c36d17a 502 /// \param[in] step The current step phase number (0 to 7)
yuliyasm 0:deef8c36d17a 503 virtual void step0(long step);
yuliyasm 0:deef8c36d17a 504
yuliyasm 0:deef8c36d17a 505 /// Called to execute a step on a stepper driver (ie where pins == 1). Only called when a new step is
yuliyasm 0:deef8c36d17a 506 /// required. Subclasses may override to implement new stepping
yuliyasm 0:deef8c36d17a 507 /// interfaces. The default sets or clears the outputs of Step pin1 to step,
yuliyasm 0:deef8c36d17a 508 /// and sets the output of _pin2 to the desired direction. The Step pin (_pin1) is pulsed for 1 microsecond
yuliyasm 0:deef8c36d17a 509 /// which is the minimum STEP pulse width for the 3967 driver.
yuliyasm 0:deef8c36d17a 510 /// \param[in] step The current step phase number (0 to 7)
yuliyasm 0:deef8c36d17a 511 virtual void step1(long step);
yuliyasm 0:deef8c36d17a 512
yuliyasm 0:deef8c36d17a 513 /// Called to execute a step on a 2 pin motor. Only called when a new step is
yuliyasm 0:deef8c36d17a 514 /// required. Subclasses may override to implement new stepping
yuliyasm 0:deef8c36d17a 515 /// interfaces. The default sets or clears the outputs of pin1 and pin2
yuliyasm 0:deef8c36d17a 516 /// \param[in] step The current step phase number (0 to 7)
yuliyasm 0:deef8c36d17a 517 virtual void step2(long step);
yuliyasm 0:deef8c36d17a 518
yuliyasm 0:deef8c36d17a 519 /// Called to execute a step on a 3 pin motor, such as HDD spindle. Only called when a new step is
yuliyasm 0:deef8c36d17a 520 /// required. Subclasses may override to implement new stepping
yuliyasm 0:deef8c36d17a 521 /// interfaces. The default sets or clears the outputs of pin1, pin2,
yuliyasm 0:deef8c36d17a 522 /// pin3
yuliyasm 0:deef8c36d17a 523 /// \param[in] step The current step phase number (0 to 7)
yuliyasm 0:deef8c36d17a 524 virtual void step3(long step);
yuliyasm 0:deef8c36d17a 525
yuliyasm 0:deef8c36d17a 526 /// Called to execute a step on a 4 pin motor. Only called when a new step is
yuliyasm 0:deef8c36d17a 527 /// required. Subclasses may override to implement new stepping
yuliyasm 0:deef8c36d17a 528 /// interfaces. The default sets or clears the outputs of pin1, pin2,
yuliyasm 0:deef8c36d17a 529 /// pin3, pin4.
yuliyasm 0:deef8c36d17a 530 /// \param[in] step The current step phase number (0 to 7)
yuliyasm 0:deef8c36d17a 531 virtual void step4(long step);
yuliyasm 0:deef8c36d17a 532
yuliyasm 0:deef8c36d17a 533 /// Called to execute a step on a 3 pin motor, such as HDD spindle. Only called when a new step is
yuliyasm 0:deef8c36d17a 534 /// required. Subclasses may override to implement new stepping
yuliyasm 0:deef8c36d17a 535 /// interfaces. The default sets or clears the outputs of pin1, pin2,
yuliyasm 0:deef8c36d17a 536 /// pin3
yuliyasm 0:deef8c36d17a 537 /// \param[in] step The current step phase number (0 to 7)
yuliyasm 0:deef8c36d17a 538 virtual void step6(long step);
yuliyasm 0:deef8c36d17a 539
yuliyasm 0:deef8c36d17a 540 /// Called to execute a step on a 4 pin half-steper motor. Only called when a new step is
yuliyasm 0:deef8c36d17a 541 /// required. Subclasses may override to implement new stepping
yuliyasm 0:deef8c36d17a 542 /// interfaces. The default sets or clears the outputs of pin1, pin2,
yuliyasm 0:deef8c36d17a 543 /// pin3, pin4.
yuliyasm 0:deef8c36d17a 544 /// \param[in] step The current step phase number (0 to 7)
yuliyasm 0:deef8c36d17a 545 virtual void step8(long step);
yuliyasm 0:deef8c36d17a 546
yuliyasm 0:deef8c36d17a 547 private:
yuliyasm 0:deef8c36d17a 548 /// Number of pins on the stepper motor. Permits 2 or 4. 2 pins is a
yuliyasm 0:deef8c36d17a 549 /// bipolar, and 4 pins is a unipolar.
yuliyasm 0:deef8c36d17a 550 uint8_t _interface; // 0, 1, 2, 4, 8, See MotorInterfaceType
yuliyasm 0:deef8c36d17a 551
yuliyasm 0:deef8c36d17a 552 /// Arduino pin number assignments for the 2 or 4 pins required to interface to the
yuliyasm 0:deef8c36d17a 553 /// stepper motor or driver
yuliyasm 0:deef8c36d17a 554 uint8_t _pin[4];
yuliyasm 0:deef8c36d17a 555
yuliyasm 0:deef8c36d17a 556 /// Whether the _pins is inverted or not
yuliyasm 0:deef8c36d17a 557 uint8_t _pinInverted[4];
yuliyasm 0:deef8c36d17a 558
yuliyasm 0:deef8c36d17a 559 /// The current absolution position in steps.
yuliyasm 0:deef8c36d17a 560 long _currentPos; // Steps
yuliyasm 0:deef8c36d17a 561
yuliyasm 0:deef8c36d17a 562 /// The target position in steps. The AccelStepper library will move the
yuliyasm 0:deef8c36d17a 563 /// motor from the _currentPos to the _targetPos, taking into account the
yuliyasm 0:deef8c36d17a 564 /// max speed, acceleration and deceleration
yuliyasm 0:deef8c36d17a 565 long _targetPos; // Steps
yuliyasm 0:deef8c36d17a 566
yuliyasm 0:deef8c36d17a 567 /// The current motos speed in steps per second
yuliyasm 0:deef8c36d17a 568 /// Positive is clockwise
yuliyasm 0:deef8c36d17a 569 float _speed; // Steps per second
yuliyasm 0:deef8c36d17a 570
yuliyasm 0:deef8c36d17a 571 /// The maximum permitted speed in steps per second. Must be > 0.
yuliyasm 0:deef8c36d17a 572 float _maxSpeed;
yuliyasm 0:deef8c36d17a 573
yuliyasm 0:deef8c36d17a 574 /// The acceleration to use to accelerate or decelerate the motor in steps
yuliyasm 0:deef8c36d17a 575 /// per second per second. Must be > 0
yuliyasm 0:deef8c36d17a 576 float _acceleration;
yuliyasm 0:deef8c36d17a 577 float _sqrt_twoa; // Precomputed sqrt(2*_acceleration)
yuliyasm 0:deef8c36d17a 578
yuliyasm 0:deef8c36d17a 579 /// The current interval between steps in microseconds.
yuliyasm 0:deef8c36d17a 580 /// 0 means the motor is currently stopped with _speed == 0
yuliyasm 0:deef8c36d17a 581 unsigned long _stepInterval;
yuliyasm 0:deef8c36d17a 582
yuliyasm 0:deef8c36d17a 583 /// The last step time in microseconds
yuliyasm 0:deef8c36d17a 584 unsigned long _lastStepTime;
yuliyasm 0:deef8c36d17a 585
yuliyasm 0:deef8c36d17a 586 /// The minimum allowed pulse width in microseconds
yuliyasm 0:deef8c36d17a 587 unsigned int _minPulseWidth;
yuliyasm 0:deef8c36d17a 588
yuliyasm 0:deef8c36d17a 589 /// Is the direction pin inverted?
yuliyasm 0:deef8c36d17a 590 ///bool _dirInverted; /// Moved to _pinInverted[1]
yuliyasm 0:deef8c36d17a 591
yuliyasm 0:deef8c36d17a 592 /// Is the step pin inverted?
yuliyasm 0:deef8c36d17a 593 ///bool _stepInverted; /// Moved to _pinInverted[0]
yuliyasm 0:deef8c36d17a 594
yuliyasm 0:deef8c36d17a 595 /// Is the enable pin inverted?
yuliyasm 0:deef8c36d17a 596 bool _enableInverted;
yuliyasm 0:deef8c36d17a 597
yuliyasm 0:deef8c36d17a 598 /// Enable pin for stepper driver, or 0xFF if unused.
yuliyasm 0:deef8c36d17a 599 //uint8_t _enablePin;
yuliyasm 0:deef8c36d17a 600 DigitalOut *_enablePin;
yuliyasm 0:deef8c36d17a 601
yuliyasm 0:deef8c36d17a 602 /// The pointer to a forward-step procedure
yuliyasm 0:deef8c36d17a 603 void (*_forward)();
yuliyasm 0:deef8c36d17a 604
yuliyasm 0:deef8c36d17a 605 /// The pointer to a backward-step procedure
yuliyasm 0:deef8c36d17a 606 void (*_backward)();
yuliyasm 0:deef8c36d17a 607
yuliyasm 0:deef8c36d17a 608 /// The step counter for speed calculations
yuliyasm 0:deef8c36d17a 609 long _n;
yuliyasm 0:deef8c36d17a 610
yuliyasm 0:deef8c36d17a 611 /// Initial step size in microseconds
yuliyasm 0:deef8c36d17a 612 float _c0;
yuliyasm 0:deef8c36d17a 613
yuliyasm 0:deef8c36d17a 614 /// Last step size in microseconds
yuliyasm 0:deef8c36d17a 615 float _cn;
yuliyasm 0:deef8c36d17a 616
yuliyasm 0:deef8c36d17a 617 /// Min step size in microseconds based on maxSpeed
yuliyasm 0:deef8c36d17a 618 float _cmin; // at max speed
yuliyasm 0:deef8c36d17a 619
yuliyasm 0:deef8c36d17a 620 /// Current direction motor is spinning in
yuliyasm 0:deef8c36d17a 621 bool _direction; // 1 == CW
yuliyasm 0:deef8c36d17a 622
yuliyasm 0:deef8c36d17a 623 };
yuliyasm 0:deef8c36d17a 624
yuliyasm 0:deef8c36d17a 625 /// @example Random.pde
yuliyasm 0:deef8c36d17a 626 /// Make a single stepper perform random changes in speed, position and acceleration
yuliyasm 0:deef8c36d17a 627
yuliyasm 0:deef8c36d17a 628 /// @example Overshoot.pde
yuliyasm 0:deef8c36d17a 629 /// Check overshoot handling
yuliyasm 0:deef8c36d17a 630 /// which sets a new target position and then waits until the stepper has
yuliyasm 0:deef8c36d17a 631 /// achieved it. This is used for testing the handling of overshoots
yuliyasm 0:deef8c36d17a 632
yuliyasm 0:deef8c36d17a 633 /// @example MultiStepper.pde
yuliyasm 0:deef8c36d17a 634 /// Shows how to multiple simultaneous steppers
yuliyasm 0:deef8c36d17a 635 /// Runs one stepper forwards and backwards, accelerating and decelerating
yuliyasm 0:deef8c36d17a 636 /// at the limits. Runs other steppers at the same time
yuliyasm 0:deef8c36d17a 637
yuliyasm 0:deef8c36d17a 638 /// @example ConstantSpeed.pde
yuliyasm 0:deef8c36d17a 639 /// Shows how to run AccelStepper in the simplest,
yuliyasm 0:deef8c36d17a 640 /// fixed speed mode with no accelerations
yuliyasm 0:deef8c36d17a 641
yuliyasm 0:deef8c36d17a 642 /// @example Blocking.pde
yuliyasm 0:deef8c36d17a 643 /// Shows how to use the blocking call runToNewPosition
yuliyasm 0:deef8c36d17a 644 /// Which sets a new target position and then waits until the stepper has
yuliyasm 0:deef8c36d17a 645 /// achieved it.
yuliyasm 0:deef8c36d17a 646
yuliyasm 0:deef8c36d17a 647 /// @example AFMotor_MultiStepper.pde
yuliyasm 0:deef8c36d17a 648 /// Control both Stepper motors at the same time with different speeds
yuliyasm 0:deef8c36d17a 649 /// and accelerations.
yuliyasm 0:deef8c36d17a 650
yuliyasm 0:deef8c36d17a 651 /// @example AFMotor_ConstantSpeed.pde
yuliyasm 0:deef8c36d17a 652 /// Shows how to run AccelStepper in the simplest,
yuliyasm 0:deef8c36d17a 653 /// fixed speed mode with no accelerations
yuliyasm 0:deef8c36d17a 654
yuliyasm 0:deef8c36d17a 655 /// @example ProportionalControl.pde
yuliyasm 0:deef8c36d17a 656 /// Make a single stepper follow the analog value read from a pot or whatever
yuliyasm 0:deef8c36d17a 657 /// The stepper will move at a constant speed to each newly set posiiton,
yuliyasm 0:deef8c36d17a 658 /// depending on the value of the pot.
yuliyasm 0:deef8c36d17a 659
yuliyasm 0:deef8c36d17a 660 /// @example Bounce.pde
yuliyasm 0:deef8c36d17a 661 /// Make a single stepper bounce from one limit to another, observing
yuliyasm 0:deef8c36d17a 662 /// accelrations at each end of travel
yuliyasm 0:deef8c36d17a 663
yuliyasm 0:deef8c36d17a 664 /// @example Quickstop.pde
yuliyasm 0:deef8c36d17a 665 /// Check stop handling.
yuliyasm 0:deef8c36d17a 666 /// Calls stop() while the stepper is travelling at full speed, causing
yuliyasm 0:deef8c36d17a 667 /// the stepper to stop as quickly as possible, within the constraints of the
yuliyasm 0:deef8c36d17a 668 /// current acceleration.
yuliyasm 0:deef8c36d17a 669
yuliyasm 0:deef8c36d17a 670 /// @example MotorShield.pde
yuliyasm 0:deef8c36d17a 671 /// Shows how to use AccelStepper to control a 3-phase motor, such as a HDD spindle motor
yuliyasm 0:deef8c36d17a 672 /// using the Adafruit Motor Shield http://www.ladyada.net/make/mshield/index.html.
yuliyasm 0:deef8c36d17a 673
yuliyasm 0:deef8c36d17a 674 /// @example DualMotorShield.pde
yuliyasm 0:deef8c36d17a 675 /// Shows how to use AccelStepper to control 2 x 2 phase steppers using the
yuliyasm 0:deef8c36d17a 676 /// Itead Studio Arduino Dual Stepper Motor Driver Shield
yuliyasm 0:deef8c36d17a 677 /// model IM120417015
yuliyasm 0:deef8c36d17a 678
yuliyasm 0:deef8c36d17a 679 #endif
yuliyasm 0:deef8c36d17a 680