Skip to content

Commit

Permalink
Merge pull request #18 from ralatsdc/rl/make-auto-focus-option-dirty
Browse files Browse the repository at this point in the history
Make auto focus option dirty
  • Loading branch information
mchadwick-iqt authored Aug 28, 2023
2 parents 60e67df + a1d6465 commit ad2bb09
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 27 deletions.
12 changes: 6 additions & 6 deletions axis-ptz-controller.env
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@ MQTT_IP=mqtt
CAMERA_IP=<CAMERA_IP>
CAMERA_USER=<CAMERA_USERNAME>
CAMERA_PASSWORD=<CAMERA_PASSWORD>
CONFIG_TOPIC="skyscan/config/json"
ORIENTATION_TOPIC="skyscan/calibration/json"
CONFIG_TOPIC=skyscan/config/json
ORIENTATION_TOPIC=skyscan/calibration/json
OBJECT_TOPIC=/Multimodal/${HOSTNAME}/selection/edgetech-axis-ptz-controller/JSON
CAPTURE_TOPIC="skyscan/captures/json"
LOGGER_TOPIC="skyscan/logger/json"
CAPTURE_TOPIC=skyscan/captures/json
LOGGER_TOPIC=skyscan/logger/json
HEARTBEAT_INTERVAL=10
TRIPOD_LATITUDE=<CAMERA_LAT>
TRIPOD_LONGITUDE=<CAMERA_LON>
Expand All @@ -24,11 +24,11 @@ TILT_RATE_MIN=1.8
TILT_RATE_MAX=150.0
FOCUS_SLOPE=0.0006
FOCUS_INTERCEPT=54
JPEG_RESOLUTION="1920x1080"
JPEG_RESOLUTION=1920x1080
JPEG_COMPRESSION=5
USE_MQTT=True
USE_CAMERA=False
AUTO_FOCUS=False
INCLUDE_AGE=True
LOG_TO_MQTT=False
CONTINUE_ON_EXCEPTION=False

