Getting the rclcpp::Duration in microseconds
Hello everyone!
I am trying to calculate the time it takes for a single message from one Node to get to the other Node.
The way I calculate the difference is by substituting the timestamp of the received message from the present time of the node (rclcpp::Node::now()) and store it as rclcpp::Duration. Next, I convert the rclcpp::Duration to std::chrono::duration of microseconds, so that I can store it later in uint64_t. There is a method to achieve this as described here.
However, I seem to not understand, will this conversion convert only the nanoseconds? (assuming the seconds() represent amount of seconds since epoch, and nanoseconds() represent nanoseconds from seconds). Should I then separately convert seconds and nanoseconds, convert them to microseconds and then sum up?
Or, the to_chrono() converts both seconds and nanoseconds (ie total time since epoch) in nanoseconds?
The code is below:
void inputCallback(const digital_twin_msgs::msg::SupplyInput::SharedPtr msg)
{
if(msg->seq_id == p_input_sub->next_id) {
p_input_sub->recv_counter += 1;
rclcpp::Duration diff = rclcpp::Node::now() - msg->stamp;
auto num_of_us = diff.to_chrono<std::chrono::duration<uint64_t, std::micro>>(); // <- is this total time since epoch in microseconds?
uint64_t var = num_of_us.count();
p_input_sub->time_diffs.push_back(var);
}else{
p_input_sub->lost_count += 1;
}
p_input_sub->next_id = msg->seq_id + 1;
}
Thank you in advance!