forked from whaleygeek/anyio
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathzeropoint.py
96 lines (84 loc) · 2.35 KB
/
zeropoint.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
#!/usr/bin/python
# Import required libraries
import sys, time, threading
import anyio.GPIO as GPIO
def init(delay = 0.0015):
global running, StepCount, StepDir, stepsToDo, StepPosition, StepPins
global StepCounter, Seq, WaitTime
# Use physical pin numbers
GPIO.setmode(GPIO.BCM)
# Define GPIO signals to use
# StepPins = [35,36,32,33] # RoboHat
StepPins = [4, 17, 27, 18] # ZeroPoint
# Set all pins as output
for pin in StepPins:
GPIO.setup(pin,GPIO.OUT)
GPIO.output(pin, False)
# Define pin sequence
Seq = [[1,0,0,1],
[1,0,1,0],
[0,1,1,0],
[0,1,0,1]]
StepCount = len(Seq)
StepDir = 1 # 1 == clockwise, -1 = anticlockwise
StepsToDo = 0 #number of steps to move
StepPosition = 0 # current steps anti-clockwise from the zero position
# Initialise variables
StepCounter = 0
WaitTime = delay
running = True
# Move pointer to zero position
StepDir = -1
stepsToDo = 700
step()
#======================================================================
# Create step loop which can run in separate thread
def step():
global running, StepCounter, stepsToDo
while running and stepsToDo>0:
for pin in range(0,4):
xpin = StepPins[pin]# Get GPIO
if Seq[StepCounter][pin]!= 0:
GPIO.output(xpin, True)
else:
GPIO.output(xpin, False)
StepCounter += StepDir
if (StepCounter>=StepCount):
StepCounter = 0
if (StepCounter<0):
StepCounter = StepCount + StepDir
stepsToDo -= 1
#print stepsToDo
time.sleep(WaitTime)
# clear the output pins
for pin in StepPins:
GPIO.output(pin, False)
running = False
def cleanup():
running = False
time.sleep(1)
GPIO.cleanup()
def stepToZero():
global StepPosition, StepDir, stepsToDo, running
#override existing command and force zero position
running = False
time.sleep(WaitTime * 4)
StepDir = -1
stepsToDo = 700
StepPosition = 0
threadN = threading.Thread(target = step)
running = True
threadN.start()
def stepTo (pos):
global StepPosition, StepDir, stepsToDo, running
if (not running):
stepsToDo = pos - (StepPosition - stepsToDo*StepDir)
StepDir = 1
if (stepsToDo < 0):
StepDir = -1
stepsToDo = -stepsToDo
StepPosition = pos
# Startup the stepping thread
threadC = threading.Thread(target = step)
running = True
threadC.start()