-
Notifications
You must be signed in to change notification settings - Fork 394
User_App_Fibonacci_Action_Client
- 1 - Installation and Configuration.
- Writing a Simple Action Server using the Execute Callback
- How to add new Message Types
Create a new empty GameObject. This is where we will attach our fibonacci client script.
And we shall call it with a very unoriginal name FibonacciClient.
Please take a look at the code at Assets/RosSharp/Scripts/RosBridgeClient/ActionHandling/FibonacciActionClientComponent.cs
Original can be found in our repository
using System;
using UnityEngine;
using RosSharp.RosBridgeClient.Actionlib;
using RosSharp.RosBridgeClient.Protocols;
using RosSharp.RosBridgeClient.MessageTypes.ActionlibTutorials;-
Systemgives youStringfor string manipulations andGuidfor generating Goal ID -
UnityEnginegives you access to Unity functions -
RosSharp.RosBridgeClient.Actionlibis the action library used forimplementing simple action clients -
RosSharp.RosBridgeClient.Protocolsis used forWebSocketsconnection to ROS -
RosSharp.RosBridgeClient.MessageTypes.ActionlibTutorialsis where the C# classes forFibonacci.actionlives
namespace RosSharp.RosBridgeClient
{
public class FibonacciActionClientComponent : MonoBehaviour
{Here we will enclose our classes in the RosBridgeClient namespace and we will have a class that extends MonoBehaviour to be executed by Unity.
public string actionName = "fibonacci";
public Protocol protocol = Protocol.WebSocketSharp;
public string serverURL = "ws://192.168.137.195:9090";
public RosSocket.SerializerEnum serializer = RosSocket.SerializerEnum.JSON;
public int timeout = 10;
public float timeStep = 0.2f;
public int fibonacciOrder = 20;-
actionName: providesrostopicname in order to communicate with ROS -
protocol: for this example, we will useWebSocketSharp. The other option isWebSocketNET -
serverURL: address for the action server. Starts withws://which stands forWebSocket, followed by ROS host machine IP and the port which RosBridge is communicating through -
serializer: for this example, we will useJSON. The other option isBSON -
timeouttime in seconds to wait for communication with ROS. Not used in this example -
timesteptime interval in seconds for each run loop in client, which is responsible for checking action status, loop intervals, etc. -
fibonacciOrderthe length of the fibonacci sequence requested
private FibonacciActionClient client;An instance of our fibonacci client class (we will cover that soon)
public string status = "";
public string feedback = "";
public string result = "";We will use those public variables to show action status in inspector
private void Awake()
{
FibonacciAction action = new FibonacciAction();
action.action_goal.goal.order = fibonacciOrder;
client = new FibonacciActionClient(action, actionName, serverURL, protocol, serializer, timeout, timeStep);
}When an instance of our extension to MonoBehaviour is created (Awake), we will create an instance of FibonacciActionClient (implemented later in the tutorial), and pass in a FibonacciAction instance with its action_goal set with our requested fibonacci order.
// Start is called before the first frame update
private void Start()
{
client.Start();
}Before the first frame update when we enter play mode (Start), start the client
// Update is called once per frame
private void Update()
{
status = client.GetStatusString();
feedback = client.GetFeedbackString();
result = client.GetResultString();
}For each frame (Update]), we will update our displayed action status from client instance.
private void OnDestroy()
{
client.Stop();
}When Unity exits play mode (OnDestroy) we will stop the client
public void SendGoal()
{
client.SendGoalFromUnity();
}
public void CancelGoal()
{
client.CancelGoalFromUnity();
}
}Above methods will be bound to inspector buttons in an editor script. More on that later
public class FibonacciActionClient : ActionClient<FibonacciAction, FibonacciActionGoal, FibonacciActionResult, FibonacciActionFeedback, FibonacciGoal, FibonacciResult, FibonacciFeedback>
{
public FibonacciActionClient(FibonacciAction action, string actionName, string serverURL, Protocol protocol, RosSocket.SerializerEnum serializer, int timeout, float timeStep) : base(action, actionName, serverURL, protocol, serializer, timeStep) { }Now, onto the FibonacciActionClient where all the action takes place
FibonacciActionClient will extend RosSharp.RosBridgeClient.Actionlib.ActionClient and we will provide generic arguments <FibonacciAction, FibonacciActionGoal, FibonacciActionResult, FibonacciActionFeedback, FibonacciGoal, FibonacciResult, FibonacciFeedback> which corresponds to generated classes from Fibonacci.action to ensure type safety (aka not using random message classes and cause bad things to happen).
protected override string GoalID()
{
return "fibonacci-unity-" + Guid.NewGuid();
}We will generate a GoalID that consists of fibonacci-unity- and a randomly generated GUID to identify the goal send from our client
protected override void OnFeedbackReceived()
{
// Not implemented since get string directly returns stored feedback
}
protected override void OnResultReceived()
{
// Not implemented since get string directly returns stored result
}For this example, the client doesn't do anything on feedback or result received. The base ActionClient has feedback and result updated when receiving those.
public string GetStatusString()
{
if (goalStatus != null) {
return ((ActionStatus)(goalStatus.status)).ToString();
}
return "";
}
public string GetFeedbackString()
{
return String.Join(",", action.action_feedback.feedback.sequence);
}
public string GetResultString()
{
return String.Join(",", action.action_result.result.sequence);
}Those methods allow our Unity MonoBehaviour class FibonacciActionClientComponent to get status, feedback and result string from client and display those in inspector.
public void SendGoalFromUnity()
{
SendGoal();
}
public void CancelGoalFromUnity()
{
CancelGoal();
}These methods expose protected methods to FibonacciActionClientComponent to be triggered on button click in inspector
protected override void Log(string log)
{
Debug.Log("Fibonacci Action Client: " + log);
}
protected override void LogWarning(string log)
{
Debug.LogWarning("Fibonacci Action Client: " + log);
}
protected override void LogError(string log)
{
Debug.LogError("Fibonacci Action Client: " + log);
}
}
}ActionClient base classes uses abstract log methods and we implement those here with Unity Debug class to be visible in Unity console and log.
Please take a look at the code at Assets/RosSharp/Scripts/RosBridgeClient/Editor/FibonacciAction/FibonacciActionClientEditor.cs
Original and be found in our repository
using UnityEditor;
using UnityEngine;UnityEditor and UnityEngine provides us with access to Unity APIs
namespace RosSharp.RosBridgeClient
{Again, we will enclose our classes in the RosBridgeClient namespace
[CustomEditor(typeof(FibonacciActionClientComponent))]
public class FibonacciActionClientEditor : Editor
{Here, we define a custom editor for FibonacciActionClientComponent, which is an extension of Unity Editor
public override void OnInspectorGUI()
{
base.OnInspectorGUI();OnInspectorGUI is responsible for managing UI elements in the inspector window. We will add our buttons here.
By calling base.OnInspectorGUI(); we will render the default elements, which are the public variables in FibonacciActionClientComponent
if (GUILayout.Button("Send Goal"))
{
((FibonacciActionClientComponent)target).SendGoal();
}
if (GUILayout.Button("Cancel Goal"))
{
((FibonacciActionClientComponent)target).CancelGoal();
}The above adds the SendGoal and CancelGoal button that calls corresponding methods in FibonacciActionClientComponent
Repaint();
}
}
}Repaint to apply changes to inspector
Now since we have all the parts ready, attach FibonacciActionClientComponent to FibonacciClient GameObject by dragging the script from project browser and dropping on FibonacciClient.
Select FibonacciClient and check the inspector to see that FibonacciActionClientComponent has been attached
Note: This section assumes that you have ROS installed on a machine accessible from Unity machine and have file_server installed in ROS. See our wiki page for installation guide
In order to get Unity to talk to ROS, fire up RosBridgeClient by running the following in terminal
$ roslaunch rosbridge_server rosbridge_websocket.launchThen get fibonacci server from ROS tutorial Writing a Simple Action Server using the Execute Callback running by executing the following in a separate terminal window:
$ rosrun actionlib_tutorials fibonacci_serverThen, back in Unity, start play mode and click Send Goal in FibonacciClient inspector. Action status, feedback and result will be displayed once server receives the goal.
© Siemens AG, 2017-2019 Author: Sifan Ye ([email protected])
-
- 1.3.1 R2D2 Setup
- 1.3.2 Gazebo Setup on VM
- 1.3.3 TurtleBot Setup (Optional for ROS2)
- 2.1 Quick Start
- 2.2 Transfer a URDF from ROS to Unity
- 2.3 Transfer a URDF from Unity to ROS
- 2.4 Unity Simulation Scene Example
- 2.5 Gazebo Simulation Scene Example
- 2.6 Fibonacci Action Client
- 2.7 Fibonacci Action Server
- 3.1 Import a URDF on Windows
- 3.2 Create, Modify and Export a URDF Model
- 3.3 Animate a Robot Model in Unity
- 4.1 Introduction to RosBridgeClient
- 4.2 Image Publication
- 4.3 URDF Transfer
- 4.4 Fibonacci Action Client/Server
- Message Handling: Readers & Writers
- Thread Safety for Message Reception
- File Server Package
- ROS-Unity Coordinate System Conversions
- Post Build Events
- Preprocessor Directives in ROS#
- Adding New Message Types
- RosBridgeClient Protocols
- RosBridgeClient Serializers
- Actions in ROS#
- Action Server State Machine Model
© Siemens AG, 2017-2025