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
Initializing ROS master on http://bat584805glnxa64:34429/.
Initializing global node /matlab_global_node_40297 with NodeURI http://bat584805glnxa64:41515/

Set up a service server and client.

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

Call service server with default message.

response = call(client)
response = 
  ROS EmptyResponse message with properties:

    MessageType: 'std_srvs/EmptyResponse'

  Use showdetails to show the contents of the message

Shut down the ROS network.

rosshutdown
Shutting down global node /matlab_global_node_40297 with NodeURI http://bat584805glnxa64:41515/
Shutting down ROS master on http://bat584805glnxa64:34429/.

Connect to a ROS network.

rosinit
Initializing ROS master on http://HYD-JPRABAKA:58371/.
Initializing global node /matlab_global_node_13656 with NodeURI http://HYD-JPRABAKA:58375/

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_13656 with NodeURI http://HYD-JPRABAKA:58375/
Shutting down ROS master on http://HYD-JPRABAKA:58371/.

Input Arguments

collapse all

Service client, specified as a ServiceClient object handle.

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

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.

Output Arguments

collapse all

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

See Also

Introduced in R2019b