SOLARMA / nxt-control-mmf

Control your LEGO Mindstorm NXT for the Torino Mini Maker Faire
MIT License
1 stars 2 forks source link

Command nxt_test fails when run as non-root user #5

Closed gmacario closed 6 years ago

gmacario commented 6 years ago

I plan to run the tests on rpi3gm26 connected to my Mindstorms NXT via USB.

gmacario commented 6 years ago

See https://github.com/SOLARMA/nxt-control-mmf/blob/master/docs/test-rpi3gm26.md

Running nxt_test as user fails

(.nxt-venv) pi@rpi3gm26:~/github/SOLARMA/nxt-control-mmf $ nxt_test --verbose
debug = True
Find brick...
Warning: Config file (should be at /home/pi/.nxt-python) was not read. Use nxt.locator.make_config() to create a config file.
Host: None Name: None Strict: True
USB: True BT: True
Traceback (most recent call last):
  File "/home/pi/.nxt-venv/lib/python3.4/site-packages/nxt/locator.py", line 121, in find_one_brick
    b = s.connect()
  File "/home/pi/.nxt-venv/lib/python3.4/site-packages/nxt/usbsock.py", line 45, in connect
    self.handle.setConfiguration(1)
  File "/home/pi/.nxt-venv/lib/python3.4/site-packages/usb/legacy.py", line 260, in setConfiguration
    self.dev.set_configuration(configuration)
  File "/home/pi/.nxt-venv/lib/python3.4/site-packages/usb/core.py", line 869, in set_configuration
    self._ctx.managed_set_configuration(self, configuration)
  File "/home/pi/.nxt-venv/lib/python3.4/site-packages/usb/core.py", line 102, in wrapper
    return f(self, *args, **kwargs)
  File "/home/pi/.nxt-venv/lib/python3.4/site-packages/usb/core.py", line 147, in managed_set_configuration
    self.managed_open()
  File "/home/pi/.nxt-venv/lib/python3.4/site-packages/usb/core.py", line 102, in wrapper
    return f(self, *args, **kwargs)
  File "/home/pi/.nxt-venv/lib/python3.4/site-packages/usb/core.py", line 120, in managed_open
    self.handle = self.backend.open_device(self.dev)
  File "/home/pi/.nxt-venv/lib/python3.4/site-packages/usb/backend/libusb1.py", line 786, in open_device
    return _DeviceHandle(dev)
  File "/home/pi/.nxt-venv/lib/python3.4/site-packages/usb/backend/libusb1.py", line 643, in __init__
    _check(_lib.libusb_open(self.devid, byref(self.handle)))
  File "/home/pi/.nxt-venv/lib/python3.4/site-packages/usb/backend/libusb1.py", line 595, in _check
    raise USBError(_strerror(ret), ret, _libusb_errno[ret])
usb.core.USBError: [Errno 13] Access denied (insufficient permissions)
Failed to connect to possible brick
Traceback (most recent call last):
  File "/home/pi/.nxt-venv/lib/python3.4/site-packages/nxt/locator.py", line 121, in find_one_brick
    b = s.connect()
  File "/home/pi/.nxt-venv/lib/python3.4/site-packages/nxt/bluesock.py", line 39, in connect
    sock = bluetooth.BluetoothSocket(bluetooth.RFCOMM)
  File "/home/pi/.nxt-venv/lib/python3.4/site-packages/bluetooth/bluez.py", line 155, in __init__
    _sock = _bt.btsocket (proto)
_bluetooth.error: (93, 'Protocol not supported')
Failed to connect to possible brick
No brick was found.
    Is the brick turned on?
    For more diagnosing use the debug=True argument or
    try the 'nxt_test' script located in /bin or ~/.local/bin
Error while running test:
  File "/home/pi/.nxt-venv/bin/nxt_test", line 27, in <module>
    b = nxt.locator.find_one_brick(debug=debug)
  File "/home/pi/.nxt-venv/lib/python3.4/site-packages/nxt/locator.py", line 162, in find_one_brick
    raise BrickNotFoundError

