Python Powered Android Robots on NXT

There's a wide range of options for hacking NXT robots. One in particular, nxt-python, is the most popular with Python fans.

nxt-python is good, but it runs on a regular machine and controls the NXT over Bluetooth, which means the robot is never more than ten meters from its life-support PC.

There's another project, Cellbots, which allows you to control an NXT robot from an Android device, but not program it in Python.

What I really wanted was to mount my phone to the NXT robot and program the whole contraption, in Python, with the program running on the Android device itself. This way, the phone would run the program, using the Android API to control the phone, and be able to simultaneously control the NXT parts using the phone's Bluetooth hardware.

It worked! My phone is now a robot. It 
has tracks to roam around on, a colour/light sensor to scan the ground with, touch sensors, an ultrasonic range finder, sensors for tilt and acceleration, geo-location and compass, as well as web access, telephony and two cameras; it's a pretty cool little robot.

If anyone's interested in doing something similar, it's twenty minutes work.

Set Up

The following looks a bit gnarly, but is actually simple if you follow along. It's all GUI stuff.

First, grab SL4A, Scripting Layer 4 Android, from their site.


Once that's installed, grab the Python interpretor from within SL4A. This will give you another 'app' called Python For Android, PY4A, on your phone. You normally run Python scripts through SL4A, but you will need to open PY4A separately for certain things.

Now download nxt-python onto your phone from their Google Code page, making sure to grab the zipped version.


Once you have the zip file on your Android, extract it into any directory, the downloads directory the zip file is already in is as good a place as any ~ you'll be able to remove all this cruft soon anyway.

Now you have the zip extracted, you need to move the nxt directory that is within the main nxt directory to your Python extensions directory. That is to say, if you open the nxt directory that you just extracted, within it is another directory called nxt. Its path should look something like...


You need to move this directory, putting it inside...


That's nxt-python sorted. You also need the PyBluez module, compiled for ARM, for nxt-python's Bluetooth code to work. Fortunately, you can just grab a copy from the PY4A website.


Once you have the PyBluez module on your phone, open the Python for Android app from your apps list and hit Import Modules. Select the PyBluez module and it'll be installed for you.

All done. Open SL4A, import nxt, and start hacking.

If you have any trouble, mail me, I'll be happy to help out.

1 comment:

  1. Hi Carl,

    This is exactly what I was looking for. Thanks. I'll give it a go.