PiTank – A web controlled tank with cannon and live video stream
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
- Enabling SSH on the Pi is pretty much a necessity for file transfer and remote login
- Putty for Windows PC
- PuTTY is a great client for SSH on windows. Get it here
- FileZilla Client
- Very useful for file transfer between PC and Pi
- Static IP Address
- 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
- RAM Split
- 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.
- Enable I2C
- Enabled I2C in sudo raspi-config
- Raspbian OS
- 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.
For more detail: PiTank – A web controlled tank with cannon and live video stream