In this lesson you will write a C plus plus publisher.
We will do the same thing as we did for the Python publisher, but just this time in C plus plus.
So you will be able to compare the code for the same functionalities in the two languages.
Let's go to our workspace again in the source repository.
And then we have our two packages.
I'm going to go to the C plus plus one this time.
And I'm going to go to the source folder of the C plus plus package where we already have.
You can see the first node and also the template for the node.
Let's create a new file named Robot News Station dot CP, which is going to be the node where we have
the publisher and nothing else to do here.
I'm just going to open VSCode and here, so I'm going to close my Python package.
I don't need that anymore.
And let's go to the C plus plus package inside my so Robot news station dot CP I'm going to use the
template.
So I'm just going to take the code from the template and put it there.
Okay.
And as you can see you might have an error just because you need to save the file.
So I'm saving the file.
And after a few seconds you see.
So I had to wait about five 10s.
Then the include is working.
Okay.
So don't worry too much if you have an error.
First save the file first and then let's rename that.
So it's going to be Robot News Station node.
Okay.
So I'm just going to use the same names as I did for Python.
And we also need to modify it here and there.
Okay.
So class name is going to be in three places.
And the node name is going to be here Robot News Station I'm going to use the same.
Let's remove those comments.
And now the node is set up.
So we have to include Lclc-rp.
We create the node.
We have the constructor where we initialize the node.
And then in the main function we start Rosta communications.
We create a shared pointer of the object for robot new station node.
And then we make the node spin and we shut down.
So let's create our publisher.
And in Cplusplus, as you might already know, the syntax is going to be a bit more complicated than
in Python.
There's a bit more stuff to write, but that's just how it is.
So the first thing actually I'm going to do is not to create the publisher, but is to import.
So to include the interface, okay.
And the interface, that's the same one we've used with Python.
So I'm going to use include and I'm going to use the string.
So if I go back here I can do Ros to interface show example interfaces MSG and string okay.
That's the interface I'm going to use, and I can use this in Python.
I can also use this in C plus plus.
And you see this string message has a data field of type string.
So how to include this in C plus plus you will start with example interfaces slash.
And you can see we also have the Autocompletion MSG slash.
And that's going to be string.
So here the string is lowercase okay.
Quite important dot h p p.
We're going to try to import HTTP files.
If you have an error like this you just save the file.
And after a few seconds it's going to be found.
Now because we are using you see we are using our clcp.
So that's one package.
But we are also using example interfaces.
That's a second package.
So we will need to add the dependencies here in the my CPP package I go to the package dot XML.
You see we have a deep end tag with Lclc-rp.
I'm going to add another one depend example.
Interfaces and I close the depend tag.
All right.
So that's the same thing as we did for Python.
But now in Cplusplus there is an additional thing you need to do because you're going to compile the
file okay.
And you need the dependency.
When you compile you need to link the dependencies.
So we need to add something in the CMake list.txt.
Here we find the dependencies.
You see we have first element cmake and we have lclc-rp.
You're going to add another find package with example interfaces.
And we've also required okay.
And then we're going to use that also when we link the dependencies here with the executable.
All right.
So if you add a new package cage inside the file, you add it to the package dot XML and also to the
Cmakelists.txt.
Great.
Now we have included the interface.
Let's create the publisher.
We are going to create a private attribute.
So we have the public and the private.
In the private attribute I'm going to create a publisher and the publisher is going to be RCL, cpp
and inside RCL cpp we have a publisher class okay.
And this publisher class we need to give.
So this is going to be a template.
We need to give the interface.
So the interface is example interfaces and then MSG and then string just like that okay.
So when you create a publisher here when you declare a publisher you have to provide also the type in
C plus plus here using those brackets.
So that's a template.
And we are not actually going to create just a publisher, we are going to create a shared pointer to
a publisher.
Okay.
So once again we use shared pointer for everything in C plus plus in rust.
And this shared pointer here this sharedptr.
As you can see this is going to create an STD shared pointer here.
Okay.
So that's something you can use to simplify your code.
And let's call it publisher.
Okay.
So that's the name we give it with an underscore here just to say that that's an attribute of the class.
Great.
So now that I have declared it here I can initialize it in my constructor.
So publisher publisher like that is equal to.
And then I'm going to use this.
Okay.
It's not mandatory but it's just to explicitly say that I'm using the node functionality.
So you don't need to do that.
But as I told you before, I'm going to do this in this course and create a publisher.
So you have a create publisher method here to create a publisher.
And that's going to return a shared pointer to a publisher.
You need to give the interface once again.
So example interfaces msg and then string.
And then you're going to open the parentheses.
And you need to give the name of the topic.
So we've already provided the interface.
So the data type like this.
Then we need to give the name.
Let's also name it Robot News okay.
And also in those parentheses we are going to provide the queue size of ten okay.
So this I already explained with Python it's just a buffer in case you have too many messages are too
big or it's a lossy network, then that's going to add messages to a buffer up to ten messages okay.
And with that we have created the publisher.
Now let's create a method to publish.
So I'm going to add a method in private here void.
We don't return anything.
Publish news okay.
Let's call it publish news.
And I'm going to put this function as private.
Because this is not something we call from outside of the class.
We're just going to use it from a timer okay.
And the timer is inside the class.
So that's why I put it here in private.
And let's create.
We need to create a message I'm going to use the auto type.
So auto MSG and the message is example interfaces msg string.
You can really see here the powerfulness of the auto completion okay I really don't recommend you to
write C plus plus code for Ros2 without auto completion okay.
Because that's going to become a nightmare.
So you create here a string object.
And actually here is kind of an exception.
It's not going to be a shared pointer.
It's just going to be the object directly okay.
So then we can do MSG dot.
And here we have a field named data that can be high.
This is okay.
And let's directly do the string construction.
Let's create a robot name.
So I'm going to do this here STD string robot name like that.
And let's initialize the robot name here.
Robot name to um, let's name it this time R2d2.
All right.
And I'm going to concatenate that robot name.
And actually just in case we get some errors.
So this is an STD string that is not an STD string.
So I'm going to do STD string of this text plus this string plus.
STD string of that text.
So hi, this is robot name from the robot news station.
Um, STD here.
Okay, so we create the object for the message.
We here we add a data field because we have a data inside the interface.
And then we just need to publish.
So we have the publisher.
We do simply publisher.
So publisher the publisher is a shared pointer.
So that's an arrow publish.
And what do we publish.
We publish the message as simple as that.
And now that we have a publisher and a method to publish, well we need to actually call that method.
And we want to publish twice per second.
So I'm going to add a timer.
So I'm going to create a timer here.
For example here our Clcp Timer base.
And also no surprise, still a shared pointer.
Okay, so this we've done it already in the section on nodes for this course.
And after the publisher I'm going to initialize my timer.
Timer is equal to this.
So once again this is not mandatory just for me to explicitly say I'm using the node class create wall
timer.
So in C plus plus it's called wall timer.
And we need to give a duration.
So what I'm going to do here instead of doing STD Chrono and duration I'm gonna make things a bit simpler.
I'm going to do using namespace std chrono literals.
So by doing using namespace std chrono literals at the beginning, then I can just give a literal for
example 0.5 s for second and that's going to be recognized.
Okay.
We don't need to write the whole STD, Chrono etc..
So I have first the period in in second here.
And then I need to provide the callback.
So to provide the callback I need to do std bind.
And we will first provide the reference to.
So to this which is robot new station publish news.
So robot news station node publish news with no parentheses.
And then comma this specify that is this object we are binding to and a semicolon here.
All right.
So if this syntax is still complicated for you well don't worry because as I told you before it's always
going to be the same thing.
So after a few repetitions you will get used to it.
And that's going to be it.
I'm going to add a log here just to finish the constructor as Clcp info.
With this get logger.
And just to say the robot news station has been started.
Okay.
Just a log to finish the constructor.
Okay.
And now we can so we can save the file.
Make sure you save the file.
And let's go to the Cmakelists.txt to create an executable.
We have the first one here.
So let's add a new line here.
Add executable.
What's going to be the name.
Let's name it Robot News Station.
So once again I use the same name for the executable for the file name and for the node name.
So robot new station where is the file.
The file is in src Slash and then robot news station WKRP.
Robot news station dot CPP.
After that, I will do payment.
Target dependencies I will first provide the name of the executable, which is Robot News Station.
That's the name here I've provided here.
You see, this is where you should start not to make confusions because that's the name of the executable.
Here was obvious cpp node was that name.
It's not the name of the file, it's not the name of the node inside the file.
This is the name of the executable you write here.
Okay so not to be confused.
And then we need to provide all the dependencies.
We need to link to that.
So we have our clcp of course.
But then if we look at the file we have our clcp.
And we also have example interfaces.
So I'm going to write example interfaces that we have also Uh, added here we find package.
Okay.
So important that you add this line and that you add the dependency here.
Then this is going to create an executable.
But we also need to install it.
So I'm going to add simply one more line here.
Robot news station.
Okay.
So one executable per line.
And you don't need to change this structure.
And that's it.
So you can see after you've created your first executable adding another one is not going to take that
much time.
Let's save the file and we should be able to compile.
So let's go back to the terminal I go to my roster workspace and I do call on build.
I'm just going to build the package.
So the C plus plus one with packages select.
So my CP pkg.
Okay.
So it has been built and now I could so I could source, install, setup bash, run it from here.
I can also just open a new terminal is going to be the same.
So let's do that and let's run Ros to run my CP pg with.
So just in case I press tab twice.
And you see I have my new executable here.
Robot news station.
And we have the robot new station has been started.
So we have the log.
And you see the node is hanging.
So the node is spinning.
And we can also do Ros to node list.
Okay.
Ros to node info with that name.
Okay.
We see we have a publisher which is Robot News and the type is string from example interfaces package.
So we can find all this information again.
And we can also do Ros two to topic eco robot news.
And you see how this is out to the two from the robot news station.
And this is actually something else we can do is we can start the subscriber node from.
So Rostron my Python package with a smartphone okay.
So we are running the C plus plus publisher.
And here I'm going to try to run the subscriber I have created with Python for the exact same topic.
So with the same name and same data type let's see what happens.
And it correctly works.
You see we receive the data from the C plus plus publisher.
And so with this example you can clearly see that Ros two is language agnostic.
You can create one node in C plus plus and one other node in Python.
And both nodes can communicate using for example, topics.