rossvcserver

Create ROS service server

Description

Use rossvcserver or robotics.ros.ServiceServer to create a ROS service server that can receive requests from, and send responses to, a ROS service client. You must create the service server before creating the service client (see ROSSVCCLIENT).

When you create the service client, it establishes a connection to the server. The connection persists while both client and server exist and can reach each other. When you create the service server, it registers itself with the ROS master. To get a list of services, or to get information about a particular service that is available on the current ROS network, use the rosservice function.

The service has an associated message type and contains a pair of messages: one for the request and one for the response. The service server receives a request, constructs an appropriate response based on a call function, and returns it to the client. The behavior of the service server is inherently asynchronous, because it becomes active only when a service client connects to the ROS network and issues a call.

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

Creation

Syntax

server = rossvcserver(servicename,svctype)
server = rossvcserver(servicename,svctype,callback)
server = robotics.ros.ServiceServer(node, name,type)
server = robotics.ros.ServiceServer(node, name,type,callback)

Description

example

server = rossvcserver(servicename,svctype) creates a service server object with the specified ServiceType available in the ROS network under the name ServiceName. The service object cannot respond to service requests until you specify a function handle callback, NewMessageFcn.

server = rossvcserver(servicename,svctype,callback) specifies the callback function that constructs a response when the server receives a request. callback specifies the NewMessageFcn property.

server = robotics.ros.ServiceServer(node, name,type) creates a service server that attaches to the ROS node, node. The server becomes available through the specified service name and type once a callback function handle is specified in NewMessageFcn.

server = robotics.ros.ServiceServer(node, name,type,callback) specifies the callback function which is set to the NewMessageFcn property.

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"

Data Types: char | string

This property is read-only.

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

Example: "gazebo_msgs/GetModelState"

Data Types: char | string

Callback property, specified as a function handle or cell array. In the first element of the cell array, specify either a function handle, string scalar, or character vector representing a function name. In subsequent elements, specify user data.

The service callback function requires at least three input arguments with one output. The first argument, src, is the associated service server object. The second argument, reqMsg, is the request message object sent by the service client. The third argument is the default response message object, defaultRespMsg. The callback returns a response message, response, based on the input request message and sends it back to the service client. Use the default response message as a starting point for constructing the request message. The function header for the callback is:

function response = serviceCallback(src,reqMsg,defaultRespMsg)

Specify the NewMessageFcn property as:

server.NewMessageFcn = @serviceCallback;

When setting the callback, you pass additional parameters to the callback function by including both the callback function and the parameters as elements of a cell array. The function header for the callback is:

function response = serviceCallback(src,reqMsg,defaultRespMsg,userData)

Specify the NewMessageFcn property as:

server.NewMessageFcn = {@serviceCallback,userData};

Object Functions

rosmessageCreate ROS messages

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/.

Introduced in R2015a