# yitinghh-bit/turtlebot3_drlnav architecture

> Architecture diagram of the GitHub repository yitinghh-bit/turtlebot3_drlnav, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-04-26). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/yitinghh-bit/turtlebot3_drlnav
- Repository: https://github.com/yitinghh-bit/turtlebot3_drlnav

## Overview

##### Project type and purpose
- Type: A ROS2-based robotics framework combining Gazebo simulation, LiDAR/odometry data feeds, and a deep-reinforcement-learning (DRL) agent implemented in PyTorch.
- Purpose: Train, evaluate, and deploy DRL navigation agents (DQN, DDPG, TD3) for TurtleBot3 (or any LiDAR-equipped robot) in both simulated and real environments.

##### File structure
- Top-level packages in src/:
– turtlebot3_drl: core DRL framework
– turtlebot3_msgs: custom ROS2 messages, services, actions for DRL steps, goals, etc.
– turtlebot3_simulations: simulation support (Gazebo worlds, robot models, obstacle plugins, fake nodes).
- util/: post-processing scripts (cleaning models, plotting reward graphs, spawn/stop scripts).
- Dockerfile + spawn_goal.sh at root: containerized environment and goal-spawning helper.
- Within turtlebot3_drl: submodules common/, drl_agent/, drl_environment/, drl_gazebo/.

##### README for additional insights
- System_architecture.png: outlines high-level ROS2 nodes and data flows.
- Launch/training instructions: shows four parallel terminals/nodes—Gazebo launch, gazebo_goals, environment, train_agent/test_agent.
- Real-robot vs simulation: almost identical flow, but environment node switches between Gazebo and real sensors.

a. Main components
– ROS2 DDS Network (DDS as the message bus)
– Gazebo Simulator
– spawn_goal node (gazebo_goals)
– DRL Environment Node
- drl_environment (simulated)
- real_environment (real robot)
– DRL Agent Node
- train_agent/test_agent (DQN/DDPG/TD3)
- real_agent for deployment
– turtlebot3_msgs (custom message definitions)
– turtlebot3_simulations (models, worlds, plugins)
– File System for model storage & logs (model/…)
– Optional GUIs: RViz2, pyqtgraph visualizer
– Utilities (reward_graph.py, clean scripts)
– Docker Container / Host OS / GPU runtime

b. Relationships and interactions
– Gazebo publishes /scan (LaserScan) and /odom (Odometry) → picked up by drl_environment.
– drl_environment node transforms raw messages into observation vectors + computes reward via reward.py → publishes Observation & Reward to DRL agent (via topics or direct API).
– DRL agent subscribes to observations, computes next action (cmd_vel messages), and publishes Twist → drl_environment or Gazebo (real robot listens on same topic).
– Agent periodically writes model checkpoints, hyperparameter files, test/train logs to disk.
– Real deployment: real_environment listens to real /scan and /odom, publishes observations; real_agent reads from disk and publishes velocity commands.

c. Architectural patterns / design principles
– Modular ROS2 nodes communicating via publish/subscribe and services/actions.
– Separation of concerns: perception (environment node), decision-making (agent), simulation (Gazebo), data definitions (msgs).
– Plugin-based simulation: custom obstacle plugins implemented as Gazebo model plugins.
– Configurability: settings.py drives hyperparameters, environment settings, real/sim switches.

d. Key technologies / frameworks
– ROS2 (Foxy): rclpy for Python nodes, colcon build system
– Gazebo 11: world definitions, custom obstacle plugins (C++)
– PyTorch: DQN, DDPG, TD3 implementations
– Docker + NVIDIA Container Toolkit for GPU acceleration
– Python utilities: matplotlib, pandas, pyqtgraph for visualization

##### Elements
– Clear labels: name each node (e.g., “drl_environment (gazebo)”), each topic (/scan, /odom, /cmd_vel), services/actions (Goal.srv).
- Blue rectangles for simulation nodes
- Green rectangles for DRL framework nodes
- Grey cloud for ROS2 DDS fabric

##### Final advice
– Be thorough: capture every ROS2 node, every custom message/service, and the filesystem interactions.
– Don’t over-optimize: it’s better to show many small components clearly than to lump everything into a monolith.
– Use layering: group by package (turtlebot3_drl, turtlebot3_simulations, turtlebot3_msgs) or by runtime (sim vs real).
– Keep it updated: as new DRL algorithms or environments are added, insert new nodes or topics.

## Diagram (Mermaid)

