0
$\begingroup$

Rosanswers logo

In a previous answer a similar issue was addressed but solved in another manner, however the solution suggsted:

def cb_once(msg, subscriber):
    #do processing here
    subscriber.unregister()

sub_once = None
sub_once = rospy.Subscriber('my_topic,', MyType, cb_once, sub_once)

However since you the right hand side of the last line has to finish before equating to the left hand side, thus sub_once point to wrong place, hence in the cb_once, subscriber.unregister() can't work. Is there another way?

Many Thanks

Guy


Originally posted by Phuicy on ROS Answers with karma: 46 on 2012-10-23

Post score: 3


Original comments

Comment by Bill Smart on 2013-07-02:
When you say "can't work" do you mean "I don't think it works" or "I tried it and it doesn't work"? The callback isn't called by Subscriber, although (I think that) there's a small chance that it will fire before Subscriber returns (very small, probably).

Comment by Bill Smart on 2013-07-02:
You can always wrap the callback code in a try block if you're worried that it will fail.

$\endgroup$

2 Answers 2

0
$\begingroup$

Rosanswers logo

I know it's been over a year but I just had this same problem and found this post. Just make sub_once a global variable.

def cb_once(msg):
   #do processing here
   sub_once.unregister()

global sub_once
sub_once = rospy.Subscriber('my_topic,', MyType, cb_once)

Originally posted by Airuno2L with karma: 3460 on 2013-07-02

This answer was ACCEPTED on the original site

Post score: 11


Original comments

Comment by Phuicy on 2013-07-02:
Thanks, I will try but I am skeptical. As the '''rospy.subscriber'' is processed first and the hence the callback ''cb_once" then ''sub_once = ", thus the '''sub_once" in the callback isnt point to the subscriber yet.

Comment by Phuicy on 2013-07-02:
Btw, thank you for replying. It what makes a good community.

Comment by felix k on 2013-07-03:
Wouldn't that be fixed by placing a global sub_once in the callback? (And moving the var to the top)

Comment by inkspell4 on 2017-02-26:
I can confirm that this will work. cb_once will only be called when a message has been received at which point if global sub_once is included in both the callback and in the place making the subscriber the line sub_once.unregister() will reference the subscriber created even below it

$\endgroup$
0
$\begingroup$

Rosanswers logo

An easy way to get a single message is this:

 msg = rospy.wait_for_message("my_topic", MyType)

This returns the message, and shuts down after receiving one message. You can specify a timeout as the third argument. It also doesn't require using a global.


Originally posted by Benjamin Blumer with karma: 287 on 2014-06-10

This answer was NOT ACCEPTED on the original site

Post score: 16


Original comments

Comment by 130s on 2014-10-20:
+1. Depending on the purpose though, I think this is cleaner and easier.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.