# tridibbanik17/ros2-battery-switch architecture

> Architecture diagram of the GitHub repository tridibbanik17/ros2-battery-switch, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-08-22). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/tridibbanik17/ros2-battery-switch
- Repository: https://github.com/tridibbanik17/ros2-battery-switch

## Overview

#### 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: [`src/battery_switch/launch/battery_switch.launch.py`](https://github.com/tridibbanik17/ros2-battery-switch/blob/main/src/battery_switch/launch/battery_switch.launch.py) (Launch description), [`src/battery_switch/battery_switch/battery_node.py`](https://github.com/tridibbanik17/ros2-battery-switch/blob/main/src/battery_switch/battery_switch/battery_node.py) (battery_1), [`src/battery_switch/battery_switch/ui_node.py`](https://github.com/tridibbanik17/ros2-battery-switch/blob/main/src/battery_switch/battery_switch/ui_node.py) (UINode / ui_node), [`src/battery_switch/setup.py`](https://github.com/tridibbanik17/ros2-battery-switch/blob/main/src/battery_switch/setup.py) (Package registration)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_package["Package"]
  node_setup["Package registration<br/>Python package metadata<br/>[setup.py]"]
  node_package_xml["ROS package manifest<br/>package metadata<br/>[package.xml]"]
end

subgraph group_producers["Battery publishers"]
  node_battery_impl["BatteryNode<br/>reusable ROS node<br/>[battery_node.py]"]
  node_battery_1["battery_1<br/>BatteryNode instance<br/>[battery_node.py]"]
  node_battery_2["battery_2<br/>BatteryNode instance<br/>[battery_node.py]"]
  node_charge_state["Charge simulation<br/>timer and state<br/>[battery_node.py]"]
  node_status_1["/battery_1/status<br/>Float32 topic"]
  node_status_2["/battery_2/status<br/>Float32 topic"]
end

subgraph group_consumer["UI router"]
  node_ui_node["UINode / ui_node<br/>ROS node<br/>[ui_node.py]"]
  node_active_battery["active_battery<br/>ROS parameter<br/>[ui_node.py]"]
  node_subscription_router["Subscription router<br/>parameter callback<br/>[ui_node.py]"]
  node_console_log["Console status output<br/>logging output<br/>[ui_node.py]"]
end

subgraph group_ros["ROS 2 runtime"]
  node_cli(("Operator / ROS 2 CLI<br/>external control"))
  node_launch["Launch description<br/>ROS 2 launch"]
  node_ros_graph{{"ROS 2 graph<br/>discovery and transport"}}
end

node_setup -->|"installs package executables"| node_launch
node_package_xml -->|"declares ROS package"| node_setup
node_cli -->|"ros2 launch"| node_launch
node_launch -->|"starts with name and initial_charge"| node_battery_1
node_launch -->|"starts with name and initial_charge"| node_battery_2
node_launch -->|"starts"| node_ui_node
node_battery_impl -.->|"reused as"| node_battery_1
node_battery_impl -.->|"reused as"| node_battery_2
node_charge_state -->|"produces charge values"| node_battery_1
node_charge_state -->|"produces charge values"| node_battery_2
node_battery_1 -->|"publishes Float32"| node_status_1
node_battery_2 -->|"publishes Float32"| node_status_2
node_status_1 -->|"transported through"| node_ros_graph
node_status_2 -->|"transported through"| node_ros_graph
node_cli -->|"ros2 param set"| node_active_battery
node_active_battery -->|"parameter update"| node_subscription_router
node_subscription_router -->|"creates one active subscription"| node_ui_node
node_ros_graph -->|"delivers selected topic"| node_ui_node
node_ui_node -->|"logs received charge"| node_console_log

click node_setup "https://github.com/tridibbanik17/ros2-battery-switch/blob/main/src/battery_switch/setup.py"
click node_package_xml "https://github.com/tridibbanik17/ros2-battery-switch/blob/main/src/battery_switch/package.xml"
click node_launch "https://github.com/tridibbanik17/ros2-battery-switch/blob/main/src/battery_switch/launch/battery_switch.launch.py"
click node_battery_impl "https://github.com/tridibbanik17/ros2-battery-switch/blob/main/src/battery_switch/battery_switch/battery_node.py"
click node_battery_1 "https://github.com/tridibbanik17/ros2-battery-switch/blob/main/src/battery_switch/battery_switch/battery_node.py"
click node_battery_2 "https://github.com/tridibbanik17/ros2-battery-switch/blob/main/src/battery_switch/battery_switch/battery_node.py"
click node_charge_state "https://github.com/tridibbanik17/ros2-battery-switch/blob/main/src/battery_switch/battery_switch/battery_node.py"
click node_ui_node "https://github.com/tridibbanik17/ros2-battery-switch/blob/main/src/battery_switch/battery_switch/ui_node.py"
click node_active_battery "https://github.com/tridibbanik17/ros2-battery-switch/blob/main/src/battery_switch/battery_switch/ui_node.py"
click node_subscription_router "https://github.com/tridibbanik17/ros2-battery-switch/blob/main/src/battery_switch/battery_switch/ui_node.py"
click node_console_log "https://github.com/tridibbanik17/ros2-battery-switch/blob/main/src/battery_switch/battery_switch/ui_node.py"

