jperkin / node-rpio

Raspberry Pi GPIO library for node.js
860 stars 126 forks source link

usleep #18

Closed ibmua closed 7 years ago

ibmua commented 8 years ago

It seems like microsecond sleep is seriously lacking precision. I was able to get much better results, though, still not too good, using node "microsleep" module to measure time in a silly while loop

while
    (
        microtime.now()
    <
        x
    )
    {
    }
mharsch commented 7 years ago

The sleep functions have an overhead cost that is relatively fixed (but does vary by pi model). e.g. calling usleep(0) takes about 72 microseconds on Rpi3, but takes about 130 microseconds on a Pi Model B. Calling usleep(1) takes about 73 microseconds on Rpi3 and 131 microseconds on PI Model B (and so on).

ibmua commented 7 years ago

You might want to include that in the docs 😉

mharsch commented 7 years ago

Correction: the 130 microsecond value was for for raspi-1 (model B), not raspi-2.

Regarding the issue of timing in general: I've observed a significant difference in the execution time of rpio functions between the first call (slower) and subsequent calls (faster). I suspect this variance is due to v8 recognizing the codepath and dynamically optimizing it for later calls. I mention this in the context of usleep() because my attempts to pad the timing of communication with an i2c device have failed due to this variance.

I wonder if there is some way to coerce node into being more deterministic.