Skip to main content

5.1 Project Setup & Package Creation

Estimated Time

15 minutes

Welcome to Phase 1! Before we can make our humanoid walk, we need to set up a solid foundation. In this section, you'll create a proper ROS 2 package with all the necessary structure and dependencies.

Project Requirements & Goals​

Let's be clear about what we're building:

Functional Requirements​

  • Generate walking gait trajectories for 6 leg joints (3 per leg)
  • Publish joint positions at 50 Hz (real-time control rate)
  • Support configurable parameters (step height, stride length, frequency)
  • Provide smooth, continuous motion suitable for visualization

Technical Requirements​

  • ROS 2 Humble (or compatible distribution)
  • Python 3 implementation using rclpy
  • Standard message types (sensor_msgs/JointState)
  • Proper package structure following ROS 2 conventions

Success Criteria​

✅ Package builds without errors ✅ All dependencies are properly declared ✅ Directory structure is clean and organized ✅ Initial test node runs successfully

Step 1: Create Your Workspace (If Needed)​

If you don't already have a ROS 2 workspace for this book, let's create one:

# Create workspace directory
mkdir -p ~/humanoid_ws/src
cd ~/humanoid_ws/src

If you already have a workspace from previous chapters, navigate to its src directory:

cd ~/your_workspace/src

Step 2: Create the Package​

Now let's create our gait_publisher package with all necessary dependencies:

ros2 pkg create gait_publisher \
--build-type ament_python \
--dependencies rclpy sensor_msgs std_msgs geometry_msgs \
--description "Walking gait trajectory publisher for humanoid robots" \
--license Apache-2.0 \
--maintainer-name "Your Name" \
--maintainer-email "your.email@example.com"
What Just Happened?
  • --build-type ament_python: Creates a Python package (not C++)
  • --dependencies: Automatically adds required packages to package.xml
  • --description: Adds metadata for documentation
  • --license: Sets open-source license (Apache 2.0 is ROS standard)

You should see output like:

going to create a new package
package name: gait_publisher
destination directory: /home/user/humanoid_ws/src
...

Step 3: Understand the Initial Structure​

Let's look at what was created:

cd gait_publisher
tree

You'll see:

gait_publisher/
├── gait_publisher/
│ └── __init__.py
├── package.xml
├── resource/
│ └── gait_publisher
├── setup.cfg
├── setup.py
└── test/
├── test_copyright.py
├── test_flake8.py
└── test_pep257.py

Key Files Explained​

package.xml: Package metadata and dependencies setup.py: Python package installation configuration setup.cfg: Additional Python configuration gait_publisher/__init__.py: Makes directory a Python package resource/gait_publisher: ROS 2 resource marker file

Step 4: Enhance the Directory Structure​

Let's add the structure we'll need for our project:

# Create directories for organized code
mkdir -p gait_publisher/math
mkdir -p launch
mkdir -p config
mkdir -p urdf

# Create __init__.py for the math module
touch gait_publisher/math/__init__.py

Your structure should now look like:

gait_publisher/
├── gait_publisher/
│ ├── __init__.py
│ └── math/
│ └── __init__.py
├── launch/
├── config/
├── urdf/
├── package.xml
├── setup.py
└── setup.cfg

Directory Purpose​

  • gait_publisher/math/: Mathematical utilities (gait generator class)
  • launch/: Launch files to start the system
  • config/: Configuration files (RViz configs, parameters)
  • urdf/: Robot description files

Step 5: Configure package.xml​

Open package.xml and let's verify/enhance it:

package.xml
<?xml version="1.0"?>
<?xml-model href="http://download.ros.org/schema/package_format3.xsd" schematypens="http://www.w3.org/2001/XMLSchema"?>
<package format="3">
<name>gait_publisher</name>
<version>1.0.0</version>
<description>Walking gait trajectory publisher for humanoid robots</description>
<maintainer email="your.email@example.com">Your Name</maintainer>
<license>Apache-2.0</license>

<!-- Build dependencies -->
<buildtool_depend>ament_python</buildtool_depend>

<!-- Runtime dependencies -->
<depend>rclpy</depend>
<depend>sensor_msgs</depend>
<depend>std_msgs</depend>
<depend>geometry_msgs</depend>

<!-- Additional dependencies for visualization -->
<exec_depend>robot_state_publisher</exec_depend>
<exec_depend>joint_state_publisher</exec_depend>
<exec_depend>rviz2</exec_depend>
<exec_depend>xacro</exec_depend>

<!-- Testing dependencies -->
<test_depend>ament_copyright</test_depend>
<test_depend>ament_flake8</test_depend>
<test_depend>ament_pep257</test_depend>
<test_depend>python3-pytest</test_depend>

