Skip to main content

Mini-Project: Complete Humanoid Simulation Environment

Overview​

In this mini-project, we'll integrate everything we've learned in Module 2 to create a complete humanoid robot digital twin that combines realistic physics simulation in Gazebo with high-fidelity visualization in Unity. This project will serve as a foundation for developing and testing humanoid robot behaviors before deployment on real hardware.

We'll create a fully functional simulation environment that includes physics modeling, sensor simulation, and real-time visualization using both Gazebo and Unity, demonstrating the complete digital twin workflow.

Learning Objectives​

After completing this mini-project, you will be able to:

  1. Design and implement a complete humanoid robot simulation environment
  2. Synchronize physics simulation in Gazebo with visualization in Unity
  3. Integrate multiple sensors in both simulation environments
  4. Validate the simulation against real-world expectations
  5. Use the digital twin for development and testing of humanoid behaviors

Project Requirements​

Technical Requirements​

  • Create a humanoid robot model compatible with both Gazebo and Unity
  • Implement realistic physics simulation in Gazebo
  • Set up high-fidelity visualization in Unity
  • Synchronize robot state between both environments
  • Implement multiple sensor models (LiDAR, camera, IMU)
  • Create an interactive environment with obstacles and navigation goals
  • Implement basic locomotion patterns for the humanoid

Deliverables​

  • URDF model of humanoid robot
  • Gazebo world with environment
  • Unity visualization scene
  • ROS nodes for coordination between environments
  • Documentation of simulation setup and validation

Project Structure​

Directory Structure​

physical_ai_humanoid_robotics/
├── src/
│ ├── robot_description/
│ │ ├── urdf/
│ │ ├── meshes/
│ │ └── launch/
│ ├── simulation/
│ │ ├── gazebo_worlds/
│ │ ├── unity_scenes/
│ │ └── config/
│ └── simulation_control/
│ ├── src/
│ └── launch/

Step-by-Step Implementation​

Step 1: Create the Humanoid Robot Model​

First, we'll create a simplified humanoid robot model in URDF format. This model will include the basic structure needed for locomotion and interaction.

<!-- robot_description/urdf/humanoid_robot.urdf -->
<?xml version="1.0"?>
<robot name="humanoid_robot">
<!-- Base/Body Link -->
<link name="base_link">
<visual>
<geometry>
<capsule length="0.6" radius="0.1"/>
</geometry>
<material name="light_gray">
<color rgba="0.7 0.7 0.7 1"/>
</material>
</visual>
<collision>
<geometry>
<capsule length="0.6" radius="0.1"/>
</geometry>
</collision>
<inertial>
<mass value="50"/>
<inertia ixx="1.0" ixy="0.0" ixz="0.0" iyy="1.0" iyz="0.0" izz="1.0"/>
</inertial>
</link>

<!-- Head -->
<link name="head">
<visual>
<geometry>
<sphere radius="0.15"/>
</geometry>
<material name="white">
<color rgba="1 1 1 1"/>
</material>
</visual>
<collision>
<geometry>
<sphere radius="0.15"/>
</geometry>
</collision>
<inertial>
<mass value="2"/>
<inertia ixx="0.01" ixy="0.0" ixz="0.0" iyy="0.01" iyz="0.0" izz="0.01"/>
</inertial>
</link>

<joint name="head_joint" type="fixed">
<parent link="base_link"/>
<child link="head"/>
<origin xyz="0 0 0.45" rpy="0 0 0"/>
</joint>

<!-- Left Arm -->
<link name="left_upper_arm">
<visual>
<geometry>
<capsule length="0.4" radius="0.05"/>
</geometry>
<material name="blue">
<color rgba="0 0 1 1"/>
</material>
</visual>
<collision>
<geometry>
<capsule length="0.4" radius="0.05"/>
</geometry>
</collision>
<inertial>
<mass value="1"/>
<inertia ixx="0.01" ixy="0.0" ixz="0.0" iyy="0.01" iyz="0.0" izz="0.01"/>
</inertial>
</link>

<joint name="left_shoulder" type="revolute">
<parent link="base_link"/>
<child link="left_upper_arm"/>
<origin xyz="0.1 0.15 0.2" rpy="0 0 0"/>
<axis xyz="0 1 0"/>
<limit lower="-1.57" upper="1.57" effort="1000" velocity="1"/>
</joint>

<link name="left_lower_arm">
<visual>
<geometry>
<capsule length="0.4" radius="0.05"/>
</geometry>
<material name="blue">
<color rgba="0 0 1 1"/>
</material>
</visual>
<collision>
<geometry>
<capsule length="0.4" radius="0.05"/>
</geometry>
</collision>
<inertial>
<mass value="1"/>
<inertia ixx="0.01" ixy="0.0" ixz="0.0" iyy="0.01" iyz="0.0" izz="0.01"/>
</inertial>
</link>

<joint name="left_elbow" type="revolute">
<parent link="left_upper_arm"/>
<child link="left_lower_arm"/>
<origin xyz="0 0 -0.4" rpy="0 0 0"/>
<axis xyz="0 1 0"/>
<limit lower="0" upper="2.35" effort="1000" velocity="1"/>
</joint>

<!-- Right Arm -->
<link name="right_upper_arm">
<visual>
<geometry>
<capsule length="0.4" radius="0.05"/>
</geometry>
<material name="blue">
<color rgba="0 0 1 1"/>
</material>
</visual>
<collision>
<geometry>
<capsule length="0.4" radius="0.05"/>
</geometry>
</collision>
<inertial>
<mass value="1"/>
<inertia ixx="0.01" ixy="0.0" ixz="0.0" iyy="0.01" iyz="0.0" izz="0.01"/>
</inertial>
</link>

<joint name="right_shoulder" type="revolute">
<parent link="base_link"/>
<child link="right_upper_arm"/>
<origin xyz="0.1 -0.15 0.2" rpy="0 0 0"/>
<axis xyz="0 1 0"/>
<limit lower="-1.57" upper="1.57" effort="1000" velocity="1"/>
</joint>

<link name="right_lower_arm">
<visual>
<geometry>
<capsule length="0.4" radius="0.05"/>
</geometry>
<material name="blue">
<color rgba="0 0 1 1"/>
</material>
</visual>
<collision>
<geometry>
<capsule length="0.4" radius="0.05"/>
</geometry>
</collision>
<inertial>
<mass value="1"/>
<inertia ixx="0.01" ixy="0.0" ixz="0.0" iyy="0.01" iyz="0.0" izz="0.01"/>
</inertial>
</link>

<joint name="right_elbow" type="revolute">
<parent link="right_upper_arm"/>
<child link="right_lower_arm"/>
<origin xyz="0 0 -0.4" rpy="0 0 0"/>
<axis xyz="0 1 0"/>
<limit lower="0" upper="2.35" effort="1000" velocity="1"/>
</joint>

<!-- Left Leg -->
<link name="left_upper_leg">
<visual>
<geometry>
<capsule length="0.5" radius="0.07"/>
</geometry>
<material name="green">
<color rgba="0 1 0 1"/>
</material>
</visual>
<collision>
<geometry>
<capsule length="0.5" radius="0.07"/>
</geometry>
</collision>
<inertial>
<mass value="3"/>
<inertia ixx="0.05" ixy="0.0" ixz="0.0" iyy="0.05" iyz="0.0" izz="0.05"/>
</inertial>
</link>

<joint name="left_hip" type="revolute">
<parent link="base_link"/>
<child link="left_upper_leg"/>
<origin xyz="-0.1 0.1 -0.3" rpy="0 0 0"/>
<axis xyz="0 1 0"/>
<limit lower="-1.57" upper="1.57" effort="2000" velocity="1"/>
</joint>

<link name="left_lower_leg">
<visual>
<geometry>
<capsule length="0.5" radius="0.07"/>
</geometry>
<material name="green">
<color rgba="0 1 0 1"/>
</material>
</visual>
<collision>
<geometry>
<capsule length="0.5" radius="0.07"/>
</geometry>
</collision>
<inertial>
<mass value="2"/>
<inertia ixx="0.03" ixy="0.0" ixz="0.0" iyy="0.03" iyz="0.0" izz="0.03"/>
</inertial>
</link>

<joint name="left_knee" type="revolute">
<parent link="left_upper_leg"/>
<child link="left_lower_leg"/>
<origin xyz="0 0 -0.5" rpy="0 0 0"/>
<axis xyz="0 1 0"/>
<limit lower="0" upper="2.35" effort="2000" velocity="1"/>
</joint>

<link name="left_foot">
<visual>
<geometry>
<box size="0.2 0.1 0.05"/>
</geometry>
<material name="dark_gray">
<color rgba="0.3 0.3 0.3 1"/>
</material>
</visual>
<collision>
<geometry>
<box size="0.2 0.1 0.05"/>
</geometry>
</collision>
<inertial>
<mass value="1"/>
<inertia ixx="0.01" ixy="0.0" ixz="0.0" iyy="0.01" iyz="0.0" izz="0.01"/>
</inertial>
</link>

<joint name="left_ankle" type="fixed">
<parent link="left_lower_leg"/>
<child link="left_foot"/>
<origin xyz="0 0 -0.25" rpy="0 0 0"/>
</joint>

