Skip to content

Commit c80fc11

Browse files
committed
team_communication: automatically detect target ip address
Closes #578.
1 parent 8976b31 commit c80fc11

File tree

2 files changed

+17
-12
lines changed

2 files changed

+17
-12
lines changed

Diff for: bitbots_team_communication/bitbots_team_communication/bitbots_team_communication/communication.py

+14-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import socket
2-
from ipaddress import IPv4Address
2+
from ipaddress import AddressValueError, IPv4Address
33
from typing import List, Optional
44

55
from rclpy.node import Node
@@ -11,7 +11,19 @@ def __init__(self, node: Node, logger, team_id, robot_id):
1111

1212
self.buffer_size: int = 1024
1313
self.socket: Optional[socket.socket] = None
14-
self.target_ip: IPv4Address = IPv4Address(node.get_parameter("target_ip").value)
14+
if node.get_parameter("detect_target_ip").value:
15+
try:
16+
# automatically detect from subnet
17+
import netifaces
18+
19+
self.target_ip: IPv4Address = IPv4Address(
20+
netifaces.ifaddresses(node.get_parameter("wifi_interface"))[netifaces.AF_INET][0]["broadcast"]
21+
)
22+
except (ImportError, ValueError, KeyError, AddressValueError):
23+
self.logger.warn("Could not detect broadcast address, falling back to configured address")
24+
self.target_ip = None
25+
if self.target_ip is None:
26+
self.target_ip: IPv4Address = IPv4Address(node.get_parameter("target_ip").value)
1527

1628
if self.target_ip.is_loopback:
1729
# local mode on loopback device, bind to port depending on bot id and team id

Diff for: bitbots_team_communication/bitbots_team_communication/config/team_communication_config.yaml

+3-10
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,14 @@
11
team_comm:
22
ros__parameters:
3-
# UDP broadcast address is the highest IP in the subnet e.g. 172.20.255.255
4-
# Sets local mode if set to loopback (127.0.0.1)
3+
# Automatically detect UDP broadcast address
4+
detect_target_ip: true
5+
# Fallback, only used if detect_target_ip is false. This should be the highest IP in the subnet e.g. 172.20.255.255
56
target_ip: 192.168.255.255
67

78
# Only used in non local mode with specific target_ip
89
target_port: 3737
910
receive_port: 3737
1011

11-
# Only used in local mode on loopback
12-
# the team communication will bind to one of these ports and send to the other ports, depending on its bot_id
13-
local_target_ports:
14-
- 4001
15-
- 4002
16-
- 4003
17-
- 4004
18-
1912
# Rate of published messages in Hz
2013
rate: 10
2114

0 commit comments

Comments
 (0)