guild icon
Toit
#sleep itself seems to take 9 ms
Thread channel in help
RobvanLopik
RobvanLopik 04/03/2023 02:08 PM
try this
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.
sleep --ms=0 does not take extra time.
I found this because the stepper motor seemed much to slow
floitsch
floitsch 04/03/2023 02:47 PM
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?
RobvanLopik
RobvanLopik 04/03/2023 03:07 PM
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
floitsch
floitsch 04/03/2023 03:08 PM
I agree. Just making sure that's the actual issue.
RobvanLopik
RobvanLopik 04/03/2023 03:13 PM
When
I run on host there is no overhead! 50 microseconds turns into 133 us
floitsch
floitsch 04/03/2023 03:14 PM
Might Reuther be the setup cost or using a timer that isn't precise enough. Both are different on the esp32
floitsch
floitsch 04/03/2023 03:17 PM
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:
end := Time.monotonic_us + 10_000 while end > Time.monotonic_us: yield
RobvanLopik
RobvanLopik 04/03/2023 04:14 PM
Actually, when the host is Windows, the overhead is 15 ms instead of near 0 on Linux
bitphlipphar
bitphlipphar 04/03/2023 05:36 PM
It's a timer resolution issue, not really overhead per se.
bitphlipphar
bitphlipphar 04/03/2023 05:37 PM
On FreeRTOS, the smallest amount of time we can wait is a tick, which defaults to 10ms. Haven't looked at Windows lately.
bitphlipphar
bitphlipphar 04/03/2023 05:38 PM
Program your microcontrollers in a fast and robust high-level language. - toit/os_win.cc at 995ecf05f0659ac0d3347bd84d181e54a4def311 · toitlang/toit
bitphlipphar
bitphlipphar 04/03/2023 05:40 PM
Program your microcontrollers in a fast and robust high-level language. - toit/timer.cc at 995ecf05f0659ac0d3347bd84d181e54a4def311 · toitlang/toit
bitphlipphar
bitphlipphar 04/03/2023 05:56 PM
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.
RobvanLopik
RobvanLopik 04/03/2023 06:04 PM
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
bitphlipphar
bitphlipphar 04/03/2023 06:05 PM
Yeah, that's a valid point. Maybe we can find a better way to support that than repeated sleeps. Thanks for bringing this up :🙏:
RobvanLopik
RobvanLopik 04/03/2023 07:11 PM
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
bitphlipphar
bitphlipphar 04/03/2023 07:20 PM
You can use RMT on Toit.
bitphlipphar
bitphlipphar 04/03/2023 07:22 PM
18 messages in total