thanks to Zoltan Hudak publish the way to use stm32f103c8t6 on mbed. now you can use it with stepper driver

Dependencies:   mbed-STM32F103C8T6 mbed

Fork of STM32F103C8T6_Hello by Zoltan Hudak

Committer:
Zeran
Date:
Tue May 23 17:28:21 2017 +0000
Revision:
11:f44fc4db4ab1
stm32f103c8t6 stepper driver

Who changed what in which revision?

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