2015-08-19 92 views
1

所以我在Windows上運行Python 3.4腳本,當我運行一個特定的腳本時,我有一個無限循環(而真正的:)我正在使用,但是當我嘗試用Ctrl-C退出腳本,它不會退出腳本。它打印鍵盤中斷,彷彿它已經退出,但後來只留下閃爍的光標,不會讓我的類型,所以我與紅色的X可以退出Windows PowerShell不退出腳本與Ctrl-C

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適用於這些腳本。

+2

你如何在PowerShell中運行Python腳本? – briantist

+0

我將目錄更改爲腳本所在的位置以及我可以訪問python的位置,然後使用命令> python filename.py運行腳本。我對python相當陌生,並且僅遵循如何學習Python Hard Work網站所述的協議。運行Python腳本的標準方式是什麼? – rtmurad

+0

我已經從問題中刪除了powershell標記。如果這個問題只發生在你從powershell提示符(我認爲不太可能)啓動python的時候,那麼你可以添加它,但是請編輯你的問題來澄清是否是這種情況。 – briantist

回答

0

我發現貼在源網頁的問題的答案:

https://github.com/tino/pyFirmata

這是位於原代碼,從該代碼序列(我離開了上面的代碼中的錯誤原來,道歉!):

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

但是,你不能擺脫這個代碼,否則串行緩衝區將過載。