Skip to content

Commit

Permalink
team_communication: automatically detect target ip address
Browse files Browse the repository at this point in the history
Closes #578.
  • Loading branch information
timonegk committed Jul 20, 2024
1 parent 8976b31 commit c80fc11
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 12 deletions.
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -1,21 +1,14 @@
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
# 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

Expand Down

0 comments on commit c80fc11

Please sign in to comment.