isServerAvailable
Description
Examples
Call Service Client with Default Message
Connect to a ROS network.
rosinit
Launching ROS Core... .Done in 1.2771 seconds. Initializing ROS master on http://172.20.220.163:57900. Initializing global node /matlab_global_node_01450 with NodeURI http://dcc821001glnxa64:32977/ and MasterURI http://localhost:57900.
Set up a service server. Use structures for the ROS message data format.
server = rossvcserver('/test', 'std_srvs/Empty', @exampleHelperROSEmptyCallback,... 'DataFormat','struct'); client = rossvcclient('/test','DataFormat','struct');
Check whether the service server is available. If it is, wait for the service client to connect to the server.
if(isServerAvailable(client)) [connectionStatus,connectionStatustext] = waitForServer(client) end
connectionStatus = logical
1
connectionStatustext = 'success'
Call service server with default message.
response = call(client)
response = struct with fields:
MessageType: 'std_srvs/EmptyResponse'
If the call
function above fails, it results in an error. Instead of an error, if you would prefer to react to a call failure using conditionals, return the status
and statustext
outputs from the call function. The status
output indicates if the call succeeded, while statustext
provides additional information.
numCallFailures = 0; [response,status,statustext] = call(client,"Timeout",3); if ~status numCallFailures = numCallFailues + 1; fprintf("Call failure number %d. Error cause: %s\n",numCallFailures,statustext) else disp(response) end
MessageType: 'std_srvs/EmptyResponse'
Shut down the ROS network.
rosshutdown
Shutting down global node /matlab_global_node_01450 with NodeURI http://dcc821001glnxa64:32977/ and MasterURI http://localhost:57900. Shutting down ROS master on http://172.20.220.163:57900.
Call ROS 2 Service Client With a Custom Callback Function
Create a sample ROS 2 network with two nodes.
node_1 = ros2node('node_1_service_client'); node_2 = ros2node('node_2_service_client');
Set up a service server and attach it to a ROS 2 node. Specify the callback function flipstring
, which flips the input string. The callback function is defined at the end of this example.
server = ros2svcserver(node_1,'/test','test_msgs/BasicTypes',@flipString);
Set up a service client of the same service type and attach it to a different node.
client = ros2svcclient(node_2,'/test','test_msgs/BasicTypes');
Wait for the service client to connect to the server.
[connectionStatus,connectionStatustext] = waitForServer(client)
connectionStatus = logical
1
connectionStatustext = 'success'
Create a request message based on the client. Assign the string to the corresponding field in the message, string_value
.
request = ros2message(client);
request.string_value = 'hello world';
Check whether the service server is available. If it is, send a service request and wait for a response. Specify that the service waits 3 seconds for a response.
if(isServerAvailable(client)) response = call(client,request,'Timeout',3); end
The response is a flipped string from the request message which you see in the string_value
field.
response.string_value
ans = 'dlrow olleh'
If the call
function above fails, it results in an error. Instead of an error, if you would prefer to react to a call failure using conditionals, return the status
and statustext
outputs from the call function. The status
output indicates if the call succeeded, while statustext
provides additional information.
numCallFailures = 0; [response,status,statustext] = call(client,request,"Timeout",3); if ~status numCallFailures = numCallFailues + 1; fprintf("Call failure number %d. Error cause: %s\n",numCallFailures,statustext) else disp(response.string_value) end
dlrow olleh
The callback function used to flip the string is defined below.
function resp = flipString(req,resp) % FLIPSTRING Reverses the order of a string in REQ and returns it in RESP. resp.string_value = fliplr(req.string_value); end
Input Arguments
client
— ROS service client
ros.ServiceClient
object handle | ros2serviceclient
object handle
ROS or ROS 2 service client, specified as a ros.ServiceClient
or
ros2serviceclient
object handle, respectively. This service client
enables you to send requests to the service server.
Output Arguments
status
— Status of service server availability
logical
scalar
Status of service server availability, returned as a logical
scalar. If a server of the same name and type as client
is not
available, status
will be false
.
Extended Capabilities
C/C++ Code Generation
Generate C and C++ code using MATLAB® Coder™.
Usage notes and limitations:
Supported only for the Build Type,
Executable
.Usage in MATLAB Function block is not supported.
Version History
Introduced in R2021b
MATLAB Command
You clicked a link that corresponds to this MATLAB command:
Run the command by entering it in the MATLAB Command Window. Web browsers do not support MATLAB commands.
Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)
Asia Pacific
- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)