Hello.
What is the limits of the sleep command? I need a 0.000666666665 delay ) but it does not seem to work. Sometimes it is perfect but most times it all over the page. It not constant.
Possibly, what youâre experiencing, is that itâs taking longer to execute sleep(0.000666666665)
than the delay of the time period.
From what I understand, execution times [of Python scripts] will vary and are dependent on what else your system is having to do, but on the whole, the sleep function is quite accurate.
Iâm no expert, but I suspect that sleep() will pause for at least the time you request. If thereâs something slowing the system down, it could be quite a bit longer. Youâre not likely to notice with sleep times of O(one second) but youâre asking for less than a microsecond â no non real-time system is going to be consistent with that level of time precision.
If you really, really need such a fine-grained pause, you might want to read up on the topic of real-time operating systems. âRetailâ OSes like Windows, MacOS and many flavors of Linux generally donât have real-time guarantees as part of their feature set.
I donât see any mention of the timeit
module so far, so Iâll point out that it can be useful in this context. Something like this command
python -m timeit -s 'from time import sleep' 'sleep(0)'
should give you a good idea of the overhead of calling time.sleep
. On my Mac itâs about 340ns, so about 0.00034s. Add your desired sleep time and youâll see youâll likely be way over budget on your naps. Thatâs without any operating system delays.
Youâre off by a factor of 1000. 0.00034s is 340”s. 340ns is 0.00000034s.
Youâre off by a factor of 1000. 0.00034s is 340”s. 340ns is 0.00000034s.
Thanks. Thatâs what I get for asking Google for â340ns in secondsâ then not starting harder at the answer. Damn AIâŠ
No, thatâs wrong too. I did ask Python for 340*1e-6. Stupid, stupid, stupid.
When you ask a computer to sleep for a certain amount of time, you will often not get control back at precisely that time, and actually there are occasionally reasons that youâll get control sooner than the time requested. What are you attempting to do here? Are you attempting to do something every 2/3 ms? If so, I would recommend having some sort of loop with a high resolution timer in it; effectively, call sleep, and then see how many times you need to tick your counter.
But itâs worth keeping in mind that this sort of precision is HARD. Have you ever played a video game at 1500 FPS? I donât think Iâve ever seen anything that runs at that kind of frame rate with any consistency.
That delay is correct. I take the required Hz pulse width and then take that value and divided it by 2.
For example, 720 Hz has a pulse width of 0.0013333 seconds. Then I divide that by 2 resulting pulse width is 0.0006667 seconds That result is the value of the sleep() command.
Looking at the following code this sleep value will produce a 50% duty cycle.
while True:
GPIO.output(Step_Pin), not(GPIO.input(Steop_Pin)))
sleep(Delay)
Hope that clears up the confusion about sleep values
Ah, yeah, thatâs not going to be nearly reliable enough. The time taken to run through the Python code for twiddling the GPIO pin is going to affect your output frequency.
Are you able to utilize a dedicated timing chip?
Yes, I am thinking about using an Adafruit ItsyBitsy 32u4 to produce the required timing. pulses using the delayMicro() function. According to the info about it, the shortest delay is 3 microseconds. ( 333.333 KHz ).
I am hoping once the code is correct I can find someone to burn the code to the unit so I do not have to reload the code every time I boot it up.