Intro: Remote Raspberry Pi Robot (PS3 Controller) - Fablab NerveCentre
These instructions try to assume very little. Hopefully, if you can boot a Raspberry Pi and have an internet connection you should be able to complete this great little project.
Basically, this is a project which will result in some specific areas which can be used in other projects.
The main outcomes will be:
Using a PS3 controller
Controlling DC motors
But along the way, you'll get a chance to look at some code and perform some often used linux commands in the terminal.
Step 1: What You Need
It's a bit of a list - but nothing is terribly expensive. I've also tried to keep the list to commonly used components that will be useful on any related project.
For example, this project could be adapted to control a garage door or to trigger some other project by incorporating a motion detector and/or camera.
So here's the list:
- Raspberry Pi & SD Card with Raspbian installed
- DC Motors (pictured are 2 different types - one set is geared)
- L293D motor driver pcb (ChrisMason has described how to breadboard this chip if you don't want to buy the pcb)
- Powered USB hub
- 9V battery & holder
- 5V power source
- PS3 Controller
- All the necessary cables/power supplies for the above
- You'll also need to download the *.sh files (attached) to your /home/pi directory.
Step 2: Wiring the Motors
When I began doing bits and pieces with Arduino and Raspberry Pi, I didn't get why motors couldn't be connected directly to them. Here's the quick explanation.....
If you look at the specifications of a motor you'll get information about it's RPM, ampage, ratio etc. Also included will be a voltage span within which it can operate. For the majority of motors used by hobbyists and makers, this voltage span is likely to be 6V or greater.
For the motors I'm using in this project, I'll be using a 9V battery. Now, a 9V battery is too much to run the Raspberry Pi from. Also, hooking up the motors directly to the RPi means that the greedy little things will try to draw more current than the RPi (or indeed an Arduino) can provide.
This can result in damaging your hardware.
You can wire the motors, battery holder and wires to the L293D pcb as shown. The 9V battery will independently power the motors. The motors themselves are attached to terminals marked "MA" and "MB" on the board I have.
The other 3 pins (per side) are inputs 1 & 2 and an enabling terminal also. The enabling terminal could be regarded as a kind of handbrake, you have to enable the motor on the channel in order to drive it. The other 2 pins are switch controls which determine the direction the motor will spin.
Step 3: Connecting the Motor Driver Pcb to the RPi
Apparently you shouldn't connect anything to the GPIO pins while the RPi is running so it's worth making sure to heed this advice or it could be expensive.
You can connect the wiring to the RPi as shown in the image.
Don't put the 9V battery in just yet. I've found that if the battery is left in, it will slowly drain. Given that setting up the RPi for this could take some time, we'll just leave it out until we're ready for testing.
Another solution to this is that you can add a simple switch to the battery holder.
Step 4: Getting the PS3 Controller Running on the Raspberry Pi
We now have the connections all made and so can move on to setting up the PS3 controller.
There are a good few tutorials on how to do this. The one I initially used is available here:
For anyone who is a beginner at using the command line in linux going through all the commands and encountering some problems can be really frustrating. To help with that, I've started off with a completely clean install of Raspbian so that I can hopefully identify every issue you might encounter.
The first thing you need to do is install some background programs and libraries. These are known as "dependencies".
I'm going to give you something of an easy option here by providing script files for you to run. This should make the process easier. To run a scriptfile you just type "sudo sh ./scriptfilenamehere.sh" at the command prompt.
The first to be run is 1-dependencies.sh (Get yourself a cup of tea)
After running this script we need to check if the RPi is seeing the bluetooth dongle. Run "hciconfig" at the command prompt. This will give an output the same as the image above if you're bluetooth dongle has been attached before booting up. (If not seen, you may need to reboot)
The next script to run is 2-downloads.sh. This script will create a directory and download some necessary files to it. This shouldn't take long.
The third script to run is 3-buildfromsource.sh. This will compile and install the sixaxis manager software. You will be prompted to answer a few questions. To do so, type nothing and just hit [ENTER] each time you are asked anything.
The next script is 4-tidyup.sh. I'm sure you can guess what this is for - just a bit of housekeeping to keep things clean.
Step 5: Connecting to Your PS3 Controller
At this point you've done the majority of the work to having your PS3 remote controlled robot up and running. Now, we're going to make sure you can connect up to the PS3 controller and that your Raspberry Pi is receiving inputs from it.
Make sure your PS3 controller is connected to a USB port on your RPi (or to your powered USB hub).
You can now run the PS3_connect_controller.sh script which will pair your controller and then ask you to press the PS button. Unplug the controller and press the PS button - you should then see the message as per the image.
Your PS3 controller is now paired and ready to use with any game or robot which can take its signals.
- When running PS3_connect_controller.sh you may not manage to connect due to the bluetooth dongle possibly "sleeping". The immensely technical solution to this is to remove it and then plug it back in!
- Everything should run correctly - if not, please comment and I can try to troubleshoot. (& update this instructable)
- One likely issue you will meet at some point is that the PS3 controller may not be seen while plugged in to USB. Type "ls /dev/input". This will list attached input devices and should include "js0". If not, plug out and plug back in your PS3 controller USB connection and wait before listing input devices again to look for js0.
Step 6: Controlling the DC Motors With the PS3 Controller
At this point you now have the hardware connections to the motors complete and can read from the PS3 controller.
Now you can download the attached code which is written in Python.
I'd recommend placing this code in a folder - call it "PS3_robot" or something. It's just easier and safer to do this as things can easily get lost or deleted when left sitting in /home/pi.
Assuming you have only just turned on your RPi (or perhaps have moved your SD card to another Pi)you will need to open 2 terminals. In the first terminal, run:
- sh ~/PS3control/PS3_connect_controller.sh
In the second terminal, run:
- sudo python ~/PS3_robot/PS3_motors.py
Now you can have a go at moving the motors using the PS3 controller. Each joystick will control forward and reverse movement (see video).
Step 7: Creating a Chassis
For motor mounts I 3d modeled a few parts - here's the one's I used for this project on thingiverse.
See the attached pdf file for the chassis and the images for some of the assembly.
You'll need a decent mess of 3mm nuts & bolts to put this together.
To assemble the pi covering spacers, a circular one goes at each corner followed by the L-Shaped spacer.
With respect to the different spacers included, some have quadrants cut from them - these 5 go above the L-shaped layer on the corner of the Ethernet connection.
The few which have flats on them are for going directly above the L-shaped layer at the power socket. The remaining circular spacers make up the rest of the height to get to the top cover.
Step 8: ......Enjoy!
And if you want to know a bit more about the scripts you have run and/or the python code you can do so by typing "nano ~/<directoryname>/<filename>".
The *.sh script files can be a reference point for you if you wish to try to "automate" any work which would otherwise be far more tedious.
The Python script can also be useful in other areas. For example - any work with the GPIO should be shut down afterwards as it is here.