-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.py
166 lines (146 loc) · 5.47 KB
/
test.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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
import Jetson.GPIO as GPIO
import time
class MotorControl:
def __init__(self):
# GPIO pin assignment for Motor 1
self.IN1_motor1 = 19 # GPIO19 -> Pin 35 on Jetson
self.IN2_motor1 = 26 # GPIO26 -> Pin 37 on Jetson
# GPIO pin assignment for Motor 2
self.IN3_motor2 = 20 # GPIO20 -> Pin 38 on Jetson
self.IN4_motor2 = 16 # GPIO16 -> Pin 36 on Jetson
# GPIO pin assignment for Motor 3
self.IN1_motor3 = 17 # GPIO17 -> Pin 11 on Jetson
self.IN2_motor3 = 18 # GPIO18 -> Pin 12 on Jetson
# GPIO pin assignment for Motor 4
self.IN3_motor4 = 12 # GPIO12 -> Pin 32 on Jetson
self.IN4_motor4 = 6 # GPIO6 -> Pin 31 on Jetson
# Setup GPIO
GPIO.setmode(GPIO.BCM) # Use BCM numbering for GPIO pins
GPIO.setup(self.IN1_motor1, GPIO.OUT)
GPIO.setup(self.IN2_motor1, GPIO.OUT)
GPIO.setup(self.IN3_motor2, GPIO.OUT)
GPIO.setup(self.IN4_motor2, GPIO.OUT)
GPIO.setup(self.IN1_motor3, GPIO.OUT)
GPIO.setup(self.IN2_motor3, GPIO.OUT)
GPIO.setup(self.IN3_motor4, GPIO.OUT)
GPIO.setup(self.IN4_motor4, GPIO.OUT)
def move_motor_forward(self, motor_num):
if motor_num == 1:
print("Moving Motor 1 forward.")
GPIO.output(self.IN1_motor1, GPIO.HIGH)
GPIO.output(self.IN2_motor1, GPIO.LOW)
elif motor_num == 2:
print("Moving Motor 2 forward.")
GPIO.output(self.IN3_motor2, GPIO.HIGH)
GPIO.output(self.IN4_motor2, GPIO.LOW)
elif motor_num == 3:
print("Moving Motor 3 forward.")
GPIO.output(self.IN1_motor3, GPIO.HIGH)
GPIO.output(self.IN2_motor3, GPIO.LOW)
elif motor_num == 4:
print("Moving Motor 4 forward.")
GPIO.output(self.IN3_motor4, GPIO.LOW) # Corrected direction
GPIO.output(self.IN4_motor4, GPIO.HIGH) # Corrected direction
else:
print("Invalid motor number.")
def move_motor_backward(self, motor_num):
if motor_num == 1:
print("Moving Motor 1 backward.")
GPIO.output(self.IN1_motor1, GPIO.LOW)
GPIO.output(self.IN2_motor1, GPIO.HIGH)
elif motor_num == 2:
print("Moving Motor 2 backward.")
GPIO.output(self.IN3_motor2, GPIO.LOW)
GPIO.output(self.IN4_motor2, GPIO.HIGH)
elif motor_num == 3:
print("Moving Motor 3 backward.")
GPIO.output(self.IN1_motor3, GPIO.LOW)
GPIO.output(self.IN2_motor3, GPIO.HIGH)
elif motor_num == 4:
print("Moving Motor 4 backward.")
GPIO.output(self.IN3_motor4, GPIO.HIGH)
GPIO.output(self.IN4_motor4, GPIO.LOW)
else:
print("Invalid motor number.")
def stop_motor(self, motor_num):
if motor_num == 1:
print("Stopping Motor 1.")
GPIO.output(self.IN1_motor1, GPIO.LOW)
GPIO.output(self.IN2_motor1, GPIO.LOW)
elif motor_num == 2:
print("Stopping Motor 2.")
GPIO.output(self.IN3_motor2, GPIO.LOW)
GPIO.output(self.IN4_motor2, GPIO.LOW)
elif motor_num == 3:
print("Stopping Motor 3.")
GPIO.output(self.IN1_motor3, GPIO.LOW)
GPIO.output(self.IN2_motor3, GPIO.LOW)
elif motor_num == 4:
print("Stopping Motor 4.")
GPIO.output(self.IN3_motor4, GPIO.LOW)
GPIO.output(self.IN4_motor4, GPIO.LOW)
else:
print("Invalid motor number.")
def move_all_forward(self):
print("Moving all motors forward.")
self.move_motor_forward(1)
self.move_motor_forward(2)
self.move_motor_forward(3)
self.move_motor_forward(4)
def move_all_backward(self):
print("Moving all motors backward.")
self.move_motor_backward(1)
self.move_motor_backward(2)
self.move_motor_backward(3)
self.move_motor_backward(4)
def stop_all(self):
print("Stopping all motors.")
self.stop_motor(1)
self.stop_motor(2)
self.stop_motor(3)
self.stop_motor(4)
def turn_left(self):
print("Turning left.")
self.move_motor_forward(1)
self.move_motor_forward(3)
self.move_motor_backward(2)
self.move_motor_backward(4)
def turn_right(self):
print("Turning right.")
self.move_motor_forward(2)
self.move_motor_forward(4)
self.move_motor_backward(1)
self.move_motor_backward(3)
def cleanup(self):
print("Cleaning up GPIO settings.")
GPIO.cleanup()
# Example usage
if __name__ == "__main__":
motor_control = MotorControl()
try:
# Move all motors forward
motor_control.move_all_forward()
time.sleep(2)
motor_control.stop_all()
time.sleep(1)
# Move all motors backward
motor_control.move_all_backward()
time.sleep(2)
motor_control.stop_all()
time.sleep(1)
# Turn left
motor_control.turn_left()
time.sleep(2)
motor_control.stop_all()
time.sleep(1)
# Turn right
motor_control.turn_right()
time.sleep(2)
motor_control.stop_all()
time.sleep(1)
except KeyboardInterrupt:
print("Program interrupted by the user.")
motor_control.cleanup()
finally:
motor_control.cleanup()
print("GPIO cleaned up and program terminated.")