ROS Project

This section will cover the development of a ROS project. It will explain the steps involved in creating the projects. We will begin by creating a ROS workspace.

Setup Tools

To ensure compatibility with ROS2 Python packages and eliminate warnings, it is recommended to use setup tools version 58.2.0, the last version known to work seamlessly. If your current version of setup tools is higher than 58.2.0, you will need to downgrade it. Execute the following command to perform the downgrade:

pip3 install setuptools==58.2.0

For more information about this solution, you can refer to this link.

ROS Workspace

If you have created the workspace, you can skip the steps below. Otherwise, follow the steps below to create a ROS workspace.

# Create a workspace directory
mkdir -p ~/ros2_ws/src
# Change directory to the workspace
cd ~/ros2_ws

Build the workspace.

# Build the workspace
colcon build --symlink-install

After building the workspace, you will be able to see the folders shown in the image below.

Workspace Directory

Once succesfully build, there will be 4 workspace directories:

  • build
  • install
  • log
  • src

The “src” directory, which we created earlier, is where you will create your ROS packages. The “build” directory is where the build files will be stored. The “install” directory is where the installed files will be stored. Lastly, the “log” directory is where the log files will be stored.

ROS Package

Let’s create a ROS package called “turtle_controller”. Make sure that you are in the source folder when running the command below. To create the package, run the following command:

# Create ros2 package turtle_controller with ament_python build type and node name turtle_driver
ros2 pkg create --build-type ament_python --node-name turtle_driver turtle_controller

The command above will create package name “turtle_controller” with a node named “turtle_driver”. Go to the folder and open it with vscode using the command below.

# Change directory to the package
cd ~/ros2_ws/src/turtle_controller
# Open the package with vscode
code .

The files and folder structure of the package is shown in the image below.

Workspace Directory

In the package.xml and setup.py files, you will find certain information that needs to be updated. Look for the sections marked with TODO in both files and make the necessary changes to those sections.

TODO changes

Once you have made the necessary changes, you can build the package by using the following command:

# Change directory to the workspace
cd ~/ros2_ws
# Build the workspace
colcon build --symlink-install

After building the package, you need to source the workspace. To do this, execute the following command:

# Source the workspace
source ~/ros2_ws/install/setup.bash

After sourcing the workspace, you can run the node using the following command:

# Run the node
ros2 run turtle_controller turtle_driver

You will see the output shown in the image below.

TODO changes


Previous | Next


Last Update: August 2023.
Copyright © 2023 Danny Ng. Distributed by an MIT license.