WEBVTT

00:00.080 --> 00:02.870
Let's now do the second step for this activity.

00:02.900 --> 00:05.330
We already have a number publisher node.

00:05.330 --> 00:07.700
We are going to create a second node.

00:07.730 --> 00:13.490
So a second file where we have another node in which we are going to subscribe to this number topic.

00:13.490 --> 00:17.300
And then we are going to add every number that we receive to a counter.

00:17.300 --> 00:21.290
And we are going to publish this counter to a new topic.

00:21.320 --> 00:21.590
Okay.

00:21.620 --> 00:27.170
So now that we have the publisher working, we know that we should receive the correct data on the subscriber

00:27.170 --> 00:27.470
side.

00:27.470 --> 00:29.090
So let's create a new file.

00:29.120 --> 00:30.890
Let's go back to our.

00:30.950 --> 00:33.980
So package my pi pkg.

00:33.980 --> 00:38.150
And in this folder where we have all the files.

00:38.150 --> 00:48.230
And let's do a number counter dot pi which we also make executable number counter.

00:48.830 --> 00:50.690
And it's right there.

00:50.780 --> 00:54.590
I'm also going to remove the pi cache.

00:54.650 --> 00:55.910
No need for that.

00:55.970 --> 00:59.540
And let's start this node.

00:59.570 --> 01:01.120
That's going to be number counter.

01:01.390 --> 01:04.330
We are going to need the template.

01:04.390 --> 01:14.920
So let's put the template here and let's name it number counter node.

01:16.450 --> 01:18.700
Let's also modify it here.

01:18.730 --> 01:25.510
I'll remove that comment and the name of the node let's say also number counter.

01:26.650 --> 01:26.890
Okay.

01:26.920 --> 01:28.270
Let's keep things simple.

01:29.080 --> 01:32.050
And then what we are going to create a subscriber.

01:32.050 --> 01:37.000
So to create a subscriber we will need a name and a message type.

01:37.000 --> 01:39.310
What's the message or the interface type.

01:39.310 --> 01:40.780
Well it's very easy.

01:40.810 --> 01:44.830
It is going to be the exact same that we have on the publisher okay.

01:44.860 --> 01:47.320
Otherwise they won't be able to communicate with each other.

01:47.320 --> 01:55.090
So from example interfaces dot msg import int 64.

01:55.090 --> 01:58.420
And we've already added that inside the package dot XML.

01:58.420 --> 02:00.340
So no need to do anything else.

02:00.370 --> 02:00.730
Great.

02:00.730 --> 02:02.980
So I'm going to start with the subscriber.

02:03.010 --> 02:03.190
Okay.

02:03.220 --> 02:04.900
That's the first thing we need to do here.

02:05.200 --> 02:07.030
I'm going to create a subscriber here.

02:07.030 --> 02:08.470
Self dot.

02:08.500 --> 02:12.400
Let's name it simply number subscriber.

02:13.510 --> 02:20.500
Subscriber like that is equal to self dot create subscription.

02:20.500 --> 02:27.100
And what do we need to give first the message type which is going to be int 64.

02:27.130 --> 02:30.610
Then we have the topic name.

02:30.610 --> 02:32.050
What is the topic name.

02:32.050 --> 02:34.870
Well topic name you see here is number.

02:34.870 --> 02:38.110
So you write number exactly okay.

02:38.140 --> 02:40.780
If you have any typo here that's not going to work.

02:40.810 --> 02:43.420
And then we need to provide a callback okay.

02:43.450 --> 02:46.420
So I'm going to write a callback first here.

02:46.450 --> 02:49.780
Let's name it simply callback.

02:49.780 --> 02:51.730
So I start with callback.

02:52.180 --> 02:55.870
And then I'm going to put the name of the topic or something similar.

02:55.870 --> 02:58.690
So callback number we receive.

02:58.690 --> 03:00.980
So we have self and then we receive a message.

03:01.010 --> 03:07.370
And I'm just going to add this to specify that this message is an int 64.

03:07.400 --> 03:09.410
That's going to help with the auto completion.

03:09.410 --> 03:16.190
And well I'm first going to do a log just to make sure that this subscriber is working okay.

03:16.220 --> 03:19.730
If you have a doubt you can always do it step by step.

03:19.730 --> 03:29.900
So let's do self get logger info and we should be able to just publish the message like that message

03:29.900 --> 03:30.740
dot data.

03:30.770 --> 03:32.030
That should work.

