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 <cmath>
|
|
|
|
|
|
2021-01-06 21:40:25 -08:00
|
|
|
#include "frc/system/NumericalIntegration.h"
|
2020-08-14 23:40:33 -07:00
|
|
|
|
|
|
|
|
// Tests that integrating dx/dt = e^x works.
|
2021-01-06 21:40:25 -08:00
|
|
|
TEST(NumericalIntegrationTest, Exponential) {
|
2021-08-19 00:23:48 -07:00
|
|
|
Eigen::Vector<double, 1> y0{0.0};
|
2020-08-14 23:40:33 -07:00
|
|
|
|
2021-08-19 00:23:48 -07:00
|
|
|
Eigen::Vector<double, 1> y1 = frc::RK4(
|
|
|
|
|
[](const Eigen::Vector<double, 1>& x) {
|
|
|
|
|
return Eigen::Vector<double, 1>{std::exp(x(0))};
|
2020-08-14 23:40:33 -07:00
|
|
|
},
|
|
|
|
|
y0, 0.1_s);
|
|
|
|
|
EXPECT_NEAR(y1(0), std::exp(0.1) - std::exp(0), 1e-3);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Tests that integrating dx/dt = e^x works when we provide a U.
|
2021-01-06 21:40:25 -08:00
|
|
|
TEST(NumericalIntegrationTest, ExponentialWithU) {
|
2021-08-19 00:23:48 -07:00
|
|
|
Eigen::Vector<double, 1> y0{0.0};
|
2020-08-14 23:40:33 -07:00
|
|
|
|
2021-08-19 00:23:48 -07:00
|
|
|
Eigen::Vector<double, 1> y1 = frc::RK4(
|
|
|
|
|
[](const Eigen::Vector<double, 1>& x, const Eigen::Vector<double, 1>& u) {
|
|
|
|
|
return Eigen::Vector<double, 1>{std::exp(u(0) * x(0))};
|
2020-08-14 23:40:33 -07:00
|
|
|
},
|
2021-08-19 00:23:48 -07:00
|
|
|
y0, Eigen::Vector<double, 1>{1.0}, 0.1_s);
|
2020-08-14 23:40:33 -07:00
|
|
|
EXPECT_NEAR(y1(0), std::exp(0.1) - std::exp(0), 1e-3);
|
|
|
|
|
}
|
|
|
|
|
|
2021-07-11 10:42:33 -04:00
|
|
|
// Tests that integrating dx/dt = e^x works with RKF45.
|
|
|
|
|
TEST(NumericalIntegrationTest, ExponentialRKF45) {
|
2021-08-19 00:23:48 -07:00
|
|
|
Eigen::Vector<double, 1> y0{0.0};
|
2021-01-06 21:40:25 -08:00
|
|
|
|
2021-08-19 00:23:48 -07:00
|
|
|
Eigen::Vector<double, 1> y1 = frc::RKF45(
|
|
|
|
|
[](const Eigen::Vector<double, 1>& x, const Eigen::Vector<double, 1>& u) {
|
|
|
|
|
return Eigen::Vector<double, 1>{std::exp(x(0))};
|
2021-01-06 21:40:25 -08:00
|
|
|
},
|
2021-08-19 00:23:48 -07:00
|
|
|
y0, Eigen::Vector<double, 1>{0.0}, 0.1_s);
|
2021-01-06 21:40:25 -08:00
|
|
|
EXPECT_NEAR(y1(0), std::exp(0.1) - std::exp(0), 1e-3);
|
2021-07-11 10:42:33 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Tests that integrating dx/dt = e^x works with RKDP
|
|
|
|
|
TEST(NumericalIntegrationTest, ExponentialRKDP) {
|
2021-08-19 00:23:48 -07:00
|
|
|
Eigen::Vector<double, 1> y0{0.0};
|
2021-07-11 10:42:33 -04:00
|
|
|
|
2021-08-19 00:23:48 -07:00
|
|
|
Eigen::Vector<double, 1> y1 = frc::RKDP(
|
|
|
|
|
[](const Eigen::Vector<double, 1>& x, const Eigen::Vector<double, 1>& u) {
|
|
|
|
|
return Eigen::Vector<double, 1>{std::exp(x(0))};
|
2021-07-11 10:42:33 -04:00
|
|
|
},
|
2021-08-19 00:23:48 -07:00
|
|
|
y0, Eigen::Vector<double, 1>{0.0}, 0.1_s);
|
2021-07-11 10:42:33 -04:00
|
|
|
EXPECT_NEAR(y1(0), std::exp(0.1) - std::exp(0), 1e-3);
|
2021-01-06 21:40:25 -08:00
|
|
|
}
|