WEBVTT

00:00.080 --> 00:05.840
And now that we have a Cplusplus publisher, let's write a Cplusplus subscriber.

00:05.840 --> 00:10.520
We are going to write a node for our smartphone like also we did with Python.

00:10.520 --> 00:15.710
And in this node we're going to create a subscriber for the topic we have just published to in the last

00:15.710 --> 00:16.280
lesson.

00:16.280 --> 00:24.470
So let's go again to our C plus plus package inside the source folder.

00:24.470 --> 00:30.410
And let's create a new file smartphone dot cp okay then.

00:30.410 --> 00:31.490
So I have this file.

00:31.490 --> 00:37.430
I'm going to open VSCode again from the source folder of the workspace.

00:37.550 --> 00:42.290
And well I've already had those robot new station here.

00:42.290 --> 00:44.390
And I have my smartphone CP.

00:44.690 --> 00:47.990
So I will once again start with the template.

00:47.990 --> 00:58.100
I will take the code from the template and put it here in the smartphone that CP, I will just do smartphone

00:58.100 --> 01:03.090
node and replace here.

01:03.120 --> 01:07.350
Plus, change the node name to smartphone.

01:07.500 --> 01:09.360
Okay, let's keep things simple.

01:09.360 --> 01:13.080
And once again I use the same name for the file name and the node name.

01:13.200 --> 01:15.600
Let's remove those comments.

01:16.680 --> 01:20.010
And I'm going to save as well.

01:20.490 --> 01:20.760
Okay.

01:20.790 --> 01:25.320
So we can have the dependency here that's found.

01:25.800 --> 01:26.160
Okay.

01:26.160 --> 01:28.290
So the node is initialized.

01:28.290 --> 01:30.510
You can see now it's going very quick.

01:30.510 --> 01:32.520
And let's create a subscriber.

01:32.520 --> 01:37.290
And actually before we create the subscriber I'm going to include the interface.

01:37.290 --> 01:38.910
So what interface.

01:38.910 --> 01:44.160
Well in this case if you have a subscriber and you already know that you have a publisher to a topic,

01:44.190 --> 01:46.350
then you need to make sure you use the same interface.

01:46.350 --> 01:47.940
So we don't even have a choice.

01:47.940 --> 01:52.800
We're going to use this example interfaces msg string.

01:53.100 --> 02:00.810
So we include it here include example Interfaces.

02:01.170 --> 02:04.770
MSG string dot http.

02:05.340 --> 02:09.510
Exact same thing and I save the file.

02:09.660 --> 02:11.310
Wait a few seconds.

02:12.090 --> 02:12.540
Okay.

02:12.570 --> 02:18.150
And then as we add these example interfaces, we should also make sure that it's in the package dot

02:18.150 --> 02:18.810
XML.

02:18.930 --> 02:23.760
It's already here and in the cmakelists.txt it's already there.

02:23.760 --> 02:26.280
So nothing else to do.

02:26.310 --> 02:28.410
Now for the subscriber.

02:28.410 --> 02:30.360
So we're going to create a subscriber.

02:30.360 --> 02:34.380
We're going to declare it here in as a private attribute.

02:34.380 --> 02:37.020
Then we're going to initialize it in the constructor.

02:37.020 --> 02:41.610
And then we're going to create a callback so that we can receive the messages.

02:41.640 --> 02:44.010
And actually I'm just going to start with the callback.

02:44.040 --> 02:44.520
Okay.

02:44.520 --> 02:50.130
So in private I'm going to use the callback because this is something that's only going to be called

02:50.190 --> 02:52.110
by the node itself.

02:52.110 --> 02:52.800
When we spin.

02:52.800 --> 02:55.410
We don't really want to call that from the outside.

02:55.410 --> 02:57.810
So that's going to be in private.

02:57.870 --> 02:59.230
It's going to be void.

02:59.260 --> 03:00.910
It's not returning anything.

03:00.910 --> 03:05.410
And let's name it Callback Robot news.

