Unable to publish message to ROS Bridge from Unity
Hello,
I am trying to follow example from the submodule developed here which is a git submodule of unityros I was checking my interface by sending string message, however, my final intention is to send an array of float values coming from slider position I have.
I did have success in receiving messages from ROS side using the rosbridge by subscribing to the topic. But the other way round I cannot understand why this is not working.
Presently I am trying this:
#if UNITY_EDITOR
void Start(){
ros = new ROSBridgeLib.ROSBridgeWebSocketConnection("ws://192.168.142.67", 9090);
ros.AddPublisher(typeof(ROSCommsSlider));
}
#endif
#if UNITY_EDITOR
private void OnApplicationQuit(){
if (ros != null) {
ros.Disconnect();
}
}
#endif
#if UNITY_EDITOR
public void my_func() {
}
#endif
// Update is called once per frame
void Update() {
#if UNITY_EDITOR
if (Input.GetKeyDown(KeyCode.C))
{
ros.Connect();
}
//ros.Render();
ROSBridgeLib.std_msgs.StringMsg ms = new ROSBridgeLib.std_msgs.StringMsg("checking_connection");
ros.Publish(ROSCommsSlider.GetMessageTopic(), ms);
#endif
}
my ROSCommsSlider looks like this:
public class ROSCommsSlider : ROSBridgeLib.ROSBridgePublisher {
// my class for sending message to ROSbridge
public static string GetMessageTopic()
{
return "/joint_values";
}
public static string GetMessageType()
{
return "std_msgs/String";
}
public static string ToYAMLString(ROSBridgeLib.std_msgs.StringMsg msg)
{
return msg.ToYAMLString();
}
public new static ROSBridgeMsg ParseMessage(JSONNode msg)
{
return new ROSBridgeLib.std_msgs.StringMsg(msg);
}
}
When I run the ros bridge, for some reason it is creating 3 client connection instance, but however, it doesn't send in the string to my topic when I do:
rostopic echo /joint_values
I made sure I am sending this string as per rosbridge protocol
Sending {"op": "advertise", "topic": "/joint_values", "type": "std_msgs/String"}
UnityEngine.Debug:Log(Object)
ROSBridgeLib.ROSBridgeWebSocketConnection:Run() (at Assets/ROSBridgeLib/ROSBridgeWebSocketConnection.cs:195)
and an error:
InvalidOperationException: The current state of the connection is not Open.
WebSocketSharp.WebSocket.Send (System.String data)
ROSBridgeLib.ROSBridgeWebSocketConnection.Publish (System.String topic, .ROSBridgeMsg msg) (at Assets/ROSBridgeLib/ROSBridgeWebSocketConnection.cs:264)
ROSCommsMain.Update () (at Assets/Scripts/ROSCommsMain.cs:73)
which I don't understand why as under my rosbridge shows: Clients Connected.
Additionally, I receive this Null Reference Issue when close the unity editor play.
NullReferenceException: Object reference not set to an instance of an object
ROSBridgeLib.ROSBridgeWebSocketConnection.Disconnect () (at Assets/ROSBridgeLib/ROSBridgeWebSocketConnection.cs:170)
ROSCommsMain.OnApplicationQuit () (at Assets/Scripts/ROSCommsMain.cs:27)
Asked by arunavanag on 2018-01-04 20:25:57 UTC
Answers
Recently, siemens announced to rossharp to use ROS together with unity. I suggest to try this.
https://github.com/siemens/ros-sharp
Asked by jihoonl on 2018-01-05 00:00:48 UTC
Comments
I have a had a look at it before, but haven't yet tried out yet. I believe they are using the same websocket library as well, that is websocket-sharp.
Asked by arunavanag on 2018-01-05 02:37:42 UTC
I have tried the package from Siemens and it's working quite well (Could do a URDF-Import and send joint_state-messages from ROS to Unity to move the robot). BTW: Hello Jihoon ;)
Asked by NEngelhard on 2018-01-05 03:46:29 UTC
Can I use this package to send message from unity to ROS? all the tutorials, point towards ROS to unity direction. I could do that with my existing code. I had issues sending message from unity to ROS.
Asked by arunavanag on 2018-01-05 03:48:09 UTC
They publish images from a virtual camera in unity to ROS, so it should work, but I haven't tried it yet.
Asked by NEngelhard on 2018-01-05 06:51:43 UTC
Hi @NEngelhard I having hard time building it as an asset. I am unable to use ROS-Sharp at all.
How did you have it import ROSSharp and build it for ur project. Because websocketsharp rosbridgeclient and other dll files having issues with .net version mis match.
Asked by arunavanag on 2018-01-10 04:53:33 UTC
Which Windows and ROS Versions are you using?
Asked by NEngelhard on 2018-01-11 03:51:27 UTC
Windows 10 , ROS kinetic
Asked by arunavanag on 2018-01-11 03:56:44 UTC
Comments
This question is quite specific to a single package that is used in niche situations. The chances that ROS Answers visitors have experience with this is small. I would not expect any answers.
I would actually recommend to post this on the issue tracker of the pkg you're trying to use.
Asked by gvdhoorn on 2018-01-05 03:42:40 UTC
@gvdhoorn thanks, I will do so.
Asked by arunavanag on 2018-01-05 03:44:24 UTC
If you do post an issue, please post a comment here linking us to that issue, so we keep things connected and future readers can find possible solutions.
Asked by gvdhoorn on 2018-01-05 03:47:39 UTC
https://github.com/michaeljenkin/unityros/issues/11
Asked by arunavanag on 2018-01-06 02:13:30 UTC