Use a PS3 Controller to Control an Arduino NXT Bot

95K13936

Intro: Use a PS3 Controller to Control an Arduino NXT Bot

UPDATE:
  I've fixed a couple of running bugs with the data packet, and I've modified the NXTI2CDevice library to be compatible with Arduino 1.0.
The attached zip file has
1. a new Arduino sketch
2. a new Processing sketch
3. the modified NXTI2CDevice library

You'll still need the Procontroll library in Processing, though.


Greetings, everyone, and welcome to my first instructable!
Today we'll be covering a few major bases to get to the final goal: controlling an Arduino-based robot via a PS3 remote! By the end of this instructable, you'll be able to control the speeds of both NXT motors with the two analog sticks of a PS3 controller.

Here's the idea:  
A PS3 communicates via bluetooth to the computer sending the value of its analog sticks to the Processing program.  Processing then sends this information as a packet of data to the Arduino bot through the Xbee wireless modules.  Lastly, the Arduino board "unpacks" this data packet and interprets it as the speed and direction to drive each motor.

Here are the  three basic milestones that we'll cover:

-- using Arduino to control NXT Motors
-- interfacing the Programming Language Processing with a PS3 controller
-- Wireless communication between your computer and Arduino via Xbee modules

If you aren't quite comfortable with either Processing or Arduino's Programming languages--fear not!  I've accompanied the project with my source code .

Here's what I'll assume:
-- you're mildly familiar with Arduino, and you can a user-created library to the IDE.
-- your computer has bluetooth-pairing capabilities
-- you can strip a few small wires and you have basic soldering skills.

