rossvcclient

Connect to ROS service server

Description

Use rossvcclient or robotics.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 robotics.ros.ServiceClient syntax when connecting to a specific ROS node.

Creation

Syntax

client = rossvcclient(servicename)
client = rossvcclient(servicename,Name,Value)
[client,reqmsg] = rossvcclient(___)
client = robotics.ros.ServiceClient(node, name)
client = robotics.ros.ServiceClient(node, name,"Timeout",timeout)

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 blocks 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 wutg.

example

client = robotics.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 robotics.ros.Node object handle, node.

client = robotics.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://ah-sradford:11311/.
Initializing global node /matlab_global_node_03934 with NodeURI http://ah-sradford:61301/

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)
A service client is calling

response = 

  ROS EmptyResponse message with properties:

    MessageType: 'std_srvs/EmptyResponse'

  Use showdetails to show the contents of the message

Shut down ROS network.

rosshutdown
Shutting down global node /matlab_global_node_03934 with NodeURI http://ah-sradford:61301/
Shutting down ROS master on http://ah-sradford:11311/.

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 = robotics.ros.Core;
node = robotics.ros.Node('/test');

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

server = robotics.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. You must create and save this function separately. req is a ROS message you create using rosmessage.


% Copyright 2015 The MathWorks, Inc.

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


Save this code as a file named flipString.m to a folder on your MATLAB® path.

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 = robotics.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')

Introduced in R2015a