03:05.410 --> 03:12.850
So once again, I like to use the callback name to start the function with callback, and then put the

03:12.850 --> 03:14.080
name of the topic.

03:14.080 --> 03:16.480
And what do we receive here?

03:16.480 --> 03:21.100
We're going to receive the message, but actually we are not going to receive the message.

03:21.100 --> 03:27.610
We are going to receive a shared pointer to the message and even better, a const shared pointer.

03:27.610 --> 03:28.900
So that's going to be const.

03:28.900 --> 03:30.130
And then the type.

03:30.130 --> 03:36.850
So example interfaces msg string.

03:36.850 --> 03:38.620
Here we string with uppercase.

03:38.920 --> 03:41.380
And then we have also shared pointer.

03:41.380 --> 03:49.540
So for all the interfaces in Ros2 you can also use this Sharedptr to make it a shared pointer.

03:49.720 --> 03:51.520
And let's call it msg.

03:51.880 --> 03:53.710
That's the message we receive.

03:54.820 --> 03:55.180
All right.

03:55.180 --> 04:01.480
So you see const the type with a short pointer and then the name that you that you choose, and then

04:01.480 --> 04:02.830
you can do whatever you want.

04:02.860 --> 04:08.260
So here we are just going to print it as Clcp info.

04:08.290 --> 04:16.030
With this get logger and let's do that.

04:16.030 --> 04:18.610
Let's print a string.

04:18.790 --> 04:20.590
And what do we print.

04:20.620 --> 04:22.060
Not just the message.

04:22.060 --> 04:23.830
The message is an object.

04:23.830 --> 04:25.540
So we're going to use.

04:25.570 --> 04:29.710
And we're going to use an arrow because it's a shared pointer to the object.

04:29.710 --> 04:34.420
So message arrow data we want to print the data.

04:34.420 --> 04:37.600
And data is an STD string.

04:37.600 --> 04:44.890
So to make it work with Lclc-rp info we're going to cast it to a C string.

04:44.890 --> 04:52.930
So you can just do dot c str with the parentheses to make it work with this percentage s.

04:52.960 --> 04:53.470
All right.

04:53.470 --> 04:54.460
And we have the callback.

04:54.460 --> 05:01.700
So now that we have the callback let's create the subscriber I'm going to add here as a private attribute.

05:01.730 --> 05:06.080
I'll Klccp subscriber.

05:06.080 --> 05:09.350
So it's not subscriber it's subscription.

05:09.350 --> 05:12.350
So we have a subscription object we need to provide.

05:12.350 --> 05:16.820
So as we did here you see with the publisher we also need to provide.

05:16.820 --> 05:17.840
So that's going to be a template.

05:17.840 --> 05:21.110
And we need to provide the interface.

05:21.110 --> 05:28.310
So I'm going to write the interface example interfaces MSG string.

05:28.670 --> 05:34.250
And of course maybe you have already guessed it's not just going to be a subscription object, it's

05:34.250 --> 05:38.330
going to be a shared pointer to the subscription object.

05:38.330 --> 05:43.490
And let's name it subscriber subscriber.

05:44.540 --> 05:47.060
So you could call it subscription subscriber.

05:47.090 --> 05:49.220
It doesn't really matter.

05:49.220 --> 05:54.320
Usually we talk about publishers and subscribers, but in the code it's subscription.

05:54.320 --> 05:57.140
So we have that declaration here.

05:57.140 --> 05:58.670
Let's initialize.

05:58.700 --> 06:08.960
Subscriber is equal to and I'm using this just to explicitly say we're using the Clcp node class and

06:08.960 --> 06:12.350
create subscription.

06:12.350 --> 06:21.500
We need to pass again the type for example interfaces msg string.

06:21.500 --> 06:23.420
And then I'm going to open the parentheses.

06:23.420 --> 06:25.580
And what do we need to provide.

06:25.610 --> 06:28.790
We need to provide first the name.

