Introduction: PS2 Controller Interfacing
Video games can be tremendous amounts of fun and we have some nostalgic ties to some games. But even our favorite most cherished games can become a bit dull if played too much, too often. And although games provide countless hours of entertainment, sometimes going out and doing something in the real world is a treat as well. Well what if you could control real world devices with a game controller?
In this Instructable I will be covering controlling a device, in this case an old Roomba 500 Series vacuum via a wireless PS2 controller using the MSP430.
Step 1: General Parts Overview and Description
The PS2 controller being somewhat outdated now by the PS3 and PS4 predecessors means they are relatively cheap to buy and there are lots of them out there. In fact, aftermarket ones are still sold for robotics uses, including the one I am using in this tutorial available through the Robot Shop here;
However it should be noted, this does work with most PS2 controllers, including the original corded PS2 controllers as well as original aftermarket versions from back in the day, of which I own many as I still have my original PS2 and I have tried them with this project and they do work. The only thing is that some button mapping is different and clock speed should be played with a little as when I originally tried it up was left and left was right, all very odd, but tweaking the code fixed that. If that doesn't work, try powering the controller with 5v instead of 3.3v. A new wireless controller like that from the Robot Shop runs $25 or so, buying off eBay or garage sales, I am sure cheaper can be found as well.
Now I have chosen a Roomba simply because its easy enough to control with a small motor controller like the L298, which can be bought as the component alone or for about $2 - $3 you can buy a board already wired up (I will link everything on the next slide). The Roomba itself is very cheap since we don't really need it to work, eBay has many Roomba's with bad boards or batteries and I picked myself up one for a grand total after shipping of about $30. I needed a battery for my Roomba, so I turned to Ace hardware and got a sealed lead acid battery, 12v and 1.3Ah (UB1213K) for $15. It vacuums for a little over an hour with this battery.
The MSP430 I used because this was for a class but I have to say I love it. It is a fantastic board, and its red, so it clearly goes faster. It was $21 and I downloaded a free version of Code Composer Studio for it.
Now before I link the bits and pieces, someone out there is going to say "Well isn't the MSP430 CMOS, and the L298 motor controller TTL? What about the PS2 controller as well? And why use a 12v battery when you could use 6v?" Yes, that is all correct to some degree, but there is a but. We need a 12v battery because the motors are 12v; actually Roomba batteries are rated for 13v, but a 12v battery is usually charged to 13.2v or so, so its close enough. The controller, at least the Lynxmotion one of the Robot Shop will play nice with CMOS (This may be the reason some other controllers may not work correctly without tweaking). And the L298 is TTL, but I built my own board and I pulled something a bit cheeky. I used a CD4069CN hex inverter between the MSP430 and the L298 and this will be explained in a later step.
Step 2: Deciding on Parts
There are fairly few parts needed for this, and most of it can be bought for around $70, and depending what you have on hand this may be less, maybe a bit more. If you have nothing at all, I still figure this could be scrounged up for ~$100.
The wireless Lynxmotion controller is fantastic, feels nice in hand, looks good, and its new so you know it works. I really highly suggest it first. Also, I assume this was not intended and was just really lucky, but the mounting holes for the original Roomba (The 500 series I have anyhow) board line up perfectly with the mounting holes and are the same size as the standoffs supplied with the Lynxmotion controller, making it really easy to mount the wireless dongle.
If your Roomba has a battery that works and a charger, feel free to utilize it. I have several broken Roombas (dead boards) and the batteries never work in them either. The sealed lead acid works well chargers with a car charger and its new, if it doesn't work return it. Ace does not have them on their website but they do stock them, although "adjusting" the battery compartment is required for it to fit, but its very easy to do.
Now comes the fun bit. I wanted to run the motors off 12v as they require, and everything else off 5v, but we have 5v and ~3v bits, there is at least 2 ways around this; the right way which is a ~3.3v supply and a bidirectional level shifter OR my way of bending the rules with what I had on hand and using only a 5v supply, a female USB connector and a hex inverter capable of CMOS and TTL logic. A third way would be Arduino that uses 5v already.
You will also need a motor controller and the L298 is a great option. It should be noted if you use a pre-built L298 board, you will need to adjust the code slightly as I have ganged 2 sets of 2 input on the L298 together, so I use only 4 pins to control it rather then 6.
- The controller (highly suggest the Lynxmotion wireless ones from Robot Shop or similar)
- A Roomba (eBay, search for broken ones, I have 3 500 series ones, I like the 500 series, that's the one I will suggest)
- A battery because I truly doubt you will get a broken Roomba with a working battery, their batteries seem to be pretty limp as far as longevity. (UB1213K, 12v, 1.3Ah)
- MSP430G2553 and download Code Composer Studio from Texas Instrument
- L298N dual motor controller (Chip or board. A Google search for L298 will bring up a plethora of results)
- CD4069CN hex inverter and a 14 or more pin DIP socket is helpful
- A few double screw terminals are handy if using the L298 chip alone and not a pre-built board
- 8 1N4004 diodes, or alternatively 2 bridge rectifiers with similar properties as the 1N4004 diode (I used 2 MDA200G, and yes it sounds odd, but how they are hooked up bridge rectifiers work and are tidier in my opinion)
- 4mm male header pins (optional but handy)
- Assorted parts; 7805 5v regulator, a 3.3v regulator (LD1117V33, optional), a on/off toggle switch, wire (short female to male), a female USB connector (optional, pick this or the 3.3v regulator)
Step 3: How I Built It
Now when I say the wrong way and the right way, what I have done isn't necessarily wrong, it works very well, but if the MSP430 was connected to something that needed to return values as well, a logic level shifter would be the way to go. So this works well enough for what I needed, but good practice would be to do it the other way as this may not always work.
Instead of using a logic level shifter, I used a hex inverter capable of doing sort of the same thing. As well I did not use a 3.3v regulator and instead, since the MSP430 can be plugged into a USB port via a USB-A to USB-Mini cable which supplies it with 5v and it regulates that itself to 3.3v, I used a 1 foot cable to plug it into the 5v supply I built to drop the 12v down for the L298. The wireless dongle for the controller I powered off the 3.3v output on the MSP430 as well.
How and Why
To communicate from the MSP430 to the L298 like I said earlier I used a hex inverter, specifically a CD4069CN. This hex inverter can be supplied with anything between 3v up to 15v and the inverter outputs when high will output the supply voltage (so 3v up to 15v, whatever you supply the IC with), in this case 5v. The inputs however are CMOS and TTL capable, so I can supply it with 0v for low and anything between 3v and 5v for high, including the 3.3v from the MSP430 when its outputs go high.
One other advantage this had was for simplicity shake. The L298 controls 2 dc motors, it has 6 pins to do this and can control; on, off, forward, backward and speed control of each motor individually using these 6 pins. If you want all that, that is fine, mine still can if you remove the hex inverter from its socket, but you can reduce that to 4 pins for control so you have; on, off, forward and backward for each motor. Its a vacuum, I wanted it to go forward, backward and turn left or right on the spot or be completely stationary, this gives excellent control of it for vacuuming and speed control wasn't needed.
Of the 6 pins, 3 control each motor; In A, In B and Enable. Enable we use simply as on and off, but if we make In A and In B inverses of each other with the hex inverter as well, whatever signal (high or low) we send to the hex inverter, when it reaches the L298 is either forward or backward now. So the wiring for one motor controller on the L298 is as follows, 2 outputs from the MSP430 go to 2 inverter inputs, one is our enable and the output of that inverter goes to the Enable pin on the L298. The other inverter output is direction and it goes to In A and the third inverter input. The third inverter out put goes to In B. Its a hex inverter, there are 6 on board, we have already used 3, the other 3 are for the second motor controller. Rinse and repeat.
So because I always feel like I am not clear or I go into stuff to much, this is it simply; to get the MSP430 to communicate with the L298 motor controller because they use different voltages, I used a CD4069CN inverter IC to sort of convert the lower MSP430 voltage to a higher one the L298 could understand. I also used it so I could use 4 pins on the MSP430 to control the 2 Roomba drive motors instead of having to use 6 pins on the MSP430. I am fairly certain this is sort of bending the rules a bit, but it works well enough here.
Step 4: Building My Version of the L298
The datasheet (below) and the picture included of the exact wiring diagram I used are tremendously handy for looking up pins and what goes where. Wiring this up is basically following those 2 documents, but I have included a second diagram on how the hex inverter is connected to all of this.
Basically, the inverter sis between the MSP430 and L298 and the 4 outputs from the MSP430 go to the inverter, and the 6 outputs from the inverter go to the L298.
As well, the 8 diodes used are arranged in such a manner bridge rectifiers do work. Each AC pin goes to a motor wire and as I recall the + pins go to GND and the - pins go to 5v.
**In the schematic I pictured, Motor DIR pins are InA, InB, etc. The Motor PWM pins are the Enable pins. I used the names that are used in the datasheet.
Step 5: Dismantling the Roomba
This needs an entire step and there is a fair bit to do with the Roomba. First off, note that there are all sorts of different Roomba models out there, I will try to encompass all of them as they all work in a similar fashion, but I still highly suggest trying to get a 500 series one, I like the way they come apart and how they look. Aesthetics matter!
Disassemble and Clean
Roombas are fairly module, each wheel/motor assembly can be taken out, the brush assembly can be removed, the suction and dust collector comes off. Everything is held in with screws, besides the face-plate on top (500 model for sure, others unsure) which can be pried off carefully as its friction fitted/tabs. I try to remove everything down to these components; brush assembly, wheel/motor assemblies, front bumper assembly, suction motor and dust bin assembly, battery, board, and the top & bottom face plates. This usually leaves a plastic circle of a carcass leftover as well. These are vacuums, they are usually a bit dusty and dirty, this is a good time to clean them up really good; get rid of all that stuff that it collected off someone else floors cleaned out of it (You are welcome for that thought).
Once this is done, I start turfing things I don't need, mainly the board, everything else I keep and most gets put back on, a few trim pieces here or there I may leave off. If you want, you can keep the board, salvage parts off of it to play around with later as they usually have a bunch of through hole pieces, some LEDs, etc. After its cleaned up most will go back together as it was, but before you do test the motors and solder some leads to them.
The motors and wheel assemblies usually have a purple casing, they have a little PCB tab that sticks out of them for the power and controls to them. If you take a a couple AA batteries in a holder and touch the leads to different pads you can find which pads power the motor. The other pads are for wheel speed sensors, if you don't care about possibly burning them out, a 9v battery can be used to test for the motor connections. If you may want to save them to do something with them in the future, 2 AA batteries at 3v will keep them safe. Once you find the 2 connections for the motor, solder a couple long wires to them. When installing them back in the frame, make sure the wires go through the hole on the frame that lines up with the tab and the wires come out the top of the unit.
The other motors either use the same tab, or some type of molex plug and the dust collector and suction motor use a friction fit thing. If you don;t wan the vacuum bits to run, don't worry about them. If you do, connect wires up to them, and the dust bin should have tabs on the frame to connect to so you can still remove the dust bin freely. I hook these right up to the power on/off switch rather then a controller.
Besides that I didn't setup any of the sensors on this Roomba, but I still install the bumper assembly with the sensors for aesthetics.
If you are using the original battery, some leads off the connections on the frame that it uses should be soldered on. If not using the original battery, the battery I have mentioned fits into the battery compartment width and length wise, but is slightly too tall. If you cut out a hole on the frame in the battery compartment the battery will fit in perfectly with the top and bottom face-plates on.
Once everything is cleaned, hacked and ready to go, reassemble the Roomba. You should have a Roomba missing the board and you may want to leave the top face-plate off for now.
Step 6: What Goes Where
The negative from the battery, the MSP430 ground, the L298 ground, the ground on everything, it all gets tied together. The positive from the battery goes to the on/off toggle switch and then to your 5v supply, whether its a bought module, or a 7805 you solder to a piece of perfboard. The 5v supply supplies the L298, the logic level shifter or hex inverter, if you have a USB female connector, it goes to that too which connects to the MSP430's USB mini slot if you are going that route. If not, the MSP430 is supplied via a 3.3v supply to its power and gnd pins. The MSP430 power and gnd pins also connect to the wireless dongle for the controller (although it says 5v on the wireless dongle, Lynxmotion does work with 3.3v, not all controllers will so you may need to connect it to 5v if you use a different controller).
The wires from the drive motors connect to the L298 output pins, I used screw terminals for these that way if something runs backwards it is easy to swap the leads, the pre-built boards usually have screw terminals for these as well.
If you want the vacuum motors to work as well I hook them up to the on/off switch so they are simply turned on when the unit itself is turned on.
At this point everything should be connected up for power, now we connect the MSP430 inputs and outputs to the wireless controller dongle and the L298. Follow the next steps for the code I have uploaded using my method, otherwise you need 2 extra pins to control the L298 and they would just be opposites of the 2 I use.
From the Wireless dongle from the controller, it goes;
- DAT to P1.1
- CMD to P1.2
- ATT to P1.5
- CLK to P1.4
- P2.0 and P2.1 go to the L298 and control motor directions (you need 4 of these if no ganging InA & InB as well as InC & InD together on the L298 as I have)
- P2.2 and P2.3 are enables to enable the motors (on/off)
(I have included a photo of mine, all wires are colour coded and no colours are used more then once if it helps)
If you play with values on the pin outs you can instead make it turn right about one wheel instead of on the spot by turning one wheel off, rather then spinning them opposite directions. As well, adding more code you can make it turn on the spot left pressing the left button, but turn left about the left wheel by pressing forward and left at the same time. but this is the basic code to get a PS2 controller communicating with an MSP430. One thing to note is the bytes received from the wireless dongle are sent least significant bit first, this was a challenge at first, mostly because I thought it sent most significant first and had several values transposed in binary. For this code I simply transposed them again and used them, although for future use a function that flips values before sending or before checking received values would be a good addition.
Step 7: Programming
The wireless controller dongle uses SPI communication, so it utilizes 4 pins on the MSP430. You need pins for Data, Command, Attention and Clock. Clock is your clock, straight forward. Attention is your Slave Select (SS, sometimes Chip Select as well, CS). Data and Command are your MISO and MOSI, respectively (We are using TX and RX pins on the MSP430 though, not MISO and MOSI pins if you have looked up a diagram). The SS is held low until ALL bytes have been sent and received, in digital mode as we are using it here, there will be 10 bytes total (5 sent, 5 received) after which the SS can be set high again. This can be looped to continuously poll what the controller is doing.
The first 3 bytes sent are to ready the dongle, and must be exactly 0x01, 0x42 and 0x00 and received back should be 0xFF, 0x41, 0x5A. If these are returned, the next 2 bytes returned will be button data, with each bit in them corresponding to a button. Button pushes are designated by a low value in the data, unpressed buttons remain high.
This way is digital mode, the default setting on the controller. This means you can use the analog sticks, but you only get forward, backward, left and right and either on or off. The controller can be set to analog mode as well. A good resource for that is;
Step 8: Code and Additional Resources
I have uploaded the C file for the code, it is titled main.c
The code is fairly simple, it loops through sending bytes to the wireless dongle, checking the first 3 returned then evaluating the next 2 bytes for which button has been pressed, then loops through the code indefinitely.
A very handy website for values and how to also get the controller into Analog mode is listed here below;
And for a little more on the MSP430G2553 like I used, Energia has some great info and a picture of all the pin outs which comes in very handy as well;
If you have questions, feel free to ask, or where I can clarify. Its been awhile since I made an Instructable, so its probably not perfect, or far from it.