Unsubscribing from topics [closed]

asked 2015-02-24 15:11:22 -0600

Aracanid gravatar image

Hi there. I'm trying to implement a feature that would allow me to unsubscribe and stop publishing messages to certain topics, this is so that another node may begin publishing and subscribing to said topics. However when attempting this I am able to subscribe without a problem, but the nodes are never able unsubscribe and instead every node ends up publishing, causing unwanted messages to be sent and received. Below is an example of the code I am using to unsubcribe from the topics.

def unsubscribe_node(self):
    # Function to unsubscribe a node from its topics and stop publishing data
    with self.subscriber_lock:
        self.is_offloaded = True
        self.face_box_sub.unregister()
        self.output_image_pub.unregister()
        self.motor_commands_pub.unregister()

And here is the resubscribe function:

def resubscribe_node(self):
    # Function to resubscribe and republish the nodes data
    with self.subscriber_lock:
        self.is_offloaded = False
        self.output_image_pub = rospy.Publisher(self.output_image, Image, queue_size=self.queue_size)
        self.face_box_sub = rospy.Subscriber(self.face_box_coordinates, FaceBox, self.update_face_box, queue_size=self.queue_size)
        self.motor_commands_pub = rospy.Publisher(self.motor_commands, MotorCommand, queue_size=self.queue_size)

Is there anything I am doing wrong that I have missed?

Any help you guys can give would be great.

edit retag flag offensive reopen merge delete

Closed for the following reason the question is answered, right answer was accepted by Aracanid
close date 2015-02-26 12:44:14.990964

Comments

1

Is there any reason why you want to unsubscribe? The ROS topic system is designed such that you can have multiple publishers and subscribers on one topic. This is less confusing, because when you do "rostopic list", you see the complete output. I suggest you simply stop calling the "publish" method.

Valts gravatar image Valts  ( 2015-02-24 19:13:00 -0600 )edit
1

In roscpp, the Subscriber class has a shutdown() method. Is unregister the right method?

Tom Moore gravatar image Tom Moore  ( 2015-02-24 21:01:47 -0600 )edit
1

I have never had to use it, but from the class reference, it seems shutdown() is the right method:

Unsubscribe the callback associated with this Subscriber. This method usually does not need to be explicitly called, as automatic shutdown happens when all copies of this Subscriber go out of scope

Valts gravatar image Valts  ( 2015-02-24 22:05:21 -0600 )edit

Even though methods might exist to do what you want to, I think there are conceptual flaws. @Valts' comment is correct.

McMurdo gravatar image McMurdo  ( 2015-02-25 07:36:12 -0600 )edit

Thanks for your responses guys. I do actually need to unsubscribe the node because the program is meant to simulate computational offloading by doing so. shutdown() doesn't seem to do anything for me :/ The terminal outputs seem to show that the unsubscribing works, but the Node_Graph doesn't.

Aracanid gravatar image Aracanid  ( 2015-02-25 10:07:56 -0600 )edit

Do you need the graph to reflect it? The callbacks aren't being fired, which, unless I misunderstand your purpose, should be sufficient, no?

Tom Moore gravatar image Tom Moore  ( 2015-02-25 11:06:07 -0600 )edit

Subscribers shut down automatically when they go out of scope. Just try setting them to None. Should do the trick.

Chrissi gravatar image Chrissi  ( 2015-02-26 05:27:41 -0600 )edit

Setting it to None also seems to do the trick but still no reflection on the node graph. I suppose editing the node graph source code will be the solution. Thanks for your help guys :)

Aracanid gravatar image Aracanid  ( 2015-02-26 12:43:45 -0600 )edit