2022-04-30 22:54:22 -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.
|
|
|
|
|
|
2023-08-28 15:13:34 -07:00
|
|
|
#include <gtest/gtest.h>
|
|
|
|
|
|
2025-11-07 19:56:21 -05:00
|
|
|
#include "wpi/math/controller/LTVUnicycleController.hpp"
|
|
|
|
|
#include "wpi/math/trajectory/TrajectoryGenerator.hpp"
|
2025-11-07 19:57:55 -05:00
|
|
|
#include "wpi/math/util/MathUtil.hpp"
|
2025-11-07 19:56:21 -05:00
|
|
|
#include "wpi/units/math.hpp"
|
2022-04-30 22:54:22 -07:00
|
|
|
|
|
|
|
|
#define EXPECT_NEAR_UNITS(val1, val2, eps) \
|
2025-11-07 20:00:05 -05:00
|
|
|
EXPECT_LE(wpi::units::math::abs(val1 - val2), eps)
|
2022-04-30 22:54:22 -07:00
|
|
|
|
2025-11-07 20:00:05 -05:00
|
|
|
static constexpr wpi::units::meter_t kTolerance{1 / 12.0};
|
|
|
|
|
static constexpr wpi::units::radian_t kAngularTolerance{2.0 * std::numbers::pi /
|
2022-04-30 22:54:22 -07:00
|
|
|
180.0};
|
|
|
|
|
|
|
|
|
|
TEST(LTVUnicycleControllerTest, ReachesReference) {
|
2025-11-07 20:00:05 -05:00
|
|
|
constexpr wpi::units::second_t kDt = 20_ms;
|
2022-04-30 22:54:22 -07:00
|
|
|
|
2025-11-07 20:00:05 -05:00
|
|
|
wpi::math::LTVUnicycleController controller{{0.0625, 0.125, 2.5}, {4.0, 4.0}, kDt};
|
|
|
|
|
wpi::math::Pose2d robotPose{2.7_m, 23_m, 0_deg};
|
2022-04-30 22:54:22 -07:00
|
|
|
|
2025-11-07 20:00:05 -05:00
|
|
|
auto waypoints = std::vector{wpi::math::Pose2d{2.75_m, 22.521_m, 0_rad},
|
|
|
|
|
wpi::math::Pose2d{24.73_m, 19.68_m, 5.846_rad}};
|
|
|
|
|
auto trajectory = wpi::math::TrajectoryGenerator::GenerateTrajectory(
|
2022-04-30 22:54:22 -07:00
|
|
|
waypoints, {8.8_mps, 0.1_mps_sq});
|
|
|
|
|
|
|
|
|
|
auto totalTime = trajectory.TotalTime();
|
|
|
|
|
for (size_t i = 0; i < (totalTime / kDt).value(); ++i) {
|
|
|
|
|
auto state = trajectory.Sample(kDt * i);
|
|
|
|
|
auto [vx, vy, omega] = controller.Calculate(robotPose, state);
|
|
|
|
|
static_cast<void>(vy);
|
|
|
|
|
|
2025-11-07 20:00:05 -05:00
|
|
|
robotPose = robotPose + wpi::math::Twist2d{vx * kDt, 0_m, omega * kDt}.Exp();
|
2022-04-30 22:54:22 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
auto& endPose = trajectory.States().back().pose;
|
|
|
|
|
EXPECT_NEAR_UNITS(endPose.X(), robotPose.X(), kTolerance);
|
|
|
|
|
EXPECT_NEAR_UNITS(endPose.Y(), robotPose.Y(), kTolerance);
|
2025-11-07 20:00:05 -05:00
|
|
|
EXPECT_NEAR_UNITS(wpi::math::AngleModulus(endPose.Rotation().Radians() -
|
2022-04-30 22:54:22 -07:00
|
|
|
robotPose.Rotation().Radians()),
|
|
|
|
|
0_rad, kAngularTolerance);
|
|
|
|
|
}
|