06:28.790 --> 06:30.740
So we've already provided the type.

06:30.980 --> 06:32.930
So no need to provide inside the parentheses.

06:32.930 --> 06:33.980
It's already here.

06:34.010 --> 06:35.480
The name is going to be.

06:35.510 --> 06:36.860
And that's very important.

06:36.860 --> 06:41.660
If we want the publisher and the subscriber to communicate we need to use the same name.

06:41.660 --> 06:45.290
So robot news just like that.

06:45.290 --> 06:46.790
Then we have the queue size.

06:46.790 --> 06:48.650
So I'm just going to use ten.

06:48.650 --> 06:52.310
And just after the queue size we have the callback.

06:52.310 --> 06:55.040
So maybe I'm going to go back to a new line here.

06:55.470 --> 06:58.350
and also here because it's going to be quite long.

06:58.350 --> 07:05.490
So to provide the callback we use once again std bind with first the reference.

07:05.490 --> 07:13.080
So that's going to be smartphone node callback robot news okay.

07:13.110 --> 07:17.910
So reference to the method without parentheses.

07:18.240 --> 07:21.540
Then we have a comma.

07:21.930 --> 07:26.370
It's going to be this to say that we bind to this node.

07:26.370 --> 07:28.560
And then that's not it.

07:28.680 --> 07:29.580
Why is that.

07:29.580 --> 07:32.010
Because we have here a parameter.

07:32.010 --> 07:35.940
So if you just try to run it like this and compile, it's not going to work.

07:35.970 --> 07:40.320
You need to add an what's called an STD placeholder.

07:40.320 --> 07:44.250
So STD placeholders.

07:45.000 --> 07:52.470
So it's just something you need to write and then underscore one because you have one argument okay.

07:52.500 --> 08:02.250
If you had two arguments you will need to add another one std placeholder with so STD placeholders underscore

08:02.280 --> 08:04.350
two and then three, etc..

08:04.380 --> 08:09.690
Okay, to make things a bit simpler here, I'm going to sew at the top.

08:09.690 --> 08:16.110
I'm going to do using namespace std placeholders.

08:16.140 --> 08:16.650
Okay.

08:16.650 --> 08:20.670
So then I can just write underscore one here.

08:20.670 --> 08:23.520
And let's not forget the semicolon at the end.

08:23.550 --> 08:23.790
Okay.

08:23.820 --> 08:26.460
So to recap here you do create subscription.

08:26.460 --> 08:29.790
You provide the interface here between brackets.

08:29.790 --> 08:31.350
Then you open the parentheses.

08:31.350 --> 08:37.830
You put first argument is the name, then queue size and then stdbind with first the reference to the

08:37.830 --> 08:39.270
function this.

08:39.270 --> 08:42.840
So the keyword this and then a placeholder.

08:42.840 --> 08:47.730
That's going to be for that argument that we have in the function.

08:47.730 --> 08:50.730
And let's finish with our clcp.

08:50.730 --> 08:53.400
So now that's done let's finish the constructor.

08:54.190 --> 09:06.070
our club info this get logger and let's just say smartphone has been studied.

09:06.430 --> 09:07.240
Okay.

09:07.240 --> 09:13.630
Just so we have something that's printed on the screen and that's it for the subscriber code.

09:13.660 --> 09:13.810
Okay.

09:13.840 --> 09:19.240
So you create a shared pointer here, you declare it, you create it here in the constructor.

09:19.240 --> 09:22.600
And then you have a callback better as a private method here.

09:22.600 --> 09:25.330
And you receive the message as a shared pointer.

09:25.330 --> 09:27.190
And then you can just do whatever you want.

09:27.190 --> 09:28.420
Here we just print it.

09:28.420 --> 09:31.240
So let's you make sure you save everything.

09:31.240 --> 09:36.010
Now you go back to the Cmakelists.txt and we are going to add a new executable.

09:36.040 --> 09:37.990
Add executable.

09:37.990 --> 09:40.870
Let's call it smartphone.

