所以我在Windows上运行Python 3.4脚本,当我运行一个特定的脚本时,我有一个无限循环(而我正在使用的是True :)但是当我尝试使用Ctrl-C退出脚本时,它没有退出脚本。它会打印键盘中断,就像它已经退出一样,但只是离开闪烁的光标而不会让我输入,所以我必须退出红色的X.
import serial
import time
import pyfirmata
#from pyfirmata import Arduino, util, PWM
board = pyfirmata.Arduino('COM4', baudrate = 9600, timeout = 5)
time.sleep(2) #sleep in units of sec
it = pyfirmata.util.Iterator(board)
it.start()
digital1 = board.get_pin('d:5:p')
digital2 = board.get_pin('d:6:p')
digital3 = board.get_pin('d:10:p')
digital4 = board.get_pin('d:11:p')
digital = (digital1, digital2, digital3, digital4)
distObject = 1.5 #start warning at 4 inches away from objects (arbitrary)
forceGraspL = 0
forceGraspR = 0
maxForceL = 60
maxForceR = 60
motorMaxForceL = maxForceL / 2
motorMaxForceR = maxForceR / 2
while True:
left = 0
right = 0
leftMid = 0
rightMid = 0
distPerc = 0
MOTORS = (left, right, leftMid, rightMid)
if (distObject != 0 and distObject < 4 and forceGraspL == 0 and forceGraspR == 0):
left = 0.9
distPerc = round(distObject / 4.0 * 100)
elif (forceGraspL != 0 or forceGraspR !=0):
if (forceGraspL < motorMaxForceL and forceGraspR < motorMaxForceR):
left = forceGraspL / motorMaxForceL
right = forceGraspR / motorMaxForceR
elif (forceGraspL < maxForceL and forceGraspR < motorMaxForceR):
left = 1
leftMid = (forceGraspL - motorMaxForceL)/ motorMaxForceL
right = forceGraspR / motorMaxForceR
elif (forceGraspL < motorMaxForceL and forceGraspR < maxForceR):
left = forceGraspL / motorMaxForceL
right = 1
rightMid = (forceGraspR - motorMaxForceR)/ motorMaxForceR
elif (forceGraspL < maxForceL and forceGraspR < maxForceR):
left = 1
leftMid = (forceGraspL - motorMaxForceL)/ motorMaxForceL
right = 1
rightMid = (forceGraspR - motorMaxForceR)/ motorMaxForceR
else:
left = 1
leftMid = 1
rightMid = 1
right = 1
if (distPerc < 100 and distPerc > 0):
for pin in range(1, length(digital)):
digital[pin].write(MOTORS(pin))
time.sleep(.5)
for pin in range(1, length(digital)):
digital[pin].write(0)
time.sleep(.5)
else:
for pin in range(1, length(digital)):
digital[pin].write(MOTORS(pin))
非常感谢有关我的脚本中可能导致此问题的任何建议。我可以告诉问题是在这个脚本中,因为我尝试了其他带有无限循环的脚本,而Ctrl-C适用于那些。
答案 0 :(得分:0)
我在源网页上发布的问题中找到了答案:
https://github.com/tino/pyFirmata
这是一个错误,位于原始代码中,来自此代码序列(我最初遗漏了上述代码,道歉!):
it = pyfirmata.util.Iterator(board)
it.start()
但是,您无法摆脱此代码,否则串行缓冲区将过载。