siemens / ros-sharp

ROS# is a set of open source software libraries and tools in C# for communicating with ROS from .NET applications, in particular Unity3D
Apache License 2.0
945 stars 364 forks source link

Unsubscribe and re-subscribe to different joint state topics at runtime #436

Closed Gescheld closed 1 year ago

Gescheld commented 1 year ago

I have a question!

Here is my question: I want to unsubscribe from my current joint state topic and subscribe to an other joint state topic at runtime with the same robot game object. Is there a way to this at runtime?

So far I wait unitl I press a button to subscribe to a joint state topic. But I couldn't figure out how to unsubscribe and re-subscribe. I tried to remove/destroy the comoponents and to add them again with the different topic name, but that didn't work either. Also, I tried to disable the JointStateSubscriber varibale (SetSubscribeJointStates(false)) and enable it again with a different topic at the JointStateSubscriber.cs component.

Those are the scripts components that are added at runtime:

Relevant Code:

1  RosSharpObject.AddComponent<RosSharp.RosBridgeClient.JointStatePatcher>();
2  // Set Urdf Robot gameobject
3  RosSharpObject.GetComponent<RosSharp.RosBridgeClient.JointStatePatcher>().UrdfRobot = robot_hologram.GetComponent<UrdfRobot>();
4 // Set subscribing value for joint states --> adds JointStateSubscriber.cs to game object
5  RosSharpObject.GetComponent<RosSharp.RosBridgeClient.JointStatePatcher>().SetSubscribeJointStates(true);
6  RosSharpObject.GetComponent<RosSharp.RosBridgeClient.JointStateSubscriber>().Topic = TopicDropdown.options[TopicDropdown.value].text;
MartinBischoff commented 1 year ago

Hi @Gescheld you will have to modify UnitySubscriber. This basic version just subscribes on Start().

You have to add e.g. the following functionality in e.g. Update(): if your doChangeSubscription condition is satisfied: unsubscribe and resubscribe

Gescheld commented 1 year ago

Hi @MartinBischoff,

Thank you for the fast answer. I don't have the opportunity to try this at the moment, but I will test it next time :)