(.nxt-venv) pi@rpi3gm26:~/github/SOLARMA/nxt-control-mmf $

Running nxt_test as root works fine

(.nxt-venv) pi@rpi3gm26:~/github/SOLARMA/nxt-control-mmf $ sudo $HOME/.nxt-venv/bin/nxt_test --verbose
debug = True
Find brick...
Warning: Config file (should be at /root/.nxt-python) was not read. Use nxt.locator.make_config() to create a config file.
Host: None Name: None Strict: True
USB: True BT: True
info: ('NXT', '00:16:53:05:15:50', 0, 51020)
Warning; the brick found does not match the name provided.
  host:None
  info[0]:
  name:None
NXT brick name: NXT
Host address: 00:16:53:05:15:50
Bluetooth signal strength: 0
Free user flash: 51020
Protocol version 1.124
Firmware version 1.31
Battery level 8322 mV
Play test sound...done
(.nxt-venv) pi@rpi3gm26:~/github/SOLARMA/nxt-control-mmf $
gmacario commented 6 years ago

Next step: Read carefully the documentation and source available in https://github.com/Eelviny/nxt-python

gmacario commented 6 years ago

Linking stack trace to source code:

  1. https://github.com/Eelviny/nxt-python/blob/master/nxt/locator.py#L121
  2. https://github.com/Eelviny/nxt-python/blob/master/nxt/usbsock.py#L45
  3. https://github.com/pyusb/pyusb/blob/v1.0.2/usb/legacy.py#L260
  4. https://github.com/pyusb/pyusb/blob/v1.0.2/usb/core.py#L869
  5. https://github.com/pyusb/pyusb/blob/v1.0.2/usb/core.py#L102
  6. https://github.com/pyusb/pyusb/blob/v1.0.2/usb/core.py#L147
  7. https://github.com/pyusb/pyusb/blob/v1.0.2/usb/core.py#L102
  8. https://github.com/pyusb/pyusb/blob/v1.0.2/usb/core.py#L120
  9. https://github.com/pyusb/pyusb/blob/v1.0.2/usb/backend/libusb1.py#L786
  10. https://github.com/pyusb/pyusb/blob/v1.0.2/usb/backend/libusb1.py#L643
  11. https://github.com/pyusb/pyusb/blob/v1.0.2/usb/backend/libusb1.py#L595
gmacario commented 6 years ago

Creating sample ~/.nxt-python config file:

