Unable to publish message to ROS Bridge from Unity [closed]
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)
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.
@gvdhoorn thanks, I will do so.
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.
https://github.com/michaeljenkin/unit...