blob: 92a15a36ba7bf19d8220aab41186bf3891919a2c [file] [log] [blame]
Austin Schuha647d602018-02-18 14:05:15 -08001#include "frc971/control_loops/jacobian.h"
2
3#include "gtest/gtest.h"
4
Stephan Pleinesf63bde82024-01-13 15:59:33 -08005namespace frc971::control_loops::testing {
Austin Schuha647d602018-02-18 14:05:15 -08006
7::Eigen::Matrix<double, 4, 4> A = (::Eigen::Matrix<double, 4, 4>() << 1, 2, 4,
Philipp Schrader790cb542023-07-05 21:06:52 -07008 1, 5, 2, 3, 4, 5, 1, 3, 2, 1, 1, 3, 7)
9 .finished();
Austin Schuha647d602018-02-18 14:05:15 -080010
11::Eigen::Matrix<double, 4, 2> B =
12 (::Eigen::Matrix<double, 4, 2>() << 1, 1, 2, 1, 3, 2, 3, 7).finished();
13
14// Function to recover A and B from.
15::Eigen::Matrix<double, 4, 1> AxBufn(const ::Eigen::Matrix<double, 4, 1> &X,
16 const ::Eigen::Matrix<double, 2, 1> &U) {
17 return A * X + B * U;
18}
19
20// Test that we can recover A from AxBufn pretty accurately.
21TEST(RungeKuttaTest, Ax) {
James Kuszmauld6e7f692024-10-29 22:29:17 -070022 ::Eigen::Matrix<double, 4, 4> NewA = NumericalJacobianX<4, 2, double>(
23 AxBufn, ::Eigen::Matrix<double, 4, 1>::Zero(),
24 ::Eigen::Matrix<double, 2, 1>::Zero());
Austin Schuha647d602018-02-18 14:05:15 -080025 EXPECT_TRUE(NewA.isApprox(A));
26}
27
28// Test that we can recover B from AxBufn pretty accurately.
29TEST(RungeKuttaTest, Bu) {
James Kuszmauld6e7f692024-10-29 22:29:17 -070030 ::Eigen::Matrix<double, 4, 2> NewB = NumericalJacobianU<4, 2, double>(
31 AxBufn, ::Eigen::Matrix<double, 4, 1>::Zero(),
32 ::Eigen::Matrix<double, 2, 1>::Zero());
Austin Schuha647d602018-02-18 14:05:15 -080033 EXPECT_TRUE(NewB.isApprox(B));
34}
35
Stephan Pleinesf63bde82024-01-13 15:59:33 -080036} // namespace frc971::control_loops::testing