Skip to content

Conversation

@dhalbert
Copy link
Collaborator

@dhalbert dhalbert commented Feb 7, 2020

Fixes #2334. Fixes #2508.

  • Increase precision of time.monotonic_ns() to microseconds on all ports. I need some rough timing values that were more granular than milliseconds.
  • Round time.sleep() to the nearest millisecond. Tested time.sleep() rounds down to next lower millisecond #2508 sample program and now get 100 Hz as intended.

time.monotonic_ns() and time.sleep() should still not be used for precise timing, but this makes them more useful than they were before.

Note that time.monotonic() still increases only on milliseconds. It was not worth improving due to the low precision of 30-bit floats.

@dhalbert dhalbert requested a review from tannewt February 7, 2020 15:40
Copy link
Member

@tannewt tannewt left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks great! Thank you.

@timchinowsky
Copy link

Noticing that the RP2040 port of monotonic_ns() only has millisecond precision - the subtick value is not used:

uint64_t port_get_raw_ticks(uint8_t *subticks) {
uint64_t microseconds = time_us_64();
return 1024 * (microseconds / 1000000) + (microseconds % 1000000) / 977;
}

This seems to work for a fix:

uint64_t port_get_raw_ticks(uint8_t *subticks) { uint64_t microseconds = time_us_64(); if (subticks != NULL) { *subticks = (uint8_t) (((microseconds % 1000000) % 977) / 31); } return 1024 * (microseconds / 1000000) + (microseconds % 1000000) / 977; }

Test code to look at difference between successive calls to monotonic_ns():

import time def test_ns(n=200): t0 = time.monotonic_ns() for i in range(n): t1 = time.monotonic_ns() print(t1-t0) t0 = t1 test_ns()

Plotting the returned data from before and after the change:

image

Before the change, the data is granular to 1 ms, e.g.

976563 0 0 0 0 976562 0 0 0 0 976563 

After, it does better:

244144 213613 183108 183108 213626 213626 213612 244144 152590 213626 244131 
@jepler
Copy link

jepler commented May 7, 2024

Noticing that the RP2040 port of monotonic_ns() only has millisecond precision - the subtick value is not used:

Good catch. Feel like filing a PR against our main branch to fix it?

@timchinowsky
Copy link

Noticing that the RP2040 port of monotonic_ns() only has millisecond precision - the subtick value is not used:

Good catch. Feel like filing a PR against our main branch to fix it?

Yes, will do.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

4 participants