<export>
<build_type>ament_python</build_type>
</export>
</package>
Dependency Types
  • <buildtool_depend>: Tools needed to build the package
  • <depend>: Required at both build and runtime
  • <exec_depend>: Only needed at runtime
  • <test_depend>: Only needed for testing

Step 6: Configure setup.py​

Open setup.py and modify it to include our additional directories:

setup.py
from setuptools import find_packages, setup
import os
from glob import glob

package_name = 'gait_publisher'

setup(
name=package_name,
version='1.0.0',
packages=find_packages(exclude=['test']),
data_files=[
('share/ament_index/resource_index/packages',
['resource/' + package_name]),
('share/' + package_name, ['package.xml']),
# Include launch files
(os.path.join('share', package_name, 'launch'),
glob(os.path.join('launch', '*launch.[pxy][yma]*'))),
# Include config files
(os.path.join('share', package_name, 'config'),
glob(os.path.join('config', '*.yaml'))),
(os.path.join('share', package_name, 'config'),
glob(os.path.join('config', '*.rviz'))),
# Include URDF files
(os.path.join('share', package_name, 'urdf'),
glob(os.path.join('urdf', '*.urdf'))),
(os.path.join('share', package_name, 'urdf'),
glob(os.path.join('urdf', '*.xacro'))),
],
install_requires=['setuptools'],
zip_safe=True,
maintainer='Your Name',
maintainer_email='your.email@example.com',
description='Walking gait trajectory publisher for humanoid robots',
license='Apache-2.0',
tests_require=['pytest'],
entry_points={
'console_scripts': [
# We'll add our node entries here in later steps
# 'gait_publisher_node = gait_publisher.gait_publisher_node:main',
],
},
)
What's Important Here?

The data_files section tells ROS 2 to install our launch files, configs, and URDF files so they're accessible after building. The glob patterns automatically include all matching files.

Step 7: Create a Simple Humanoid URDF​

Let's create a basic humanoid robot model. We'll keep it simple—just legs for walking visualization:

urdf/simple_humanoid.urdf
<?xml version="1.0"?>
<robot name="simple_humanoid">

<!-- Base link (pelvis/torso) -->
<link name="base_link">
<visual>
<geometry>
<box size="0.3 0.2 0.4"/>
</geometry>
<material name="blue">
<color rgba="0.3 0.3 0.8 1.0"/>
</material>
</visual>
<collision>
<geometry>
<box size="0.3 0.2 0.4"/>
</geometry>
</collision>
<inertial>
<mass value="10.0"/>
<inertia ixx="0.1" ixy="0.0" ixz="0.0"
iyy="0.1" iyz="0.0" izz="0.1"/>
</inertial>
</link>

<!-- Left Hip Joint and Thigh -->
<joint name="left_hip_joint" type="revolute">
<parent link="base_link"/>
<child link="left_thigh"/>
<origin xyz="0 0.1 -0.2" rpy="0 0 0"/>
<axis xyz="0 1 0"/>
<limit lower="-1.57" upper="1.57" effort="100" velocity="10"/>
</joint>

<link name="left_thigh">
<visual>
<origin xyz="0 0 -0.2" rpy="0 0 0"/>
<geometry>
<cylinder radius="0.05" length="0.4"/>
</geometry>
<material name="gray">
<color rgba="0.5 0.5 0.5 1.0"/>
</material>
</visual>
<collision>
<origin xyz="0 0 -0.2" rpy="0 0 0"/>
<geometry>
<cylinder radius="0.05" length="0.4"/>
</geometry>
</collision>
<inertial>
<mass value="2.0"/>
<inertia ixx="0.05" ixy="0.0" ixz="0.0"
iyy="0.05" iyz="0.0" izz="0.001"/>
</inertial>
</link>

<!-- Left Knee Joint and Shin -->
<joint name="left_knee_joint" type="revolute">
<parent link="left_thigh"/>
<child link="left_shin"/>
<origin xyz="0 0 -0.4" rpy="0 0 0"/>
<axis xyz="0 1 0"/>
<limit lower="-2.0" upper="0.0" effort="100" velocity="10"/>
</joint>

<link name="left_shin">
<visual>
<origin xyz="0 0 -0.2" rpy="0 0 0"/>
<geometry>
<cylinder radius="0.04" length="0.4"/>
</geometry>
<material name="gray"/>
</visual>
<collision>
<origin xyz="0 0 -0.2" rpy="0 0 0"/>
<geometry>
<cylinder radius="0.04" length="0.4"/>
</geometry>
</collision>
<inertial>
<mass value="1.5"/>
<inertia ixx="0.05" ixy="0.0" ixz="0.0"
iyy="0.05" iyz="0.0" izz="0.001"/>
</inertial>
</link>

