2020-12-26 14:12:05 -08: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.
|
2020-08-14 23:40:33 -07:00
|
|
|
|
|
|
|
|
#include <functional>
|
|
|
|
|
|
2023-08-28 15:13:34 -07:00
|
|
|
#include <Eigen/Eigenvalues>
|
|
|
|
|
#include <gtest/gtest.h>
|
|
|
|
|
|
2022-04-29 22:29:20 -07:00
|
|
|
#include "frc/EigenCore.h"
|
2020-08-14 23:40:33 -07:00
|
|
|
#include "frc/system/Discretization.h"
|
2021-01-06 21:40:25 -08:00
|
|
|
#include "frc/system/NumericalIntegration.h"
|
2020-08-14 23:40:33 -07:00
|
|
|
|
|
|
|
|
// Check that for a simple second-order system that we can easily analyze
|
|
|
|
|
// analytically,
|
|
|
|
|
TEST(DiscretizationTest, DiscretizeA) {
|
2022-04-29 22:29:20 -07:00
|
|
|
frc::Matrixd<2, 2> contA{{0, 1}, {0, 0}};
|
2020-08-14 23:40:33 -07:00
|
|
|
|
2022-04-29 22:29:20 -07:00
|
|
|
frc::Vectord<2> x0{1, 1};
|
|
|
|
|
frc::Matrixd<2, 2> discA;
|
2020-08-14 23:40:33 -07:00
|
|
|
|
|
|
|
|
frc::DiscretizeA<2>(contA, 1_s, &discA);
|
2022-04-29 22:29:20 -07:00
|
|
|
frc::Vectord<2> x1Discrete = discA * x0;
|
2020-08-14 23:40:33 -07:00
|
|
|
|
|
|
|
|
// We now have pos = vel = 1 and accel = 0, which should give us:
|
2022-04-29 22:29:20 -07:00
|
|
|
frc::Vectord<2> x1Truth{1.0 * x0(0) + 1.0 * x0(1), 0.0 * x0(0) + 1.0 * x0(1)};
|
2020-08-14 23:40:33 -07:00
|
|
|
|
|
|
|
|
EXPECT_EQ(x1Truth, x1Discrete);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Check that for a simple second-order system that we can easily analyze
|
|
|
|
|
// analytically,
|
|
|
|
|
TEST(DiscretizationTest, DiscretizeAB) {
|
2022-04-29 22:29:20 -07:00
|
|
|
frc::Matrixd<2, 2> contA{{0, 1}, {0, 0}};
|
|
|
|
|
frc::Matrixd<2, 1> contB{0, 1};
|
2020-08-14 23:40:33 -07:00
|
|
|
|
2022-04-29 22:29:20 -07:00
|
|
|
frc::Vectord<2> x0{1, 1};
|
|
|
|
|
frc::Vectord<1> u{1};
|
|
|
|
|
frc::Matrixd<2, 2> discA;
|
|
|
|
|
frc::Matrixd<2, 1> discB;
|
2020-08-14 23:40:33 -07:00
|
|
|
|
|
|
|
|
frc::DiscretizeAB<2, 1>(contA, contB, 1_s, &discA, &discB);
|
2022-04-29 22:29:20 -07:00
|
|
|
frc::Vectord<2> x1Discrete = discA * x0 + discB * u;
|
2020-08-14 23:40:33 -07:00
|
|
|
|
|
|
|
|
// We now have pos = vel = accel = 1, which should give us:
|
2022-04-29 22:29:20 -07:00
|
|
|
frc::Vectord<2> x1Truth{1.0 * x0(0) + 1.0 * x0(1) + 0.5 * u(0),
|
|
|
|
|
0.0 * x0(0) + 1.0 * x0(1) + 1.0 * u(0)};
|
2020-08-14 23:40:33 -07:00
|
|
|
|
|
|
|
|
EXPECT_EQ(x1Truth, x1Discrete);
|
|
|
|
|
}
|
|
|
|
|
|
2022-09-04 17:24:38 -07:00
|
|
|
// T
|
|
|
|
|
// Test that the discrete approximation of Q_d ≈ ∫ e^(Aτ) Q e^(Aᵀτ) dτ
|
|
|
|
|
// 0
|
2020-08-14 23:40:33 -07:00
|
|
|
TEST(DiscretizationTest, DiscretizeSlowModelAQ) {
|
2022-04-29 22:29:20 -07:00
|
|
|
frc::Matrixd<2, 2> contA{{0, 1}, {0, 0}};
|
|
|
|
|
frc::Matrixd<2, 2> contQ{{1, 0}, {0, 1}};
|
2020-08-14 23:40:33 -07:00
|
|
|
|
|
|
|
|
constexpr auto dt = 1_s;
|
|
|
|
|
|
2022-09-04 17:24:38 -07:00
|
|
|
// T
|
|
|
|
|
// Q_d ≈ ∫ e^(Aτ) Q e^(Aᵀτ) dτ
|
|
|
|
|
// 0
|
2024-11-07 23:46:52 -08:00
|
|
|
frc::Matrixd<2, 2> discQIntegrated =
|
|
|
|
|
frc::RKDP<std::function<frc::Matrixd<2, 2>(units::second_t,
|
|
|
|
|
const frc::Matrixd<2, 2>&)>,
|
|
|
|
|
frc::Matrixd<2, 2>>(
|
|
|
|
|
[&](units::second_t t, const frc::Matrixd<2, 2>&) {
|
|
|
|
|
return frc::Matrixd<2, 2>((contA * t.value()).exp() * contQ *
|
|
|
|
|
(contA.transpose() * t.value()).exp());
|
|
|
|
|
},
|
|
|
|
|
0_s, frc::Matrixd<2, 2>::Zero(), dt);
|
2020-08-14 23:40:33 -07:00
|
|
|
|
2022-04-29 22:29:20 -07:00
|
|
|
frc::Matrixd<2, 2> discA;
|
|
|
|
|
frc::Matrixd<2, 2> discQ;
|
2020-08-14 23:40:33 -07:00
|
|
|
frc::DiscretizeAQ<2>(contA, contQ, dt, &discA, &discQ);
|
|
|
|
|
|
|
|
|
|
EXPECT_LT((discQIntegrated - discQ).norm(), 1e-10)
|
|
|
|
|
<< "Expected these to be nearly equal:\ndiscQ:\n"
|
|
|
|
|
<< discQ << "\ndiscQIntegrated:\n"
|
|
|
|
|
<< discQIntegrated;
|
|
|
|
|
}
|
|
|
|
|
|
2022-09-04 17:24:38 -07:00
|
|
|
// T
|
|
|
|
|
// Test that the discrete approximation of Q_d ≈ ∫ e^(Aτ) Q e^(Aᵀτ) dτ
|
|
|
|
|
// 0
|
2020-08-14 23:40:33 -07:00
|
|
|
TEST(DiscretizationTest, DiscretizeFastModelAQ) {
|
2022-04-29 22:29:20 -07:00
|
|
|
frc::Matrixd<2, 2> contA{{0, 1}, {0, -1406.29}};
|
|
|
|
|
frc::Matrixd<2, 2> contQ{{0.0025, 0}, {0, 1}};
|
2020-08-14 23:40:33 -07:00
|
|
|
|
2021-07-25 07:42:59 -07:00
|
|
|
constexpr auto dt = 5_ms;
|
2020-08-14 23:40:33 -07:00
|
|
|
|
2022-09-04 17:24:38 -07:00
|
|
|
// T
|
|
|
|
|
// Q_d = ∫ e^(Aτ) Q e^(Aᵀτ) dτ
|
|
|
|
|
// 0
|
2024-11-07 23:46:52 -08:00
|
|
|
frc::Matrixd<2, 2> discQIntegrated =
|
|
|
|
|
frc::RKDP<std::function<frc::Matrixd<2, 2>(units::second_t,
|
|
|
|
|
const frc::Matrixd<2, 2>&)>,
|
|
|
|
|
frc::Matrixd<2, 2>>(
|
|
|
|
|
[&](units::second_t t, const frc::Matrixd<2, 2>&) {
|
|
|
|
|
return frc::Matrixd<2, 2>((contA * t.value()).exp() * contQ *
|
|
|
|
|
(contA.transpose() * t.value()).exp());
|
|
|
|
|
},
|
|
|
|
|
0_s, frc::Matrixd<2, 2>::Zero(), dt);
|
2020-08-14 23:40:33 -07:00
|
|
|
|
2022-04-29 22:29:20 -07:00
|
|
|
frc::Matrixd<2, 2> discA;
|
|
|
|
|
frc::Matrixd<2, 2> discQ;
|
2020-08-14 23:40:33 -07:00
|
|
|
frc::DiscretizeAQ<2>(contA, contQ, dt, &discA, &discQ);
|
|
|
|
|
|
|
|
|
|
EXPECT_LT((discQIntegrated - discQ).norm(), 1e-3)
|
|
|
|
|
<< "Expected these to be nearly equal:\ndiscQ:\n"
|
|
|
|
|
<< discQ << "\ndiscQIntegrated:\n"
|
|
|
|
|
<< discQIntegrated;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Test that DiscretizeR() works
|
|
|
|
|
TEST(DiscretizationTest, DiscretizeR) {
|
2022-04-29 22:29:20 -07:00
|
|
|
frc::Matrixd<2, 2> contR{{2.0, 0.0}, {0.0, 1.0}};
|
|
|
|
|
frc::Matrixd<2, 2> discRTruth{{4.0, 0.0}, {0.0, 2.0}};
|
2020-08-14 23:40:33 -07:00
|
|
|
|
2022-04-29 22:29:20 -07:00
|
|
|
frc::Matrixd<2, 2> discR = frc::DiscretizeR<2>(contR, 500_ms);
|
2020-08-14 23:40:33 -07:00
|
|
|
|
|
|
|
|
EXPECT_LT((discRTruth - discR).norm(), 1e-10)
|
|
|
|
|
<< "Expected these to be nearly equal:\ndiscR:\n"
|
|
|
|
|
<< discR << "\ndiscRTruth:\n"
|
|
|
|
|
<< discRTruth;
|
|
|
|
|
}
|