ROS Resources: Documentation | Support | Discussion Forum | Index | Service Status | ros @ Robotics Stack Exchange
Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

Hi,

  1. 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_;  };
    
  2. Yes, it depends on the topic frequency which you subscribe to.

Hi,

  1. 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_;  };
    
  2. Yes, it depends on the topic frequency which you subscribe to. to and the callback function running time

Hope this will help!