// 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 = (Eigen::Matrix() << 1, 2, 4, 1, 5, 2, 3, 4, 5, 1, 3, 2, 1, 1, 3, 7) .finished(); Eigen::Matrix B = (Eigen::Matrix() << 1, 1, 2, 1, 3, 2, 3, 7).finished(); // Function from which to recover A and B Eigen::Matrix AxBuFn(const Eigen::Matrix& x, const Eigen::Matrix& 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::Matrix::Zero(), Eigen::Matrix::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::Matrix::Zero(), Eigen::Matrix::Zero()); EXPECT_TRUE(newB.isApprox(B)); } Eigen::Matrix C = (Eigen::Matrix() << 1, 2, 4, 1, 5, 2, 3, 4, 5, 1, 3, 2) .finished(); Eigen::Matrix D = (Eigen::Matrix() << 1, 1, 2, 1, 3, 2).finished(); // Function from which to recover C and D Eigen::Matrix CxDuFn(const Eigen::Matrix& x, const Eigen::Matrix& 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::Matrix::Zero(), Eigen::Matrix::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::Matrix::Zero(), Eigen::Matrix::Zero()); EXPECT_TRUE(newD.isApprox(D)); }