isServerAvailable
Description
Examples
Connect to a ROS network.
rosinit
Launching ROS Core... .................Done in 18.4449 seconds. Initializing ROS master on http://172.19.136.75:57095. Initializing global node /matlab_global_node_56016 with NodeURI http://vdi-wd1bgl-223:50018/ and MasterURI http://localhost:57095.
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 = numCallFailures + 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_56016 with NodeURI http://vdi-wd1bgl-223:50018/ and MasterURI http://localhost:57095. Shutting down ROS master on http://172.19.136.75:57095.
Create a sample ROS 2 network with two nodes.
node_1 = ros2node('node_1_service_server'); 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 = numCallFailures + 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
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 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
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)