Skip to content

ibraEssam/ros2_ws_template

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

7 Commits
 
 
 
 
 
 
 
 

Repository files navigation

ROS2 Control Workspace

A containerized ROS2 development environment using Dev Containers for consistent, reproducible builds across different systems.

Table of Contents

Overview

This workspace is configured for ROS2 development using Docker Dev Containers. The setup provides:

  • Containerized Environment: Isolated development environment with consistent ROS2 distribution (Jazzy)
  • VS Code Integration: Seamless development experience with VS Code Dev Containers extension
  • GPU Support: NVIDIA GPU acceleration for accelerated applications
  • X11 Forwarding: GUI application support for RViz and other visualization tools
  • DDS Communication: Network and IPC configuration for ROS2 DDS middleware

Prerequisites

System Requirements

Quick Start

1. Clone the Repository

git clone <repository-url>
cd ros2_control_ws

2. Open in VS Code Dev Container

  1. Open VS Code
  2. Open the workspace folder
  3. When prompted, click "Reopen in Container" (or use Command Palette: Dev Containers: Reopen in Container)
  4. VS Code will build and open the Dev Container

3. Build the Workspace

Inside the container terminal:

cd ~/ws
colcon build

4. Source the Installation

source install/setup.bash

Development Setup

Workspace Configuration

The Dev Container is configured via two files:

.devcontainer/devcontainer.json

Configures VS Code, build context, and container runtime parameters:

  • Container Name: ros2_ws_container
  • Base Image: osrf/ros:jazzy-desktop-full
  • Workspace Mount: Local workspace → /home/$USER/ws
  • Extensions: Pre-installed ROS2 development tools
  • Environment Variables: ROS settings, ccache, X11 forwarding

Dockerfile

Builds the container image with:

  • ROS2 Jazzy desktop-full distribution
  • Development dependencies (gdb, python3-pip, etc.)
  • User account matching host system (avoids permission issues)
  • Pre-installed Python packages (bottle, glances)

Key Configuration Features

Feature Purpose
--cap-add=SYS_PTRACE Enable debugging with gdb
--ipc=host Shared memory transport (RViz GUIs)
--network=host DDS discovery and network access
--pid=host Process namespace sharing
--privileged USB device access
--gpus=all GPU acceleration
--runtime=nvidia NVIDIA container runtime
X11 volume mount GUI forwarding to host display

Building and Running

Build Commands

# Full build of entire workspace
colcon build

# Build specific package
colcon build --packages-select <package_name>

# Build with specific mixin (release, debug, ccache, lld)
colcon build --mixin release ccache

# Build without tests
colcon build --cmake-args -DCMAKE_BUILD_TYPE=Release

Source Setup Files

# Source the install space
source install/setup.bash

# Use overlay (if building on top of existing ROS2 installation)
source /opt/ros/jazzy/setup.bash
source install/setup.bash

Run ROS2 Nodes

# List available packages
colcon list

# Run a node
ros2 run <package_name> <node_name>

# View the computational graph
ros2 graph

Launch Files

# Run a launch file
ros2 launch <package_name> <launch_file>.py

# With arguments
ros2 launch <package_name> <launch_file>.py argument:=value

Project Structure

ros2_ws_template/
├── .devcontainer/
│   └── devcontainer.json     # VS Code Dev Container configuration
├── src/                       # ROS2 source code
│   └── <packages>            # Individual ROS2 packages
├── build/                     # Colcon build output (generated)
├── install/                   # Installation directory (generated)
├── log/                       # Colcon log output (generated)
├── Dockerfile                # Container image definition
└── README.md                 # This file

Adding Packages

Create new packages in the src/ directory:

# Create a new Python package
ros2 pkg create --build-type ament_python <package_name>

# Create a new C++ package
ros2 pkg create --build-type ament_cmake <package_name>

Common Commands

Development Workflow

# One-time setup (inside container)
cd ~/ws && source /opt/ros/jazzy/setup.bash

# Build everything
colcon build --symlink-install

# Build and test
colcon test

# View test results
colcon test-result --verbose

# Clean build artifacts
colcon clean build --remove-on-error

