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