This example shows how to send and cancel ROS 2 action goals. Action types must be set up beforehand with an action server running. This example uses the /fibonacci
action. Follow these steps to set up the action server:
Set Up ROS 2 Action Client
Create a ROS 2 node.
Create an action client for /fibonacci
action by specifying the node, action name, and action type. Set the quality of service (QoS) parameters.Wait for the action client to connect to the server.
Before sending the goal, define the callback options framework for the goal execution process. In this example, you specify a callback function to execute when the server returns a feedback response.
Send and Cancel Goals
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.
Create a new goal message and set the order to an int32
value of 10.
Send both the goals to the action server using the sendGoal
function. Specify the same callback options for both goals.
Goal with GoalUUID ca8dbca2b8608a6f2add01b298f6930 accepted by server, waiting for result!
Partial sequence feedback for goal ca8dbca2b8608a6f2add01b298f6930 is 0 1 1
Goal with GoalUUID f493913f4acd2224f31145ae74bbc35 accepted by server, waiting for result!
Partial sequence feedback for goal f493913f4acd2224f31145ae74bbc35 is 0 1 1
Cancel the specific goal associated with the sequence order 8
. Use the goal handle object associated with that goal as input to the cancelGoal
function, and specify the cancel callback to execute once the client receives the cancel response. This function returns immediately without waiting for the cancel response to arrive.
Goal ca8dbca2b8608a6f2add01b298f6930 is cancelled with return code 0
You can wait until the cancel response arrives from the server by using the cancelGoalAndWait
function. Cancel the goal associated with the sequence order of 10 and wait until the client receives the cancel response.
cancelResponse = struct with fields:
MessageType: 'action_msgs/CancelGoalResponse'
ERROR_NONE: 0
ERROR_REJECTED: 1
ERROR_UNKNOWN_GOAL_ID: 2
ERROR_GOAL_TERMINATED: 3
return_code: 0
goals_canceling: [1×1 struct]
Cancel Goals Before Timestamp
Send the goal message with sequence order 10
. Note the timestamp in a ROS 2 message by using the ros2time
function.
Goal with GoalUUID d8268c566b234e8784f0f1a8ec12b2 accepted by server, waiting for result!
Partial sequence feedback for goal d8268c566b234e8784f0f1a8ec12b2 is 0 1 1
Then, send a second goal message with sequence order 8
. Note the timestamp.
Goal with GoalUUID 9585bff2ba44bf60daa630a952b458be accepted by server, waiting for result!
Partial sequence feedback for goal 9585bff2ba44bf60daa630a952b458be is 0 1 1
Cancel the goal sent before the first time stamp using cancelGoalsBefore
function.
Goals cancelled with return code 0
Use the cancelGoalsBeforeAndWait
function to cancel the goal sent before second time stamp and wait for the cancel response.
cancelResponse = struct with fields:
MessageType: 'action_msgs/CancelGoalResponse'
ERROR_NONE: 0
ERROR_REJECTED: 1
ERROR_UNKNOWN_GOAL_ID: 2
ERROR_GOAL_TERMINATED: 3
return_code: 0
goals_canceling: [1×1 struct]
Cancel All Goals
Cancel all the active goals that the client sent.
Goals cancelled with return code 0
Cancel all the active goals that the client sent and wait for cancel response.
cancelResponse = struct with fields:
MessageType: 'action_msgs/CancelGoalResponse'
ERROR_NONE: 0
ERROR_REJECTED: 1
ERROR_UNKNOWN_GOAL_ID: 2
ERROR_GOAL_TERMINATED: 3
return_code: 0
goals_canceling: [1×1 struct]
Helper Functions
helperFeedbackCallback
defines the callback function to execute when the client receives a feedback response from the action server.
helperCancelGoalCallback
defines the callback function to execute when the client receives a cancel response after canceling a specific goal.
helperCancelGoalsCallback
defines the callback function to execute when the client receives a cancel response after canceling a set of goals.