Let's now do the second step for this activity.
We already have a number publisher node.
We are going to create a second node.
So a second file where we have another node in which we are going to subscribe to this number topic.
And then we are going to add every number that we receive to a counter.
And we are going to publish this counter to a new topic.
Okay.
So now that we have the publisher working, we know that we should receive the correct data on the subscriber
side.
So let's create a new file.
Let's go back to our.
So package my pi pkg.
And in this folder where we have all the files.
And let's do a number counter dot pi which we also make executable number counter.
And it's right there.
I'm also going to remove the pi cache.
No need for that.
And let's start this node.
That's going to be number counter.
We are going to need the template.
So let's put the template here and let's name it number counter node.
Let's also modify it here.
I'll remove that comment and the name of the node let's say also number counter.
Okay.
Let's keep things simple.
And then what we are going to create a subscriber.
So to create a subscriber we will need a name and a message type.
What's the message or the interface type.
Well it's very easy.
It is going to be the exact same that we have on the publisher okay.
Otherwise they won't be able to communicate with each other.
So from example interfaces dot msg import int 64.
And we've already added that inside the package dot XML.
So no need to do anything else.
Great.
So I'm going to start with the subscriber.
Okay.
That's the first thing we need to do here.
I'm going to create a subscriber here.
Self dot.
Let's name it simply number subscriber.
Subscriber like that is equal to self dot create subscription.
And what do we need to give first the message type which is going to be int 64.
Then we have the topic name.
What is the topic name.
Well topic name you see here is number.
So you write number exactly okay.
If you have any typo here that's not going to work.
And then we need to provide a callback okay.
So I'm going to write a callback first here.
Let's name it simply callback.
So I start with callback.
And then I'm going to put the name of the topic or something similar.
So callback number we receive.
So we have self and then we receive a message.
And I'm just going to add this to specify that this message is an int 64.
That's going to help with the auto completion.
And well I'm first going to do a log just to make sure that this subscriber is working okay.
If you have a doubt you can always do it step by step.
So let's do self get logger info and we should be able to just publish the message like that message
dot data.
That should work.
So then we can go back to our create subscription.
Here we put self dot callback number.
And then we have the queue size because it's a bit long I'm going to press enter here.
All right.
And we have our subscriber here.
We have registered a callback which is that method.
So when the node is spinning whenever we receive a message on the number topic, we are going to call
this method.
So let's try that.
Actually, let's just try this very simple code.
To try this, I'm going to go back to my setup.py and add a new executable named number counter is equal
to my pi pkg dot file, name is number counter and the function name is main.
Okay.
And make sure you have a comma okay.
Every time.
Now that I have this let's go back.
Let's go to our roster workspace and let's run the command.
So it should be here call code build.
We are going to do this package only and symlink install okay.
It has been built.
So right here I already have my publisher running okay.
So you can study it again.
Or you can just keep it running.
And in that terminal I'm going to source.
I'm going to source because I have just built the package.
Okay.
I have a new node, so I need to source it.
And let's also source that one just in case we use it later.
And let's do Ros to run my pi PG with number.
And this time it's going to be number counter.
Okay.
So we make sure that we find the executable with the autocompletion.
And then we make sure that it's working.
And you see we have an error.
And it seems the error is with the log.
Yeah probably if I go back well probably I need to cast this as a string.
Okay.
Otherwise it doesn't really like it.
So let's save and let's go back.
I have used Simulink install and I have source this terminal, so I should be able to run it again.
And this time it's working.
Okay.
We receive the number two every one second.
You can verify with the log.
That's every one second.
So let's stop that.
And now we know that this subscriber is working.
So we have the publisher working.
The subscriber is working.
We don't really need this log anymore okay.
I was just to test that.
It's working.
Now what we want to do is to add all the numbers that we get inside a counter, and then we want to
publish that counter on a new topic.
So let's first create a counter.
That's quite easy.
We do self dot counter is equal to zero here.
And then we do self dot counter plus equal message dot data okay.
We just add the data.
So the number we receive to the counter which starts at zero as simple as that.
And then we want to publish this counter.
So let's create a new publisher for example here Let's do self dot.
So we're going to publish what.
A number count or number counter.
Let's do number count.
Publisher is equal to self.
Create publisher.
And we need to decide because that's a new topic.
We need to decide on a message type and a name.
What's going to be the message type.
So I didn't give you the message type.
But.
Well if we were using Int64 to publish a number and then we add all those numbers inside the counter.
So that's still that's still a number.
That's the same data type.
Okay.
So it's still an integer.
So we can use the same.
We can use int 64.
All right.
Let's not make it complicated.
If we have the same data type we use the same message.
And then we need to decide on a name.
So I'm going to name it number count.
And we need a size ten.
Let's go back to a new line here.
All right.
So in our constructor we have what we have a publisher and we have a subscriber.
Now how are we going to publish to this topic.
So that's going to be the most interesting part of this activity is that we are not going to publish
from a timer, because what we've seen so far, for example, in the number publisher, is that you
create a publisher and then how to publish to this topic.
Well, we create a timer and we say every x amount of time we just publish something.
But you can publish from anywhere, so you can publish from a timer callback, but you can also publish
from a subscriber callback.
And here it makes sense because we want to publish the counter when it's updated.
And when do we update the counter?
Well, we update it when we receive a new number.
So in this subscriber callback we are going to create a new message and to publish it.
So here I can do.
So I'm not going to do MSG I'm not going to name it.
Message like that because we already have.
Message here.
So I'm going to do.
New message that's going to be an INT 64.
Because that's what we have here as well.
And then new message dot data is equal to self counter.
And then we can just do self dot number count.
Publisher dot publish with the new message.
All right.
So this way we only publish when we receive a message here with this subscriber callback.
And this way it's kind of a data pipeline.
Okay.
You receive a message and as soon as you receive a message, you can process it.
You can do what you can do different kind of stuff.
And then you send another message on another topic.
And here, well, I just write this code here in the callback number, I could have created another
function named for example publish number counter so you can do as you want.
Here I'm just going to keep it like this.
And let's add a log self get logger info.
Number counter has been studied.
All right let's save the file.
And that's about it for the code.
So to recap when we create the node here we create a publisher and a subscriber.
We register a callback with the subscriber.
Okay.
So when the node is spinning, whenever we receive a message on the number topic, we're going to go
to this callback.
And when we go to this callback, we add the number to the counter.
And we publish this counter to the number count topic.
So that's also a very common structure in robotics.
Now we can try it.
So no need to compile anything.
We've used Simulink install I'm just going to stop the publisher And let's start the subscribe.
So the number counter again and you see number counter has been started.
We have nothing actually we don't print anything here.
So we will need to check the topics.
So let's go here.
Let's do Ros two topic list.
And you see that we have a number topic and a number count topic.
If I do Ros two topic equal number.
Well we have nothing because now what is publishing on this number topic.
Okay.
And if I do number count we also have nothing.
Okay.
Because nobody is publishing on that.
All right we need to start the number publisher.
Okay.
So number publisher has been started.
And then the number counter is receiving the messages and adding them to a counter and publishing to
the counter.
So let's just print again.
What do we have on the number topic.
So you see that's what's published here.
Data two.
And then let's do a Ross two topic equal with the number count.
And well at this time you see the counter is 54 and 56.
And then plus two every time.
All right.
You can see if I stop for example the number publisher, we don't receive any new number here.
So we don't publish any new counter data okay.
If I start the number publisher again we continue okay, 7678, etc..
Now if I stop the number counter, you see, if I stop the number counter, well, we don't publish
anything else on the number count topic.
And if I start it again.
Then it's going to receive messages from there.
And you see we have two four.
So this counter starts again at zero.
All right.
If you start this node again, you see, when we start the node again, we reset the counter to zero.
Great.
Let's.
So the two are running.
Now I'm going to run our graph here.
And you can see that's exactly what we had on the image for the activity.
So maybe well you can choose node topics all.
Maybe you will have that.
So you can uncheck that things okay.
And maybe also leaf topics.
And you see we have our number publisher node publishing to the number topic.
The number counter node is subscribing to the number topic and publishing to the number count topic.
So we have a data pipeline.
And well that's the end of this activity.
As a reminder, you can find the complete Cplusplus solution at the end of this section.
So you can download the whole workspace, including all the Python and C plus plus code for this section.