|
9 | 9 | """ |
10 | 10 |
|
11 | 11 | import os |
| 12 | +import time |
12 | 13 | from argparse import ArgumentParser |
13 | 14 | from logging import debug as logging_debug |
14 | 15 | from logging import error as logging_error |
@@ -452,36 +453,52 @@ def __create_connection_with_retry( # pylint: disable=too-many-arguments, too-m |
452 | 453 | retries=retries, |
453 | 454 | progress_callback=progress_callback, |
454 | 455 | ) |
455 | | - logging_debug(_("Waiting for MAVLink heartbeat")) |
| 456 | + logging_debug(_("Waiting for MAVLink heartbeats...")) |
456 | 457 | if not self.master: |
457 | 458 | msg = f"Failed to create mavlink connect to {self.comport.device}" |
458 | 459 | raise ConnectionError(msg) |
459 | | - m = self.master.wait_heartbeat(timeout=timeout) |
460 | | - if m is None: |
| 460 | + # --- NEW: collect all vehicles detected within timeout --- |
| 461 | + start_time = time.time() |
| 462 | + detected_vehicles = {} # sysid -> last HEARTBEAT |
| 463 | + |
| 464 | + while time.time() - start_time < timeout: |
| 465 | + m = self.master.recv_match(type="HEARTBEAT", blocking=False) |
| 466 | + if m is None: |
| 467 | + time.sleep(0.1) |
| 468 | + continue |
| 469 | + sysid = m.get_srcSystem() |
| 470 | + compid = m.get_srcComponent() |
| 471 | + detected_vehicles[(sysid, compid)] = m |
| 472 | + logging_debug(_("Detected vehicle %u:%u (autopilot=%u, type=%u)"), sysid, compid, m.autopilot, m.type) |
| 473 | + |
| 474 | + if not detected_vehicles: |
461 | 475 | 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 | | - ) |
466 | 476 |
|
467 | | - self.info.set_autopilot(m.autopilot) |
468 | | - if self.info.is_supported: |
469 | | - msg = _("Autopilot type {self.info.autopilot}") |
470 | | - logging_info(msg.format(**locals())) |
471 | | - else: |
472 | | - msg = _("Unsupported autopilot type {self.info.autopilot}") |
473 | | - return msg.format(**locals()) |
| 477 | + for (sysid, compid), m in detected_vehicles.items(): |
| 478 | + self.info.set_system_id_and_component_id(sysid, compid) |
| 479 | + logging_debug( |
| 480 | + _("Connection established with systemID %d, componentID %d."), self.info.system_id, self.info.component_id |
| 481 | + ) |
| 482 | + self.info.set_autopilot(m.autopilot) |
| 483 | + if self.info.is_supported: |
| 484 | + msg = _("Autopilot type {self.info.autopilot}") |
| 485 | + logging_info(msg.format(**locals())) |
| 486 | + else: |
| 487 | + msg = _("Unsupported autopilot type {self.info.autopilot}") |
| 488 | + # return msg.format(**locals()) |
| 489 | + continue |
474 | 490 |
|
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())) |
| 491 | + self.info.set_type(m.type) |
| 492 | + msg = _("Vehicle type: {self.info.mav_type} running {self.info.vehicle_type} firmware") |
| 493 | + logging_info(msg.format(**locals())) |
478 | 494 |
|
479 | 495 | self.__request_banner() |
480 | 496 | banner_msgs = self.__receive_banner_text() |
481 | 497 |
|
482 | 498 | self.__request_message(mavutil.mavlink.MAVLINK_MSG_ID_AUTOPILOT_VERSION) |
483 | 499 | m = self.master.recv_match(type="AUTOPILOT_VERSION", blocking=True, timeout=timeout) |
484 | 500 | return self.__process_autopilot_version(m, banner_msgs) |
| 501 | + |
485 | 502 | except (ConnectionError, SerialException, PermissionError, ConnectionRefusedError) as e: |
486 | 503 | if log_errors: |
487 | 504 | logging_warning(_("Connection failed: %s"), e) |
|
0 commit comments