03:32.030 --> 03:35.870
So then we can go back to our create subscription.

03:35.870 --> 03:41.210
Here we put self dot callback number.

03:41.210 --> 03:48.830
And then we have the queue size because it's a bit long I'm going to press enter here.

03:48.860 --> 03:49.280
All right.

03:49.280 --> 03:51.080
And we have our subscriber here.

03:51.080 --> 03:54.500
We have registered a callback which is that method.

03:54.500 --> 04:00.760
So when the node is spinning whenever we receive a message on the number topic, we are going to call

04:00.760 --> 04:01.990
this method.

04:01.990 --> 04:03.190
So let's try that.

04:03.190 --> 04:06.550
Actually, let's just try this very simple code.

04:06.550 --> 04:15.910
To try this, I'm going to go back to my setup.py and add a new executable named number counter is equal

04:15.910 --> 04:26.740
to my pi pkg dot file, name is number counter and the function name is main.

04:26.770 --> 04:27.310
Okay.

04:27.310 --> 04:29.260
And make sure you have a comma okay.

04:29.290 --> 04:30.040
Every time.

04:30.190 --> 04:33.310
Now that I have this let's go back.

04:33.340 --> 04:39.460
Let's go to our roster workspace and let's run the command.

04:39.580 --> 04:42.250
So it should be here call code build.

04:42.640 --> 04:48.730
We are going to do this package only and symlink install okay.

04:48.730 --> 04:50.470
It has been built.

04:50.470 --> 04:55.630
So right here I already have my publisher running okay.

04:55.660 --> 04:57.370
So you can study it again.

04:57.400 --> 04:59.820
Or you can just keep it running.

04:59.820 --> 05:02.910
And in that terminal I'm going to source.

05:04.230 --> 05:06.810
I'm going to source because I have just built the package.

05:06.840 --> 05:07.230
Okay.

05:07.260 --> 05:09.390
I have a new node, so I need to source it.

05:09.390 --> 05:14.280
And let's also source that one just in case we use it later.

05:15.030 --> 05:20.700
And let's do Ros to run my pi PG with number.

05:20.700 --> 05:23.970
And this time it's going to be number counter.

05:24.000 --> 05:24.480
Okay.

05:24.480 --> 05:29.430
So we make sure that we find the executable with the autocompletion.

05:29.430 --> 05:31.710
And then we make sure that it's working.

05:31.710 --> 05:33.690
And you see we have an error.

05:34.440 --> 05:37.380
And it seems the error is with the log.

05:37.470 --> 05:44.400
Yeah probably if I go back well probably I need to cast this as a string.

05:46.920 --> 05:47.400
Okay.

05:47.430 --> 05:49.290
Otherwise it doesn't really like it.

05:49.290 --> 05:51.510
So let's save and let's go back.

05:51.540 --> 05:57.180
I have used Simulink install and I have source this terminal, so I should be able to run it again.

05:57.180 --> 05:58.940
And this time it's working.

05:58.970 --> 05:59.180
Okay.

05:59.210 --> 06:03.020
We receive the number two every one second.

06:03.020 --> 06:04.430
You can verify with the log.

06:04.430 --> 06:05.930
That's every one second.

06:05.960 --> 06:07.490
So let's stop that.

06:07.490 --> 06:09.860
And now we know that this subscriber is working.

06:09.860 --> 06:11.540
So we have the publisher working.

06:11.570 --> 06:13.220
The subscriber is working.

06:13.220 --> 06:15.320
We don't really need this log anymore okay.

06:15.350 --> 06:16.550
I was just to test that.

06:16.550 --> 06:17.300
It's working.

06:17.300 --> 06:23.960
Now what we want to do is to add all the numbers that we get inside a counter, and then we want to

06:23.990 --> 06:26.690
publish that counter on a new topic.

06:26.690 --> 06:29.030
So let's first create a counter.

06:29.180 --> 06:29.990
That's quite easy.

06:30.020 --> 06:35.060
We do self dot counter is equal to zero here.

06:35.060 --> 06:43.310
And then we do self dot counter plus equal message dot data okay.

06:43.340 --> 06:45.080
We just add the data.

06:45.080 --> 06:49.700
So the number we receive to the counter which starts at zero as simple as that.

06:49.700 --> 06:52.700
And then we want to publish this counter.

06:52.700 --> 06:59.530
So let's create a new publisher for example here Let's do self dot.

06:59.560 --> 07:00.820
So we're going to publish what.