```mermaid
flowchart TB
    %% Cloud
    DDS["ROS2 DDS Network"]:::cloud

    %% Simulation Subgraph
    subgraph "Simulation Layer"
        direction TB
        Gazebo["Gazebo Simulator"]:::sim
        FakeNode["Gazebo Fake Node"]:::sim
        SpawnShell["Spawn Goal Shell"]:::sim
        SpawnUtil["Spawn Goal Utility"]:::sim
    end

    %% Real Layer
    subgraph "Real Robot Layer"
        direction TB
        RealRobot["Real Robot Sensors"]:::sim
    end

    %% Environment Nodes
    subgraph "Environment Nodes" 
        direction TB
        EnvSim["drl_environment (gazebo)"]:::drl
        EnvReal["drl_environment (real)"]:::drl
        Reward["Reward Computation"]:::drl
    end

    %% Agent Nodes
    subgraph "DRL Agent Nodes"
        direction TB
        AgentCore["DRL Agent Framework"]:::drl
        DQN["DQN Implementation"]:::drl
        DDPG["DDPG Implementation"]:::drl
        TD3["TD3 Implementation"]:::drl
        OffPolicy["Off-Policy Agent Base"]:::drl
    end

    %% Shared Msgs
    Msgs["turtlebot3_msgs"]:::cloud

    %% Storage
    Storage["Model Storage & Logs"]:::storage

    %% Utilities
    subgraph "Utilities & Post-Processing"
        direction TB
        CleanSingle["Clean Single Model"]:::gui
        PurgeAll["Purge All Models"]:::gui
        RewardGraph["Reward Graph Plotter"]:::gui
        StopRobot["Stop Robot Script"]:::gui
    end

    %% Connections
    DDS --> Gazebo
    DDS --> RealRobot
    Gazebo -->|"/scan & /odom"| EnvSim
    FakeNode -->|"/scan & /odom"| EnvSim
    RealRobot -->|"/scan & /odom"| EnvReal
    SpawnShell -->|"Goal.srv"| Gazebo
    SpawnUtil -->|"Goal.srv"| Gazebo
    EnvSim -->|"Observation & Reward"| AgentCore
    EnvReal -->|"Observation & Reward"| AgentCore
    AgentCore -->|"/cmd_vel"| Gazebo
    AgentCore -->|"/cmd_vel"| RealRobot
    Reward --> EnvSim
    Reward --> EnvReal
    AgentCore --> Storage
    DQN --> AgentCore
    DDPG --> AgentCore
    TD3 --> AgentCore
    OffPolicy --> AgentCore
    Msgs -.-> EnvSim
    Msgs -.-> EnvReal
    Msgs -.-> AgentCore
    CleanSingle --> Storage
    PurgeAll --> Storage
    RewardGraph --> Storage
    StopRobot --> Storage

    %% Click Events
    click Gazebo "https://github.com/yitinghh-bit/turtlebot3_drlnav/tree/main/src/turtlebot3_simulations/turtlebot3_gazebo"
    click FakeNode "https://github.com/yitinghh-bit/turtlebot3_drlnav/tree/main/src/turtlebot3_simulations/turtlebot3_fake_node"
    click SpawnShell "https://github.com/yitinghh-bit/turtlebot3_drlnav/blob/main/spawn_goal.sh"
    click SpawnUtil "https://github.com/yitinghh-bit/turtlebot3_drlnav/blob/main/util/spawn_goal.sh"
    click EnvSim "https://github.com/yitinghh-bit/turtlebot3_drlnav/blob/main/src/turtlebot3_drl/turtlebot3_drl/drl_environment/drl_environment.py"
    click EnvReal "https://github.com/yitinghh-bit/turtlebot3_drlnav/blob/main/src/turtlebot3_drl/turtlebot3_drl/drl_environment/drl_environment_real.py"
    click Reward "https://github.com/yitinghh-bit/turtlebot3_drlnav/blob/main/src/turtlebot3_drl/turtlebot3_drl/drl_environment/reward.py"
    click AgentCore "https://github.com/yitinghh-bit/turtlebot3_drlnav/blob/main/src/turtlebot3_drl/turtlebot3_drl/drl_agent/drl_agent.py"
    click DQN "https://github.com/yitinghh-bit/turtlebot3_drlnav/blob/main/src/turtlebot3_drl/turtlebot3_drl/drl_agent/dqn.py"
    click DDPG "https://github.com/yitinghh-bit/turtlebot3_drlnav/blob/main/src/turtlebot3_drl/turtlebot3_drl/drl_agent/ddpg.py"
    click TD3 "https://github.com/yitinghh-bit/turtlebot3_drlnav/blob/main/src/turtlebot3_drl/turtlebot3_drl/drl_agent/td3.py"
    click OffPolicy "https://github.com/yitinghh-bit/turtlebot3_drlnav/blob/main/src/turtlebot3_drl/turtlebot3_drl/drl_agent/off_policy_agent.py"
    click Storage "https://github.com/yitinghh-bit/turtlebot3_drlnav/tree/main/src/turtlebot3_drl/model"
    click Msgs "https://github.com/yitinghh-bit/turtlebot3_drlnav/tree/main/src/turtlebot3_msgs"
    click CleanSingle "https://github.com/yitinghh-bit/turtlebot3_drlnav/blob/main/util/clean_single_model.py"
    click PurgeAll "https://github.com/yitinghh-bit/turtlebot3_drlnav/blob/main/util/purge_all_models.py"
    click RewardGraph "https://github.com/yitinghh-bit/turtlebot3_drlnav/blob/main/util/reward_graph.py"
    click StopRobot "https://github.com/yitinghh-bit/turtlebot3_drlnav/blob/main/util/stop_robot.sh"

    %% Styles
    classDef sim fill:#CCE5FF,stroke:#00529B,color:#00529B;
    classDef drl fill:#D5FFD5,stroke:#2E8B57,color:#2E8B57;
    classDef storage fill:#FFE5B4,stroke:#CC8400,color:#CC8400;
    classDef cloud fill:#EEEEEE,stroke:#888888,color:#555555;
    classDef gui stroke-dasharray: 5 5,fill:#F0F0F0,stroke:#999999,color:#333333;
```
