因此,我正在使用RPi开发一个项目,并且(使用python代码)我希望它每0.00001秒检查一次,并记下直到满足条件为止所花费的时间。
我通过导入时间来使用time.sleep()
如果我将该值设置为0.001或更大,则只有它起作用,如果它小于0.001,则时间变慢或变快, 例如。大约需要3秒才能检查1秒...
我该怎么办?除了time.sleep外,您还能提出其他建议吗?
以下代码仅表示
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: ## YOU CAN ignore[it's just so the whole programs keeps repeating]
##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
答案 0 :(得分:0)
所有对时间要求严格的功能都有限制。 我建议考虑一下sleep()的使用,并改用Python文档中所述的Timer对象。 https://docs.python.org/2.4/lib/timer-objects.html
答案 1 :(得分:0)
这里至少要注意两件事:
time.sleep()
和基础系统调用的参数是一个下限。无法保证系统不会进入更长时间的睡眠。 (实际上,我们可以预测,如果系统负载很轻,通常只有少量)。>>> import timeit
>>> timeit.timeit('pass', number=10000)
8.20159912109e-05