2

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)
  • Does the original code still work? (e.g. is this *actually* the code causing the problem, or just a coincidence?) – Amber Sep 12 '11 at 05:21
  • Yes, the original still works - every time! I run the original code, the USB is found and functions. I run the new "class" structured code, it cannot find the USB. I go back and run the original code, and again it find the USB and works fine. – George Nunez Sep 12 '11 at 13:34

1 Answers1

1

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.

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()
Cédric Julien
  • 78,516
  • 15
  • 127
  • 132
  • Rats! I hate it when a simple typo distracts from the original problem - sorry about that. That missing self was a simple typo when I minimized the code to post. The problem is not the storing of the variable/object - the problem is that nxt.locator.find_one_brick() fails to find the USB device – George Nunez Sep 12 '11 at 13:39
  • [trying again] Rats! I hate it when a simple typo distracts from the original problem - sorry about that. That missing self was a simple typo when I minimized the code to post.
    The problem is not the storing of the variable/object - the problem is that nxt.locator.find_one_brick() fails to find the USB device and has nothing to store, failing when in a class structure, but working fine when NOT in a class structure. It seems odd that a class structure code approach would cause a problem?
    Just as sanity check, I tried the "self" changes provided with no luck.
    – George Nunez Sep 12 '11 at 13:51
  • @George Nunez : oh. And if you use your class outside from nose ? with a `if __name__ == "__main__": BasicRobotTestCase().test_flip_cube()` ? – Cédric Julien Sep 12 '11 at 13:57
  • Wow, it does appear to be nosetests. I tried as you suggested, but it still did not find the USB. I thought you were on the right track, though, in regards to nosetests (nice observation by the way), so I used the same class structure, but used straight python (without nosetests), and that worked fine. So something that nosetests is doing is causing the libUSB to fail - I suspect that nosetests is finding some other libUSB that is not configured correctly or something. THANK YOU! – George Nunez Sep 12 '11 at 14:13