07:00.850 --> 07:04.240
A number count or number counter.

07:04.270 --> 07:05.620
Let's do number count.

07:06.130 --> 07:11.410
Publisher is equal to self.

07:11.800 --> 07:14.170
Create publisher.

07:14.170 --> 07:16.540
And we need to decide because that's a new topic.

07:16.540 --> 07:19.960
We need to decide on a message type and a name.

07:19.960 --> 07:21.310
What's going to be the message type.

07:21.310 --> 07:23.530
So I didn't give you the message type.

07:23.530 --> 07:24.340
But.

07:24.370 --> 07:32.200
Well if we were using Int64 to publish a number and then we add all those numbers inside the counter.

07:32.200 --> 07:33.970
So that's still that's still a number.

07:33.970 --> 07:35.200
That's the same data type.

07:35.230 --> 07:35.620
Okay.

07:35.650 --> 07:37.240
So it's still an integer.

07:37.240 --> 07:38.590
So we can use the same.

07:38.590 --> 07:42.370
We can use int 64.

07:42.400 --> 07:43.090
All right.

07:43.090 --> 07:44.680
Let's not make it complicated.

07:44.680 --> 07:48.370
If we have the same data type we use the same message.

07:48.370 --> 07:51.340
And then we need to decide on a name.

07:51.340 --> 07:54.160
So I'm going to name it number count.

07:54.160 --> 07:57.230
And we need a size ten.

07:57.620 --> 07:59.270
Let's go back to a new line here.

07:59.750 --> 08:00.260
All right.

08:00.260 --> 08:04.520
So in our constructor we have what we have a publisher and we have a subscriber.

08:04.550 --> 08:08.420
Now how are we going to publish to this topic.

08:08.420 --> 08:12.980
So that's going to be the most interesting part of this activity is that we are not going to publish

08:12.980 --> 08:17.780
from a timer, because what we've seen so far, for example, in the number publisher, is that you

08:17.810 --> 08:21.470
create a publisher and then how to publish to this topic.

08:21.470 --> 08:26.750
Well, we create a timer and we say every x amount of time we just publish something.

08:27.050 --> 08:33.980
But you can publish from anywhere, so you can publish from a timer callback, but you can also publish

08:33.980 --> 08:36.260
from a subscriber callback.

08:36.260 --> 08:41.360
And here it makes sense because we want to publish the counter when it's updated.

08:41.360 --> 08:43.280
And when do we update the counter?

08:43.280 --> 08:46.130
Well, we update it when we receive a new number.

08:46.130 --> 08:53.000
So in this subscriber callback we are going to create a new message and to publish it.

08:53.600 --> 08:54.740
So here I can do.

08:54.740 --> 08:56.950
So I'm not going to do MSG I'm not going to name it.

08:56.980 --> 08:58.900
Message like that because we already have.

08:58.930 --> 08:59.920
Message here.

08:59.920 --> 09:00.580
So I'm going to do.

09:00.610 --> 09:07.300
New message that's going to be an INT 64.

09:07.810 --> 09:10.420
Because that's what we have here as well.

09:10.960 --> 09:18.100
And then new message dot data is equal to self counter.

09:19.480 --> 09:23.740
And then we can just do self dot number count.

09:23.770 --> 09:30.130
Publisher dot publish with the new message.

09:30.160 --> 09:30.670
All right.

09:30.670 --> 09:37.630
So this way we only publish when we receive a message here with this subscriber callback.

09:37.630 --> 09:39.970
And this way it's kind of a data pipeline.

09:40.000 --> 09:40.150
Okay.

09:40.180 --> 09:44.560
You receive a message and as soon as you receive a message, you can process it.

09:44.560 --> 09:46.990
You can do what you can do different kind of stuff.

09:46.990 --> 09:50.440
And then you send another message on another topic.

09:50.440 --> 09:56.100
And here, well, I just write this code here in the callback number, I could have created another

09:56.100 --> 10:01.230
function named for example publish number counter so you can do as you want.

10:01.260 --> 10:02.700
Here I'm just going to keep it like this.

10:02.700 --> 10:09.150
And let's add a log self get logger info.

10:10.380 --> 10:14.610
Number counter has been studied.

10:14.640 --> 10:16.290
All right let's save the file.

10:16.290 --> 10:17.820
And that's about it for the code.

10:17.820 --> 10:23.160
So to recap when we create the node here we create a publisher and a subscriber.

