Main Content

call

Call the ROS service server and receive a response

Description

example

response = call(serviceclient) sends a default service request message and waits for a service response. The default service request message is an empty message of type serviceclient.ServiceType.

example

response = call(serviceclient,requestmsg) specifies a service request message, requestmsg, to be sent to the service.

example

response = call(___,Name,Value) provides additional options specified by one or more Name,Value pair arguments, using any of the arguments from the previous syntaxes.

Examples

collapse all

Connect to a ROS network.

rosinit
Launching ROS Core...
.Done in 1.0719 seconds.
Initializing ROS master on http://192.168.0.10:60855.
Initializing global node /matlab_global_node_30809 with NodeURI http://bat800205glnxa64:43225/

Set up a service server and client. Use structures for the ROS message data format.

server = rossvcserver('/test', 'std_srvs/Empty', @exampleHelperROSEmptyCallback,...
                      'DataFormat','struct');
client = rossvcclient('/test','DataFormat','struct');

Call service server with default message.

response = call(client)
response = struct with fields:
    MessageType: 'std_srvs/EmptyResponse'

Shut down the ROS network.

rosshutdown
Shutting down global node /matlab_global_node_30809 with NodeURI http://bat800205glnxa64:43225/
Shutting down ROS master on http://192.168.0.10:60855.

Connect to a ROS network.

rosinit
Launching ROS Core...
.Done in 1.0534 seconds.
Initializing ROS master on http://192.168.0.10:59273.
Initializing global node /matlab_global_node_00005 with NodeURI http://bat800205glnxa64:46751/

Set up a service server and client. This server calculates the sum of two integers and is based on a ROS service tutorial.

sumserver = rossvcserver('/sum','roscpp_tutorials/TwoInts',@exampleHelperROSSumCallback);
sumclient = rossvcclient('/sum');

Get the request message for the client and modify the parameters.

reqMsg = rosmessage(sumclient);
reqMsg.A = 2;
reqMsg.B = 1;

Call service and get a response. The response should be the sum of the two integers given in the request message. Wait 5 seconds for the service to time out.

response = call(sumclient,reqMsg,'Timeout',5)
response = 
  ROS TwoIntsResponse message with properties:

    MessageType: 'roscpp_tutorials/TwoIntsResponse'
            Sum: 3

  Use showdetails to show the contents of the message

Shut down the ROS network.

rosshutdown
Shutting down global node /matlab_global_node_00005 with NodeURI http://bat800205glnxa64:46751/
Shutting down ROS master on http://192.168.0.10:59273.

Input Arguments

collapse all

Service client, specified as a ServiceClient object handle.

Request message, specified as a Message object handle or structure. The default message type is serviceclient.ServiceType.

Note

In a future release, ROS Toolbox will use message structures instead of objects for ROS messages.

To use message structures now, set the "DataFormat" name-value argument to "struct". For more information, see ROS Message Structures.

Name-Value Pair Arguments

Specify optional comma-separated pairs of Name,Value arguments. Name is the argument name and Value is the corresponding value. Name must appear inside quotes. You can specify several name and value pair arguments in any order as Name1,Value1,...,NameN,ValueN.

Example: "TimeOut",5

Timeout for service response in seconds, specified as a comma-separated pair consisting of "Timeout" and a scalar. If the service client does not receive a service response and the timeout period elapses, call displays an error message and lets MATLAB® continue running the current program. The default value of inf prevents MATLAB from running the current program until the service client receives a service response.

Message format, specified as "object" or "struct". Set this property on creation of the service client using the name-value input. For more information, see ROS Message Structures.

Output Arguments

collapse all

Response message sent by the service server, returned as a Message object handle or structure.

Compatibility Considerations

expand all

Behavior change in future release

Introduced in R2019b