Hidden Links


Auto-Raning Nerf Gun

Final Project: Nerf Launcher (Range Compensating Sniper Revolver Mortar Ballistic Nerf Gun) for ME 405 Mechatronics

by Stefan Laengle (ME), Roeurn Tourn (CPE), Chris Nguyen (EE)



The Nerf Launcher is designed to compensate for the error of human judgment. By turning an encoded motor to a certain position our Nerf gun will adjust its angle to hit the user’s intended target. This is an automated process that operates under closed loop control to adjust for the change in the dart’s trajectory due to gravity. A sonar sensor is implemented to measure the distance from the target, and a convertor computes the angle of elevation according to the sensor’s distance. The software is programmed in C++ by utilizing interrupts, classes, tasks, and several state diagrams to compute the necessary calculations and to control the system on an autonomous level. We realized a laser range sensor would probably be more efficient than the ultrasound we used, and more expensive sources of technology would help increase the precision of our project.

1.0 Introduction

When approaching the final project we considered all of the skills we have learned in our

previous experiments and how to implement them into a unique design. This document discusses the hardware and software development required to autonomously control a Nerf gun via an ME405 microcontroller. The microcontroller used in for this project was the ATmega128A chip, which is a high performance low power 8-bit Amtel product that we programmed in C++ using the AVR libc compiler.

The basis of our focuses on the accuracy of a Nerf gunshot by ensuring that our Nerf gun hits a user defined target precisely by using an ultrasonic range sensor. In our perspective a single solid hit is much more effective then the many inaccurate hits of an automatic Nerf gun, making our design simulate a (6-shot) rifle. The position sensor measures the distance from the Nerf gun to the target and calculates the required firing angle, which eliminates some of the approximations that a normal user must consider.

Considering the fact that the main costumers for normal Nerf guns are mothers of young boys from the ages of 5 to 55 years old, we felt like that they would main customers for this project as well. For these costumers our final retail product would have to be made more child proof, so that it is more compact and durable, and harder for kids to take apart. Another potential costumer would be a weapons specialist from the US army, affiliated with the government or military. Traditional motors and sniper weapons have a mechanical sighting system that requires the used to estimate the distance to the target. For these costumers we would put emphasis on the accuracy of the device. Our Nerf gun would most likely transform into a more harmful tool used as a weapon in the military. However, such weapons are already in development and it’s not the goal of this project to advance that research. Another possibility would be to using our project towards some sort of projectile device for the government, such as a satellite launcher. We chose to use a range sensor to learn how to implement a tool in a similar manner that we interfaced and calibrated the motors to the project board, and because the use of sensors is important in many current applications.

*All of the pin out and register references determined in this and previous relevant experiments according to the ATmega128 chip where found via Amtel Documentation online.

2.0 Specifications

Design and implement a Nerf gun that guides the target position and fires using a closed control feedback loop. The gun must have some sort of automatic response, and the position must be controlled by motor(s).
The max target distance is 10 meters of the Nerf gun, with an expected accuracy of 90% for hitting targets within 1 foot of the red target laser. The max angle of our Nerf gun should theoretically be 45° to reach max distance. The gun should also be a portable handheld device that is self-adjusting in accordance with the range in order to hit the target exactly. Another goal was to implement the function in real time using scheduling methods. 



3.0 Design Development

 The design consisted of a reconstructive hardware and software approach. The intention was to redirect hardware configurations explored in previous experiments with encoders and motors, reuse the methods used in previous experiments for software, and to implement the new position sensor both mechanically and in software to complete the project.

3.1 Hardware Design

The hardware approach to designing this project consisted of several hardware tests to calculate and confirm the characteristics of our Nerf gun and our position sensor. We also machined a mechanical design with the intention to keep our Nerf gun compact, light weight, and ready for practical use in the field (lab of engineers). 

3.1.1 Initial Velocity Calculation 

After purchasing our Nerf gun the first task we had to complete was measuring the average projectile velocity. We did this by measuring the distance traveled by the projectile and the time that it takes for the Nerf dart to traverse that distance. First the Nerf gun is placed at a known height (h) as close to horizontal as possible. By directing the initial velocity of the Nerf projectile in a horizontal (positive x) direction, the y component of velocity can be neglected, simplifying the standard ballistics equation below.


 Three tests at varying heights allowed us to calculate 3 average velocities, which gave us a total average velocity of about 12m/s. However we did recognize a trend for the average velocity to increase with distance. The progressive increase in calculated average velocity is most likely due to a slight down angle in the placement of the Nerf gun, causing an initial downward velocity. To fully correct for this requires a direct velocity measurement of the Nerf projectile or a retest with the Nerf weapon pointed exactly at 0 degrees to the horizon and zeroed out with a leveler. For our purposes we decided to use the 12m/s for our further calculations.

