Skip to content

User_App_Fibonacci_Action_Server

Mehmet Emre Çakal edited this page Jan 20, 2025 · 8 revisions

Fibonacci Action Server

Note: This tutorial assumes that you have completed:

Table of Contents

  1. Configure Action Server on Unity
  2. Run the Action Client on ROS
  3. The Code Explained
ROS2 Humble

1. Configure Action Server on Unity

  • Create a new empty GameObject, name it as 'RosConnector'. This is where we will attach our RosConnector and UnityFibonacciActionServer components.

    • Or you can head to the Unity Package Manager window and under the Samples section of ROS# you can import the Fibonacci Action Scene. For the sake of this tutorial, disable the client component under RosConnector.
  • Action Name is the name of the action, set it to "/fibonacci".

  • Status and Feedback are the fields to inform users about the current state of action server, and updated by FibonacciActionServer object in accordance with the server state machine model.

  • Simply run the Unity application, and then run the action client on the ROS side.

2. Run the Action Client on ROS

  • Run RosBridge Websocket and the Fibonacci Action Client example from the ROS2 tutorials package action_tutorials_page. The package can be installed using the package manager sudo apt install ros-humble-action-tutorials-cpp or from the ROS2 tutorial page: Writing an Action Server and Client (C++).

  • Note that to be able to cancel actions, the send_action_goals_in_new_thread parameter of the websocket should be set.

  • Although there is also a Python version of the tutorial package from the ROS2 documentation, we recommend using the C++ package because there are some bugs with the Python package that make it unable to cancel actions and crash rqt.

ros2 run rosbridge_server rosbridge_websocket --ros-args -p send_action_goals_in_new_thread:=true

# Another terminal
ros2 run action_tutorials_cpp fibonacci_action_client

User_App_Actionlib_RosServer_ROS2 User_App_Actionlib_FibonacciClient_ROS2

  • Alternatively, you can enable both the client and the server in the Unity scene without using the action_tutorials_cpp. But the rosbridge_server must be running in any case.

3. The Code Explained

The FibonacciActionServer.cs is a derived client implementation of the generic abstract class ActionServer.cs. In ROS#, each action server and client must implement their respective abstract classes. For more information about the Fibonacci Action server implementation, see .NET Fibonacci Action Client/Server. For more information about the under-the-hood abstract classes and state machine model, see Actions Middleware Implementation, and Action Server State Machine Model, respectively. In this subsection, only the Unity usage example of the Fibonacci Action Server is discussed.

private void Start()
{
    rosConnector = GetComponent<RosConnector>();
    fibonacciActionServer = new FibonacciActionServer(actionName, rosConnector.RosSocket, new Log(x => Debug.Log(x)));
    fibonacciActionServer.Initialize();
}

To create a client instance, a RosSocket instance, the action name, and a logger function must be passed to the constructor. The action name is not the same as the action message type, it is just a custom name. Message types are handled under the hood. Console.Log is used as the logger function in this example.

private void Update()
{
    status = fibonacciActionServer.GetStatus().ToString();
    feedback = fibonacciActionServer.GetFeedbackSequenceString();
}

Although not necessary, status, feedback can be stored in the update loop for monitoring. These values are already handled and sent within the server implementation by their corresponding callback functions. For more information, see Actions Middleware Implementation.

ROS1 Noetic

1. Configure Action Server on Unity

  • Create a new empty GameObject, name it as 'RosConnector'. This is where we will attach our RosConnector and UnityFibonacciActionServer components.

  • Action Name is the name of the action, set it to "fibonacci". Usually published and subscribed topics are named as 'action_name/goal', 'action_name/feedback', etc.

  • Status and Feedback are the fields to inform users about the current state of action server, and updated by FibonacciActionServer object in accordance with the server state machine model.

  • Simply run the Unity3D application, and then run the action client on the ROS side.

User_App_ROS_UnityActionClient_Inspector

2. Run the Action Client on ROS

  • Note: This section assumes that you have ROS installed on a machine accessible from Unity machine and have rosbridge_server installed in ROS. See our wiki page for installation guide

  • In order to get Unity to talk to ROS, fire up RosBridge WebSocket by running the following in terminal

