Lego is great for teaching kids about how things work while letting them have fun at the same time. I know I always enjoyed "playing" with lego when I was a kid. This instructable describes how I built a FPV (First Person View) tank out of lego and a Raspberry Pi 3 (Raspi 3). i tried to keep it as simple as possible, only the step where you adapt the motors to work with lego requires tools and a bit of skill.
The tank basically uses two motors so if you don't want a tank you can make a Romba type robot, the build will be different but the wiring and programs will be exactly the same.
### This project is basically a version 1, so if you want to improve it (of which there is plenty of room to do) please leave a comment. Also all code will be available, on my Github page, links are in the steps.
Things you will need:
- Some Lego, I used a Lego Technic Arctic Truck that I had lying around. Use your imagination though, this set had tracks and everything to make those work so it was good for this project.
- A Raspberry Pi, I used a Raspberry Pi 3 because that's what I had, if you have a different model you might be able to make it work but the GPIO pins will differ.
- A micro SD card with Raspian installed, for the Raspi 3.
- A Pi camera, Adafruit sells a few as well as different ribbon cables of various lengths. A bought mine from Aliexpress, it has a fisheye lens and was cheap. You might be able to use a webcam, but a Pi camera works out of the box.
- A USB Power Bank to power the Raspi 3, I think mine cost $8, its 2000mah so runs the Raspi 3 for a while.
- A battery to power the motors, I used a battery I pulled out of a cheap RC car, it's 7.2 volts, 500mah, and rechargeable so it works pretty well. A 9V battery would be good but you will then also need a connector.
- Jumper cables, to connect GPIO pins together, at least 5 female to female.
- L298N Motor Driver Board, these are pretty cheap and allow you to control to motors separately. These are also pretty standard for this type of application.
- 2 x DC gearbox motor, these ones from Adafruit are good, the exact same one can be bought from Aliexpress too
- Double sided foam tape
- heat shrink
- rubber bands
- micro USB cable
If you are going to adapt the DC gearbox motors like I did then you will need:
- box cutter
- dremel with circular saw blade
- mini files
- 5 minute epoxy
You will also need your own PC/laptop to control and program the Raspi 3.
Step 1: Build the Tank
So these aren't really instructions because this step should really be your own design. I will talk about some of the things I had to consider when building this, but the real fun lies in finding out for yourself (much like actual Lego). Use the photos if they are helpful, it took me ages to develop this design, in the end, the simple road was the best.
- Build the rear axles first
- and make sure to leave enough room for the motors and make them wide enough for you components to fit inside of. I wanted mine to be quite wide as I wanted everything sitting inside of the axles, this allowed for the tank to be quite low an fit under things and chase the cat.
- The wheels on the tracks here have a hole that fits a Lego cross axle, so bear in mind this is where your motors are going to mount.
- You need enough clearance for the track at the back and front. You can notice in picture 2 that the "L" peices are not semetric, this is to allow the track room. I originally had them semetric but the track kept rubbing and did at one point become jammed and break the motor adaptor.
- using long bits with cross supports at regular intervals. Make sure you space the cross supports so that components fit inbetween them, this helps keep a low profile.
- the length will be determined by how much track you have. This track has no stretch so a bit of slack is needed. If you have a rubber track you can make it tighter. Also a running wheel is a nice idea but overall is not needed.
- This step was petty straight forward and just involves a bit of trial and error.
Hopefully these notes are helpful. I built this thing before writing the instructable and am a bit hesitant to pull it apart now that its going. I really do beleive that a better design can be made so I think your own development would be best. Leave a comment if you want me to make it into a full guide, if there is enough demand I'll do it.
Step 2: DC Gearbox Motor to Lego Adaptor
Again, I made this before writing and didn't take any photos. I think there are a bunch of different tutorials out there that do this. Adafruit actually sell an adaptor, this is the best way, plus you don't need to destroy your motor mounts. I'm in NZ though so Adafruit is not avaliable, but DIY is :-). This is what I did (sorry about the bad diagrams):
- Get all your tools ready, we are going to be cutting plastic, so this won't be uber difficult. I used a pair of workshop glasses because personally I hate small peices of plastic all over my eyeballs. I also used one of those green cutting boards so I didn't mess up my table.
- So the diagram shows a top and side view. Basically, the gray is the little white part on the DC gearbox motor and the red is where we cut. The red on the top view is actually supposed to be a cross section for a Lego cross axle. We are going to remove that material so the axle fits snuggly inside. Try and cut this as close to the center a possible and almost right to the bottom. I started by first roughly cutting with the circular saw bit on my dremel, then a started shaving of bits with a box cutter until I had a perfect fit.
- Once you have that bit cut and the axle fits more or less straight (it should look like a weird folded in half fork) you can epoxy the Lego cross axle in. Before you apply any epoxy make sure you apply masking tape over the yellow case of the motor so you don't accedeintally stick the shaft to the case. Mix the 5 minute epoxy well and apply a thick layer over the white bit and the cross axle, we are looking for a 1-2mm thick coat. 5 minute epoxy goes tacky and unusable pretty quickly so work fast here.
- Once you have a desent coat on and the epoxy is not running you should be done. It does run a bit so just be careful. Once it is set to a rubber like consistency you can easily trim off any excess epoxy, that's how I got a nice flat end on mine.
- Leave it to dry over night and you should have a Lego DC gearbox motor by the morning
Step 3: Wiring It Up
The wiring for this project is pretty simple. We will use the 4 female to female jumper cables to connect our GPIO pins to the L298N motor controller and a male to female jumper cable to connect the ground of the Raspi 3 to the ground of the L298N motor controller. We will also use some wire to connect the motors to the L298N motor controller.
I choose to use this combination of pins on the Raspi because they are clustered together. Feel free to use any set of GPIO pins and GND though. Just remember to change it in the code.
You can use the wiring diagram or follow the steps below:
Raspi 3 --> L298N
GND (pin 14) --> GND
GPIO27 (pin 13) --> IN1
GPIO22 (pin 15) --> IN2
GPIO23 (pin 16) --> IN3
GPIO24 (pin 18) --> IN4
For the motors and the way they are mounted I wired them up this way.
Out2, Out3 --> negative
Out1, Out4 --> positive
Essentially negative was towards the back and positive was forward. If you get them the otherway round, the tank just drives in the opposite direction which is easy to fix in software.
Step 4: The Programming
So for this step we're going to need to set a few things up if you haven't already.
- Python 2 or 3
Firstly we need to have Raspian on a micro SD card, so have a formatted SD of at least 8GB ready (You can also install NOOBS lite if you only have a 4GB card).
To install Raspian I would recommend using NOOBS. The official link is here. Download the zip file onto your PC, and extract the files into the sd card. Make sure the files and folders are there and not within a noobs folder.
Once you've done this, plug the SD card into the Raspi 3, connect a screen (A TV with HDMI works fine if you do not have a monitor) and a keyboard and mouse.
You should see the install screen load, connect to wifi (wifi is needed for this project to work) and install, I just use the default settings as they work well.
Configuring for headless mode
So once Raspian is installed and you are logged into the Raspi 3, you can start to configure the Raspi 3 to run in headless mode (i.e. use SSH instead of a screen and keyboard). Just a note you will need to use sudo so make sure you know the root password.
Open a terminal and type 'sudo raspi-config', you should have a blue and grey screen like in the images. Move to 'Interfacing options', press enter, then move to 'P2 SSH' press enter and enter again for 'yes', again for 'ok'.
Now go back into 'interfacing options' and enable the camera.
Back in the menu press right and enter to select 'finish'.
Next we need to find our IP address, you can do this by typing 'ifconfig' into the terminal. Find the block starting with wlan0 (usually the last one) and note down your IP address. It should be on the second line and look something like this 192.168.1.XX if you're using your home wifi.
Great that is the end of the configuration
Installing additional software
So, to run the scripts I have, you will need to have python 2 or 3 installed. If you want to learn python, I would recommend learning python 3, the differences are minor but python 3 is now more widely used. It should come preinstalled with Raspian but we should just double check.
Type 'python --version', you should get an output like 'Python 2.7.13' which means you have python 2 installed. To check if you have python 3 just type 'python3 --version' and you should get a similar output. If you don't have python 2 or 3, you can type 'sudo apt-get install python' or 'sudo apt-get install python3', respectivily.
You will also need Git to get the code, again, it should be preinstalled. Type 'git --version' to check and use 'sudo apt-get install git' if you don't have it.
MJPG-Streamer is one of the ways I got access to the Picamera. It allows you to access the camera via a browser and manipulate the image. It is probably the nicer and easier way to use the camera if you're uncomfortable with code.
- Again we are going to use Git. Type 'git clone https://github.com/jacksonliam/mjpg-streamer.git' into the Raspi 3 terminal. The software will be downloaded, it doesn't take too long.
- Probably the best thing to do at this point is to look at the 'README.md' file and follow the installation instructions. They worked seamlessly for me. If you have any questions, leave them in the comments and I'll try and help
Once installed you can run it. I will go through how I do that below.
Putting it all together
Great, now we should be ready to go. Make sure your Raspi 3 is powered on. On your PC/laptop, open a terminal (I'm assuming you're using linux or Mac, if using Windows you will have to download putty. There are plenty of tutorials online about how to use this, dont worry, it's easy) and type 'ssh firstname.lastname@example.org.XX (assuming you haven't changed the default username) or what ever your IP address was that we found earlier. Type in your password (this should definitly not be the default). Great, now you are in a terminal session on the Raspi 3 via your PC/laptop.
So, in the terminal type 'git clone https://github.com/astrobenhart/Raspi-3-FPV-Lego-T... This should only take a few seconds as the files are really small. You can then navigate into the directory using 'cd Raspi-3-FPV-Lego-Tank', now type 'ls' and make sure you see these 5 files: 'demo.py', 'drive.py', 'Picamera_tank.py, 'finished.jpg', and 'README.md'. Make sure you look at the readme for any updates.
This py script is great for testing your wiring is all working. It just goes through different combinations of driving the motors backwards and forwards.
use 'python demo.py' to run. It takes half a minute or so to complete.
This is the py script you will run to control the tank. It sets up the GPIO pin mapping and creates defs for moving. It also captures key presses to control the tank.
Use 'python drive.py' to run. Give it a second, your terminal should go blank.
use 'w, a, s, d' to move and spacebar to stop. When you're ready to close the program press 'n'.
This is my version of a streamer for the camera. This only runs with python 3 (i.e. use 'python3 Picamera_tank.py' to run). This can be run in a second terminal window or you can press ctrl-z and type bg to run it in the background on the same terminal. Personally I like to use a seperate terminal.
You shouldn't have to install anything extra, but if you do use pip. If you run into any troubles, leave a comment.
Once this is running on the Raspi 3, log into the brower of your PC/laptop and go to 192.168.1.XX:8000 (the IP we found earlier). You should see the camera output. If the image needs to be rotated, you will need to edit the py script. Near the bottom is a comment, below enter in the degrees of rotation you need. For me it was 180 as my camera is upside down.
To run MJPG-Streamer
To run MJPG-Streamer, I navigate to 'mjpg-streamer/mjpg-streamer-experimental' and run './mjpg_streamer -o "output_http.so -w ./www" -i "input_raspicam.so -hf"'.
Once that is running go to 192.168.1.XX:8080 (the IP we found earlier) and click on streaming. Play around with the other options, they might be useful to you.
And that's it. You should now be able to drive you own FPV tank around where ever you can get your wifi. Have fun.
Step 5: Finished Product
And here is a video of it all working.
Just a note that I have the Raspi 3 plugged into my laptop to power it in the video as the power bank ran out during some testing. It lasted nearly an hour which I was pretty happy about.
Please leave comments if you have any and I hope you enjoy making this Raspberry Pi 3 FPV Lego Tank if you give it a go.
This is an entry in the