Skip to content
100644 337 lines (336 sloc) 36.4 KB
843e9aa @ladyada Slightly modified to support AFMotor by passing in function pointers
ladyada authored Jan 4, 2011
1 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
2 <html><head><meta http-equiv="Content-Type" content="text/html;charset=UTF-8">
3 <title>AccelStepper: AccelStepper.h Source File</title>
4 <link href="doxygen.css" rel="stylesheet" type="text/css">
5 <link href="tabs.css" rel="stylesheet" type="text/css">
6 </head><body>
7 <!-- Generated by Doxygen 1.5.6 -->
8 <div class="navigation" id="top">
9 <div class="tabs">
10 <ul>
11 <li><a href="index.html"><span>Main&nbsp;Page</span></a></li>
12 <li><a href="annotated.html"><span>Classes</span></a></li>
13 <li class="current"><a href="files.html"><span>Files</span></a></li>
14 </ul>
15 </div>
16 <h1>AccelStepper.h</h1><div class="fragment"><pre class="fragment"><a name="l00001"></a>00001 <span class="comment">// AccelStepper.h</span>
17 <a name="l00002"></a>00002 <span class="comment">//</span><span class="comment"></span>
18 <a name="l00003"></a>00003 <span class="comment">/// \mainpage AccelStepper library for Arduino</span>
19 <a name="l00004"></a>00004 <span class="comment">///</span>
20 <a name="l00005"></a>00005 <span class="comment">/// This is the Arduino AccelStepper 1.2 library.</span>
21 <a name="l00006"></a>00006 <span class="comment">/// It provides an object-oriented interface for 2 or 4 pin stepper motors.</span>
22 <a name="l00007"></a>00007 <span class="comment">///</span>
23 <a name="l00008"></a>00008 <span class="comment">/// The standard Arduino IDE includes the Stepper library</span>
24 <a name="l00009"></a>00009 <span class="comment">/// ( for stepper motors. It is</span>
25 <a name="l00010"></a>00010 <span class="comment">/// perfectly adequate for simple, single motor applications.</span>
26 <a name="l00011"></a>00011 <span class="comment">///</span>
27 <a name="l00012"></a>00012 <span class="comment">/// AccelStepper significantly improves on the standard Arduino Stepper library in several ways:</span>
28 <a name="l00013"></a>00013 <span class="comment">/// \li Supports acceleration and deceleration</span>
29 <a name="l00014"></a>00014 <span class="comment">/// \li Supports multiple simultaneous steppers, with independent concurrent stepping on each stepper</span>
30 <a name="l00015"></a>00015 <span class="comment">/// \li API functions never delay() or block</span>
31 <a name="l00016"></a>00016 <span class="comment">/// \li Supports 2 and 4 wire steppers</span>
32 <a name="l00017"></a>00017 <span class="comment">/// \li Supports stepper drivers such as the Sparkfun EasyDriver (based on 3967 driver chip)</span>
33 <a name="l00018"></a>00018 <span class="comment">/// \li Very slow speeds are supported</span>
34 <a name="l00019"></a>00019 <span class="comment">/// \li Extensive API</span>
35 <a name="l00020"></a>00020 <span class="comment">/// \li Subclass support</span>
36 <a name="l00021"></a>00021 <span class="comment">///</span>
37 <a name="l00022"></a>00022 <span class="comment">/// The latest version of this documentation can be downloaded from </span>
38 <a name="l00023"></a>00023 <span class="comment">///</span>
39 <a name="l00024"></a>00024 <span class="comment">///</span>
40 <a name="l00025"></a>00025 <span class="comment">/// Example Arduino programs are included to show the main modes of use.</span>
41 <a name="l00026"></a>00026 <span class="comment">///</span>
42 <a name="l00027"></a>00027 <span class="comment">/// The version of the package that this documentation refers to can be downloaded </span>
43 <a name="l00028"></a>00028 <span class="comment">/// from</span>
44 <a name="l00029"></a>00029 <span class="comment">/// You can find the latest version at</span>
45 <a name="l00030"></a>00030 <span class="comment">///</span>
46 <a name="l00031"></a>00031 <span class="comment">/// Tested on Arduino Diecimila and Mega with arduino-0018 on OpenSuSE 11.1 and avr-libc-1.6.1-1.15,</span>
47 <a name="l00032"></a>00032 <span class="comment">/// cross-avr-binutils-2.19-9.1, cross-avr-gcc-4.1.3_20080612-26.5.</span>
48 <a name="l00033"></a>00033 <span class="comment">///</span>
49 <a name="l00034"></a>00034 <span class="comment">/// \par Installation</span>
50 <a name="l00035"></a>00035 <span class="comment">/// Install in the usual way: unzip the distribution zip file to the libraries</span>
51 <a name="l00036"></a>00036 <span class="comment">/// sub-folder of your sketchbook. </span>
52 <a name="l00037"></a>00037 <span class="comment">///</span>
53 <a name="l00038"></a>00038 <span class="comment">/// This software is Copyright (C) 2010 Mike McCauley. Use is subject to license</span>
54 <a name="l00039"></a>00039 <span class="comment">/// conditions. The main licensing options available are GPL V2 or Commercial:</span>
55 <a name="l00040"></a>00040 <span class="comment">/// </span>
56 <a name="l00041"></a>00041 <span class="comment">/// \par Open Source Licensing GPL V2</span>
57 <a name="l00042"></a>00042 <span class="comment">/// This is the appropriate option if you want to share the source code of your</span>
58 <a name="l00043"></a>00043 <span class="comment">/// application with everyone you distribute it to, and you also want to give them</span>
59 <a name="l00044"></a>00044 <span class="comment">/// the right to share who uses it. If you wish to use this software under Open</span>
60 <a name="l00045"></a>00045 <span class="comment">/// Source Licensing, you must contribute all your source code to the open source</span>
61 <a name="l00046"></a>00046 <span class="comment">/// community in accordance with the GPL Version 2 when your application is</span>
62 <a name="l00047"></a>00047 <span class="comment">/// distributed. See</span>
63 <a name="l00048"></a>00048 <span class="comment">/// </span>
64 <a name="l00049"></a>00049 <span class="comment">/// \par Commercial Licensing</span>
65 <a name="l00050"></a>00050 <span class="comment">/// This is the appropriate option if you are creating proprietary applications</span>
66 <a name="l00051"></a>00051 <span class="comment">/// and you are not prepared to distribute and share the source code of your</span>
67 <a name="l00052"></a>00052 <span class="comment">/// application. Contact for details.</span>
68 <a name="l00053"></a>00053 <span class="comment">///</span>
69 <a name="l00054"></a>00054 <span class="comment">/// \par Revision History</span>
70 <a name="l00055"></a>00055 <span class="comment">/// \version 1.0 Initial release</span>
71 <a name="l00056"></a>00056 <span class="comment">///</span>
72 <a name="l00057"></a>00057 <span class="comment">/// \version 1.1 Added speed() function to get the current speed.</span>
73 <a name="l00058"></a>00058 <span class="comment">/// \version 1.2 Added runSpeedToPosition() submitted by Gunnar Arndt.</span>
74 <a name="l00059"></a>00059 <span class="comment">/// \version 1.3 Added support for stepper drivers (ie with Step and Direction inputs) with _pins == 1</span>
75 <a name="l00060"></a>00060 <span class="comment">/// </span>
76 <a name="l00061"></a>00061 <span class="comment">///</span>
77 <a name="l00062"></a>00062 <span class="comment">/// \author Mike McCauley (</span>
78 <a name="l00063"></a>00063 <span class="comment"></span><span class="comment">// Copyright (C) 2009 Mike McCauley</span>
79 <a name="l00064"></a>00064 <span class="comment">// $Id: AccelStepper.h,v 1.2 2010/10/24 07:46:18 mikem Exp mikem $</span>
80 <a name="l00065"></a>00065
81 <a name="l00066"></a>00066 <span class="preprocessor">#ifndef AccelStepper_h</span>
82 <a name="l00067"></a>00067 <span class="preprocessor"></span><span class="preprocessor">#define AccelStepper_h</span>
83 <a name="l00068"></a>00068 <span class="preprocessor"></span>
84 <a name="l00069"></a>00069 <span class="preprocessor">#include &lt;stdlib.h&gt;</span>
85 <a name="l00070"></a>00070 <span class="preprocessor">#include &lt;wiring.h&gt;</span>
86 <a name="l00071"></a>00071
87 <a name="l00072"></a>00072 <span class="comment">// These defs cause trouble on some versions of Arduino</span>
88 <a name="l00073"></a>00073 <span class="preprocessor">#undef round</span>
89 <a name="l00074"></a>00074 <span class="preprocessor"></span><span class="comment"></span>
90 <a name="l00075"></a>00075 <span class="comment">/////////////////////////////////////////////////////////////////////</span>
91 <a name="l00076"></a>00076 <span class="comment">/// \class AccelStepper AccelStepper.h &lt;AccelStepper.h&gt;</span>
92 <a name="l00077"></a>00077 <span class="comment">/// \brief Support for stepper motors with acceleration etc.</span>
93 <a name="l00078"></a>00078 <span class="comment">///</span>
94 <a name="l00079"></a>00079 <span class="comment">/// This defines a single 2 or 4 pin stepper motor, or stepper moter with fdriver chip, with optional</span>
95 <a name="l00080"></a>00080 <span class="comment">/// acceleration, deceleration, absolute positioning commands etc. Multiple</span>
96 <a name="l00081"></a>00081 <span class="comment">/// simultaneous steppers are supported, all moving </span>
97 <a name="l00082"></a>00082 <span class="comment">/// at different speeds and accelerations. </span>
98 <a name="l00083"></a>00083 <span class="comment">///</span>
99 <a name="l00084"></a>00084 <span class="comment">/// \par Operation</span>
100 <a name="l00085"></a>00085 <span class="comment">/// This module operates by computing a step time in milliseconds. The step</span>
101 <a name="l00086"></a>00086 <span class="comment">/// time is recomputed after each step and after speed and acceleration</span>
102 <a name="l00087"></a>00087 <span class="comment">/// parameters are changed by the caller. The time of each step is recorded in</span>
103 <a name="l00088"></a>00088 <span class="comment">/// milliseconds. The run() function steps the motor if a new step is due.</span>
104 <a name="l00089"></a>00089 <span class="comment">/// The run() function must be called frequently until the motor is in the</span>
105 <a name="l00090"></a>00090 <span class="comment">/// desired position, after which time run() will do nothing.</span>
106 <a name="l00091"></a>00091 <span class="comment">///</span>
107 <a name="l00092"></a>00092 <span class="comment">/// \par Positioning</span>
108 <a name="l00093"></a>00093 <span class="comment">/// Positions are specified by a signed long integer. At</span>
109 <a name="l00094"></a>00094 <span class="comment">/// construction time, the current position of the motor is consider to be 0. Positive</span>
110 <a name="l00095"></a>00095 <span class="comment">/// positions are clockwise from the initial position; negative positions are</span>
111 <a name="l00096"></a>00096 <span class="comment">/// anticlockwise. The curent position can be altered for instance after</span>
112 <a name="l00097"></a>00097 <span class="comment">/// initialization positioning.</span>
113 <a name="l00098"></a>00098 <span class="comment">///</span>
114 <a name="l00099"></a>00099 <span class="comment">/// \par Caveats</span>
115 <a name="l00100"></a>00100 <span class="comment">/// This is an open loop controller: If the motor stalls or is oversped,</span>
116 <a name="l00101"></a>00101 <span class="comment">/// AccelStepper will not have a correct </span>
117 <a name="l00102"></a>00102 <span class="comment">/// idea of where the motor really is (since there is no feedback of the motor's</span>
118 <a name="l00103"></a>00103 <span class="comment">/// real position. We only know where we _think_ it is, relative to the</span>
119 <a name="l00104"></a>00104 <span class="comment">/// initial starting point).</span>
120 <a name="l00105"></a>00105 <span class="comment">///</span>
121 <a name="l00106"></a>00106 <span class="comment">/// The fastest motor speed that can be reliably supported is 1000 steps per</span>
122 <a name="l00107"></a>00107 <span class="comment">/// second (1 step every millisecond). However any speed less than that down</span>
123 <a name="l00108"></a>00108 <span class="comment">/// to very slow speeds (much less than one per second) are supported,</span>
124 <a name="l00109"></a>00109 <span class="comment">/// provided the run() function is called frequently enough to step the</span>
125 <a name="l00110"></a>00110 <span class="comment">/// motor whenever required.</span>
126 <a name="l00111"></a><a class="code" href="classAccelStepper.html">00111</a> <span class="comment"></span><span class="keyword">class </span><a class="code" href="classAccelStepper.html" title="Support for stepper motors with acceleration etc.">AccelStepper</a>
127 <a name="l00112"></a>00112 {
128 <a name="l00113"></a>00113 <span class="keyword">public</span>:<span class="comment"></span>
129 <a name="l00114"></a>00114 <span class="comment"> /// Constructor. You can have multiple simultaneous steppers, all moving</span>
130 <a name="l00115"></a>00115 <span class="comment"> /// at different speeds and accelerations, provided you call their run()</span>
131 <a name="l00116"></a>00116 <span class="comment"> /// functions at frequent enough intervals. Current Position is set to 0, target</span>
132 <a name="l00117"></a>00117 <span class="comment"> /// position is set to 0. MaxSpeed and Acceleration default to 1.0.</span>
133 <a name="l00118"></a>00118 <span class="comment"> /// The motor pins will be initialised to OUTPUT mode during the</span>
134 <a name="l00119"></a>00119 <span class="comment"> /// constructor by a call to enableOutputs().</span>
135 <a name="l00120"></a>00120 <span class="comment"> /// \param[in] pins Number of pins to interface to. 1, 2 or 4 are</span>
136 <a name="l00121"></a>00121 <span class="comment"> /// supported. 1 means a stepper driver (with Step and Direction pins)</span>
137 <a name="l00122"></a>00122 <span class="comment"> /// 2 means a 2 wire stepper. 4 means a 4 wire stepper.</span>
138 <a name="l00123"></a>00123 <span class="comment"> /// Defaults to 4 pins.</span>
139 <a name="l00124"></a>00124 <span class="comment"> /// \param[in] pin1 Arduino digital pin number for motor pin 1. Defaults</span>
140 <a name="l00125"></a>00125 <span class="comment"> /// to pin 2. For a driver (pins==1), this is the Step input to the driver. Low to high transition means to step)</span>
141 <a name="l00126"></a>00126 <span class="comment"> /// \param[in] pin2 Arduino digital pin number for motor pin 2. Defaults</span>
142 <a name="l00127"></a>00127 <span class="comment"> /// to pin 3. For a driver (pins==1), this is the Direction input the driver. High means forward.</span>
143 <a name="l00128"></a>00128 <span class="comment"> /// \param[in] pin3 Arduino digital pin number for motor pin 3. Defaults</span>
144 <a name="l00129"></a>00129 <span class="comment"> /// to pin 4.</span>
145 <a name="l00130"></a>00130 <span class="comment"> /// \param[in] pin4 Arduino digital pin number for motor pin 4. Defaults</span>
146 <a name="l00131"></a>00131 <span class="comment"> /// to pin 5.</span>
147 <a name="l00132"></a>00132 <span class="comment"></span> <a class="code" href="classAccelStepper.html#a1290897df35915069e5eca9d034038c">AccelStepper</a>(uint8_t pins = 4, uint8_t pin1 = 2, uint8_t pin2 = 3, uint8_t pin3 = 4, uint8_t pin4 = 5);
148 <a name="l00133"></a>00133 <span class="comment"></span>
149 <a name="l00134"></a>00134 <span class="comment"> /// Set the target position. The run() function will try to move the motor</span>
150 <a name="l00135"></a>00135 <span class="comment"> /// from the current position to the target position set by the most</span>
151 <a name="l00136"></a>00136 <span class="comment"> /// recent call to this function.</span>
152 <a name="l00137"></a>00137 <span class="comment"> /// \param[in] absolute The desired absolute position. Negative is</span>
153 <a name="l00138"></a>00138 <span class="comment"> /// anticlockwise from the 0 position.</span>
154 <a name="l00139"></a>00139 <span class="comment"></span> <span class="keywordtype">void</span> <a class="code" href="classAccelStepper.html#ce236ede35f87c63d18da25810ec9736">moveTo</a>(<span class="keywordtype">long</span> absolute);
155 <a name="l00140"></a>00140 <span class="comment"></span>
156 <a name="l00141"></a>00141 <span class="comment"> /// Set the target position relative to the current position</span>
157 <a name="l00142"></a>00142 <span class="comment"> /// \param[in] relative The desired position relative to the current position. Negative is</span>
158 <a name="l00143"></a>00143 <span class="comment"> /// anticlockwise from the current position.</span>
159 <a name="l00144"></a>00144 <span class="comment"></span> <span class="keywordtype">void</span> <a class="code" href="classAccelStepper.html#68942c66e78fb7f7b5f0cdade6eb7f06">move</a>(<span class="keywordtype">long</span> relative);
160 <a name="l00145"></a>00145 <span class="comment"></span>
161 <a name="l00146"></a>00146 <span class="comment"> /// Poll the motor and step it if a step is due, implementing</span>
162 <a name="l00147"></a>00147 <span class="comment"> /// accelerations and decelerations to achive the ratget position. You must call this as</span>
163 <a name="l00148"></a>00148 <span class="comment"> /// fequently as possible, but at least once per minimum step interval,</span>
164 <a name="l00149"></a>00149 <span class="comment"> /// preferably in your main loop.</span>
165 <a name="l00150"></a>00150 <span class="comment"> /// \return true if the motor is at the target position.</span>
166 <a name="l00151"></a>00151 <span class="comment"></span> <span class="keywordtype">boolean</span> <a class="code" href="classAccelStepper.html#608b2395b64ac15451d16d0371fe13ce">run</a>();
167 <a name="l00152"></a>00152 <span class="comment"></span>
168 <a name="l00153"></a>00153 <span class="comment"> /// Poll the motor and step it if a step is due, implmenting a constant</span>
169 <a name="l00154"></a>00154 <span class="comment"> /// speed as set by the most recent call to setSpeed().</span>
170 <a name="l00155"></a>00155 <span class="comment"> /// \return true if the motor was stepped.</span>
171 <a name="l00156"></a>00156 <span class="comment"></span> <span class="keywordtype">boolean</span> <a class="code" href="classAccelStepper.html#a4a6bdf99f698284faaeb5542b0b7514">runSpeed</a>();
172 <a name="l00157"></a>00157 <span class="comment"></span>
173 <a name="l00158"></a>00158 <span class="comment"> /// Sets the maximum permitted speed. the run() function will accelerate</span>
174 <a name="l00159"></a>00159 <span class="comment"> /// up to the speed set by this function.</span>
175 <a name="l00160"></a>00160 <span class="comment"> /// \param[in] speed The desired maximum speed in steps per second. Must</span>
176 <a name="l00161"></a>00161 <span class="comment"> /// be &gt; 0. Speeds of more than 1000 steps per second are unreliable. </span>
177 <a name="l00162"></a>00162 <span class="comment"></span> <span class="keywordtype">void</span> <a class="code" href="classAccelStepper.html#bee8d466229b87accba33d6ec929c18f">setMaxSpeed</a>(<span class="keywordtype">float</span> <a class="code" href="classAccelStepper.html#4f0989d0ae264e7eadfe1fa720769fb6">speed</a>);
178 <a name="l00163"></a>00163 <span class="comment"></span>
179 <a name="l00164"></a>00164 <span class="comment"> /// Sets the acceleration and deceleration parameter.</span>
180 <a name="l00165"></a>00165 <span class="comment"> /// \param[in] acceleration The desired acceleration in steps per second</span>
181 <a name="l00166"></a>00166 <span class="comment"> /// per second. Must be &gt; 0.</span>
182 <a name="l00167"></a>00167 <span class="comment"></span> <span class="keywordtype">void</span> <a class="code" href="classAccelStepper.html#dfb19e3cd2a028a1fe78131787604fd1">setAcceleration</a>(<span class="keywordtype">float</span> acceleration);
183 <a name="l00168"></a>00168 <span class="comment"></span>
184 <a name="l00169"></a>00169 <span class="comment"> /// Sets the desired constant speed for use with runSpeed().</span>
185 <a name="l00170"></a>00170 <span class="comment"> /// \param[in] speed The desired constant speed in steps per</span>
186 <a name="l00171"></a>00171 <span class="comment"> /// second. Positive is clockwise. Speeds of more than 1000 steps per</span>
187 <a name="l00172"></a>00172 <span class="comment"> /// second are unreliable. Very slow speeds may be set (eg 0.00027777 for</span>
188 <a name="l00173"></a>00173 <span class="comment"> /// once per hour, approximately. Speed accuracy depends on the Arduino</span>
189 <a name="l00174"></a>00174 <span class="comment"> /// crystal. Jitter depends on how frequently you call the runSpeed() function.</span>
190 <a name="l00175"></a>00175 <span class="comment"></span> <span class="keywordtype">void</span> <a class="code" href="classAccelStepper.html#e79c49ad69d5ccc9da0ee691fa4ca235">setSpeed</a>(<span class="keywordtype">float</span> speed);
191 <a name="l00176"></a>00176 <span class="comment"></span>
192 <a name="l00177"></a>00177 <span class="comment"> /// The most recently set speed</span>
193 <a name="l00178"></a>00178 <span class="comment"> /// \return the most recent speed in steps per second</span>
194 <a name="l00179"></a>00179 <span class="comment"></span> <span class="keywordtype">float</span> <a class="code" href="classAccelStepper.html#4f0989d0ae264e7eadfe1fa720769fb6">speed</a>();
195 <a name="l00180"></a>00180 <span class="comment"></span>
196 <a name="l00181"></a>00181 <span class="comment"> /// The distance from the current position to the target position.</span>
197 <a name="l00182"></a>00182 <span class="comment"> /// \return the distance from the current position to the target position</span>
198 <a name="l00183"></a>00183 <span class="comment"> /// in steps. Positive is clockwise from the current position.</span>
199 <a name="l00184"></a>00184 <span class="comment"></span> <span class="keywordtype">long</span> <a class="code" href="classAccelStepper.html#748665c3962e66fbc0e9373eb14c69c1">distanceToGo</a>();
200 <a name="l00185"></a>00185 <span class="comment"></span>
201 <a name="l00186"></a>00186 <span class="comment"> /// The most recently set target position.</span>
202 <a name="l00187"></a>00187 <span class="comment"> /// \return the target position</span>
203 <a name="l00188"></a>00188 <span class="comment"> /// in steps. Positive is clockwise from the 0 position.</span>
204 <a name="l00189"></a>00189 <span class="comment"></span> <span class="keywordtype">long</span> <a class="code" href="classAccelStepper.html#96685e0945b7cf75d5959da679cd911e">targetPosition</a>();
205 <a name="l00190"></a>00190
206 <a name="l00191"></a>00191 <span class="comment"></span>
207 <a name="l00192"></a>00192 <span class="comment"> /// The currently motor position.</span>
208 <a name="l00193"></a>00193 <span class="comment"> /// \return the current motor position</span>
209 <a name="l00194"></a>00194 <span class="comment"> /// in steps. Positive is clockwise from the 0 position.</span>
210 <a name="l00195"></a>00195 <span class="comment"></span> <span class="keywordtype">long</span> <a class="code" href="classAccelStepper.html#5dce13ab2a1b02b8f443318886bf6fc5">currentPosition</a>();
211 <a name="l00196"></a>00196 <span class="comment"></span>
212 <a name="l00197"></a>00197 <span class="comment"> /// Resets the current position of the motor, so that wherever the mottor</span>
213 <a name="l00198"></a>00198 <span class="comment"> /// happens to be right now is considered to be the new position. Useful</span>
214 <a name="l00199"></a>00199 <span class="comment"> /// for setting a zero position on a stepper after an initial hardware</span>
215 <a name="l00200"></a>00200 <span class="comment"> /// positioning move.</span>
216 <a name="l00201"></a>00201 <span class="comment"> /// \param[in] position The position in steps of wherever the motor</span>
217 <a name="l00202"></a>00202 <span class="comment"> /// happens to be right now.</span>
218 <a name="l00203"></a>00203 <span class="comment"></span> <span class="keywordtype">void</span> <a class="code" href="classAccelStepper.html#9d917f014317fb9d3b5dc14e66f6c689">setCurrentPosition</a>(<span class="keywordtype">long</span> position);
219 <a name="l00204"></a>00204 <span class="comment"></span>
220 <a name="l00205"></a>00205 <span class="comment"> /// Moves the motor to the target position and blocks until it is at</span>
221 <a name="l00206"></a>00206 <span class="comment"> /// position. Dont use this in event loops, since it blocks.</span>
222 <a name="l00207"></a>00207 <span class="comment"></span> <span class="keywordtype">void</span> <a class="code" href="classAccelStepper.html#344f58fef8cc34ac5aa75ba4b665d21c">runToPosition</a>();
223 <a name="l00208"></a>00208 <span class="comment"></span>
224 <a name="l00209"></a>00209 <span class="comment"> /// Runs at the currently selected speed until the target position is reached</span>
225 <a name="l00210"></a>00210 <span class="comment"> /// Does not implement accelerations.</span>
226 <a name="l00211"></a>00211 <span class="comment"></span> <span class="keywordtype">boolean</span> <a class="code" href="classAccelStepper.html#9270d20336e76ac1fd5bcd5b9c34f301">runSpeedToPosition</a>();
227 <a name="l00212"></a>00212 <span class="comment"></span>
228 <a name="l00213"></a>00213 <span class="comment"> /// Moves the motor to the new target position and blocks until it is at</span>
229 <a name="l00214"></a>00214 <span class="comment"> /// position. Dont use this in event loops, since it blocks.</span>
230 <a name="l00215"></a>00215 <span class="comment"> /// \param[in] position The new target position.</span>
231 <a name="l00216"></a>00216 <span class="comment"></span> <span class="keywordtype">void</span> <a class="code" href="classAccelStepper.html#176c5d2e4c2f21e9e92b12e39a6f0e67">runToNewPosition</a>(<span class="keywordtype">long</span> position);
232 <a name="l00217"></a>00217 <span class="comment"></span>
233 <a name="l00218"></a>00218 <span class="comment"> /// Disable motor pin outputs by setting them all LOW</span>
234 <a name="l00219"></a>00219 <span class="comment"> /// Depending on the design of your electronics this may turn off</span>
235 <a name="l00220"></a>00220 <span class="comment"> /// the power to the motor coils, saving power.</span>
236 <a name="l00221"></a>00221 <span class="comment"> /// This is useful to support Arduino low power modes: disable the outputs</span>
237 <a name="l00222"></a>00222 <span class="comment"> /// during sleep and then reenable with enableOutputs() before stepping</span>
238 <a name="l00223"></a>00223 <span class="comment"> /// again.</span>
239 <a name="l00224"></a>00224 <span class="comment"></span> <span class="keywordtype">void</span> <a class="code" href="classAccelStepper.html#3591e29a236e2935afd7f64ff6c22006">disableOutputs</a>();
240 <a name="l00225"></a>00225 <span class="comment"></span>
241 <a name="l00226"></a>00226 <span class="comment"> /// Enable motor pin outputs by setting the motor pins to OUTPUT</span>
242 <a name="l00227"></a>00227 <span class="comment"> /// mode. Called automatically by the constructor.</span>
243 <a name="l00228"></a>00228 <span class="comment"></span> <span class="keywordtype">void</span> <a class="code" href="classAccelStepper.html#a279a50d30d0413f570c692cff071643">enableOutputs</a>();
244 <a name="l00229"></a>00229
245 <a name="l00230"></a>00230 <span class="keyword">protected</span>:
246 <a name="l00231"></a>00231 <span class="comment"></span>
247 <a name="l00232"></a>00232 <span class="comment"> /// Forces the library to compute a new instantaneous speed and set that as</span>
248 <a name="l00233"></a>00233 <span class="comment"> /// the current speed. Calls</span>
249 <a name="l00234"></a>00234 <span class="comment"> /// desiredSpeed(), which can be overridden by subclasses. It is called by</span>
250 <a name="l00235"></a>00235 <span class="comment"> /// the library:</span>
251 <a name="l00236"></a>00236 <span class="comment"> /// \li after each step</span>
252 <a name="l00237"></a>00237 <span class="comment"> /// \li after change to maxSpeed through setMaxSpeed()</span>
253 <a name="l00238"></a>00238 <span class="comment"> /// \li after change to acceleration through setAcceleration()</span>
254 <a name="l00239"></a>00239 <span class="comment"> /// \li after change to target position (relative or absolute) through</span>
255 <a name="l00240"></a>00240 <span class="comment"> /// move() or moveTo()</span>
256 <a name="l00241"></a>00241 <span class="comment"></span> <span class="keywordtype">void</span> <a class="code" href="classAccelStepper.html#ffbee789b5c19165846cf0409860ae79">computeNewSpeed</a>();
257 <a name="l00242"></a>00242 <span class="comment"></span>
258 <a name="l00243"></a>00243 <span class="comment"> /// Called to execute a step. Only called when a new step is</span>
259 <a name="l00244"></a>00244 <span class="comment"> /// required. Subclasses may override to implement new stepping</span>
260 <a name="l00245"></a>00245 <span class="comment"> /// interfaces. The default calls step1(), step2() or step4() depending on the</span>
261 <a name="l00246"></a>00246 <span class="comment"> /// number of pins defined for the stepper.</span>
262 <a name="l00247"></a>00247 <span class="comment"> /// \param[in] step The current step phase number (0 to 3)</span>
263 <a name="l00248"></a>00248 <span class="comment"></span> <span class="keyword">virtual</span> <span class="keywordtype">void</span> <a class="code" href="classAccelStepper.html#3c9a220819d2451f79ff8a0c0a395b9f">step</a>(uint8_t <a class="code" href="classAccelStepper.html#3c9a220819d2451f79ff8a0c0a395b9f">step</a>);
264 <a name="l00249"></a>00249
265 <a name="l00250"></a>00250 <span class="comment"></span>
266 <a name="l00251"></a>00251 <span class="comment"> /// Called to execute a step on a stepper drover (ie where pins == 1). Only called when a new step is</span>
267 <a name="l00252"></a>00252 <span class="comment"> /// required. Subclasses may override to implement new stepping</span>
268 <a name="l00253"></a>00253 <span class="comment"> /// interfaces. The default sets or clears the outputs of Step pin1 to step, </span>
269 <a name="l00254"></a>00254 <span class="comment"> /// and sets the output of _pin2 to the desired direction. The Step pin (_pin1) is pulsed for 1 microsecond</span>
270 <a name="l00255"></a>00255 <span class="comment"> /// which is the minimum STEP pulse width for the 3967 driver.</span>
271 <a name="l00256"></a>00256 <span class="comment"> /// \param[in] step The current step phase number (0 to 3)</span>
272 <a name="l00257"></a>00257 <span class="comment"></span> <span class="keyword">virtual</span> <span class="keywordtype">void</span> <a class="code" href="classAccelStepper.html#cc64254ea242b53588e948335fd9305f">step1</a>(uint8_t step);
273 <a name="l00258"></a>00258 <span class="comment"></span>
274 <a name="l00259"></a>00259 <span class="comment"> /// Called to execute a step on a 2 pin motor. Only called when a new step is</span>
275 <a name="l00260"></a>00260 <span class="comment"> /// required. Subclasses may override to implement new stepping</span>
276 <a name="l00261"></a>00261 <span class="comment"> /// interfaces. The default sets or clears the outputs of pin1 and pin2</span>
277 <a name="l00262"></a>00262 <span class="comment"> /// \param[in] step The current step phase number (0 to 3)</span>
278 <a name="l00263"></a>00263 <span class="comment"></span> <span class="keyword">virtual</span> <span class="keywordtype">void</span> <a class="code" href="classAccelStepper.html#88f11bf6361fe002585f731d34fe2e8b">step2</a>(uint8_t step);
279 <a name="l00264"></a>00264 <span class="comment"></span>
280 <a name="l00265"></a>00265 <span class="comment"> /// Called to execute a step on a 4 pin motor. Only called when a new step is</span>
281 <a name="l00266"></a>00266 <span class="comment"> /// required. Subclasses may override to implement new stepping</span>
282 <a name="l00267"></a>00267 <span class="comment"> /// interfaces. The default sets or clears the outputs of pin1, pin2,</span>
283 <a name="l00268"></a>00268 <span class="comment"> /// pin3, pin4.</span>
284 <a name="l00269"></a>00269 <span class="comment"> /// \param[in] step The current step phase number (0 to 3)</span>
285 <a name="l00270"></a>00270 <span class="comment"></span> <span class="keyword">virtual</span> <span class="keywordtype">void</span> <a class="code" href="classAccelStepper.html#49e448d179bbe4e0f8003a3f9993789d">step4</a>(uint8_t step);
286 <a name="l00271"></a>00271 <span class="comment"></span>
287 <a name="l00272"></a>00272 <span class="comment"> /// Compute and return the desired speed. The default algorithm uses</span>
288 <a name="l00273"></a>00273 <span class="comment"> /// maxSpeed, acceleration and the current speed to set a new speed to</span>
289 <a name="l00274"></a>00274 <span class="comment"> /// move the motor from teh current position to the target</span>
290 <a name="l00275"></a>00275 <span class="comment"> /// position. Subclasses may override this to provide an alternate</span>
291 <a name="l00276"></a>00276 <span class="comment"> /// algorithm (but do not block). Called by computeNewSpeed whenever a new speed neds to be</span>
292 <a name="l00277"></a>00277 <span class="comment"> /// computed. </span>
293 <a name="l00278"></a>00278 <span class="comment"></span> <span class="keyword">virtual</span> <span class="keywordtype">float</span> <a class="code" href="classAccelStepper.html#6e4bd79c395e69beee31d76d0d3287e4">desiredSpeed</a>();
294 <a name="l00279"></a>00279
295 <a name="l00280"></a>00280 <span class="keyword">private</span>:<span class="comment"></span>
296 <a name="l00281"></a>00281 <span class="comment"> /// Number of pins on the stepper motor. Permits 2 or 4. 2 pins is a</span>
297 <a name="l00282"></a>00282 <span class="comment"> /// bipolar, and 4 pins is a unipolar.</span>
298 <a name="l00283"></a>00283 <span class="comment"></span> uint8_t _pins; <span class="comment">// 2 or 4</span>
299 <a name="l00284"></a>00284 <span class="comment"></span>
300 <a name="l00285"></a>00285 <span class="comment"> /// Arduino pin number for the 2 or 4 pins required to interface to the</span>
301 <a name="l00286"></a>00286 <span class="comment"> /// stepper motor.</span>
302 <a name="l00287"></a>00287 <span class="comment"></span> uint8_t _pin1, _pin2, _pin3, _pin4;
303 <a name="l00288"></a>00288 <span class="comment"></span>
304 <a name="l00289"></a>00289 <span class="comment"> /// The current absolution position in steps.</span>
305 <a name="l00290"></a>00290 <span class="comment"></span> <span class="keywordtype">long</span> _currentPos; <span class="comment">// Steps</span>
306 <a name="l00291"></a>00291 <span class="comment"></span>
307 <a name="l00292"></a>00292 <span class="comment"> /// The target position in steps. The AccelStepper library will move the</span>
308 <a name="l00293"></a>00293 <span class="comment"> /// motor from teh _currentPos to the _targetPos, taking into account the</span>
309 <a name="l00294"></a>00294 <span class="comment"> /// max speed, acceleration and deceleration</span>
310 <a name="l00295"></a>00295 <span class="comment"></span> <span class="keywordtype">long</span> _targetPos; <span class="comment">// Steps</span>
311 <a name="l00296"></a>00296 <span class="comment"></span>
312 <a name="l00297"></a>00297 <span class="comment"> /// The current motos speed in steps per second</span>
313 <a name="l00298"></a>00298 <span class="comment"> /// Positive is clockwise</span>
314 <a name="l00299"></a>00299 <span class="comment"></span> <span class="keywordtype">float</span> _speed; <span class="comment">// Steps per second</span>
315 <a name="l00300"></a>00300 <span class="comment"></span>
316 <a name="l00301"></a>00301 <span class="comment"> /// The maximum permitted speed in steps per second. Must be &gt; 0.</span>
317 <a name="l00302"></a>00302 <span class="comment"></span> <span class="keywordtype">float</span> _maxSpeed;
318 <a name="l00303"></a>00303 <span class="comment"></span>
319 <a name="l00304"></a>00304 <span class="comment"> /// The acceleration to use to accelerate or decelerate the motor in steps</span>
320 <a name="l00305"></a>00305 <span class="comment"> /// per second per second. Must be &gt; 0</span>
321 <a name="l00306"></a>00306 <span class="comment"></span> <span class="keywordtype">float</span> _acceleration;
322 <a name="l00307"></a>00307 <span class="comment"></span>
323 <a name="l00308"></a>00308 <span class="comment"> /// The current interval between steps in milliseconds.</span>
324 <a name="l00309"></a>00309 <span class="comment"></span> <span class="keywordtype">unsigned</span> <span class="keywordtype">long</span> _stepInterval;
325 <a name="l00310"></a>00310 <span class="comment"></span>
326 <a name="l00311"></a>00311 <span class="comment"> /// The last step time in milliseconds</span>
327 <a name="l00312"></a>00312 <span class="comment"></span> <span class="keywordtype">unsigned</span> <span class="keywordtype">long</span> _lastStepTime;
328 <a name="l00313"></a>00313 };
329 <a name="l00314"></a>00314
330 <a name="l00315"></a>00315 <span class="preprocessor">#endif </span>
331 </pre></div></div>
332 <hr size="1"><address style="text-align: right;"><small>Generated on Sun Oct 24 18:22:50 2010 for AccelStepper by&nbsp;
333 <a href="">
334 <img src="doxygen.png" alt="doxygen" align="middle" border="0"></a> 1.5.6 </small></address>
335 </body>
336 </html>
Something went wrong with that request. Please try again.