-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStepper.py
More file actions
75 lines (60 loc) · 2.56 KB
/
Stepper.py
File metadata and controls
75 lines (60 loc) · 2.56 KB
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
import RPi.GPIO as GPIO
import time
GPIO.setmode(GPIO.BCM)
GPIO.setwarnings(False)
class Stepper:
# adjust if different
def __init__(self, pin1=17, pin2=18, pin3=22, pin4=23):
self.StepState = 0; # 8 per full step
self.Position = 0; # 400 per revolution
self.StepCount = 8
self.Seq = range(0, self.StepCount)
self.Seq[0] = [0,1,0,0]
self.Seq[1] = [0,1,0,1]
self.Seq[2] = [0,0,0,1]
self.Seq[3] = [1,0,0,1]
self.Seq[4] = [1,0,0,0]
self.Seq[5] = [1,0,1,0]
self.Seq[6] = [0,0,1,0]
self.Seq[7] = [0,1,1,0]
self.coil_A_1_pin = pin1
self.coil_A_2_pin = pin2
self.coil_B_1_pin = pin3
self.coil_B_2_pin = pin4
GPIO.setup(self.coil_A_1_pin, GPIO.OUT)
GPIO.setup(self.coil_A_2_pin, GPIO.OUT)
GPIO.setup(self.coil_B_1_pin, GPIO.OUT)
GPIO.setup(self.coil_B_2_pin, GPIO.OUT)
#self.setStep(self.Seq[self.StepState][0], self.Seq[self.StepState][1], self.Seq[self.StepState][2], self.Seq[self.StepState][3])
def setStep(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 forward(self, delay, steps):
for i in range(steps):
self.StepState = self.StepState + 1
self.Position = self.Position + 1
if self.StepState == self.StepCount:
self.StepState = 0
self.setStep(self.Seq[self.StepState][0], self.Seq[self.StepState][1], self.Seq[self.StepState][2], self.Seq[self.StepState][3])
time.sleep(delay)
def backwards(self, delay, steps):
for i in range(steps):
self.StepState = self.StepState - 1
self.Position = self.Position - 1
if self.StepState < 0:
self.StepState = self.StepCount - 1
self.setStep(self.Seq[self.StepState][0], self.Seq[self.StepState][1], self.Seq[self.StepState][2], self.Seq[self.StepState][3])
time.sleep(delay)
def setPosition(self, pos):
if pos < self.Position:
self.backwards(.002, self.Position - pos)
elif pos > self.Position:
self.forward(.002, pos - self.Position)
print "Current Position: ", self.Position
if __name__ == '__main__':
motor = Stepper();
while True:
steps = raw_input("How many steps forward? ")
motor.setPosition(int(steps));