2022-04-08 21:20:53 -07:00
|
|
|
// Copyright (c) FIRST and other WPILib contributors.
|
|
|
|
|
// Open Source Software; you can modify and/or share it under the terms of
|
|
|
|
|
// the WPILib BSD license file in the root directory of this project.
|
|
|
|
|
|
|
|
|
|
package edu.wpi.first.math;
|
|
|
|
|
|
2022-10-26 22:20:08 -07:00
|
|
|
import edu.wpi.first.math.geometry.Pose3d;
|
2022-04-08 21:20:53 -07:00
|
|
|
import edu.wpi.first.math.geometry.Rotation2d;
|
2022-10-26 22:20:08 -07:00
|
|
|
import edu.wpi.first.math.geometry.Rotation3d;
|
|
|
|
|
import edu.wpi.first.math.geometry.Transform3d;
|
|
|
|
|
import edu.wpi.first.math.geometry.Translation3d;
|
2022-04-08 21:20:53 -07:00
|
|
|
import edu.wpi.first.math.util.Units;
|
|
|
|
|
import org.junit.jupiter.api.Assertions;
|
|
|
|
|
import org.junit.jupiter.api.Test;
|
|
|
|
|
|
|
|
|
|
class ComputerVisionUtilTest {
|
|
|
|
|
@Test
|
|
|
|
|
void testCalculateDistanceToTarget() {
|
2022-10-26 22:20:08 -07:00
|
|
|
var cameraHeight = 1;
|
2022-04-08 21:20:53 -07:00
|
|
|
var targetHeight = 3;
|
2022-10-26 22:20:08 -07:00
|
|
|
var cameraPitch = Units.degreesToRadians(0);
|
2022-04-08 21:20:53 -07:00
|
|
|
var targetPitch = Units.degreesToRadians(30);
|
2022-05-06 18:36:58 +03:00
|
|
|
var targetYaw = Units.degreesToRadians(0);
|
2022-04-08 21:20:53 -07:00
|
|
|
|
2022-10-26 22:20:08 -07:00
|
|
|
var distanceAlongGround =
|
|
|
|
|
ComputerVisionUtil.calculateDistanceToTarget(
|
|
|
|
|
cameraHeight, targetHeight, cameraPitch, targetPitch, targetYaw);
|
|
|
|
|
Assertions.assertEquals(3.464, distanceAlongGround, 0.01);
|
2022-04-08 21:20:53 -07:00
|
|
|
|
2022-10-26 22:20:08 -07:00
|
|
|
cameraHeight = 1;
|
2022-04-08 21:20:53 -07:00
|
|
|
targetHeight = 2;
|
2022-10-26 22:20:08 -07:00
|
|
|
cameraPitch = Units.degreesToRadians(20);
|
2022-04-08 21:20:53 -07:00
|
|
|
targetPitch = Units.degreesToRadians(-10);
|
|
|
|
|
|
2022-10-26 22:20:08 -07:00
|
|
|
distanceAlongGround =
|
2022-04-08 21:20:53 -07:00
|
|
|
ComputerVisionUtil.calculateDistanceToTarget(
|
2022-10-26 22:20:08 -07:00
|
|
|
cameraHeight, targetHeight, cameraPitch, targetPitch, targetYaw);
|
|
|
|
|
Assertions.assertEquals(5.671, distanceAlongGround, 0.01);
|
2022-04-08 21:20:53 -07:00
|
|
|
|
2022-10-26 22:20:08 -07:00
|
|
|
cameraHeight = 3;
|
2022-04-08 21:20:53 -07:00
|
|
|
targetHeight = 1;
|
2022-10-26 22:20:08 -07:00
|
|
|
cameraPitch = Units.degreesToRadians(0);
|
2022-04-08 21:20:53 -07:00
|
|
|
targetPitch = Units.degreesToRadians(-30);
|
|
|
|
|
|
2022-10-26 22:20:08 -07:00
|
|
|
distanceAlongGround =
|
2022-04-08 21:20:53 -07:00
|
|
|
ComputerVisionUtil.calculateDistanceToTarget(
|
2022-10-26 22:20:08 -07:00
|
|
|
cameraHeight, targetHeight, cameraPitch, targetPitch, targetYaw);
|
|
|
|
|
Assertions.assertEquals(3.464, distanceAlongGround, 0.01);
|
2022-05-06 18:36:58 +03:00
|
|
|
|
2022-10-26 22:20:08 -07:00
|
|
|
cameraHeight = 1;
|
2022-05-06 18:36:58 +03:00
|
|
|
targetHeight = 3;
|
2022-10-26 22:20:08 -07:00
|
|
|
cameraPitch = Units.degreesToRadians(0);
|
2022-05-06 18:36:58 +03:00
|
|
|
targetPitch = Units.degreesToRadians(30);
|
|
|
|
|
targetYaw = Units.degreesToRadians(30);
|
|
|
|
|
|
2022-10-26 22:20:08 -07:00
|
|
|
distanceAlongGround =
|
2022-05-06 18:36:58 +03:00
|
|
|
ComputerVisionUtil.calculateDistanceToTarget(
|
2022-10-26 22:20:08 -07:00
|
|
|
cameraHeight, targetHeight, cameraPitch, targetPitch, targetYaw);
|
|
|
|
|
Assertions.assertEquals(4, distanceAlongGround, 0.01);
|
2022-04-08 21:20:53 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
|
void testEstimateFieldToRobot() {
|
2022-10-26 22:20:08 -07:00
|
|
|
var cameraHeight = 1;
|
2022-04-08 21:20:53 -07:00
|
|
|
var targetHeight = 3;
|
2022-10-26 22:20:08 -07:00
|
|
|
var cameraPitch = 0;
|
2022-04-08 21:20:53 -07:00
|
|
|
var targetPitch = Units.degreesToRadians(30);
|
|
|
|
|
var targetYaw = new Rotation2d();
|
|
|
|
|
var gyroAngle = new Rotation2d();
|
2022-10-26 22:20:08 -07:00
|
|
|
var fieldToTarget = new Pose3d();
|
|
|
|
|
var cameraToRobot = new Transform3d();
|
2022-04-08 21:20:53 -07:00
|
|
|
|
2022-10-26 22:20:08 -07:00
|
|
|
var distanceAlongGround =
|
|
|
|
|
ComputerVisionUtil.calculateDistanceToTarget(
|
|
|
|
|
cameraHeight, targetHeight, cameraPitch, targetPitch, targetYaw.getRadians());
|
|
|
|
|
var range = Math.hypot(distanceAlongGround, targetHeight - cameraHeight);
|
2022-04-08 21:20:53 -07:00
|
|
|
var fieldToRobot =
|
|
|
|
|
ComputerVisionUtil.estimateFieldToRobot(
|
|
|
|
|
ComputerVisionUtil.estimateCameraToTarget(
|
2022-10-26 22:20:08 -07:00
|
|
|
new Translation3d(range, new Rotation3d(0.0, targetPitch, targetYaw.getRadians())),
|
2022-04-08 21:20:53 -07:00
|
|
|
fieldToTarget,
|
|
|
|
|
gyroAngle),
|
|
|
|
|
fieldToTarget,
|
|
|
|
|
cameraToRobot);
|
|
|
|
|
|
|
|
|
|
Assertions.assertEquals(-3.464, fieldToRobot.getX(), 0.1);
|
|
|
|
|
Assertions.assertEquals(0, fieldToRobot.getY(), 0.1);
|
2022-10-26 22:20:08 -07:00
|
|
|
Assertions.assertEquals(2.0, fieldToRobot.getZ(), 0.1);
|
|
|
|
|
Assertions.assertEquals(0, Units.radiansToDegrees(fieldToRobot.getRotation().getZ()), 0.1);
|
2022-04-08 21:20:53 -07:00
|
|
|
|
|
|
|
|
gyroAngle = Rotation2d.fromDegrees(-30);
|
|
|
|
|
|
2022-10-26 22:20:08 -07:00
|
|
|
distanceAlongGround =
|
|
|
|
|
ComputerVisionUtil.calculateDistanceToTarget(
|
|
|
|
|
cameraHeight, targetHeight, cameraPitch, targetPitch, targetYaw.getRadians());
|
|
|
|
|
range = Math.hypot(distanceAlongGround, targetHeight - cameraHeight);
|
2022-04-08 21:20:53 -07:00
|
|
|
fieldToRobot =
|
|
|
|
|
ComputerVisionUtil.estimateFieldToRobot(
|
|
|
|
|
ComputerVisionUtil.estimateCameraToTarget(
|
2022-10-26 22:20:08 -07:00
|
|
|
new Translation3d(range, new Rotation3d(0.0, targetPitch, targetYaw.getRadians())),
|
2022-04-08 21:20:53 -07:00
|
|
|
fieldToTarget,
|
|
|
|
|
gyroAngle),
|
|
|
|
|
fieldToTarget,
|
|
|
|
|
cameraToRobot);
|
|
|
|
|
|
|
|
|
|
Assertions.assertEquals(-3.0, fieldToRobot.getX(), 0.1);
|
|
|
|
|
Assertions.assertEquals(1.732, fieldToRobot.getY(), 0.1);
|
2022-10-26 22:20:08 -07:00
|
|
|
Assertions.assertEquals(2.0, fieldToRobot.getZ(), 0.1);
|
|
|
|
|
Assertions.assertEquals(-30.0, Units.radiansToDegrees(fieldToRobot.getRotation().getZ()), 0.1);
|
2022-04-08 21:20:53 -07:00
|
|
|
}
|
|
|
|
|
}
|