diff --git a/MethodicConfigurator/ardupilot_methodic_configurator.py b/MethodicConfigurator/ardupilot_methodic_configurator.py index f5736b5..92c2d11 100755 --- a/MethodicConfigurator/ardupilot_methodic_configurator.py +++ b/MethodicConfigurator/ardupilot_methodic_configurator.py @@ -13,7 +13,7 @@ from logging import getLevelName as logging_getLevelName from logging import debug as logging_debug from logging import info as logging_info -from logging import warning as logging_warning +#from logging import warning as logging_warning from logging import error as logging_error from sys import exit as sys_exit @@ -80,9 +80,6 @@ def connect_to_fc_and_read_parameters(args): else: logging_info("Vehicle type explicitly set to %s.", vehicle_type) - if vehicle_type == "": # did not guess it, default to ArduCopter - vehicle_type = "ArduCopter" - logging_warning("Could not detect vehicle type. Defaulting to ArduCopter.") return flight_controller,vehicle_type @@ -130,7 +127,7 @@ def main(): raise # Get the list of intermediate parameter files files that will be processed sequentially - files = list(local_filesystem.file_parameters.keys()) + files = list(local_filesystem.file_parameters.keys()) if local_filesystem.file_parameters else [] vehicle_dir_window = None if not files: @@ -139,7 +136,7 @@ def main(): start_file = local_filesystem.get_start_file(args.n) - component_editor(args, flight_controller, vehicle_type, local_filesystem, vehicle_dir_window) + component_editor(args, flight_controller, local_filesystem.vehicle_type, local_filesystem, vehicle_dir_window) # Call the GUI function with the starting intermediate parameter file ParameterEditorWindow(start_file, flight_controller, local_filesystem, VERSION) diff --git a/MethodicConfigurator/backend_filesystem.py b/MethodicConfigurator/backend_filesystem.py index 877f821..7755880 100644 --- a/MethodicConfigurator/backend_filesystem.py +++ b/MethodicConfigurator/backend_filesystem.py @@ -85,18 +85,32 @@ def __init__(self, vehicle_dir: str, vehicle_type: str, fw_version: str, allow_e VehicleComponents.__init__(self) ConfigurationSteps.__init__(self, vehicle_dir, vehicle_type) ProgramSettings.__init__(self) + self.vehicle_type = vehicle_type self.fw_version = fw_version self.allow_editing_template_files = allow_editing_template_files if vehicle_dir is not None: self.re_init(vehicle_dir, vehicle_type) def re_init(self, vehicle_dir: str, vehicle_type: str): - ConfigurationSteps.re_init(self, vehicle_dir, vehicle_type) self.vehicle_dir = vehicle_dir - self.vehicle_type = vehicle_type self.param_default_dict = {} self.doc_dict = {} + if not self.load_vehicle_components_json_data(vehicle_dir): + return + + if self.fw_version is None: + self.fw_version = self.get_fc_fw_version_from_vehicle_components_json() + + if vehicle_type == "": + vehicle_type = self.get_fc_fw_type_from_vehicle_components_json() + if vehicle_type == "": + vehicle_type = "ArduCopter" + logging_warning("Could not detect vehicle type. Defaulting to %s.", vehicle_type) + self.vehicle_type = vehicle_type + + ConfigurationSteps.re_init(self, vehicle_dir, vehicle_type) + # Rename parameter files if some new files got added to the vehicle directory self.rename_parameter_files() @@ -105,11 +119,6 @@ def re_init(self, vehicle_dir: str, vehicle_type: str): if not self.file_parameters: return # No files intermediate parameters files found, no need to continue, the rest needs them - self.load_vehicle_components_json_data(vehicle_dir) - - if self.fw_version is None: - self.fw_version = self.get_fc_fw_version_from_vehicle_components_json() - # Read ArduPilot parameter documentation xml_url = get_xml_url(vehicle_type, self.fw_version) xml_dir = get_xml_dir(vehicle_dir) @@ -507,15 +516,10 @@ def copy_fc_params_values_to_template_created_vehicle_files(self, fc_parameters: Par.export_to_param(Par.format_params(param_dict), os_path.join(self.vehicle_dir, param_filename)) return '' - @staticmethod - def supported_vehicles(): - return ['AP_Periph', 'AntennaTracker', 'ArduCopter', 'ArduPlane', - 'ArduSub', 'Blimp', 'Heli', 'Rover', 'SITL'] - @staticmethod def add_argparse_arguments(parser): parser.add_argument('-t', '--vehicle-type', - choices=LocalFilesystem.supported_vehicles(), + choices=VehicleComponents.supported_vehicles(), default='', help='The type of the vehicle. Defaults to ArduCopter') parser.add_argument('--vehicle-dir', diff --git a/MethodicConfigurator/backend_filesystem_configuration_steps.py b/MethodicConfigurator/backend_filesystem_configuration_steps.py index 5c32e08..c1e3a86 100644 --- a/MethodicConfigurator/backend_filesystem_configuration_steps.py +++ b/MethodicConfigurator/backend_filesystem_configuration_steps.py @@ -43,6 +43,8 @@ def __init__(self, _vehicle_dir: str, vehicle_type: str): self.log_loaded_file = False def re_init(self, vehicle_dir: str, vehicle_type: str): + if vehicle_type == '': + return self.configuration_steps_filename = vehicle_type + "_configuration_steps.json" # Define a list of directories to search for the configuration_steps_filename file search_directories = [vehicle_dir, os_path.dirname(os_path.abspath(__file__))] diff --git a/MethodicConfigurator/backend_filesystem_vehicle_components.py b/MethodicConfigurator/backend_filesystem_vehicle_components.py index bc9881e..47e52fe 100644 --- a/MethodicConfigurator/backend_filesystem_vehicle_components.py +++ b/MethodicConfigurator/backend_filesystem_vehicle_components.py @@ -60,6 +60,18 @@ def save_vehicle_components_json_data(self, data, vehicle_dir: str) -> bool: return True return False + def get_fc_fw_type_from_vehicle_components_json(self) -> str: + if self.vehicle_components and 'Components' in self.vehicle_components: + components = self.vehicle_components['Components'] + else: + components = None + if components: + fw_type = components.get('Flight Controller', {}).get('Firmware', {}).get('Type', '') + if fw_type in self.supported_vehicles(): + return fw_type + logging_error(f"Firmware type {fw_type} in {self.vehicle_components_json_filename} is not supported") + return "" + def get_fc_fw_version_from_vehicle_components_json(self) -> str: if self.vehicle_components and 'Components' in self.vehicle_components: components = self.vehicle_components['Components'] @@ -73,6 +85,11 @@ def get_fc_fw_version_from_vehicle_components_json(self) -> str: logging_error(f"FW version string {version_str} on {self.vehicle_components_json_filename} is invalid") return None + @staticmethod + def supported_vehicles(): + return ['AP_Periph', 'AntennaTracker', 'ArduCopter', 'ArduPlane', + 'ArduSub', 'Blimp', 'Heli', 'Rover', 'SITL'] + @staticmethod def get_vehicle_components_overviews(): """ diff --git a/MethodicConfigurator/frontend_tkinter_component_editor.py b/MethodicConfigurator/frontend_tkinter_component_editor.py index d9edfff..11fd9e3 100644 --- a/MethodicConfigurator/frontend_tkinter_component_editor.py +++ b/MethodicConfigurator/frontend_tkinter_component_editor.py @@ -22,6 +22,8 @@ from MethodicConfigurator.backend_filesystem import LocalFilesystem +from MethodicConfigurator.backend_filesystem_vehicle_components import VehicleComponents + from MethodicConfigurator.battery_cell_voltages import BatteryCell from MethodicConfigurator.frontend_tkinter_component_editor_base import ComponentEditorWindowBase @@ -144,7 +146,7 @@ def add_entry_or_combobox(self, value, entry_frame, path): combobox_config = { ('Flight Controller', 'Firmware', 'Type'): { - "values": LocalFilesystem.supported_vehicles(), + "values": VehicleComponents.supported_vehicles(), }, ('RC Receiver', 'FC Connection', 'Type'): { "values": ["RCin/SBUS"] + serial_ports + can_ports,