Ask Your Question
1

How to quit/exit from a subscriber

asked 2013-02-25 05:51:42 -0500

safzam gravatar image

updated 2013-02-26 02:52:08 -0500

dornhege gravatar image

Hey all, I have two publishers with two topics using rospy. I have a third node that subscribe to each of both topics in sequence. I want to use one subscriber for a specified times (may be 10 or 20) and after that exit from this subscriber. Then I subscribe to second and want it to be automatically stopped after given number of times as above. I scanned the google I came to know subscriber.unregister() but it gives error that "'tuple' has no attribute 'unregister'". Any solution to exit from rospy subscriber automatically with out pressing Crl+C.

sb = None 
sb=rospy.Subscriber(topic1,type1,self.callback,(self,sb))
rospy.spin()
sb=rospy.Subscriber(topic2,type2,self.callback,(self,sb))
rospy.spin()

def callback(self,sb):
 self.c = self.c + 1
 if self.c == 10:
    sb.unregister()

Thanks

edit retag flag offensive close merge delete

Comments

According to the API that should work. Can you come up with a minimal example to reproduce this.

dornhege gravatar image dornhege  ( 2013-02-25 06:44:46 -0500 )edit

sb = None; sb=rospy.Subscriber(topic1,type1,self.callback,(sb)); rospy.spin(); sb=rospy.Subscriber(topic2,type2,self.callback,(sb); rospy.spin() def callback(self,sb): self.c = self.c + 1; if self.c == 10: sb.unregister();

safzam gravatar image safzam  ( 2013-02-25 09:54:19 -0500 )edit

Please update your original post with the code. It's not readable in the comment. As far as I can see, something with the parentheses seems wrong.

dornhege gravatar image dornhege  ( 2013-02-25 23:14:02 -0500 )edit

I have included code up, but its mixed with message. Infact code creates a subscriber and call a callback, I want that callback should be closed after few iteration and then next comes. Both subscriber use the same call back function with differe messagetype. I wanna know how to quit callback.thnx

safzam gravatar image safzam  ( 2013-02-26 00:49:45 -0500 )edit

1 Answer

Sort by ยป oldest newest most voted
0

answered 2013-02-26 02:55:25 -0500

dornhege gravatar image

Looks like you're passing the tuple (self, sb) as the callback args and this tuple now is passed to the callback function under the name sb.

edit flag offensive delete link more

Comments

but my question is different. I wanna know how to exit from a callback. unregister does not work in this case, It giver error no attribute as "unregister". Is there any way to exit from a call back after letting it execute for some specified times???? thanks

safzam gravatar image safzam  ( 2013-02-27 03:50:54 -0500 )edit

unregister does, what it is supposed to do. The problem is that you are not calling unregister on the subscriber, but on the tuple that you pass in.

dornhege gravatar image dornhege  ( 2013-02-27 04:29:57 -0500 )edit

ok then can you please write to me how can I call it on the subscriber? just simple example would be nice. thnx

safzam gravatar image safzam  ( 2013-02-27 05:06:52 -0500 )edit

I think the problem that you are not passing a subscriber, but a tuple with (self, subscriber) to your function.

dornhege gravatar image dornhege  ( 2013-02-27 06:54:46 -0500 )edit

Infact in tuple 'self' I need to keep the data in the callback. But I have checked it by removing the tuple then I get error there is no attribute 'unregister'. I would aks you to just write to me the definiton and call of the callback how can I pass only subscriber please? Thanks

safzam gravatar image safzam  ( 2013-02-28 00:41:35 -0500 )edit

Given that you are within a class, I'd suggest to use self.sb = rospy.Subscriber... not passing any other values and within the callback call self.sb.unregister().

dornhege gravatar image dornhege  ( 2013-02-28 01:09:45 -0500 )edit

thank you!

safzam gravatar image safzam  ( 2013-03-04 23:43:24 -0500 )edit

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

1 follower

Stats

Asked: 2013-02-25 05:51:42 -0500

Seen: 2,348 times

Last updated: Feb 26 '13