# Full clean
colcon clean workspace

ROS2 Tools

# List packages
ros2 pkg list

# View package info
ros2 pkg prefix <package_name>

# Topic introspection
ros2 topic list
ros2 topic echo <topic_name>
ros2 topic info <topic_name>

# Service discovery
ros2 service list
ros2 service call <service_name> <service_type> "{<args>}"

# Node information
ros2 node list
ros2 node info <node_name>

# Check dependencies
ros2 dependency graph <package_name>

System Introspection

# View system architecture
ros2 graph

# Record bag file
ros2 bag record <topic_names>

# Play bag file
ros2 bag play <bag_file>

# Check ROS2 environment
ros2 doctor

GUI Applications

RViz

Launch the ROS2 visualization tool:

rviz2

Requirements:

  • X11 display forwarding configured in devcontainer.json
  • Host X11 socket mounted in container
  • DISPLAY environment variable set

Other GUI Tools

Works with any X11-based application:

  • Gazebo simulation
  • rqt tools
  • Custom visualization applications

Note: GUI forwarding works best on Linux. macOS users may need XQuartz, and Windows users should use WSL2 with X11 support.

GPU Support

The Dev Container is configured for NVIDIA GPU acceleration using the NVIDIA Container Runtime.

Verify GPU Access

# Inside the container
nvidia-smi

Requirements

  • NVIDIA GPU with CUDA compute capability 3.5+
  • NVIDIA Container Toolkit installed on host
  • Docker daemon configured with NVIDIA runtime

Disable GPU (if needed)

Remove "--gpus=all" and "--runtime=nvidia" from devcontainer.json and rebuild.

Troubleshooting

Container Won't Start

  1. Verify Docker is running:

    docker ps
  2. Check Docker permissions:

    sudo usermod -aG docker $USER
  3. Rebuild the container:

    • Command Palette: Dev Containers: Rebuild Container

GUI Not Working

  1. Check DISPLAY variable:

    echo $DISPLAY
  2. Verify X11 socket access:

    ls -la /tmp/.X11-unix/
  3. On Linux, allow X11 access:

    xhost +local:

Build Failures

  1. Update packages:

    sudo apt-get update && sudo apt-get upgrade
  2. Missing dependencies:

    rosdep update
    rosdep install --from-paths src --ignore-src -y
  3. Rebuild from scratch:

    colcon clean workspace
    colcon build

Permission Errors

The container creates a user matching your host system UID/GID to avoid permission issues. If problems persist:

# Check user ID
id

# File permissions inside container
ls -la ~/ws

DDS Communication Issues

The container uses --network=host and --ipc=host for DDS middleware. Verify with:

ros2 node list
ros2 topic list

C++ Development Tools

Using Bear and Clangd for Enhanced C++ Support

For improved code intelligence and navigation in C++ ROS2 packages, you can use Bear to generate a compile_commands.json file and the Clangd extension for VS Code. Both tools are pre-installed in the Dev Container.

Generating compile_commands.json with Bear

Bear is pre-installed and generates compile_commands.json by intercepting compiler invocations. To create the compilation database:

# Clean previous build
colcon clean workspace

# Build with Bear to generate compile_commands.json
bear -- colcon build

This will create a compile_commands.json file in the workspace root, which contains information about how each source file is compiled.

Using Clangd Extension

The Clangd extension (by LLVM) is pre-installed in the Dev Container and provides C++ language server support in VS Code. It will automatically detect the compile_commands.json file and provide features like:

  • Code completion
  • Go to definition
  • Find references
  • Hover information
  • Error diagnostics

If the extension doesn't automatically find the file, you can configure the path in VS Code settings:

  1. Open Settings (Ctrl+,)
  2. Search for "clangd"
  3. Set "Clangd: Arguments" to include --compile-commands-dir=/path/to/workspace

Replace /path/to/workspace with your actual workspace path (e.g., /home/$USER/ws).

For more information, refer to the Clangd documentation.

Resources

Official ROS2 Documentation

Dev Containers

Related Tools

About

A containerized ROS2 development environment using Dev Containers for consistent, reproducible builds across different systems.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors