# matpomgit/alf-light-tracking architecture

> Architecture diagram of the GitHub repository matpomgit/alf-light-tracking, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-04-18). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/matpomgit/alf-light-tracking
- Repository: https://github.com/matpomgit/alf-light-tracking

## Overview

1. **Overall shape**
   - This is a single ROS2 Python package, `g1_light_tracking`, inside `ros2_ws`.
   - The repo is organized around one runtime goal: detect a light spot, turn that into movement commands, and optionally bridge those commands either to a Unitree G1 robot or to a turtlesim simulator.
   - The main architectural boundary is between **perception** (camera/CSV input to detection) and **control/actuation** (detection to `/cmd_vel`, then bridge to robot or simulation).

2. **Primary runtime pipeline**
   - The preferred online path is: `/camera/image_raw` from a D435i camera node → `light_spot_detector_node` → `/light_tracking/detection_json` → `g1_light_follower_node` → `/cmd_vel` → a bridge node.
   - The same follower layer is reused in simulation, where `/cmd_vel` is bridged to `/turtle1/cmd_vel`.
   - The README emphasizes a fail-safe philosophy: prefer rejecting uncertain detections over publishing wrong motion.

3. **Perception subsystem**
   - `light_spot_detector_node.py` is the main detector node.
   - Supporting modules under `vision/` define the detector abstraction, detection data types, and detector registry, which suggests the detector is pluggable rather than hard-wired.
   - `config/perception.yaml` controls detection thresholds and can switch `legacy_mode` on, indicating two selection strategies: current filtered detection logic and older area-based selection.
   - `d435i_node.py` likely provides the camera source integration for Intel RealSense D435i.

4. **Control / follower subsystem**
   - `g1_light_follower_node.py` consumes `detection_json` and produces velocity commands on `/cmd_vel`.
   - `config/control.yaml` contains follower tuning and speed limits, so the control loop is parameterized rather than embedded in code.
   - This subsystem is intentionally downstream of perception; it does not appear to own camera logic or robot transport.

5. **Actuation / bridge layer**
   - `unitree_cmd_vel_bridge_node.py` is the robot-facing transport layer for the Unitree G1 path.
   - `turtlesim_cmd_vel_bridge_node.py` is the simulation bridge, mapping the same `/cmd_vel` stream to `/turtle1/cmd_vel`.
   - `config/bridge.yaml` contains transport-specific settings for the Unitree bridge, keeping hardware details out of the follower.

6. **Offline replay and testability**
   - `csv_detection_replay_node.py` turns CSV records into `/light_tracking/detection_json`, allowing the whole control stack to be tested without a camera.
   - The turtlesim launch file wires this replay into the same follower logic, which makes the detection/control boundary easy to validate in isolation.
   - This is a key architectural feature: the repo is designed so perception can be substituted with recorded data.

7. **Arm skills as a separate capability**
   - `arm_skill_bridge_node.py` exposes simple services for `/arm_skills/pick_box`, `/arm_skills/place_box`, and `/arm_skills/stop`.
   - The arm path is separate from the light-tracking pipeline and appears to be an action-oriented bridge to the Unitree arm SDK.
   - Its inputs are service calls, not detection streams, which makes it a distinct subsystem in the diagram.

8. **Launch orchestration**
   - `launch/light_tracking_stack.launch.py` assembles the full robot pipeline.
   - `launch/light_tracking_turtlesim.launch.py` assembles the offline/simulation pipeline.
   - Launch files are the main composition boundary in this repo: they decide which nodes run together and which config files are injected.

9. **Calibration and tooling**
   - `tools/calibrate_perception.py` and the duplicate package-local version under `g1_light_tracking/tools/` calibrate detection thresholds from a reference video.
   - The calibration tool writes back to `config/perception.yaml` and produces a Markdown report in `logs/`.
   - This makes perception tuning an explicit offline workflow rather than an ad hoc runtime tweak.

10. **Configuration boundary**
   - `config/perception.yaml`, `config/control.yaml`, and `config/bridge.yaml` split the system into three tunable concerns: detection, motion behavior, and transport/hardware integration.
   - That separation is important for diagramming because it shows which components are policy/config-driven versus code-driven.
   - The presence of a `logs/` folder with a sample run log and a calibration report indicates the repo supports diagnosis and validation alongside execution.

11. **Runtime technologies and assumptions**
   - The codebase is ROS2-first and Python-based, built with `colcon` and packaged via `setup.py` / `package.xml`.
   - The README assumes Ubuntu + ROS2 + Python 3.10+, which matters because all communication is via ROS topics/services and the node graph is the system boundary.
   - External dependencies are mostly ROS message/service APIs plus whatever the Unitree and camera integrations require.

12. **Reference material, not runtime logic**
   - `docs/reference-links/` contains curated pointers for RT Linux, ROS2 real-time, logging, tf2, tracing, and bag recording.
   - These files are support material for operating or tuning the system, not part of the runtime architecture.
   - In a diagram, they belong outside the core data flow.

13. **Recommended diagram emphasis**
   - The clearest architecture diagram would show three lanes: **data sources** (`D435i`, CSV replay), **processing** (`light_spot_detector_node`, `g1_light_follower_node`), and **outputs** (`Unitree bridge`, `turtlesim bridge`, arm services).
   - A second axis should distinguish **online robot mode** from **offline/simulation/calibration mode**.
   - The central interface between perception and control is `/light_tracking/detection_json`; the central actuation interface is `/cmd_vel`.

Main entry points: [`ros2_ws/g1_light_tracking/g1_light_tracking/g1_light_follower_node.py`](https://github.com/matpomgit/alf-light-tracking/blob/main/ros2_ws/g1_light_tracking/g1_light_tracking/g1_light_follower_node.py) (Follower), [`ros2_ws/g1_light_tracking/g1_light_tracking/light_spot_detector_node.py`](https://github.com/matpomgit/alf-light-tracking/blob/main/ros2_ws/g1_light_tracking/g1_light_tracking/light_spot_detector_node.py) (Light detector), [`ros2_ws/g1_light_tracking/launch/light_tracking_stack.launch.py`](https://github.com/matpomgit/alf-light-tracking/blob/main/ros2_ws/g1_light_tracking/launch/light_tracking_stack.launch.py) (Robot launch), [`ros2_ws/g1_light_tracking/launch/light_tracking_turtlesim.launch.py`](https://github.com/matpomgit/alf-light-tracking/blob/main/ros2_ws/g1_light_tracking/launch/light_tracking_turtlesim.launch.py) (Turtlesim launch)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_group_sources["Inputs"]
  node_node_d435i["D435i node<br/>camera source<br/>[d435i_node.py]"]
  node_node_csv_replay["CSV replay<br/>offline source"]
end

subgraph group_group_perception["Perception"]
  node_node_detector["Light detector<br/>perception node"]
  node_node_vision_iface["Detector API<br/>vision abstraction"]
  node_node_vision_types["Detection types<br/>vision model<br/>[detection_types.py]"]
  node_node_detector_registry["Registry"]
  node_node_detectors["Detectors<br/>vision impls<br/>[detectors.py]"]
end

subgraph group_group_control["Control"]
  node_node_follower["Follower<br/>control node"]
end

subgraph group_group_actuation["Actuation"]
  node_node_unitree_bridge["Unitree bridge<br/>cmd_vel bridge"]
  node_node_turtlesim_bridge["Turtlesim bridge<br/>cmd_vel bridge"]
end

subgraph group_group_arm["Arm Skills"]
  node_node_arm_bridge["Arm bridge<br/>service bridge"]
  node_node_arm_controller["Arm controller"]
end

subgraph group_group_launch["Launch & Tools"]
  node_node_stack_launch["Robot launch<br/>launch file"]
  node_node_turtle_launch["Turtlesim launch<br/>launch file"]
  node_node_calibrate["Calibration tool<br/>offline tool"]
  node_node_calib_report["Calibration report"]
end

subgraph group_group_config["Configuration"]
  node_node_perception_cfg["Perception cfg<br/>yaml config<br/>[perception.yaml]"]
  node_node_control_cfg["Control cfg<br/>yaml config<br/>[control.yaml]"]
  node_node_bridge_cfg["Bridge cfg<br/>yaml config<br/>[bridge.yaml]"]
end

node_node_d435i -->|"/camera/image_raw"| node_node_detector
node_node_csv_replay -->|"/light_tracking/detection_json"| node_node_follower
node_node_detector -->|"/light_tracking/detection_json"| node_node_follower
node_node_follower -->|"/cmd_vel"| node_node_unitree_bridge
node_node_follower -->|"/cmd_vel"| node_node_turtlesim_bridge
node_node_arm_bridge -->|"services"| node_node_arm_controller
node_node_stack_launch -->|"starts"| node_node_d435i
node_node_stack_launch -->|"starts"| node_node_detector
node_node_stack_launch -->|"starts"| node_node_follower
node_node_stack_launch -->|"starts"| node_node_unitree_bridge
node_node_stack_launch -->|"starts"| node_node_arm_bridge
node_node_turtle_launch -->|"starts"| node_node_csv_replay
node_node_turtle_launch -->|"starts"| node_node_follower
node_node_turtle_launch -->|"starts"| node_node_turtlesim_bridge
node_node_calibrate -->|"writes"| node_node_perception_cfg
node_node_calibrate -->|"produces"| node_node_calib_report
node_node_perception_cfg -->|"configures"| node_node_detector
node_node_control_cfg -->|"configures"| node_node_follower
node_node_bridge_cfg -->|"configures"| node_node_unitree_bridge
node_node_vision_iface -->|"defines"| node_node_detectors
node_node_vision_types -->|"defines"| node_node_detector
node_node_detector_registry -->|"selects"| node_node_detector
node_node_detectors -->|"implements"| node_node_detector
node_node_arm_bridge -.->|"optional"| node_node_stack_launch

click node_node_d435i "https://github.com/matpomgit/alf-light-tracking/blob/main/ros2_ws/g1_light_tracking/g1_light_tracking/d435i_node.py"
click node_node_csv_replay "https://github.com/matpomgit/alf-light-tracking/blob/main/ros2_ws/g1_light_tracking/g1_light_tracking/csv_detection_replay_node.py"
click node_node_detector "https://github.com/matpomgit/alf-light-tracking/blob/main/ros2_ws/g1_light_tracking/g1_light_tracking/light_spot_detector_node.py"
click node_node_vision_iface "https://github.com/matpomgit/alf-light-tracking/blob/main/ros2_ws/g1_light_tracking/g1_light_tracking/vision/detector_interfaces.py"
click node_node_vision_types "https://github.com/matpomgit/alf-light-tracking/blob/main/ros2_ws/g1_light_tracking/g1_light_tracking/vision/detection_types.py"
click node_node_detector_registry "https://github.com/matpomgit/alf-light-tracking/blob/main/ros2_ws/g1_light_tracking/g1_light_tracking/vision/detector_registry.py"
click node_node_detectors "https://github.com/matpomgit/alf-light-tracking/blob/main/ros2_ws/g1_light_tracking/g1_light_tracking/vision/detectors.py"
click node_node_follower "https://github.com/matpomgit/alf-light-tracking/blob/main/ros2_ws/g1_light_tracking/g1_light_tracking/g1_light_follower_node.py"
click node_node_unitree_bridge "https://github.com/matpomgit/alf-light-tracking/blob/main/ros2_ws/g1_light_tracking/g1_light_tracking/unitree_cmd_vel_bridge_node.py"
click node_node_turtlesim_bridge "https://github.com/matpomgit/alf-light-tracking/blob/main/ros2_ws/g1_light_tracking/g1_light_tracking/turtlesim_cmd_vel_bridge_node.py"
click node_node_arm_bridge "https://github.com/matpomgit/alf-light-tracking/blob/main/ros2_ws/g1_light_tracking/g1_light_tracking/arm_skill_bridge_node.py"
click node_node_arm_controller "https://github.com/matpomgit/alf-light-tracking/blob/main/ros2_ws/g1_light_tracking/g1_light_tracking/arm_skill_controller.py"
click node_node_stack_launch "https://github.com/matpomgit/alf-light-tracking/blob/main/ros2_ws/g1_light_tracking/launch/light_tracking_stack.launch.py"
click node_node_turtle_launch "https://github.com/matpomgit/alf-light-tracking/blob/main/ros2_ws/g1_light_tracking/launch/light_tracking_turtlesim.launch.py"
click node_node_calibrate "https://github.com/matpomgit/alf-light-tracking/blob/main/ros2_ws/g1_light_tracking/tools/calibrate_perception.py"
click node_node_perception_cfg "https://github.com/matpomgit/alf-light-tracking/blob/main/ros2_ws/g1_light_tracking/config/perception.yaml"
click node_node_control_cfg "https://github.com/matpomgit/alf-light-tracking/blob/main/ros2_ws/g1_light_tracking/config/control.yaml"
click node_node_bridge_cfg "https://github.com/matpomgit/alf-light-tracking/blob/main/ros2_ws/g1_light_tracking/config/bridge.yaml"
click node_node_calib_report "https://github.com/matpomgit/alf-light-tracking/blob/main/ros2_ws/g1_light_tracking/config/perception_calibration_report.md"

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_node_d435i,node_node_csv_replay,node_node_perception_cfg,node_node_control_cfg,node_node_bridge_cfg toneBlue
class node_node_detector,node_node_vision_iface,node_node_vision_types,node_node_detector_registry,node_node_detectors toneAmber
class node_node_follower toneMint
class node_node_unitree_bridge,node_node_turtlesim_bridge toneRose
class node_node_arm_bridge,node_node_arm_controller toneIndigo
class node_node_stack_launch,node_node_turtle_launch,node_node_calibrate,node_node_calib_report toneTeal
```

## Components

### Inputs

Live and replayed data sources that enter the pipeline

- [D435i node](https://github.com/matpomgit/alf-light-tracking/blob/main/ros2_ws/g1_light_tracking/g1_light_tracking/d435i_node.py): camera source · Publishes camera frames from the Intel RealSense D435i · `ros2_ws/g1_light_tracking/g1_light_tracking/d435i_node.py`
- [CSV replay](https://github.com/matpomgit/alf-light-tracking/blob/main/ros2_ws/g1_light_tracking/g1_light_tracking/csv_detection_replay_node.py): offline source · Replays recorded detections into the live control pipeline · `ros2_ws/g1_light_tracking/g1_light_tracking/csv_detection_replay_node.py`

### Perception

Camera ingestion and light-spot detection

- [Light detector](https://github.com/matpomgit/alf-light-tracking/blob/main/ros2_ws/g1_light_tracking/g1_light_tracking/light_spot_detector_node.py): perception node · Consumes images and publishes light spot detections as JSON · `ros2_ws/g1_light_tracking/g1_light_tracking/light_spot_detector_node.py`
- [Detector API](https://github.com/matpomgit/alf-light-tracking/blob/main/ros2_ws/g1_light_tracking/g1_light_tracking/vision/detector_interfaces.py): vision abstraction · Detector interface and shared contracts · `ros2_ws/g1_light_tracking/g1_light_tracking/vision/detector_interfaces.py`
- [Detection types](https://github.com/matpomgit/alf-light-tracking/blob/main/ros2_ws/g1_light_tracking/g1_light_tracking/vision/detection_types.py): vision model · Shared detection data structures · `ros2_ws/g1_light_tracking/g1_light_tracking/vision/detection_types.py`
- [Registry](https://github.com/matpomgit/alf-light-tracking/blob/main/ros2_ws/g1_light_tracking/g1_light_tracking/vision/detector_registry.py): vision registry · Selects and registers detector implementations · `ros2_ws/g1_light_tracking/g1_light_tracking/vision/detector_registry.py`
- [Detectors](https://github.com/matpomgit/alf-light-tracking/blob/main/ros2_ws/g1_light_tracking/g1_light_tracking/vision/detectors.py): vision impls · Concrete detector implementations used by the node · `ros2_ws/g1_light_tracking/g1_light_tracking/vision/detectors.py`

### Control

Detection-to-motion follower logic

- [Follower](https://github.com/matpomgit/alf-light-tracking/blob/main/ros2_ws/g1_light_tracking/g1_light_tracking/g1_light_follower_node.py): control node · Consumes detections and publishes velocity commands · `ros2_ws/g1_light_tracking/g1_light_tracking/g1_light_follower_node.py`

### Actuation

Bridges from ROS velocity commands to robot or simulator

- [Unitree bridge](https://github.com/matpomgit/alf-light-tracking/blob/main/ros2_ws/g1_light_tracking/g1_light_tracking/unitree_cmd_vel_bridge_node.py): cmd_vel bridge · Maps /cmd_vel to Unitree G1 actuation · `ros2_ws/g1_light_tracking/g1_light_tracking/unitree_cmd_vel_bridge_node.py`
- [Turtlesim bridge](https://github.com/matpomgit/alf-light-tracking/blob/main/ros2_ws/g1_light_tracking/g1_light_tracking/turtlesim_cmd_vel_bridge_node.py): cmd_vel bridge · Maps /cmd_vel to /turtle1/cmd_vel for simulation · `ros2_ws/g1_light_tracking/g1_light_tracking/turtlesim_cmd_vel_bridge_node.py`

### Arm Skills

Separate service-based arm capability

- [Arm bridge](https://github.com/matpomgit/alf-light-tracking/blob/main/ros2_ws/g1_light_tracking/g1_light_tracking/arm_skill_bridge_node.py): service bridge · Exposes pick, place, and stop services for arm control · `ros2_ws/g1_light_tracking/g1_light_tracking/arm_skill_bridge_node.py`
- [Arm controller](https://github.com/matpomgit/alf-light-tracking/blob/main/ros2_ws/g1_light_tracking/g1_light_tracking/arm_skill_controller.py): arm controller · Implements arm skill execution logic · `ros2_ws/g1_light_tracking/g1_light_tracking/arm_skill_controller.py`

### Launch & Tools

Composition, replay, and calibration entry points

- [Robot launch](https://github.com/matpomgit/alf-light-tracking/blob/main/ros2_ws/g1_light_tracking/launch/light_tracking_stack.launch.py): launch file · Composes the online robot pipeline · `ros2_ws/g1_light_tracking/launch/light_tracking_stack.launch.py`
- [Turtlesim launch](https://github.com/matpomgit/alf-light-tracking/blob/main/ros2_ws/g1_light_tracking/launch/light_tracking_turtlesim.launch.py): launch file · Composes the replay and simulation pipeline · `ros2_ws/g1_light_tracking/launch/light_tracking_turtlesim.launch.py`
- [Calibration tool](https://github.com/matpomgit/alf-light-tracking/blob/main/ros2_ws/g1_light_tracking/tools/calibrate_perception.py): offline tool · Derives perception thresholds from recorded video and writes back config · `ros2_ws/g1_light_tracking/tools/calibrate_perception.py`
- [Calibration report](https://github.com/matpomgit/alf-light-tracking/blob/main/ros2_ws/g1_light_tracking/config/perception_calibration_report.md): report · Markdown output documenting threshold tuning results · `ros2_ws/g1_light_tracking/config/perception_calibration_report.md`

### Configuration

Runtime tuning and transport settings

- [Perception cfg](https://github.com/matpomgit/alf-light-tracking/blob/main/ros2_ws/g1_light_tracking/config/perception.yaml): yaml config · Detection thresholds and legacy selection mode · `ros2_ws/g1_light_tracking/config/perception.yaml`
- [Control cfg](https://github.com/matpomgit/alf-light-tracking/blob/main/ros2_ws/g1_light_tracking/config/control.yaml): yaml config · Follower tuning and speed limits · `ros2_ws/g1_light_tracking/config/control.yaml`
- [Bridge cfg](https://github.com/matpomgit/alf-light-tracking/blob/main/ros2_ws/g1_light_tracking/config/bridge.yaml): yaml config · Transport and hardware settings for the Unitree bridge · `ros2_ws/g1_light_tracking/config/bridge.yaml`

## Connections

- D435i node → Light detector: /camera/image_raw · Live image stream into perception
- CSV replay → Follower: /light_tracking/detection_json · Offline detection input for control testing
- Light detector → Follower: /light_tracking/detection_json · Primary perception-to-control interface
- Follower → Unitree bridge: /cmd_vel · Velocity commands for robot actuation
- Follower → Turtlesim bridge: /cmd_vel · Velocity commands for simulation actuation
- Arm bridge → Arm controller: services · Service requests dispatched to arm skill logic
- Robot launch → D435i node: starts · Composes the live robot pipeline
- Robot launch → Light detector: starts · Launches perception node
- Robot launch → Follower: starts · Launches control node
- Robot launch → Unitree bridge: starts · Launches robot bridge
- Robot launch → Arm bridge: starts · Launches arm services
- Turtlesim launch → CSV replay: starts · Uses replay as perception substitute
- Turtlesim launch → Follower: starts · Reuses same control logic in simulation
- Turtlesim launch → Turtlesim bridge: starts · Launches simulator bridge
- Calibration tool → Perception cfg: writes · Updates detection thresholds
- Calibration tool → Calibration report: produces · Generates tuning report
- Perception cfg → Light detector: configures · Controls detector selection and thresholds
- Control cfg → Follower: configures · Sets follower tuning and limits
- Bridge cfg → Unitree bridge: configures · Sets transport and hardware parameters
- Detector API → Detectors: defines · Shared detector contract used by implementations
- Detection types → Light detector: defines · Shared detection payloads
- Registry → Light detector: selects · Chooses active detector implementation
- Detectors → Light detector: implements · Concrete detection algorithms used by the node
- Arm bridge → Robot launch: optional · Included in the full robot composition
