Here's how to make a Lego robot you can control from any screen over wifi. It also has a webcam so you can see where your going and LED lights for eyes ! This is a great project to do with your kids as they can do the logo building and you can involve them in as much of the electronics as they are happy with.
There is lots of flexibility in how you implement this project, and this means you can easily take it to pieces and redesign again. I found it useful, when I got stuck to simply start again. Once you have done this a few times, its pretty easy and quick to rebuild from the ground up. The great thing about this is that you don’t need to be worried about "breaking" anything as you'll develop the skills to easily rebuild.
What you need
- Raspberry Pi B+
- Base and motor kit
- Lego base plate
- Industrial strength super glue
- Craft knife
- Soldering equipment
- SD card
- SD card reader
- Motor controller kit
- 2 LEDs
- mid priced Webcam (you could try a cheap one)
- PC and a tablet / phone
- Wifi Router which you have admin access to
- Lots of bits of Lego
- Emergency phone charger
- 4 jump wires each with at least 1 female terminal
- 4 AA batteries (rechargeable is good)
Step 1: Prepare the Base
I started with a base and 2 motors like this motor set at Amazon
Take the perspex base and glue a thin Lego base plate onto it. I used industrial strength super glue for this which dries in minutes and is super strong. When the glue is fixed, cut away the excess Lego base plate so it follows the line of the perspex base plate. then drill holes through the Lego plate that match the perspex base. I drilled the holes as and when I needed them. You now have the foundation for building you Lego structure.
Step 2: Assemble the Wheels
Now assemble the wheels. Follow the manufacturers instructions here. The wheels I bought had all the instructions in Chinese, which I cant read, but it was pretty easy to figure out from the pictures. Once assembled attach the wires to the motors with a small blob of solder.
Step 3: Attach Wheels to Base
Next attach the wheels onto the base plate, you can drill holes in the base if you need. You'll probably find out towards the end of the project that you want the wires led through the base somewhere else, but you can always drill a new hole and relocate later.
Step 4: Prepare the Pi
Now get the PI ready. I used a model B+ with a USB wifi dongle. I used the Full desktop image based on Debian Jessie which you can download from the Raspbian Pi site. Its worth creating the SD card from scratch, and keep the disk image handy as you can easily start again. You might need to do a bit of googling to figure out how to create the disk image on your PC. I created my image using Ubuntu like this: Right click on the disk image and select "open with disk image writer". Write the image to the SD card. The disk image is quite small, so if you have a larger SD card (eg: 30GB) you can use partitioning software like GParted to use up all the space available. Once you’ve worked out how to do this it takes about 5 minutes, and is really worth it as if you mess up the software you can just reinstall again.
Step 5: Mount the Controller Board
Next you need a motor controller board. I used the kit from RyanTek . It comes with an excellent soldering guide, but I think you can also get it pre soldered. If you haven’t done any soldering before, don’t worry, just take it slowly and practice a bit before you start on the motor board.
Now, attach the battery pack (with 4 AA batteries) and the motors up to controller board then attach the controller board onto the GPIO pins of the Pi. Again the controller board will give you more details. Don’t worry about getting the right motor wires into the right slots, so long as they are not connected up to the DC power slots. I don’t glue or screw the Pi or the Battery pack onto the Lego board as this gives you more flexibility when designing the Lego body. You can also change your design easily as you go along.
Step 6: Start Building the Lego Body
Now its time to build some Lego. At this point you can get really young kids involved, so long as they can hold a piece of Lego ! We started with just a basic square housing till everything was working then got more adventurous with the dog bot. The great thing here is that you can rebuild the robot body as much as you like.
Start by creating a home for the Pi. Remember to leave room for the USB ports and the power cables. Build a fair amount of looseness into any housing you build. Its OK for things to rattle around a bit and you cant be too accurate with Lego.
We used Lego windows to provide access to the USB ports which you'll need later.
Step 7: Prepare the Pi
Its a good time now to start up the Pi, and get the the robot moving.
I've been developing the code to use with this project at Git hub.The Read me on the git hub pages take you through the instructions on how to install all the software on the Pi, but I'll also take you through the steps here.
First start up the Pi, by plugging in the power cable. You'll need to connect the HDMI cable to a TV and use a USB keyboard and mouse. First thing is to connect up to your Wifi. you do this by clicking on the icon on the top right of the Raspian desktop and proving the details. Once connected to the Wifi you can do everything else from the command line, so if you prefer you can connect up to the Pi using ssh, but for now, as we are connected to a TV we can open a terminal from the Raspian desktop.
Step 8: Install the Software on the Pi
Open up the terminal on the Pi and type the following commands.
- sudo apt-get update
This updates Raspian with latest software.
Next, install the webcam software
- sudo apt-get install motion
You also need to configure motion at this point. I've listed what needs to be updated in which files on the github pages. There is also an instructable about getting a webcam working on your Pi. Get to know the settings and play around with them once you are comfortable with the setup.
- sudo apt-get install python-dev python-pip
This installs Python libraries and also the python software packaging manager.
- sudo pip install 'pubnub>=4.1.2'
This installs Pubnub, the software that manages the messaging between the control panel and your wireless robot.
It takes a while to install this part so go and make a cup of tea !!
Step 9: Install the Robot Software
Now you have installed all the dependent software bits and pieces, its time to install the code required to run the robot.
this will create a lego-robot folder in your home directory.
Before you can run the robot code, you need to create a PubNub account and get a publish and subscribe key. you can do this by following the getting started now instructions at PubNub. Once you're set up you need to open up the two files:
and replace the pub and sub keys with your keys that you can get from the pubnub dashboard.
Note that the channel value can be anything, so long as it matches on client and server.
Step 10: Install the Controller Software and Check It All Works
On a PC, clone the git hub code in the same way as we did on the robot.
You might not have git installed on your PC. If not please follow the instructions at git hub.
You also have the option of downloading the code as a zip file, although I would advise installing GIT and copying the files by cloning the repository.
you'll need to up date the lego-robot/client/keys.js file with your personal publish and subscribe keys.
When you are done, open up the lego-robot/client/buttons.html file in a web browser. You should now be able to control your robot using the control buttons.
The diagram above shows what’s happening:
- When you click on a button a message is sent to pubnub (using your publish key)
- PubNub then forwards to the robot who...
- Is listening (using the subscribe key) for messages
- The robot then translates to an action.
I used pubNub as its free, quick (realtime) and greatly simplifies the sending and receiving of messages using a really nice API.
You can dig into the client and server code and see the messages being sent using simple keys like "forwards" and "backwards".
There is also a joystick style controller at lego-robot/client/joystick.html.
I'll be improving these controllers, and adding new ones all the
time, so if you ever want to update software, you can use the command "git pull". Please also feel free to contribute to the git repository and create more user friendly controls.
Now you are all ready to check the movement works OK.
On the Pi terminal, change directory into lego-robot/server and type:
- sudo python robot.py
this starts up a python script to listen out for incoming messages from Pubnub.
Now try pressing some buttons on the controller, you should see these responses received on the Pi Terminal (as shown in the picture above) and the robot.py translates these messages to movements by mapping them to signals on the Pi GPIO.
You should be able to move the robot around.
Step 11: Prepare the Webcam and LED Lights
You can use any webcam that's linux compatible (most of them are). I would go for a mid range one (about £15) as I tried a really cheap one and couldnt get any kind of a clear image from it. First step is to disassemble the webcam so you just have the basic board and camera.
If you want to install lights, then take an LED light and connect two jump wires to each terminal. I used jump wires that have a female terminal at one end so you can easily plug it into the Pis GPIO pins later. The other end of the wire you need to strip, to remove the terminal. You can solder here as well to make a secure connection. I also used some heat shrinking wrap, which you can use to cover up each wire, although I wouldn’t worry about making too good a job of it till you are happy that everything’s working, then you can finish up and improve. Its also a good idea to use a resistor to stop your LED from burning put.
There is a detailed instructable here detailing how to wire LEDs.
As the picture shows, I use a Lego block with a hole in it as an easy way to mount the LED onto the robots head.
Step 12: Create the Head With Nose-cam and Lights
Create a head housing for the camera. I wont be too specific here as its time for you to be creative. However you do need to build around the camera . Dog bot integrates the camera into its nose and has the eyes as LEDs. Make sure you leave a hole at the back of the head to lead the wires out.
Step 13: Complete the Body
Now you are happy that the dog bot is basically moving you can create a housing for the battery packs. I used a pack of 4 AA batteries (provided with the wheels and base) and a emergency phone charger pack. The AA batteries are for the DC motors for the wheels and the mobile phone pack is for the Pi. Its a good idea to make the batteries easily accessible as you'll have to access them for recharging and replacing.
Once your happy, put a roof over the Pi. I don’t worry too much about hiding the wires and circuitry, as that’s part of the robots image. However its all up to you at this point. You might want to consider how heavy your bot is getting, the heavier the slower it moves.
Step 14: Mount the Head
Its time to get everything connected.
Connect the Webcams USB lead to the front of the bot, using the access holes you created. You can secure wire to the robot by creating some Lego housing. I also shortened the USB cord by cutting and re-soldering it, but only do this if you really have an urge to solder 4 tiny wires together. Might be easier to buy a short USB lead and reconnect it to the back of the camera.
You also need to connect up the LED lights to the Pis GPIO pins not taken up by the motor board controller. These are GPIO pins 20 and 21 and the two spare ground pins nearby. Use the spare ground pins nearby. If the light doesnt work then try switching round ground with the GPIO output. TO work out which pins to connect to google a good GPIO diagram.
Step 15: Test the LEDs and the Nose Cam
To start the webcam software type:
- sudo motion
Also make sure you have started the robot.py script (as detailed in previuos instruction):
- sudo python robot.py
You should now be able to use the lights switch on the controller to turn the lights on and off.
The web cam should broadcast on your Pis IP address on port 8080, so for example type the following into your web browser.
I found the Pi IP address by logging onto my router as an admin and looking at the attached devices. Instructions on how to do this are usually on the back of the router, or refer to your routers instructions online.
you can explore the configuration settings of the motion software, as described in the previous step. I find that sometimes the streaming pauses, but generally it works OK.
You can access this video stream from anything running a browser connected to you Wifi network.
Step 16: Finishing Up
You should now be able to control the robot from a browser on a PC. However its nice to be able to control the bot from any touch device like Ipad or phone.
To do this you need to host the client files (buttons.html etc) on a webserver.
I started by installing a webserver on the Pi, which works OK. However its easier to just upload the HTML file to any other webserver. I simply copied the HTML file to my public blue host account. You might want to file protect access to these files as they contain the pub sub keys to your pub nub channel. Once you do this you can access the controller from any device, even from outside your house.
Another useful thing to do is to up date the \etc\rc.local script on your raspberry Pi; adding these two lines:
nohup sudo python /home/pi/lego-robot/server/robot.py &
These will run each time the Pi starts up, so you dont have to run the command manually.
Step 17: Have Fun !
Please feel free to leave comments if you get stuck and if you want to improve any of the software please contribute to the github repository. I'll be continuing to work on this project and here's some ideas:
- A "big trak" style controller where you program a sequence of commands like "left 10, forward 6, back 2..." and then hit "run" and the robot carries out the commands.
- A tilting neck, so you can look upwards with the webcam
- A speaker that translates text from the controller to speech
- Voice controlled control