10:23.160 --> 10:26.010
We register a callback with the subscriber.

10:26.040 --> 10:26.670
Okay.

10:26.670 --> 10:32.970
So when the node is spinning, whenever we receive a message on the number topic, we're going to go

10:32.970 --> 10:33.960
to this callback.

10:33.960 --> 10:37.740
And when we go to this callback, we add the number to the counter.

10:37.740 --> 10:41.820
And we publish this counter to the number count topic.

10:41.820 --> 10:45.210
So that's also a very common structure in robotics.

10:45.240 --> 10:46.950
Now we can try it.

10:46.950 --> 10:49.530
So no need to compile anything.

10:49.530 --> 10:59.030
We've used Simulink install I'm just going to stop the publisher And let's start the subscribe.

10:59.060 --> 11:04.220
So the number counter again and you see number counter has been started.

11:04.460 --> 11:08.360
We have nothing actually we don't print anything here.

11:08.360 --> 11:10.070
So we will need to check the topics.

11:10.070 --> 11:11.630
So let's go here.

11:12.380 --> 11:16.700
Let's do Ros two topic list.

11:17.090 --> 11:21.140
And you see that we have a number topic and a number count topic.

11:21.170 --> 11:24.890
If I do Ros two topic equal number.

11:25.670 --> 11:29.540
Well we have nothing because now what is publishing on this number topic.

11:29.570 --> 11:30.170
Okay.

11:30.200 --> 11:35.390
And if I do number count we also have nothing.

11:35.450 --> 11:36.050
Okay.

11:36.050 --> 11:38.120
Because nobody is publishing on that.

11:38.150 --> 11:42.200
All right we need to start the number publisher.

11:43.610 --> 11:43.790
Okay.

11:43.820 --> 11:45.680
So number publisher has been started.

11:45.680 --> 11:52.040
And then the number counter is receiving the messages and adding them to a counter and publishing to

11:52.070 --> 11:52.730
the counter.

11:52.730 --> 11:55.030
So let's just print again.

11:55.060 --> 11:56.680
What do we have on the number topic.

11:56.680 --> 11:59.050
So you see that's what's published here.

11:59.680 --> 12:00.640
Data two.

12:00.670 --> 12:04.840
And then let's do a Ross two topic equal with the number count.

12:06.550 --> 12:11.980
And well at this time you see the counter is 54 and 56.

12:11.980 --> 12:14.110
And then plus two every time.

12:14.140 --> 12:14.800
All right.

12:14.800 --> 12:21.970
You can see if I stop for example the number publisher, we don't receive any new number here.

12:21.970 --> 12:26.380
So we don't publish any new counter data okay.

12:26.380 --> 12:33.700
If I start the number publisher again we continue okay, 7678, etc..

12:33.730 --> 12:38.830
Now if I stop the number counter, you see, if I stop the number counter, well, we don't publish

12:38.830 --> 12:41.260
anything else on the number count topic.

12:41.260 --> 12:42.970
And if I start it again.

12:46.120 --> 12:48.070
Then it's going to receive messages from there.

12:48.070 --> 12:49.990
And you see we have two four.

12:49.990 --> 12:53.440
So this counter starts again at zero.

12:55.140 --> 12:55.650
All right.

12:55.650 --> 13:01.080
If you start this node again, you see, when we start the node again, we reset the counter to zero.

13:02.130 --> 13:02.910
Great.

13:02.910 --> 13:03.750
Let's.

13:03.960 --> 13:05.730
So the two are running.

13:05.730 --> 13:08.250
Now I'm going to run our graph here.

13:10.530 --> 13:16.800
And you can see that's exactly what we had on the image for the activity.

13:17.130 --> 13:21.090
So maybe well you can choose node topics all.

13:21.120 --> 13:23.310
Maybe you will have that.

13:23.310 --> 13:27.240
So you can uncheck that things okay.

13:27.270 --> 13:28.830
And maybe also leaf topics.

13:28.830 --> 13:32.820
And you see we have our number publisher node publishing to the number topic.

13:32.850 --> 13:38.730
The number counter node is subscribing to the number topic and publishing to the number count topic.

13:38.730 --> 13:40.620
So we have a data pipeline.

13:40.620 --> 13:43.080
And well that's the end of this activity.

13:43.080 --> 13:48.270
As a reminder, you can find the complete Cplusplus solution at the end of this section.

13:48.270 --> 13:54.390
So you can download the whole workspace, including all the Python and C plus plus code for this section.
