Skip to main content

UDP Forwarder and ROS2 Publisher - udp_forwarder_node.cpp

udp_forwarder_node.cpp

This is the main file running the communications logic on Jonny Boi's side. It doubles as both a UDP server and a ROS2 publisher/subscriber, receiving raw encoded protobuffers from either the microcontrollers or Basestation and routing them to the appropriate destination.

The Handler Pattern

The core of this file is a registry-based dispatch system. When a PBEnvelope arrives over UDP, the node extracts the payload_case() — an enum value that identifies what type of message is inside — and looks up the corresponding handler in an unordered_map. Each handler extends the Handler base class and is responsible for deserializing one specific protobuf message type and publishing it as a ROS2 message on the appropriate topic. Registering a new handler looks like this:

handlers_.emplace(static_cast<int>(PBEnvelope::kImuInfo),
    std::make_unique<ImuHandler>(this, "imu_data", 10));

The three arguments are the payload case enum value, the handler instance, and the ROS2 topic name with queue size.

The rx_loop

UDP receiving runs on a dedicated background thread separate from the ROS2 spin thread. This means the node can continuously receive packets without blocking the ROS2 executor. Keep this in mind when debugging timing or threading issues.

Configuration Parameters

listen_port, dst_a_port, dst_b_port, and dst_ip are all declared as ROS2 parameters at startup and can be overridden at launch time without recompiling, via a launch file or --ros-args.

UDP Forwarding

Currently, every received UDP packet is forwarded raw to both dstA_ and dstB_ before handler dispatch. This is a known limitation — see the Known Limitations page for more details.