Skip to content

Commit

Permalink
IMPROVEMENT: Try to find the vehicle type as late as possible, derivi…
Browse files Browse the repository at this point in the history
…ng it from vehicle_components.json as a last resort
  • Loading branch information
amilcarlucas committed Jul 11, 2024
1 parent f43298a commit 79b79fc
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 20 deletions.
9 changes: 3 additions & 6 deletions MethodicConfigurator/ardupilot_methodic_configurator.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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


Expand Down Expand Up @@ -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:
Expand All @@ -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)
Expand Down
30 changes: 17 additions & 13 deletions MethodicConfigurator/backend_filesystem.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand All @@ -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)
Expand Down Expand Up @@ -514,15 +523,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',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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__))]
Expand Down
17 changes: 17 additions & 0 deletions MethodicConfigurator/backend_filesystem_vehicle_components.py
Original file line number Diff line number Diff line change
Expand Up @@ -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']
Expand All @@ -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():
"""
Expand Down
4 changes: 3 additions & 1 deletion MethodicConfigurator/frontend_tkinter_component_editor.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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,
Expand Down

0 comments on commit 79b79fc

Please sign in to comment.