blob: aa64638bedb7d3c6dd03da7eb44a0131f6b735e2 [file] [log] [blame]
Austin Schuhacd335a2017-01-01 16:20:54 -08001#include "frc971/control_loops/runge_kutta.h"
2
3#include "gtest/gtest.h"
4
5namespace frc971 {
6namespace control_loops {
7namespace testing {
8
9// Tests that integrating dx/dt = e^x works.
10TEST(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 Schuh268a94f2018-02-17 17:10:19 -080024// Tests that integrating dx/dt = e^x works when we provide a U.
25TEST(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 Schuhacd335a2017-01-01 16:20:54 -080039} // namespace testing
40} // namespace control_loops
41} // namespace frc971