<!-- Right Leg -->
<link name="right_upper_leg">
<visual>
<geometry>
<capsule length="0.5" radius="0.07"/>
</geometry>
<material name="green">
<color rgba="0 1 0 1"/>
</material>
</visual>
<collision>
<geometry>
<capsule length="0.5" radius="0.07"/>
</geometry>
</collision>
<inertial>
<mass value="3"/>
<inertia ixx="0.05" ixy="0.0" ixz="0.0" iyy="0.05" iyz="0.0" izz="0.05"/>
</inertial>
</link>

<joint name="right_hip" type="revolute">
<parent link="base_link"/>
<child link="right_upper_leg"/>
<origin xyz="-0.1 -0.1 -0.3" rpy="0 0 0"/>
<axis xyz="0 1 0"/>
<limit lower="-1.57" upper="1.57" effort="2000" velocity="1"/>
</joint>

<link name="right_lower_leg">
<visual>
<geometry>
<capsule length="0.5" radius="0.07"/>
</geometry>
<material name="green">
<color rgba="0 1 0 1"/>
</material>
</visual>
<collision>
<geometry>
<capsule length="0.5" radius="0.07"/>
</geometry>
</collision>
<inertial>
<mass value="2"/>
<inertia ixx="0.03" ixy="0.0" ixz="0.0" iyy="0.03" iyz="0.0" izz="0.03"/>
</inertial>
</link>

<joint name="right_knee" type="revolute">
<parent link="right_upper_leg"/>
<child link="right_lower_leg"/>
<origin xyz="0 0 -0.5" rpy="0 0 0"/>
<axis xyz="0 1 0"/>
<limit lower="0" upper="2.35" effort="2000" velocity="1"/>
</joint>

<link name="right_foot">
<visual>
<geometry>
<box size="0.2 0.1 0.05"/>
</geometry>
<material name="dark_gray">
<color rgba="0.3 0.3 0.3 1"/>
</material>
</visual>
<collision>
<geometry>
<box size="0.2 0.1 0.05"/>
</geometry>
</collision>
<inertial>
<mass value="1"/>
<inertia ixx="0.01" ixy="0.0" ixz="0.0" iyy="0.01" iyz="0.0" izz="0.01"/>
</inertial>
</link>

<joint name="right_ankle" type="fixed">
<parent link="right_lower_leg"/>
<child link="right_foot"/>
<origin xyz="0 0 -0.25" rpy="0 0 0"/>
</joint>

<!-- Sensors -->
<link name="imu_link">
<visual>
<geometry>
<box size="0.02 0.02 0.02"/>
</geometry>
</visual>
<collision>
<geometry>
<box size="0.02 0.02 0.02"/>
</geometry>
</collision>
<inertial>
<mass value="0.1"/>
<inertia ixx="1e-5" ixy="0" ixz="0" iyy="1e-5" iyz="0" izz="1e-5"/>
</inertial>
</link>

<joint name="imu_joint" type="fixed">
<parent link="base_link"/>
<child link="imu_link"/>
<origin xyz="0 0 0.2" rpy="0 0 0"/>
</joint>

<link name="lidar_link">
<visual>
<geometry>
<cylinder radius="0.05" length="0.05"/>
</geometry>
</visual>
<collision>
<geometry>
<cylinder radius="0.05" length="0.05"/>
</geometry>
</collision>
<inertial>
<mass value="0.2"/>
<inertia ixx="1e-4" ixy="0" ixz="0" iyy="1e-4" iyz="0" izz="2e-4"/>
</inertial>
</link>

<joint name="lidar_joint" type="fixed">
<parent link="head"/>
<child link="lidar_link"/>
<origin xyz="0 0 0.1" rpy="0 0 0"/>
</joint>

<link name="camera_link">
<visual>
<geometry>
<box size="0.05 0.05 0.05"/>
</geometry>
</visual>
<collision>
<geometry>
<box size="0.05 0.05 0.05"/>
</geometry>
</collision>
<inertial>
<mass value="0.1"/>
<inertia ixx="1e-5" ixy="0" ixz="0" iyy="1e-5" iyz="0" izz="1e-5"/>
</inertial>
</link>

<joint name="camera_joint" type="fixed">
<parent link="head"/>
<child link="camera_link"/>
<origin xyz="0.1 0 0.05" rpy="0 0 0"/>
</joint>
</robot>

Step 2: Add Gazebo-Specific Elements to the Model​

Create a Gazebo model file that includes physics properties, sensors, and controllers:

