0
$\begingroup$

I would like to get seconds, and nanoseconds from inside my node, so I have a timer_callback that executes each 100ms, to note seconds and nanoseconds of the rclcpp::Time

    void timer_callback() {
        rclcpp::Time t = this->now();
        cout << unsigned(t.seconds()) << '\t' << unsigned(t.nanoseconds()) << endl;
    }

What I get is, number of seconds since last epoch, and a uint32 representation of nanoseconds, that is actually accurate, (1sec=1e9ns), in which is measurable by chaning timer1 frequency, i.e. a diff of the nanoseconds is usable, but it does not reset each second, basically it has its own 4.2~ second epoch.

What is the epoch for nanoseconds?

Any ideas on how to get the seconds and nanoseconds to make a timestamp that is microseconds accurate?

Best Regards, C.A.

[node-1] 1697373475 3482658792

[node-1] 1697373475 3982695401

[node-1] 1697373476 187537493

[node-1] 1697373476 687584889

[node-1] 1697373477 1187751915

[node-1] 1697373477 1687731420

[node-1] 1697373478 2187763841

[node-1] 1697373478 2687771385

[node-1] 1697373479 3187776990

[node-1] 1697373479 3687628969
$\endgroup$
0

1 Answer 1

1
$\begingroup$

I think the issue is in the unsigned() cast.

Code changed to this:

rclcpp::Time t = this->now();
std::cout << t.seconds() << '\t' << t.nanoseconds() << std::endl;
std::cout << unsigned(t.seconds()) << '\t' << unsigned(t.nanoseconds()) << std::endl;

Gives output:

1.6974e+09  1697398304675212161
1697398304  3498322817

Nanoseconds is not a uint32:

rclcpp::Time::nanoseconds() returns a rcl_time_point_value_t, which is a typedef of an rcutils_time_point_value_t, which is a typedef of an int64_t.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.