forked from NGCPSlo/Code2022-23
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Stepper.py
95 lines (70 loc) · 2.96 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
import time
import Jetson.GPIO as GPIO
GPIO.setmode(GPIO.BOARD)
class Stepper:
def __init__(self, number_of_steps: int, motor_pin_1: int, motor_pin_2: int, motor_pin_3: int, motor_pin_4: int):
self.step_number = 0
self.direction = 0
self.last_step_time = 0
self.number_of_steps = number_of_steps
self.motor_pin_1 = motor_pin_1
self.motor_pin_2 = motor_pin_2
self.motor_pin_3 = motor_pin_3
self.motor_pin_4 = motor_pin_4
GPIO.setup(self.motor_pin_1, GPIO.OUT)
GPIO.setup(self.motor_pin_2, GPIO.OUT)
GPIO.setup(self.motor_pin_3, GPIO.OUT)
GPIO.setup(self.motor_pin_4, GPIO.OUT)
self.pin_count = 4
def setSpeed(self, whatSpeed: int):
self.step_delay = 60 * 1000 * 1000 / self.number_of_steps / whatSpeed
#Moves the stepper motor "steps_to_move" number of times
#Positive -> CW Negative -> CCW
def step(self, steps_to_move: int):
steps_left:int = abs(steps_to_move)
if(steps_to_move > 0):
self.direction = 1
if(steps_to_move < 0):
self.direction = 0
while(steps_left > 0):
now:int = time.process_time_ns() / 1000
if(now - self.last_step_time >= self.step_delay):
self.last_step_time = now
if(self.direction == 1):
self.step_number += 1
if(self.step_number == self.number_of_steps):
self.step_number = 0
else:
if(self.step_number == 0):
self.step_number = self.number_of_steps
self.step_number -= 1
steps_left -= 1
self.stepMotor(self.step_number % 4)
#Controls the motor
def stepMotor(self, this_step: int):
#1010
if this_step == 0:
GPIO.output(self.motor_pin_1, GPIO.HIGH)
GPIO.output(self.motor_pin_2, GPIO.LOW)
GPIO.output(self.motor_pin_3, GPIO.HIGH)
GPIO.output(self.motor_pin_4, GPIO.LOW)
#0110
elif this_step == 1:
GPIO.output(self.motor_pin_1, GPIO.LOW)
GPIO.output(self.motor_pin_2, GPIO.HIGH)
GPIO.output(self.motor_pin_3, GPIO.HIGH)
GPIO.output(self.motor_pin_4, GPIO.LOW)
#0101
elif this_step == 2:
GPIO.output(self.motor_pin_1, GPIO.LOW)
GPIO.output(self.motor_pin_2, GPIO.HIGH)
GPIO.output(self.motor_pin_3, GPIO.LOW)
GPIO.output(self.motor_pin_4, GPIO.HIGH)
#1001
elif this_step == 3:
GPIO.output(self.motor_pin_1, GPIO.HIGH)
GPIO.output(self.motor_pin_2, GPIO.LOW)
GPIO.output(self.motor_pin_3, GPIO.LOW)
GPIO.output(self.motor_pin_4, GPIO.HIGH)
def close(self):
GPIO.cleanup()