(.nxt-venv) pi@rpi3gm26:~/github/SOLARMA/nxt-control-mmf $ python
Python 3.4.2 (default, Oct 19 2014, 13:31:11) 
[GCC 4.9.1] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import nxt.locator
>>> nxt.locator.make_config()
Welcome to the nxt-python config file generator!
This function creates an example file which find_one_brick uses to find a brick.
The file has been written at /home/pi/.nxt-python
The file contains less-than-sane default values to get you started.
You must now edit the file with a text editor and change the values to match what you would pass to find_one_brick
The fields for name, host, and strict correspond to the similar args accepted by find_one_brick
The method field contains the string which would be passed to Method()
Any field whose corresponding option does not need to be passed to find_one_brick should be commented out (using a # at the start of the line) or simply removed.
If you have questions, check the wiki and then ask on the mailing list.
>>> quit()
(.nxt-venv) pi@rpi3gm26:~/github/SOLARMA/nxt-control-mmf $ cat $HOME/.nxt-python 
[Brick]
name = MyNXT
host = 54:32:59:92:F9:39
strict = 0
method = usb=True, bluetooth=False

(.nxt-venv) pi@rpi3gm26:~/github/SOLARMA/nxt-control-mmf $
gmacario commented 6 years ago

Running nxt_test as non-root user after creating the sample ~/.nxt-python:

(.nxt-venv) pi@rpi3gm26:~/github/SOLARMA/nxt-control-mmf $ nxt_test --debug
debug = True
Find brick...
Host: 54:32:59:92:F9:39 Name: MyNXT Strict: True
USB: True BT: False
Traceback (most recent call last):
  File "/home/pi/.nxt-venv/src/nxt-python/nxt/locator.py", line 121, in find_one_brick
    b = s.connect()
  File "/home/pi/.nxt-venv/src/nxt-python/nxt/usbsock.py", line 45, in connect
    self.handle.setConfiguration(1)
  File "/home/pi/.nxt-venv/lib/python3.4/site-packages/usb/legacy.py", line 260, in setConfiguration
    self.dev.set_configuration(configuration)
  File "/home/pi/.nxt-venv/lib/python3.4/site-packages/usb/core.py", line 869, in set_configuration
    self._ctx.managed_set_configuration(self, configuration)
  File "/home/pi/.nxt-venv/lib/python3.4/site-packages/usb/core.py", line 102, in wrapper
    return f(self, *args, **kwargs)
  File "/home/pi/.nxt-venv/lib/python3.4/site-packages/usb/core.py", line 147, in managed_set_configuration
    self.managed_open()
  File "/home/pi/.nxt-venv/lib/python3.4/site-packages/usb/core.py", line 102, in wrapper
    return f(self, *args, **kwargs)
  File "/home/pi/.nxt-venv/lib/python3.4/site-packages/usb/core.py", line 120, in managed_open
    self.handle = self.backend.open_device(self.dev)
  File "/home/pi/.nxt-venv/lib/python3.4/site-packages/usb/backend/libusb1.py", line 786, in open_device
    return _DeviceHandle(dev)
  File "/home/pi/.nxt-venv/lib/python3.4/site-packages/usb/backend/libusb1.py", line 643, in __init__
    _check(_lib.libusb_open(self.devid, byref(self.handle)))
  File "/home/pi/.nxt-venv/lib/python3.4/site-packages/usb/backend/libusb1.py", line 595, in _check
    raise USBError(_strerror(ret), ret, _libusb_errno[ret])
usb.core.USBError: [Errno 13] Access denied (insufficient permissions)
Failed to connect to possible brick
No brick was found.
    Is the brick turned on?
    For more diagnosing use the debug=True argument or
    try the 'nxt_test' script located in /bin or ~/.local/bin
Error while running test:
  File "/home/pi/.nxt-venv/src/nxt-python/scripts/nxt_test", line 27, in <module>
    b = nxt.locator.find_one_brick(debug=debug)
  File "/home/pi/.nxt-venv/src/nxt-python/nxt/locator.py", line 162, in find_one_brick
    raise BrickNotFoundError

(.nxt-venv) pi@rpi3gm26:~/github/SOLARMA/nxt-control-mmf $
gmacario commented 6 years ago

SOLUTION: Write a udev rule for the NXT USB Device - change permissions of /dev/bus/usb/xxx/yyy

cat << END | sudo tee /etc/udev/rules.d/10-mindstorms.rules
SUBSYSTEMS=="usb", ATTRS{idVendor}=="0694", ATTRS{idProduct}=="0002", GROUP="users"
# SUBSYSTEMS=="usb", ATTRS{serial}=="001653051550", GROUP="users"
END

References:

gmacario commented 6 years ago

Result after creating file /etc/udev/rules.d/10-mindstorms.rules:

(.nxt-venv) pi@rpi3gm26:~ $ nxt_test --debug
debug = True
Find brick...
Host: None Name: None Strict: True
USB: True BT: False
info: ('NXT', '00:16:53:05:15:50', 0, 51020)
Warning; the brick found does not match the name provided.
  host:None
  info[0]:
  name:None
NXT brick name: NXT
Host address: 00:16:53:05:15:50
Bluetooth signal strength: 0
Free user flash: 51020
Protocol version 1.124
Firmware version 1.31
Battery level 7837 mV
Play test sound...done
(.nxt-venv) pi@rpi3gm26:~ $

Closing issue