Hand tools and fabrication machines
We have many valuable possessions that need protection, but we can't be everywhere at once. Luckily, using image recognition and motion detection software, we can build autonomous gun turrets to protect our most prized possessions even in our absence. Whether its those pesky possums always destroying your backyard vegetable garden, or your family members getting into your cookie jar, you can create your own security guard to protect against them.
In this project, we will build a motion tracking airsoft (or nerf gun) turret with a Raspberry Pi 3. The gun turret is autonomous so it moves and fires the gun when it detects motion. There is also an interactive mode so that you can control it manually from your keyboard. We used an airsoft gun for this project, but you can easily modify this build to use a Nerf instead. This project is small, lightweight and entirely battery operated.
We created two separate operation modes: Interactive and Motion Detection. Interactive allows you to control the turret remotely and stream live video. Motion Detection uses openCV and computer vision to track moving targets in front of the camera.
Since this device fires projectiles, please use the necessary safety precautions while operating the turret. Use a nerf gun instead if you are planning on shooting people, never an airsoft gun!
Get all the 3D printable part files on our Patreon: https://www.patreon.com/posts/tracking-turret-37733071
Check out this video for a detailed walk-through on how to build it:
Building the Base
First off, take the 2' by 2' medium-density fibreboard (MDF), and trace out two 9-inch diameter circles with a pencil and cut them out using either a handsaw, or preferably an electric jigsaw.
Measure out the center of the circular MDF pieces and mark it with a pencil. A motor will be mounted to the center of one circle, and a 3D-printed flanged collar will be mounted to the center of the other circle.
For the the motor mounting holes, take a piece of paper and lay it over the motor mounting face, piercing the paper with the motor shaft. Using a pen, poke holes in the paper for each of the threaded mounting holes. This paper can then be used as a template to locate the holes on the MDF circle. Center this template on one of the MDF circles, and drill the four mounting holes using a 1/8" drill bit. Drill out the center hole using a 5/8" drill bit.
Using four M3 machine screws and four washers, mount one of the stepper motors to the MDF circle, and tighten it down with an allen wrench. With the other MDF circle, center a 3D-printed collared flange onto the center of the board and use a pencil to mark the drilling locations for each of the three mounting screw holes. Using a hand drill or screwdriver, attach the flanged collar to the board using three #8 countersink, half-inch long screws. Then line the notch of the motor shaft up with the notch in the flanged collar, and press it in.
Wire the Gun
Before moving onto the next step, prepare your airsoft or nerf gun so it is ready to be mounted onto the turret when it is built. The method of preparation depends on your airsoft or nerf gun model, but there are two options we found for attaching wires to our gun:
The first, but perhaps more difficult option, is to take apart your gun and find the switch that gets closed when the trigger is pulled. Remove the wires from this switch and solder them directly to your own longer power and ground wires and feed them out of the gun. Then reassemble the gun. The physical switch actuated by the trigger that you just disconnected will later be replaced by an electrical relay controlled by the Raspberry Pi. Although we used this option, we found it extremely difficult to reassemble our gun properly.
The second option is to leave your gun intact, but solder the power and ground wires to the electrical contacts of the batteries instead. You will then have to hold the trigger down using tape or other means so that the internal switch is closed.
Building the Turret Legs
Next we are ready to make the legs of the turret that will hold the gun. The shape of the legs will depend on your gun, but we used an approximately 7.9 inch tall leg with a 6.3 inch wide base and is about 2 inches wide at the top.
Draw your desired leg shape onto a piece of paper, and cut it out with a pair of scissors. You can then use this paper as a template to trace on your MDF board. Use a handsaw, or preferably an electric jigsaw, to cut the legs out of the MDF board.
On one of the legs, drill a 5/8" hole for the shaft of the second motor to go through, centered around four 1/8" holes for mounting the second motor, just like you did on one of the circular MDF boards. The other leg will have a half-inch hole for supporting the 3D-printed flanged shaft to rotate in.
Mount the second motor to the leg with the four 1/8" holes centered around the 5/8" hole using four M3 screws and washers. Then insert the second 3D-printed flanged collar over the motor shaft.
Now take the two 90-degree angled brackets and screw them into the bottom of each leg as shown in the picture using whatever screws fit in the holes of your particular metal brackets.
Assembling the Turret
Now you have to figure out how far apart to space the legs from each other. Measure the width of your gun at your desired attachment point. Put the 3D-printed flanged shaft into the 1/2" hole of the other leg, and stand the legs up, spacing them apart so the width of the gun is between the two flanged parts. You can then measure how far your legs should be spaced apart, and mark this spacing on the circular MDF board. After marking the circular MDF board, screw the leg with the motor on it to one side.
Before screwing the other leg in, use some sticky, double-sided, outdoor mounting tape and place it on the face of both the flanged collar and flanged shaft. With the help of a friend, place the gun in between the flanges, and hold the other leg in place while you use a hand drill to screw the leg in.
Insert the stepper motor hat onto the Raspberry Pi. Wire the power input of the stepper motor hat to the power output of the step-up converter (shown on the left side of the first picture below).
There are two stepper motor terminals on the motor hat. Wire the motor on the base of the turret to terminal A (shown on the right side of the first picture below). Wire the gun pivot motor to the other terminal B.
Place these electronics, as well as the portable battery charger, onto the base of the turret. You can use micro-USB cables to connect the Raspberry Pi and and step-up converter to the portable battery charger later when it is ready to go.
Connect the relay to the stepper motor hat by connecting the power and ground of the relay to the power rail of the stepper motor hat (red and purple wires in the third picture below). Then connect the signal wire to the GPIO pin 22 on the Pi (orange wire in the third picture below). Connect the output of the relay to the wires on your gun.
That's pretty much it for the electronics. Check out the YouTube video, as well as the links included in the description for more detailed explanations on wiring. The final thing to plug in is the webcam to the Pi using its USB cable. The placement of the webcam depends on which mode is being used: autonomous motion tracking, or interact mode. With motion tracking mode, place the camera on the table so it is stationary while the gun tracks moving targets. With manual mode, you'll want to try to align the camera field of view with the direction of the gun, so point the webcam down the boresight of the gun barrel in this case. This way, you'll see everything the gun sees when you are controlling it and moving it around.
For detailed instructions on software setup and calibration, view the YouTube video and the GitHub repository. The GitHub README file includes all the dependencies needed in order for our python code to work.
For communicating with the stepper motor hat, you will have to configure your Raspberry Pi for I2C communication. Follow the link in the video description for a guide to setting up the Raspberry Pi for I2C. You will then need to install the library for controlling the stepper motor hat from the Adafruit GitHub repository.
There are also a few libraries you will need to install for image processing and recognition, including OpenCV and imutils. Again, a more detailed explanation is offered in the video.
Motion Tracking Mode Calibration
Because the software does not know the position of the gun on startup, you will need to calibrate the turret when it is turned on. The python program will prompt you to use the keyboard to adjust the elevation and yaw angles of the turret, and confirm that it is leveled and centered.
After the turret position is calibrated, the software will then calibrate the exposure level of the camera and pick a reference image for motion detection. Because the motion detection works by comparing subsequent images to the initial reference image for changes, you will want to make sure there is nothing moving in front of the camera during its image calibration.
No calibration is necessary in interactive mode. Simply place the camera in line with the gun barrel and use the keyboard to move and fire your turret. Use the "a" and "d" keys to pivot left and right, and the "w" and "s" keys to angle the turret up and down. Press "Enter" to fire.
Congrats! You now have an impenetrable defense to guard against your adversaries. But remember that with great power comes great responsibility. Have fun and be safe!
Davis @ Hacker Shack