<!-- robot_description/urdf/humanoid_robot.gazebo -->
<?xml version="1.0"?>
<robot>
<!-- Gazebo materials -->
<gazebo reference="base_link">
<material>Gazebo/Grey</material>
</gazebo>

<gazebo reference="head">
<material>Gazebo/White</material>
</gazebo>

<gazebo reference="left_upper_arm">
<material>Gazebo/Blue</material>
</gazebo>

<gazebo reference="left_lower_arm">
<material>Gazebo/Blue</material>
</gazebo>

<gazebo reference="right_upper_arm">
<material>Gazebo/Blue</material>
</gazebo>

<gazebo reference="right_lower_arm">
<material>Gazebo/Blue</material>
</gazebo>

<gazebo reference="left_upper_leg">
<material>Gazebo/Green</material>
</gazebo>

<gazebo reference="left_lower_leg">
<material>Gazebo/Green</material>
</gazebo>

<gazebo reference="right_upper_leg">
<material>Gazebo/Green</material>
</gazebo>

<gazebo reference="right_lower_leg">
<material>Gazebo/Green</material>
</gazebo>

<!-- Joint dynamics -->
<gazebo>
<plugin name="gazebo_ros_control" filename="libgazebo_ros_control.so">
<robotNamespace>/humanoid_robot</robotNamespace>
</plugin>
</gazebo>

<!-- IMU Sensor -->
<gazebo reference="imu_link">
<sensor type="imu" name="imu_sensor">
<always_on>true</always_on>
<update_rate>100</update_rate>
<visualize>true</visualize>
<imu>
<angular_velocity>
<x>
<noise type="gaussian">
<mean>0.0</mean>
<stddev>1.7e-3</stddev>
</noise>
</x>
<y>
<noise type="gaussian">
<mean>0.0</mean>
<stddev>1.7e-3</stddev>
</nois>
</y>
<z>
<noise type="gaussian">
<mean>0.0</mean>
<stddev>1.7e-3</stddev>
</noise>
</z>
</angular_velocity>
<linear_acceleration>
<x>
<noise type="gaussian">
<mean>0.0</mean>
<stddev>1.7e-2</stddev>
</noise>
</x>
<y>
<noise type="gaussian">
<mean>0.0</mean>
<stddev>1.7e-2</stddev>
</noise>
</y>
<z>
<noise type="gaussian">
<mean>0.0</mean>
<stddev>1.7e-2</stddev>
</noise>
</z>
</linear_acceleration>
</imu>
<plugin name="imu_controller" filename="libgazebo_ros_imu.so">
<alwaysOn>true</alwaysOn>
<updateRate>100.0</updateRate>
<bodyName>imu_link</bodyName>
<topicName>imu/data</topicName>
<serviceName>imu/service</serviceName>
<gaussianNoise>0.01</gaussianNoise>
<frameName>imu_link</frameName>
<initialOrientationAsReference>false</initialOrientationAsReference>
</plugin>
</sensor>
</gazebo>

<!-- LiDAR Sensor -->
<gazebo reference="lidar_link">
<sensor type="ray" name="humanoid_lidar">
<pose>0 0 0 0 0 0</pose>
<visualize>false</visualize>
<update_rate>10</update_rate>
<ray>
<scan>
<horizontal>
<samples>720</samples>
<resolution>1</resolution>
<min_angle>-3.14159</min_angle>
<max_angle>3.14159</max_angle>
</horizontal>
</scan>
<range>
<min>0.10</min>
<max>30.0</max>
<resolution>0.01</resolution>
</range>
</ray>
<plugin name="laser_controller" filename="libgazebo_ros_laser.so">
<topicName>scan</topicName>
<frameName>lidar_link</frameName>
</plugin>
</sensor>
</gazebo>

<!-- Camera Sensor -->
<gazebo reference="camera_link">
<sensor type="camera" name="humanoid_camera">
<update_rate>30.0</update_rate>
<camera name="head">
<horizontal_fov>1.3962634</horizontal_fov>
<image>
<width>800</width>
<height>600</height>
<format>R8G8B8</format>
</image>
<clip>
<near>0.02</near>
<far>300</far>
</clip>
<noise>
<type>gaussian</type>
<mean>0.0</mean>
<stddev>0.007</stddev>
</noise>
</camera>
<plugin name="camera_controller" filename="libgazebo_ros_camera.so">
<cameraName>humanoid_camera</cameraName>
<imageTopicName>image_raw</imageTopicName>
<cameraInfoTopicName>camera_info</cameraInfoTopicName>
<frameName>camera_link</frameName>
<hackBaseline>0.07</hackBaseline>
<distortionK1>0.0</distortionK1>
<distortionK2>0.0</distortionK2>
<distortionK3>0.0</distortionK3>
<distortionT1>0.0</distortionT1>
<distortionT2>0.0</distortionT2>
</plugin>
</sensor>
</gazebo>
</robot>

