forked from MathiasCiarlo/ROSBridgeLib
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
fc97591
commit 8a22169
Showing
5 changed files
with
166 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 (); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() + "}"; | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() + "}"; | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters