How tridibbanik17/ros2-battery-switch works
Purpose and Runtime
battery_switch is a ROS 2 Jazzy Python package demonstrating runtime routing between two simulated battery-status publishers. It runs in a ROS 2 workspace via colcon; the primary operational entry point is ros2 launch battery_switch battery_switch.launch.py.
Key package metadata and executable registration are in:
• src/battery_switch/setup.py
• src/battery_switch/package.xml
Launch-Orchestrated Topology
The launch description creates three ROS 2 processes: two named instances of the reusable battery executable and one UI consumer:
• src/battery_switch/launch/battery_switch.launch.py
• src/battery_switch/battery_switch/battery_node.py
• src/battery_switch/battery_switch/ui_node.py
Launch-time node names and initial-charge parameters distinguish battery_1 from battery_2; this is the intended composition boundary rather than separate battery implementations.
Battery Simulation Publishers
BatteryNode is the producer stage. Each instance owns one namespaced status topic, publishes std_msgs/Float32 charge values at 1 Hz, decreases charge by a small random amount, and clamps it at zero:
• src/battery_switch/battery_switch/battery_node.py
Its output contract is a numeric battery-charge stream on /battery_1/status or /battery_2/status. The initial_charge parameter supplies per-instance initialization from the launch layer.
Dynamic UI Subscription Router
UINode is the consumer and switching-control stage:
• src/battery_switch/battery_switch/ui_node.py
It exposes the active_battery ROS parameter, defaulting to battery_1. It maintains exactly one active subscription and logs values received from that selected battery. The UI is console-oriented; there is no web or graphical frontend.
Runtime Switching Flow
An operator sets /ui_node’s active_battery through ROS 2 parameter services/CLI. The UI node’s parameter callback is the reconfiguration boundary: it destroys the prior subscription and creates one for the selected battery’s /status topic.
Primary flow:
1. Launch starts battery_1, battery_2, and ui_node.
2. Battery instances emit independent Float32 streams.
3. ui_node subscribes to the currently selected stream.
4. A parameter update switches the subscription without restarting any node.
ROS 2 Infrastructure Boundaries
The system relies on the ROS 2 graph for discovery, topic transport, parameter services, logging, and standard /parameter_events behavior. External interaction is through ROS 2 CLI commands such as ros2 launch, ros2 param set, and ros2 topic echo, not an application-specific API.
The only application-level message boundary is battery status (Float32); the control boundary is the UI node’s active_battery parameter.
Main entry points: battery_switch.launch.py Launch description · battery_node.py battery_1 · ui_node.py UINode / ui_node · setup.py Package registration
Components 15
Package
- Package registration · Registers the battery and UI executables.
src/battery_switch/setup.py - ROS package manifest
src/battery_switch/package.xml
Battery publishers
- BatteryNode · Publishes simulated charge at 1 Hz.
src/battery_switch/battery_switch/battery_node.py - battery_1 · Initialized by launch with its own charge value.
src/battery_switch/battery_switch/battery_node.py - battery_2 · Initialized by launch with its own charge value.
src/battery_switch/battery_switch/battery_node.py - Charge simulation · Randomly decreases charge and clamps it at zero.
src/battery_switch/battery_switch/battery_node.py - /battery_1/status · Battery 1 charge stream.
- /battery_2/status · Battery 2 charge stream.
UI router
- UINode / ui_node · Console consumer with one active battery subscription.
src/battery_switch/battery_switch/ui_node.py - active_battery · Selects battery_1 by default or battery_2 at runtime.
src/battery_switch/battery_switch/ui_node.py - Subscription router · Replaces the prior subscription when selection changes.
src/battery_switch/battery_switch/ui_node.py - Console status output
src/battery_switch/battery_switch/ui_node.py
ROS 2 runtime
- Operator / ROS 2 CLI · Launches the system and changes UI parameters.
- Launch description · Composes two battery instances and one UI process.
src/battery_switch/launch/battery_switch.launch.py - ROS 2 graph · Provides topic transport, parameter services, events, and logging.
Connections 19
- Package registration → Launch description: installs package executables
- ROS package manifest → Package registration: declares ROS package
- Operator / ROS 2 CLI → Launch description: ros2 launch
- Launch description → battery_1: starts with name and initial_charge
- Launch description → battery_2: starts with name and initial_charge
- Launch description → UINode / ui_node: starts
- BatteryNode → battery_1: reused as
- BatteryNode → battery_2: reused as
- Charge simulation → battery_1: produces charge values
- Charge simulation → battery_2: produces charge values
- battery_1 → /battery_1/status: publishes Float32
- battery_2 → /battery_2/status: publishes Float32
- /battery_1/status → ROS 2 graph: transported through
- /battery_2/status → ROS 2 graph: transported through
- Operator / ROS 2 CLI → active_battery: ros2 param set
- active_battery → Subscription router: parameter update
- Subscription router → UINode / ui_node: creates one active subscription
- ROS 2 graph → UINode / ui_node: delivers selected topic
- UINode / ui_node → Console status output: logs received charge