<!-- Left Ankle Joint and Foot -->
<joint name="left_ankle_joint" type="revolute">
<parent link="left_shin"/>
<child link="left_foot"/>
<origin xyz="0 0 -0.4" rpy="0 0 0"/>
<axis xyz="0 1 0"/>
<limit lower="-0.5" upper="0.5" effort="100" velocity="10"/>
</joint>

<link name="left_foot">
<visual>
<origin xyz="0.05 0 0" rpy="0 0 0"/>
<geometry>
<box size="0.2 0.08 0.05"/>
</geometry>
<material name="black">
<color rgba="0.1 0.1 0.1 1.0"/>
</material>
</visual>
<collision>
<origin xyz="0.05 0 0" rpy="0 0 0"/>
<geometry>
<box size="0.2 0.08 0.05"/>
</geometry>
</collision>
<inertial>
<mass value="0.5"/>
<inertia ixx="0.01" ixy="0.0" ixz="0.0"
iyy="0.01" iyz="0.0" izz="0.01"/>
</inertial>
</link>

<!-- RIGHT LEG (mirror of left) -->

<!-- Right Hip Joint and Thigh -->
<joint name="right_hip_joint" type="revolute">
<parent link="base_link"/>
<child link="right_thigh"/>
<origin xyz="0 -0.1 -0.2" rpy="0 0 0"/>
<axis xyz="0 1 0"/>
<limit lower="-1.57" upper="1.57" effort="100" velocity="10"/>
</joint>

<link name="right_thigh">
<visual>
<origin xyz="0 0 -0.2" rpy="0 0 0"/>
<geometry>
<cylinder radius="0.05" length="0.4"/>
</geometry>
<material name="gray"/>
</visual>
<collision>
<origin xyz="0 0 -0.2" rpy="0 0 0"/>
<geometry>
<cylinder radius="0.05" length="0.4"/>
</geometry>
</collision>
<inertial>
<mass value="2.0"/>
<inertia ixx="0.05" ixy="0.0" ixz="0.0"
iyy="0.05" iyz="0.0" izz="0.001"/>
</inertial>
</link>

<!-- Right Knee Joint and Shin -->
<joint name="right_knee_joint" type="revolute">
<parent link="right_thigh"/>
<child link="right_shin"/>
<origin xyz="0 0 -0.4" rpy="0 0 0"/>
<axis xyz="0 1 0"/>
<limit lower="-2.0" upper="0.0" effort="100" velocity="10"/>
</joint>

<link name="right_shin">
<visual>
<origin xyz="0 0 -0.2" rpy="0 0 0"/>
<geometry>
<cylinder radius="0.04" length="0.4"/>
</geometry>
<material name="gray"/>
</visual>
<collision>
<origin xyz="0 0 -0.2" rpy="0 0 0"/>
<geometry>
<cylinder radius="0.04" length="0.4"/>
</geometry>
</collision>
<inertial>
<mass value="1.5"/>
<inertia ixx="0.05" ixy="0.0" ixz="0.0"
iyy="0.05" iyz="0.0" izz="0.001"/>
</inertial>
</link>

<!-- Right Ankle Joint and Foot -->
<joint name="right_ankle_joint" type="revolute">
<parent link="right_shin"/>
<child link="right_foot"/>
<origin xyz="0 0 -0.4" rpy="0 0 0"/>
<axis xyz="0 1 0"/>
<limit lower="-0.5" upper="0.5" effort="100" velocity="10"/>
</joint>

<link name="right_foot">
<visual>
<origin xyz="0.05 0 0" rpy="0 0 0"/>
<geometry>
<box size="0.2 0.08 0.05"/>
</geometry>
<material name="black"/>
</visual>
<collision>
<origin xyz="0.05 0 0" rpy="0 0 0"/>
<geometry>
<box size="0.2 0.08 0.05"/>
</geometry>
</collision>
<inertial>
<mass value="0.5"/>
<inertia ixx="0.01" ixy="0.0" ixz="0.0"
iyy="0.01" iyz="0.0" izz="0.01"/>
</inertial>
</link>

</robot>
Robot Design Notes
  • 6 joints total: 3 per leg (hip, knee, ankle)
  • Revolute joints: All rotate around Y-axis (pitch motion)
  • Realistic limits: Hip ±90°, Knee 0° to -115°, Ankle ±30°
  • Simple geometry: Cylinders for legs, boxes for torso and feet

Step 8: Create a Placeholder Node​

Let's create a simple test node to verify our setup works:

gait_publisher/test_node.py
#!/usr/bin/env python3
"""
Simple test node to verify package setup.
This will be replaced with the actual gait publisher in later steps.
"""

