As you start to learn ROS, you’ve quickly discovered that everything in ROS is made with components called nodes. So, what is a ROS node ?
In this post I’ll explain you the concept and the “why” behind ROS nodes. It’s important to know why you need them before you actually use them for real. And instead of using the classic approach (code, code and code), I’ll use a real life example.
For starters, you can just see a node as a sub-part of your robotics application. Your application will contain many nodes, which will be put into packages. Nodes will then communicate with each other.
This post is a part of the What is ROS? series.
>> As an additional resource to this article I’ve also made a video explanation, with a slightly different approach. To get the most benefits, read the article and also watch the video.
You are learning ROS?
Check out ROS For Beginners and learn ROS step by step.
After watching the video, subscribe to the Robotics Back-End Youtube channel so you don’t miss the next tutorials!
A mobile robot controlled by a camera
Let’s start with a standard robotics application which involves a mobile robot and a camera. The robot has 3 ROS packages (from low to high level):
- Hardware control package: directly controls the hardware (wheels and other actuators)
- Motion planning package: monitors and controls the robot trajectory
- Camera package: processes images and give useful info and commands to the robot
Those packages are the 3 main parts of your application. As you can see they’re all empty. Let’s now fill in those packages with useful nodes which will be responsible for the execution of the program.
Nodes for the camera package
The camera package will handle a camera as an independent unit.
So, what should we put inside?
First, we need a driver for the camera, to be able to program it, and get frames from it.
Then we also need a program that will take those frames and do some image processing work.
We could also add any other program related to the camera we are using.
All those programs in blue are nodes. Each node is launched separately. First you will launch the driver, and then the image processing node. The nodes will then communicate using ROS communication functionalities, for example topics, services and actions.
All right, we have our camera package filled with all the nodes we need.
Note that sometimes, it can be quite hard to know if you should put 2 nodes in the same package. For example, the image processing node could be part of another package which only handles image processing for any camera. We could add many other processing nodes, and this package would communicate with any other camera package which include drivers. But for this example, let’s just say that the image processing is specific to the camera we’re talking about, and both driver and image processing are using some common dependencies.
Nodes for the motion planning package
In this package you can expect to have a motion planning node, which will compute motion planning for any given robot. We can also add a path correction node, which role is to modify the motion planning due to external factors.
Great! We have 2 packages filled with nodes.
What we can do now, is to make 2 nodes inside different packages communicate together.
Let’s link the image processing node to the path correction node. The image processing node will analyze frames coming from the camera and will send an analysis of the environment to the path correction node. This ROS node will then be able to notify the motion planning node.
Nodes for the hardware control package
And we finish with our third package, which is the hardware control. This package, as an independent unit, will control the hardware of the robot. That can be wheels, a robotic arm joints, or anything else.
In this package we’ll find some drivers to control the motors. The drivers are controlled from the main control loop node. And let’s say that the position data coming from the motor encoders is sent back to the control loop for more precise control. This data is also published by a state publisher node.
The motion planning node from the motion planning package will send computed trajectories to the main control loop node, inside the hardware control package.
The hardware state of the robot is published, and both the motion planning and path correction nodes are receiving it. Thus, the motion planning can be dynamically changed thanks to the hardware or camera data.
Note that this architecture is really similar to what you can actually see in real life robots.
Wrapping things up – What is a node ?
Let’s go back to the ROS node definition.
A ROS node, according to ROS wiki, is basically a process that performs computation. It is an executable program running inside your application. You will write many nodes and put them into packages.
Nodes are combined into a graph and communicate with each other using ROS topics, services, actions, etc.
Note that 2 nodes can’t have the same name. If you want to run multiple instances of the same node, you’ll have to add a prefix or suffix to the name, or declare them as anonymous.
What are the main benefits of nodes ? Why should you use nodes instead of just writing all your code in the same place ?
Reduce code complexity
If you correctly separate your application into packages and nodes, then it will be much easier for you to scale your application.
Trust me, if you write everything in one block, you’ll soon spend more time fixing your code than actually developing new functionalities.
Better fault tolerance
Nodes also provide a great fault tolerance. As nodes only communicate through ROS, they are not directly linked. If one node crashes, it will not make the other nodes crash.
First, that’s great for debugging. You can decide to run and test only a sub-part or your entire application, which will speed up your development time.
Secondly, that’s great if you have, let’s say, a critical node running your hardware that is well tested, and you just added another new node in your program. Even if this later node is subject to crash, it will not affect the critical hardware node.
ROS is language agnostic. It means that you can write one node in Python, another node in C++, and both can communicate without any problem. Python and C++ are the 2 most common languages for ROS. Through some libraries you can use other languages to create new nodes.
This is really great. For example you can choose to develop your application in Python, while some nodes will be written in C++ because they need fast execution speed.
Final note: As you’ll put most of your code into ROS nodes, mastering them is a quite important task when you begin with ROS.