A few years ago, I saw a project that showcased a semi-autonomous turret that could fire on its own once aimed. That gave me the idea to use a Pixy 2 camera to acquire targets and then aim the nerf gun automatically, which could then lock on and fire all on its own.
For this project, the gun would need eyes, so I chose to use the Pixy 2 due to how easily it can interface with the mainboard. Then I needed a microcontroller, so I chose an Arduino Mega 2560 due to how many pins it has.
Since the gun needs two axes, yaw and pitch, it requires two stepper motors. Because of that, DFRobot sent me their dual DRV8825 motor driver board.
I began by loading up Fusion 360 and inserting an attached canvas of the nerf gun. Then I created a solid body from that canvas. After the gun was designed, I made a platform with a few bearing-based supports that would allow the gun to rotate left to right. I placed a stepper motor next to the rotating platform to drive it.
But the bigger question is how to make the gun pitch up and down. For that, a linear drive system with one point attached to the moveable block and another point at the back of the gun was needed. A rod would connect the two points, allowing the gun to pivot along its central axis.
Almost all the parts in my design are meant to be 3D printed, so I used my two printers to create them. Then I created the moveable platform by first using Fusion 360 to generate the necessary toolpaths for my CNC router, then I cut out the disk from a sheet of plywood.
After all the parts had been created, it was time to assemble them. I started by connecting the bearing supports to the rotating disk. Then I put together the linear pitch assembly by running the 6mm aluminum rods and the threaded rod through the pieces.
Lastly, I attached the nerf gun itself with a steel rod and two post made from aluminum extrusions.
Now for the most difficult part of the project: programming. A projectile-firing machine is very complex, and the math behind it can be confusing. I started by writing out the program flow and logic step-by-step, detailing what would happen at each machine state. The different states go as follows:
- Acquire target
- Position the gun
- Spool up the motors
- Fire the gun
- Wind down the motors
Acquiring the target involves first setting up the Pixy to track neon pink objects as targets. Then the gun moves until the target is centered in the Pixy’s view, where its distance from the gun barrel to the target is then measured. By using this distance, the horizontal and vertical distances can be found by using some basic trigonometric functions. My code has a function called get_angle() which uses these two distances to calculate how much of an angle is needed to hit that target.
The gun then moves to this position and turns on the motors via a MOSFET. After it has spooled up for five seconds it then moves the servo motor to pull the trigger. The MOSFET then switches the motor off and then the nerf gun returns to looking for targets.
Having FunI put a neon pink index card to the wall to test the gun’s accuracy. It did well, as my program calibrates and adjusts the angle for the measured distance. Here is a video demonstrating the gun working:
Comments