Skip to content

immersive-command-system/ROSBridgeLibFork

This branch is 3 commits ahead of, 6 commits behind MathiasCiarlo/ROSBridgeLib:master.

Folders and files

NameName
Last commit message
Last commit date

Latest commit

3ef421d · Mar 20, 2021

History

61 Commits
Nov 21, 2016
Nov 21, 2016
Nov 21, 2016
Nov 23, 2016
Nov 23, 2016
Feb 6, 2021
Aug 2, 2019
Nov 21, 2016
Jun 10, 2020
Nov 23, 2016
Apr 28, 2019
Aug 1, 2019
Feb 6, 2021
Nov 16, 2016
Nov 21, 2016
Aug 3, 2020
Feb 6, 2021
Nov 16, 2016

Repository files navigation

ROSBridgeLib

A Unity library for communication with ROS through RosBridge

The first version of this I believe origins from Michael Jenkin, in the repo unityros. He made a sample unity project showing turtlesim, with good instructions on how to use this project. All honor goes to him. I created this project because there was no repository containing the barebone library.

Included messages

This repository does not contain every ROS message. If you need to add one, please make a pull request.

Documentation

Documentation is in the code. I added some more in addition to what Michael Jenkin (original author) did. The main file is ROSBridgeWebSocketConnection.cs, which sets up everything.

Example usage

This is an example application where a ball is controlled. Basically, there are three important script types to notice. First, create a main script responsible for initializing RosBridge:

public class RollABallRosController : MonoBehaviour {
  private ROSBridgeWebSocketConnection ros = null;
    
  void Start() {
    // Where the rosbridge instance is running, could be localhost, or some external IP
    ros = new ROSBridgeWebSocketConnection ("ws://localhost", 9090);

    // Add subscribers and publishers (if any)
    ros.AddSubscriber (typeof(BallPoseSubscriber));
    ros.AddPublisher (typeof(BallControlPublisher));

    // Fire up the subscriber(s) and publisher(s)
    ros.Connect ();
  }
  
  // Extremely important to disconnect from ROS. Otherwise packets continue to flow
  void OnApplicationQuit() {
    if(ros!=null) {
      ros.Disconnect ();
    }
  }
  // Update is called once per frame in Unity
  void Update () {
    ros.Render ();
  }
}

Then, create a subscriber script which will receive updates from a chosen ROS topic

// Ball subscriber:
public class BallPoseSubscriber : ROSBridgeSubscriber {
  static GameObject ball;

  // These two are important
  public new static string GetMessageTopic() {
    return "/path/to/pose/topic";
  }

  public new static string GetMessageType() {
    return "std_msgs/PoseMsg";
  }

  // Important function (I think.. Converts json to PoseMsg)
  public new static ROSBridgeMsg ParseMessage(JSONNode msg) {
    return new PoseMsg (msg);
  }

  // This function should fire on each received ros message
  public new static void CallBack(ROSBridgeMsg msg) {

    // Update ball position, or whatever
    ball.x = msg.x; // Check msg definition in rosbridgelib
    ball.y = msg.y;
    ball.z = msg.z;
  }
}

If you need to publish data to ROS, create a publisher:

// Ball publisher: // Using twist msgs for example?
public class BallControlPublisher: ROSBridgePublisher {

  // The following three functions are important
  public static string GetMessageTopic() {
    return "/topic/to/publish/to";
  }

  public static string GetMessageType() {
      return "std_msgs/TwistMsg";
  }

  public static string ToYAMLString(TwistMsg msg) {
    return msg.ToYAMLString();
  }

  public new static ROSBridgeMsg ParseMessage(JSONNode msg) {
    return new TwistMsg(msg);
  }    
}

// And in some other class where the ball is controlled:
TwistMsg msg = new TwistMsg(x, y, z); // Circa

// Publish it (ros is the object defined in the first class)
ros.Publish(BallControlPublisher.GetMessageTopic(), msg);

License

Note: SimpleJSON is included here as a convenience. It has its own licensing requirements. See source code and unity store for details.

Releases

No releases published

Packages

No packages published

Languages

  • C# 100.0%