Introduction: PiTank - a Web Controlled Tank With Cannon and Live Video Stream
First Prize in the
Raspberry Pi Contest
The PiTank is a web controller tank built for a competition at my school's robotics club. The project took about two weeks to complete with all 5 team members participating in various aspects. Its main projectile are ping pong balls (up to 3).The firing mechanism is based on a spud gun. The PiTank is controlled using a web browser and is capable of real-time video streaming. It is powered by a Raspberry Pi and an Arduino, along with a few other components. Here is a picture of the completed project and a demonstration video.
The instructable itself will touch on every aspect of the build but will not go into the small details. However, links to other useful tutorials and reference guides will be provided.
Step 1: Materials
The list of materials is mostly guideline. I won't go into every little piece that you'll need for the project. You can substitute a lot of the material with what's available in your area.
Raspberry Pi Components
- 1 x Raspberry Pi ( any version works)
- 1 x Raspberry Pi Camera
- 1 x Wifi Adapter
- 1 x Adafruit Servo Board
- 1 x USB battery pack (make sure it has 2A output)
- 2 or 3 Servos (2 for pan/tilt turret control and 1 for reload mechanism)
- 1 x Pan & Tilt Mount
- Wires to hook up everything
- Electrical Tape
- USB Cables
- HDMI cable
- 1 x Voltage Level Translator (RPi can only accept 3.3V on it's Digital IO pins)
- 1 x Arduino Uno
- 1 x Arduino Motor Shield
- 1 x Relay Board
- Wire to hook up everything
- Motors ( if you chassis didn't come with motors)
- 1 x 2AA battery pack for the BBQ igniter
- 1 x 6AA Battery pack for the motors
As for the cannon you'll need
- Water bottle
- ABS/PVC pipe (slighter bigger than your projectile's diameter)
- PVC T adapter
- BBQ Igniter
- Some wooden sticks
You'll need a chassis to put everything in. I used this one . Make sure you get the 4WD as it has 2 more motors which gives it extra power. Unfortunately I got the 2WD version so my robot is a little slow.
You'll also need some basic tools such as
- Screw drivers
- Hot Glue
- Super Glue
- Soldering Iron and acc.
- Wires for soldering
Step 2: System Overview
There are 2 main parts to the robot :
Raspberry pi :
- Main controller
- Hosts the web page
- Controls the camera
- Controls the servos
- Communicates with the Arduino via serial ports
- Controls the motor
- Controls the relay and BBQ sparker (used to fire the canon)
Step 3: Building - Part 1 (cannon)
The cannon is the coolest part of the project.It is made out of a hard transparent plastic project with some pvc pipes and attachments and some wooden sticks. It uses the same principle as a spud gun. The barrel is hot glued to the bottle. The wooden sticks serves as a mounting point for the fuel spray servo.
You'll need to make 2 holes to insert the spark wires (image 2) and a bigger hole to spray the the propellant ( image 4). You might need to experiment a bit to find the correct size of the hole,since the air/fuel ratio is important to achieve ignition. For the propellant , I used Bullet Axe mounted on a servo ( see image 4). The top of the can should point towards the right side of the image. When the servo is activated, it will press down on the Axe can and spray into the ignition chamber.
We used a T attachement to build a simple magazine chamber ( see image 5), It can hold up to 3 ping pong balls.
We also put a screw in the PVC pipe to prevent the ping-pong ball from rolling into the chamber. You can see it in image 2.
Make sure you wear appropriate protecting when testing/building the cannon.
Step 4: Building - Part 2 (Raspberry Pi Hardware)
As mentioned previously, The RPi communicates to the servo board and to the Arduino. It also capture live video using the RPi Camera
- Servo Board
- Follow these instructions for set-up
- Make sure you have an external power supply for the servo
- I2C should only take 4 wires total.
- The I2C port is dependent on which version of the RPi you use, make sure you know which port you're using
Step 5: Building - Part 2.5 (Raspberry Pi Software)
The software setup is a bit more complicated than than the hardware. But I'll go over it step by step.
- First you should get my codes here. It's a GitHub repo.
- Raspbian OS
- Get it here
- Flash to a SD/Micro SD card using WinDiskImager32. Or use other methods if running on Linux/OSX
- Raspbian OS
- Enabling SSH on the Pi is pretty much a necessity for file transfer and remote login
- PuTTY is a great client for SSH on windows. Get it here
- Very useful for file transfer between PC and Pi
- You'll want a static IP for the Pi so you don't have to find it everytime you want to use SSH. By using SSH and Static IP, we won't been needing a dedicated monitor/keyboard to program the Pi.
- Make sure you enable the camera in sudo raspi-config
- The camera needs at least 128 MB to work, so dedicated at least 128 MB to video card memory in sudo raspi-config.
- I recommend using at least the Medium overclocking if you don't have a Pi 2. The live stream will be much smoother.
- Enabled I2C in sudo raspi-config
- Servo Board
- Complete the tutorial in the previous page and make sure everything works.
- Under the servo folder in the github repo, there are 2 files : piservo.py, pimotor.py
- Piservo.py contains the code for the pan/tilt servo, while pimotor.py contains the code for the drivetrain motors and reload servo. You should take a look at these files and change some servo settings according to your need.
- The folder also contains the necessary library for the servo board
- Depending on I2C port, you'll need to make some adjustment in Adafruit_PWM_Servo_Driver.py
self.i2c = Adafruit_I2C(address,0) // if you use port 0 (256MB)
<p>self.i2c = Adafruit_I2C(address,1) // port 1 (512MB, PI2)</p>
- Copy the folder to your Pi home folder. You can move them to another location.
- You can automate the execution of the scripts by adding them to /etc/rc.local using VI or Nano. They will be called every time the Pi boots.
sudo python /your/file/location/piservo.py & sudo python /your/file/location/pimotor.py &
Don't execute the scripts yet. We have a few more things to setup .
- In pimotor.py, we open a serial port and talk to the Arduino by sending byte messages. Different messages tells the Arduino to do different things such as moving foward/backward or sparking the igniter.
- Control and Live Stream
- Probably the most complicated thing to set-up
- We're basing our live-stream and control software on the excellent http://elinux.org/RPi-Cam-Web-Interface. Installing it is pretty straightforward.
- This program allows you to stream the camera feed directly to a web page via a web server running on the Pi. It also allows the user to have full control of the camera directly from the webpage. We will be extending the software so it can provide a control interface to our robot directly from the webpage.
- In order to do so we will use a simple html -> js -> php control scheme
- We need to edit some files in /var/www. You should use FileZilla and SSH for this step
- Copy jquery-2.1.1.min.js, pimotor.js and piservo.js to /var/www/js
- Make a backup copy of index.php in /var/www and overwrite it with the one provided in my code
- Copy pimotor.php and piservo.php to /var/www/
- Copy layout.css to /var/ww/css
- Note that you can't copy the files directly to /var/www using FileZilla you need to copy them to your home folder and manually copy them to /var/www using sudo and command line.
Step 6: Building - Part 3 (Arduino Hardware)
The Arduino controls the motors and the relay for the firing mechanism and communicates with the Pi via the voltage translator
- Follow the tutorial on how to setup the motor shield with Arduino
- Make sure you use a separate power supply for the motors
- Look at the diagram.
The BBQ igniter works by pushing on the tail cap. Inside the tail there are 2 AA batteries. When we push the cap, the batteries makes contact and ignites the sparker. In order to attach it to the relay. I soldered the battery contacts to 2 wires (orange and black). The orange wire represents the red wire on the diagram and is connected to the + contact on the external battery pack.
- I de-soldered the connector on the board and soldered the pins directly on to the board.
- The Sig wire is connected to pin 2 on the Arduino
- The voltage translator needs to be powered by 5V on one side and 3.3V on the other side. I used the 5V and 3V output on the Arduino to do so.
- Pin 0 and 1 are connected to the voltage translator like so
- Arduino TX <---Voltage Translator----> RPi RX
- Arduino RX <----Voltage Translator----> RPi TX
Step 7: Building - Part 3.5(Arduino Software)
Just upload the code called serialwithpi.ino to the Arduino
Step 8: Building - Part 4 (Putting It All Together)
This is a more of a trial and error step. Depending on what components you use, the procedure is different. I will provide some photos and general guideline
- The cannon is mounted to the servo using a wooden stick and a pipe clamp
- The Rpi is mounted on the cannon using a wooden stick glues to the cannon barrel
- We've decide to not use the pan servo for the cannon because it was too unstable
Step 9: Testing
- If everything is programmed and connected correctly. You should be able to type in your Pi's IP address into the browser and see the main interface. From there you can directly control the robot. Of course you need to run the 2 python scripts pimotor.py and piservo.py if they are not in rc.local
- If you want you can make the robot accessible from anywhere in the world by using port fowarding on your router. I'm not going to go into details on how to do it, but just make sure you install appropriate software suchs as fail2ban to protect yourself on the Web. Also it's is a good idea to make the webpage password protected ( it is an option when you install RPI-Cam-Web-Interface)
Step 10: Improvements and Final Thoughts
I would like to improve the robot by reducing as much weight as possible. The Arduino is probably not necessary as motor shield for the Pi probably exists.
The canon has a rather long cool down period between shots. Adding a fan would probably help the airflow and shorten the cool down period.
The webpage needs some formatting fixes
All in all we had a lot of fun doing this project and certainly learned a ton!
We have a be nice policy.
Please be positive and constructive.