And now that we have a Cplusplus publisher, let's write a Cplusplus subscriber.
We are going to write a node for our smartphone like also we did with Python.
And in this node we're going to create a subscriber for the topic we have just published to in the last
lesson.
So let's go again to our C plus plus package inside the source folder.
And let's create a new file smartphone dot cp okay then.
So I have this file.
I'm going to open VSCode again from the source folder of the workspace.
And well I've already had those robot new station here.
And I have my smartphone CP.
So I will once again start with the template.
I will take the code from the template and put it here in the smartphone that CP, I will just do smartphone
node and replace here.
Plus, change the node name to smartphone.
Okay, let's keep things simple.
And once again I use the same name for the file name and the node name.
Let's remove those comments.
And I'm going to save as well.
Okay.
So we can have the dependency here that's found.
Okay.
So the node is initialized.
You can see now it's going very quick.
And let's create a subscriber.
And actually before we create the subscriber I'm going to include the interface.
So what interface.
Well in this case if you have a subscriber and you already know that you have a publisher to a topic,
then you need to make sure you use the same interface.
So we don't even have a choice.
We're going to use this example interfaces msg string.
So we include it here include example Interfaces.
MSG string dot http.
Exact same thing and I save the file.
Wait a few seconds.
Okay.
And then as we add these example interfaces, we should also make sure that it's in the package dot
XML.
It's already here and in the cmakelists.txt it's already there.
So nothing else to do.
Now for the subscriber.
So we're going to create a subscriber.
We're going to declare it here in as a private attribute.
Then we're going to initialize it in the constructor.
And then we're going to create a callback so that we can receive the messages.
And actually I'm just going to start with the callback.
Okay.
So in private I'm going to use the callback because this is something that's only going to be called
by the node itself.
When we spin.
We don't really want to call that from the outside.
So that's going to be in private.
It's going to be void.
It's not returning anything.
And let's name it Callback Robot news.
So once again, I like to use the callback name to start the function with callback, and then put the
name of the topic.
And what do we receive here?
We're going to receive the message, but actually we are not going to receive the message.
We are going to receive a shared pointer to the message and even better, a const shared pointer.
So that's going to be const.
And then the type.
So example interfaces msg string.
Here we string with uppercase.
And then we have also shared pointer.
So for all the interfaces in Ros2 you can also use this Sharedptr to make it a shared pointer.
And let's call it msg.
That's the message we receive.
All right.
So you see const the type with a short pointer and then the name that you that you choose, and then
you can do whatever you want.
So here we are just going to print it as Clcp info.
With this get logger and let's do that.
Let's print a string.
And what do we print.
Not just the message.
The message is an object.
So we're going to use.
And we're going to use an arrow because it's a shared pointer to the object.
So message arrow data we want to print the data.
And data is an STD string.
So to make it work with Lclc-rp info we're going to cast it to a C string.
So you can just do dot c str with the parentheses to make it work with this percentage s.
All right.
And we have the callback.
So now that we have the callback let's create the subscriber I'm going to add here as a private attribute.
I'll Klccp subscriber.
So it's not subscriber it's subscription.
So we have a subscription object we need to provide.
So as we did here you see with the publisher we also need to provide.
So that's going to be a template.
And we need to provide the interface.
So I'm going to write the interface example interfaces MSG string.
And of course maybe you have already guessed it's not just going to be a subscription object, it's
going to be a shared pointer to the subscription object.
And let's name it subscriber subscriber.
So you could call it subscription subscriber.
It doesn't really matter.
Usually we talk about publishers and subscribers, but in the code it's subscription.
So we have that declaration here.
Let's initialize.
Subscriber is equal to and I'm using this just to explicitly say we're using the Clcp node class and
create subscription.
We need to pass again the type for example interfaces msg string.
And then I'm going to open the parentheses.
And what do we need to provide.
We need to provide first the name.
So we've already provided the type.
So no need to provide inside the parentheses.
It's already here.
The name is going to be.
And that's very important.
If we want the publisher and the subscriber to communicate we need to use the same name.
So robot news just like that.
Then we have the queue size.
So I'm just going to use ten.
And just after the queue size we have the callback.
So maybe I'm going to go back to a new line here.
and also here because it's going to be quite long.
So to provide the callback we use once again std bind with first the reference.
So that's going to be smartphone node callback robot news okay.
So reference to the method without parentheses.
Then we have a comma.
It's going to be this to say that we bind to this node.
And then that's not it.
Why is that.
Because we have here a parameter.
So if you just try to run it like this and compile, it's not going to work.
You need to add an what's called an STD placeholder.
So STD placeholders.
So it's just something you need to write and then underscore one because you have one argument okay.
If you had two arguments you will need to add another one std placeholder with so STD placeholders underscore
two and then three, etc..
Okay, to make things a bit simpler here, I'm going to sew at the top.
I'm going to do using namespace std placeholders.
Okay.
So then I can just write underscore one here.
And let's not forget the semicolon at the end.
Okay.
So to recap here you do create subscription.
You provide the interface here between brackets.
Then you open the parentheses.
You put first argument is the name, then queue size and then stdbind with first the reference to the
function this.
So the keyword this and then a placeholder.
That's going to be for that argument that we have in the function.
And let's finish with our clcp.
So now that's done let's finish the constructor.
our club info this get logger and let's just say smartphone has been studied.
Okay.
Just so we have something that's printed on the screen and that's it for the subscriber code.
Okay.
So you create a shared pointer here, you declare it, you create it here in the constructor.
And then you have a callback better as a private method here.
And you receive the message as a shared pointer.
And then you can just do whatever you want.
Here we just print it.
So let's you make sure you save everything.
Now you go back to the Cmakelists.txt and we are going to add a new executable.
Add executable.
Let's call it smartphone.
It's going to be the name of the executable.
Where is the file.
The file is in source smartphone dot CVP smartphone dot cpp Then payment target dependencies with the
executable name.
Smart phone.
What do we need to link?
We need to link our Clcp and example interfaces.
If you are not sure you go back you see what do we include?
We include that one and we include that one.
That's it for the executable.
We just add the executable to the targets to install.
So that's going to be smart phone and that's it.
So one executable per line.
And you don't add any comma or anything here.
Okay.
Let's save.
And then we can go back and let's just go to our roster workspace.
And let's do a call on build with packages select and my CP package.
Okay, so you can see it took 19 seconds for me.
Um, let's open a new terminal.
Just so we have everything sourced.
I'm going to also split it in two and we can start.
I'm going to start it here plus two.
Run my CP pkg and that's going to be smart.
Should be found smart phone.
Let's press enter.
And you see smartphone has been started I can do Ros two node list okay.
Smart phone I can do Ros two node info with a smart phone.
You can see that in the.
So this time in the subscribers we have robot news okay.
So for the publishers it was in the in the publishers here.
Here we have a subscriber.
So the smartphone is a subscriber to the Robot News.
And let's test the full communication.
So Ros to run my CP pkg with We've robot news station.
You see that?
We received the messages.
Now I'm going to stop that one.
And let's start to run my pi pg.
Let's start the Python one.
And you can see that we also receive.
And the message is different.
You see the robot name is different.
So I'm going to stop that one.
You see the the robot name is different.
We receive messages from two different publishers.
But because both publishers.
So the C plus Plus one and the Python one were publishing to the same topic with the same data type,
then we receive all the messages in the subscriber.
We don't even know from who we receive the messages.
It doesn't matter at all, okay?
We just receive messages that correspond to the string data type on the robot news topic.
So now what you can try is you can run any combination of Python or C plus plus publisher with any other
combination of Python or C plus plus subscriber.