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
- Remember to adjust the path if your workspace directory is named differently.
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.
- Open the Extensions view by clicking on the square icon on the sidebar or pressing Ctrl+Shift+X.
- Type "ros" into the search bar. The ROS extension by Microsoft should appear as the top result.
- 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:
- We start by importing
rclpy
, the ROS 2 client library for Python, andNode
, a class that provides provides the framework for ROS 2 nodes. - We define the class
ExampleNode
. It inherits fromNode
and represents a ROS 2 node. - In the constructor, we call the parent class's constructor with the string "first_node". This sets up the name for the node.
self.get_logger().info("Hello from ROS2")
uses the node’s logging facility to print a message to the console.- In the main function,
rclpy.init(args=args)
initializes the ROS 2 Python client library (rclpy). It can take arguments passed from the command line but defaults to None if none are provided. - We then create an instance of the
ExampleNode
class and usespin
to start it and keep it alive.
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!