Windows Powershell not quitting script with Ctrl-C

2.4k Views Asked by At

So I am running Python 3.4 scripts on Windows, and when I run a particular script, I have an infinite loop (while True:) which I am using, but when I try to quit the script with Ctrl-C, it is not exiting the script. It prints Keyboard Interrupt, as if it has quit, but then just leaves the blinking cursor and will not let me type, so I have to exit out with the red 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))

Any suggestions on what in my script might be causing this problem is greatly appreciated. I can tell that the problem is in this script because I have tried other scripts with infinite loops, and the Ctrl-C works for those.

1

There are 1 best solutions below

0
On BEST ANSWER

I found the answer in the problems posted on the source webpage:

https://github.com/tino/pyFirmata

It is a bug that lies in the original code, from this code sequence (which I left out of the above code originally, apologies!):

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

However, you can not get rid of this code otherwise the serial buffer will overload.