-
Notifications
You must be signed in to change notification settings - Fork 0
/
stepper.py
73 lines (65 loc) · 1.54 KB
/
stepper.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
import RPi.GPIO as GPIO
import time
import math
GPIO.setmode(GPIO.BOARD)
#stepPins = [7,8,5,3]
stepPins = [7,8,5,3]
stepdelay = 0.001
#degperstep = 5.625
degperstep = 0.087890625
currentangle = 0
lasthalfstep = 0; #the last half step to be performed.
for pin in stepPins:
GPIO.setup(pin,GPIO.OUT)
GPIO.output(pin,0)
seq = [ [1,0,0,0],
[1,1,0,0],
[0,1,0,0],
[0,1,1,0],
[0,0,1,0],
[0,0,1,1],
[0,0,0,1],
[1,0,0,1] ]
def makeCWstep():
global lasthalfstep
global currentangle
if lasthalfstep == 7:
lasthalfstep = 0
else:
lasthalfstep += 1
for pin in range(4):
GPIO.output(stepPins[pin],seq[lasthalfstep][pin])
currentangle+=degperstep
def makeCCWstep():
global lasthalfstep
global currentangle
if lasthalfstep == 0:
lasthalfstep = 7
else:
lasthalfstep -= 1
for pin in range(4):
GPIO.output(stepPins[pin],seq[lasthalfstep][pin])
currentangle-=degperstep
def CW(degrees):
steps=int(degrees/degperstep)
x = 0
while x < steps:
makeCWstep()
time.sleep(stepdelay)
x+=1
def CCW(degrees):
steps=int(degrees/degperstep)
x = 0
while x < steps:
makeCCWstep()
time.sleep(stepdelay)
x+=1
def home():
global currentangle
if currentangle > 0:
CCW(currentangle)
elif currentangle == 0:
return
elif currentangle < 0:
CW(abs(currentangle))
#CCW(22.5)