Defcon Bot

June 30, 2007 at 1:49 am

Joe and I have created a gun for the Defconbots competition. Basically, it is a shooting gallery with autonomous guns; the goal is to shoot down all the targets before the other person does. See the video here:

Real Stage 1 Sentry Gun


The most common way that people are shooting down the targets is through the use of an off-the-shelf airsoft gun and mounting it to a pan/tilt servo. Initially, we looked in to doing our gun that way, however the cheap airsoft guns didn’t have the accuracy we wanted, and weren’t as hackable as we would’ve liked (to increase the hopper size). There were more accurate airsoft guns, but they are much more expensive and seemed to be a waste because we would be gutting almost all of the parts. Because of that, we decided to make our own mechanism to fire the BBs. We took the basic premise of airsoft (use air to propel the BB), and replaced the firing mechanism with air from an air compressor. The most difficult part was coming up with an efficient way to load the BBs into the stream of air, and fire one at a time. After trying a few different methods, we settled on a method where the BBs are loaded from the top in a small column directly above a horizontal shaft (the barrel) with a hollow plunger that prevents BBs from dropping from the loading column into the barrel. The plunger is retracted to let a single BB fall into the barrel, then pushed forward to close off the loading column so that more BBs don’t fall in when the one in the chamber is fired. There is a servo that controls the plunger, and additionally the servo can trigger a switch that opens a valve connected to the air compressor, causing the BB to shoot out of the barrel at a high speed. A video of this is here:

To get the gun to point at the correct target, we are going to us a pan/tilt system designed for cameras. It seemed like the best option in this case to just buy something that would work well enough for our needs, rather than try to create something custom. After it is all put together, we plan on making it look like the sentry gun from Team Fortress, however, we can’t agree on what color it should be, either blue or red.


The electronics for this project are going to be relatively simple as well. To control the three servos (pan + tilt + firing), we are going to use the Pololu 8-channel servo controller. Normally we might use an atmega to do the control, however I already created code to control the pololu controller for another project and it’s really inexpensive/easy to use. If it required more complicated feedback or logic, a microcontroller might have been the better choice, but in this case it wasn’t, so the right decision was just to use the Pololu controller. To control the valve, we had two options. One was to use another channel on the servo controller and something like a Banebot motor controller to apply power to the electronic valve, the other was to use the servo that pushes the plunger to push a switch that opens the valve. The ladder option was chosen.


The software to control the gun was created by us using C#. The first step that is needed is to identify the targets. For this, we used a sobel edge detector on the image which detected all of the edges in the image. The edges occur whenever there is a sharp contrast between one area and the area next to it, in this case we wanted to find the edges of the targets to the background. As a side effect of doing it this way, we weren’t constrained to finding white targets against black backgrounds, but anything that had contrast against the background (however a solid background is still needed). A thresholding function was then added to specify just how much contrast was required before detecting a target. Then each target is pulled out of the image by using a flood-fill algorithm and numbered. See the video of this code working:

After the targets have been identified, the gun needs to be able to go to each target and shoot them down. To do this, a solver was created for the Traveling Salesman Problem to determine the optimal path to go to each target. After an optimal path is found, the gun must actually move to the position of each one. The basic strategy is that the gun will fire at a particular location on the screen (usually near the center). If the current target is above, then it must move the tilt servo up; if it’s to the right, it must move the pan servo to the right. After the servos have been commanded to move, another image is sampled from the webcam and the process is repeated until the target is in the center of the screen. At this point, the gun starts the firing process which moves the piston back to let a BB in the shaft, then forward until the valve is opened, opening the air and firing the BB; finally, the piston moves back in to the “safe” position which does not open the valve and blocks new BBs from entering the barrel. Then the servos move a random distance away and the process is repeated (the motion is because sometimes the software thinks the target is in the center, but it truly isn’t). This process is repeated until there are no targets to shoot at. At this point it goes into a “seek” mode and will pan to the left and right looking for new targets and occasionally playing the sound the sentry gun in TFC plays.