3.1.2 Ballistics Test Data

The Nerf weapon was placed and fired at 0 degrees with respect to the horizon. Three test were completed at heights (h), with its position marked for subsequent test. These marking ensured that subsequent test could be repeated with a tolerance of ±5mm in the positive x direction. The weapon was then fired, and the Nerf bullet strikes were marked at distance (d) from the weapon. The following is the data from our test.


Test 1 

h = 34.9cm 

d = 264cm, 274cm, 274cm, 284cm, 292cm 

AVG d = 277cm 

Calculated fall time: 266ms 

Calculated average velocity: 10.4m/s


Test 2 

h = 49.5cm 

d = 376cm, 378cm, 381cm, 381cm, 386cm, 391cm 

AVG d = 381cm 

Calculated fall time: 317ms 

Calculated average velocity: 12.02m/s


Test 3 

h = 69.2cm 

d = 470cm, 475cm, 488cm, 486cm, 493cm, 519cm, 531cm, 533cm, 532cm, 535cm, 521cm, 510cm, 525cm 

AVG d = 509.5cm 

Calculated fall time: 374.7ms 

Calculated average velocity: 13.59m/s 

Note these calculations do not compensate for drag, thus the calculated average velocity is increasing for greater distance. The population mean is used for all futher calcuations.

3.1.3 Distance Measurements and Calibration Test

 Conducting calibration test requires a direct distance measurement and mapping with the ADC voltage output. A test station was created by mounting the sensor onto a wooden board, powering the sensor via a spiced USB cable, and recording the analog voltage value read across a multi-meter. The entire station was then placed on a chair in a large open room, and a series of distance and voltage measurements were taken until the output voltage reached a plateau at 2.48V, which mapped to approximately 640 centimeters. This is defined as the max target distance that our Nerf launcher can operate at. 




For each sensor voltage output recorded, that same voltage was inputted on to the ADC on board record the digital output. This test allowed us to accurately map distance to voltage, and then to ADC output. This data was used to extrapolate a formula for actual sensor distance to target (centimeters) from ADC output.

3.1.4 Mechanical Construction

 Our design consists of a wood and metal built chassis, a hand held Nerf gun, and a position sensor. The position sensor is mounted directly onto the chassis parallel to the gun’s aiming direction. The Nerf gun is mounted with a pivot point located at the base of the barrel by the trigger. Using a motor and a drill screw the Nerf gun is angled from the horizontal base of the chassis at an angle predetermined from the distance from the target reported from the position sensor. (We assume the user is holding the Nerf launcher at a horizontal grad with the Nerf gun on top)





3.2 Software Design

Our software design relied heavily on effective use of our previous experiments so that we could focus on implementing the new functions successfully. We relied on the libraries used in previous experiments that where provided in the lib file, and also attempted an extension with the math.h library file for our convertor math. Our project also required the use of interrupts in a similar manner to Experiment 6 so that we could read the data from the encoder motor used in that experiment. The task/timing table is shown below


2.1.1            READ ULTRA SOUND     

The ADC distance reading method that controls the position sensor system is located in the avr_adc class, and is defined as read_ultra_sound.  The ADC’s read_once function is called multiple times and an average of the readings is made. Using the calibration equations, this average is multiplied by 33, and then divided by 92. The final return value is divided 100 to return centimeters.  


 3.2.2 Peak Detection (un-implemented code)

The current system takes 100 samples and computes a simple average. A filtering method was written, but due to time constrains this method could not be finalized. The code is still part of avr_adc.cpp, commented out under the read_ultraSound() method. The filter created an int array Readings_count[60]. Each element would represent the area between the weapon and the maxi- mum readable distance. Each reading from the ultrasound would increment one of the array ele- ments depending on its distance. Essentially each ultrasound ping would “vote” for a distance, and in the end the winner or the runner up would be returned to the user.

The majority of this code was written before our team determined that the maxim range of the ultrasound was 6.4 meters. Our team expected two major peaks, the primary target, and the wall behind the target. The peak detector ran several passes over the data converting it as it went. The first pass would store data in the array, Readings_count, this array would indicate the raw(vote count) information. The following chart shows a sample of our expected values.

 The second pass would write to an array called deriv_Readings_count which would take two adjacent read_ultraSound readings and subtract them, this would produce a first derivative of our readings. A third pass would increment peaks[] when ever it detects a transition from positive to negative. The next few passes would filter the peaks for the two highest and return the second highest peak.


3.2.3 Closed Loop Control

