Skip to content
This repository has been archived by the owner on Dec 8, 2024. It is now read-only.

feat: add overlord_overlord.py, update camera_overlord.py to be compatible #74

Merged
merged 2 commits into from
Oct 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 38 additions & 21 deletions client/drivers/camera_overlord.py
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)
50 changes: 50 additions & 0 deletions client/overlord_overlord.py
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()