2

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.

rtmurad
  • 127
  • 2
  • 14
  • 2
    How are you running python scripts in PowerShell? – briantist Aug 19 '15 at 16:18
  • I change the directory to where the scripts are located and where I can access python, and then am running the scripts using the command >python filename.py. I am fairly new to python, and am just following protocol described by How To Learn Python the Hard Way website. What is the standard way to run Python scripts? – rtmurad Aug 19 '15 at 16:44
  • I've removed the powershell tag from the question. If this problem only happens when you launch python from a powershell prompt (which I think is unlikely), then you could add it back but please edit your question to clarify if that's the case. – briantist Aug 19 '15 at 16:47
  • I'm sorry, I'm not sure I understand your statement. Powershell is the only thing I use to run Python. And as stated in the question, this script I have posted is the only one I am having trouble with, but since my problem has to do with Powershell, I am unsure why you would want the label gone? – rtmurad Aug 19 '15 at 16:52
  • Your script is a python script. You are launching it from powershell, but just by invoking the python executable, in much the same way you might invoke it from the windows command prompt, or from windows explorer. It's not related to powershell at all (or if it is, then it's unclear how). If you're saying that this same script works correctly *unless* it's launched from powershell, then I can totally understand why you would tag it as such, but that doesn't seem to be the case. If you think I'm off base, go ahead and put the tag back on; I won't push the issue. – briantist Aug 19 '15 at 16:56
  • I think what @briantist is suggesting is to try running your script from the Windows command prompt (`cmd`) instead of from PowerShell, and see if you get the same unexpected behavior. On occasion, I've noted strange behavior when running certain tools from PowerShell (Python included) that I could not explain and was too lazy to investigate further. – Rusty Shackleford Aug 19 '15 at 17:07
  • Ah yes, thank you both, sorry I just did not understand the statements. You are both correct, using cmd.exe I get the same odd behavior. – rtmurad Aug 19 '15 at 17:12

1 Answers1

0

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.

rtmurad
  • 127
  • 2
  • 14