root / arduino-1.0 / libraries / Stepper / Stepper.cpp @ 58d82c77
History | View | Annotate | Download (6.17 KB)
1 |
/*
|
---|---|
2 |
Stepper.cpp - - Stepper library for Wiring/Arduino - Version 0.4
|
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 |
|
9 |
Drives a unipolar or bipolar stepper motor using 2 wires or 4 wires
|
10 |
|
11 |
When wiring multiple stepper motors to a microcontroller,
|
12 |
you quickly run out of output pins, with each motor requiring 4 connections.
|
13 |
|
14 |
By making use of the fact that at any time two of the four motor
|
15 |
coils are the inverse of the other two, the number of
|
16 |
control connections can be reduced from 4 to 2.
|
17 |
|
18 |
A slightly modified circuit around a Darlington transistor array or an L293 H-bridge
|
19 |
connects to only 2 microcontroler pins, inverts the signals received,
|
20 |
and delivers the 4 (2 plus 2 inverted ones) output signals required
|
21 |
for driving a stepper motor.
|
22 |
|
23 |
The sequence of control signals for 4 control wires is as follows:
|
24 |
|
25 |
Step C0 C1 C2 C3
|
26 |
1 1 0 1 0
|
27 |
2 0 1 1 0
|
28 |
3 0 1 0 1
|
29 |
4 1 0 0 1
|
30 |
|
31 |
The sequence of controls signals for 2 control wires is as follows
|
32 |
(columns C1 and C2 from above):
|
33 |
|
34 |
Step C0 C1
|
35 |
1 0 1
|
36 |
2 1 1
|
37 |
3 1 0
|
38 |
4 0 0
|
39 |
|
40 |
The circuits can be found at
|
41 |
|
42 |
http://www.arduino.cc/en/Tutorial/Stepper
|
43 |
|
44 |
|
45 |
*/
|
46 |
|
47 |
|
48 |
#include "Arduino.h" |
49 |
#include "Stepper.h" |
50 |
|
51 |
/*
|
52 |
* two-wire constructor.
|
53 |
* Sets which wires should control the motor.
|
54 |
*/
|
55 |
Stepper::Stepper(int number_of_steps, int motor_pin_1, int motor_pin_2) |
56 |
{ |
57 |
this->step_number = 0; // which step the motor is on |
58 |
this->speed = 0; // the motor speed, in revolutions per minute |
59 |
this->direction = 0; // motor direction |
60 |
this->last_step_time = 0; // time stamp in ms of the last step taken |
61 |
this->number_of_steps = number_of_steps; // total number of steps for this motor |
62 |
|
63 |
// Arduino pins for the motor control connection:
|
64 |
this->motor_pin_1 = motor_pin_1;
|
65 |
this->motor_pin_2 = motor_pin_2;
|
66 |
|
67 |
// setup the pins on the microcontroller:
|
68 |
pinMode(this->motor_pin_1, OUTPUT);
|
69 |
pinMode(this->motor_pin_2, OUTPUT);
|
70 |
|
71 |
// When there are only 2 pins, set the other two to 0:
|
72 |
this->motor_pin_3 = 0; |
73 |
this->motor_pin_4 = 0; |
74 |
|
75 |
// pin_count is used by the stepMotor() method:
|
76 |
this->pin_count = 2; |
77 |
} |
78 |
|
79 |
|
80 |
/*
|
81 |
* constructor for four-pin version
|
82 |
* Sets which wires should control the motor.
|
83 |
*/
|
84 |
|
85 |
Stepper::Stepper(int number_of_steps, int motor_pin_1, int motor_pin_2, int motor_pin_3, int motor_pin_4) |
86 |
{ |
87 |
this->step_number = 0; // which step the motor is on |
88 |
this->speed = 0; // the motor speed, in revolutions per minute |
89 |
this->direction = 0; // motor direction |
90 |
this->last_step_time = 0; // time stamp in ms of the last step taken |
91 |
this->number_of_steps = number_of_steps; // total number of steps for this motor |
92 |
|
93 |
// Arduino pins for the motor control connection:
|
94 |
this->motor_pin_1 = motor_pin_1;
|
95 |
this->motor_pin_2 = motor_pin_2;
|
96 |
this->motor_pin_3 = motor_pin_3;
|
97 |
this->motor_pin_4 = motor_pin_4;
|
98 |
|
99 |
// setup the pins on the microcontroller:
|
100 |
pinMode(this->motor_pin_1, OUTPUT);
|
101 |
pinMode(this->motor_pin_2, OUTPUT);
|
102 |
pinMode(this->motor_pin_3, OUTPUT);
|
103 |
pinMode(this->motor_pin_4, OUTPUT);
|
104 |
|
105 |
// pin_count is used by the stepMotor() method:
|
106 |
this->pin_count = 4; |
107 |
} |
108 |
|
109 |
/*
|
110 |
Sets the speed in revs per minute
|
111 |
|
112 |
*/
|
113 |
void Stepper::setSpeed(long whatSpeed) |
114 |
{ |
115 |
this->step_delay = 60L * 1000L / this->number_of_steps / whatSpeed; |
116 |
} |
117 |
|
118 |
/*
|
119 |
Moves the motor steps_to_move steps. If the number is negative,
|
120 |
the motor moves in the reverse direction.
|
121 |
*/
|
122 |
void Stepper::step(int steps_to_move) |
123 |
{ |
124 |
int steps_left = abs(steps_to_move); // how many steps to take |
125 |
|
126 |
// determine direction based on whether steps_to_mode is + or -:
|
127 |
if (steps_to_move > 0) {this->direction = 1;} |
128 |
if (steps_to_move < 0) {this->direction = 0;} |
129 |
|
130 |
|
131 |
// decrement the number of steps, moving one step each time:
|
132 |
while(steps_left > 0) { |
133 |
// move only if the appropriate delay has passed:
|
134 |
if (millis() - this->last_step_time >= this->step_delay) { |
135 |
// get the timeStamp of when you stepped:
|
136 |
this->last_step_time = millis();
|
137 |
// increment or decrement the step number,
|
138 |
// depending on direction:
|
139 |
if (this->direction == 1) { |
140 |
this->step_number++;
|
141 |
if (this->step_number == this->number_of_steps) { |
142 |
this->step_number = 0; |
143 |
} |
144 |
} |
145 |
else {
|
146 |
if (this->step_number == 0) { |
147 |
this->step_number = this->number_of_steps; |
148 |
} |
149 |
this->step_number--;
|
150 |
} |
151 |
// decrement the steps left:
|
152 |
steps_left--; |
153 |
// step the motor to step number 0, 1, 2, or 3:
|
154 |
stepMotor(this->step_number % 4); |
155 |
} |
156 |
} |
157 |
} |
158 |
|
159 |
/*
|
160 |
* Moves the motor forward or backwards.
|
161 |
*/
|
162 |
void Stepper::stepMotor(int thisStep) |
163 |
{ |
164 |
if (this->pin_count == 2) { |
165 |
switch (thisStep) {
|
166 |
case 0: /* 01 */ |
167 |
digitalWrite(motor_pin_1, LOW); |
168 |
digitalWrite(motor_pin_2, HIGH); |
169 |
break;
|
170 |
case 1: /* 11 */ |
171 |
digitalWrite(motor_pin_1, HIGH); |
172 |
digitalWrite(motor_pin_2, HIGH); |
173 |
break;
|
174 |
case 2: /* 10 */ |
175 |
digitalWrite(motor_pin_1, HIGH); |
176 |
digitalWrite(motor_pin_2, LOW); |
177 |
break;
|
178 |
case 3: /* 00 */ |
179 |
digitalWrite(motor_pin_1, LOW); |
180 |
digitalWrite(motor_pin_2, LOW); |
181 |
break;
|
182 |
} |
183 |
} |
184 |
if (this->pin_count == 4) { |
185 |
switch (thisStep) {
|
186 |
case 0: // 1010 |
187 |
digitalWrite(motor_pin_1, HIGH); |
188 |
digitalWrite(motor_pin_2, LOW); |
189 |
digitalWrite(motor_pin_3, HIGH); |
190 |
digitalWrite(motor_pin_4, LOW); |
191 |
break;
|
192 |
case 1: // 0110 |
193 |
digitalWrite(motor_pin_1, LOW); |
194 |
digitalWrite(motor_pin_2, HIGH); |
195 |
digitalWrite(motor_pin_3, HIGH); |
196 |
digitalWrite(motor_pin_4, LOW); |
197 |
break;
|
198 |
case 2: //0101 |
199 |
digitalWrite(motor_pin_1, LOW); |
200 |
digitalWrite(motor_pin_2, HIGH); |
201 |
digitalWrite(motor_pin_3, LOW); |
202 |
digitalWrite(motor_pin_4, HIGH); |
203 |
break;
|
204 |
case 3: //1001 |
205 |
digitalWrite(motor_pin_1, HIGH); |
206 |
digitalWrite(motor_pin_2, LOW); |
207 |
digitalWrite(motor_pin_3, LOW); |
208 |
digitalWrite(motor_pin_4, HIGH); |
209 |
break;
|
210 |
} |
211 |
} |
212 |
} |
213 |
|
214 |
/*
|
215 |
version() returns the version of the library:
|
216 |
*/
|
217 |
int Stepper::version(void) |
218 |
{ |
219 |
return 4; |
220 |
} |