// 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 #include "frc/system/NumericalJacobian.h" Eigen::Matrix A{ {1, 2, 4, 1}, {5, 2, 3, 4}, {5, 1, 3, 2}, {1, 1, 3, 7}}; Eigen::Matrix B{{1, 1}, {2, 1}, {3, 2}, {3, 7}}; // Function from which to recover A and B Eigen::Vector AxBuFn(const Eigen::Vector& x, const Eigen::Vector& u) { return A * x + B * u; } // Test that we can recover A from AxBuFn() pretty accurately TEST(NumericalJacobianTest, Ax) { Eigen::Matrix newA = frc::NumericalJacobianX<4, 4, 2>(AxBuFn, Eigen::Vector::Zero(), Eigen::Vector::Zero()); EXPECT_TRUE(newA.isApprox(A)); } // Test that we can recover B from AxBuFn() pretty accurately TEST(NumericalJacobianTest, Bu) { Eigen::Matrix newB = frc::NumericalJacobianU<4, 4, 2>(AxBuFn, Eigen::Vector::Zero(), Eigen::Vector::Zero()); EXPECT_TRUE(newB.isApprox(B)); } Eigen::Matrix C{{1, 2, 4, 1}, {5, 2, 3, 4}, {5, 1, 3, 2}}; Eigen::Matrix D{{1, 1}, {2, 1}, {3, 2}}; // Function from which to recover C and D Eigen::Vector CxDuFn(const Eigen::Vector& x, const Eigen::Vector& u) { return C * x + D * u; } // Test that we can recover C from CxDuFn() pretty accurately TEST(NumericalJacobianTest, Cx) { Eigen::Matrix newC = frc::NumericalJacobianX<3, 4, 2>(CxDuFn, Eigen::Vector::Zero(), Eigen::Vector::Zero()); EXPECT_TRUE(newC.isApprox(C)); } // Test that we can recover D from CxDuFn() pretty accurately TEST(NumericalJacobianTest, Du) { Eigen::Matrix newD = frc::NumericalJacobianU<3, 4, 2>(CxDuFn, Eigen::Vector::Zero(), Eigen::Vector::Zero()); EXPECT_TRUE(newD.isApprox(D)); }