ROS 2 Basics

In this lesson, we will learn about the basic concepts and usage of ROS 2.

Learning Objectives

By the end of this lesson, you will be able to:

  • Understand what ROS 2 is and why it is used in robotics
  • Explain the basic concepts of ROS 2 (nodes, topics, messages, etc.)
  • Use basic ROS 2 commands and tools
  • Understand the structure of ROS 2 messages

1. Introduction

To operate a robot, various programs are needed. For example, programs that receive information from sensors, programs that control motors, programs that calculate the robot’s position, etc. ROS 2 is a mechanism for efficiently developing and executing these programs.

2. What is ROS 2

ROS 2 (Robot Operating System 2) is a software framework for robot development. A framework is a mechanism that makes programming easier for specific purposes. ROS 2 provides standardized ways to handle common robotics tasks, allowing developers to focus on implementing their specific robot behaviors rather than reinventing basic communication and control mechanisms.

Main Features of ROS 2

  1. Distributed System: Multiple programs can run simultaneously and communicate with each other.
  2. Language Independent: You can write programs in various programming languages such as Python, C++, and Java.
  3. Open Source: It’s free to use, and developers around the world collaborate to improve it.
  4. Rich Libraries: Various functions necessary for robot development are already implemented.

3. Basic Concepts of ROS 2

Node

A node is the basic execution unit that constitutes a ROS 2 system. One program runs as one node. Each node typically performs a specific function, and multiple nodes work together to create a complete robot system.

Key Points:

  • Each node has a unique name
  • Nodes can run on the same computer or different computers
  • Nodes communicate with each other through topics, services, or actions

Examples:

  • A node that receives images from a camera
  • A node that controls motors
  • A node that calculates the robot’s position

Topic

A topic is a communication channel for nodes to exchange information. A specific type of data is sent to one topic. Topics use a publish-subscribe pattern, where one or more nodes can publish data to a topic, and one or more nodes can subscribe to receive that data.

Key Points:

  • Topics are named with forward slashes (e.g., /camera/image)
  • Multiple nodes can publish to or subscribe from the same topic
  • Topics are asynchronous - publishers and subscribers don’t need to know about each other

Examples:

  • /camera/image - Image data from a camera
  • /cmd_vel - Robot velocity commands
  • /odom - Robot position information (odometry)

Publisher and Subscriber

  • Publisher: A node that sends data to a topic. It creates a publisher object and uses it to send messages.
  • Subscriber: A node that receives data from a topic. It creates a subscriber object and provides a callback function that is executed when new data arrives.

Examples:

  • A camera node publishes image data to the /camera/image topic
  • A navigation node subscribes to image data from the /camera/image topic to process the images for navigation

Message

A message defines the format of data exchanged through topics. ROS 2 provides various types of messages.

Examples:

  • std_msgs/String - String data
  • sensor_msgs/Image - Image data
  • geometry_msgs/Twist - Velocity and angular velocity data

Service and Action

  • Service: A mechanism for synchronous communication in request and response format. When a client sends a request, it waits for a response from the server. Services are useful for one-time operations like getting sensor status or setting parameters.

  • Action: A mechanism for managing long-running tasks with feedback. Actions are similar to services but allow for cancellation and provide periodic feedback during execution. They are useful for tasks like navigation to a goal, where progress updates are needed.

4. Main Tools of ROS 2

Build System

In ROS 2, programs are built using a tool called colcon (collective construction). Building is the process of converting program source code into executable files. The build system compiles your code, resolves dependencies, and installs the packages so they can be run.

# Build the workspace
colcon build

# Build only a specific package
colcon build --packages-select package_name

# Build with symbolic links (convenient for development)
colcon build --symlink-install

The --symlink-install option is a convenient feature, especially when developing Python packages. When using this option:

  • After modifying source code, changes are reflected immediately without rebuilding

Environment Setup After Building

# Set up the environment for built packages
source install/setup.bash

Visualization Tools

  • RViz2: A tool for visualizing robot state and sensor data in 3D
  • rqt: A GUI framework that provides various ROS 2 tools

5. ROS 2 Message Example

Below is an example of the geometry_msgs/msg/PoseStamped message that represents the position and orientation of a robot.

std_msgs/Header header
  uint32 seq          # Sequence number
  time stamp          # Timestamp
  string frame_id     # Coordinate frame name

geometry_msgs/Pose pose
  geometry_msgs/Point position
    float64 x         # X coordinate (meters)
    float64 y         # Y coordinate (meters)
    float64 z         # Z coordinate (meters)
  geometry_msgs/Quaternion orientation
    float64 x         # Quaternion X component
    float64 y         # Quaternion Y component
    float64 z         # Quaternion Z component
    float64 w         # Quaternion W component

Exercises

Start Kachaka

Start Kachaka

Basic ROS 2 Commands

# Display running nodes
ros2 node list

# Display available topics
ros2 topic list

# Display the relationship between nodes and topics
rqt_graph

Remote Control with Keyboard

ros2 run teleop_twist_keyboard teleop_twist_keyboard