-
Notifications
You must be signed in to change notification settings - Fork 2
/
stepper_motor.py
136 lines (108 loc) · 4.03 KB
/
stepper_motor.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
import RPi.GPIO as GPIO
import time
import threading
# TODO: add acceleration
class Stepper(object):
def __init__(self, a1, a2, b1, b2, steps=50.0, rpm=30.0):
self.rpm = rpm
self.steps_per_rev = steps
self.current_step = 0
self.coil_A_1_pin = a1
self.coil_A_2_pin = a2
self.coil_B_1_pin = b1
self.coil_B_2_pin = b2
GPIO.setup(a1, GPIO.OUT)
GPIO.setup(a2, GPIO.OUT)
GPIO.setup(b1, GPIO.OUT)
GPIO.setup(b2, GPIO.OUT)
self.set_step(0, 0, 0, 0)
def forward(self, steps, delay=None, rpm=None, hold_position=True):
self.current_step = 0
d = delay if delay else self.get_delay(rpm)
for step in range(0, steps):
self.set_step(1, 0, 1, 0)
time.sleep(d)
self.set_step(0, 1, 1, 0)
time.sleep(d)
self.set_step(0, 1, 0, 1)
time.sleep(d)
self.set_step(1, 0, 0, 1)
time.sleep(d)
if hold_position is False:
self.set_step(0, 0, 0, 0)
def backward(self, steps, delay=None, rpm=None, hold_position=True):
self.current_step = 0
d = delay if delay else self.get_delay(rpm)
for step in range(0, steps):
self.set_step(1, 0, 0, 1)
time.sleep(d)
self.set_step(0, 1, 0, 1)
time.sleep(d)
self.set_step(0, 1, 1, 0)
time.sleep(d)
self.set_step(1, 0, 1, 0)
time.sleep(d)
if hold_position is False:
self.set_step(0, 0, 0, 0)
def set_rpm(self, rpm):
self.rpm = rpm
def microstep_forward(self, steps, delay=None, rpm=None, hold_position=True):
self.microstep(steps, delay, rpm, hold_position)
def microstep_backward(self, steps, delay=None, rpm=None, hold_position=True):
self.microstep(-steps, delay, rpm, hold_position)
def microstep(self, steps, delay=None, rpm=None, hold_position=True):
d = delay if delay else self.get_delay(rpm)
steps_left = abs(steps)
while steps_left > 0:
if steps > 0: # forward
self.current_step += 1
if self.current_step == self.steps_per_rev*4:
self.current_step = 0
else: # backwards
if self.current_step == 0:
self.current_step = self.steps_per_rev*4
self.current_step -= 1
steps_left -= 1
self.step_sequence(self.current_step % 4)
time.sleep(d)
if hold_position is False:
self.set_step(0, 0, 0, 0)
self.current_step = 0
def step_sequence(self, num):
if num == 0:
self.set_step(1, 0, 1, 0)
elif num == 1:
self.set_step(0, 1, 1, 0)
elif num == 2:
self.set_step(0, 1, 0, 1)
else:
self.set_step(1, 0, 0, 1)
def set_step(self, w1, w2, w3, w4):
GPIO.output(self.coil_A_1_pin, w1)
GPIO.output(self.coil_A_2_pin, w2)
GPIO.output(self.coil_B_1_pin, w3)
GPIO.output(self.coil_B_2_pin, w4)
def get_delay(self, rpm=None):
local_rpm = rpm if rpm else self.rpm
return 60.0 * 1000.0 / local_rpm / (self.steps_per_rev * 4.0) / 1000.0
def cleanup(self):
self.set_step(0, 0, 0, 0)
if __name__ == "__main__":
GPIO.setmode(GPIO.BCM)
stepper1 = Stepper(2, 3, 4, 17)
stepper2 = Stepper(27, 22, 10, 9)
while True:
steps_f = raw_input("How many steps forward? ")
st1 = threading.Thread(target=stepper2.forward, args=(int(steps_f),))
st2 = threading.Thread(target=stepper1.backwards, args=(int(steps_f),))
st1.start()
st2.start()
st1.join()
st2.join()
steps_b = raw_input("How many steps backwards? ")
st1 = threading.Thread(target=stepper2.backwards, args=(int(steps_b),))
st2 = threading.Thread(target=stepper1.forward, args=(int(steps_b),))
st1.start()
st2.start()
st1.join()
st2.join()