59 changes: 41 additions & 18 deletions axis-ptz-controller/axis_ptz_controller.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ def __init__(
jpeg_compression: int = 5,
use_mqtt: bool = True,
use_camera: bool = True,
auto_focus: bool = False,
include_age: bool = True,
log_to_mqtt: bool = False,
continue_on_exception: bool = False,
Expand Down Expand Up @@ -133,6 +134,8 @@ def __init__(
Flag to use MQTT, or not
use_camera: bool
Flag to use camera configuration and control, or not
auto_focus: bool
Flag to auto focus, or not
include_age: bool
Flag to include object message age in lead time, or not
log_to_mqtt: bool
Expand Down Expand Up @@ -179,6 +182,7 @@ def __init__(
self.jpeg_compression = jpeg_compression
self.use_mqtt = use_mqtt
self.use_camera = use_camera
self.auto_focus = auto_focus
self.include_age = include_age
self.log_to_mqtt = log_to_mqtt
self.continue_on_exception = continue_on_exception
Expand Down Expand Up @@ -316,10 +320,18 @@ def __init__(

# Initialize camera pointing
if self.use_camera:
logging.debug(f"Absolute move to pan: {self.rho_c}, and tilt: {self.tau_c}")
self.camera_control.absolute_move(
self.rho_c, self.tau_c, self.zoom, 50, self.focus
)
if self.auto_focus:
logging.info(
f"Absolute move to pan: {self.rho_c}, and tilt: {self.tau_c}, with zoom: {self.zoom}"
)
self.camera_control.absolute_move(self.rho_c, self.tau_c, self.zoom, 50)
else:
logging.info(
f"Absolute move to pan: {self.rho_c}, and tilt: {self.tau_c}, with zoom: {self.zoom}, and focus: {self.focus}"
)
self.camera_control.absolute_move(
self.rho_c, self.tau_c, self.zoom, 50, self.focus
)

# Log configuration parameters
logging.info(
Expand Down Expand Up @@ -355,6 +367,7 @@ def __init__(
jpeg_compression = {jpeg_compression}
use_mqtt = {use_mqtt}
use_camera = {use_camera}
auto_focus: {auto_focus}
include_age = {include_age}
log_to_mqtt = {log_to_mqtt}
continue_on_exception = {continue_on_exception}
Expand Down Expand Up @@ -639,12 +652,20 @@ def _object_callback(
# Point the camera at any new object directly
if self.object_id != object_id:
self.object_id = object_id
logging.info(
f"Absolute move to pan: {self.rho_o}, and tilt: {self.tau_o}, with zoom: {self.zoom}, and focus: {self.focus}"
)
self.camera_control.absolute_move(
self.rho_o, self.tau_o, self.zoom, 50, self.focus
)
if self.auto_focus:
logging.info(
f"Absolute move to pan: {self.rho_o}, and tilt: {self.tau_o}, with zoom: {self.zoom}"
)
self.camera_control.absolute_move(
self.rho_o, self.tau_o, self.zoom, 50
)
else:
logging.info(
f"Absolute move to pan: {self.rho_o}, and tilt: {self.tau_o}, with zoom: {self.zoom}, and focus: {self.focus}"
)
self.camera_control.absolute_move(
self.rho_o, self.tau_o, self.zoom, 50, self.focus
)
duration = max(
math.fabs(self.rho_c - self.rho_o) / (self.pan_rate_max / 2),
math.fabs(self.tau_c - self.tau_o) / (self.tilt_rate_max / 2),
Expand Down Expand Up @@ -708,14 +729,15 @@ def _object_callback(
# Compute and set focus, command camera pan and tilt rates,
# and begin capturing images, if needed
if self.use_camera:
self.focus = int(
(self.focus_max - self.focus_min)
* (self.focus_slope * self.distance3d + self.focus_intercept)
/ 100.0
+ self.focus_min
) # [%]
logging.debug(f"Commanding focus: {self.focus}")
self.camera_control.set_focus(self.focus)
if not self.auto_focus:
self.focus = int(
(self.focus_max - self.focus_min)
* (self.focus_slope * self.distance3d + self.focus_intercept)
/ 100.0
+ self.focus_min
) # [%]
logging.debug(f"Commanding focus: {self.focus}")
self.camera_control.set_focus(self.focus)

pan_rate_index = self._compute_pan_rate_index(self.rho_dot_c)
tilt_rate_index = self._compute_tilt_rate_index(self.tau_dot_c)
Expand Down Expand Up @@ -1027,6 +1049,7 @@ def main(self) -> None:
jpeg_compression=int(os.getenv("JPEG_COMPRESSION", 5)),
use_mqtt=ast.literal_eval(os.getenv("USE_MQTT", "True")),
use_camera=ast.literal_eval(os.getenv("USE_CAMERA", "True")),
auto_focus=ast.literal_eval(os.getenv("AUTO_FOCUS", "True")),
include_age=ast.literal_eval(os.getenv("INCLUDE_AGE", "True")),
log_to_mqtt=ast.literal_eval(os.getenv("LOG_TO_MQTT", "False")),
continue_on_exception=ast.literal_eval(
Expand Down
9 changes: 6 additions & 3 deletions axis-ptz-controller/camera_control.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,12 @@ def absolute_move(
Returns the response from the device to the command sent.
"""
return self._camera_command(
{"pan": pan, "tilt": tilt, "zoom": zoom, "speed": speed, "focus": focus}
)
command = {"pan": pan, "tilt": tilt, "zoom": zoom, "speed": speed}
# Defensively only set focus if needed
if focus is not None:
command["focus"] = focus
logging.info(f"command: {command}")
return self._camera_command(command)

def get_ptz(self) -> Tuple[float, float, float, float]:
"""
Expand Down

0 comments on commit ad2bb09

Please sign in to comment.