所以我在 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 适用于这些脚本。