Ask Your Question
0

How to pull a message from queue with time interval?

asked 2017-08-16 04:16:20 -0500

Sergey Kravchenko gravatar image

When I subscribe my node, I set messages queue size and callback function:

int main(int argc, char **argv) {

    ros::init(argc, argv, "scs_drive");

    ros::NodeHandle n;

    ros::Subscriber sub = n.subscribe("scs", 1000, writePosCallback);

    ros::spin();
    return 0;
}

The incoming messages are being processed as soon as previous callback function operations are finished. But I want to process incoming messages from queue not ASAP but each millisecond.

So maybe it is possible to pull messages here:

int main(int argc, char **argv) {

    ros::init(argc, argv, "scs_drive");

    ros::NodeHandle n;

    ros::Subscriber sub = n.subscribe("scs", 1000, writePosCallback);

    ros::Rate loop_rate(1000);

    while (ros::ok()) {

        //some code here that pulls message from queue and process incoming data

        ros::spinOnce();
        loop_rate.sleep();
    }

    return 0;
}

Or I need to set pause in callback function and it is the one choice to process messages with a certain time interval?

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
1

answered 2017-08-16 05:33:23 -0500

gvdhoorn gravatar image

updated 2017-08-16 05:34:35 -0500

I don't think this is supported directly (ie: with some special spinner or something), but you could create your own CallbackQueue and then call CallbackQueue::callOne(..) at the appropriate time.

Note that this will mean that you're probably more likely to run into buffer overruns as spinOnce() actually calls callAvailable() which processes all available callbacks (here), so more regularly empties your queue.


It might already exist, but if you want to abstract this nicely you could implement a new spinner that implements this behaviour. Then you could instantiate that in your code instead of having to maintain your own CallbackQueue instance.

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: 2017-08-16 04:16:20 -0500

Seen: 330 times

Last updated: Aug 16 '17