09:40.900 --> 09:43.000
It's going to be the name of the executable.

09:43.000 --> 09:44.560
Where is the file.

09:44.740 --> 09:58.600
The file is in source smartphone dot CVP smartphone dot cpp Then payment target dependencies with the

09:58.600 --> 10:00.040
executable name.

10:00.040 --> 10:02.050
Smart phone.

10:02.080 --> 10:04.360
What do we need to link?

10:04.660 --> 10:09.580
We need to link our Clcp and example interfaces.

10:09.610 --> 10:12.550
If you are not sure you go back you see what do we include?

10:12.730 --> 10:15.730
We include that one and we include that one.

10:16.420 --> 10:18.100
That's it for the executable.

10:18.190 --> 10:22.390
We just add the executable to the targets to install.

10:23.140 --> 10:28.330
So that's going to be smart phone and that's it.

10:28.330 --> 10:30.310
So one executable per line.

10:30.310 --> 10:32.830
And you don't add any comma or anything here.

10:32.860 --> 10:33.400
Okay.

10:33.400 --> 10:34.510
Let's save.

10:34.510 --> 10:39.940
And then we can go back and let's just go to our roster workspace.

10:39.940 --> 10:50.680
And let's do a call on build with packages select and my CP package.

10:53.450 --> 10:56.420
Okay, so you can see it took 19 seconds for me.

10:56.750 --> 10:59.060
Um, let's open a new terminal.

10:59.060 --> 11:00.920
Just so we have everything sourced.

11:00.920 --> 11:04.730
I'm going to also split it in two and we can start.

11:04.730 --> 11:06.950
I'm going to start it here plus two.

11:06.980 --> 11:11.960
Run my CP pkg and that's going to be smart.

11:11.990 --> 11:14.270
Should be found smart phone.

11:14.750 --> 11:15.920
Let's press enter.

11:15.920 --> 11:23.000
And you see smartphone has been started I can do Ros two node list okay.

11:23.030 --> 11:28.580
Smart phone I can do Ros two node info with a smart phone.

11:29.510 --> 11:31.670
You can see that in the.

11:31.700 --> 11:35.150
So this time in the subscribers we have robot news okay.

11:35.180 --> 11:38.570
So for the publishers it was in the in the publishers here.

11:38.720 --> 11:40.490
Here we have a subscriber.

11:40.490 --> 11:43.490
So the smartphone is a subscriber to the Robot News.

11:43.490 --> 11:45.980
And let's test the full communication.

11:45.980 --> 11:53.090
So Ros to run my CP pkg with We've robot news station.

11:54.020 --> 11:54.590
You see that?

11:54.590 --> 11:56.120
We received the messages.

11:56.600 --> 11:58.520
Now I'm going to stop that one.

11:58.520 --> 12:03.440
And let's start to run my pi pg.

12:04.580 --> 12:06.110
Let's start the Python one.

12:07.670 --> 12:09.830
And you can see that we also receive.

12:09.860 --> 12:11.360
And the message is different.

12:11.360 --> 12:12.890
You see the robot name is different.

12:12.890 --> 12:14.210
So I'm going to stop that one.

12:14.420 --> 12:17.480
You see the the robot name is different.

12:17.510 --> 12:20.750
We receive messages from two different publishers.

12:20.750 --> 12:22.340
But because both publishers.

12:22.340 --> 12:27.290
So the C plus Plus one and the Python one were publishing to the same topic with the same data type,

12:27.290 --> 12:30.260
then we receive all the messages in the subscriber.

12:30.260 --> 12:33.170
We don't even know from who we receive the messages.

12:33.170 --> 12:34.970
It doesn't matter at all, okay?

12:35.000 --> 12:41.090
We just receive messages that correspond to the string data type on the robot news topic.

12:41.090 --> 12:47.750
So now what you can try is you can run any combination of Python or C plus plus publisher with any other

12:47.750 --> 12:51.200
combination of Python or C plus plus subscriber.
