Austin Schuh | acd335a | 2017-01-01 16:20:54 -0800 | [diff] [blame] | 1 | #include "frc971/control_loops/runge_kutta.h" |
| 2 | |
| 3 | #include "gtest/gtest.h" |
| 4 | |
| 5 | namespace frc971 { |
| 6 | namespace control_loops { |
| 7 | namespace testing { |
| 8 | |
| 9 | // Tests that integrating dx/dt = e^x works. |
| 10 | TEST(RungeKuttaTest, Exponential) { |
| 11 | ::Eigen::Matrix<double, 1, 1> y0; |
| 12 | y0(0, 0) = 0.0; |
| 13 | |
| 14 | ::Eigen::Matrix<double, 1, 1> y1 = RungeKutta( |
| 15 | [](::Eigen::Matrix<double, 1, 1> x) { |
| 16 | ::Eigen::Matrix<double, 1, 1> y; |
| 17 | y(0, 0) = ::std::exp(x(0, 0)); |
| 18 | return y; |
| 19 | }, |
| 20 | y0, 0.1); |
| 21 | EXPECT_NEAR(y1(0, 0), ::std::exp(0.1) - ::std::exp(0), 1e-3); |
| 22 | } |
| 23 | |
Austin Schuh | 268a94f | 2018-02-17 17:10:19 -0800 | [diff] [blame] | 24 | // Tests that integrating dx/dt = e^x works when we provide a U. |
| 25 | TEST(RungeKuttaTest, ExponentialWithU) { |
| 26 | ::Eigen::Matrix<double, 1, 1> y0; |
| 27 | y0(0, 0) = 0.0; |
| 28 | |
| 29 | ::Eigen::Matrix<double, 1, 1> y1 = RungeKutta( |
| 30 | [](::Eigen::Matrix<double, 1, 1> x, ::Eigen::Matrix<double, 1, 1> u) { |
| 31 | ::Eigen::Matrix<double, 1, 1> y; |
| 32 | y(0, 0) = ::std::exp(u(0, 0) * x(0, 0)); |
| 33 | return y; |
| 34 | }, |
| 35 | y0, (::Eigen::Matrix<double, 1, 1>() << 1.0).finished(), 0.1); |
| 36 | EXPECT_NEAR(y1(0, 0), ::std::exp(0.1) - ::std::exp(0), 1e-3); |
| 37 | } |
| 38 | |
Austin Schuh | acd335a | 2017-01-01 16:20:54 -0800 | [diff] [blame] | 39 | } // namespace testing |
| 40 | } // namespace control_loops |
| 41 | } // namespace frc971 |