-
Notifications
You must be signed in to change notification settings - Fork 0
/
serial_communication_picture.py
49 lines (38 loc) · 1.3 KB
/
serial_communication_picture.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
import serial
import time
import threading
import subprocess
from datetime import datetime
SERIAL_PORT = '/dev/ttyACM0'
BAUD_RATE = 9600
ser = serial.Serial(SERIAL_PORT, BAUD_RATE)
THRESHOLD_RPM = 200
photo_taken = False
def take_photo():
timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
filename = f"/home/pi/JSP/SUA_PICTURE_{timestamp}.jpg"
threading.Thread(target=subprocess.run, args=(["libcamera-still", "-o", filename, "-t", "500"],)).start()
print(f"photo taken: {filename}")
try:
while True:
if ser.in_waiting > 0:
line = ser.readline().decode('utf-8').strip()
print(f"Recieve data: {line}")
if line.startswith("RPM: "):
try:
rpm = int(line.split(": ")[1])
print(f"Current RPM: {rpm}")
if rpm >= THRESHOLD_RPM and not photo_taken:
take_photo()
photo_taken = True
if rpm < THRESHOLD_RPM:
photo_taken = False
except ValueError:
print(f"Invalid data: {line}")
else:
print(f"Invalid data format: {line}")
time.sleep(0.1)
except KeyboardInterrupt:
print("program terminated")
finally:
ser.close()