ROS Resources: Documentation | Support | Discussion Forum | Index | Service Status | ros @ Robotics Stack Exchange
Ask Your Question
0

Reading from topic once using rospy.wait_for_message()

asked 2021-12-03 00:40:11 -0500

bhomaidan gravatar image

updated 2021-12-03 00:40:33 -0500

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.

edit retag flag offensive close merge delete

Comments

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

Can you tell me what error you are getting?

Ranjit Kathiriya gravatar image Ranjit Kathiriya  ( 2021-12-03 02:30:58 -0500 )edit

1 Answer

Sort by ยป oldest newest most voted
0

answered 2021-12-03 02:30:21 -0500

Ranjit Kathiriya gravatar image

updated 2021-12-05 07:59:04 -0500

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.

edit flag offensive delete link more

Comments

@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.

bhomaidan gravatar image bhomaidan  ( 2021-12-03 07:10:49 -0500 )edit

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.

Ranjit Kathiriya gravatar image Ranjit Kathiriya  ( 2021-12-03 07:16:18 -0500 )edit

@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??

bhomaidan gravatar image bhomaidan  ( 2021-12-03 07:20:10 -0500 )edit
1

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.

Ranjit Kathiriya gravatar image Ranjit Kathiriya  ( 2021-12-03 07:22:19 -0500 )edit
1

@RanjitKathiriya thanks, that solved my problem.

bhomaidan gravatar image bhomaidan  ( 2021-12-03 07:57:30 -0500 )edit

Tick the answer, if you got your answer. That can help others.

Ranjit Kathiriya gravatar image Ranjit Kathiriya  ( 2021-12-03 08:00:07 -0500 )edit

Question Tools

2 followers

Stats

Asked: 2021-12-03 00:40:11 -0500

Seen: 713 times

Last updated: Dec 05 '21