blob: 14b49523986942c5a0418fa593f1b2de48277820 [file] [log] [blame]
James Kuszmaul59a5c612019-01-22 07:56:08 -08001#include "frc971/control_loops/c2d.h"
2
3#include <functional>
4
James Kuszmaul59a5c612019-01-22 07:56:08 -08005#include "gtest/gtest.h"
6
Philipp Schrader790cb542023-07-05 21:06:52 -07007#include "frc971/control_loops/runge_kutta.h"
8
James Kuszmaul59a5c612019-01-22 07:56:08 -08009namespace frc971 {
10namespace controls {
11namespace testing {
12
13class C2DTest : public ::testing::Test {
14 public:
15 C2DTest() {
16 // Create a trivial second-order system.
17 A_continuous << 0, 1, 0, 0;
18 B_continuous << 0, 1;
19 Q_continuous << 1, 0, 0, 1;
20 }
21
22 protected:
23 Eigen::Matrix<double, 2, 2> A_continuous;
24 Eigen::Matrix<double, 2, 1> B_continuous;
25 Eigen::Matrix<double, 2, 2> Q_continuous;
26};
27
28// Check that for a simple second-order system that we can easily analyze
29// analytically, C2D creates valid A/B matrices.
30TEST_F(C2DTest, DiscretizeAB) {
31 Eigen::Matrix<double, 2, 1> X0;
32 X0 << 1, 1;
33 Eigen::Matrix<double, 1, 1> U;
34 U << 1;
35 Eigen::Matrix<double, 2, 2> A_d;
36 Eigen::Matrix<double, 2, 1> B_d;
37
38 C2D(A_continuous, B_continuous, ::std::chrono::seconds(1), &A_d, &B_d);
39 Eigen::Matrix<double, 2, 1> X1_discrete = A_d * X0 + B_d * U;
40 // We now have pos = vel = accel = 1, which should give us:
41 Eigen::Matrix<double, 2, 1> X1_truth;
42 X1_truth(1, 0) = X0(1, 0) + 1.0 * U(0, 0);
43 X1_truth(0, 0) = X0(0, 0) + 1.0 * X0(1, 0) + 0.5 * U(0, 0);
44 EXPECT_EQ(X1_truth, X1_discrete);
45}
46
47// Test that the discrete approximation of Q is roughly equal to
48// integral from 0 to dt of e^(A tau) Q e^(A.T tau) dtau
49TEST_F(C2DTest, DiscretizeQ) {
50 Eigen::Matrix<double, 2, 2> Q_d;
51 const auto dt = ::std::chrono::seconds(1);
52 DiscretizeQ(Q_continuous, A_continuous, dt, &Q_d);
53 // TODO(james): Using Runge Kutta for this is a bit silly as f is just a
54 // function of t, not Q, but I don't want to rewrite any of our math
55 // utilities.
56 // Note that we are being very explicit about the types of everything in this
57 // integration because otherwise it doesn't compile very well.
58 Eigen::Matrix<double, 2, 2> Q_d_integrated = control_loops::RungeKutta<
59 ::std::function<Eigen::Matrix<double, 2, 2>(
60 const double, const Eigen::Matrix<double, 2, 2> &)>,
61 Eigen::Matrix<double, 2, 2>>(
62 [this](const double t, const Eigen::Matrix<double, 2, 2> &) {
63 return Eigen::Matrix<double, 2, 2>(
64 (A_continuous * t).exp() * Q_continuous *
65 (A_continuous.transpose() * t).exp());
66 },
67 Eigen::Matrix<double, 2, 2>::Zero(), 0, 1.0);
68 EXPECT_LT((Q_d_integrated - Q_d).norm(), 1e-10)
Philipp Schrader790cb542023-07-05 21:06:52 -070069 << "Expected these to be nearly equal:\nQ_d:\n"
70 << Q_d << "\nQ_d_integrated:\n"
71 << Q_d_integrated;
James Kuszmaul59a5c612019-01-22 07:56:08 -080072}
73
James Kuszmaulb2a2f352019-03-02 16:59:34 -080074// Tests that the "fast" discretization produces nearly identical results.
75TEST_F(C2DTest, DiscretizeQAFast) {
76 Eigen::Matrix<double, 2, 2> Q_d;
77 Eigen::Matrix<double, 2, 2> Q_d_fast;
78 Eigen::Matrix<double, 2, 2> A_d;
79 Eigen::Matrix<double, 2, 2> A_d_fast;
80 Eigen::Matrix<double, 2, 1> B_d;
81 const auto dt = ::std::chrono::seconds(1);
82 DiscretizeQ(Q_continuous, A_continuous, dt, &Q_d);
83 C2D(A_continuous, B_continuous, dt, &A_d, &B_d);
84 DiscretizeQAFast(Q_continuous, A_continuous, dt, &Q_d_fast, &A_d_fast);
85 EXPECT_LT((Q_d - Q_d_fast).norm(), 1e-20);
86 EXPECT_LT((A_d - A_d_fast).norm(), 1e-20);
87}
88
James Kuszmaul59a5c612019-01-22 07:56:08 -080089} // namespace testing
90} // namespace controls
91} // namespace frc971