1

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

4

1 回答 1

0

我在源网页上发布的问题中找到了答案:

https://github.com/tino/pyFirmata

这是原始代码中的一个错误,来自这个代码序列(我最初在上面的代码中遗漏了,抱歉!):

it = pyfirmata.util.Iterator(board)
it.start()

但是,您不能摆脱此代码,否则串行缓冲区将过载。

于 2015-08-20T16:49:27.387 回答