Main Content

waitForServer

Wait for action server to start

Description

waitForServer(client) waits until the action server is started up and available to send goals. The IsServerConnected property of the SimpleActionClient shows the status of the server connection. Press Ctrl+C to abort the wait.

example

waitForServer(client,timeout) specifies a timeout period in seconds. If the action server does not start up in the timeout period, this function displays an error.

status = waitForServer(___) returns a status indicating whether the action server is available, using any of the arguments from the previous syntaxes. If the server is not available within the timeout, status will be false, and this function will not display an error.

Examples

collapse all

This example shows how to create a ROS action client and execute the action. Action types must be set up beforehand with an action server running.

You must have set up the '/fibonacci' action type. To run this action server, use the following command on the ROS system:

rosrun actionlib_tutorials fibonacci_server

Connect to a ROS network. You must be connected to a ROS network to gather information about what actions are available. Replace ipaddress with your network address.

ipaddress = '192.168.203.133';
rosinit(ipaddress,11311)
Initializing global node /matlab_global_node_81947 with NodeURI http://192.168.203.1:54283/

List actions available on the network. The only action set up on this network is the '/fibonacci' action.

rosaction list
/fibonacci

Create an action client by specifying the action name. Use structures for ROS messages.

[actClient,goalMsg] = rosactionclient('/fibonacci','DataFormat','struct');

Wait for the action client to connect to the server.

waitForServer(actClient);

The fibonacci action will calculate the fibonacci sequence for a given order specified in the goal message. The goal message was returned when creating the action client and can be modified to send goals to the ROS action server. Set the order to an int32 value of 8.

goalMsg.Order = int32(8);

Send the goal and wait for its completion. Specify a timeout of 10 seconds to complete the action.

[resultMsg,resultState] = sendGoalAndWait(actClient,goalMsg,10);
rosShowDetails(resultMsg)
ans = 
    '
       MessageType :  actionlib_tutorials/FibonacciResult
       Sequence    :  [0, 1, 1, 2, 3, 5, 8, 13, 21]'

Disconnect from the ROS network.

rosshutdown
Shutting down global node /matlab_global_node_81947 with NodeURI http://192.168.203.1:54283/

This example shows how to send and cancel goals for ROS actions. Action types must be setup beforehand with an action server running.

You must have set up the '/fibonacci' action type. To run this action server, use the following command on the ROS system:

rosrun actionlib_tutorials fibonacci_server

First, set up a ROS action client. Then, send a goal message with modified parameters. Finally, cancel your goal and all goals on the action server.

Connect to a ROS network with a specified IP address. Create a ROS action client connected to the ROS network using rosactionclient. Specify the action name. Wait for the client to be connected to the server.

rosinit('192.168.203.133',11311)
Initializing global node /matlab_global_node_18287 with NodeURI http://192.168.203.1:55284/
[actClient,goalMsg] = rosactionclient('/fibonacci','DataFormat','struct');
waitForServer(actClient);

Send a goal message with modified parameters. Wait for the goal to finish executing.

goalMsg.Order = int32(4);
[resultMsg,resultState] = sendGoalAndWait(actClient,goalMsg)
resultMsg = struct with fields:
    MessageType: 'actionlib_tutorials/FibonacciResult'
       Sequence: [0 1 1 2 3]

resultState = 
'succeeded'
rosShowDetails(resultMsg)
ans = 
    '
       MessageType :  actionlib_tutorials/FibonacciResult
       Sequence    :  [0, 1, 1, 2, 3]'

Send a new goal message without waiting.

goalMsg.Order = int32(5);
sendGoal(actClient,goalMsg)

Cancel the goal on the ROS action client, actClient.

cancelGoal(actClient)

Cancel all the goals on the action server that actClient is connected to.

cancelAllGoals(actClient)

Delete the action client.

delete(actClient)

Disconnect from the ROS network.

rosshutdown
Shutting down global node /matlab_global_node_18287 with NodeURI http://192.168.203.1:55284/

Input Arguments

collapse all

ROS action client, specified as a SimpleActionClient object handle. This simple action client enables you to track a single goal at a time.

Timeout period for setting up ROS action server, specified as a scalar in seconds. If the client does not connect to the server in the specified time period, an error is displayed.

Output Arguments

collapse all

Status of the action server start up, returned as a logical scalar. If the server is not available within the timeout period, status will be false.

Note

Use the status output argument when you use waitForServer for code generation. This will avoid runtime errors and outputs the status instead, which can be reacted to in the calling code.

Extended Capabilities

Version History

Introduced in R2019b