The closed loop control is implemented in task_motor.cpp. It controls the interface between the motors and position sensor. This file calls the functions of the read_motor class for the motor_two object to maintain control with the motor encoder. The state diagram for the motor control is shown on the next page.


 Figure 10 Motor Control State Diagram

 3.2.4 Convertor 

The math module is in a class defined as convertor.h, and its functionality is to take the distance in centimeters from the position sensor and return the desired angle the Nerf gun should be adjusted to in order to hit the target. The angle is expressed as the number of ticks the motor that controls the angle should be set to. The force of gravity is taken into consideration for these calculations. The state diagram for the Convertor is shown below.


 3.2.4 Convertor (Top) 

To calculate the math needed to convert the distance read in cm to number of ticks for the motor to set a certain angle we first derived the set of equations found in the Appendix 4.1 Con- vertor Calculations. These equations where derived according to the lengths defined in the following diagram.



These lengths where then realized into our calculation function as shown below in the fol- lowing code. Unfortunately this code didn’t seem to work, even after we ensured that we where including the math.h file necessary for the trigonometric computations. Our error message seemed to refer to some sort of pre-allocated memory, and didn’t want to compute any trig func- tions that had variables in their parameters. In conclusion for this module we ended up simply computing all of the values in an Excel spreadsheet and exporting those values into a 500 charac- ter LUT yielding values for every 1mm of precision. Although the LUT is easier for the processor to compute, is less accurate than the equation method would be. Since the goal for this experi- ment is to strive for accuracy we decided to sacrifice the memory and code space of a large LUT for what little precision we could still strive for.


 3.2.6 User Interface

 The user interface for our project is designed for simplicity. After initialized the terminal simply remains idle unless the motor encoder receives a signal. This trips the interrupts to allow a print on the screen displaying the distance to the specified target and the current angle of the Nerf gun. The state diagram for the user interface is shown below.

4.0 Results and Conclusions

Overall we feel like our final project was a success. To test the Nerf launcher we fired the gun from several different distances away from a user defined target specified by the red laser mounted to the front of the gun parallel to the sensor’s measurement path. The gun seemed to work reasonable well, in that the Nerf shaft rose when the sensor’s red laser seemed to be far away, and the shaft lowered when the red laser appeared close. We found of noise and mechanical vibrations in the final product, but we were pleased with the functionality of the closed loop control because the angle calibration seemed to work completely independently just fine, and quickly as well. As it was our first time working with the position sensor we ran into a few bumps in our filtering design. This led us to realize that the decision of what type of sensor we purchase, and its precision level, is very important in the overall design. If we strive for a millimeter of precision with our code but receive a jumble of inputs that are centimeters off from each other, than the average of the sensor inputs is a pretty inaccurate estimation to base our work off of. With more time to implement the trig functions into our convertor method we would have had slightly more accuracy as well, and then we would be able to pursue other aspects to increase our accuracy such as drag, or we would have been able to alter our closed control code as well as the closed control hardware configuration to begin to eliminate noise mechanical disturbances. It seems there are always more this to work on or to implement in a project, but as far as this class is concerned we feel that we learned an enormous amount throughout the experiments and in approximately 4 weeks we conjured up an autonomously functional Nerf launcher that runs under closed loop control. More importantly we experienced some of the many obstacles that lie along the road of design and implementation of any project, which benefits us as engineers in the long run. Our design was created to eliminate the human error found in the approximation in angling of projectile launchers, and we created a system that executes automated closed loop control successfully to an acceptable degree of accuracy. That is satisfactory.

4.1 Discussion of Target Specifications

 When testing the closed loop control we found very good responses by implemented it in a linear fashion. To calculate the target distance that the motor needed to turn the drill bit in ticks we set the amplification constant to half of the amplitude where the system gets unstable. Due to the limited range of the ultra-sonic device, it was only possible to measure the distance up to 5 meters. This set our new max angle of the Nerf gun to approximately 25°. Unfortunately we weren’t able to implement the real time mode of scheduling by using the run functions, but we were able to complete the main task of closed loop control.

4.2 Possible Future Upgrades

Battery pack with mounted circuit board and microcontroller for fully portable utilization(similar to a cordless power drill) 

Implementation of laser range sensor(as opposed to our ultra sound that produced a cone of sounds and listened for the echoes) 

Increase Nerf power (alternate dart weights, aerodynamic darts, gunpowder...) 

Level sensor to measure angle gun is being held to horizontal (to earth's surface) for much more flexibility of our user target angles 

Consideration of wind resistance (drag) 

Automatic reloading action 

Upgrade to more efficient position sensor for multiple target types(soft/solid/different colors) 

Implementation into a efficient and precise sniper rifle