sendFeedback
Description
sendFeedback(
sends the feedback message server
,goalStruct
,feedbackMsg
)feedbackMsg
, to the action client that sent
the goal associated with the goal structure goalStruct
. Use this
function in the goal execution callback function specified to the
ExecuteGoalFcn
property of the ROS 2 action server
server
, to send the feedbackMsg
when the server
is executing the goal.
Examples
Create ROS 2 Action Server and Execute a Goal
This example shows how to create a ROS 2 action server, connect an action client to it, receive a goal, and execute it.
Create a ROS 2 node.
node = ros2node("/node_1");
Set up an action server for calculating Fibbonacci sequence. Specify the goal execution, reception and cancel callbacks.
server = ros2actionserver(node,"/fibbonacci","example_interfaces/Fibonacci",ReceiveGoalFcn=@goalReceptionCB,... ExecuteGoalFcn=@goalExecutionCB,CancelGoalFcn=@cancelGoalCB)
server = ros2actionserver with properties: ActionName: '/fibbonacci' ActionType: 'example_interfaces/Fibonacci' MultiGoalMode: 'on' ExecuteGoalFcn: @goalExecutionCB ReceiveGoalFcn: @goalReceptionCB CancelGoalFcn: @cancelGoalCB GoalServiceQoS: 'History: keeplast, Depth: 10, Reliability: reliable, Durability: volatile, Deadline: Inf, Lifespan: Inf, Liveliness: automatic, Lease Duration: Inf' ResultServiceQoS: 'History: keeplast, Depth: 10, Reliability: reliable, Durability: volatile, Deadline: Inf, Lifespan: Inf, Liveliness: automatic, Lease Duration: Inf' CancelServiceQoS: 'History: keeplast, Depth: 10, Reliability: reliable, Durability: volatile, Deadline: Inf, Lifespan: Inf, Liveliness: automatic, Lease Duration: Inf' FeedbackTopicQoS: 'History: keeplast, Depth: 10, Reliability: reliable, Durability: volatile, Deadline: Inf, Lifespan: Inf, Liveliness: automatic, Lease Duration: Inf' StatusTopicQoS: 'History: keeplast, Depth: 1, Reliability: reliable, Durability: transientlocal, Deadline: Inf, Lifespan: Inf, Liveliness: automatic, Lease Duration: Inf'
Create an action client and specify a goal message to calculate the Fibbonacci sequence up to 10 terms past the first term 0
.
client = ros2actionclient(node,"/fibbonacci","example_interfaces/Fibonacci"); waitForServer(client); goalMsg = ros2message(client); goalMsg.order = int32(10);
Send the goal. Use ros2ActionSendGoalOptions
function to specify callback options when the client receives feedback and result messages from the server.
callbackOpts = ros2ActionSendGoalOptions(FeedbackFcn=@printFeedback,ResultFcn=@printResult); goalHandle = sendGoal(client,goalMsg,callbackOpts);
Supporting Functions
goalReceptionCB
is the goal reception callback that is triggered when the action server receives a new goal. Use the handleGoalResponse
object function to accept or reject a new goal.
function goalReceptionCB(src,goalStruct) fprintf("[Server] Goal received, UUID: %s\n",goalStruct.goalUUID) if goalStruct.goal.order < 1 % Reject Goal handleGoalResponse(src,goalStruct,'REJECT'); else handleGoalResponse(src,goalStruct,'ACCEPT_AND_EXECUTE'); end end
goalExecutionCB
is the goal execution callback that is triggered after a new goal is accepted and the server is ready to execute it. In this example, use goalExecutionCB
to calculate the Fibonacci sequence for the number of terms specified in the goal message. First, check whether the client has preempted the goal using the isPreemptRequested
object function. If not, continue goal execution and send periodic feedback to the client about the goal execution status.
function [result,success] = goalExecutionCB(src,goalStruct,defaultFeedbackMsg,defaultResultMsg) fprintf('[Server] Goal accepted and executing, UUID: %s\n', goalStruct.goalUUID); success = true; result = defaultResultMsg; feedback = defaultFeedbackMsg; feedback.sequence = int32([0;1]); for k=1:goalStruct.goal.order-1 % Check that the client has not preempted the goal if isPreemptRequested(src,goalStruct) success = false; break end % Periodically send feedback to the client feedback.sequence = [feedback.sequence; int32(0)]; feedback.sequence(end) = feedback.sequence(end-1) + feedback.sequence(end-2); sendFeedback(src,goalStruct,feedback); end if success result.sequence = feedback.sequence; end end
cancelGoalCB
is the cancel goal callback that is triggered after the server receives a cancel request from the client.
function cancelGoalCB(~,goalStruct) fprintf('[Server] Received request to cancel goal with UUID: %s\n', goalStruct.goalUUID); end
printFeedback
function is triggered when the client receives the feedback message from the server.
function printFeedback(goalHandle,resp) seq = resp.sequence; fprintf("[Client] Feedback: Fibonacci sequence for goal %s calculated currently: [", goalHandle.GoalUUID); for i=1:numel(seq) fprintf(" %d",seq(i)); end fprintf(' ]\n'); end
printResult
function is triggered when the client receives the result message from the server.
function printResult(goalHandle,resp) seq = resp.result.sequence; fprintf("[Client] Result: Fibonacci sequence for goal %s is: [", goalHandle.GoalUUID); for i=1:numel(seq) fprintf(" %d",seq(i)); end fprintf(' ]\n'); end
Input Arguments
server
— ROS 2 action server
ros2actionserver
object
ROS 2 action server, specified as a ros2actionserver
object.
goalStruct
— Structure containing the goal information
structure
Structure containing the goal information, specified as a ROS 2 action goal
structure. The new goal reception callback function in the
ReceiveGoalFcn
property of the ros2actionserver
object automatically generates this structure as an input argument. The ROS 2 action
goal structure contains these fields:
goal
— Goal message structuregoalUUID
— Unique ID of the goal
feedbackMsg
— Feedback message for action client
ROS 2 message
Feedback message for action client, specified as a ROS 2 message. The type of the
feedbackMsg
must match the ActionType
property
of the server
.
Extended Capabilities
C/C++ Code Generation
Generate C and C++ code using MATLAB® Coder™.
Version History
Introduced in R2023b
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 (한국어)