ROS Resources: Documentation | Support | Discussion Forum | Index | Service Status | Q&A answers.ros.org
Ask Your Question
0

Retrieve first message in a topic

asked 2020-08-24 08:35:53 -0600

fun01 gravatar image

Hi,

I need to retrieve the first message from a ros topic and store it in variable or a file. Is there a way to do so?

Thanks in advance!

edit retag flag offensive close merge delete

Comments

Starting rosbag record before starting the node that publishes the required ROS message, might be a way to do it.

praskot gravatar image praskot  ( 2020-08-24 18:24:46 -0600 )edit

@fun01 You may also want to check WaitForMessage that will wait for a single message to arrive on a topic. Another possible solution is to have a subscriber and in the callback unsubscribe after the first call.

Weasfas gravatar image Weasfas  ( 2020-08-25 04:16:05 -0600 )edit

1 Answer

Sort by ยป oldest newest most voted
1

answered 2022-05-03 08:17:43 -0600

Vic gravatar image

If your node is started before the topic is running, you can create a subscriber and a static boolean in your callback (assuming you're working in C++ and ROS1) :

static bool isFirstTime = true;
static geometry_msgs::Pose firstPose

static void callbackSubscriber(const geometry_msgs::Pose::ConstPtr& msg)
{
    if(isFirstTime)
    {
        isFirstTime = false;
        firstPose = msg;
    }
}

Disclaimer : I haven't tested, just wrote it off to give a global idea. Not sure this is a "clean" solution too.

edit flag offensive delete link more

Your Answer

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

Add Answer

Question Tools

2 followers

Stats

Asked: 2020-08-24 08:35:53 -0600

Seen: 232 times

Last updated: May 03