From f4efb31cdec87e05cc2b821c17ce8d157470e3f9 Mon Sep 17 00:00:00 2001 From: Rafael Araujo Lehmkuhl Date: Wed, 9 Mar 2022 17:29:54 -0300 Subject: [PATCH] core: ardupilot-manager: Allow to plug more than one serial board This commit changes the way serial devices are detected. Instead of relying on UDEV rules it uses `pyserial` to detect available serial devices. With UDEV rules, we can only detect one at a given time. With those changes, the limitation no longer exists. The validation rules (manufacturer and product name) are kept. --- .../flight_controller_detector/Detector.py | 29 ++++++++++--------- install/udev/100.autopilot.rules | 3 -- 2 files changed, 15 insertions(+), 17 deletions(-) diff --git a/core/services/ardupilot_manager/flight_controller_detector/Detector.py b/core/services/ardupilot_manager/flight_controller_detector/Detector.py index b11d09005e..55b844447e 100644 --- a/core/services/ardupilot_manager/flight_controller_detector/Detector.py +++ b/core/services/ardupilot_manager/flight_controller_detector/Detector.py @@ -2,7 +2,7 @@ from typing import List, Optional from loguru import logger -from serial.tools.list_ports_linux import comports +from serial.tools.list_ports_linux import SysFS, comports from smbus2 import SMBus from typedefs import FlightController, Platform @@ -53,6 +53,10 @@ def is_navigator_r5_connected() -> bool: logger.debug("No Navigator board detected.") return None + @staticmethod + def is_valid_serial_controller(port: SysFS) -> bool: + return port.manufacturer == "ArduPilot" or (port.manufacturer == "3D Robotics" and "PX4" in port.product) + @staticmethod def detect_serial_flight_controllers() -> List[FlightController]: """Check if a Pixhawk1 or any other valid serial flight controller is connected. @@ -60,19 +64,16 @@ def detect_serial_flight_controllers() -> List[FlightController]: Returns: List[FlightController]: List with connected serial flight controller. """ - serial_path = "/dev/autopilot" - for port in comports(include_links=True): - if not port.device == serial_path: - continue - return [ - FlightController( - name=port.product or port.name, - manufacturer=port.manufacturer, - platform=Platform.Pixhawk1, - path=serial_path, - ) - ] - return [] + return [ + FlightController( + name=port.product or port.name, + manufacturer=port.manufacturer, + platform=Platform.Pixhawk1, + path=port.device, + ) + for port in comports() + if Detector.is_valid_serial_controller(port) + ] @classmethod def detect(cls) -> List[FlightController]: diff --git a/install/udev/100.autopilot.rules b/install/udev/100.autopilot.rules index 3d682d2c8d..0120e670bb 100644 --- a/install/udev/100.autopilot.rules +++ b/install/udev/100.autopilot.rules @@ -1,6 +1,3 @@ -SUBSYSTEM=="tty", ATTRS{manufacturer}=="3D Robotics", ATTRS{product}=="PX4*", SYMLINK+="autopilot", ENV{ID_MM_DEVICE_IGNORE}="1", ENV{ID_MM_PORT_IGNORE}="1" -SUBSYSTEM=="tty", ATTRS{manufacturer}=="ArduPilot", SYMLINK+="autopilot", ENV{ID_MM_DEVICE_IGNORE}="1", ENV{ID_MM_PORT_IGNORE}="1" - # CP210X USB UART ATTRS{idVendor}=="10c4", ATTRS{idProduct}=="ea60", MODE:="0666", ENV{ID_MM_DEVICE_IGNORE}="1", ENV{ID_MM_PORT_IGNORE}="1"