ESP_IOT v2.5
IOT ESP Coding
Stepper.cpp
Go to the documentation of this file.
1/*
2 * Stepper.cpp - Stepper library for Wiring/Arduino - Version 1.1.0
3 *
4 * Original library (0.1) by Tom Igoe.
5 * Two-wire modifications (0.2) by Sebastian Gassner
6 * Combination version (0.3) by Tom Igoe and David Mellis
7 * Bug fix for four-wire (0.4) by Tom Igoe, bug fix from Noah Shibley
8 * High-speed stepping mod by Eugene Kozlenko
9 * Timer rollover fix by Eugene Kozlenko
10 * Five phase five wire (1.1.0) by Ryan Orendorff
11 *
12 * This library is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU Lesser General Public
14 * License as published by the Free Software Foundation; either
15 * version 2.1 of the License, or (at your option) any later version.
16 *
17 * This library is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20 * Lesser General Public License for more details.
21 *
22 * You should have received a copy of the GNU Lesser General Public
23 * License along with this library; if not, write to the Free Software
24 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
25 *
26 *
27 * Drives a unipolar, bipolar, or five phase stepper motor.
28 *
29 * When wiring multiple stepper motors to a microcontroller, you quickly run
30 * out of output pins, with each motor requiring 4 connections.
31 *
32 * By making use of the fact that at any time two of the four motor coils are
33 * the inverse of the other two, the number of control connections can be
34 * reduced from 4 to 2 for the unipolar and bipolar motors.
35 *
36 * A slightly modified circuit around a Darlington transistor array or an
37 * L293 H-bridge connects to only 2 microcontroler pins, inverts the signals
38 * received, and delivers the 4 (2 plus 2 inverted ones) output signals
39 * required for driving a stepper motor. Similarly the Arduino motor shields
40 * 2 direction pins may be used.
41 *
42 * The sequence of control signals for 5 phase, 5 control wires is as follows:
43 *
44 * Step C0 C1 C2 C3 C4
45 * 1 0 1 1 0 1
46 * 2 0 1 0 0 1
47 * 3 0 1 0 1 1
48 * 4 0 1 0 1 0
49 * 5 1 1 0 1 0
50 * 6 1 0 0 1 0
51 * 7 1 0 1 1 0
52 * 8 1 0 1 0 0
53 * 9 1 0 1 0 1
54 * 10 0 0 1 0 1
55 *
56 * The sequence of control signals for 4 control wires is as follows:
57 *
58 * Step C0 C1 C2 C3
59 * 1 1 0 1 0
60 * 2 0 1 1 0
61 * 3 0 1 0 1
62 * 4 1 0 0 1
63 *
64 * The sequence of controls signals for 2 control wires is as follows
65 * (columns C1 and C2 from above):
66 *
67 * Step C0 C1
68 * 1 0 1
69 * 2 1 1
70 * 3 1 0
71 * 4 0 0
72 *
73 * The circuits can be found at
74 *
75 * http://www.arduino.cc/en/Tutorial/Stepper
76 */
77
78#include "Arduino.h"
79#include "Stepper.h"
80
81/*
82 * two-wire constructor.
83 * Sets which wires should control the motor.
84 */
85Stepper::Stepper(int number_of_steps, int motor_pin_1, int motor_pin_2)
86{
87 this->step_number = 0; // which step the motor is on
88 this->direction = 0; // motor direction
89 this->last_step_time = 0; // time stamp in us of the last step taken
90 this->number_of_steps = number_of_steps; // total number of steps for this motor
91
92 // Arduino pins for the motor control connection:
93 this->motor_pin_1 = motor_pin_1;
94 this->motor_pin_2 = motor_pin_2;
95
96 // setup the pins on the microcontroller:
97 pinMode(this->motor_pin_1, OUTPUT);
98 pinMode(this->motor_pin_2, OUTPUT);
99
100 // When there are only 2 pins, set the others to 0:
101 this->motor_pin_3 = 0;
102 this->motor_pin_4 = 0;
103 this->motor_pin_5 = 0;
104
105 // pin_count is used by the stepMotor() method:
106 this->pin_count = 2;
107}
108
109
110/*
111 * constructor for four-pin version
112 * Sets which wires should control the motor.
113 */
114Stepper::Stepper(int number_of_steps, int motor_pin_1, int motor_pin_2,
115 int motor_pin_3, int motor_pin_4)
116{
117 this->step_number = 0; // which step the motor is on
118 this->direction = 0; // motor direction
119 this->last_step_time = 0; // time stamp in us of the last step taken
120 this->number_of_steps = number_of_steps; // total number of steps for this motor
121
122 // Arduino pins for the motor control connection:
123 this->motor_pin_1 = motor_pin_1;
124 this->motor_pin_2 = motor_pin_2;
125 this->motor_pin_3 = motor_pin_3;
126 this->motor_pin_4 = motor_pin_4;
127
128 // setup the pins on the microcontroller:
129 pinMode(this->motor_pin_1, OUTPUT);
130 pinMode(this->motor_pin_2, OUTPUT);
131 pinMode(this->motor_pin_3, OUTPUT);
132 pinMode(this->motor_pin_4, OUTPUT);
133
134 // When there are 4 pins, set the others to 0:
135 this->motor_pin_5 = 0;
136
137 // pin_count is used by the stepMotor() method:
138 this->pin_count = 4;
139}
140
141/*
142 * constructor for five phase motor with five wires
143 * Sets which wires should control the motor.
144 */
145Stepper::Stepper(int number_of_steps, int motor_pin_1, int motor_pin_2,
146 int motor_pin_3, int motor_pin_4,
147 int motor_pin_5)
148{
149 this->step_number = 0; // which step the motor is on
150 this->direction = 0; // motor direction
151 this->last_step_time = 0; // time stamp in us of the last step taken
152 this->number_of_steps = number_of_steps; // total number of steps for this motor
153
154 // Arduino pins for the motor control connection:
155 this->motor_pin_1 = motor_pin_1;
156 this->motor_pin_2 = motor_pin_2;
157 this->motor_pin_3 = motor_pin_3;
158 this->motor_pin_4 = motor_pin_4;
159 this->motor_pin_5 = motor_pin_5;
160
161 // setup the pins on the microcontroller:
162 pinMode(this->motor_pin_1, OUTPUT);
163 pinMode(this->motor_pin_2, OUTPUT);
164 pinMode(this->motor_pin_3, OUTPUT);
165 pinMode(this->motor_pin_4, OUTPUT);
166 pinMode(this->motor_pin_5, OUTPUT);
167
168 // pin_count is used by the stepMotor() method:
169 this->pin_count = 5;
170}
171
172/*
173 * Sets the speed in revs per minute
174 */
175void Stepper::setSpeed(long whatSpeed)
176{
177 //! if 0 then blows chow..
178 if (whatSpeed == 0)
179 whatSpeed = 15;
180 if (this->number_of_steps == 0)
181 this->number_of_steps = 2048;
182
183 this->step_delay = 60L * 1000L * 1000L / this->number_of_steps / whatSpeed;
184}
185
186//! 8.14.25 set speed and number_of_steps
187void Stepper::setSpeedSteps(long whatSpeed, int number_of_steps)
188{
189 this->number_of_steps = number_of_steps;
190 Stepper::setSpeed(whatSpeed);
191}
192
193
194/*
195 * Moves the motor steps_to_move steps. If the number is negative,
196 * the motor moves in the reverse direction.
197 */
198void Stepper::step(int steps_to_move)
199{
200 int steps_left = abs(steps_to_move); // how many steps to take
201
202 // determine direction based on whether steps_to_mode is + or -:
203 if (steps_to_move > 0) { this->direction = 1; }
204 if (steps_to_move < 0) { this->direction = 0; }
205
206
207 // decrement the number of steps, moving one step each time:
208 while (steps_left > 0)
209 {
210 unsigned long now = micros();
211 // move only if the appropriate delay has passed:
212 if (now - this->last_step_time >= this->step_delay)
213 {
214 // get the timeStamp of when you stepped:
215 this->last_step_time = now;
216 // increment or decrement the step number,
217 // depending on direction:
218 if (this->direction == 1)
219 {
220 this->step_number++;
221 if (this->step_number == this->number_of_steps) {
222 this->step_number = 0;
223 }
224 }
225 else
226 {
227 if (this->step_number == 0) {
228 this->step_number = this->number_of_steps;
229 }
230 this->step_number--;
231 }
232 // decrement the steps left:
233 steps_left--;
234 // step the motor to step number 0, 1, ..., {3 or 10}
235 if (this->pin_count == 5)
236 stepMotor(this->step_number % 10);
237 else
238 stepMotor(this->step_number % 4);
239 }
240 }
241}
242
243/*
244 * Moves the motor forward or backwards.
245 */
246void Stepper::stepMotor(int thisStep)
247{
248 if (this->pin_count == 2) {
249 switch (thisStep) {
250 case 0: // 01
251 digitalWrite(motor_pin_1, LOW);
252 digitalWrite(motor_pin_2, HIGH);
253 break;
254 case 1: // 11
255 digitalWrite(motor_pin_1, HIGH);
256 digitalWrite(motor_pin_2, HIGH);
257 break;
258 case 2: // 10
259 digitalWrite(motor_pin_1, HIGH);
260 digitalWrite(motor_pin_2, LOW);
261 break;
262 case 3: // 00
263 digitalWrite(motor_pin_1, LOW);
264 digitalWrite(motor_pin_2, LOW);
265 break;
266 }
267 }
268 if (this->pin_count == 4) {
269 switch (thisStep) {
270 case 0: // 1010
271 digitalWrite(motor_pin_1, HIGH);
272 digitalWrite(motor_pin_2, LOW);
273 digitalWrite(motor_pin_3, HIGH);
274 digitalWrite(motor_pin_4, LOW);
275 break;
276 case 1: // 0110
277 digitalWrite(motor_pin_1, LOW);
278 digitalWrite(motor_pin_2, HIGH);
279 digitalWrite(motor_pin_3, HIGH);
280 digitalWrite(motor_pin_4, LOW);
281 break;
282 case 2: //0101
283 digitalWrite(motor_pin_1, LOW);
284 digitalWrite(motor_pin_2, HIGH);
285 digitalWrite(motor_pin_3, LOW);
286 digitalWrite(motor_pin_4, HIGH);
287 break;
288 case 3: //1001
289 digitalWrite(motor_pin_1, HIGH);
290 digitalWrite(motor_pin_2, LOW);
291 digitalWrite(motor_pin_3, LOW);
292 digitalWrite(motor_pin_4, HIGH);
293 break;
294 }
295 }
296
297 if (this->pin_count == 5) {
298 switch (thisStep) {
299 case 0: // 01101
300 digitalWrite(motor_pin_1, LOW);
301 digitalWrite(motor_pin_2, HIGH);
302 digitalWrite(motor_pin_3, HIGH);
303 digitalWrite(motor_pin_4, LOW);
304 digitalWrite(motor_pin_5, HIGH);
305 break;
306 case 1: // 01001
307 digitalWrite(motor_pin_1, LOW);
308 digitalWrite(motor_pin_2, HIGH);
309 digitalWrite(motor_pin_3, LOW);
310 digitalWrite(motor_pin_4, LOW);
311 digitalWrite(motor_pin_5, HIGH);
312 break;
313 case 2: // 01011
314 digitalWrite(motor_pin_1, LOW);
315 digitalWrite(motor_pin_2, HIGH);
316 digitalWrite(motor_pin_3, LOW);
317 digitalWrite(motor_pin_4, HIGH);
318 digitalWrite(motor_pin_5, HIGH);
319 break;
320 case 3: // 01010
321 digitalWrite(motor_pin_1, LOW);
322 digitalWrite(motor_pin_2, HIGH);
323 digitalWrite(motor_pin_3, LOW);
324 digitalWrite(motor_pin_4, HIGH);
325 digitalWrite(motor_pin_5, LOW);
326 break;
327 case 4: // 11010
328 digitalWrite(motor_pin_1, HIGH);
329 digitalWrite(motor_pin_2, HIGH);
330 digitalWrite(motor_pin_3, LOW);
331 digitalWrite(motor_pin_4, HIGH);
332 digitalWrite(motor_pin_5, LOW);
333 break;
334 case 5: // 10010
335 digitalWrite(motor_pin_1, HIGH);
336 digitalWrite(motor_pin_2, LOW);
337 digitalWrite(motor_pin_3, LOW);
338 digitalWrite(motor_pin_4, HIGH);
339 digitalWrite(motor_pin_5, LOW);
340 break;
341 case 6: // 10110
342 digitalWrite(motor_pin_1, HIGH);
343 digitalWrite(motor_pin_2, LOW);
344 digitalWrite(motor_pin_3, HIGH);
345 digitalWrite(motor_pin_4, HIGH);
346 digitalWrite(motor_pin_5, LOW);
347 break;
348 case 7: // 10100
349 digitalWrite(motor_pin_1, HIGH);
350 digitalWrite(motor_pin_2, LOW);
351 digitalWrite(motor_pin_3, HIGH);
352 digitalWrite(motor_pin_4, LOW);
353 digitalWrite(motor_pin_5, LOW);
354 break;
355 case 8: // 10101
356 digitalWrite(motor_pin_1, HIGH);
357 digitalWrite(motor_pin_2, LOW);
358 digitalWrite(motor_pin_3, HIGH);
359 digitalWrite(motor_pin_4, LOW);
360 digitalWrite(motor_pin_5, HIGH);
361 break;
362 case 9: // 00101
363 digitalWrite(motor_pin_1, LOW);
364 digitalWrite(motor_pin_2, LOW);
365 digitalWrite(motor_pin_3, HIGH);
366 digitalWrite(motor_pin_4, LOW);
367 digitalWrite(motor_pin_5, HIGH);
368 break;
369 }
370 }
371}
372
373/*
374 version() returns the version of the library:
375*/
377{
378 return 5;
379}
void setSpeedSteps(long whatSpeed, int number_of_steps)
8.14.25 set speed and number_of_steps
Definition: Stepper.cpp:187
void step(int number_of_steps)
Definition: Stepper.cpp:198
int version(void)
Definition: Stepper.cpp:376
Stepper(int number_of_steps, int motor_pin_1, int motor_pin_2)
Definition: Stepper.cpp:85
void setSpeed(long whatSpeed)
Definition: Stepper.cpp:175