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