Skip to content

Dev_NewMessageTypes

Martin Bischoff edited this page Jun 28, 2019 · 13 revisions

How to add new Message Types

Option 1: Generate Message Types in Unity

  • Via the RosBridgeClient > Generate Messages menu.

The following editor window creates action messages which are used in actionlib tutorials .

Option 2: Generate Messages in the RosbridgeClient Library

1. Declare a new message type

Add a new class in Libraries\RosBridgeClient\Messages or any of its subfolders, and make it extend the Message class.

using Newtonsoft.Json;

namespace RosSharp.RosBridgeClient.Messages.Standard
{
    public class Float32 : Message
    {
        [JsonIgnore]
        public const string RosMessageName = "std_msgs/Float32";
        public float data;

        public Float32()
        {
            data = 0;
        }
    }
}

2. Build RosSharp.sln in Visual Studio

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

3. Update RosBridgeClient.dll in your Unity Project

Copy RosBridgeClient.dll

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

4. Use the new MessageType in your Unity Project

Create a new script in Assets\RosSharp\Scripts\RosCommunication.

Example Publisher Script:

namespace RosSharp.RosBridgeClient
{
    public class FloatPublisher : Publisher<Messages.Standard.Float32>
    {
        public float messageData;

        private Messages.Standard.Float32 message;

        protected override void Start()
        {
            base.Start();
            InitializeMessage();
        }

        private void InitializeMessage()
        {
            message = new Messages.Standard.Float32
            {
                data = messageData
            };
        }

        private void Update()
        {
            message.data = messageData;
            Publish(message);
        }
    }
}

Example Subscriber Script:

namespace RosSharp.RosBridgeClient
{
    public class FloatSubscriber : Subscriber<Messages.Standard.Float32>
    {
        public float messageData;

        protected override void Start()
        {
            base.Start();
        }

        protected override void ReceiveMessage(Messages.Standard.Float32 message)
        {
            messageData = message.data;
        }
    }
}

© Siemens AG, 2017-2019 Author: Suzannah Smith (suzannah.smith@siemens.com)

Clone this wiki locally