2011-09-12 31 views
2

我有使用Python/NXT/libusb的如下(注意:在使用樂高的NXT其中有一個USB接口):一個簡單的工作示例libusb似乎在類編碼結構下失敗,但沒有工作?

import nxt.locator 
from nxt.motor import * 

def flip_cube(b): 
    m_arm = Motor(b, PORT_B) 
    m_arm.turn(75, 85) 
    m_arm.turn(-50, 85) 

b = nxt.locator.find_one_brick() 
flip_cube(b) 

以上工作正常。

作爲一個培訓練習,我嘗試「objectize」python代碼,以便我可以開始在代碼周圍放置庫,但是現在LibUSB庫抱怨它無法找到usb設備。咦?我究竟做錯了什麼。這裏是我使用類結構的代碼的嘗試:

import nxt.locator 
from nxt.motor import * 

class BasicRobotTestCase(): 
    __test__ = True 

    def __init__(self): 
     b = nxt.locator.find_one_brick() 

    def flip_cube(self): 
     m_arm = Motor(b, PORT_B) 
     m_arm.turn(75, 85) 
     m_arm.turn(-50, 85) 

    def test_flip_cube(self): 
     flip_cube() 

當我執行上面的,我得到以下錯誤(儘管如果我重新執行的第一個樣本,它再次執行罰款):

E 
====================================================================== 
ERROR: Failure: USBError (No such device (it may have been disconnected)) 
---------------------------------------------------------------------- 
Traceback (most recent call last): 
    File "/Library/Python/2.6/site-packages/nose-0.11.4-py2.6.egg/nose/loader.py", line 485, in makeTest 
    return self._makeTest(obj, parent) 
    File "/Library/Python/2.6/site-packages/nose-0.11.4-py2.6.egg/nose/loader.py", line 539, in _makeTest 
    return MethodTestCase(obj) 
    File "/Library/Python/2.6/site-packages/nose-0.11.4-py2.6.egg/nose/case.py", line 331, in __init__ 
    self.inst = self.cls() 
    File "/Users/gnunez/git-projects/pdca_automation/rubics/tests/basic_robot_test_case.py", line 8, in __init__ 
    b = nxt.locator.find_one_brick() 
    File "/Users/gnunez/git-projects/pdca_automation/nxt/locator.py", line 112, in find_one_brick 
    for s in find_bricks(host, name, silent, method): 
    File "/Users/gnunez/git-projects/pdca_automation/nxt/locator.py", line 43, in find_bricks 
    for s in socks: 
    File "/Users/gnunez/git-projects/pdca_automation/nxt/usbsock.py", line 83, in find_bricks 
    for bus in usb.busses(): 
    File "build/bdist.macosx-10.6-universal/egg/usb/legacy.py", line 330, in busses 
    return (Bus(),) 
    File "build/bdist.macosx-10.6-universal/egg/usb/legacy.py", line 326, in __init__ 
    self.devices = [Device(d) for d in core.find(find_all=True)] 
    File "build/bdist.macosx-10.6-universal/egg/usb/legacy.py", line 311, in __init__ 
    self.configurations = [Configuration(c) for c in dev] 
    File "build/bdist.macosx-10.6-universal/egg/usb/core.py", line 706, in __iter__ 
    yield Configuration(self, i) 
    File "build/bdist.macosx-10.6-universal/egg/usb/core.py", line 407, in __init__ 
    configuration 
    File "build/bdist.macosx-10.6-universal/egg/usb/_debug.py", line 52, in do_trace 
    return f(*args, **named_args) 
    File "build/bdist.macosx-10.6-universal/egg/usb/backend/libusb10.py", line 423, in get_configuration_descriptor 
    config, byref(cfg))) 
    File "build/bdist.macosx-10.6-universal/egg/usb/backend/libusb10.py", line 357, in _check 
    raise USBError(_str_error[retval.value]) 
USBError: No such device (it may have been disconnected) 
+0

原始代碼是否仍然有效? (例如,這*實際上*代碼引起的問題,或者只是一個巧合?) – Amber

+0

是的,原始仍然有效 - 每一次!我運行原始代碼,找到並運行USB。我運行新的「類」結構化代碼,它找不到USB。我回去並運行原始代碼,並再次找到USB並正常工作。 –

回答

1

當您創建BasicRobotTestCase,你失去b變量,如果你不把它保存爲您的實例(即self.b

編輯的成員:好,丟失的self是不是t的起源他的問題,也許是因爲你的班級改變了一些東西到USB抓取,試圖instanciate類directlry。

import nxt.locator 
from nxt.motor import * 

class BasicRobotTestCase(): 
    __test__ = True 

    def __init__(self): 
     self.b = nxt.locator.find_one_brick() # Store the brick in self.b 

    def flip_cube(self): 
     m_arm = Motor(self.b, PORT_B) # use the stored brick in self.b to create the motor 
     m_arm.turn(75, 85) 
     m_arm.turn(-50, 85) 

    def test_flip_cube(self): 
     self.flip_cube() 

if __name__=="__main__": 
    BasicRobotTestCase().test_flip_cube() 
+0

老鼠!我討厭它,當一個簡單的錯字分心從原來的問題 - 對此感到遺憾。當我最小化要發佈的代碼時,缺少的自我是一個簡單的錯字。問題不在於存儲變量/對象 - 問題是nxt.locator.find_one_brick()無法找到USB設備 –

+0

[再次嘗試]老鼠!我討厭它,當一個簡單的錯字分心從原來的問題 - 對此感到遺憾。當我最小化要發佈的代碼時,缺少的自我是一個簡單的錯字。
問題不在於存儲變量/對象 - 問題在於nxt.locator.find_one_brick()無法找到USB設備,並且沒有任何可存儲的內容,在類結構中失敗,但在不在班級結構。類結構代碼方法會導致問題似乎很奇怪?
就像健康檢查一樣,我嘗試了沒有運氣的「自我」改變。 –

+0

@George Nunez:哦。如果你從鼻子以外使用你的課程? 'if __name__ =='__main__「:BasicRobotTestCase()。test_flip_cube()'? –