Here's a few concepts that you'll learn about on the side:
(You don't need to know about these beforehand to get started on this project)
-- Xbee wireless communication
-- i2c communication (between the NXT motors and Arduino)

All right, Let's get started!

Lastly, feel free to check out the video for the final results of this project in action!

http://www.youtube.com/watch?v=-NCEqi-qMsc

STEP 1: Materials

For this instructable, you'll need quite a few things, but we're covering some major milestones in the process, and, heck, all of these parts can be reused for later projects!

I got the hard-to-find (nonLego) pieces from both Sparkfun.com and Mindsensors.com


Robot Materials:
-- two Mindstorms NXT Motors and two cables
-- Lego Technic Pieces to build your robot Chassis
-- Standard Arduino (I used the UNO.  This actually allows me to skip adding pull-up resistors to the analog inputs)
-- 9V batter and 9V battery Clip to power the Arduino.  ( We could power it off the motor battery, but I'll hold off on that to keep it simple.)
-- Battery Pack (or other power supply.  I'm using a 7.2V NiMH pack)
-- I2C Host Cable with NXT Connector
-- Tape (Gaffer's Tape is probably the least messy, but any tape will do the job)

http://www.mindsensors.com/index.php?module=pagemaster&PAGE_user_op=view_page&PAGE_id=136

-- Multiplexer for NXT Motors (this piece gives us the ability to speak to the NXT motors lots of ugly wire snipping)

http://www.mindsensors.com/index.php?module=pagemaster&PAGE_user_op=view_page&PAGE_id=134



Communication Materials
-- 2 XBee 1mW Wire Antennas  (Xbee radios)
http://www.sparkfun.com/products/8665

-- XBee Explorer USB connector
http://www.sparkfun.com/products/8687

-- XBee Wireless Shield
http://www.sparkfun.com/products/9976

Other Hardware:
-- header pins (longer header pins are a bit more user-friendly)
-- PS3 controller
-- mini USB cable for connecting the Xbee to your computer
-- USB cable to program the Arduino

A bundle of OpenSource Software:
-- The Arduino IDE
 http://arduino.cc
-- Processing
      http://processing.org/

-- The NXT Library for speaking to the NXT i2c Motors
https://launchpad.net/nxti2cdevice
(Side note:  This NXT Library is a great project all to itself!  Check out the man Clinton Blackmore behind the accomplishment at   http://robotclub.ab.ca/   )
-- The Processing Language Library called Pro Controll which will allow us to communicate with the PS3 controller. 
http://creativecomputing.cc/p5libs/procontroll/


Final Tools to get the job done:
-- Soldering Iron
-- Solder
-- Exacto knife (or any small-and-precise knife)
-- wire strippers for stripping very thin wire.

STEP 2: Connect the PS3 Controller to Your Computer

First things First!  Our Computer must be able to communicate to the PS3 controller.  
I'm using a Mac Laptop, but there are many guides to do this.  Here's where I found my help:

I first downloaded and installed the driver available here:
http://tattiebogle.net/index.php/ProjectRoot/Ps3Controller

Here's the guide that I followed (Many thanks, RandomTechKid!) :
http://www.youtube.com/watch?v=y5LiI4uGXNk

STEP 3: Connect the PS3 Controller to Processing

We'll be using the ProControll Library within Processing.

First We'll need to download both Processing and the ProControll Library:

http://processing.org

http://creativecomputing.cc/p5libs/procontroll/


Next, we'll need to connect our PS3 Controller to the computer.  On macs, you'll know you're paired when you see the bluetooth symbol change. Check out the image below.

Now that we have both packages, install Processing and unzip the ProControll library.

Processing creates a folder called Processing somewhere in your computer when you install it.  (Mine was in Documents.)  This is where all of your Processing projects are saved.
In our case, we need to add the ProControll Library to our libraries available to us in Processing. 

Create a folder in the Processing Folder called libraries and simply drag the unzipped ProControll library into this folder.

If you have Processing open right now, you'll want to restart it before you can begin using the library that we just added.

Next up: Let's connect that PS3 Controller!

(Ok, this is probably the most intimidating step, but I'll guide you through it.)

The ProControll library has a great set of example code to get us started.  
By dragging and dropping the example code from the printDevices() documentation on ProControll's web page, we can see that the PS3 is recognized by Processing.

http://creativecomputing.cc/p5libs/procontroll/controllio_method_printdevices.htm#
Here's what you'll need to do:
after running the pringDevices() example code, check to make sure that the name of the PS3 controller is the same as in the Test code that I included.  If it is, Great!  Move on to the next step! 

If not, you'll need to replace the name of the controller with the name  that you'll receive after running the printDevices() example code.  You'll place this name in the code that I'll give you in the later steps.  You'll also need to do the same for the names of the two analog sticks.


(You may want to run the Test code that I included that moves around a square in response to the left PS3 analog stick to see if you've got everything down so far.)


STEP 4: Assemble Your Robot Chassis

So far, so good!

In this case, I decided not to include instructions on how to build my LegoNXT chassis.  

All you'll need to do is create a chassis that runs off of two motors.  Each motor drives a wheel on that corresponding side (in my case, a tread).

If you're not too LEGO savvy, no sweat!  Check out the gracious providers of many instructions at www.nxtprograms.com for a guide to building a simple chassis:

Here's a really simple one to get you started:
http://www.nxtprograms.com/mini_rover/steps.html

Once you've built that chassis, we'll be mounting a few Arduino components to it in the next step.

STEP 5: Assemble the Arduino Hardware

Take a look at that i2C cable from Mindsensors.  (you could also sacrifice one from your NXT set, but this one is a bit more hacker-friendly because the wires can be stripped more easily.)

First cut the cable in half!  You only need one half in this case.


With an exacto knife, separate the individual strands of the wire.
(Be Gentle!  separate them just over an inch apart from each other.)

Next, (Gently) strip the insulation off of each wire except the red one.

From your stack of header pins, give yourself 4 separate individual header pins.  Solder the following:

One header pin to the 5V wire
One header pin to the two Ground wires (just twist those two ground wires together)
One header pin to the SCL wire
One header pin to the SDA wire

Insulate all of your solder joints with some tape!

Next: the Arduino assembly

Attach an Xbee radio to the Xbee shield from Sparkfun.
Mount this shield onto the Arduino.

Attach the modded NXT wire to Arduino as follows.
5V goes to the Arduino 5V
Grounds go to the Arduino Gnd
SCL goes to Arduino's analog pin 5
SDA goes to Arduino's analog pin 4
(For the last two connections, you'll have to twist the NXT wire because the wire connections are reversed.)

Next: connect the mindsensors multiplexor to the Arduino.  Insert it into the NXT socket because our Arduino is "mimicking" the NXT by speaking in the i2c protocol that the NXT uses to communicate to the motors.

Lastly:
connect the motor chassis motor cables in the corresponding M1 and M2 connections.  If you find that you're controlling the motor that doesn't correlate to the correct analog stick, simply switch the connections here in the future.

From here on out:  Simply mount everything onto the chassis!  (I used Gaffers Tape here.)

STEP 6: Add the NXT Library to the Arduino IDE

Download and unzip the NXT Library from the following link:

https://launchpad.net/nxti2cdevice/+download

From here, you'll want to install the Arduino IDE first, if you haven't already.

Next, create a folder called libraries in the Arduino Directory.  (This is just like what we did for Processing and the ProControll library, so I wont cover it as "in-depth" with all of the detailed pictures.)
Simply drag'n'drop the NXTI2CDevice folder into this libraries folder in the Arduino IDE.


STEP 7: Connect the Xbees

If you're using two Brand-new Xbees from Sparkfun, by default, they should be speaking on the same frequency. If not, you can visit this site to give you a detailed tutorial as to how to do this:

http://tronixstuff.wordpress.com/2010/08/06/moving-forward-with-arduino-%E2%80%93-chapter-14-xbee-introduction/

here's another tutorial for the same idea:

http://forums.trossenrobotics.com/tutorials/how-to-diy-128/xbee-basics-3259/

Ok, when we have two paired Xbees, we can now mimick a hard wire serial communication from the computer to your Arduino.


STEP 8: Download, Run, and Our Summary

Next, download the Arduino code that I included at the bottom of this page:
Upload that Arduino sketch onto your Arduino bot!

NOTE: when you upload this code, make sure that you switch the little switch on the Sparkfun shield to the DLINE setting.  When you want to run the code, though, switch the little switch back to the UART setting.


Almost there!

Download the Processing Sketch that I also included.

Here we go!
First mount the other Xbee radio onto the explorer and connect it to the computer.
Then Pair the PS3 controller.
Then run the Processing Sketch. 
Then connect the NXT motors to their battery and connect the Arduino to it's 9v battery.
(Make sure you're switch is set to UART)

Enjoy the analog control of your NXT project!



Here's what we've done:

We've interfaced the NXT motors with the Arduino using ONLY Two pins!
We've interfaced a PS3 controller with Processing!
We've succeeded in wireless communication between the computer and Arduino.
We've successfully achieved analog control of two NXT motors via remote control!

Thanks for coming this far! You've done many things to get here!  I'd appreciate any feedback you can give, whether they be questions... comments... (ok, ok, snide remarks too, as long as I can improve myself from here).

Happy Hacking!


37 Comments

What exactly do I need? What is this "XBee radio" that you refer to? Do I need an adaptor for that to an Arduino? Where do I get the XBee radio? How do I connect it to an Arduino?

Can I simply link the arduino to the computer instead of using the XBee?
I keep hearing you say that you want to figure out if you can do this or that...can you please actually do it? Range doesn't matter to me, so Can I or Can't I directly bluetooth the Ps3 controller to an arduino shield?
Thanks, but I really just need some way to take Ps3 controller stick movement and tie that to analog output pins on arduino. Can I cut out all of the wireless, just for a start?
Wait. So XBee can communicate with PS3 controllers?
Can u explain why u need 2 Xbee radios for this, instead of 1?

Thanks,
Sjors
Sure, sjors073. The Controller needs to communicate with the robot wirelessly. First I'm sending the controller's signals to the computer via the computer's built-in bluetooth radio. The computer then has to transmit these signals over to the robot. There are many ways to do this, but I chose Xbees for their simplicity. The computer needs an Xbee attached to send out the data to the robot via radio waves. The robot then uses it's onboard Xbee to receive the radio waves and interpret the data sent wirelessly from the computer. The idea is that the two Xbees are like Walkie-talkies. The robot has one, and the computer has one. since Xbees and the Ps3 controller communicate wirelessly in two totally different ways, I"m having the computer stand in as the middle-man, converting the information from one signal into information for the other.
Alright i get it.

So this means that i cannot controll my robot without a computer/laptop as a middle-man? I always have to take my laptop with me to controll the robot?

We're building a remote controlled hovercraft, and we'd like to controll it with a ps3 controller. We know the range of the ps3 controller isn't that far, but we can walk after the hovercraft when controlling, that's not a problem. But do we have to take our laptop with us too, to controll it properly?

Many thanks for the fast reply,
Sjors
That's right; the ps3 controller has a very weird Bluetooth setup in that it doesn't give out it's address to let other devices talk to it unless it's physically connected to that device first, via a cable. It's way easier to do that on a computer than on the robot itself, so I opted for the xbees. Yep, you will have to take your computer with you, but on the plus side, your range of control will be limited by the Xbees, not the ps3 controller, which has about a 10 ft range. Depending on the model Xbees you get, you could have a decent range of control of a few hundred feet! Just note that the xbees that I have listed here had about a 50 ft range. I'm planning on redoing this instructable code for the arduino and ps3 controller to make it slightly more robust in the coming weeks. If you start soon and have some trouble with the code, rest assured that I should have a new version in the near future.
Please can I got your code ? :)

I'm having a problem while testing the controller


Failed to poll device: Failed to poll device (8007000c)
Failed to poll device: Failed to poll device (8007000c)

0
0
0
0

<<< available PLAYSTATION(R)3 Controller sliders: >>>

     0: Z Rotation absolute
     1: Z Axis absolute
     2: Y Axis absolute
     3: X Axis absolute
     4: Pointer absolute

<<< >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>

<<< available PLAYSTATION(R)3 Controller buttons: >>>

     0: Button 0
     1: Button 1
     2: Button 2
     3: Button 3
     4: Button 4
     5: Button 5
     6: Button 6
     7: Button 7
     8: Button 8
     9: Button 9
     10: Button 10
     11: Button 11
     12: Button 12
     13: Button 13
     14: Button 14
     15: Button 15
     16: Button 16
     17: Button 17
     18: Button 18

<<< >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
Exception in thread "Animation Thread" java.lang
.NullPointerException
 at Test.draw(Test.java:60)
 at processing.core.PApplet.handleDraw(Unknown Source)
 at processing.core.PApplet.run(Unknown Source)
 at java.lang.Thread.run(Thread.java:662)

It looks like the library doesn't correctly understand the information from the controller. This actually may have to do with the driver of the ps3 controller, not the code itself. I actually don't have access to a ps3 controller right now, but I may be able to help in a day or two.

It looks like the driver is telling the proCONTROLL library that there are buttons or some other method of input that actually doesn't exist.

At this point, can you actually see the "paired" bluetooth symbol if you try to connect the controller to your computer?
lm using ds3 tool to my controller and the controller is connected by USB cable not BLUETOOTH DONGLE so there will be no bluetooth symbol
Hello, I'm having the same problem as devil and it's allready 2012 and I don't see a response to this problem..can you please help? (I think many ran into this problem). Ty.
I installed the library proCONTROLL processing is not top of my hair
I installed proCONTROLL library in processing.
Hey I am interested in doing something similar but I'm wondering how to go about it without the NXT motors, is it possible? Mostly I'm interested in how the micro controller on the robot would be programmed
can i use the following Battery if not which one

http://www.ebay.co.uk/itm/Ansmann-RC-Car-7-2v-3000mah-Battery-Delta-Pro-Charger-/280716528572?pt=UK_ToysGames_RadioControlled_JN&hash=item415c024bbc

please reply ASAP!!!
I was about ask abou that thanks .PLEASE HELP ME WITH THE CONTROLLER TEST
Sure thing; I'll be able to take a closer look at it this evening. (I currently can't get to a controller until then.)
More Comments