import rclpy
from rclpy.node import Node


class TestNode(Node):
"""Minimal node for testing package setup."""

def __init__(self):
super().__init__('test_node')
self.get_logger().info('Test node started successfully!')
self.get_logger().info('Package setup is working correctly.')

# Create a timer that prints every 2 seconds
self.timer = self.create_timer(2.0, self.timer_callback)
self.counter = 0

def timer_callback(self):
"""Periodic callback to show node is alive."""
self.counter += 1
self.get_logger().info(f'Test node alive: {self.counter} ticks')


def main(args=None):
rclpy.init(args=args)
node = TestNode()

try:
rclpy.spin(node)
except KeyboardInterrupt:
pass
finally:
node.destroy_node()
rclpy.shutdown()


if __name__ == '__main__':
main()

Step 9: Register the Test Node​

Update setup.py to include our test node in the entry points:

setup.py
from setuptools import find_packages, setup
import os
from glob import glob

package_name = 'gait_publisher'

setup(
name=package_name,
version='1.0.0',
packages=find_packages(exclude=['test']),
data_files=[
('share/ament_index/resource_index/packages',
['resource/' + package_name]),
('share/' + package_name, ['package.xml']),
(os.path.join('share', package_name, 'launch'),
glob(os.path.join('launch', '*launch.[pxy][yma]*'))),
(os.path.join('share', package_name, 'config'),
glob(os.path.join('config', '*.yaml'))),
(os.path.join('share', package_name, 'config'),
glob(os.path.join('config', '*.rviz'))),
(os.path.join('share', package_name, 'urdf'),
glob(os.path.join('urdf', '*.urdf'))),
(os.path.join('share', package_name, 'urdf'),
glob(os.path.join('urdf', '*.xacro'))),
],
install_requires=['setuptools'],
zip_safe=True,
maintainer='Your Name',
maintainer_email='your.email@example.com',
description='Walking gait trajectory publisher for humanoid robots',
license='Apache-2.0',
tests_require=['pytest'],
entry_points={
'console_scripts': [
'test_node = gait_publisher.test_node:main',
],
},
)

Step 10: Build and Test​

Now let's build the package and test our setup:

# Navigate to workspace root
cd ~/humanoid_ws

# Build the package
colcon build --packages-select gait_publisher

# Source the workspace
source install/setup.bash

# Run the test node
ros2 run gait_publisher test_node

You should see output like:

[INFO] [test_node]: Test node started successfully!
[INFO] [test_node]: Package setup is working correctly.
[INFO] [test_node]: Test node alive: 1 ticks
[INFO] [test_node]: Test node alive: 2 ticks
...

Press Ctrl+C to stop the node.

Step 11: Verify URDF​

Let's also verify that our URDF is accessible and valid:

# Check that the URDF was installed
ls install/gait_publisher/share/gait_publisher/urdf/

# Verify URDF syntax
check_urdf install/gait_publisher/share/gait_publisher/urdf/simple_humanoid.urdf

You should see:

robot name is: simple_humanoid
---------- Successfully Parsed XML ---------------
root Link: base_link has 2 child(ren)
child(1): left_thigh
child(1): left_shin
child(1): left_foot
child(2): right_thigh
child(1): right_shin
child(1): right_foot

Checkpoint: What We've Built​

Let's review what we've accomplished:

✅ Created a proper ROS 2 package with Python support ✅ Organized directory structure for code, launch files, configs, and URDF ✅ Configured dependencies in package.xml ✅ Set up build system with enhanced setup.py ✅ Created a simple humanoid URDF with 6 leg joints ✅ Built and tested a placeholder node ✅ Verified installation of all package components

Troubleshooting Common Issues​

Issue: "Package not found" after building​

Solution: Make sure you sourced the workspace:

source ~/humanoid_ws/install/setup.bash

Issue: colcon build fails with Python errors​

Solution: Check that __init__.py exists in all package directories:

ls gait_publisher/__init__.py
ls gait_publisher/math/__init__.py

Issue: URDF file not found​

Solution: Verify the glob patterns in setup.py match your file names and rebuild:

colcon build --packages-select gait_publisher --symlink-install

Issue: "No executable found" when running test_node​

Solution: Check entry_points in setup.py and rebuild. Make sure the module path is correct.

Next Steps​

Excellent work! You've set up a solid foundation for your gait publisher. Your package structure is clean, your dependencies are declared, and you have a working URDF model.

In the next section, we'll dive into the mathematics of walking gaits and implement the GaitGenerator class that will compute the joint trajectories needed for walking motion.


← Previous: Chapter 5 Overview | Next: 5.2 Gait Mathematics →

Chapter 5 Sections: