From 1568f89df07a10a1b99ad3e40099156e632e0575 Mon Sep 17 00:00:00 2001 From: Timon Engelke Date: Sat, 20 Jul 2024 10:27:03 +0200 Subject: [PATCH] team_communication: automatically detect target ip address Closes #578. --- .../bitbots_team_communication/communication.py | 16 ++++++++++++++-- .../config/team_communication_config.yaml | 14 ++++---------- 2 files changed, 18 insertions(+), 12 deletions(-) diff --git a/bitbots_team_communication/bitbots_team_communication/bitbots_team_communication/communication.py b/bitbots_team_communication/bitbots_team_communication/bitbots_team_communication/communication.py index 14d8966c5..3d9f1ce5e 100644 --- a/bitbots_team_communication/bitbots_team_communication/bitbots_team_communication/communication.py +++ b/bitbots_team_communication/bitbots_team_communication/bitbots_team_communication/communication.py @@ -1,5 +1,5 @@ import socket -from ipaddress import IPv4Address +from ipaddress import AddressValueError, IPv4Address from typing import List, Optional from rclpy.node import Node @@ -11,7 +11,19 @@ def __init__(self, node: Node, logger, team_id, robot_id): self.buffer_size: int = 1024 self.socket: Optional[socket.socket] = None - self.target_ip: IPv4Address = IPv4Address(node.get_parameter("target_ip").value) + if node.get_parameter("detect_target_ip").value: + try: + # automatically detect from subnet + import netifaces + + self.target_ip: IPv4Address = IPv4Address( + netifaces.ifaddresses(node.get_parameter("wifi_interface"))[netifaces.AF_INET][0]["broadcast"] + ) + except (ImportError, ValueError, KeyError, AddressValueError): + self.logger.warn("Could not detect broadcast address, falling back to configured address") + self.target_ip = None + if self.target_ip is None: + self.target_ip: IPv4Address = IPv4Address(node.get_parameter("target_ip").value) if self.target_ip.is_loopback: # local mode on loopback device, bind to port depending on bot id and team id diff --git a/bitbots_team_communication/bitbots_team_communication/config/team_communication_config.yaml b/bitbots_team_communication/bitbots_team_communication/config/team_communication_config.yaml index ba995f402..abb781412 100644 --- a/bitbots_team_communication/bitbots_team_communication/config/team_communication_config.yaml +++ b/bitbots_team_communication/bitbots_team_communication/config/team_communication_config.yaml @@ -1,21 +1,15 @@ team_comm: ros__parameters: - # UDP broadcast address is the highest IP in the subnet e.g. 172.20.255.255 - # Sets local mode if set to loopback (127.0.0.1) + # Automatically detect UDP broadcast address + detect_target_ip: true + wifi_interface: "wlp3s0" + # Fallback, only used if detect_target_ip is false. This should be the highest IP in the subnet e.g. 172.20.255.255 target_ip: 192.168.255.255 # Only used in non local mode with specific target_ip target_port: 3737 receive_port: 3737 - # Only used in local mode on loopback - # the team communication will bind to one of these ports and send to the other ports, depending on its bot_id - local_target_ports: - - 4001 - - 4002 - - 4003 - - 4004 - # Rate of published messages in Hz rate: 10