-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathbutton_interface.py
More file actions
62 lines (50 loc) · 2.09 KB
/
Copy pathbutton_interface.py
File metadata and controls
62 lines (50 loc) · 2.09 KB
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
import serial, struct, os, time
usb_port = 'COM6' # Update this to your port
baud_rate = 115200 #921600
class ButtonInterface:
def __init__(self, shared_memory_object):
self.shared_memory_object = shared_memory_object
self.ser = serial.Serial(usb_port, baud_rate, timeout=1)
self.delay = 0.001
def read_packet(self) -> tuple[int, int, int, int, float]:
"""
Reads data packet from electrical system
"""
while True:
byte = self.ser.read(1)
if not byte:
continue
if byte[0] == 0xAA: # Found header
# Read remaining 10 bytes of the packet
data = self.ser.read(10)
if len(data) < 10:
continue # incomplete packet, discard and resync
greenPressed = data[0]
bluePressed = data[1]
extKillState = data[2]
intKillState = data[3]
depth_bytes = data[4:8]
depth = struct.unpack('<f', depth_bytes)[0]
# Optionally verify trailing \r\n
if data[8] != 13 or data[9] != 10:
print("Warning: packet missing expected newline characters")
return greenPressed, bluePressed, extKillState, intKillState, depth
def run_loop(self):
"""
Check if green button is pressed
"""
if self.read_packet()[0]: # if green pressed
os.system(os.path.expanduser("python3 ~/robosub_software_2025/display_manager/start_services.py")) # startup display
os.system(os.path.expanduser("python3 ~/robosub_software_2025/launch.py")) # run launch
time.sleep(self.delay) # delay
def print_data(self) -> None:
"""
Prints data read from button USB
"""
gp, bp, ek, ik, depth = self.read_packet()
print(f"Green: {gp}, Blue: {bp}, ExtKill: {ek}, IntKill: {ik}, Depth: {depth}")
def close(self) -> None:
"""
Close serial connection
"""
self.ser.close()