-
Notifications
You must be signed in to change notification settings - Fork 0
/
stepper.py
115 lines (95 loc) · 2.73 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
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
from time import sleep
import RPi.GPIO as GPIO
#
PUL = 17 #
DIR = 27 #
ENA = 22 #
# mode/dir /home
MODE = 2
MANUAL_DIR = 3
HOME_DETECT = 4
def initIO():
print('IO Initialization Start')
GPIO.setmode(GPIO.BCM)
GPIO.setup(PUL, GPIO.OUT)
GPIO.setup(DIR, GPIO.OUT)
GPIO.setup(ENA, GPIO.OUT)
GPIO.setup(MODE, GPIO.IN)
GPIO.setup(MANUAL_DIR, GPIO.IN)
GPIO.setup(HOME_DETECT, GPIO.IN)
print('IO Initialization Complete')
#
#
def cleanupIO():
print('IO Cleanup started')
GPIO.cleanup() # run this just to make sure
#
#
def ismanualmode():
ismanual = GPIO.input(MODE)
return not ismanual
#
#
def ishome(): # normally 1 means that 0 = is home.
ishome = GPIO.input(HOME_DETECT)
return not ishome
#
#
def manualpositionleft():
posleft = GPIO.input(MANUAL_DIR)
return not posleft
#
def gohome():
GPIO.output(ENA, GPIO.LOW)
GPIO.output(DIR, GPIO.HIGH)
for y in range(5000):
GPIO.output(PUL, GPIO.HIGH)
GPIO.output(PUL, GPIO.LOW)
sleep(.001)
if ishome():
break
GPIO.output(ENA, GPIO.HIGH)
#
#
def takesteps(stepcount, direction):
sleep(.1) # pause due to a possible change direction
GPIO.output(ENA, GPIO.LOW) # enable
if direction == 0:
GPIO.output(DIR, GPIO.LOW) # RIGHT
else:
GPIO.output(DIR, GPIO.HIGH) # LEFT
# PARAMS for acceleration /decel..
ACC_DCC_WINDOW_STEPS = 400 # how big of a window to accel or decel over ..
ACC_DCC_RATE = .000003 # rate at which to inc/dec the delay...
accend = ACC_DCC_WINDOW_STEPS
dccstart = stepcount-ACC_DCC_WINDOW_STEPS # decel starts at end-500 steps
mydelay = .002 # starting delay.. it will inc/dec from here...
# accel range
printmax = False
for x in range(stepcount):
GPIO.output(PUL, GPIO.HIGH)
# sleep(mydelay)
GPIO.output(PUL, GPIO.LOW)
sleep(mydelay)
if x < accend:
#if x % 10 == 0: # evey 10 steps
mydelay -= ACC_DCC_RATE # reduce delay down..
# print("delay %s" % mydelay)
elif x > dccstart:
#if x % 10 == 0:
mydelay += ACC_DCC_RATE # ramp delay up = slow it down...
# print("delay %s" % mydelay)
else:
if printmax == False:
printmax = True
print("max speed delay: %5f" % mydelay)
# home check
if ishome() and direction == 1: # if moving Left and home,,, break out
print("made it home...done")
break
GPIO.output(ENA, GPIO.HIGH) # disable, free up the motor.
return
def goFarLeftPostion():
takesteps(2200,1)
def goFarRightPostion():
takesteps(2200,0)