This repository has been archived by the owner on Dec 8, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add overlord_overlord.py, update camera_overlord.py to be compa…
…tible (#74) * feat: add overlord_lord.py, update camera_overlord.py to be compatible * style: add comments to overlord_overlord.py
- Loading branch information
Showing
2 changed files
with
88 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,31 +1,48 @@ | ||
# This script periodically takes photos and saves them to | ||
# a temporary file on ram. This allows multiple programs to | ||
# access the camera feed at once. | ||
""" | ||
This script periodically takes photos and saves them to a temporary file on ram. This | ||
allows multiple programs to access the camera feed at once. | ||
from picamera2 import Picamera2 | ||
Exits gracefully with status INTERRUPTED and closes the camera if a sigint is received. | ||
""" | ||
|
||
import logging | ||
import os | ||
import signal | ||
import time | ||
from enum import Enum | ||
|
||
from picamera2 import Picamera2 | ||
|
||
|
||
class ExitCode(Enum): | ||
INTERRUPTED = 1 | ||
|
||
|
||
if __name__ == '__main__': | ||
logger = logging.getLogger(__name__) | ||
|
||
|
||
def handle_quit(signo, frame): | ||
logger.info("Received SIGINT, quitting") | ||
picam2.close() | ||
quit(ExitCode.INTERRUPTED.value) | ||
|
||
|
||
if __name__ == "__main__": | ||
picam2 = Picamera2() | ||
config = picam2.create_still_configuration({'size':(640,480)}) | ||
config = picam2.create_still_configuration({"size": (640, 480)}) | ||
picam2.configure(config) | ||
picam2.start() | ||
picam2.options['quality'] = 80 | ||
picam2.options["quality"] = 80 | ||
|
||
signal.signal(signal.SIGINT, handle_quit) | ||
|
||
try: | ||
f = open('/tmp/snapshot.jpg','x') | ||
f = open("/tmp/snapshot.jpg", "x") | ||
f.close() | ||
except: | ||
print('Snapshot already exists') | ||
|
||
try: | ||
while True: | ||
picam2.capture_file("/tmp/snapshot2.jpg") | ||
os.replace("/tmp/snapshot2.jpg", "/tmp/snapshot.jpg") | ||
time.sleep(0.5) | ||
|
||
except KeyboardInterrupt: | ||
picam2.close() | ||
print('Closed nicely') | ||
quit() | ||
except FileExistsError: | ||
print("Snapshot already exists") | ||
|
||
while True: | ||
picam2.capture_file("/tmp/snapshot2.jpg") | ||
os.replace("/tmp/snapshot2.jpg", "/tmp/snapshot.jpg") | ||
time.sleep(0.5) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
""" | ||
One overlord to rule them all... | ||
""" | ||
|
||
import logging | ||
import multiprocessing | ||
import os | ||
import signal | ||
import subprocess | ||
|
||
PYTHON_DEFAULT = "python3.10" | ||
PYTHON_CAMERA = "python3.11" | ||
|
||
|
||
def spawn_camera_overlord(): | ||
subprocess.run([PYTHON_CAMERA, "drivers/camera_overlord.py"]) | ||
|
||
|
||
def spawn_pi_overlord(): | ||
subprocess.run([PYTHON_DEFAULT, "drivers/main.py"]) | ||
|
||
|
||
def main(): | ||
logger = logging.getLogger(__name__) | ||
|
||
# Spawn a new process to run the camera indefinitely | ||
camera_overlord = multiprocessing.Process(target=spawn_camera_overlord, args=()) | ||
logger.info("Starting camera overlord") | ||
camera_overlord.start() | ||
|
||
# Spawn a new process to run the Raspberry Pi code | ||
pi_overlord = multiprocessing.Process(target=spawn_pi_overlord, args=()) | ||
logger.info("Starting pi overlord") | ||
pi_overlord.start() | ||
|
||
# If the Pi process ever terminates, gracefully terminate the camera process | ||
_, pi_overlord_wait_status = os.waitpid(pi_overlord.pid, 0) | ||
pi_overlord_exit_code = os.waitstatus_to_exit_code(pi_overlord_wait_status) | ||
logger.info(f"Reaped pi overlord with exit code {pi_overlord_exit_code}") | ||
|
||
os.kill(camera_overlord.camerad, signal.SIGINT) | ||
_, camera_overlord_wait_status = os.waitcamerad(camera_overlord.camerad, 0) | ||
camera_overlord_exit_code = os.waitstatus_to_exit_code(camera_overlord_wait_status) | ||
logger.info(f"Reaped camera overlord with exit code {camera_overlord_exit_code}") | ||
|
||
logger.info("Exiting") | ||
|
||
|
||
if __name__ == "__main__": | ||
main() |