[wpimath] Remove broken and obsoleted ComputerVisionUtil functions (#4775)

The ComputerVisionUtil class was added before AprilTag support was
announced. Now that it has, the functions for estimating a pose from
range and yaw are no longer needed; it's just better to get the pose
directly from the AprilTag.

The coordinate system on some function arguments was confusing or didn't
match the NWU convention the rest of the library uses. It's easier to
remove the functions now and add them back after they're fixed since the
fixes aren't trivial.

The range function was removed because it uses pitch and yaw in the
camera's spherical coordinate system, which is obsoleted by AprilTags.
AprilTags give you a 6DOF pose directly, so range can be obtained via
Pose2d.getTranslation().getDistance().

Fixes #4757.
This commit is contained in:
Tyler Veness
2022-12-06 21:11:27 -08:00
committed by GitHub
parent 2d0faecf4f
commit f18fd41ac3
5 changed files with 66 additions and 466 deletions

View File

@@ -4,108 +4,30 @@
package edu.wpi.first.math;
import static org.junit.jupiter.api.Assertions.assertEquals;
import edu.wpi.first.math.geometry.Pose3d;
import edu.wpi.first.math.geometry.Rotation2d;
import edu.wpi.first.math.geometry.Rotation3d;
import edu.wpi.first.math.geometry.Transform3d;
import edu.wpi.first.math.geometry.Translation3d;
import edu.wpi.first.math.util.Units;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
class ComputerVisionUtilTest {
@Test
void testCalculateDistanceToTarget() {
var cameraHeight = 1;
var targetHeight = 3;
var cameraPitch = Units.degreesToRadians(0);
var targetPitch = Units.degreesToRadians(30);
var targetYaw = Units.degreesToRadians(0);
void testObjectToRobotPose() {
var robot = new Pose3d(1.0, 2.0, 0.0, new Rotation3d(0.0, 0.0, Units.degreesToRadians(30.0)));
var cameraToObject =
new Transform3d(
new Translation3d(1.0, 1.0, 1.0),
new Rotation3d(0.0, Units.degreesToRadians(-20.0), Units.degreesToRadians(45.0)));
var robotToCamera =
new Transform3d(
new Translation3d(1.0, 0.0, 2.0),
new Rotation3d(0.0, 0.0, Units.degreesToRadians(25.0)));
Pose3d object = robot.plus(robotToCamera).plus(cameraToObject);
var distanceAlongGround =
ComputerVisionUtil.calculateDistanceToTarget(
cameraHeight, targetHeight, cameraPitch, targetPitch, targetYaw);
Assertions.assertEquals(3.464, distanceAlongGround, 0.01);
cameraHeight = 1;
targetHeight = 2;
cameraPitch = Units.degreesToRadians(20);
targetPitch = Units.degreesToRadians(-10);
distanceAlongGround =
ComputerVisionUtil.calculateDistanceToTarget(
cameraHeight, targetHeight, cameraPitch, targetPitch, targetYaw);
Assertions.assertEquals(5.671, distanceAlongGround, 0.01);
cameraHeight = 3;
targetHeight = 1;
cameraPitch = Units.degreesToRadians(0);
targetPitch = Units.degreesToRadians(-30);
distanceAlongGround =
ComputerVisionUtil.calculateDistanceToTarget(
cameraHeight, targetHeight, cameraPitch, targetPitch, targetYaw);
Assertions.assertEquals(3.464, distanceAlongGround, 0.01);
cameraHeight = 1;
targetHeight = 3;
cameraPitch = Units.degreesToRadians(0);
targetPitch = Units.degreesToRadians(30);
targetYaw = Units.degreesToRadians(30);
distanceAlongGround =
ComputerVisionUtil.calculateDistanceToTarget(
cameraHeight, targetHeight, cameraPitch, targetPitch, targetYaw);
Assertions.assertEquals(4, distanceAlongGround, 0.01);
}
@Test
void testEstimateFieldToRobot() {
var cameraHeight = 1;
var targetHeight = 3;
var cameraPitch = 0;
var targetPitch = Units.degreesToRadians(30);
var targetYaw = new Rotation2d();
var gyroAngle = new Rotation2d();
var fieldToTarget = new Pose3d();
var cameraToRobot = new Transform3d();
var distanceAlongGround =
ComputerVisionUtil.calculateDistanceToTarget(
cameraHeight, targetHeight, cameraPitch, targetPitch, targetYaw.getRadians());
var range = Math.hypot(distanceAlongGround, targetHeight - cameraHeight);
var fieldToRobot =
ComputerVisionUtil.estimateFieldToRobot(
ComputerVisionUtil.estimateCameraToTarget(
new Translation3d(range, new Rotation3d(0.0, targetPitch, targetYaw.getRadians())),
fieldToTarget,
gyroAngle),
fieldToTarget,
cameraToRobot);
Assertions.assertEquals(-3.464, fieldToRobot.getX(), 0.1);
Assertions.assertEquals(0, fieldToRobot.getY(), 0.1);
Assertions.assertEquals(2.0, fieldToRobot.getZ(), 0.1);
Assertions.assertEquals(0, Units.radiansToDegrees(fieldToRobot.getRotation().getZ()), 0.1);
gyroAngle = Rotation2d.fromDegrees(-30);
distanceAlongGround =
ComputerVisionUtil.calculateDistanceToTarget(
cameraHeight, targetHeight, cameraPitch, targetPitch, targetYaw.getRadians());
range = Math.hypot(distanceAlongGround, targetHeight - cameraHeight);
fieldToRobot =
ComputerVisionUtil.estimateFieldToRobot(
ComputerVisionUtil.estimateCameraToTarget(
new Translation3d(range, new Rotation3d(0.0, targetPitch, targetYaw.getRadians())),
fieldToTarget,
gyroAngle),
fieldToTarget,
cameraToRobot);
Assertions.assertEquals(-3.0, fieldToRobot.getX(), 0.1);
Assertions.assertEquals(1.732, fieldToRobot.getY(), 0.1);
Assertions.assertEquals(2.0, fieldToRobot.getZ(), 0.1);
Assertions.assertEquals(-30.0, Units.radiansToDegrees(fieldToRobot.getRotation().getZ()), 0.1);
assertEquals(
robot, ComputerVisionUtil.objectToRobotPose(object, cameraToObject, robotToCamera));
}
}