Dev_NewMessageTypes

Martin Bischoff edited this page Jul 23, 2018 · 3 revisions

How to add new Message Types

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-2018 Author: Suzannah Smith (suzannah.smith@siemens.com)

Clone this wiki locally
You can’t perform that action at this time.
You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session.
Press h to open a hovercard with more details.