Workspace and Package Building

In the previous sections, we have installed the openEuler system and the complete version of ROS 2 Humble.

This section describes how to create your own workspace to build code and test the communication between topics and services between nodes.

Creating a Workspace

A ROS workspace is a directory with a specific structure, which is used to store and manage all the materials involved in the development of robot code. A workspace usually contains a src subdirectory, which stores the source code of ROS software packages. The source code can be the open-source code that you have downloaded or the code that you have developed.

Open the terminal and run the following commands to create a workspace named "ros2_ws" and its src subdirectory:

bash
mkdir -p ~/ros2_ws/src

At this time, the src directory is empty. Download the code from the ros_arm_tutorials repository to the src directory as the sample source code for subsequent node communication tests.

bash
cd ~/ros2_ws/src
git clone -b ros2-humble https://gitee.com/xiao_yun_wang/ros_arm_tutorials.git

After the download is complete, you can install the tree tool to view the directory structure in the workspace.

bash
 dnf install tree
 tree -L 3

1

The code of the ros2-humble branch of ros_arm_tutorials contains the following five packages:

Package (Software Package)Content
base_demoCustom messages and services, topic publishing/subscription node, service server/client node, and parameter operation example node
advance_demoAction definition and server/client node, common ROS tools, dynamic parameter configuration node, TF2 example node, and RVIZ marker publishing and display
myrobot_descriptionURDF model of the three-degree-of-freedom robotic arm and mobile robot
xarm_descriptionURDF model file package of the XBot-Arm
xarm_moveit_configMoveIt! configuration and launch package for the XBot-Arm, generated using the setup assistant

For more information, see the original repository.

Building Packages

ROS 2 uses the colcon build tool to build source code. For more information, see the design document.

By default, colcon creates the following directories as the same level as the src directory:

  • build: stores intermediate files.

  • install: installation directory of each software package. By default, each software package is installed in a separate subdirectory.

  • log: contains various log information about each colcon invocation.

Since build types like ament_cmake do not support the concept of a development space, and the software package needs to be installed, colcon supports the --symlink-install option. This allows you to change the installed files by changing the files in the source space (such as Python files or other non-compiled resources), thus speeding up iteration.

Install colcon.

bash
pip3 install -U pytest colcon-common-extensions

In the root directory of the workspace, use the colcon build function to build the package.

bash
cd ~/ros2_ws/
colcon build --symlink-install

During the build process, some warnings may be displayed due to reasons such as inconsistent CMake versions. You can ignore them.

1

After the build is complete, a message is displayed, indicating that five software packages have been built.

1

Setting the Environment

Run the source ~/ros2_ws/install/setup.bash command to set the environment, or run the following command to add the environment to the.bashrc file:

bash
echo "source ~/ros2_ws/install/setup.bash" >> ~/.bashrc
source ~/.bashrc

Testing the Topic Communication Node

Based on the distributed design, ROS 2 divides a complex system into many modular nodes (processes) that implement different functions. Data is transmitted between different nodes through messages. Topic communication is an asynchronous communication mechanism based on the publish-subscribe pattern. The base_demo package provides the example code node for topic communication. Each node provides the implementation of Python and C++ programming modes.

Open a terminal and run the following command to start the Python or C++ node of the topic publisher:

bash
ros2 run base_demo topic_pub.py

or

bash
ros2 run base_demo topic_pub

Open a new terminal and run the ros2 topic list command to view the current topic list. In the list, /robot_info is the topic published by the node.

Run the ros2 topic echo /robot_info command to view the messages received by the topic.

1

Open a new terminal and run the following command to start the Python node of the topic subscriber:

bash
ros2 run base_demo topic_sub.py

Alternatively, start the C++ node:

bash
ros2 run base_demo topic_sub

After the node is started, it receives topic messages, processes the message data, and publishes the data to the terminal for display.

1

Testing the Service Communication Node

Service communication is a synchronous communication mode based on the request/response model. The base_demo function package provides the sample code node for service communication. Each node provides the implementation of Python and C++ programming modes.

Open a terminal and run the following command to start the Python node of the service server:

bash
ros2 run base_demo service_server.py

Alternatively, start the C++ node:

bash
ros2 run base_demo service_server

Open a new terminal and run the following command to start the Python node of the service client:

bash
ros2 run base_demo service_client.py

Alternatively, start the C++ node:

bash
ros2 run base_demo service_client

After the client node is started, it sends a request to the user-defined service in the code. After receiving the request, the server processes the request and displays the message "Target object is box" and "find box and response" on the terminal, and returns the response.

After receiving the response, the client node displays the corresponding information on the terminal.

1

Note: This section describes how to create a workspace and build a code package. For details about how to compile the source code, see the following reference.

ros_arm_tutorials

You can refer to this tutorial to compile and build your own ROS programs on openEuler.

References