Skip to main content

Chapter 2: Digital Twin & Simulation

Create a virtual robot replica that mirrors and controls your physical humanoid.

Overview​

This chapter teaches you to build a Digital Twin—a synchronized virtual copy of a physical robot. You'll learn to run simulations in Gazebo, create custom worlds, spawn humanoid models, and build a bridge node that enables real-time bidirectional communication between simulation and reality.

Prerequisites: Chapter 1 (URDF Fundamentals) completed, ROS 2 Humble installed

Time Estimate: 6-12 hours total (2-4 hours per tier)


Learning Path​

Beginner Tier​

Goal: Understand digital twin concepts and run your first simulation

LessonTitleDurationDescription
B1What is a Digital Twin?45-60 minCore concepts, mental models, physical-virtual sync
B2Running Your First Simulation60-90 minGazebo launch, navigation, ROS 2 integration

Assets:

Outcome: Launch Gazebo with a pre-built humanoid world and explain digital twin concepts.


Intermediate Tier​

Goal: Build custom simulation worlds and spawn humanoid models

LessonTitleDurationDescription
I1Building Simulation Worlds60-90 minWorld file creation, physics config, lighting
I2Spawning and Controlling Models60-90 minURDF spawning, joint control, ROS 2 launch

Assets:

Outcome: Create your own .world file and spawn a URDF humanoid with joint control.


Advanced Tier​

Goal: Implement bidirectional data synchronization for digital twin loop

LessonTitleDurationDescription
A1Digital Twin Architecture60-90 minData sync patterns, topic mapping, latency design
A2Building the Bridge Node90-120 minBridge implementation, latency monitoring, AI training

Assets:

Outcome: Build a bridge node with <50ms latency for real-time robot-simulation sync.


Exercises​

ExerciseTierFocusTime
Exercise 01: Launch WorldBeginnerLaunch and explore Gazebo world30-45 min
Exercise 02: Create WorldIntermediateBuild a custom simulation world45-60 min
Exercise 03: Build BridgeAdvancedImplement digital twin bridge60-90 min

Key Concepts​

ConceptDefinitionTarget
Digital TwinVirtual replica synchronized with physical robotReal-time sync
Gazebo ClassicPhysics simulation platform for roboticsVersion 11.x
Real-Time Factor (RTF)Simulation speed vs real time>= 0.8
Bridge NodeROS 2 node connecting sim to physical robotBidirectional
Latency ThresholdMaximum acceptable delay<= 50ms

Technical Requirements​

ComponentVersionNotes
Ubuntu22.04 LTSNative or WSL2
ROS 2HumbleWith rclpy
GazeboClassic (11.x)gazebo_ros_pkgs
Python3.10+Standard library

Troubleshooting​

Gazebo Issues​

ProblemCauseSolution
Gazebo won't startMissing dependenciessudo apt install gazebo11 ros-humble-gazebo-ros-pkgs
Black screenGPU driver issueTry export LIBGL_ALWAYS_SOFTWARE=1
Slow startupFirst-time model downloadWait for models to download; check ~/.gazebo/models/
World file not foundIncorrect pathUse absolute path or set GAZEBO_RESOURCE_PATH

RTF Issues​

ProblemCauseSolution
RTF < 0.5Complex worldSimplify collision geometry, reduce polygon count
RTF drops over timeMemory leakRestart Gazebo; check for orphaned processes
RTF spikesPhysics solver issuesIncrease solver iterations in world file
RTF shows 0Simulation pausedPress Space in Gazebo to resume

URDF Spawning Issues​

ProblemCauseSolution
Model explodesInvalid inertiaCheck all links have positive definite inertia
Model falls through floorMissing collisionAdd <collision> tags to all links
Joints don't moveMissing transmissionAdd <transmission> and controller
Spawn service failsTopic not availableEnsure gazebo_ros is launched first

Latency Issues​

ProblemCauseSolution
Latency > 100msNetwork congestionUse wired connection, reduce message size
Latency spikesPython GC pausesUse C++ for production, tune GC
Latency grows over timeQueue buildupReduce publish rate, use BEST_EFFORT QoS
Clock skewUnsync'd clocksUse /use_sim_time parameter

Bridge Issues​

ProblemCauseSolution
No messages receivedQoS mismatchAlign publisher/subscriber QoS profiles
Mode not changingParameter not setUse ros2 param set /bridge_node mode "live"
Connection lost alertsTimeout too shortIncrease watchdog timeout
Safety blocks all commandsLimits too strictAdjust max_joint_position/velocity params

Quick Reference​

Common Commands​

# Launch Gazebo with world
gazebo --verbose path/to/world.world

# Check Gazebo stats
gz stats

# Spawn URDF model
ros2 run gazebo_ros spawn_entity.py -entity robot -file robot.urdf -x 0 -y 0 -z 1

# List topics
ros2 topic list | grep -E "(joint|sim|hw)"

# Echo joint states
ros2 topic echo /joint_states --once

# Reset simulation
ros2 service call /reset_simulation std_srvs/srv/Empty

Environment Setup​

# Source ROS 2
source /opt/ros/humble/setup.bash

# Set Gazebo paths (if needed)
export GAZEBO_MODEL_PATH=$GAZEBO_MODEL_PATH:~/ros2_ws/src/your_models
export GAZEBO_RESOURCE_PATH=$GAZEBO_RESOURCE_PATH:~/ros2_ws/src/your_worlds

Chapter Contents​

02-digital-twin/
├── README.md # This file
├── beginner/
│ ├── B1-digital-twin-concepts.md # What is a Digital Twin?
│ ├── B2-first-simulation.md # Running Your First Simulation
│ └── assets/
│ └── humanoid_lab.world # Demo world file
├── intermediate/
│ ├── I1-building-worlds.md # Building Simulation Worlds
│ ├── I2-spawning-models.md # Spawning and Controlling Models
│ └── assets/
│ ├── simple_lab.world # Template world
│ ├── launch/
│ │ └── spawn_humanoid.launch.py
│ └── src/
│ └── joint_commander.py
├── advanced/
│ ├── A1-data-synchronization.md # Digital Twin Architecture
│ ├── A2-building-bridge.md # Building the Bridge Node
│ ├── src/
│ │ ├── bridge_node.py # Bridge node implementation
│ │ ├── latency_monitor.py # Latency tracking
│ │ └── sensor_streamer.py # AI training streamer
│ └── assets/
│ └── diagrams/
│ └── ai-training-architecture.md
└── exercises/
├── exercise-01-launch-world.md # Beginner exercise
├── exercise-02-create-world.md # Intermediate exercise
└── exercise-03-build-bridge.md # Advanced exercise

PreviousHomeNext
Chapter 1: ROS 2 & Nervous SystemBook HomeChapter 3: AI Robot Brain