Skip to content

Commit 6a968a8

Browse files
committed
IMPROVEMENT: Try to find the vehicle type as late as possible, deriving it from vehicle_components.json as a last resort
1 parent f43298a commit 6a968a8

5 files changed

+42
-20
lines changed

MethodicConfigurator/ardupilot_methodic_configurator.py

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
from logging import getLevelName as logging_getLevelName
1414
from logging import debug as logging_debug
1515
from logging import info as logging_info
16-
from logging import warning as logging_warning
16+
#from logging import warning as logging_warning
1717
from logging import error as logging_error
1818
from sys import exit as sys_exit
1919

@@ -80,9 +80,6 @@ def connect_to_fc_and_read_parameters(args):
8080
else:
8181
logging_info("Vehicle type explicitly set to %s.", vehicle_type)
8282

83-
if vehicle_type == "": # did not guess it, default to ArduCopter
84-
vehicle_type = "ArduCopter"
85-
logging_warning("Could not detect vehicle type. Defaulting to ArduCopter.")
8683
return flight_controller,vehicle_type
8784

8885

@@ -130,7 +127,7 @@ def main():
130127
raise
131128

132129
# Get the list of intermediate parameter files files that will be processed sequentially
133-
files = list(local_filesystem.file_parameters.keys())
130+
files = list(local_filesystem.file_parameters.keys()) if local_filesystem.file_parameters else []
134131

135132
vehicle_dir_window = None
136133
if not files:
@@ -139,7 +136,7 @@ def main():
139136

140137
start_file = local_filesystem.get_start_file(args.n)
141138

142-
component_editor(args, flight_controller, vehicle_type, local_filesystem, vehicle_dir_window)
139+
component_editor(args, flight_controller, local_filesystem.vehicle_type, local_filesystem, vehicle_dir_window)
143140

144141
# Call the GUI function with the starting intermediate parameter file
145142
ParameterEditorWindow(start_file, flight_controller, local_filesystem, VERSION)

