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.
|
|
|
|
|
|
|
|
|
|
#include "frc/ComputerVisionUtil.h"
|
|
|
|
|
#include "frc/geometry/Pose2d.h"
|
|
|
|
|
#include "frc/geometry/Rotation2d.h"
|
|
|
|
|
#include "frc/geometry/Transform2d.h"
|
|
|
|
|
#include "gtest/gtest.h"
|
|
|
|
|
#include "units/angle.h"
|
|
|
|
|
#include "units/length.h"
|
|
|
|
|
|
|
|
|
|
TEST(ComputerVisionUtilTest, CalculateDistanceToTarget) {
|
2022-10-26 22:20:08 -07:00
|
|
|
auto cameraHeight = 1_m;
|
2022-04-08 21:20:53 -07:00
|
|
|
auto targetHeight = 3_m;
|
2022-10-26 22:20:08 -07:00
|
|
|
auto cameraPitch = 0_deg;
|
2022-04-08 21:20:53 -07:00
|
|
|
auto targetPitch = 30_deg;
|
2022-05-06 18:36:58 +03:00
|
|
|
auto targetYaw = 0_deg;
|
2022-04-08 21:20:53 -07:00
|
|
|
|
2022-10-26 22:20:08 -07:00
|
|
|
auto distanceAlongGround = frc::CalculateDistanceToTarget(
|
|
|
|
|
cameraHeight, targetHeight, cameraPitch, targetPitch, targetYaw);
|
|
|
|
|
EXPECT_NEAR(3.464, distanceAlongGround.value(), 0.01);
|
2022-04-08 21:20:53 -07:00
|
|
|
|
2022-10-26 22:20:08 -07:00
|
|
|
cameraHeight = 1_m;
|
2022-04-08 21:20:53 -07:00
|
|
|
targetHeight = 2_m;
|
2022-10-26 22:20:08 -07:00
|
|
|
cameraPitch = 20_deg;
|
2022-04-08 21:20:53 -07:00
|
|
|
targetPitch = -10_deg;
|
|
|
|
|
|
2022-10-26 22:20:08 -07:00
|
|
|
distanceAlongGround = frc::CalculateDistanceToTarget(
|
|
|
|
|
cameraHeight, targetHeight, cameraPitch, targetPitch, targetYaw);
|
|
|
|
|
EXPECT_NEAR(5.671, distanceAlongGround.value(), 0.01);
|
2022-04-08 21:20:53 -07:00
|
|
|
|
2022-10-26 22:20:08 -07:00
|
|
|
cameraHeight = 3_m;
|
2022-04-08 21:20:53 -07:00
|
|
|
targetHeight = 1_m;
|
2022-10-26 22:20:08 -07:00
|
|
|
cameraPitch = 0_deg;
|
2022-04-08 21:20:53 -07:00
|
|
|
targetPitch = -30_deg;
|
|
|
|
|
|
2022-10-26 22:20:08 -07:00
|
|
|
distanceAlongGround = frc::CalculateDistanceToTarget(
|
|
|
|
|
cameraHeight, targetHeight, cameraPitch, targetPitch, targetYaw);
|
|
|
|
|
EXPECT_NEAR(3.464, distanceAlongGround.value(), 0.01);
|
2022-05-06 18:36:58 +03:00
|
|
|
|
2022-10-26 22:20:08 -07:00
|
|
|
cameraHeight = 1_m;
|
2022-05-06 18:36:58 +03:00
|
|
|
targetHeight = 3_m;
|
2022-10-26 22:20:08 -07:00
|
|
|
cameraPitch = 0_deg;
|
2022-05-06 18:36:58 +03:00
|
|
|
targetPitch = 30_deg;
|
|
|
|
|
targetYaw = 30_deg;
|
|
|
|
|
|
2022-10-26 22:20:08 -07:00
|
|
|
distanceAlongGround = frc::CalculateDistanceToTarget(
|
|
|
|
|
cameraHeight, targetHeight, cameraPitch, targetPitch, targetYaw);
|
|
|
|
|
EXPECT_NEAR(4, distanceAlongGround.value(), 0.01);
|
2022-04-08 21:20:53 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
TEST(ComputerVisionUtilTest, EstimateFieldToRobot) {
|
2022-10-26 22:20:08 -07:00
|
|
|
auto cameraHeight = 1_m;
|
2022-04-08 21:20:53 -07:00
|
|
|
auto targetHeight = 3_m;
|
2022-10-26 22:20:08 -07:00
|
|
|
auto cameraPitch = 0_deg;
|
2022-04-08 21:20:53 -07:00
|
|
|
auto targetPitch = 30_deg;
|
|
|
|
|
frc::Rotation2d targetYaw;
|
|
|
|
|
frc::Rotation2d gyroAngle;
|
2022-10-26 22:20:08 -07:00
|
|
|
frc::Pose3d fieldToTarget;
|
|
|
|
|
frc::Transform3d cameraToRobot;
|
2022-04-08 21:20:53 -07:00
|
|
|
|
2022-10-26 22:20:08 -07:00
|
|
|
auto distanceAlongGround =
|
|
|
|
|
frc::CalculateDistanceToTarget(cameraHeight, targetHeight, cameraPitch,
|
|
|
|
|
targetPitch, targetYaw.Radians());
|
|
|
|
|
auto range =
|
|
|
|
|
units::math::hypot(distanceAlongGround, targetHeight - cameraHeight);
|
2022-04-08 21:20:53 -07:00
|
|
|
auto fieldToRobot = frc::EstimateFieldToRobot(
|
|
|
|
|
frc::EstimateCameraToTarget(
|
2022-10-26 22:20:08 -07:00
|
|
|
frc::Translation3d{
|
|
|
|
|
range, frc::Rotation3d{0_rad, targetPitch, targetYaw.Radians()}},
|
2022-04-08 21:20:53 -07:00
|
|
|
fieldToTarget, gyroAngle),
|
|
|
|
|
fieldToTarget, cameraToRobot);
|
|
|
|
|
|
|
|
|
|
EXPECT_NEAR(-3.464, fieldToRobot.X().value(), 0.1);
|
|
|
|
|
EXPECT_NEAR(0, fieldToRobot.Y().value(), 0.1);
|
2022-10-26 22:20:08 -07:00
|
|
|
EXPECT_NEAR(2.0, fieldToRobot.Z().value(), 0.1);
|
|
|
|
|
EXPECT_NEAR(0, fieldToRobot.Rotation().Z().value(), 0.1);
|
2022-04-08 21:20:53 -07:00
|
|
|
|
|
|
|
|
gyroAngle = -30_deg;
|
|
|
|
|
|
2022-10-26 22:20:08 -07:00
|
|
|
distanceAlongGround =
|
|
|
|
|
frc::CalculateDistanceToTarget(cameraHeight, targetHeight, cameraPitch,
|
|
|
|
|
targetPitch, targetYaw.Radians());
|
|
|
|
|
range = units::math::hypot(distanceAlongGround, targetHeight - cameraHeight);
|
2022-04-08 21:20:53 -07:00
|
|
|
fieldToRobot = frc::EstimateFieldToRobot(
|
|
|
|
|
frc::EstimateCameraToTarget(
|
2022-10-26 22:20:08 -07:00
|
|
|
frc::Translation3d{
|
|
|
|
|
range, frc::Rotation3d{0_rad, targetPitch, targetYaw.Radians()}},
|
2022-04-08 21:20:53 -07:00
|
|
|
fieldToTarget, gyroAngle),
|
|
|
|
|
fieldToTarget, cameraToRobot);
|
|
|
|
|
|
|
|
|
|
EXPECT_NEAR(-3.0, fieldToRobot.X().value(), 0.1);
|
|
|
|
|
EXPECT_NEAR(1.732, fieldToRobot.Y().value(), 0.1);
|
2022-10-26 22:20:08 -07:00
|
|
|
EXPECT_NEAR(2.0, fieldToRobot.Z().value(), 0.1);
|
|
|
|
|
EXPECT_NEAR(-30.0, units::degree_t{fieldToRobot.Rotation().Z()}.value(), 0.1);
|
2022-04-08 21:20:53 -07:00
|
|
|
}
|