forked from ArduPilot/MethodicConfigurator
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdata_model_flightcontroller_info.py
More file actions
299 lines (256 loc) · 12.7 KB
/
data_model_flightcontroller_info.py
File metadata and controls
299 lines (256 loc) · 12.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
"""
Manages FC information using FC interface.
This file is part of ArduPilot Methodic Configurator. https://github.com/ArduPilot/MethodicConfigurator
SPDX-FileCopyrightText: 2024-2026 Amilcar do Carmo Lucas <amilcar.lucas@iav.de>
SPDX-License-Identifier: GPL-3.0-or-later
"""
from collections.abc import Sequence
from logging import info as logging_info
from typing import Union
from pymavlink import mavutil
# import pymavlink.dialects.v20.ardupilotmega
from ardupilot_methodic_configurator import _
from ardupilot_methodic_configurator.data_model_fc_ids import (
APJ_BOARD_ID_MCU_SERIES_DICT,
APJ_BOARD_ID_NAME_DICT,
APJ_BOARD_ID_VENDOR_DICT,
VID_PID_PRODUCT_DICT,
VID_VENDOR_DICT,
)
class FlightControllerInfo: # pylint: disable=too-many-instance-attributes
"""
Handle flight controller information.
It includes methods for setting various attributes such as system ID, component ID,
autopilot type, vehicle type, and capabilities among others.
"""
def __init__(self) -> None:
self.system_id = ""
self.component_id = ""
self.autopilot = ""
self.vehicle_type = ""
self.firmware_type = ""
self.mav_type = ""
self.flight_sw_version = ""
self.flight_sw_version_and_type = ""
self.board_version = ""
self.apj_board_id = ""
self.flight_custom_version = ""
self.os_custom_version = ""
self.vendor = ""
self.vendor_id = ""
self.vendor_and_vendor_id = ""
self.product = ""
self.product_id = ""
self.product_and_product_id = ""
self.mcu_series = ""
self.is_supported = False
self.is_mavftp_supported = False
self.capabilities: dict[str, str] = {}
def reset(self) -> None:
"""Reset all cached flight controller metadata."""
self.system_id = ""
self.component_id = ""
self.autopilot = ""
self.vehicle_type = ""
self.firmware_type = ""
self.mav_type = ""
self.flight_sw_version = ""
self.flight_sw_version_and_type = ""
self.board_version = ""
self.apj_board_id = ""
self.flight_custom_version = ""
self.os_custom_version = ""
self.vendor = ""
self.vendor_id = ""
self.vendor_and_vendor_id = ""
self.product = ""
self.product_id = ""
self.product_and_product_id = ""
self.mcu_series = ""
self.is_supported = False
self.is_mavftp_supported = False
self.capabilities.clear()
def get_info(self) -> dict[str, Union[str, dict[str, str]]]:
return {
_("USB Vendor"): self.vendor_and_vendor_id,
_("USB Product"): self.product_and_product_id,
_("Board Type"): self.apj_board_id,
_("Hardware Version"): self.board_version,
_("Autopilot Type"): self.autopilot,
_("ArduPilot Vehicle Type"): self.vehicle_type,
_("ArduPilot FW Type"): self.firmware_type,
_("MAV Type"): self.mav_type,
_("Firmware Version"): self.flight_sw_version_and_type,
_("Git Hash"): self.flight_custom_version,
_("OS Git Hash"): self.os_custom_version,
_("Capabilities"): self.capabilities,
_("System ID"): self.system_id,
_("Component ID"): self.component_id,
_("MCU Series"): self.mcu_series,
}
def set_system_id_and_component_id(self, system_id: str, component_id: str) -> None:
self.system_id = system_id
self.component_id = component_id
def set_autopilot(self, autopilot: int) -> None:
self.autopilot = self.__decode_mav_autopilot(autopilot)
self.is_supported = autopilot == mavutil.mavlink.MAV_AUTOPILOT_ARDUPILOTMEGA
def set_type(self, mav_type: int) -> None:
self.vehicle_type = self.__classify_vehicle_type(mav_type)
self.mav_type = self.__decode_mav_type(mav_type)
def set_flight_sw_version(self, version: int) -> None:
v_major, v_minor, v_patch, v_fw_type = self.__decode_flight_sw_version(version)
self.flight_sw_version = f"{v_major}.{v_minor}.{v_patch}"
self.flight_sw_version_and_type = self.flight_sw_version + " " + v_fw_type
def set_board_version(self, board_version: int) -> None:
self.board_version = str(board_version & 0x0FFFF)
apj_board_id = board_version >> 16
self.apj_board_id = str(apj_board_id)
self.firmware_type = str(",".join(APJ_BOARD_ID_NAME_DICT.get(apj_board_id, [_("Unknown")])))
vendor_derived_from_apj_board_id = str(",".join(APJ_BOARD_ID_VENDOR_DICT.get(apj_board_id, ["ArduPilot"])))
if vendor_derived_from_apj_board_id != "ArduPilot" and self.vendor in ["ArduPilot", _("Unknown")]:
self.vendor = vendor_derived_from_apj_board_id
self.mcu_series = str(",".join(APJ_BOARD_ID_MCU_SERIES_DICT.get(apj_board_id, [_("Unknown")])))
def set_flight_custom_version(self, flight_custom_version: Sequence[int]) -> None:
self.flight_custom_version = "".join(chr(c) for c in flight_custom_version)
def set_os_custom_version(self, os_custom_version: Sequence[int]) -> None:
self.os_custom_version = "".join(chr(c) for c in os_custom_version)
def set_usb_vendor_and_product_ids(self, vendor_id: int, product_id: int) -> None:
self.vendor_id = f"0x{vendor_id:04X}" if vendor_id else _("Unknown")
self.vendor = str(",".join(VID_VENDOR_DICT.get(vendor_id, [_("Unknown")])))
self.vendor_and_vendor_id = f"{self.vendor} ({self.vendor_id})"
self.product_id = f"0x{product_id:04X}" if product_id else _("Unknown")
self.product = str(",".join(VID_PID_PRODUCT_DICT.get((vendor_id, product_id), [_("Unknown")])))
self.product_and_product_id = f"{self.product} ({self.product_id})"
def set_capabilities(self, capabilities: int) -> None:
self.capabilities = self.__decode_flight_capabilities(capabilities)
self.is_mavftp_supported = bool(capabilities & mavutil.mavlink.MAV_PROTOCOL_CAPABILITY_FTP)
@staticmethod
def __decode_flight_sw_version(flight_sw_version: int) -> tuple[int, int, int, str]:
"""
Decode 32 bit flight_sw_version mavlink parameter.
corresponds to ArduPilot encoding in GCS_MAVLINK::send_autopilot_version.
"""
fw_type_id = (flight_sw_version >> 0) % 256 # E221, E222
patch = (flight_sw_version >> 8) % 256 # E221, E222
minor = (flight_sw_version >> 16) % 256 # E221
major = (flight_sw_version >> 24) % 256 # E221
if fw_type_id == 0:
fw_type = "dev"
elif fw_type_id == 64:
fw_type = "alpha"
elif fw_type_id == 128:
fw_type = "beta"
elif fw_type_id == 192:
fw_type = "rc"
elif fw_type_id == 255:
fw_type = "official"
else:
fw_type = "undefined"
return major, minor, patch, fw_type
@staticmethod
def __decode_flight_capabilities(capabilities: int) -> dict[str, str]:
"""
Decode 32 bit flight controller capabilities bitmask mavlink parameter.
Returns a dict of concise English descriptions of each active capability.
"""
capabilities_dict: dict[str, str] = {}
# Iterate through each bit in the capabilities bitmask
for bit in range(32):
# Check if the bit is set
if capabilities & (1 << bit):
# Use the bit value to get the corresponding capability enum
capability = mavutil.mavlink.enums["MAV_PROTOCOL_CAPABILITY"].get(1 << bit, "Unknown capability")
if hasattr(capability, "description"):
# Append the abbreviated name and description of the capability dictionary
capabilities_dict[capability.name.replace("MAV_PROTOCOL_CAPABILITY_", "")] = capability.description
else:
capabilities_dict[f"BIT{bit}"] = capability
return capabilities_dict
# see for more info:
# import pymavlink.dialects.v20.ardupilotmega
# pymavlink.dialects.v20.ardupilotmega.enums["MAV_TYPE"]
@staticmethod
def __decode_mav_type(mav_type: int) -> str:
return str(
mavutil.mavlink.enums["MAV_TYPE"].get(mav_type, mavutil.mavlink.EnumEntry("None", "Unknown type")).description
)
@staticmethod
def __decode_mav_autopilot(mav_autopilot: int) -> str:
return str(
mavutil.mavlink.enums["MAV_AUTOPILOT"]
.get(mav_autopilot, mavutil.mavlink.EnumEntry("None", "Unknown type"))
.description
)
@staticmethod
def __classify_vehicle_type(mav_type_int: int) -> str:
"""
Classify the vehicle type based on the MAV_TYPE enum.
Args:
mav_type_int (int): The MAV_TYPE enum value.
Returns:
str: The classified vehicle type.
"""
# Define the mapping from MAV_TYPE_* integer to vehicle type category
mav_type_to_vehicle_type: dict[int, str] = {
mavutil.mavlink.MAV_TYPE_FIXED_WING: "ArduPlane",
mavutil.mavlink.MAV_TYPE_QUADROTOR: "ArduCopter",
mavutil.mavlink.MAV_TYPE_COAXIAL: "Heli",
mavutil.mavlink.MAV_TYPE_HELICOPTER: "Heli",
mavutil.mavlink.MAV_TYPE_ANTENNA_TRACKER: "AntennaTracker",
mavutil.mavlink.MAV_TYPE_GCS: "AP_Periph",
mavutil.mavlink.MAV_TYPE_AIRSHIP: "ArduBlimp",
mavutil.mavlink.MAV_TYPE_FREE_BALLOON: "ArduBlimp",
mavutil.mavlink.MAV_TYPE_ROCKET: "ArduCopter",
mavutil.mavlink.MAV_TYPE_GROUND_ROVER: "Rover",
mavutil.mavlink.MAV_TYPE_SURFACE_BOAT: "Rover",
mavutil.mavlink.MAV_TYPE_SUBMARINE: "ArduSub",
mavutil.mavlink.MAV_TYPE_HEXAROTOR: "ArduCopter",
mavutil.mavlink.MAV_TYPE_OCTOROTOR: "ArduCopter",
mavutil.mavlink.MAV_TYPE_TRICOPTER: "ArduCopter",
mavutil.mavlink.MAV_TYPE_FLAPPING_WING: "ArduPlane",
mavutil.mavlink.MAV_TYPE_KITE: "ArduPlane",
mavutil.mavlink.MAV_TYPE_ONBOARD_CONTROLLER: "AP_Periph",
mavutil.mavlink.MAV_TYPE_VTOL_DUOROTOR: "ArduPlane",
mavutil.mavlink.MAV_TYPE_VTOL_QUADROTOR: "ArduPlane",
mavutil.mavlink.MAV_TYPE_VTOL_TILTROTOR: "ArduPlane",
mavutil.mavlink.MAV_TYPE_VTOL_RESERVED2: "ArduPlane",
mavutil.mavlink.MAV_TYPE_VTOL_RESERVED3: "ArduPlane",
mavutil.mavlink.MAV_TYPE_VTOL_RESERVED4: "ArduPlane",
mavutil.mavlink.MAV_TYPE_VTOL_RESERVED5: "ArduPlane",
mavutil.mavlink.MAV_TYPE_GIMBAL: "AP_Periph",
mavutil.mavlink.MAV_TYPE_ADSB: "AP_Periph",
mavutil.mavlink.MAV_TYPE_PARAFOIL: "ArduPlane",
mavutil.mavlink.MAV_TYPE_DODECAROTOR: "ArduCopter",
mavutil.mavlink.MAV_TYPE_CAMERA: "AP_Periph",
mavutil.mavlink.MAV_TYPE_CHARGING_STATION: "AP_Periph",
mavutil.mavlink.MAV_TYPE_FLARM: "AP_Periph",
mavutil.mavlink.MAV_TYPE_SERVO: "AP_Periph",
mavutil.mavlink.MAV_TYPE_ODID: "AP_Periph",
mavutil.mavlink.MAV_TYPE_DECAROTOR: "ArduCopter",
mavutil.mavlink.MAV_TYPE_BATTERY: "AP_Periph",
mavutil.mavlink.MAV_TYPE_PARACHUTE: "AP_Periph",
mavutil.mavlink.MAV_TYPE_LOG: "AP_Periph",
mavutil.mavlink.MAV_TYPE_OSD: "AP_Periph",
mavutil.mavlink.MAV_TYPE_IMU: "AP_Periph",
mavutil.mavlink.MAV_TYPE_GPS: "AP_Periph",
mavutil.mavlink.MAV_TYPE_WINCH: "AP_Periph",
# Add more mappings as needed
}
# Return the classified vehicle type based on the MAV_TYPE enum
return mav_type_to_vehicle_type.get(mav_type_int, "")
def log_flight_controller_info(self) -> None:
"""Log flight controller information at INFO level."""
logging_info("Firmware Version: %s", self.flight_sw_version_and_type)
logging_info("Firmware first 8 hex bytes of the FC git hash: %s", self.flight_custom_version)
logging_info("Firmware first 8 hex bytes of the ChibiOS git hash: %s", self.os_custom_version)
logging_info("Flight Controller firmware type: %s (%s)", self.firmware_type, self.apj_board_id)
logging_info("Flight Controller HW / board version: %s", self.board_version)
logging_info("Flight Controller USB vendor ID: %s", self.vendor)
logging_info("Flight Controller USB product ID: %s", self.product)
def format_display_value(self, value: Union[str, dict[str, str], None]) -> str:
"""Format a value for display in the UI."""
if value:
if isinstance(value, dict):
return ", ".join(value.keys())
return str(value)
return _("N/A")