0
$\begingroup$

Rosanswers logo

Hi, I want to read a message from a topic once, thus I tried to use wait_for_message() like this:

topic_name = "/yumi/rws/system_states"
system_info = rospy.wait_for_message(topic_name, SystemState, timeout=5)

but the timeout was reached, and I couldn't read a message from the topic!

The topic is publishing messages, and I was able to check them from the terminal using:

rostopic echo "/yumi/rws/system_states", how can I read a message from the topic once? thanks in advance.


Originally posted by bhomaidan on ROS Answers with karma: 114 on 2021-12-03

Post score: 0


Original comments

Comment by Ranjit Kathiriya on 2021-12-03:\

but the timeout was reached, and I couldn't read a message from the topic!

Can you tell me what error you are getting?

$\endgroup$

1 Answer 1

0
$\begingroup$

Rosanswers logo

Maybe you can unsubscribe from the topic after a 1-time call.

You can do so, by calling subscriber.unregister() this function.

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

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

Or

-- Solution

not a ros node, can that be possibly the problem for endless waiting??

have you created rospy.init_node? If not, I think you cannot execute this line without initializing the node.

system_info = rospy.wait_for_message(topic_name, SystemState, timeout=5)

This syntax it looks correct.


Originally posted by Ranjit Kathiriya with karma: 1622 on 2021-12-03

This answer was ACCEPTED on the original site

Post score: 0


Original comments

Comment by bhomaidan on 2021-12-03:
@RanjitKathiriya There is no spinOnce in rsopy, and I have read that some users were able to use wait_for_message instead, that's why I do like to use it if possible, but I'm not sure if I'm using it correctly.

Comment by Ranjit Kathiriya on 2021-12-03:\

I have read that some users were able to use wait_for_message instead, that's why I do like to use it if possible

I have checked your syntax it looks correct. If possible can you re-edit your question and share the full code file. That's why I was asking for a detailed error in your question comment.

Moreover, you also can use unregister() this is also a good option to consider.

Comment by bhomaidan on 2021-12-03:
@RanjitKathiriya I'm using this line system_info = rospy.wait_for_message(topic_name, SystemState, timeout=5) inside a normal function, and not a ros node, can that be possibly the problem for endless waiting??

Comment by Ranjit Kathiriya on 2021-12-03:\

not a ros node, can that be possibly the problem for endless waiting??

have you created rospy.init_node? If not, I think you cannot execute this line without initializing the node.

system_info = rospy.wait_for_message(topic_name, SystemState, timeout=5)

This syntax it looks correct.

Comment by bhomaidan on 2021-12-03:
@RanjitKathiriya thanks, that solved my problem.

Comment by Ranjit Kathiriya on 2021-12-03:
Tick the answer, if you got your answer. That can help others.

$\endgroup$

Your Answer

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