Search Unity

  1. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Possibility to unsubscribe subscriber or unregister publisher?

Discussion in 'Robotics' started by Tuntenfisch, Sep 4, 2023.

  1. Tuntenfisch

    Tuntenfisch

    Joined:
    May 14, 2015
    Posts:
    11
    Hello,

    I'm using ROS-TCP-Connector to establish a connection between ROS and Unity. The
    ROSConnection
    class provides functions like

    • Subscribe<T>(string topic, Action<T> callback)
    • SubscribeByMessageName(string topic, string rosMessageName, Action<Message> callback)

    • RegisterPublisher<T>(string rosTopicName, int? queue_size = null, bool? latch = null)
    But I see no way to unsubscribe a subscriber, i.e. once you subscribe using a callback, that callback gets called for all eternity, even if you are no longer interested in the topic. In a similar fashion, a publisher will stay registered because there is no mechanism to unregister. Although, in case of the publisher I'm not sure what the implications are.

    I haven't used services, yet, so I cannot say whether the same functionality is missing there, too.

    The reason I'm interested in this functionality is because I'm trying to abstract the singleton nature of the
    ROSConnection
    class away, by introducing separate
    ROSPublisher
    and
    ROSSubscriber
    classes:



    The idea is that developers in my team instantiate the newly introduced classes
    ROSPublisher
    and
    ROSSubscriber
    to publish and subscribe to topics. Once they are done, they simply call
    Dispose()
    on them. But, unless
    ROSConnection
    offers some mechanism to unregister/unsubscribe, there is no real way to dispose the instances.
     
    Last edited: Sep 4, 2023
  2. Envilon

    Envilon

    Joined:
    Aug 8, 2020
    Posts:
    55
    Hi! There is an Unsubscribe method in the ROSConnection. I was using it in my project to unsubscribe from a topic when I no longer wanted to receive messages from it.

    As for the publishers, it is true that it seems there is no such method in the ROSConnection. However, I just found this SendPublisherUnregistration method in some InternalAPI, so maybe it was planned, and it would be possible to use it somehow. I did not take a closer look. When I was implementing this feature in my project, I simply set `enabled = false` because I was publishing my messages from the Update method. When `enabled == fasle`, the MonoBehaviour won't run the Update method.
     
  3. Tuntenfisch

    Tuntenfisch

    Joined:
    May 14, 2015
    Posts:
    11
    Oh, I totally forgot to talk about
    Unsubscribe(string topic)
    . You're correct, thats a thing. But as far as I can tell, it unsubscribes all callbacks from a given topic. So if I had two
    ROSSubscriber
    instances subscribed to the same topic and one of them calls
    Unsubscribe(string topic)
    during dispose, the other one would also suddenly stop receiving messages.

    I'm looking for a mechanism like
    Unsubscribe(topic, callback)
    so that I can unsubscribe a specific callback.

    I technically could create another class which acts as an adapter to
    ROSConnection
    . The
    ROSConnectionAdapter
    could then replicate the API of
    ROSConnection
    (while keeping track of individual callbacks when subscribing and providing an additional API to unsubscribe those callbacks again). Put that would be a pain in the butt...
     
  4. Envilon

    Envilon

    Joined:
    Aug 8, 2020
    Posts:
    55
    Ah, that is true, it does unsubscribe everything from the topic. In my case, I was only switching between different subscribers, and I only had a single active subscription at a time, so I didn't find this issue.

    Maybe it would be worth raising an issue in the ROS-TCP-Connector repository (as a feature request), but I highly doubt anything would happen anytime soon. So... unfortunately, you might need to implement this yourself. :(
     
  5. Tuntenfisch

    Tuntenfisch

    Joined:
    May 14, 2015
    Posts:
    11
    Yeah, I think you are right about raising the issue part. There's not been much activity in the ROS-TCP-Connector repository for quite some time now. Would be nice to know if someone in Unity is still maintaining it.

    I'll probably end up implementing it myself on top of the API provided by
    ROSConnection
    . Thanks for you reply. :)
     
    Envilon likes this.