Robotics StackExchange | Archived questions

How to read only once a message published in a topic?

I created a ROS node that continuously publishes a geometry_msgs/Pose type message within a topic, the published information comes from a sensor, I would like to know if it is possible to read only a single topic message instead of reading all the messages that are being published to the queue.

The following image shows the information that is published within the topic. How can I get the information from a single message to store it inside a variable?

image description

If I use a subscriber node, the callback() function reads all the information that is being published on the topic and I only want to access a single message.

Asked by LuisEFA1998 on 2022-07-04 20:03:15 UTC

Comments

You can "echo" or print a single message from a particular ROS topic using:
rostopic echo -n COUNT where COUNT equals the number of messages you want to print or see.

I figured this out using rostopic echo -h.

Cheers

Asked by electrophod on 2022-07-05 00:42:16 UTC

I've closed this as a duplicate, as it is a duplicate of at least #q270298, #q222029, #q292582, #q62176 and a couple more.

You might have done so already, but for future readers: please search before posting. And use Google: append site:answers.ros.org to your search query. For me, subscribe only once site:answers.ros.org returns many results, including the Q&As I've linked.

Asked by gvdhoorn on 2022-07-05 01:36:01 UTC

Additionally, @LuisEFA1998:

The following image shows the information that is published within the topic.

please do not post screenshots of terminals. This goes against the support guidelines and is also unnecessary. It's all text, so copy-paste the text into your question, then format it using the Preformatted Text button (the one with 101010 on it).

Asked by gvdhoorn on 2022-07-05 01:37:35 UTC

Answers