Step 3: Create a Gazebo World​

Create a world file for Gazebo with a simple indoor environment:

<!-- simulation/gazebo_worlds/humanoid_environment.world -->
<?xml version="1.0" ?>
<sdf version="1.6">
<world name="humanoid_environment">
<light name="sun" type="directional">
<cast_shadows>true</cast_shadows>
<pose>0 0 10 0 -0 0</pose>
<diffuse>0.8 0.8 0.8 1</diffuse>
<specular>0.2 0.2 0.2 1</specular>
<attenuation>
<range>1000</range>
<constant>0.9</constant>
<linear>0.01</linear>
<quadratic>0.001</quadratic>
</attenuation>
<direction>-0.0 0.0 -1</direction>
</light>

<model name="ground_plane">
<static>true</static>
<link name="link">
<collision name="collision">
<geometry>
<plane>
<normal>0 0 1</normal>
<size>100 100</size>
</plane>
</geometry>
<surface>
<friction>
<ode>
<mu>1.0</mu>
<mu2>1.0</mu2>
</ode>
</friction>
</surface>
</collision>
<visual name="visual">
<geometry>
<plane>
<normal>0 0 1</normal>
<size>100 100</size>
</plane>
</geometry>
<material>
<ambient>0.7 0.7 0.7 1</ambient>
<diffuse>0.7 0.7 0.7 1</diffuse>
<specular>0.0 0.0 0.0 1</specular>
</material>
</visual>
</link>
</model>

<!-- Room walls -->
<model name="wall1">
<static>true</static>
<link name="link">
<collision name="collision">
<geometry>
<box>
<size>20 0.2 2.5</size>
</box>
</geometry>
</collision>
<visual name="visual">
<geometry>
<box>
<size>20 0.2 2.5</size>
</box>
</geometry>
<material>
<ambient>0.8 0.8 0.8 1</ambient>
<diffuse>0.8 0.8 0.8 1</diffuse>
<specular>0.2 0.2 0.2 1</specular>
</material>
</visual>
</link>
<pose>-10 0 1.25 0 0 0</pose>
</model>

<model name="wall2">
<static>true</static>
<link name="link">
<collision name="collision">
<geometry>
<box>
<size>20 0.2 2.5</size>
</box>
</geometry>
</collision>
<visual name="visual">
<geometry>
<box>
<size>20 0.2 2.5</size>
</box>
</geometry>
<material>
<ambient>0.8 0.8 0.8 1</ambient>
<diffuse>0.8 0.8 0.8 1</diffuse>
<specular>0.2 0.2 0.2 1</specular>
</material>
</visual>
</link>
<pose>10 0 1.25 0 0 0</pose>
</model>

<model name="wall3">
<static>true</static>
<link name="link">
<collision name="collision">
<geometry>
<box>
<size>0.2 20 2.5</size>
</box>
</geometry>
</collision>
<visual name="visual">
<geometry>
<box>
<size>0.2 20 2.5</size>
</box>
</geometry>
<material>
<ambient>0.8 0.8 0.8 1</ambient>
<diffuse>0.8 0.8 0.8 1</diffuse>
<specular>0.2 0.2 0.2 1</specular>
</material>
</visual>
</link>
<pose>0 -10 1.25 0 0 0</pose>
</model>

<model name="wall4">
<static>true</static>
<link name="link">
<collision name="collision">
<geometry>
<box>
<size>0.2 20 2.5</size>
</box>
</geometry>
</collision>
<visual name="visual">
<geometry>
<box>
<size>0.2 20 2.5</size>
</box>
</geometry>
<material>
<ambient>0.8 0.8 0.8 1</ambient>
<diffuse>0.8 0.8 0.8 1</diffuse>
<specular>0.2 0.2 0.2 1</specular>
</material>
</visual>
</link>
<pose>0 10 1.25 0 0 0</pose>
</model>

<!-- Objects in environment -->
<model name="table1">
<static>true</static>
<link name="link">
<collision name="collision">
<geometry>
<box>
<size>1.5 1 0.8</size>
</box>
</geometry>
</collision>
<visual name="visual">
<geometry>
<box>
<size>1.5 1 0.8</size>
</box>
</geometry>
<material>
<ambient>0.3 0.2 0.1 1</ambient>
<diffuse>0.3 0.2 0.1 1</diffuse>
<specular>0.5 0.5 0.5 1</specular>
</material>
</visual>
</link>
<pose>-3 2 0.4 0 0 0</pose>
</model>

