@@ -408,7 +408,7 @@ def _send_command_and_wait_ack( # pylint: disable=too-many-arguments,too-many-p
408408 logging_error (error_msg )
409409 return False , error_msg
410410
411- def __create_connection_with_retry ( # pylint: disable=too-many-arguments, too-many-positional-arguments
411+ def __create_connection_with_retry ( # pylint: disable=too-many-arguments, too-many-positional-arguments, too-many-locals, too-many-branches
412412 self ,
413413 progress_callback : Union [None , Callable [[int , int ], None ]],
414414 retries : int = 3 ,
@@ -452,36 +452,53 @@ def __create_connection_with_retry( # pylint: disable=too-many-arguments, too-m
452452 retries = retries ,
453453 progress_callback = progress_callback ,
454454 )
455- logging_debug (_ ("Waiting for MAVLink heartbeat " ))
455+ logging_debug (_ ("Waiting for MAVLink heartbeats... " ))
456456 if not self .master :
457457 msg = f"Failed to create mavlink connect to { self .comport .device } "
458458 raise ConnectionError (msg )
459- m = self .master .wait_heartbeat (timeout = timeout )
460- if m is None :
459+ # --- NEW: collect all vehicles detected within timeout ---
460+ start_time = time_time ()
461+ detected_vehicles = {} # (sysid, compid) -> last HEARTBEAT
462+
463+ while time_time () - start_time < timeout :
464+ m = self .master .recv_match (type = "HEARTBEAT" , blocking = False )
465+ if m is None :
466+ time_sleep (0.1 )
467+ continue
468+ sysid = m .get_srcSystem ()
469+ compid = m .get_srcComponent ()
470+ detected_vehicles [(sysid , compid )] = m
471+ logging_debug (_ ("Detected vehicle %u:%u (autopilot=%u, type=%u)" ), sysid , compid , m .autopilot , m .type )
472+
473+ if not detected_vehicles :
461474 return _ ("No MAVLink heartbeat received, connection failed." )
462- self .info .set_system_id_and_component_id (m .get_srcSystem (), m .get_srcComponent ())
463- logging_debug (
464- _ ("Connection established with systemID %d, componentID %d." ), self .info .system_id , self .info .component_id
465- )
466475
467- self .info .set_autopilot (m .autopilot )
468- if self .info .is_supported :
469- msg = _ ("Autopilot type {self.info.autopilot}" )
476+ for (sysid , compid ), m in detected_vehicles .items ():
477+ self .info .set_system_id_and_component_id (sysid , compid )
478+ logging_debug (
479+ _ ("Connection established with systemID %d, componentID %d." ), self .info .system_id , self .info .component_id
480+ )
481+ self .info .set_autopilot (m .autopilot )
482+ if self .info .is_supported :
483+ msg = _ ("Autopilot type {self.info.autopilot}" )
484+ logging_info (msg .format (** locals ()))
485+ else :
486+ msg = _ ("Unsupported autopilot type {self.info.autopilot}" )
487+ continue
488+ self .info .set_type (m .type )
489+ msg = _ ("Vehicle type: {self.info.mav_type} running {self.info.vehicle_type} firmware" )
470490 logging_info (msg .format (** locals ()))
471- else :
472- msg = _ ("Unsupported autopilot type {self.info.autopilot}" )
473- return msg .format (** locals ())
474491
475- self .info .set_type (m .type )
476- msg = _ ("Vehicle type: {self.info.mav_type} running {self.info.vehicle_type} firmware" )
477- logging_info (msg .format (** locals ()))
492+ if not self .info .is_supported :
493+ return _ ("No supported autopilots found" )
478494
479495 self .__request_banner ()
480496 banner_msgs = self .__receive_banner_text ()
481497
482498 self .__request_message (mavutil .mavlink .MAVLINK_MSG_ID_AUTOPILOT_VERSION )
483499 m = self .master .recv_match (type = "AUTOPILOT_VERSION" , blocking = True , timeout = timeout )
484500 return self .__process_autopilot_version (m , banner_msgs )
501+
485502 except (ConnectionError , SerialException , PermissionError , ConnectionRefusedError ) as e :
486503 if log_errors :
487504 logging_warning (_ ("Connection failed: %s" ), e )
0 commit comments