Project Autoturret


This project was inspired by a similar (identical, really) idea that I had seen on the internet (found here, a project that I thought received an undue amount of press). The only difference is, I believed that I could do it using much less money (true) with comparable results (half true).

The general premise is fairly straightforward: mount an automatic airsoft gun on two RC servos, connect the servos and a digital camera to a computer, and have the computer aim and fire the gun automatically. The premise itself is fairly simple; as always, the devil is in the details.

First things first: I acquired all of the gadgets I would need. In the Conrad electronics store in Munich (I was visiting), I picked up two RC servos for 5 euros each (very cheap) plus connector cables (not necessary, but helpful). Then, I got an Airsoft Mini AEG to use as the gun - $20. Those things are cheap as hell; they shoot about 30-40 feet and they don't particularly hurt when they hit you. But the goal was more of a proof-of-concept of the ability to do it cheaply more than anything else. So, total cost now was still under $40.
I had a crappy webcam sitting around to use as a USB, an old Intel camera with a resolution of about 160x120 or thereabouts. Which was fine - one thing I realized quickly was that high resolution was completely unnecessary, since the image processing and location techniques I would use locate the center of (image) mass of an object. Oh, and the only other thing I actually ordered was a lazy suzan roller bearing ($1 + $3 S&H). So, for right around $40 I had all of the basic materials I needed. First, I cut apart the gun to remove all excess plastic, batteries, etc. I mounted a laser pointer inside of it for calibration and I also removed the trigger switch so that I could control it electronically when the time came.
The next step was making the mount. I had some thin sheet metal lying around in the form of a housing for some old equipment; I cut that into two rectangles and made a right angle fold in the bottom of each, so i could mount them to a flat surface. Next, I glued two plasic discs that attach to the servos to the sides of the gun, one on each side, and drilled holes in the two metal sheets into which the discs would fit. I made the base out of wood and placed one servo underneath to control the vertical axis, and I placed another one on the side of one of the metal sheets to control the horizontal axis. Once complete, the mount was actually extremely stable. I had expected some wobble, but there was essentially none to speak of. The gun moved very smoothly on both axes and the whole thing just worked great.
- Note: Throughout this process, I saved a significant amount of money over the project I had based it off of. That one had been made out of some really heavy (unnecessarily heavy) pieces of wood, using more servos than required (maybe 4?). One reason that project used more servos was because a servomotor mechanically pulled the trigger; not sure what the logic was behind that. Most of the airsofts I have seen have a trigger that operates a simple switch; sometimes semi/full auto is done mechanically, but mostly electronically. The only reasons I chose a cheapo gun were cost and because I wasn't really sure if the project was going to work, so I didn't want to waste an expensive gun (aka cost).

The next step was figuring out how to control the gun. The RC servos are controlled via pulse-width modulation, so by adjusting the duration of a pulse you could set the angle of the motor (via a linear relationship). Very cool technology for $6, if I do say so myself. Now, that seemed very much like a thing that a sound output would be more than capable of handling. Hm, I thought - two motors, and a stereo output from a headphone jack. A pulsed signal. So, I set to work writing code that used the DirectSound library to output sound. I soon hit upon a very (very!) pleasant surprise. Sound is typically stored at 44.1 kHz, rarely more than that, so I figured sound cards could play back that, or maybe 88.2 at best. Turns out, with DirectSound, your soundcard can play back sounds at 200 kHz resolution. Why is this good? More samples per second translates to a much, much finer resolution when controlling the motors. I also, however, hit some less pleasant surprises. The main one was that the voltage of a sound output was just not reliably high enough to power the motor; another was that the ground of the sound output would often get pulled up and down by various circuitry (though that was mostly my inexperience at electronics). And there was still the problem of how to control when the gun fires using only two audio channels, when both were already constantly in use. Anyway, the first problem I dealt with by using some transistors or a hex inverter or something... I forget... to boost the voltage up to 6V. Once that was out of the way, the next step was firing control. I decided that I could adjust the relative phase of the two signals: when the pulses overlap, the turret was to fire; otherwise, just aim. That was something I could control from within the program, so I set to work designing the circuit. The basic idea is that I add the two signals and then use a threshold circuit; when the two overlap, there is a binary 1 that charges a capacitor, otherwise there is a 0 and the capacitor discharges. When the capacitor is charged, it triggers a relay that fires. After two days of designing, redesigning, simulating, and testing, I finally had a good, working circuit that was reliable. After some testing, I discovered that the base accuracy of the aiming (using the laser pointer) was about an inch at a distance of twenty feet. However, the motor would often refuse to set itself to exactly the right position, instead stopping a few degrees before where it was supposed to. This caused an unfavorable lag that was probably due to the cheapness of the motors that I used, and the accuracy suffered somewhat for it.

