-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmotor.py
109 lines (100 loc) · 3.95 KB
/
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
try:
import RPi.GPIO as GPIO
except:
import Mock.GPIO as GPIO
import time
import math
from constants import GPIO_ENABLED
class Stepper:
def __init__(self, in1, in2, in3, in4, switchPin):
self.MINSLEEP = 0.0007
self.STEPSPERREV = 4096
self.steps = 0
self.in1 = in1
self.in2 = in2
self.in3 = in3
self.in4 = in4
self.switchPin = switchPin
GPIO.setup(in1, GPIO.OUT)
GPIO.setup(in2, GPIO.OUT)
GPIO.setup(in3, GPIO.OUT)
GPIO.setup(in4, GPIO.OUT)
GPIO.setup(switchPin, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)
def rotateUntilSwitchActivation(self, forward = True, speed = 10, rotateUntilSwitchIs = True):
if not GPIO_ENABLED:
time.sleep(2)
return
speed = min(max(speed, 1), 10)
# If the pin is already on, rotate until the button is no longer pressed.
while (GPIO.input(self.switchPin) == rotateUntilSwitchIs):
self.step(1, forward)
time.sleep(self.MINSLEEP / speed * 10)
# After the button is unpressed, rotate until it is pressed again.
while (GPIO.input(self.switchPin) != rotateUntilSwitchIs):
self.step(1, forward)
time.sleep(self.MINSLEEP / speed * 10)
def rotateDegrees(self, angle, forward = True, speed = 10):
if not GPIO_ENABLED:
time.sleep(2)
return
speed = min(max(speed, 1), 10)
numSteps = math.ceil(angle * self.STEPSPERREV / 360)
for i in range(numSteps):
self.step(1, forward)
time.sleep(self.MINSLEEP / speed * 10)
def step(self, numSteps, direction):
for x in range(numSteps):
mode = self.steps % 8
if mode == 0:
GPIO.output(self.in1, GPIO.LOW)
GPIO.output(self.in2, GPIO.LOW)
GPIO.output(self.in3, GPIO.LOW)
GPIO.output(self.in4, GPIO.HIGH)
elif mode == 1:
GPIO.output(self.in1, GPIO.LOW)
GPIO.output(self.in2, GPIO.LOW)
GPIO.output(self.in3, GPIO.HIGH)
GPIO.output(self.in4, GPIO.HIGH)
elif mode == 2:
GPIO.output(self.in1, GPIO.LOW)
GPIO.output(self.in2, GPIO.LOW)
GPIO.output(self.in3, GPIO.HIGH)
GPIO.output(self.in4, GPIO.LOW)
elif mode == 3:
GPIO.output(self.in1, GPIO.LOW)
GPIO.output(self.in2, GPIO.HIGH)
GPIO.output(self.in3, GPIO.HIGH)
GPIO.output(self.in4, GPIO.LOW)
elif mode == 4:
GPIO.output(self.in1, GPIO.LOW)
GPIO.output(self.in2, GPIO.HIGH)
GPIO.output(self.in3, GPIO.LOW)
GPIO.output(self.in4, GPIO.LOW)
elif mode == 5:
GPIO.output(self.in1, GPIO.HIGH)
GPIO.output(self.in2, GPIO.HIGH)
GPIO.output(self.in3, GPIO.LOW)
GPIO.output(self.in4, GPIO.LOW)
elif mode == 6:
GPIO.output(self.in1, GPIO.HIGH)
GPIO.output(self.in2, GPIO.LOW)
GPIO.output(self.in3, GPIO.LOW)
GPIO.output(self.in4, GPIO.LOW)
elif mode == 7:
GPIO.output(self.in1, GPIO.HIGH)
GPIO.output(self.in2, GPIO.LOW)
GPIO.output(self.in3, GPIO.LOW)
GPIO.output(self.in4, GPIO.HIGH)
else:
GPIO.output(self.in1, GPIO.LOW)
GPIO.output(self.in2, GPIO.LOW)
GPIO.output(self.in3, GPIO.LOW)
GPIO.output(self.in4, GPIO.LOW)
if direction == True:
self.steps += 1
elif direction == False:
self.steps -= 1
if self.steps > self.STEPSPERREV:
self.steps = 0
elif self.steps < 0:
self.steps = self.STEPSPERREV