MethodicConfigurator/backend_filesystem.py

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -85,18 +85,32 @@ def __init__(self, vehicle_dir: str, vehicle_type: str, fw_version: str, allow_e
8585
VehicleComponents.__init__(self)
8686
ConfigurationSteps.__init__(self, vehicle_dir, vehicle_type)
8787
ProgramSettings.__init__(self)
88+
self.vehicle_type = vehicle_type
8889
self.fw_version = fw_version
8990
self.allow_editing_template_files = allow_editing_template_files
9091
if vehicle_dir is not None:
9192
self.re_init(vehicle_dir, vehicle_type)
9293

9394
def re_init(self, vehicle_dir: str, vehicle_type: str):
94-
ConfigurationSteps.re_init(self, vehicle_dir, vehicle_type)
9595
self.vehicle_dir = vehicle_dir
96-
self.vehicle_type = vehicle_type
9796
self.param_default_dict = {}
9897
self.doc_dict = {}
9998

99+
if not self.load_vehicle_components_json_data(vehicle_dir):
100+
return
101+
102+
if self.fw_version is None:
103+
self.fw_version = self.get_fc_fw_version_from_vehicle_components_json()
104+
105+
if vehicle_type == "":
106+
vehicle_type = self.get_fc_fw_type_from_vehicle_components_json()
107+
if vehicle_type == "":
108+
vehicle_type = "ArduCopter"
109+
logging_warning("Could not detect vehicle type. Defaulting to %s.", vehicle_type)
110+
self.vehicle_type = vehicle_type
111+
112+
ConfigurationSteps.re_init(self, vehicle_dir, vehicle_type)
113+
100114
# Rename parameter files if some new files got added to the vehicle directory
101115
self.rename_parameter_files()
102116

@@ -105,11 +119,6 @@ def re_init(self, vehicle_dir: str, vehicle_type: str):
105119
if not self.file_parameters:
106120
return # No files intermediate parameters files found, no need to continue, the rest needs them
107121

108-
self.load_vehicle_components_json_data(vehicle_dir)
109-
110-
if self.fw_version is None:
111-
self.fw_version = self.get_fc_fw_version_from_vehicle_components_json()
112-
113122
# Read ArduPilot parameter documentation
114123
xml_url = get_xml_url(vehicle_type, self.fw_version)
115124
xml_dir = get_xml_dir(vehicle_dir)
@@ -514,15 +523,10 @@ def copy_fc_params_values_to_template_created_vehicle_files(self, fc_parameters:
514523
Par.export_to_param(Par.format_params(param_dict), os_path.join(self.vehicle_dir, param_filename))
515524
return ''
516525

517-
@staticmethod
518-
def supported_vehicles():
519-
return ['AP_Periph', 'AntennaTracker', 'ArduCopter', 'ArduPlane',
520-
'ArduSub', 'Blimp', 'Heli', 'Rover', 'SITL']
521-
522526
@staticmethod
523527
def add_argparse_arguments(parser):
524528
parser.add_argument('-t', '--vehicle-type',
525-
choices=LocalFilesystem.supported_vehicles(),
529+
choices=VehicleComponents.supported_vehicles(),
526530
default='',
527531
help='The type of the vehicle. Defaults to ArduCopter')
528532
parser.add_argument('--vehicle-dir',

MethodicConfigurator/backend_filesystem_configuration_steps.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,8 @@ def __init__(self, _vehicle_dir: str, vehicle_type: str):
4343
self.log_loaded_file = False
4444

4545
def re_init(self, vehicle_dir: str, vehicle_type: str):
46+
if vehicle_type == '':
47+
return
4648
self.configuration_steps_filename = vehicle_type + "_configuration_steps.json"
4749
# Define a list of directories to search for the configuration_steps_filename file
4850
search_directories = [vehicle_dir, os_path.dirname(os_path.abspath(__file__))]

MethodicConfigurator/backend_filesystem_vehicle_components.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,18 @@ def save_vehicle_components_json_data(self, data, vehicle_dir: str) -> bool:
6060
return True
6161
return False
6262

63+
def get_fc_fw_type_from_vehicle_components_json(self) -> str:
64+
if self.vehicle_components and 'Components' in self.vehicle_components:
65+
components = self.vehicle_components['Components']
66+
else:
67+
components = None
68+
if components:
69+
fw_type = components.get('Flight Controller', {}).get('Firmware', {}).get('Type', '')
70+
if fw_type in self.supported_vehicles():
71+
return fw_type
72+
logging_error(f"Firmware type {fw_type} in {self.vehicle_components_json_filename} is not supported")
73+
return ""
74+
6375
def get_fc_fw_version_from_vehicle_components_json(self) -> str:
6476
if self.vehicle_components and 'Components' in self.vehicle_components:
6577
components = self.vehicle_components['Components']
@@ -73,6 +85,11 @@ def get_fc_fw_version_from_vehicle_components_json(self) -> str:
7385
logging_error(f"FW version string {version_str} on {self.vehicle_components_json_filename} is invalid")
7486
return None
7587

88+
@staticmethod
89+
def supported_vehicles():
90+
return ['AP_Periph', 'AntennaTracker', 'ArduCopter', 'ArduPlane',
91+
'ArduSub', 'Blimp', 'Heli', 'Rover', 'SITL']
92+
7693
@staticmethod
7794
def get_vehicle_components_overviews():
7895
"""

MethodicConfigurator/frontend_tkinter_component_editor.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@
2222

2323
from MethodicConfigurator.backend_filesystem import LocalFilesystem
2424

25+
from MethodicConfigurator.backend_filesystem_vehicle_components import VehicleComponents
26+
2527
from MethodicConfigurator.battery_cell_voltages import BatteryCell
2628

2729
from MethodicConfigurator.frontend_tkinter_component_editor_base import ComponentEditorWindowBase
@@ -144,7 +146,7 @@ def add_entry_or_combobox(self, value, entry_frame, path):
144146

145147
combobox_config = {
146148
('Flight Controller', 'Firmware', 'Type'): {
147-
"values": LocalFilesystem.supported_vehicles(),
149+
"values": VehicleComponents.supported_vehicles(),
148150
},
149151
('RC Receiver', 'FC Connection', 'Type'): {
150152
"values": ["RCin/SBUS"] + serial_ports + can_ports,

0 commit comments

Comments
 (0)