sleep itself seems to take 9 ms
# help
r
try this
Copy code
main:
  t0 := Time.monotonic_us  
  1000.repeat:
    sleep --ms=1
  print (Time.monotonic_us - t0)
results in ~9800 instead of 1000. Increasing the sleep time makes the discrepancy go away.
Copy code
sleep --ms=0
does not take extra time. I found this because the stepper motor seemed much to slow
f
There is a certain overhead for setting the sleep up. Is that what you are experiencing or is there something else happening for small numbers like 1?
r
I think 9 ms is a lot of overhead. In the stepper 1 ms is used, that effectively is 10 ms making it 10 times slower than expected
f
I agree. Just making sure that's the actual issue.
r
When I run on host there is no overhead! 50 microseconds turns into 133 us
f
Might Reuther be the setup cost or using a timer that isn't precise enough. Both are different on the esp32
Just fyi: most of us are on vacation for the next week, so there is likely nothing going to happen in that time. 1. Please remind us when we come back (just in case we forget). 2. As a workaround I would:
Copy code
end := Time.monotonic_us + 10_000
while end > Time.monotonic_us: yield
r
Actually, when the host is Windows, the overhead is 15 ms instead of near 0 on Linux
k
It's a timer resolution issue, not really overhead per se.
On FreeRTOS, the smallest amount of time we can wait is a tick, which defaults to 10ms. Haven't looked at Windows lately.
If we can find a better primitive to support precise, cancelable sleep I'd be very supportive of using that instead of pthread_cond_wait with a timeout.
r
Thanks for the info. Maybe worth documenting, because the form sleep --ms= suggests milliseconds are meaningful. Anyway, this makes the steppermotor 10 times slower than expected
k
Yeah, that's a valid point. Maybe we can find a better way to support that than repeated sleeps. Thanks for bringing this up 🙏
r
For the stepper I think I have seen software that uses RMT. For the sleep you may need to roll your own; after all you have a microsecond clock in Time.monotonic_us
k
You can use RMT on Toit.