classDef toneNeutral fill:#f8fafc,stroke:#334155,stroke-width:1.5px,color:#0f172a
classDef toneBlue fill:#dbeafe,stroke:#2563eb,stroke-width:1.5px,color:#172554
classDef toneAmber fill:#fef3c7,stroke:#d97706,stroke-width:1.5px,color:#78350f
classDef toneMint fill:#dcfce7,stroke:#16a34a,stroke-width:1.5px,color:#14532d
classDef toneRose fill:#ffe4e6,stroke:#e11d48,stroke-width:1.5px,color:#881337
classDef toneIndigo fill:#e0e7ff,stroke:#4f46e5,stroke-width:1.5px,color:#312e81
classDef toneTeal fill:#ccfbf1,stroke:#0f766e,stroke-width:1.5px,color:#134e4a
class node_setup,node_package_xml toneBlue
class node_battery_impl,node_battery_1,node_battery_2,node_charge_state,node_status_1,node_status_2 toneAmber
class node_ui_node,node_active_battery,node_subscription_router,node_console_log toneMint
class node_cli,node_launch,node_ros_graph toneRose
```

## Components

### Package

- [Package registration](https://github.com/tridibbanik17/ros2-battery-switch/blob/main/src/battery_switch/setup.py): Python package metadata · Registers the battery and UI executables. · `src/battery_switch/setup.py`
- [ROS package manifest](https://github.com/tridibbanik17/ros2-battery-switch/blob/main/src/battery_switch/package.xml): package metadata · `src/battery_switch/package.xml`

### Battery publishers

- [BatteryNode](https://github.com/tridibbanik17/ros2-battery-switch/blob/main/src/battery_switch/battery_switch/battery_node.py): reusable ROS node · Publishes simulated charge at 1 Hz. · `src/battery_switch/battery_switch/battery_node.py`
- [battery_1](https://github.com/tridibbanik17/ros2-battery-switch/blob/main/src/battery_switch/battery_switch/battery_node.py): BatteryNode instance · Initialized by launch with its own charge value. · `src/battery_switch/battery_switch/battery_node.py`
- [battery_2](https://github.com/tridibbanik17/ros2-battery-switch/blob/main/src/battery_switch/battery_switch/battery_node.py): BatteryNode instance · Initialized by launch with its own charge value. · `src/battery_switch/battery_switch/battery_node.py`
- [Charge simulation](https://github.com/tridibbanik17/ros2-battery-switch/blob/main/src/battery_switch/battery_switch/battery_node.py): timer and state · Randomly decreases charge and clamps it at zero. · `src/battery_switch/battery_switch/battery_node.py`
- **/battery_1/status**: Float32 topic · Battery 1 charge stream.
- **/battery_2/status**: Float32 topic · Battery 2 charge stream.

### UI router

- [UINode / ui_node](https://github.com/tridibbanik17/ros2-battery-switch/blob/main/src/battery_switch/battery_switch/ui_node.py): ROS node · Console consumer with one active battery subscription. · `src/battery_switch/battery_switch/ui_node.py`
- [active_battery](https://github.com/tridibbanik17/ros2-battery-switch/blob/main/src/battery_switch/battery_switch/ui_node.py): ROS parameter · Selects battery_1 by default or battery_2 at runtime. · `src/battery_switch/battery_switch/ui_node.py`
- [Subscription router](https://github.com/tridibbanik17/ros2-battery-switch/blob/main/src/battery_switch/battery_switch/ui_node.py): parameter callback · Replaces the prior subscription when selection changes. · `src/battery_switch/battery_switch/ui_node.py`
- [Console status output](https://github.com/tridibbanik17/ros2-battery-switch/blob/main/src/battery_switch/battery_switch/ui_node.py): logging output · `src/battery_switch/battery_switch/ui_node.py`

### ROS 2 runtime

- **Operator / ROS 2 CLI**: external control · Launches the system and changes UI parameters.
- [Launch description](https://github.com/tridibbanik17/ros2-battery-switch/blob/main/src/battery_switch/launch/battery_switch.launch.py): ROS 2 launch · Composes two battery instances and one UI process. · `src/battery_switch/launch/battery_switch.launch.py`
- **ROS 2 graph**: discovery and transport · Provides topic transport, parameter services, events, and logging.

## Connections

- 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
