Creating a Simple Node in ROS 2

Last update: 05-13-2024

This blog post will guide you through the process of creating a simple node in ROS 2 using Python. We will cover setting up your workspace, writing a basic node, and running it. Let’s dive in!

1. Setting Up the Workspace

First, you need to create a new ROS 2 workspace and a 'src' folder inside it by running the following command:

mkdir -p ~/ros2_ws/src

2. Building the Workspace

To prepare our environment, we'll build the workspace—even though it's initially empty—to generate the necessary installation files. This ensures that when we source these files in the next step, they already exist. Navigate to the workspace folder and build it:

cd ~/ros2_ws
colcon build

After building, List the files and folders in your workspace:

ls

You should see new directories: 'build', 'install', and 'log', alongside 'src'.

3. Sourcing the Workspace

To make the packages and nodes visible, you need to source them in your environment. Edit your .bashrc file:

nano ~/.bashrc

And append the following line to the end of the file:

source ~/ros2_ws/install/setup.bash

4. Creating a New Package

Create a new package named 'example_controller' in the src folder. Notice that for Python, we specify the ament_python build type and require the rclpy dependency:

cd ~/ros2_ws/src
ros2 pkg create example_controller --build-type ament_python --dependencies rclpy

5. Using Visual Studio Code

Open your 'src' folder in Visual Studio Code to start working on your package:

code ~/ros2_ws/src

While optional, installing the ROS extension for VSCode, developed by Microsoft, can significantly enhance your development experience.

6. Installing the ROS Extension for VSCode

While optional, installing VSCode's ROS extension can enhance your development experience.

  1. Open the Extensions view by clicking on the square icon on the sidebar or pressing Ctrl+Shift+X.
  2. Type "ros" into the search bar. The ROS extension by Microsoft should appear as the top result.
  3. Click on the install button next to the extension to add it to VSCode.

7. Creating a Node File

Notice that your package directory contains a subfolder with the same name (e.g., example_controller/example_controller). We will create the Python files for our nodes in this subfolder.

Create a new python file using VSCode's GUI, or use the terminal:

cd ~/ros2_ws/src/
touch example_controller/example_controller/example_node.py

8. Writing Node Code

Here’s a simple code snippet for a minimal ROS 2 node:

import rclpy
from rclpy.node import Node

class ExampleNode(Node):
def __init__(self):
    super().__init__("first_node")
    self.get_logger().info("Hello from ROS2")

def main(args=None):
rclpy.init(args=args)
node = ExampleNode()
rclpy.spin(node)
rclpy.shutdown()

if __name__ == '__main__':
main()

Explanation:

9. Configuring the Node Entry Point

In the 'setup.py' file of your package, add an entry point for your node to make it executable from a ROS 2 environment:

entry_points = {
'console_scripts': [
    'ex_node = example_controller.example_node:main'
]
}

'ex_node' is the command you'll use to run your node.

10. Building the Project

Rebuild your project to include all recent changes:

cd ~/ros2_ws
colcon build

After building the project, source your .bashrc file for the new package to be added:

source ~/.bashrc

11. Running the Node

Finally, run your node with:

ros2 run example_controller ex_node

If everything is set up correctly, you should see the log message "Hello from ROS2" indicating that your node is running (The node won't stop on its own; kill it with ctrl+C).

Congratulations on creating and running your first simple ROS 2 node!

0 Comments

Add a new comment: