Why does the POSIX API clock_gettime() have different precision on SITL and drone?

I want to calculate the relative time difference in nanoseconds (e.g. the execution time of a branch) via the POSIX API clock_gettime(). I can easily get the exact value of the time difference when using SITL, but on the drone, this time difference is always 0.
As far as I know, PX4 should be calling Nuttx’s clock_gettime() API.

Why don’t you use hrt_absolute_time() which gives you microseconds?

I want to get nanosecond times, hrt_absolute_time() doesn’t provide nanosecond times.

I see. I don’t think you can get nanosecond resolution in the current PX4 NuttX configuration. You have to do some research how to enable it.

Look for CONFIG_RTC_HIRES in System Time and Clock — NuttX latest documentation

I wonder why you need to get nanoseconds? Is it for benchmarking? If so, I’d suggest to do the same thing multiple times and measure microseconds. That’s likely more accurate than fiddling with nanoseconds, because measuring time in nanoseconds is likely going to have overhead.

1 Like

Thank you for your advice, it was very helpful to me.

1 Like