[ROS2] msg stamp time difference
Hi,
I'm trying to assess if the msg is too old by comparing current time and the msg time stamp.
rclcpp::Time time_now = rclcpp::Clock().now();
double dt = (time_now - trajectory_buf_->header.stamp).nanoseconds();
However, the application crashes with "terminate called after throwing an instance of 'std::runtime_error' what(): can't subtract times with different time sources"
Alternatively, I tried it this way:
std_msgs::msg::Header::_stamp_type time_now = rclcpp::Clock().now();
double dt = (time_now - trajectory_buf_->header.stamp).nanoseconds();
This one doesn't compile with "no match for ‘operator-’"
What is the right way to do it?
I believe the issue here is not that the types of
time_now
andtrajectory_buf_->header.stamp
are different, it's that the value they contain came from a different time source (see Clock and Time/Clock/ROS Time Source for some background).the trajectory_buf message is being stamped in another node using
rclcpp::Clock().now()
call, that I'm also trying to use here. I don't see how the source is different. I looked through the article you mentioned, but didn't find the explanation.