libusb 似乎在类编码结构下失败,但没有类编码结构却可以工作?
我有一个使用 Python/NXT/libusb 的简单工作示例,如下所示(注意:使用具有 USB 接口的乐高 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)
I have a simple working example using Python/NXT/libusb as follows (note: using Lego's NXT which has a USB interface):
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)
Above works fine.
As a training exercise, I try to "objectize" the python code, so that I could start putting libraries around the code, but now the LibUSB library complains that it cant find the usb device. Huh? What am I doing wrong. Here is my attempt of the code using a class structure:
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()
When I execute the above, I get the following error (even though if I re-execute the first sample, again it executes fine):
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)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
当您创建 BasicRobotTestCase 时,如果您不将 b 变量存储为实例的成员(即
self.b
),您就会丢失 b 变量编辑: 好吧,丢失的
self
不是问题的根源,也许鼻子实例化你的类的方式改变了USB抓取的内容,尝试直接实例化类。When you create your BasicRobotTestCase, you lose the b variable if you don't store it as a member of your instance (ie
self.b
)EDIT: well, the missing
self
weren't the origin of the problem, maybe the way nose instanciate your class change something to the USB grabbing, try to instanciate the class directlry.