The last remaining problem was the actual image processing and aiming. That was fairly straightforward, once I got the image capture drivers figured out. I used a very simple method of frame differencing, so the program is extremely easy to fool, but oh well. Once I computed a difference image, where the more changing/more different areas are brighter, I simply computed a weighted average of the pixels. This gives me the rough center of the object, and the sum gave me the rough size. I aimed for the center, and if the size was sufficiently large, I told the gun to fire.
Now, this method inherently has a few drawbacks that I was too lazy to fix. First, say you hold up a circle. The program will average the positions and aim at the middle of the circle, which is empty. Even if you have two people stand side by side, the program will aim between them. The latter case is easily avoidable, using region separation, which identifies discrete regions in a black-and-white image. I tried to implement it briefly and failed, and didn't have enough time to try again properly.

Anyway, that's that. I put the gun together and tested it, and it tracks fairly well (after doing the pixel-pulse calibration). I mounted the camera above the turret to minimize the difference in perspective. I found that the mounted laser pointer would follow a large legal pad I waved around in the air decently. However, the gun would track quite far behind moving objects and even so not stop at the correct position, due to the aformentioned problem with the servos. I wasn't able to resolve that problem; the best way to fix that would be to not use RC servos, but to read the angular position of the motor manually to ensure it is aimed properly.
Now, there was one problem that truly scared me - the first time I tried a full-firing test, the gun just aimed everywhere wildly while firing. I had feared that it was due to the recoil, which would be very difficult to fix, but quickly realized it wasn't. What was happening was that the gun was doing two things: a) drawing so much power from the AA batteries that the servos went crazy and b) the noise from the motor in the gun also screwed with the servos. Hooking the two up to separate batteries solved both problems. A few problems, however, did remain. They would all be fixable with a redesign. The most major is that I (stupidly) didn't mount the gun on the line of recoil, right by the piston. I really hoped it wouldn't end up being a problem, but that was kinda wrong. A problem it was, as the gun tilts backwards every time it fires. It recovers well enough that the shots are grouped decently, but when it's firing full auto the spray is still nowhere near efficient (though if it's a spray you're looking for, I suppose it works).

In conclusion, it works decently well under a variety of conditions - I even have a cheap near-IR webcam for it if I want to hunt rodents in the dark. At some point I may design a proper one using a machine shop and a high-power weapon that is actually effective, but it works well enough for now. As always, email me if you have any questions, comments, or suggestions.


Technical details:

Basics of RC servo control
I used a pair of 50Hz signals that I output through the Stereo L/R headphone output of the computer, at 200,000 samples per second; this gave me 1-3 degrees of resolution or so for the motor control. I shifted the signals as follows in order to turn the turret on and off:

The signals are added; when they're in phase, the combined signal exceeds a threshold, causing a capacitor to charge. When the capacitor is charged, it ultimately triggers a relay that drives the motor that fires the turret. Works like a charm.




View of turret


Turret, camera, circuit


Firing circuit


Turret, esp. laser and vertical servo


View from bottom


Horizontal servo and plastic spacers


Targeting program


 

 

 

Copyright 2008 - Tamas Szalay.