Controlling the NXT using Python

This page is work in progress.
The first version should be complete in April 2008.

See the Lego Project page for more information.


Python is my favourite programming language, so I could not resits writing my first controll scripts for the NXT with it.

To get started I installed PyBluez, PyUSB and NXT_Python.

PyBluez and PyUSB came with my system so it was an easy:

sudo aptitude install python-pyusb python-bluetooth

NXT_Python, I just downloaded, unpacked it and ran the setup script:

sudo python setup.py install

Using a USB connection

That is probably the simplest way but the least flexible one due to the USB wire. NXT_Python comes with a few example scrips, so I just connected my NXT to my machine with the USB cable that came in the box and ran some scripts. They all use the first NXT they find attached to the USB.

Using a Bluetooth connection

NXT_Python has a locator that will search all USB devices and then look for any NXT over Bluetooth. This is pretty handy but also not very fast. Scanning for an active NXT using Bluetooth took more than a few seconds, which started to get annoying. I also had problems with connecting to the found NXT and the script would exit with an exception:

Traceback (most recent call last):
  File "examples/spin.py", line 22, in <module>
    spin_around(sock.connect())
  File "/usr/lib/python2.5/site-packages/nxt/bluesock.py", line 33, in connect
    sock.connect((self.host, BlueSock.PORT))
  File "<string>", line 5, in connect
bluetooth.BluetoothError: (114, 'Operation already in progress')

To get around the issue I replaced the locator code and created a socket using the Bluetooth MAC address of the NXT:

# The locator code below did not work and also takes 
# ages using a bluetooth connected NXT
# sock = nxt.locator.find_one_brick()
# So, lets just use the NXT's MAC address directly
sock = nxt.bluesock.BlueSock("00:16:53:05:7A:1E")

Just replace the MAC address with your NXT's one.

Wii + NXT == Fun

I had a Wiimote laying around and after a quick search I found a python script that would give me all the info I needed to use the it to controll my NXT bot.

I have uploaded the code into my public SVN repository.
You can browse the controller code or check it out using:

svn co http://jan.kollhof.net/svn/Lego/python/wiimote

Open the controller.py file and replace the MAC addresses with your own.

To find your NXT's and Wiimote's MAC, start your NXT and make sure Bluetooth is activated and in discovery mode, press button 1+2 on your Wiimote and run:

hcitool scan

It should give find your NXT and Wiimote and give you something like:

Scanning ...
        00:16:53:05:7A:1E       NXT
        00:19:1D:BB:7D:28       Nintendo RVL-CNT-01

Once you have updated the controller.py run it and wait until it has connected to the NXT and Wiimote and prints ready.

Hold your Wiimote like a steering wheel:

  • turning it forward will make the bot go forward
  • turning it back will make it go slower, stop and then go backwards
  • tilting it left and right will make the bot turn
  • press 1 on the Wiimote to open the claw and 2 to close it
  • press A on the Wiimote to quit and close the connections

Note, the controller script is still very very buggy!!!