-
Notifications
You must be signed in to change notification settings - Fork 13
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
team_communication: automatically detect target ip address #589
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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 | ||
|
@@ -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: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Since you removed |
||
# local mode on loopback device, bind to port depending on bot id and team id | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Its used:
|
||
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 | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
self.target_ip
may not exist at this point