Main Content

sendFeedback

Send feedback to action client during goal execution

Description

example

sendFeedback(server,feedbackMsg) sends the feedback message, feedbackMsg, to the action client that sent the goal currently being executed by the action server, server.

Examples

collapse all

This example shows how to create a ROS action server, connect an action client to it, receive goal, and execute it.

Connect to a ROS network.

rosinit
Launching ROS Core...
Done in 0.76538 seconds.
Initializing ROS master on http://172.29.207.162:51080.
Initializing global node /matlab_global_node_79834 with NodeURI http://dcc434238glnxa64:37733/ and MasterURI http://localhost:51080.

Set up an action server for calculating Fibonacci sequence. Use structures for the ROS message data format. Use fibbonacciExecution function as the callback.

cb = @fibonacciExecution; 
server = rosactionserver("/fibonacci","actionlib_tutorials/Fibonacci",ExecuteGoalFcn=cb,DataFormat="struct")
server = 
  SimpleActionServer with properties:

        ActionName: '/fibonacci'
        ActionType: 'actionlib_tutorials/Fibonacci'
    ExecuteGoalFcn: @fibonacciExecution
        DataFormat: 'struct'

Create action client and send a goal to the server to calculate the Fibonacci sequence up to 10 terms past the first two terms, 0 and 1. Display the result sequence.

client = rosactionclient("/fibonacci","actionlib_tutorials/Fibonacci",DataFormat="struct");
goal = rosmessage(client);
goal.Order = int32(10);
result = sendGoalAndWait(client,goal);
result.Sequence
ans = 12x1 int32 column vector

    0
    1
    1
    2
    3
    5
    8
   13
   21
   34
      ⋮

Shut down ROS network.

rosshutdown
Shutting down global node /matlab_global_node_79834 with NodeURI http://dcc434238glnxa64:37733/ and MasterURI http://localhost:51080.
Shutting down ROS master on http://172.29.207.162:51080.

Supporting Functions

The callback function fibbonacciExecution is executed every time the server receives a goal execution request from the client. This function checks if the goal has been preempted, executes the goal and sends feedback to the client during goal execution.

function [result,success] = fibonacciExecution(src,goal,defaultFeedback,defaultResult)

    % Initialize variables
    success = true;
    result = defaultResult;
    feedback = defaultFeedback;
    feedback.Sequence = int32([0 1]);

    for k = 1:goal.Order
        % Check that the client has not canceled or sent a new goal
        if isPreemptRequested(src)
            success = false;
            break
        end

        % Send feedback to the client periodically
        feedback.Sequence(end+1) = feedback.Sequence(end-1) + feedback.Sequence(end);
        sendFeedback(src,feedback)
        
        % Pause to allow time to complete other callbacks (like client feedback)
        pause(0.2)
    end

    if success
        result.Sequence = feedback.Sequence;
    end

end

Input Arguments

collapse all

ROS action server, specified as a SimpleActionServer object handle.

Feedback message for action client, specified as a ROS message. The type and format of the feedbackMsg must match the ActionType and DataFormat properties of server, respectively.

Version History

Introduced in R2022a