What is a ros2 node?
A node is a subpart of your application that you will code, for example, with Python or C plus plus.
A node should have a single purpose.
Your application will contain many nodes which will be put into packages, and nodes will then communicate
with each other.
But let's just see with a real life example, I will show you a simplified example so you can easily
understand what a node is and why it is useful.
So let's go back to the previous slide where I explained to you what packages were.
A package is an independent unit inside your application.
Now a package which is empty will not do anything.
So we will create nodes inside the packages.
Let's start with the camera package.
This 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.
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 that we are using all those programs in blue
are nodes.
Each node can be launched separately.
So first we will launch the driver and then the image processing node.
Now the nodes will communicate between each other using Ros to communication functionalities which we
will see later in this course.
All right.
We have our camera package filled with all the nodes that we need.
Note that sometimes it can be quite hard to know if you should put two 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 then add many other processing nodes, and this new package would communicate with any other
camera package, which includes a driver.
But for this example, let's just say that the image processing is specific to the camera we are talking
about, and both driver and image processing are using some common dependencies.
We have two other packages in our application.
Let's check the motion planning package.
So in this package you can expect to have a motion planning node, which will compute motion planning
for a robot.
We can also add a path correction node whose role is to modify the motion planning due to external factors.
So great!
We have two packages filled with nodes.
What we can do now is to make two nodes inside different packages communicate together.
We 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 node will then be able to notify the motion planning node.
And we finished with our third package which is the hardware control.
This package has an independent unit will control the hardware of the robot that can be wheels, robotic
arm joints, or anything else.
In this package, we will find some drivers to control the motors with a control loop.
And let's say that the position feedback, which is coming back from the motor encoders, is also sent
back to the control loop for precise control and published by a state publisher node.
So we have two nodes in this package, and now the motion planning node from the motion planning package
will send computed trajectories to the driver inside the hardware control package to complete the architecture.
Let's say that the hardware status of the robot is published, and both the motion planning and path
correction nodes are getting those messages.
Well, that's maybe quite complicated for you to get everything right now, but I think you have a better
idea of what a node is and how they are used.
Feel free to come back to this lesson after you make some progress in this course.
You will understand more things.
And so you've seen the big picture with a real life example.
Now let's go back to the node definition.
A node is a subprogram of your application which is responsible for one thing.
Just as when you write a class with object oriented programming, you know that the class only serves
a single purpose.
Well, if you have two different functionalities to implement, you will have two different classes
that's similar for nodes.
Nodes are combined into a graph and communicate between each other using for example, topics, services,
parameters, etc. we will cover the communication tools in the next sections.
Now what are the characteristics and benefits of nodes?
First, they help you 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.
Believe me on that.
If you write everything into one big block of code, then after some time you will spend more time fixing
your code than actually developing new functionalities.
Nodes also provide great fault tolerance.
If you run your nodes in different processes, then they are not directly linked and they can still
continue to communicate thanks to Ross two communications.
If one node crashes, it will not make the other nodes crash first.
That's great for debugging.
And second, that's great if you have, let's say, a critical node running your hardware that is well
tested and you just add another node in your program that you want to test.
Even if this later node can crash, it will not affect the critical hardware node.
And then Ros2 is language agnostic.
It means that you can write one node in Python, another node in Cplusplus, and both nodes can communicate
without any problem.
Python and Cplusplus are the two most common languages for Ros, and with some other libraries you can
also use other languages.
This characteristic is really great.
For example, you can choose to develop most of your application in Python, while some nodes will be
written in Cplusplus because they need fast execution speed.
And just a quick note, and we're going to come back to this later.
Two nodes cannot have the same name.
If you want to run multiple instances of the same node, you will have to change their name or add them
into a different namespace for example.
All right.
So in the next lessons we will see how to actually create nodes in both Python and C plus.
Plus how to use them with command line tools and then how to make them communicate between each other.