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.
选择网站
选择网站以获取翻译的可用内容,以及查看当地活动和优惠。根据您的位置,我们建议您选择:。
您也可以从以下列表中选择网站:
如何获得最佳网站性能
选择中国网站(中文或英文)以获得最佳网站性能。其他 MathWorks 国家/地区网站并未针对您所在位置的访问进行优化。
美洲
- América Latina (Español)
- Canada (English)
- United States (English)
欧洲
- 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)