Repository navigation
Expand file tree
/
Copy pathlibs.py
More file actions
executable file
·77 lines (56 loc) · 1.72 KB
/
Copy pathlibs.py
File metadata and controls
executable file
·77 lines (56 loc) · 1.72 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
65
66
67
68
69
70
71
72
73
74
75
76
77
#!/usr/bin/env python3
import math
import os
import random
import time
import subprocess
from pathlib import Path
import csv
# only for the relay
from gpiozero import OutputDevice # type:ignore
# ------ camera locks ----
LOCK_FILE = "/tmp/camera.lock"
UDP_TARGET = "udp://192.168.1.2:4197" # flow IP
STREAM_COMMAND = [
"rpicam-vid", "-t", "0", "-n", "--inline",
"-o", UDP_TARGET,
"--width", "1920", "--height", "1080"
]
SERVO_MIN_DUTY = 637981
SERVO_MAX_DUTY = 2543445
def is_camera_locked():
"""Returns True if another script is using the camera."""
return os.path.exists(LOCK_FILE)
def acquire_camera():
# 1. Create lockfile
with open(LOCK_FILE, "w") as f:
f.write("locked")
# just in case
subprocess.run(["pkill", "-f", "rpicam-vid"], check=False)
time.sleep(0.5)
def release_camera():
# Remove the lock file so the streamer can take over again
if os.path.exists(LOCK_FILE):
os.remove(LOCK_FILE)
# ----- servo control -----
def imageProg() -> int:
# unused rn
# assume it takes between like 100 and 5000 ms
time.sleep(random.randrange(20, 2010)/1000)
return 1
# if __name__ == "__main__":
def csv_logger(data_row, file_path, headers=None):
"""
A generic CSV tool.
If data_row is None, it only initializes the file with headers.
"""
path = Path(file_path)
file_exists = path.is_file()
with path.open(mode='a', newline='', encoding='utf-8') as f:
writer = csv.writer(f)
# 1. Handle Initialization (Header)
if not file_exists and headers:
writer.writerow(headers)
# 2. Handle Data (Only if data_row is provided)
if data_row is not None:
writer.writerow(data_row)