Minor tidying to Picamera Thing

This commit is contained in:
Julian Stirling 2025-06-23 13:07:26 +01:00
parent dd63d74651
commit c659ba8f5e

View file

@ -97,7 +97,7 @@ class StreamingPiCamera2(BaseCamera):
def __init__(self, camera_num: int = 0): def __init__(self, camera_num: int = 0):
self.camera_num = camera_num self.camera_num = camera_num
self.camera_configs: dict[str, dict] = {} self.camera_configs: dict[str, dict] = {}
# NB persistent controls will be updated with settings, in __enter__. # Note persistent controls will be updated with settings, in __enter__.
self.persistent_controls = { self.persistent_controls = {
"AeEnable": False, "AeEnable": False,
"AnalogueGain": 1.0, "AnalogueGain": 1.0,
@ -129,25 +129,34 @@ class StreamingPiCamera2(BaseCamera):
""" """
with self.picamera() as cam: with self.picamera() as cam:
for i in range(discard_frames): for i in range(discard_frames):
# Discard frames, so we know our data is fresh # Discard frames, so data is fresh
cam.capture_metadata() cam.capture_metadata()
for k, v in cam.capture_metadata().items(): for key, value in cam.capture_metadata().items():
if k in self.persistent_controls: if key not in self.persistent_controls:
if k in self.persistent_control_tolerances: # Skip keys that are not persistent controls
if ( continue
np.abs(self.persistent_controls[k] - v) if self._control_value_within_tolerance(key, value):
< self.persistent_control_tolerances[k] logging.debug(
): "Ignoring a small change in persistent control %s from %s to "
logging.debug( "%s while updating persistent controls.",
f"Ignoring a small change in persistent control {k}" key,
f"from {self.persistent_controls[k]} to {v}" self.persistent_controls[key],
"while updating persistent controls." self.persistent_controls[key],
) )
continue # Ignore small changes, to avoid drift else:
self.persistent_controls[k] = v self.persistent_controls[key] = value
self.thing_settings.update( self.thing_settings.update(self.persistent_controls)
self.persistent_controls
) # TODO: Is this saving to the wrong place? def _control_value_within_tolerance(self, key: str, value: float) -> bool:
"""Return True if the updated value for a persistent control is within
tolerance of current value. This allows ignoring small changes
"""
if key not in self.persistent_control_tolerances:
# Not tolerance range set, so it is not within tollerance
return False
difference = np.abs(self.persistent_controls[key] - value)
return difference < self.persistent_control_tolerances[key]
def settings_to_persistent_controls(self): def settings_to_persistent_controls(self):
"""Update the persistent controls dict from the settings dict """Update the persistent controls dict from the settings dict
@ -246,7 +255,6 @@ class StreamingPiCamera2(BaseCamera):
so will fail if it's run before those are populated in `__enter__`. so will fail if it's run before those are populated in `__enter__`.
""" """
if "tuning" in self.thing_settings: if "tuning" in self.thing_settings:
# TODO: should this be a separate file?
self.tuning = self.thing_settings["tuning"].dict self.tuning = self.thing_settings["tuning"].dict
else: else:
logging.info("Did not find tuning in settings, reading from camera...") logging.info("Did not find tuning in settings, reading from camera...")
@ -288,6 +296,7 @@ class StreamingPiCamera2(BaseCamera):
self.populate_default_tuning() self.populate_default_tuning()
self.initialise_tuning() self.initialise_tuning()
self.initialise_picamera() self.initialise_picamera()
# populate sensor modes by reading the property
self.sensor_modes self.sensor_modes
self.settings_to_persistent_controls() self.settings_to_persistent_controls()
self.settings_to_properties() self.settings_to_properties()
@ -420,10 +429,8 @@ class StreamingPiCamera2(BaseCamera):
self.lores_mjpeg_stream.stop() self.lores_mjpeg_stream.stop()
logging.info("Stopped MJPEG stream.") logging.info("Stopped MJPEG stream.")
# Increase the resolution for taking an image # Adding a sleep to prevent camera getting confused by rapid commands
time.sleep( time.sleep(0.2)
0.2
) # Sprinkled a sleep to prevent camera getting confused by rapid commands
@thing_action @thing_action
def capture_image( def capture_image(