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