Skip to content

User_App_Fibonacci_Action_Client

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

Fibonacci Action Client

Note: This tutorial assumes that you have completed:

Table of Contents

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

1. Launch the Action Server on ROS

  • Run RosBridge Websocket and the Fibonacci Action Server 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_server

User_App_Actionlib_RosServer_ROS2 User_App_Actionlib_FibonacciServer_ROS2

2. Configure Action Client on Unity

  • Create a new empty GameObject, name it as 'RosConnector'. This is where we will attach our RosConnector and UnityFibonacciActionClient 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 server component under RosConnector.
  • 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.

  • Fibonacci Order is the order of Fibonacci series defined as the goal for this particular action. Users set the goal, and clicks on Send Goal to activate the action server. Users can cancel the activated server by clicking on Cancel Goal.

  • Status, Feedback, and Result are the fields to inform users about the current state of action server, and updated via callback functions of subscribed topics to the action server.

  • Simply run the Unity application, and click on "Send Goal".

3. The Code Explained

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

private void Start()
{
    rosConnector = GetComponent<RosConnector>();
    fibonacciActionClient = new FibonacciActionClient(actionName, rosConnector.RosSocket);
    fibonacciActionClient.Initialize();
}

To create a client instance, a RosSocket instance and the action name 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.

private void Update()
{
    status   = fibonacciActionClient.GetStatusString();
    feedback = fibonacciActionClient.GetFeedbackString();
    result   = fibonacciActionClient.GetResultString();
}

Although not necessary, status, feedback, and result can be stored in the update loop for monitoring. These values are already handled within the client implementation by their corresponding callback functions.

public void RegisterGoal()
{
    fibonacciActionClient.SetActionGoal(new FibonacciGoal(fibonacciOrder));
}

To register a goal, instead of just passing the order as an integer, the correct message type must be passed to the SetActionGoal method. The FibonacciGoal type in this example is one of the auto generated message types by RosSharp.MessageGeneration.

ROS1 Noetic

1. Launch the Action Server 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_server

User_App_Actionlib_RosServer

2. Configure Action Client on Unity

  • Create a new empty GameObject, name it as 'RosConnector'. This is where we will attach our RosConnector and UnityFibonacciActionClient 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.

  • Fibonacci Order is the order of Fibonacci series defined as the goal for this particular action. Users set the goal, and clicks on Send Goal to activate the action server. Users can preempt the activated server by clicking on Cancel Goal.

  • Status, Feedback, and Result are the fields to inform users about the current state of action server, and updated via callback functions of subscribed topics to the action server.

  • Simply run the Unity3D application, and click on "Send Goal".

User_App_ROS_UnityActionClient_Inspector

3. The Code Explained

  • ActionClient.cs is an abstract class which can be implemented by providing the message types of the defined ROS action. Please see the example implementation FibonacciActionClient.cs to observe the message types used for Fibonacci action example.
public abstract class ActionClient<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
  • ActionClient has generic callback functions for subscriptions to 'action_name/status', 'action_name/feedback', and 'action_name/result', in which application-specific handling functions are called.

  • In order to implement ActionClient, users only have to implement the handling functions, for example, OnResultReceived() function to process the result received from the action server.

// Implement by user to handle result.
protected abstract void OnResultReceived();
private void ResultCallback(TActionResult actionResult)
{
    action.action_result = actionResult;
    OnResultReceived();
}
public class UnityFibonacciActionClient : MonoBehaviour
{
    private RosConnector rosConnector;
    public FibonacciActionClient fibonacciActionClient;

    public string actionName;
    public int fibonacciOrder = 20;
    public string status = "";
    public string feedback = "";
    public string result = "";

    private void Start()
    {
        rosConnector = GetComponent<RosConnector>();
        fibonacciActionClient = new FibonacciActionClient(actionName, rosConnector.RosSocket);
        fibonacciActionClient.Initialize();
    }
  • Note that this is a MonoBehaviour class. On Unity3D application start, FibonacciActionClient object is instantiated with an action name and a RosSocket.
private void Update()
{
    status   = fibonacciActionClient.GetStatusString();
    feedback = fibonacciActionClient.GetFeedbackString();
    result   = fibonacciActionClient.GetResultString();
}
  • On every Unity3D update step, we just call the getter functions of FibonacciActionClient object to update the fields on the inspector.
public void RegisterGoal()
{
    fibonacciActionClient.fibonacciOrder = fibonacciOrder;
}
  • RegisterGoal() binds the Fibonacci order from the Unity3D inspector to the FibonacciActionClient object.

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


Next tutorial: Fibonacci Action Server


© Siemens AG, 2017-2025

Clone this wiki locally