<model name="chair1">
<static>true</static>
<link name="link">
<collision name="collision">
<geometry>
<box>
<size>0.5 0.5 0.8</size>
</box>
</geometry>
</collision>
<visual name="visual">
<geometry>
<box>
<size>0.5 0.5 0.8</size>
</box>
</geometry>
<material>
<ambient>0.4 0.4 0.4 1</ambient>
<diffuse>0.4 0.4 0.4 1</diffuse>
<specular>0.5 0.5 0.5 1</specular>
</material>
</visual>
</link>
<pose>-2.5 3.5 0.4 0 0 0</pose>
</model>

<!-- Spawn the humanoid robot -->
<include>
<uri>model://humanoid_robot</uri>
<pose>0 0 1.0 0 0 0</pose>
</include>

<gravity>0 0 -9.8</gravity>
<magnetic_field>6e-06 2.3e-05 -4.2e-05</magnetic_field>
<atmosphere type="adiabatic"/>
<physics type="ode">
<max_step_size>0.001</max_step_size>
<real_time_factor>1</real_time_factor>
<real_time_update_rate>1000</real_time_update_rate>
</physics>
</world>
</sdf>

Step 4: Create a Control Node for Robot Simulation​

Create a ROS node to control the humanoid robot in simulation:

// simulation_control/src/humanoid_controller.cpp
#include <ros/ros.h>
#include <sensor_msgs/JointState.h>
#include <geometry_msgs/Twist.h>
#include <control_msgs/JointTrajectoryControllerState.h>
#include <trajectory_msgs/JointTrajectory.h>
#include <std_msgs/Float64.h>

#include <iostream>
#include <vector>
#include <cmath>

class HumanoidController {
private:
ros::NodeHandle nh_;
ros::Publisher joint_pub_;
ros::Subscriber joint_sub_;
ros::Timer timer_;

std::vector<std::string> joint_names_;
std::vector<double> joint_positions_;
std::vector<double> joint_velocities_;
std::vector<double> joint_efforts_;

public:
HumanoidController() {
// Initialize joint names
joint_names_.push_back("left_hip");
joint_names_.push_back("left_knee");
joint_names_.push_back("right_hip");
joint_names_.push_back("right_knee");
joint_names_.push_back("left_shoulder");
joint_names_.push_back("left_elbow");
joint_names_.push_back("right_shoulder");
joint_names_.push_back("right_elbow");

// Initialize joint vectors
joint_positions_.resize(joint_names_.size(), 0.0);
joint_velocities_.resize(joint_names_.size(), 0.0);
joint_efforts_.resize(joint_names_.size(), 0.0);

// Publishers and subscribers
joint_pub_ = nh_.advertise<trajectory_msgs::JointTrajectory>("/humanoid_robot/joint_group_position_controller/command", 1);
joint_sub_ = nh_.subscribe("/humanoid_robot/joint_states", 1, &HumanoidController::jointStatesCallback, this);

// Timer for publishing commands
timer_ = nh_.createTimer(ros::Duration(0.1), &HumanoidController::timerCallback, this);

ROS_INFO("Humanoid Controller initialized");
}

void jointStatesCallback(const sensor_msgs::JointState::ConstPtr& msg) {
// Update internal joint state
for(size_t i = 0; i < joint_names_.size(); ++i) {
for(size_t j = 0; j < msg->name.size(); ++j) {
if(msg->name[j] == joint_names_[i]) {
joint_positions_[i] = msg->position[j];
if(j < msg->velocity.size()) joint_velocities_[i] = msg->velocity[j];
if(j < msg->effort.size()) joint_efforts_[i] = msg->effort[j];
break;
}
}
}
}

void timerCallback(const ros::TimerEvent& event) {
// Publish joint trajectory command
trajectory_msgs::JointTrajectory traj_msg;
traj_msg.joint_names = joint_names_;

trajectory_msgs::JointTrajectoryPoint point;
point.positions.resize(joint_names_.size());

// Simple walking gait pattern - this is a basic example
static double t = 0.0;
t += 0.1; // Increment time

// Create a simple walking pattern for demonstration
point.positions[0] = 0.5 * sin(t); // left_hip
point.positions[1] = 0.7 * sin(t + 0.5); // left_knee
point.positions[2] = 0.5 * sin(t + 0.2); // right_hip
point.positions[3] = 0.7 * sin(t + 0.7); // right_knee
point.positions[4] = 0.2 * sin(t + 0.3); // left_shoulder
point.positions[5] = 0.3 * cos(t + 0.3); // left_elbow
point.positions[6] = 0.2 * sin(t + 0.3); // right_shoulder
point.positions[7] = 0.3 * cos(t + 0.3); // right_elbow

point.velocities.resize(joint_names_.size(), 0.0);
point.accelerations.resize(joint_names_.size(), 0.0);

point.time_from_start = ros::Duration(0.1);

traj_msg.points.push_back(point);

joint_pub_.publish(traj_msg);
}

void run() {
ros::spin();
}
};