$ roslaunch rosbridge_server rosbridge_websocket.launch
$ rosrun actionlib_tutorials fibonacci_client

User_App_Actionlib_RosClient

  • You should now see on Unity3D inspector window that status and feedback fields are being updated. Once the goal has succeeded, check back in your ROS machine and see that the client has received a result.

User_App_Actionlib_RosClientSuccess

3. The Code Explained

Original at: http://wiki.ros.org/actionlib/DetailedDescription?action=AttachFile&do=get&target=server_states_detailed.png

Image from ROS Actionlib Detailed Description

User_App_UnityServer_StateMachine

Transition Representing Method On transition method
Receive Goal private void GoalCallback(actionGoal) protected abstract void OnGoalReceived()
Cancel Request private void CancelCallback(goalID) protected abstract void OnGoalPreempting()
setAccepted protected void SetAccepted() protected abstract void OnGoalActive()
setRejected protected void SetRejected() protected virtual void OnGoalRejected()
setSucceeded protected void SetSucceeded() protected virtual void OnGoalSucceeded()
setCanceled protected void SetCanceled() protected virtual void OnGoalCanceled()
  • ActionServer.cs is an abstract class which can be implemented by providing the message types of the defined ROS action. Please see the example implementation FibonacciActionServer.cs to observe the message types used for Fibonacci action example.
public abstract class ActionServer<TAction, TActionGoal, TActionResult, TActionFeedback, TGoal, TResult, TFeedback>
    where TAction : Action<TActionGoal, TActionResult, TActionFeedback, TGoal, TResult, TFeedback>
    where TActionGoal : ActionGoal<TGoal>
    where TActionResult : ActionResult<TResult>
    where TActionFeedback : ActionFeedback<TFeedback>
    where TGoal : Message
    where TResult : Message
    where TFeedback : Message
    {
        public string status = "";
        public string feedback = "";

        private ManualResetEvent isProcessingGoal = new ManualResetEvent(false);
        private Thread goalHandler;
  • ManualResetEvent isProcessingGoal is used to notify goal processing thread to stop processing the goal.

  • Thread goalHandler is the asynchronous goal handling thread. Goal handling is executed on a separate thread to allow for goal preempting.

  • ActionServer has generic callback functions for subscriptions to 'action_name/goal' and 'action_name/cancel', in which application-specific state transition functions are called.

  • In order to implement ActionServer, users only have to implement these state transition functions, for example, OnGoalReceived() function to process the goal request from the action client.

// When receive a new goal
protected abstract void OnGoalReceived();
private void GoalCallback(TActionGoal actionGoal)
{
    action.action_goal = actionGoal;
    UpdateAndPublishStatus(ActionStatus.PENDING);
    OnGoalReceived();
}
  • FibonacciActionServer object can be instantiated either on Unity3D or in a console application.

  • For the use of action server implementation on Unity3D, please see UnityFibonacciActionServer.cs.

public class UnityFibonacciActionSever : MonoBehaviour
{
    private RosConnector rosConnector;
    private FibonacciActionServer fibonacciActionServer;

    public string actionName;
    public string status;
    public string feedback;

    private void Start()
    {
        rosConnector = GetComponent<RosConnector>();
        fibonacciActionServer = new FibonacciActionServer(actionName, rosConnector.RosSocket, new Log(x => Debug.Log(x)));
        fibonacciActionServer.Initialize();
    }
  • Note that this is a MonoBehaviour class. On Unity3D application start, FibonacciActionServer object is instantiated with an action name, a RosSocket, and a Log delegate.
private void Update()
{
    fibonacciActionServer.PublishStatus();
    status = fibonacciActionServer.GetStatus().ToString();
    feedback = fibonacciActionServer.GetFeedbackSequenceString();
}
  • On every Unity3D update step, we publish the status of the server to ROS and call some getter functions of the server instance to update the fields on the inspector.

  • For the use of action server implementation in a console application, please see FibonacciActionServerConsoleExample.cs.


Next tutorial: Unity application examples without ROS communication


© Siemens AG, 2017-2025

Clone this wiki locally