所以,我正在使用RPi进行一个项目,并且(使用python代码(,我希望它每0.00001秒检查一次某些内容,并注意满足条件所需的时间。
我正在使用time.sleep()
,通过导入时间
更高,则只有这样它才能工作,如果它低于 0.001,那么时间会变慢或更快,例如。检查 3 秒需要大约 1 秒...
我能做什么?你能建议其他方法吗,除了时间睡眠?
下面的代码只是说
if GPIO 16 on the pi is high,
then, until GPIO 32 is not high, it will time the time
当 GPIO 32 为高电平时,循环中断并代码结束
第一个while循环是这样程序不断循环,我需要它这样做,你可以忽略
import RPi.GPIO as hello
import time
hello.setwarnings(False)
hello.setmode(hello.BOARD)
hello.setup(16, hello.IN)
hello.setup(32, hello.IN)
t=0
while 1:
##Main Code, with the problem...
while 1:
t=0
if hello.input(16)==1:
print(t)
while hello.input(32)==0:
t=t+0.00001
time.sleep(0.00001)#Not Working
if hello.input(32)==1:
print(t)
print("Speed=",14/t,"cm/s")
break
所有时间关键型函数都有限制。我建议过度考虑使用sleep((并使用Python纪录片中所述的计时器对象。https://docs.python.org/2.4/lib/timer-objects.html
这里至少有两点需要注意:
- 要
time.sleep()
和基础系统调用的参数是下限。无法保证系统不会休眠更长时间。(事实上,我们可以预测它会,如果系统负载很轻,通常只是少量。 - 在纳秒时间范围内执行任何 Python 代码在当代硬件上都是不现实的。
>>> import timeit >>> timeit.timeit('pass', number=10000) 8.20159912109e-05