# Using limelight to change shooter speed based off distance

One thing I’d like to clarify before I begin, I am aware that there are a few posts that ask a similar question to what I’m asking. What I’ve gathered from those posts is that you need to create a lookup table with distance/speed then use linear interpolation to actually have your shooter speed change based off distance.

This is great and all but what I’m confused on is how exactly would I implement that in my code? How do I put a lookup table in code? How do I use interpolation in a command? What I’m basically looking for is some examples of these implementations. Anything would be appreciated

1 Like

Here is what I would do, but warning I’ve never actually done it myself before.

The previous posts (which you could have just replied to) said used a linear interpolation. So with the robot built and ready to implement, record the data of the limelight of the height of the target. Then shoot balls into the goal and keep changing the speed of the shooter until it is consistent. Then put both data points into an excel table where the input column would be the height of the target from the Limelight and the shooter speed is the output column. Repeat these steps until you have at least 5 rows. Then plot the data points in a scatter plot and add a linear interpolation with excel plotting the equation on the graph. That equation can then be used to take in the height of the goal and will output the speed at which the ball should be shot at. Where the x is the input and y is the output.

It will end up looking something like this:

I hope this helps. I am happy to answer any questions you may have.

6 Likes

You can look at the code of team 254 they have a custom library for this.

2 Likes

I can get to work on the excel graph right now, just one question; how come you use the height of the goal for the x axis? Like wouldn’t it stay the same? I was thinking that the x axis would be the distance from the limelight to the goal

Definitely will be checking it out

1 Like

Typically when calculating the distance to the goal you use the height of the goal in the image to covert it from y pixels to distance. So you can either use the calculated distance or directly the y pixel height.

1 Like

I used height of the goal as reference from this: Case Study: Estimating Distance — Limelight 1.0 documentation

You can use distance from the goal but how are you measuring that?

I have an estimate method that uses the d = (h2-h1) / tan(a1+a2) equation showed in the case study. I confirmed that the distance it returns is in fact how far the limelight is from the reflective tape on the top hub.

I see, I didnt know you could that

We have a class for LinearInterpolation based on a table

Then in our ShooterConstants we defined the array and Interpolator based on the Limelights Y angle of error.

``````public static final double[][] SHOOTER_SPEED_ARRAY = {
{9.6,3000},
{4,3080},
{3,3090},
{-0.3,3120},
{-0.6,3200},
{-3,3220},
{-4.6,3350},
{-6.3,3500},
{-8.6,3560},
{-9,3590},
{-13,3650},
{-14.7,3740},
{-17,3840},
{-19.1,3940}
};

public static final LinearInterpolator SHOOTER_SPEED_INTERPOLATOR = new LinearInterpolator(SHOOTER_SPEED_ARRAY);``````

Then you can use your vertical angle of error to set your motor speed based on the interpolator.

``````if (m_Vision.hasTarget()){
double vertical_angle = m_Vision.getVerticalAngleOfErrorDegrees();
m_shooter.setTargetVelocity(SHOOTER_SPEED_INTERPOLATOR.getInterpolatedValue(vertical_angle));
}``````

Of course your vision class and shooter may have different method names but I think you get the idea.

1 Like

I added this to wpilib a couple months ago:

1 Like

When you guys shoot using this method do you find the balls shot to be consistent? To be a bit more specific I have this problem where when I shoot 2 balls back to back the second one will lag behind. Whether this is natural or because the first ball slightly slows the shooter down I do not know. I’d be grateful if you could let me know how it works out for you guys

That problem has more to do with inertia than the targeting. The first ball going through slows down the wheels and you are feeding the second ball through before the wheels are up to speed. A well tuned feed forward and PD will help you recover faster. We also have code to make sure the motor is at the set speed within a tolerance before it feeds the ball and the next ball

1 Like

This is how my team did it this year.

2 Likes

I see, I think I understand how to bring the table over into the code now.

I figured it would have something to do with pid, but thank you for confirming

One other thing, if I have an secondary shooter motor used to decrease backspin do I also have to make a seperate chart and write seperate code to change its speed?

We used our second motor as a % of the first motor but 2 different tables will work better for allowing angle control. you can dig around our repo. What I posted wasn’t the full targeting code as our BaseTurnToTarget aimed our drivetrain and spun she shooter up. we do at speed checks before firing.

Because you will probably want to spin the two motors at a different speeds you’ll need different tables. In our code we have a shooter speeds (the flywheel motor) and cowling speeds (the backspin motor).

1 Like

Thank you guys, this has been very helpful. I’ll see if I can get to work on implementing all of this

You can also use this:
https://algs4.cs.princeton.edu/14analysis/PolynomialRegression.java.html

for a better fit

1 Like

One thing that just came into mind, the feedfoward gains you get for the shooter (whether it be manual or from sysid), is it for both the shooter and cowling motor? To be more specific, did you use seperate gains for each motor or for both of them as a whole?

1 Like