int main(int argc, char** argv) {
ros::init(argc, argv, "humanoid_controller");

HumanoidController controller;
controller.run();

return 0;
}

Step 5: Create Unity Scene for Visualization​

For Unity visualization, we'll create a basic script that connects to ROS and visualizes the robot. The actual Unity implementation would involve:

  1. Setting up the Unity project with ROS TCP connector
  2. Creating a humanoid robot model in Unity
  3. Writing a script to synchronize joint states from ROS

Here's a basic Unity script for visualization:

// simulation_control/unity_scripts/UnityRobotVisualizer.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Unity.Robotics.ROSTCPConnector;
using Unity.Robotics.ROSTCPConnector.MessageTypes.Sensor;
using Newtonsoft.Json;

public class UnityRobotVisualizer : MonoBehaviour
{
ROSConnection ros;
string jointStateTopic = "/humanoid_robot/joint_states";

Dictionary<string, Transform> jointMap = new Dictionary<string, Transform>();

// Robot joints in Unity
[Header("Robot Joint Transforms")]
public Transform leftHipJoint;
public Transform leftKneeJoint;
public Transform rightHipJoint;
public Transform rightKneeJoint;
public Transform leftShoulderJoint;
public Transform leftElbowJoint;
public Transform rightShoulderJoint;
public Transform rightElbowJoint;

void Start()
{
ros = ROSConnection.GetOrCreateInstance();
ros.Subscribe<JointStateMsg>(jointStateTopic, OnJointStateReceived);

// Initialize joint map
InitializeJointMap();
}

void InitializeJointMap()
{
// Map joint names to transforms
jointMap["left_hip"] = leftHipJoint;
jointMap["left_knee"] = leftKneeJoint;
jointMap["right_hip"] = rightHipJoint;
jointMap["right_knee"] = rightKneeJoint;
jointMap["left_shoulder"] = leftShoulderJoint;
jointMap["left_elbow"] = leftElbowJoint;
jointMap["right_shoulder"] = rightShoulderJoint;
jointMap["right_elbow"] = rightElbowJoint;
}

void OnJointStateReceived(JointStateMsg jointState)
{
for (int i = 0; i < jointState.name.Count; i++)
{
string jointName = jointState.name[i];
float jointPosition = jointState.position[i];

if (jointMap.ContainsKey(jointName))
{
// Update the joint transform based on received position
// Apply the position with a rotation around the appropriate axis
Vector3 rotationAxis = GetRotationAxis(jointName);
jointMap[jointName].Rotate(rotationAxis, jointPosition * Mathf.Rad2Deg);
}
}
}

Vector3 GetRotationAxis(string jointName)
{
// Define rotation axes for different joints
// This is a simplified example - real implementation would be more specific
switch (jointName)
{
case "left_hip":
case "right_hip":
return Vector3.right; // Hip joints rotate in the sagittal plane
case "left_knee":
case "right_knee":
return Vector3.right; // Knee joints rotate in the sagittal plane
case "left_shoulder":
case "right_shoulder":
return Vector3.up; // Shoulder joints can rotate around multiple axes
case "left_elbow":
case "right_elbow":
return Vector3.forward; // Elbow joints rotate around the forward axis
default:
return Vector3.up;
}
}

void Update()
{
// Additional update logic for visualization
// e.g., camera following, environment interaction
}
}

Step 6: Create Launch Files​

Create a launch file to run the complete simulation:

<!-- simulation_control/launch/humanoid_simulation.launch -->
<launch>
<!-- Load robot description -->
<param name="robot_description" command="$(find xacro)/xacro $(find robot_description)/urdf/humanoid_robot.urdf" />

<!-- Start joint state publisher -->
<node name="joint_state_publisher" pkg="joint_state_publisher" type="joint_state_publisher">
<param name="use_gui" value="false" />
<rosparam param="source_list">[humanoid_robot/joint_states]</rosparam>
</node>

<!-- Start robot state publisher -->
<node name="robot_state_publisher" pkg="robot_state_publisher" type="robot_state_publisher" />

