-
Notifications
You must be signed in to change notification settings - Fork 0
/
relay.py
35 lines (26 loc) · 1.17 KB
/
relay.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
import serial
import time
import subprocess
import signal
import sys
# Use the correct serial port based on what you found, either '/dev/ttyACM0' or '/dev/ttyUSB0'
arduino = serial.Serial(port='/dev/ttyACM0', baudrate=9600, timeout=.1)
def send_command(command):
arduino.write(command.encode()) # Send the command to the Arduino
time.sleep(0.05) # Small delay to ensure the command is sent properly
def signal_handler(sig, frame):
print("Signal received, sending 'X' to microcontroller...")
send_command('X') # Send the 'X' command when the process is terminated
sys.exit(0) # Exit the program
# Register the signal handler for termination signals
signal.signal(signal.SIGINT, signal_handler) # Handles Ctrl+C
signal.signal(signal.SIGTERM, signal_handler) # Handles termination signal
# Example usage
while True:
command = input("Enter command (X: Press Both Buttons, Y: Release Both Buttons and Start two_cameras.py): ").strip().upper()
if command == 'X':
send_command('X') # Press both buttonsy
elif command == 'Y':
send_command('Y') # Release both buttons
else:
print("Invalid command. Please enter X or Y.")