I have some code (Java) up and running that can detect AprilTags via a USB Camera connected to the RoboRIO.
However, I don’t really know how to determine the length from the camera to the AprilTag (preferably in inches/imperial because I’m American).
I wrote some code like this first:
public double determineDistance(double tagHeight, double cameraHeight, double cameraAngle) {
// convert angle to radians
double angleRadians = Math.toRadians(cameraAngle);
double distance = (tagHeight - cameraHeight) / Math.tan(angleRadians);
return distance;
}
But this wouldn’t work because if the camera moved back and forth, I would still get the same results from the method, as it’s still being fed the same params.
Looking back at some old code, I wrote a method that does this properly (even accounting for when the robot moves), for the LimeLight:
public double estimateDistance(double limelightMountAngleDegrees, double limelightLensHeightInches, double goalHeightInches) {
NetworkTable _table = NetworkTableInstance.getDefault().getTable("limelight");
NetworkTableEntry _ty = _table.getEntry("ty");
double targetOffsetAngle_Vertical = _ty.getDouble(0.0);
double angleToGoalDegrees = limelightMountAngleDegrees + targetOffsetAngle_Vertical;
double angleToGoalRadians = angleToGoalDegrees * (3.14159 / 180.0);
//calculate distance
double distanceFromLimelightToGoalInches = (goalHeightInches - limelightLensHeightInches)/Math.tan(angleToGoalRadians);
return distanceFromLimelightToGoalInches;
}
To my understanding, this code works because we’re able to get the vertical offset of the camera (LimeLight), and as the robot moves around, this offset changes, thus our distance changes.
But then, I don’t know how I can retrieve the vertical offset of a camera connected to the RoboRIO.
I’ve googled and googled, but don’t know a way to get the vertical offset of the camera.
Perhaps there are other, easier ways of doing this, or the answer is floating in front of my face, and I just can’t see it.
But right now I’m stumped.
Any help is greatly appreciated, manso