Skip to content

Commit

Permalink
remove default calibration files and update cal loading
Browse files Browse the repository at this point in the history
  • Loading branch information
aromanielloNTIA committed Mar 5, 2024
1 parent a9999c7 commit caa811a
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 23 deletions.
38 changes: 29 additions & 9 deletions src/initialization/sensor_loader.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
from scos_actions.calibration.differential_calibration import DifferentialCalibration
from scos_actions.calibration.sensor_calibration import SensorCalibration
from scos_actions.hardware.sensor import Sensor
from scos_actions.hardware.sigan_iface import SIGAN_SETTINGS_KEYS
from scos_actions.metadata.utils import construct_geojson_point

from utils.signals import register_component_with_status
Expand Down Expand Up @@ -58,11 +59,6 @@ def load_sensor(sensor_capabilities: dict) -> Sensor:
sensor_loc["z"] if "z" in sensor_loc else None,
)
switches = load_switches(settings.SWITCH_CONFIGS_DIR)
sensor_cal = get_calibration(settings.SENSOR_CALIBRATION_FILE, "sensor")
differential_cal = get_calibration(
settings.DIFFERENTIAL_CALIBRATION_FILE,
"differential",
)
preselector = load_preselector(
settings.PRESELECTOR_CONFIG,
settings.PRESELECTOR_MODULE,
Expand All @@ -87,16 +83,40 @@ def load_sensor(sensor_capabilities: dict) -> Sensor:
except Exception as ex:
logger.warning(f"unable to create signal analyzer: {ex}")

# Create sensor before handling calibrations
sensor = Sensor(
signal_analyzer=sigan,
# TODO GPS Not Implemented
capabilities=sensor_capabilities,
preselector=preselector,
switches=switches,
location=location,
sensor_cal=sensor_cal,
differential_cal=differential_cal,
sensor_calibration=None,
differential_cal=None,
)

# Calibration loading
if not settings.RUNNING_TESTS:
# Load the onboard cal file as the sensor calibration, if it exists
onboard_cal = get_calibration(settings.ONBOARD_CALIBRATION_FILE, "onboard")
if onboard_cal is not None:
sensor.sensor_calibration = onboard_cal
else:
# Otherwise, try using the sensor calibration file
sensor_cal = get_calibration(settings.SENSOR_CALIBRATION_FILE, "sensor")
if sensor_cal is not None:
sensor.sensor_calibration = sensor_cal
# Now run the calibration action defined in the environment
# TODO
# This will create an onboard_cal file if needed, and set it
# as the sensor's sensor_calibration.
# Now load the differential calibration, if it exists
differential_cal = get_calibration(
settings.DIFFERENTIAL_CALIBRATION_FILE,
"differential",
)
sensor.differential_calibration = differential_cal

return sensor


Expand Down Expand Up @@ -180,7 +200,7 @@ def get_calibration(
Load calibration data from file.
:param cal_file_path: Path to the JSON calibration file.
:param cal_type: Calibration type to load, either "sensor" or "differential"
:param cal_type: Calibration type to load: "onboard", "sensor" or "differential"
:return: The ``Calibration`` object, if loaded, or ``None`` if loading failed.
"""
try:
Expand All @@ -195,7 +215,7 @@ def get_calibration(
logger.debug(f"Loading calibration file: {cal_file_path}")
# Create calibration object
cal_file_path = Path(cal_file_path)
if cal_type.lower() == "sensor":
if cal_type.lower() in ["sensor", "onboard"]:
cal = SensorCalibration.from_json(cal_file_path)
elif cal_type.lower() == "differential":
cal = DifferentialCalibration.from_json(cal_file_path)
Expand Down
38 changes: 24 additions & 14 deletions src/sensor/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,29 +69,39 @@
ACTIONS_DIR = path.join(CONFIG_DIR, "actions")
DRIVERS_DIR = path.join(REPO_ROOT, "drivers")

DEFAULT_SENSOR_CALIBRATION_FILE = path.join(
CONFIG_DIR, "default_sensor_calibration.json"
)
DEFAULT_DIFFERENTIAL_CALIBRATION_FILE = path.join(
CONFIG_DIR, "default_differential_calibration.json"
)
os.environ["DEFAULT_SENSOR_CALIBRATION_FILE"] = str(DEFAULT_SENSOR_CALIBRATION_FILE)
os.environ["DEFAULT_DIFFERENTIAL_CALIBRATION_FILE"] = str(
DEFAULT_DIFFERENTIAL_CALIBRATION_FILE
)
# JSON configs
########### Calibration Files ##################

# Onboard calibration file; should only be autogenerated by actions.
# If present and parseable, this will be used in place of the SENSOR_CALIBRATION_FILE.
if path.exists(
onboard_cal_path := path.join(CONFIG_DIR, "onboard_sensor_calibration.json")
):
ONBOARD_SENSOR_CALIBRATION_FILE = onboard_cal_path
else:
ONBOARD_SENSOR_CALIBRATION_FILE = None
os.environ["ONBOARD_SENSOR_CALIBRATION_FILE"] = ONBOARD_SENSOR_CALIBRATION_FILE

# Sensor calibration file; should be provided manually. Will not be
# overwritten when calibration actions.
if path.exists(sensor_cal_path := path.join(CONFIG_DIR, "sensor_calibration.json")):
SENSOR_CALIBRATION_FILE = sensor_cal_path
else:
SENSOR_CALIBRATION_FILE = DEFAULT_SENSOR_CALIBRATION_FILE
SENSOR_CALIBRATION_FILE = None
os.environ["SENSOR_CALIBRATION_FILE"] = SENSOR_CALIBRATION_FILE

# Differential calibration file; should be provided manually. If present,
# this will be used to apply additional scaling on top of the sensor or onboard
# calibration. This can be used to shift the data reference point from the onboard
# calibration terminal to another point in the signal path.
if path.exists(diff_cal_path := path.join(CONFIG_DIR, "differential_calibration.json")):
DIFFERENTIAL_CALIBRATION_FILE = diff_cal_path
else:
DIFFERENTIAL_CALIBRATION_FILE = DEFAULT_DIFFERENTIAL_CALIBRATION_FILE
DIFFERENTIAL_CALIBRATION_FILE = None
os.environ["DIFFERENTIAL_CALIBRATION_FILE"] = DIFFERENTIAL_CALIBRATION_FILE

# Sensor Definition File
if path.exists(sensor_def_path := path.join(CONFIG_DIR, "sensor_definition.json")):
SENSOR_DEFINITION_FILE = sensor_def_path
os.environ["SENSOR_CALIBRATION_FILE"] = SENSOR_CALIBRATION_FILE
MEDIA_ROOT = path.join(REPO_ROOT, "files")
PRESELECTOR_CONFIG = path.join(CONFIG_DIR, "preselector_config.json")

Expand Down

0 comments on commit caa811a

Please sign in to comment.