This is the solution for the activity.
Number three, we will add a new service server inside the number counter.
Note here that we have created.
In the last section I will only provide a video for the Python solution.
But of course you can find the complete C plus plus solution inside the code archive that you can download
at the end of this section, so you can open the number counter dot p y that we've created before.
And actually before we write anything, let's just go to the terminal and let's check the interface
that we will need to use.
So let's do Ros2 interface show.
And I gave you that one.
I gave you example interfaces slash SRV slash set bool.
So you can see we have some commands first saying that this is like something to test.
And then we have a request here that contains only one field name is data and the type is bool for boolean.
Then we have the three dashes and then we have the response.
So that's what we're going to receive from the server to the client.
So a success flag is going to be true or false.
And then a message.
For example if you have an error you could give more information about what went wrong.
So great.
We know the interface.
So that's the package.
And we know what's inside the interface.
So now we can go back to the code.
And in this number counter dot p y.
Let's add this interface.
So after the other imports here I'm going to do from example interfaces dot.
And here it's sorry because we import a service interface import set bool.
All right.
And well we've already added example interfaces into the package dot XML.
So the dependency for that.
So we don't need to do it anymore.
Then I'm going to add a service server.
Where do I add this service?
Well, just in the constructor after.
Well, for example, we have a publisher a subscriber.
I'm going to add it right here before the end log.
So let's name it self dot.
We want to reset the counter.
So reset counter service for example.
And that's going to be self dot create service.
We need to provide the type set bool.
We need to provide the name.
So what's the name.
Well we decided here because we created the service.
When we create the server and we just want to reset the counter.
So let's call it reset counter.
Let's keep things simple here.
And finally we will need to provide a callback.
Okay.
Because when the node is spinning it can receive requests.
When we receive a request, in order to be able to process it we have to write a callback.
And so let's just add a callback inside the class here.
The order doesn't matter.
Make sure that the function is inside the class and let's name it callback reset counter okay with self.
And then we have the request and the response request response.
Okay.
So as you can see for a subscriber we just get the message.
But for a service server we get the request.
And already the response object.
I'm going to make it explicit here.
So the request is set bull dot request.
And the response is set bull dot response.
All right let's just put pass for now so we can finish this line here.
So I'm going to put self dot callback reset counter.
And let's go to a new line here to make it a bit cleaner.
So you see we create a service with the interface the name and the callback.
Now let's go to the callback and let's implement the callback.
So what do we want to do?
You see we receive here a wool data.
So let's say that we want to reset the counter if this is true okay.
So if you provide false we are not going to reset the counter.
So I can do if request dot data.
If this is true then we do self dot counter.
So the counter is here is equal to zero just as simple as that.
So you see we initialize the counter to zero when we start the node.
Then whenever we receive a message here on the number topic we're going to add the data to the counter.
But if we receive a request for this service server here reset counter, we also reset the counter to
zero.
And then well that's the action okay.
The action is if we request to reset the counter.
Then we reset the counter to zero.
But then we need to also provide a response for the client.
So response dot.
And we have two things we have success and message.
So let's do response dot.
Success is equal to true and response.
Let's add a message.
You could just leave it empty but let's just say okay.
Or let's say counter has been reset.
All right.
And let's add an else.
Okay.
If the data here is false, then we are not going to reset the counter.
And we are going to put the response dot success for example false.
Just for the example and response dot message let's say counter has not been reset.
Well here it's just because you provide it false in the request.
But if, for example, you would have a service to enable a motor or something like that, then you
would probably need to do some checks.
And if some checks are failing, then you could return a response with a message that explained what
was the issue.
Okay, so that's it.
But actually we need to return the response.
So very important.
Okay.
Don't forget that.
Otherwise you will get an error when you run the service.
Okay.
So you see we just do the action.
We fill the response and we return the response.
And basically that's it.
That's it for this activity, for the code.
It's not much to do.
Okay.
So to recap we have added the interface here.
And then we create a service server inside the constructor.
And we have a callback to handle incoming requests.
Now I'm going to save that.
We don't need to do anything else in the setup.py because we already have the number counter executable
here, so nothing to do.
Let's go back to the terminal and well let's build it again because I'm not sure if last time I built
was with Simulink install called build packages select and the Python package.
Okay.
And I'm just going to start new terminals so that everything is sourced correctly.
And to test this well I'm gonna start first the number publisher.
So Ros to run number um that's my Pi PG with number publisher here I'm going to start the number counter
here Ros to run.
With number counter I start with two because well the counter is only going to be increased if we receive
data on the number topic.
So we also need the number publisher.
So let's see what we have here Ros2 service list.
So you see we have lots of services because we have those that are automatically generated.
But then we find the reset counter okay Ros2 service type with reset counter.
And we find set bool interface okay.
So let's make a simple test here.
Let's clear that and we can do Ros2 service call with.
So that's reset counter.
And then example interfaces set bool.
And we had a field named data which is supposed to be a boolean.
So let's put true.
And you see we have here success true Message counter has been reset, but we don't see anything because,
well, to see something we need to do a rostopic echo with the number count and you can see the counter
going up.
And then if I call the service again, you will see that we start at zero again.
You see we went back to zero okay.
Every time I run the service, every time I make a request we start at zero.
Now let's say I make a request, but I put data false.
In this case, the service is still working correctly.
But you see, because we have provided false.
And then in the code we said that if it's false we don't reset the counter.
Then you see here we receive success.
False counter has not been reset and you can see it also here the counter is not reset.
All right.
So with those two cases, you can see the application is working.
All right.
And if you want you can also create some code for the client.
So you could use the for example add to in clients no OOP.
And modify this to create a simple script to call this new service.
Or you could also use that one with object oriented programming.
So you can do that if you want to practice a bit more.
And the activity is now finished.
Once again, you can download all the code at the end of this section.
Okay.
The C plus plus solution is going to be exact same stuff okay.
Just using the C plus plus syntax.
And just a final note here.
If I come back to this number counter, you can see that in a node you can have many different things.
Here we have one publisher one subscriber one service server okay.
So you can have any combination of those.
You can create as many as you want.
Just make sure that they all have a different name.