Skip to content

Commit

Permalink
Adds tests and package
Browse files Browse the repository at this point in the history
  • Loading branch information
jskyjohnson committed May 19, 2020
1 parent fc97591 commit 8a22169
Show file tree
Hide file tree
Showing 5 changed files with 166 additions and 2 deletions.
28 changes: 28 additions & 0 deletions ConnectionManager.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class ConnectionManager : MonoBehaviour {
private ROSBridgeLib.ROSBridgeWebSocketConnection ros = null;

public string IP = "192.168.1.142";

void Start() {
// Where the rosbridge instance is running, could be localhost, or some external IP
ros = new ROSBridgeLib.ROSBridgeWebSocketConnection("ws://" + IP, 9090);

ros.AddSubscriber (typeof(TestSubscriber));
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 ();
}
}
37 changes: 37 additions & 0 deletions TestSubscriber.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
using SimpleJSON;
using UnityEngine;

public class TestSubscriber : ROSBridgeLib.ROSBridgeSubscriber {
public new static string GetMessageTopic() {
return "/test_object";
}

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

public new static ROSBridgeMsg ParseMessage(JSONNode msg) {
return new ROSBridgeLib.geometry_msgs.PoseMsg (msg);
}

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

GameObject TestObject = GameObject.FindGameObjectWithTag("TestObject");

ROSBridgeLib.geometry_msgs.TransformStampedMsg incomingMessage = (ROSBridgeLib.geometry_msgs.TransformStampedMsg)msg;
float x = incomingMessage.GetTransform().GetTranslation().GetX();
float y = incomingMessage.GetTransform().GetTranslation().GetY();
float z = incomingMessage.GetTransform().GetTranslation().GetZ();

float qx = incomingMessage.GetTransform().GetRotation().GetX();
float qy = incomingMessage.GetTransform().GetRotation().GetY();
float qz = incomingMessage.GetTransform().GetRotation().GetZ();
float qw = incomingMessage.GetTransform().GetRotation().GetW();

Vector3 IncomingPosition = new Vector3(x, z, y);
Quaternion IncomingRotation = new Quaternion(qx, qy, qz, qw);

TestObject.transform.SetPositionAndRotation(IncomingPosition, IncomingRotation);
}
}
49 changes: 49 additions & 0 deletions geometry_msgs/TransformMsg.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
using System.Collections;
using System.Text;
using SimpleJSON;

/**
* Define a geometry_msgs pose message. This has been hand-crafted from the corresponding
* geometry_msgs message file.
*
* @author Miquel Massot Campos
*/

namespace ROSBridgeLib {
namespace geometry_msgs {
public class TransformMsg : ROSBridgeMsg {
public PointMsg _translation;
public QuaternionMsg _rotation;

public TransformMsg(JSONNode msg) {
_translation = new PointMsg(msg["translation"]);
_rotation = new QuaternionMsg(msg["rotation"]);
}

public TransformMsg(PointMsg p, QuaternionMsg q) {
_translation = p;
_rotation = q;
}

public static string getMessageType() {
return "geometry_msgs/Transform";
}

public PointMsg GetTranslation() {
return _translation;
}

public QuaternionMsg GetRotation() {
return _rotation;
}

public override string ToString() {
return "geometry_msgs/Transform [translation=" + _translation.ToString() + ", rotation=" + _rotation.ToString() + "]";
}

public override string ToYAMLString() {
return "{\"translation\": " + _translation.ToYAMLString() + ", \"rotation\": " + _rotation.ToYAMLString() + "}";
}
}
}
}
43 changes: 43 additions & 0 deletions geometry_msgs/TransformStampedMsg.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
using System.Collections;
using System.Text;
using SimpleJSON;
using ROSBridgeLib.std_msgs;

/*
* @brief ROSBridgeLib
* @author Michael Jenkin, Robert Codd-Downey, Andrew Speers and Miquel Massot Campos
*/

namespace ROSBridgeLib {
namespace geometry_msgs {
public class TransformStampedMsg : ROSBridgeMsg {
public HeaderMsg _header;
public TransformMsg _transform;

public TransformStampedMsg(JSONNode msg) {
_header = new HeaderMsg(msg["header"]);
_transform = new TransformMsg(msg["transform"]);
}

public static string GetMessageType() {
return "geometry_msgs/TransformPosed";
}

public HeaderMsg GetHeader() {
return _header;
}

public TransformMsg GetTransform() {
return _transform;
}

public override string ToString() {
return "TransformStamped [header=" + _header.ToString() + ", transform=" + _transform.ToString() + "]";
}

public override string ToYAMLString() {
return "{\"header\" : " + _header.ToYAMLString() + ", \"transform\" : " + _transform.ToYAMLString() + "}";
}
}
}
}
11 changes: 9 additions & 2 deletions sensor_msgs/PointCloud2Msg.cs
Original file line number Diff line number Diff line change
@@ -1,14 +1,19 @@
using SimpleJSON;


using SimpleJSON;
using ROSBridgeLib.std_msgs;
using UnityEngine;
using PointCloud;
//Fix Pointcloud...
//using PointCloud;

/**
* Define a PointCloud2 message.
*
* @author Miquel Massot Campos
*/

/*
namespace ROSBridgeLib {
namespace sensor_msgs {
public class PointCloud2Msg : ROSBridgeMsg {
Expand Down Expand Up @@ -121,3 +126,5 @@ public override string ToYAMLString() {
}
}
}
*/

0 comments on commit 8a22169

Please sign in to comment.