Ask Your Question
1

Change publishing rate in source code

asked 2016-07-18 08:04:48 -0600

ROSkinect gravatar image

updated 2016-07-19 03:42:42 -0600

I'm subscribing to /camera/depth/image topic, after some processing I want to publish it again but in different rate.

I'm already trying to do it using some code but I didn't succeed yet. (Publishing is working)

I found a very short answer that I'm trying to follow but not very clear enough. Here

EDIT - SOLUTION:

I found a solution in throttle package, but I don't get really exactly the rate I input which is g_period=msgs_per_sec in the package:

The rate I get is:

rostopic hz /depth_output 

average rate: 15.018
    min: 0.063s max: 0.069s std dev: 0.00187s window: 14

Here is it:

ros::Time g_last_time;
ros::Duration g_period;

g_period = ros::Duration(1.0/20); //msgs_per_sec

ros::Time now;
now = ros::Time::now();
if((now - g_last_time) > g_period)
 {
   depth_pub_.publish(depth_msg.toImageMsg());
   g_last_time = now;
 }
edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
1

answered 2016-07-19 04:28:10 -0600

updated 2016-07-19 04:29:13 -0600

You can use ros::Rate where frequency is the desired frequency.

// Run at the configured rate, discarding msgs if necessary

ros::Rate rate(frequency);

//main loop

while (ros::ok())
  {
  ros::spinOnce();
  rate.sleep();

  if (std::abs((rate.expectedCycleTime() - ros::Duration(1.0 / frequency)).toSec()) > 0.001)
  {
      // Change rate dynamically; if must be above 0, as 0 will provoke a segfault on next spinOnce
      ROS_DEBUG("Changing frequency from %.2f to %.2f", 1.0 / rate.expectedCycleTime().toSec(), frequency);
      rate = ros::Rate(frequency);
  }

}
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

1 follower

Stats

Asked: 2016-07-18 08:04:48 -0600

Seen: 1,026 times

Last updated: Jul 19 '16