ROS Resources: Documentation | Support | Discussion Forum | Index | Service Status | ros @ Robotics Stack Exchange |
1 | initial version |
Hi,
You can add a member variable in your class to save the last time when you get to msg.
class moving_robot{
public:
moving_robot() : last_time_(ros::Time::now())
{
sub = nh.subscribe("/cmd_vel", 10, &moving_robot::callback, this);
}
private:
void callback(const geometry_msgs::Twist::ConstPtr &msg)
{
ros::Time curr_time = ros::Time::now();
double dt = (curr_time - last_time_).toSec();
ROS_INFO_STREAM("dt: " << dt);
last_time_ = curr_time;
// other works
}
ros::NodeHandle nh;
ros::Subscriber sub;
ros::Time last_time_; };
Yes, it depends on the topic frequency which you subscribe to.
2 | No.2 Revision |
Hi,
You can add a member variable in your class to save the last time when you get to msg.
class moving_robot{
public:
moving_robot() : last_time_(ros::Time::now())
{
sub = nh.subscribe("/cmd_vel", 10, &moving_robot::callback, this);
}
private:
void callback(const geometry_msgs::Twist::ConstPtr &msg)
{
ros::Time curr_time = ros::Time::now();
double dt = (curr_time - last_time_).toSec();
ROS_INFO_STREAM("dt: " << dt);
last_time_ = curr_time;
// other works
}
ros::NodeHandle nh;
ros::Subscriber sub;
ros::Time last_time_; };
Yes, it depends on the topic frequency which you subscribe to. to and the callback function running time
Hope this will help!