-
Notifications
You must be signed in to change notification settings - Fork 0
/
motor.py
88 lines (59 loc) · 2.3 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
# Simple stepper motor control
import time
import board
import digitalio
"""State for EN pin so that the stepper is active (will not allow moving, and may buzz)"""
ENABLED = True
"""State for EN pin so that the stepper is disabled (free moving, not locked in place, no buzz)"""
DISABLED = False
# Intended to wire MS1, MS2, MS3 directly to 3.3 logic high to control, not set up to be controlled with IO pins
"""Full step, pins expected to be wired: MS1 = low, MS2 = low, MS3 = low"""
FULL_STEP = 1
"""1/2 microstep, pins expected to be wired: MS1 = high, MS2 = low, MS3 = low"""
HALF_STEP = 2
"""1/4 microstep, pins expected to be wired: MS1 = low, MS2 = high, MS3 = low"""
QUARTER_STEP = 4
"""1/8 microstep, pins expected to be wired: MS1 = high, MS2 = high, MS3 = low"""
EIGHTH_STEP = 8
"""1/16 microstep, pins expected to be wired: MS1 = high, MS2 = high, MS3 = high"""
SIXTEENTH_STEP = 16
class Speed(object):
def __init__(self, delay, style) -> None:
self.delay = delay
self.stepMode = style
# Experiments - Will need to be re-done if changing power supply or adjusting output POT on the A4988
# FULL 0.00100 - PERFECTO! no need to experiment more!
currentSpeed = Speed(.001, FULL_STEP)
class Motor(object):
def __init__(self) -> None:
self.enablePin = digitalio.DigitalInOut(board.GP15)
self.stepPin = digitalio.DigitalInOut(board.GP12)
self.dirPin = digitalio.DigitalInOut(board.GP13)
self.stepPin.direction = digitalio.Direction.OUTPUT
self.dirPin.direction = digitalio.Direction.OUTPUT
self.enablePin.direction = digitalio.Direction.OUTPUT
# Start off with it disengaged
self.enablePin.value = DISABLED
def __del__(self):
self.enablePin.value = DISABLED
UP = False
DOWN = True
_motor = Motor()
def onestep(direction):
_motor.enablePin.value = ENABLED
_motor.dirPin.value = direction
_motor.stepPin.value = True
time.sleep(currentSpeed.delay)
_motor.stepPin.value = False
def manySteps(direction, steps):
count = 0
while count < steps:
onestep(direction)
count += 1
time.sleep(currentSpeed.delay)
def engage():
_motor.enablePin.value = ENABLED
def release():
_motor.enablePin.value = DISABLED
def isEngaged() -> bool:
return _motor.enablePin.value == ENABLED