Let's now create a Cplusplus service client for the service server we have created just before.
And as we did with Python, we will first start with kind of a script with no OOP to see the different
steps of the client, and then we will integrate the client inside an OOP node in the next video.
So let's create.
Let's go to our workspace the C plus plus package and source.
Let's add an add to its client no OOP dot cpp.
So I'm using the same name here.
We are going to create the client in that file.
Let's go back to VS code.
And we have the file here I'm still going to start from the template but I'm going to modify it.
So I take the template code.
And I'm not going to use this class.
Okay.
I'm just going to create a node that's going to be this time an rcl cp node.
Okay I need to provide the name add to hint client.
Let's name it no OP.
Okay.
And I'm going to remove this pin as well.
We're not going to make the speed like that.
And let's actually save to get the Autocompletion for Slcp.
Okay.
And let's start from this and let's create a client.
So we will start by initializing Ros2 communications.
Then we create a node.
It's going to be a shared pointer to a node.
We're going to write our code.
And at the end we shut down Ros2.
So let's create a client I'm going to just use the auto keyword.
Again client is equal to and we're going to use the node object here.
And in the node we have the functionality create client as simple as that.
And we need, of course, to provide an interface.
So I'm also going to include the interface which is example interfaces solve and add to instance HPP.
And let's let's save right now to get the auto completion okay.
So it's important that you use the same interface in the server and in the client of course.
So when we create the client it's going to be a template for the interface.
So we provide an interface here.
Example interfaces solve add to ints like that.
And then we need to open the parentheses.
And in this that's going to be the name of the service.
So if we go back here what was the name of the service.
It's add to int right here.
So I will just use the same name for the client and the server so that they can communicate.
All right.
So two things.
To create the client you need the interface and the a name as always.
Then we are going to also wait for the service to be up, because if you try to send the request and
the server is not up, then you're going to get an error.
So let's do here a while.
Not client.
Wait.
So you see I use an error because of course that's going to be a shared pointer.
So wait for service and we need to provide a timeout.
I'm just going to be using here namespace std chrono literals.
And let's say one second.
So basically wait for service is going to return true if the service is found.
But then if it's not found it's going to wait.
If you provide a timeout it's going to wait until the timeout.
So one second if after one second the service is not found, that's going to return false.
And so with the negation here is going to become true.
And so as long as the server is not up every one second, we can do something.
For example, print clcp.
This time I can use Clcp one okay node get logger and we're going to print waiting for the server okay.
You can just add whatever you want okay.
And after this line we know that the server is found.
So if the server is found we can create a request and send a request I'm going to do auto request is
equal to.
And we are going to create a request object.
And actually not just a request object.
But we need to do STD make shared.
So we're going to create a shared pointer to a request object which is example interfaces SRB And two
ints and then we add requests.
You see we have request and response.
So we have two messages inside this interface.
Close the bracket and then we have parentheses.
And so here is a quite big difference when you create a request.
So in C plus plus when you create a request message or if you create if I go back to for example the
number publisher.
You see when you create a message for a topic, you see we don't create a shared pointer.
We just create the object itself and then we publish it.
That's the difference here in a service client we create a shared pointer of the request.
So this is a small inconsistency here in C plus plus okay.
And now we have our request we can request a with an arrow is equal to.
Let's give a number six.
For example request B is equal to two.
So the sum should be eight.
and then we are going to use the client to send the request to the server.
And here we have the async send request.
So we're going to use the asynchronous call so that then we can spin until we get the response.
So async send request I give the shared pointer to the.
So I give the request here which is the shared pointer to the request object.
And that's going to return a future.
And I can use the auto keyword again.
You see that's going to be a future here.
Well that's a that's a client future object something that's specific to Ros2.
And then I can do a clcp spin but not spin spin until future complete.
And I will need to pass first the node because we need to make the node spin.
And for what future?
For the future we have just created here, which is the one we get from the async send request function.
All right.
And then after this returns then we can try to get the response.
So let's do auto response is equal to future dot get okay.
That's how you get the response.
And you can see here we get a shared pointer.
Actually not just the response but a shared pointer to the add to insert response.
All right.
And then if you want you can print a CSP info this get logger.
Oh actually it's not this it's node.
We are not in a class.
We use the node here.
And then I think I've already written that here.
You see we can just do the sum.
I'm just going to take that code and use it there Okay.
So we print the request A plus.
The request b is equal to the response sum that we get after the spinning.
All right.
And the program is complete.
Let's save.
Let's just do a quick recap.
So we initialize rows to communications.
We start a node.
From the node we create a client.
We wait for the service to be up.
After the service is up we can create a request.
We send the request asynchronously.
And then we're going to make the node spin until that future is complete okay.
So the future we get after we send the request then we get the request from the future.
And well then you just do whatever you want with the response.
Let's add an executable to test that.
So another one here add executable add to its client.
No, OOP is quite a long name from SK.
Add two ints client no oop cpp.
That's the file name and an amount target dependencies with this executable name and what dependencies
we need are clcp of course and example interfaces.
Let's not forget to install the executable.
Add two ints client.
No OOP here.
Okay so once again we add those two lines and that one here.
Let's save everything.
And let's go back to the terminal to build the package.
So let's do a call on build I should have the the command with packages.
Select my C plus plus package.
Okay.
And let's well let's use those terminals I'm just going to use that one.
Actually the both terminals, because we have just built and we have a new node.
And let's start here.
Ros to run my CPP package with add two ints client no OOP.
So that's the C plus plus one right.
That's the one we've just written.
And this we should see a log a warning log waiting for the server because there is no server.
Now I'm going to start the server here.
So the C plus plus server for example.
And you see as soon as I start the server it's been found on the client.
The client will send a request.
It's received by the server which processes the request, sends back the response, and then we get
the response on the client side.
Great.
So you have seen the mechanism for the C plus plus client, which is exactly the same as the Python
one.
Okay.
That's the same behavior as we did for the Python client.
And to recap here this code.
So that's not a normal OOP code for rust, but this code is very useful to test services very fast.
Okay, if you have a service server and a request is a bit too complicated to use in the terminal,
then you just use this as a template and you can just test your server.
In fact, for just that script, you could even just use the Python script.
Okay, you don't even need this C plus.
Plus that was just for the example.
But you could use the where is it that script here in Python if you have written that okay.
Because it's just for testing purposes, you're not going to use that in your application in your end
application.
So for just testing purposes you could just use the Python one.
And why is that?
Because if you use Simulink install then you can just modify the code and make different tests.
And you don't need to build every time.
So it's going to be much faster.
Okay.
So for this example the C plus plus one is actually not really needed.
But now you know how to write a C plus plus client without OOP.
Let's see the OOP version which is what you will use in the nodes that you write for your future applications.