How to Control LEGO NXT from your laptop with a Gamepad


Gamepad nxt

The EV3 has fancy ipad remote control. Infrared too. But I happen to have an NXT in perfect working condition. And I had an old gamepad in the attic. So why not try and connect all of that gear? In this tutorial I show you how.

Step 1. Get the Mac (and python) to read gamepad input

I connected the generic USB gamepad to my mac but not much happened. So I tried connecting a PS3 sixaxis controller. This showed a visible connection, in the list of bluetooth devices. Ok, now python. Some googling gave me three options for libraries: jaraco.input, PyGame, Py-SDL2. There were some more but I couldn’t get them to install. Turned out that jaraco.input only worked on linux and windows. PyGame took a very long time to compile, and in the meantime I tried SDL2. Installing SDL2 via homebrew was really fast and the coding examples worked right away. It turns out I could read both the cheap generic USB controller and the nice PS3 sixaxis controller. Sweet!

When you can get it to install with homebrew and pip, it’s as simple as this:

Step 2. Send motor commands to the NXT via bluetooth

For sending motor commands I had already found the jaraco.nxt library. I used that one for sending bluetooth messages in the Sumo project. It saw that it contained motor commands too. As it turned, sending the motor commands wasn’t as easy as I thought. Jaraco comes with pretty meager documentation and examples, so I had to do some digging in the source code. It turned out that you have to explicitly turn motor regulation on, otherwise the motors only seem to run at half power. Turning that on was an undocumented feature.

So here’s the code for sending a simple motor command, turning the motor on with a certain speed.

Step 3. Converting Gamepad input into motor input that works with the robot configuration

As I was driving around an omnibot, the great HTRotabot, I still needed to convert (normalised) joystick input to motor speeds for the three motors. A simple sin() distribution of the power over the motors does the trick. The motors are all in a 120 degree (2/3*PI radians) to each other. Here’s the code:

Like this article? Help us make more!

Your support matters

We appreciate your support on Patreon and YouTube! Small things count.

Become a Patron

Don't miss a thing

Subscribe below to get an email when we publish a new article.

Share this with someone who needs to know

Leave a Reply

Item added to cart.
0 items - 0.00