Services¶
You can create a ServiceClient
using the Ros2 Singleton
Here’s a short modified example of the service example provided in the
Examples.
Button {
property var serviceClient: Ros2.createServiceClient("/add_two_ints", "example_interfaces/srv/AddTwoInts")
onClicked: {
var result = serviceClient.sendRequestAsync(
{ a: 1, b: 3 },
function (result) {
textResult.text = !!result ? ("Result: " + result.sum) : "Failed"
})
}
}
In the first step, a service client is created. Here, the first argument is the service
that is called and the second is the type
of the service.
When the button is clicked, the service client is used to send a request with the first argument
being the request
and the second is a callback that is called once the service returns.
The callback receives the result of the service call or the boolean value false
if the
call failed.
The code will continue execution while the service call is processed, hence, if you want to
prohibit concurrent calls to the same service, you’ll have to add your own logic to check
whether a service call is currently active.
API¶
-
class ServiceClient : public QObjectRos2¶
Public Functions
-
ServiceClient(QString name, QString type)¶
- Parameters:
name – The service topic.
type – The type of the service, e.g., “example_interfaces/srv/AddTwoInts”
-
bool isServiceReady() const¶
Returns whether the service is ready.
-
void sendRequestAsync(const QVariantMap &req, const QJSValue &callback)¶
Calls a service asynchronously returning immediately. Once the service call finishes, the optional callback is called with the result if provided.
- Parameters:
req – The service request, i.e., a filled request message of the service type.
callback – The callback that is called once the service has finished. If the request failed, the callback is called with false.
Properties
- bool ready
True if the ServiceClient is connected to the Service and the Service is ready, false otherwise.
-
ServiceClient(QString name, QString type)¶