This is the solution for the activity number two.
In this activity, you have to create some kind of a data pipeline between multiple nodes using topics.
I will only provide a video solution for Python here, and you can also download the complete solution
for Cplusplus.
At the end of this section.
The Cplusplus solution will be exactly the same just using the Cplusplus syntax and Clcp instead of
CLP, and I have divided the solution into two steps.
In this first step in this video, we are going to create the number publisher node, and then in the
next video, in the second step we will do the number counter node.
And the reason why I'm doing this is because we are first going to create the publisher, okay.
We are going to publish a number.
And then we can validate that this publisher is correctly working using the Ros two command line tool.
Then when we know that the publisher is correctly working, we can create the subscriber side.
So let's create a new node which will contain the publisher.
Let's go to our workspace in.
So we're going to continue using the My Python package that we've created before we go to the folder
which has the same name as the package.
And here we already have our nodes, I'm going to add a new one named number publisher Dot Pi.
And let's make it executable okay.
If we want to use the Simulink install.
So chmod plus x.
And you see now it's in green right here I will open VSCode.
So for example here let's go back to the source folder of the workspace.
And let's do code dot.
Okay I'm going to clean that a bit.
We don't need all those files.
And let's go to our number publisher Dot Pi I will start as we did before.
I'm just going to take the template and let's put it there.
Okay.
So we have the template for the node.
We want to create a number publisher node okay which is going to be the class name.
And we need to provide the name here as well.
And the name of the node is going to be number publisher.
And let's remove also those comments here.
Great.
So now we are going to create a publisher inside the constructor.
And this publisher is going to publish on a number topic.
And it's going to publish a number.
And actually I gave you the type that we're going to use for this topic.
And this is the.
So I'm going to type it here.
So Ross to interface you can do Ross to interface show.
And this is example interfaces.
So let's use Autocompletion here and then MSG and then slash int 64.
Okay so we've already seen a string we have here int 64.
Which means that 64 bit integer.
Let's see what we have in this interface because we're going to need to use it.
And well this is very similar to what we had before.
So there is a you see we have a few comments here.
So you can just ignore that.
And we basically have one field which name is data okay.
And this field here is an int 64.
So basically just an integer.
All right.
So now that we know this let's include that interface inside our node.
So right here from example interfaces dot msg import.
And you can see we find the int.
So we have int 1632 64 eight okay we have different ones.
We're just going to use in 64 here.
And because we use example interfaces we just make sure that in our package dot XML we have the dependency.
But that's already the case because we've used this package already before for the other nodes in this
package.
So let's go back and let's create a publisher.
I'm going to create the publisher in the constructor.
So self dot.
So let's name it number publisher okay.
Let's try to give explicit names.
And we're going to use self dot create publisher.
What's the first thing we need to give.
You see the message type.
So that's going to be int 64.
That's what we've just imported here.
Then we need to give a name.
So what name here I gave you.
The name before is going to be number topic okay.
The topic doesn't exist yet.
There is no subscriber or publisher to this topic.
So here we create the first publisher.
We decide of the name and then we also have a queue size.
I'm just going to put ten.
Then after I create a publisher I'm also going to create a function to publish.
So that's best practice.
To do that we create a function that's just going to publish a message on that topic and let's name
it Publish number with self.
Because we are in a class and what do we do?
We create a message.
So msg is equal to int 64.
And in this message if you remember we had.
So in the terminal we saw that we have a data field of type int 64.
So let's go back and we can do message dot data.
Usually we find the data here is equal to and well let's give a number.
Actually I'm going to create a self number here is equal to two for example.
And let's do message data is equal to self number.
So we're going to publish the same number every time okay.
Let's just for this example.
And then well we have the message.
What do we do with this message.
We are going to publish it with the number publisher.
So self number publisher dot Publish.
Publish what?
The message.
As simple as that.
So you see, the structure is always the same.
And then.
Well, how do we call that?
So we want to call it, let's say every one second.
So I don't know if I gave you a frequency to call this function to publish a message, but let's say
every one second.
How do we do this.
We are going to create a timer.
So self dot let's call it number timer is equal to self create timer.
We provide a timer period in Saigon.
So let's say 1.0 and then the reference to the function.
So that's going to be the callback self dot publish number without any parentheses.
Once again.
And that's basically it I'm going to add another log self getlogger just to say so info just to say
that number publisher has been studied.
And that's the end of the code.
So when we create the node here we're going to go to the constructor.
We're going to create a publisher.
We're going to create a timer.
And then when we make the node spin well with the timer we will have one callback every one second.
So this function is going to be called every one second.
And we're just going to publish every one second.
So let's make sure we save the file.
And then we are going to create an executable.
So let's go to setup.py.
Let's add a comma here new line.
And then well let's name it number.
Publisher is equal to.
We have the mypy pkg.
And then dot what's the file name.
The file name is number publisher number publisher okay.
We don't put the extension.
And then what function we want to call.
Recall what is always the same main.
So I put main here.
I'm going to save this.
Okay.
Very important.
You save all the files.
Let's go back to the terminal and let's go back here to this folder Ros2 workspace I'm going to do call
code build packages.
Select with the Python package and let's use also symlink install.
Let's press enter.
Let's build this package.
So it's going to install the Python executables.
And actually with symlink installed we can directly run the code that we have written so that if we
modify it we can just run it again.
And we don't need to do a build again.
So let's try that.
I'm going to try that in this terminal.
And first because we have actually built something.
So even if we use all of those options we have built.
So I need to do a source bash I see, and I'm just going to source all the terminal so you can open
new ones or just source the existing one.
Okay.
And here let's do so.
Let's clear and let's do Ros to run my Pi PG.
And that's going to be number publisher.
You see you should find it with the Autocompletion.
Let's press enter.
And we see number publisher has been started here.
Let's do a Ros to node list.
We can see the number publisher.
Let's do a Ros two topic list.
We can see the number topic okay.
Plus the two other topics we always have.
And we can do Ros two topic equal with number.
And what do we have.
You see we have data two every one second.
So it is correctly working.
And we have finished the first step of this activity with this number publisher.