<!-- Start Gazebo with the humanoid environment -->
<include file="$(find gazebo_ros)/launch/empty_world.launch">
<arg name="world_name" value="$(find simulation)/gazebo_worlds/humanoid_environment.world"/>
<arg name="paused" value="false"/>
<arg name="use_sim_time" value="true"/>
<arg name="gui" value="true"/>
<arg name="headless" value="false"/>
<arg name="debug" value="false"/>
</include>

<!-- Spawn the humanoid robot in Gazebo -->
<node name="spawn_urdf" pkg="gazebo_ros" type="spawn_model"
args="-param robot_description -urdf -model humanoid_robot -x 0 -y 0 -z 1.0" />

<!-- Start the humanoid controller -->
<node name="humanoid_controller" pkg="simulation_control" type="humanoid_controller" output="screen" />

<!-- Start the RViz visualization -->
<node name="rviz" pkg="rviz" type="rviz" args="-d $(find simulation_control)/rviz/humanoid_config.rviz" required="false" />
</launch>

Step 7: Create Configuration Files​

Create a controller configuration file for Gazebo:

# simulation_control/config/joint_position_controllers.yaml
humanoid_robot:
# Joint trajectory controller
joint_group_position_controller:
type: position_controllers/JointTrajectoryController
joints:
- left_hip
- left_knee
- right_hip
- right_knee
- left_shoulder
- left_elbow
- right_shoulder
- right_elbow
constraints:
goal_time: 0.6
stopped_velocity_tolerance: 0.05
left_hip:
trajectory: 0.05
goal: 0.01
left_knee:
trajectory: 0.05
goal: 0.01
right_hip:
trajectory: 0.05
goal: 0.01
right_knee:
trajectory: 0.05
goal: 0.01
left_shoulder:
trajectory: 0.05
goal: 0.01
left_elbow:
trajectory: 0.05
goal: 0.01
right_shoulder:
trajectory: 0.05
goal: 0.01
right_elbow:
trajectory: 0.05
goal: 0.01
stop_trajectory_duration: 0.5
state_publish_rate: 25
action_monitor_rate: 10

Testing and Validation​

Running the Complete Simulation​

To run the complete simulation:

  1. Launch the simulation:

    roslaunch simulation_control humanoid_simulation.launch
  2. Observe the humanoid robot in Gazebo, ensuring:

    • The robot model is correctly loaded
    • Physics simulation is working properly
    • Sensors are publishing data
    • The controller is sending commands
  3. Verify that the robot is performing the programmed walking gait

Validation Metrics​

For validating the digital twin:

  1. Kinematic Accuracy: Ensure joint limits and ranges match the real robot
  2. Dynamic Behavior: Verify that the simulated robot behaves similarly to real physics
  3. Sensor Data Quality: Check that sensor readings are realistic
  4. Control Response: Confirm that control inputs produce expected motions

Advanced Topics​

Adding More Realistic Dynamics​

To improve the simulation, consider:

  1. Adding more sophisticated joint dynamics
  2. Implementing balance control algorithms
  3. Including muscle-like actuators for more biological movement

Extending to Real Robot​

To connect your simulation to a real robot:

  1. Implement state synchronization between simulation and reality
  2. Use sim-to-real transfer techniques to adapt learned behaviors
  3. Validate behaviors in simulation before running on the real robot

Troubleshooting Notes​

  1. Robot not spawning: Check URDF file validity and ensure all mesh files exist

  2. Physics issues: Adjust physics parameters in the Gazebo world file if the robot behaves unrealistically

  3. Controller problems: Verify that joint names match between URDF and controller configuration

  4. Performance issues: Reduce complexity of models or physics parameters if simulation runs slowly

Summary​

In this mini-project, we've created a complete digital twin for a humanoid robot by integrating Gazebo physics simulation with Unity visualization. We've implemented sensor simulation, control systems, and a realistic environment to allow for comprehensive testing and development of humanoid behaviors.

This foundation provides a platform for developing more complex humanoid behaviors, testing navigation algorithms, and validating perception systems before deployment on real hardware.

Next Steps​

In the next module, we'll explore NVIDIA Isaac ROS for implementing AI-powered perception and navigation systems for our humanoid robot, building on the digital twin we've created in this module.

References​

  1. ROS Control Framework: http://wiki.ros.org/ros_control
  2. Gazebo Simulation: http://gazebosim.org/
  3. Unity Robotics: https://unity.com/solutions/robotics
  4. Robot Operating System: http://wiki.ros.org/
  5. NVIDIA Isaac ROS: https://developer.nvidia.com/isaac-ros/isaac-ros-documentation
💬
🤖
ROBO-ASSISTANT
Hello! I'm your AI assistant for Physical AI & Humanoid Robotics. How can I help you today?