Skip to content

Dev_NewMessageTypes

Martin Bischoff edited this page Mar 23, 2018 · 13 revisions

How to add new Message Types

1. Edit Message.cs

public class StandardFloat32 : Message
{
    public float data;
    public StandardFloat32()
    {
        data = 0;
    }
}
public static readonly Dictionary<string, Type> Dictionary = new Dictionary<string, Type>
{
    ...
            { "std_msgs/Float32", typeof(StandardFloat32) },
    ...            
}

3. Build RosBridgeClient.sln in Visual Studio

  • Open RosBridgeClient.sln in Visual Studio
  • In Build > Configuration Manager select:
    • Active Solution Configuration: Release
    • Active Solution Platform: Any CPU
  • Click Build > Build Solution

4. Update RosBridgeClient.dll in your Unity Project

Copy RosBridgeClient.dll

  • from ..\RosBridgeClient\bin\Release\
  • to ...\Unity3D\Assets\RosSharp\Plugins\

5. Use your new MessageType in your Unity Project

Example Publisher Script:

using UnityEngine;
using RosSharp.RosBridgeClient;

[RequireComponent(typeof(RosConnector))]
public class FloatPublisher : MonoBehaviour
{
    public string Topic;
    public float MessageData;

    private StandardFloat32 message;
    private RosSocket rosSocket;
    private int publicationId;

    private void Start()
    {
        rosSocket = GetComponent<RosConnector>().RosSocket;
        publicationId = rosSocket.Advertise(Topic, "std_msgs/Float32");
        message = new StandardFloat32();
    }

    private void Update()
    {
        message.data = MessageData;
        rosSocket.Publish(publicationId, message);
    }
}

Example Subscriber Script:

using UnityEngine;
using RosSharp.RosBridgeClient;

[RequireComponent(typeof(RosConnector))]
public class FloatSubscriber : MonoBehaviour
{
    public string Topic;
    public float MessageData;

    private RosSocket rosSocket;

    private void Start()
    {
        rosSocket = GetComponent<RosConnector>().RosSocket;
        rosSocket.Subscribe(Topic, "std_msgs/Float32", Receive);
    }

    private void Receive(Message message)
    {
        MessageData = ((StandardFloat32)message).data;
    }
}

© Siemens AG, 2017-2018 Author: Dr. Martin Bischoff (martin.bischoff@siemens.com)

Clone this wiki locally