// 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 #include "frc/system/NumericalIntegration.h" // Tests that integrating dx/dt = e^x works. TEST(NumericalIntegrationTest, Exponential) { Eigen::Vector y0{0.0}; Eigen::Vector y1 = frc::RK4( [](const Eigen::Vector& x) { return Eigen::Vector{std::exp(x(0))}; }, 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. TEST(NumericalIntegrationTest, ExponentialWithU) { Eigen::Vector y0{0.0}; Eigen::Vector y1 = frc::RK4( [](const Eigen::Vector& x, const Eigen::Vector& u) { return Eigen::Vector{std::exp(u(0) * x(0))}; }, y0, Eigen::Vector{1.0}, 0.1_s); EXPECT_NEAR(y1(0), std::exp(0.1) - std::exp(0), 1e-3); } // Tests that integrating dx/dt = e^x works with RKF45. TEST(NumericalIntegrationTest, ExponentialRKF45) { Eigen::Vector y0{0.0}; Eigen::Vector y1 = frc::RKF45( [](const Eigen::Vector& x, const Eigen::Vector& u) { return Eigen::Vector{std::exp(x(0))}; }, y0, Eigen::Vector{0.0}, 0.1_s); EXPECT_NEAR(y1(0), std::exp(0.1) - std::exp(0), 1e-3); } // Tests that integrating dx/dt = e^x works with RKDP TEST(NumericalIntegrationTest, ExponentialRKDP) { Eigen::Vector y0{0.0}; Eigen::Vector y1 = frc::RKDP( [](const Eigen::Vector& x, const Eigen::Vector& u) { return Eigen::Vector{std::exp(x(0))}; }, y0, Eigen::Vector{0.0}, 0.1_s); EXPECT_NEAR(y1(0), std::exp(0.1) - std::exp(0), 1e-3); }