ros2svcserver
Description
Use ros2svcserver
to create a ROS 2 service server that can
receive requests from, and send responses to, a ROS 2 service client.
When you create the service server, it registers itself with the ROS 2 network. When you
create a service client, it establishes a connection to the server. The connection persists
while both client and server exist and can reach each other. To get a list of services, or to
get information about a particular service that is available on the current ROS 2 network, use
the ros2
function.
The service has an associated message type that 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 callback 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 2 network and issues a call. For more information, see Call and Provide ROS 2 Services.
Creation
Syntax
Description
creates a service server of the specified server
= ros2svcserver(node
,servicename
,servicetype
,callback
)servicetype
available in
the ROS 2 network under the name servicename
. It attaches the
server to the ROS 2 node specified by the ros2node
object, node
. It also specifies the
callback
function, which is set to the
NewRequestFcn
property. The input arguments
servicename
and servicetype
, are set to the
ServiceType
and ServiceName
properties,
respectively.
sets the rest of the properties based
on the additional options specified by one or more server
= ros2svcserver(___,Name=Value)Name=Value
pair
arguments, using the arguments from the previous syntax.
Properties
Object Functions
ros2message | Create ROS 2 message structures |
Examples
Tips
ROS 2 service servers cannot communicate errors in callback execution directly to clients. In such situations, the servers only return the default response without any indication of failure. Hence, it is recommended to use try-catch blocks within the callback function, and set specific fields in the response message to communicate the success/failure of the callback execution on the server side.
Extended Capabilities
Version History
Introduced in R2021b