Skip to content

Commit 206d670

Browse files
committed
Added files again
1 parent 7ceeb6b commit 206d670

36 files changed

Lines changed: 5957 additions & 0 deletions

1.0/codes/auto.py

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
import select
2+
import sys
3+
import tty
4+
import termios
5+
import serial
6+
import time
7+
import os
8+
9+
# Initialize serial communication
10+
ser = serial.Serial("/dev/ttyUSB0", 57600, timeout=1)
11+
ser.flush()
12+
13+
original_settings = termios.tcgetattr(sys.stdin)
14+
15+
16+
def get_key(timeout=0.1):
17+
fd = sys.stdin.fileno()
18+
try:
19+
tty.setraw(fd) # Set the terminal to raw mode
20+
rlist, _, _ = select.select([sys.stdin], [], [], timeout)
21+
if rlist:
22+
ch = sys.stdin.read(1) # Read one character
23+
else:
24+
ch = None # No key pressed
25+
finally:
26+
termios.tcsetattr(fd, termios.TCSADRAIN, original_settings) # Restore terminal settings
27+
return ch
28+
29+
def send_stop():
30+
ser.write(b'q\n')
31+
print("Stopping")
32+
33+
34+
# Function to clear the terminal except for the first line
35+
def clear_terminal_but_first():
36+
os.system('clear')
37+
print("Press ESC to exit.")
38+
39+
# Main loop to listen for key presses
40+
try:
41+
print("Press ESC to exit.")
42+
43+
while True:
44+
key = get_key()
45+
46+
if key == '\x1b': # ESC key
47+
ser.write(b'exit\n')
48+
print("Exiting...")
49+
break
50+
51+
finally:
52+
ser.close()
53+
print("Serial connection closed.")
54+

1.0/codes/control.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import serial
2+
import time
3+
4+
ser = serial.Serial("/dev/ttyUSB1", 57600, timeout=1)
5+
ser.flush()
6+
7+
print('dir:')
8+
print('w - Front')
9+
print('a - Left')
10+
print('s - Back')
11+
print('d - Right')
12+
13+
try:
14+
while True:
15+
data = input("Enter: <dir> <time>\n")
16+
17+
ser.write(data.encode())
18+
19+
finally:
20+
ser.close()

1.0/codes/controls/control.py

Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
#!/usr/bin/env python3
2+
3+
import Jetson.GPIO as GPIO
4+
import time
5+
import serial
6+
7+
right_motor_pins = [
8+
(26, 20),
9+
(12, 6),
10+
(17, 18)
11+
]
12+
13+
left_motor_pins = [
14+
(16, 19),
15+
(8, 11),
16+
(25, 9)
17+
]
18+
19+
GPIO.setmode(GPIO.BCM)
20+
GPIO.setwarnings(False)
21+
ser = serial.Serial('/dev/ttyUSB0', 57600, timeout=1)
22+
23+
for pin_pair in left_motor_pins + right_motor_pins:
24+
GPIO.setup(pin_pair[0], GPIO.OUT)
25+
GPIO.setup(pin_pair[1], GPIO.OUT)
26+
27+
def move_forward():
28+
for pin1, pin2 in left_motor_pins + right_motor_pins:
29+
GPIO.output(pin1, GPIO.HIGH)
30+
GPIO.output(pin2, GPIO.LOW)
31+
32+
def move_backward():
33+
for pin1, pin2 in left_motor_pins + right_motor_pins:
34+
GPIO.output(pin2, GPIO.HIGH)
35+
GPIO.output(pin1, GPIO.LOW)
36+
37+
def stop():
38+
for pin1, pin2 in left_motor_pins + right_motor_pins:
39+
GPIO.output(pin1, GPIO.LOW)
40+
GPIO.output(pin2, GPIO.LOW)
41+
42+
def rotate(angle, direction='left', time_constant=0.02):
43+
rotation_time = angle * time_constant
44+
45+
if direction == 'left':
46+
for pin1, pin2 in left_motor_pins:
47+
GPIO.output(pin1, GPIO.LOW)
48+
GPIO.output(pin2, GPIO.HIGH)
49+
50+
for pin1, pin2 in right_motor_pins:
51+
GPIO.output(pin1, GPIO.HIGH)
52+
GPIO.output(pin2, GPIO.LOW)
53+
54+
elif direction == 'right':
55+
for pin1, pin2 in left_motor_pins:
56+
GPIO.output(pin1, GPIO.HIGH)
57+
GPIO.output(pin2, GPIO.LOW)
58+
59+
for pin1, pin2 in right_motor_pins:
60+
GPIO.output(pin1, GPIO.LOW)
61+
GPIO.output(pin2, GPIO.HIGH)
62+
63+
time.sleep(rotation_time)
64+
stop()
65+
66+
def receive_data():
67+
if ser.in_waiting > 0:
68+
data = ser.readline().decode().strip()
69+
return data
70+
71+
def mov(direction, t):
72+
try:
73+
t = float(t) # convert t to float in case of decimals
74+
if direction == 'w':
75+
move_forward()
76+
time.sleep(t)
77+
elif direction == 's':
78+
move_backward()
79+
time.sleep(t)
80+
elif direction == 'a':
81+
rotate(90, 'left')
82+
elif direction == 'd':
83+
rotate(90, 'right')
84+
elif direction == 'q':
85+
stop()
86+
elif direction == 'rl':
87+
rotate(t, 'left')
88+
elif direction == 'rr':
89+
rotate(t, 'right')
90+
stop()
91+
return
92+
else:
93+
print("Invalid command!")
94+
except ValueError:
95+
print("Error in time conversion.")
96+
stop()
97+
'''
98+
try:
99+
print('Manual Mode Started')
100+
while True:
101+
data = receive_data()
102+
if data:
103+
print(data)
104+
try:
105+
d, t = data.split(" ")
106+
mov(d, t)
107+
except ValueError:
108+
print("Received malformed data: ", data)
109+
finally:
110+
ser.close()
111+
GPIO.cleanup()
112+
'''
113+
rotate(90, 'left')
114+
move_forward()
115+
time.sleep(10)
116+
stop()

0 commit comments

Comments
 (0)