remove sleeps and improve queue logic (#7163)

This commit is contained in:
Josh Hawkins 2023-07-14 19:01:10 -05:00 committed by GitHub
parent 598ae98bfe
commit dc2e786a6f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -119,7 +119,7 @@ class PtzAutoTrackerThread(threading.Thread):
self.config = config self.config = config
def run(self): def run(self):
while not self.stop_event.is_set(): while not self.stop_event.wait(1):
for camera_name, cam in self.config.cameras.items(): for camera_name, cam in self.config.cameras.items():
if cam.onvif.autotracking.enabled: if cam.onvif.autotracking.enabled:
self.ptz_autotracker.camera_maintenance(camera_name) self.ptz_autotracker.camera_maintenance(camera_name)
@ -128,7 +128,7 @@ class PtzAutoTrackerThread(threading.Thread):
if self.ptz_autotracker.tracked_object.get(camera_name): if self.ptz_autotracker.tracked_object.get(camera_name):
self.ptz_autotracker.tracked_object[camera_name] = None self.ptz_autotracker.tracked_object[camera_name] = None
self.ptz_autotracker.tracked_object_previous[camera_name] = None self.ptz_autotracker.tracked_object_previous[camera_name] = None
time.sleep(1)
logger.info("Exiting autotracker...") logger.info("Exiting autotracker...")
@ -199,66 +199,39 @@ class PtzAutoTracker:
def _process_move_queue(self, camera): def _process_move_queue(self, camera):
while True: while True:
try: try:
if self.move_queues[camera].qsize() > 1: move_data = self.move_queues[camera].get()
# Accumulate values since last moved frame_time, pan, tilt = move_data
pan = 0
tilt = 0
while not self.move_queues[camera].empty(): # if we're receiving move requests during a PTZ move, ignore them
frame_time, queued_pan, queued_tilt = self.move_queues[ if ptz_moving_at_frame_time(
camera frame_time,
].queue[0] self.ptz_metrics[camera]["ptz_start_time"].value,
self.ptz_metrics[camera]["ptz_stop_time"].value,
# if we're receiving move requests during a PTZ move, ignore them ):
if ptz_moving_at_frame_time( # instead of dequeueing this might be a good place to preemptively move based
frame_time, # on an estimate - for fast moving objects, etc.
self.ptz_metrics[camera]["ptz_start_time"].value, logger.debug(
self.ptz_metrics[camera]["ptz_stop_time"].value, f"Move queue: PTZ moving, dequeueing move request - frame time: {frame_time}, final pan: {pan}, final tilt: {tilt}"
): )
self.move_queues[camera].get() continue
# instead of dequeueing this might be a good place to preemptively move based
# on an estimate - for fast moving objects, etc.
logger.debug(
f"Move queue: PTZ moving, dequeueing move request - frame time: {frame_time}, queued pan: {queued_pan}, queued tilt: {queued_tilt}, final pan: {pan}, final tilt: {tilt}"
)
else:
# TODO: this may need rethinking
logger.debug(
f"Move queue: PTZ NOT moving, frame time: {frame_time}, queued pan: {queued_pan}, queued tilt: {queued_tilt}, final pan: {pan}, final tilt: {tilt}"
)
_, queued_pan, queued_tilt = self.move_queues[camera].get()
# If exceeding the movement range, keep it in the queue and move now
if (
abs(pan + queued_pan) > 1.0
or abs(tilt + queued_tilt) > 1.0
):
logger.debug("Pan or tilt value exceeds 1.0")
break
pan += queued_pan
tilt += queued_tilt
else: else:
move_data = self.move_queues[camera].get() # on some cameras with cheaper motors it seems like small values can cause jerky movement
frame_time, pan, tilt = move_data # TODO: double check, might not need this
if abs(pan) > 0.02 or abs(tilt) > 0.02:
self.onvif._move_relative(camera, pan, tilt, 1)
else:
logger.debug(
f"Not moving, pan and tilt too small: {pan}, {tilt}"
)
# on some cameras with cheaper motors it seems like small values can cause jerky movement # Wait until the camera finishes moving
# TODO: double check, might not need this while not self.ptz_metrics[camera]["ptz_stopped"].is_set():
if abs(pan) > 0.02 or abs(tilt) > 0.02: # check if ptz is moving
self.onvif._move_relative(camera, pan, tilt, 1) self.onvif.get_camera_status(camera)
else:
logger.debug(f"Not moving, pan and tilt too small: {pan}, {tilt}")
# Wait until the camera finishes moving
while not self.ptz_metrics[camera]["ptz_stopped"].is_set():
# check if ptz is moving
self.onvif.get_camera_status(camera)
except queue.Empty: except queue.Empty:
time.sleep(0.1) continue
def _enqueue_move(self, camera, frame_time, pan, tilt): def _enqueue_move(self, camera, frame_time, pan, tilt):
move_data = (frame_time, pan, tilt) move_data = (frame_time, pan, tilt)