Skip to content

Commit

Permalink
Update camera.py
Browse files Browse the repository at this point in the history
fix exception handling when camera isn't found so program does not crash
  • Loading branch information
kk60503 authored May 12, 2024
1 parent 57bee31 commit 369dcf3
Showing 1 changed file with 15 additions and 9 deletions.
24 changes: 15 additions & 9 deletions oresat_star_tracker/camera.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
"""Star tracker AR013x camera"""

import sys
import io
import os
from os.path import abspath, dirname
from enum import Enum

import cv2
import numpy as np
from olaf import logger


class CameraError(Exception):
Expand All @@ -19,12 +22,19 @@ class Camera:

def __init__(self, mock: bool = False):
self._mock = mock

if self._mock:
self.state = CameraState.MOCK
self._capture_path = f"{dirname(abspath(__file__))}/data/mock.bmp"
else:
self._capture_path = "/dev/prucam"
self.image_size = self.read_image_size()
try:
self.image_size = self.read_image_size()
except FileNotFoundError:
self.state = CameraState.NOT_FOUND
logger.debug("Camera device not found")
else:
self.state = CameraState.RUNNING

def read_image_size(self):
"""Read dimensions of image from the camera"""
Expand All @@ -34,14 +44,10 @@ def read_image_size(self):

def read_context_setting(self, name: str) -> int:
"""'Read a context setting."""

context_path = "/sys/devices/platform/prucam/context_settings"
try:
with open(f"{context_path}/{name}", "r") as f:
value = int(f.read())
except FileNotFoundError:
raise CameraError(f"no sysfs attribute {name} for camera")
return value
with open(f"{context_path}/{name}", "r") as f:
value = int(f.read())
return value

def capture(self, color: bool = True) -> np.ndarray:
"""Capture an image
Expand Down

0 comments on commit 369dcf3

Please sign in to comment.