libusb似乎在类编码结构下失败但没有一个工作?

时间:2011-09-12 05:05:45

标签: python libusb nxt

我有一个使用Python / NXT / libusb的简单工作示例如下(注意:使用带有USB接口的Lego NXT):

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)

以上工作正常。

作为一个训练练习,我尝试“对象化”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)

1 个答案:

答案 0 :(得分:1)

当您创建BasicRobotTestCase时,如果您不将其存储为实例的成员(即self.b),则会丢失b变量

编辑:好吧,失踪的self不是问题的根源,也许就是鼻子将你的班级改变为USB抢夺的方式,尝试将班级改为直接。

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()