This repository has been archived by the owner on Jul 16, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstepper.py.backup
92 lines (78 loc) · 3.66 KB
/
stepper.py.backup
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
# Carnegie Mellon Rocket Command, Device Driver for TMC2208 Motor Driver IC
# Chris Stange, <[email protected]> (December 26, 2021)
#if the stepper motor vibrates and does not rotate, wiring is incorrect
# THIS DEVICE DRIVER OPERATES THE TMC2208 IN LEGACY MODE
import RPi.GPIO as GPIO
import time
# Pin numbers are specified by their pin header number, not the GPIO number
GPIO.setmode(GPIO.BOARD)
class Stepper:
def __init__(self, en_pin, res1_pin, res2_pin, dir_pin, step_pin):
# In order to control the TMC2208 we need an Enable, 2 Resolution, a Direction, and a Step pin
self.en_pin = en_pin # This is the EN pin on the TMC2208
self.res1_pin = res1_pin # This is the MS1 pin on the TMC2208
self.res2_pin = res2_pin # This is the MS2 pin on the TMC2208
self.dir_pin = dir_pin # This is the DIR pin on the TMC2208
self.step_pin = step_pin # This is the STEP pin on the TMC2208
# Set all pins as outputs
GPIO.setup(self.en_pin, GPIO.OUT)
GPIO.setup(self.res1_pin, GPIO.OUT)
GPIO.setup(self.res2_pin, GPIO.OUT)
GPIO.setup(self.dir_pin, GPIO.OUT)
GPIO.setup(self.step_pin, GPIO.OUT)
# Initialize motor as off
self.enable = False
GPIO.output(self.en_pin, 1)
# Intialize resolution to 1/2
self.resolution = (0,1)
GPIO.output(self.res1_pin, self.resolution[0])
GPIO.output(self.res2_pin, self.resolution[1])
# Initialize direction as forward
GPIO.output(self.dir_pin, 0)
# Initialize no motor movement
GPIO.output(self.step_pin, 0)
# Resolution dictionary
self.dict = {'1/2': (0,1),
'1/4': (1,0),
'1/8': (0,0),
'1/16': (1,1)}
# Enables motor
def start_motor(self):
self.enable = True
GPIO.output(self.en_pin, 0)
# Disables motor
def stop_motor(self):
self.enable = False
GPIO.output(self.en_pin, 1)
# Sets resolution
# Resolution can either be 1/2, 1/4, 1/8, or 1/16
def set_resolution(self, resolution):
if resolution in self.dict:
self.resolution = self.dict[resolution]
GPIO.output(self.res1_pin, self.resolution[0])
GPIO.output(self.res2_pin, self.resolution[1])
else:
printf("Invalid resolution\nValid resolutions are '1/2', '1/4', '1/8', '1/16'")
# Moves the stepper
# Input the number of steps, the direction, and the frequency at which the steps should be made
# Number of steps and frequency are ints
# Direction is 0 for forward and 1 for backward
# THIS FUNCTION WILL NOT DO ANYTHING IF start_motor() IS NOT CALLED FIRST
def move_motor(self, step_count, step_direction, step_frequency):
if self.enable:
if (step_direction == 0) or (step_direction == 1):
if isinstance(step_count, int) and isinstance(step_frequency, int):
step_period = 1/step_frequency
GPIO.output(self.dir_pin, step_direction)
for i in range(step_count):
print(i)
GPIO.output(self.step_pin, 1)
time.sleep(step_period/2)
GPIO.output(self.step_pin, 0)
time.sleep(step_period/2)
else:
print("Invalid step count or frequency\nPlease ensure inputs are ints")
else:
print("Invalid motor direction\nPlease input 0 for forward\nPlease input 1 for backwards")
else:
print("Motor not enabled\nPlease call start_motor() first")