我正在做一个项目,其中输入是从车轮编码器以浮点值的形式获取的。如何每60秒获取一次输入并将输入提供给神经网络模型。 在代码km_per_hour中,必须每60秒记录一次并输入到神经网络。
if __name__ == '__main__':
init_GPIO()
init_interrupt()
while True:
calculate_speed(40)
print('kmh:{1:.0f}'.format(km_per_hour))
sleep(0.1)
答案 0 :(得分:0)
捕获每个回合开始时的时间,并检查是否已过60秒。
if __name__ == '__main__':
import time
init_GPIO()
init_interrupt()
while True:
start_time = time.time() # seconds since unix time
end_time = start_time + 60
list_speeds = list()
while time.time() < end_time:
km_per_hour = calculate_speed(40)
list_speeds.append(km_per_hour)
print('kmh:{1:.0f}'.format(km_per_hour))
sleep(0.1)
else: # while-else
feed_your_dino(list_speeds)
如果您不想锁定输入,可以执行以下操作,以在新线程中运行该函数,而不是锁定程序:
if __name__ == '__main__':
import time, threading
init_GPIO()
init_interrupt()
while True:
start_time = time.time() # seconds since unix time
end_time = start_time + 60
list_speeds = list()
while time.time() < end_time:
km_per_hour = calculate_speed(40)
list_speeds.append(km_per_hour)
print('kmh:{1:.0f}'.format(km_per_hour))
sleep(0.1)
else: # while-else
th_my_ai = threading.Thread(target=feed_your_dino, args=(list_speeds,))
th_my_ai.start()