-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrun.py
More file actions
64 lines (55 loc) · 1.92 KB
/
Copy pathrun.py
File metadata and controls
64 lines (55 loc) · 1.92 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
63
64
import usb.core
import usb.util
import subprocess
import time
import sys
import signal
# Replace these with your Pixy2's actual Vendor ID and Product ID
VENDOR_ID = 0xf000
PRODUCT_ID = 0xb1ac
def reset_usb_device(vendor_id, product_id):
dev = usb.core.find(idVendor=vendor_id, idProduct=product_id)
if dev is None:
print("[Wrapper] Pixy2 device not found.")
return False
try:
dev.reset()
print("[Wrapper] USB device reset successfully.")
return True
except usb.core.USBError as e:
print(f"[Wrapper] USB reset failed: {e}")
return False
def launch_pixy2_interface():
process = subprocess.Popen(
["python", "src/video_servo_interface.py", "--ui", "textual"]
)
try:
process.wait()
return process.returncode == 0
except KeyboardInterrupt:
print("[Wrapper] Interrupt received, terminating subprocess...")
process.send_signal(signal.SIGINT)
process.wait()
sys.exit(1)
def main():
print("[Wrapper] Launching Pixy2 interface...")
if launch_pixy2_interface():
print("[Wrapper] Pixy2 initialized successfully.")
return
print("[Wrapper] First attempt failed, waiting 5 seconds...")
time.sleep(5)
print("[Wrapper] Retrying Pixy2 interface launch...")
if launch_pixy2_interface():
print("[Wrapper] Pixy2 initialized successfully after retry.")
return
print("[Wrapper] Retry failed, attempting USB reset...")
if reset_usb_device(VENDOR_ID, PRODUCT_ID):
print("[Wrapper] Waiting 5 seconds after USB reset...")
time.sleep(5)
print("[Wrapper] Launching Pixy2 interface after USB reset...")
if launch_pixy2_interface():
print("[Wrapper] Pixy2 initialized successfully after USB reset.")
return
print("[Wrapper] All attempts failed. Please manually reconnect Pixy2.")
if __name__ == "__main__":
main()