Estimate Distance with Limelighy

Hey guys, I need help with a code I’m developing to estimate distance using Limelight vision. In my code, I included the LimelightHelpers library and used the formula (AprilTag Height - Camera Height) / tan((Camera Angle + (Limelight Ty)) * (PI/180)), where the camera angle is in degrees and heights are in centimeters, I tried in inches, but it didn’t even come close to working. I achieved a difference of 0.2cm from the ideal, however, when I increase or decrease this distance, this difference increases exponentially, more than 30 times that. If you can help me, I would greatly appreciate it. Thanks guys.

1 Like

A good place to start: make sure your limelight’s mounted angle (“Camera Angle”) and height (“Camera Height”) are as precise as possible. Any error can have a huge impact on the trigonometry as you increase distance.

Afaik, the formula you showed will only output the x distance.
For the xy distance, you should do:
HighDifference/tan(CameraAngle + ty)/cos(tx)

1 Like

We had the same problem (just not as bad). Recalibrating the camera (the camera, not the pipeline) did help little, and the rest was playing with the camera angle and height until we achieved a “good enough” result. We don’t know what the root cause of the problem was but with those steps, we got close enough.

we used this function to get xy distance:
deltaHight = tagHight - cammeraHight;
deltaAngle = getTy() + cammeraAngle;
return deltaHight / Math.tan(Math.toRadians(deltaAngle));

this function is what is written in limelight docs.

if the trig isnt giving you accurate results you could try the approach 2056 uses (i cant remember which chief post they talk about it).

basically do a linear interpolation map between ty and distance. place the robot at known distances in small (~6inch) increments, and record the reported ty and known distance in the interpolation map

edit: the chief post from 2056:

This summary of your intention is exactly right and matches the LL documentation and one of the posts below. If you post your actual code, we could check that you entered it right.

Assuming there are no typos in the code then likely the camera angle you entered into the LL has the sign flipped. You should verify the camera angle is correctly entered into the LL by moving the camera up and down in front of the target and see what LL reports for the Ty angle. Verify those numbers make sense. Also vary the pitch angle of the camera to help verify the sign is correct. If the camera is in your hand, it takes about 30 seconds to move the camera up and down and tilt the camera to point up and down and verify the LL numbers are reported correctly.

The units of length are irrelevant but must be identical; they yield the same d, distance, in the same units as the h1 and h2 units.

One post mentions an xy distance. That usage is highly unusual and I doubt you want that but if that’s what you really wanted then a different formula must be used.