# Programming Deep Dive

This shorter deep-dive goes into greater detail about how the some of the code behind Slice’s shooter works.

## The Issue

As mentioned in our last code deep dive, Slice’s shooting code uses team 1757’s shooter trajectory calculator to calculate an optimal shot. However, this calculator only gives us the optimal velocity and launch angle of the note. It’s a more complicated process in order to convert the launch velocity into the rotational velocity of the flywheels.

The naive approach would be to assume the launch velocity is the speed that the outer surface of the flywheel is moving at. The below equation shows how to convert from a linear velocity to the rotational velocity of the flywheel.

This equation is theoretically perfect, but realistically the flywheels are not magic devices that instantly accelerate the note to precisely the correct speed. Both inertia and a lack of friction cause the note to accelerate to a slower speed that the above equation assumes, so this will always undershoot.

## The Solution

There comes a point where the system becomes so complicated that it isn’t worth doing the physics equations to solve it. Instead of trying to come up with a complex model of the acceleration of the note inside the shooter, Slice is instead opting to use empirical data. Here’s the process:

Firstly, we are using the equation above to predict the correct rotational velocity of the flywheels.As previously stated, this will always undershoot, but afterwards we manually adjust it until we make the shot consistently. From this we collect two data points: the original velocity of the flywheels, unadjusted, and the actual speed that made the perfect shot after adjustment. By repeating this for a few distances, we can create a plot. Here’s an example of what it might look like.

After collecting data, we created a function to predict the actual speed given the theoretical velocity. From a glance, it looks like this example data fits a quadratic function, so by typing the equation for one into desmos, it will automatically find the constants a, b, and c for a parabola that best fits the data.:

Of course, it’s possible to use any function, but polynomials are usually easiest to work with. If you are using desmos, you need to load all your data into a table by pressing the + button in the corner, and when typing your equation you should use the tilde (~) sign instead of the equals (=) sign in order to get it to make a prediction.

Note the R2 variable directly below the equation. That represents how well the function fits the data: the closer to 1, the better. After finding a function with a reasonably high R2, we can plug it into the code and test it out to ensure it works.

(Beware, just because a function has an R2 of 1 doesn’t mean it’s perfect, in fact an R2 of 1 is usually too good to be true, because that means the function is “overfitting” and basically guessing anywhere other than where it was tested. For polynomial function this can be easily prevented by using a polynomial that is at least 2 degrees less than the number of data points collected)

A benefit of this approach is that it not only accounts for the imperfect acceleration of the note, but is also able to compensate for air resistance, thus encapsulating numerous complex physics equations into a single, simpler function.