Skip to content

User_App_QuickStart

Mehmet Emre Çakal edited this page Oct 10, 2024 · 2 revisions

Quick Start - After Installing ROS#

This tutorial assumes that you have completed:

1. Setting Up ROS#

  1. Create an empty GameObject in your Unity scene (Ctrl+Shift+N). Add empty GameObject
  2. Add the Ros Connector script to the GameObject (Ctrl+Shift+A). Add Ros Connector
  3. Fill in your local address and select your desired serializer and protocol. A specific serializer/protocol might not work for your other packages. If both serializers/protocols are not working for your needs, head to serializers and protocols wiki pages for more information. If Unity doesn't complain, leave it as is. Input Local-IP
  4. Run your (rosbridge server and) Unity project. If you see a 'connected' notification, you've successfully configured/connected the ROS#. Success

2. Subscribing to a ROS Topic

ROS# comes with various of prewritten message types, subscriber and publisher classes. For the sake of simplicty, let's use the StringSubscriber.cs for this tutorial. Before moving on, let's examine the code first.

2.1 Understanding the Code

com.siemens.ros-sharp\Runtime\RosBridgeClient\RosCommunication\StringSubscriber.cs:

namespace RosSharp.RosBridgeClient
{
    public class StringSubscriber : UnitySubscriber<MessageTypes.Std.String>
    {
        public bool isMessageReceived;
        private string messageData;

        protected override void Start()
        {
            base.Start();
        }
       ...
  • We see that the StringSubsriber class implements the UnitySubsriber abstract MonoBehaviour template class, just like any other subsriber class in ROS#. As the name suggests, the StringSubsriber class implements the template with the <MessageTypes.Std.String> message type. Message types are located in the RosSharp.RosBridgeClient.MessageTypes namespace. ROS# ships with a bundle of popular message types, but you can create a completely custom message type for your needs if you wish. See adding new message types for more information.
        ...
        protected override void ReceiveMessage(MessageTypes.Std.String message)
        {
            messageData = message.data;
            isMessageReceived = true;
        }
        ...
  • ReceiveMessage is an implemented abstract callback method that is triggered in every incoming message. In this example, it just updates the messageData.
        ...
        void FixedUpdate()
            {
                Debug.Log("Received message: " + messageData);
            }
        }
}
  • Finally, we can use Unity's built-in FixedUpdate method to print the messageData at a fixed interval. This debugging rate is not relevant to the subscription rate, as it's basically controlled by the publisher and, most importantly, by the TimeStep variable in the base UnitySubsriber class.

2.2 Implementation

  • Open your RosConnectorObject and add the StringSubsriber script. Let's use /test for the topic name and 0 for the time step, which basically means to receive messages as fast as the publisher sends them.

    String Sub
  • Now go to your terminal, open rosbridge and publish some messages.

ros2 launch rosbridge_server rosbridge_websocket_launch.xml

# Another terminal
ros2 topic pub /test std_msgs/msg/String "data: 'Hello ROS#'" 
Sub Success

3. Writing a Custom Publisher

Adding an existing class is very easy, but how about writing one? That is, if not the same, also very easy! Note that there is a Float64 message type in RosSharp.RosBridgeClient.MessageTypes.Std, but there is no Float64Publisher.cs. Why is that? Because we do not want to increase package complexity, size and compile time for a message type you may never use.

3.1 Understanding the Code

  • As we learned from the StringSubscriber, we first need to implement the UnityPublisher abstract class with the correct message type, which is <MessageTypes.Std.Float64> this time.
using UnityEngine;
namespace RosSharp.RosBridgeClient
{
    public class Float64Publisher : UnityPublisher<MessageTypes.Std.Float64>
    {
        public double DoubleData = 0;

        private MessageTypes.Std.Float64 message;
        ...
  • After declaring the necessary variables and calling the Start base method, since we are now the one creating the message, we need to additionally call the InitalizeMessage method. This is the constructor for the <MessageTypes.Std.Float64> message, i.e. the Float64.cs class.
        ...
        protected override void Start()
        {
            base.Start();
            InitializeMessage();
        }
        private void InitializeMessage()
        {
            message = new MessageTypes.Std.Float64
            {
                data = DoubleData
            };
        }
        ...
  • After initialization, again using the built-in FixedUpdate method, we can publish the message at a fixed interval, as the name suggests. This time, however, there is no direct way to change the publishing rate as we had in the UnitySubscriber class, i.e. the Time Step variable. If the fixed time step in the Unity project settings is not suitable for you, i.e. maybe you are doing a simulation in Unity with a fixed physics time step of 2ms and 500 messages per second might be an unnecessary load for the rosbrige_server, you can implement a simple timer via e.g. System.Timers. But for the sake of this tutorial, let's keep things simple.
        ...
        private void FixedUpdate()
        {
            UpdateMessage();
        }
        public void UpdateMessage()
        {
            message.data = DoubleData;
            Publish(message);
            Debug.Log("Published message: " + message.data);
        }
    }
}

3.2 Implementation

  • Now add our freshly written script to the RosConnectorObject, then write your favorite topic name and double value.

    Float64 Publisher
  • Similary, launch the rosbridge_server from your terminal, run Unity, and subscribe to the topic.

ros2 launch rosbridge_server rosbridge_websocket_launch.xml

# Another terminal, after running Unity
ros2 topic echo /test
Publish Success

Next tutorial: Transfer a URDF from ROS to Unity


© Siemens AG, 2017-2024

Clone this wiki locally