rossvcclient

Connect to ROS service server

Description

Use rossvcclient or ros.ServiceClient to create a ROS service client object. This service client uses a persistent connection to send requests to, and receive responses from, a ROS service server. The connection persists until the service client is deleted or the service server becomes unavailable.

Use the ros.ServiceClient syntax when connecting to a specific ROS node.

Creation

Description

example

client = rossvcclient(servicename) creates a service client with the given ServiceName that connects to, and gets its ServiceType from, a service server. This command syntax prevents the current MATLAB® program from running until it can connect to the service server.

client = rossvcclient(servicename,Name,Value) provides additional options specified by one or more Name,Value pair arguments.

[client,reqmsg] = rossvcclient(___) returns a new service request message in reqmsg, using any of the arguments from previous syntaxes. The message type of reqmsg is determined by the service that client is connected to. The message is initialized with default values. You can also create the request message using rosmessage.

example

client = ros.ServiceClient(node, name) creates a service client that connects to a service server. The client gets its service type from the server. The service client attaches to the ros.Node object handle, node.

client = ros.ServiceClient(node, name,"Timeout",timeout) specifies a timeout period in seconds for the client to connect the service server.

Properties

expand all

This property is read-only.

Name of the service, specified as a string scalar or character vector.

Example: "/gazebo/get_model_state"

This property is read-only.

Type of service, specified as a string scalar or character vector.

Example: "gazebo_msgs/GetModelState"

Object Functions

rosmessageCreate ROS messages
callCall the ROS service server and receive a response

Examples

collapse all

Connect to a ROS network.

rosinit
Initializing ROS master on http://bat1012801glnxa64:46529/.
Initializing global node /matlab_global_node_96100 with NodeURI http://bat1012801glnxa64:46537/

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_96100 with NodeURI http://bat1012801glnxa64:46537/
Shutting down ROS master on http://bat1012801glnxa64:46529/.

Create a ROS service serve by creating a ServiceServer object and use ServiceClient objects to request information over the network. The callback function used by the server takes a string, reverses it, and returns the reversed string.

Start the ROS master and node.

master = ros.Core;
node = ros.Node('/test');
Using Master URI http://localhost:43163 from the global node to connect to the ROS master.

Create a service server. This server expects a string as a request and responds with a string based on the callback.

server = ros.ServiceServer(node,'/data/string',...
                                    'roseus/StringString');

Create a callback function. This function takes an input string as the Str property of req and returns it as the Str property of resp. The function definition is shown here, but is defined below the example. req is a ROS message you create using rosmessage.

function [resp] = flipString(~,req,resp)
% FLIPSTRING Reverses the order of a string in REQ and returns it in RESP.
resp.Str = fliplr(req.Str);
end

Assign the callback function for incoming service calls.

server.NewRequestFcn = @flipString;

Create a service client and connect to the service server. Create a request message based on the client.

client = ros.ServiceClient(node,'/data/string');
request = rosmessage(client);
request.Str = 'hello world';

Send a service request and wait for a response. Specify that the service waits 3 seconds for a response.

response = call(client,request,'Timeout',3)
response = 
  ROS StringStringResponse message with properties:

    MessageType: 'roseus/StringStringResponse'
            Str: 'dlrow olleh'

  Use showdetails to show the contents of the message

The response is a flipped string from the request message.

Clear the service client, service server, and ROS node. Shut down the ROS master.

clear('client', 'server', 'node')
clear('master')
function [resp] = flipString(~,req,resp)
% FLIPSTRING Reverses the order of a string in REQ and returns it in RESP.
resp.Str = fliplr(req.Str);
end

Introduced in R2019b