mirror of
https://github.com/wpilibsuite/allwpilib
synced 2026-07-01 02:41:48 +00:00
[wpimath] Add time-varying RKDP (#7362)
This makes the ground truth for the Taylor series AQ discretization more accurate.
This commit is contained in:
@@ -72,7 +72,7 @@ class DiscretizationTest {
|
||||
// Q_d = ∫ e^(Aτ) Q e^(Aᵀτ) dτ
|
||||
// 0
|
||||
final var discQIntegrated =
|
||||
RungeKuttaTimeVarying.rungeKuttaTimeVarying(
|
||||
NumericalIntegration.rk4(
|
||||
(Double t, Matrix<N2, N2> x) ->
|
||||
contA.times(t).exp().times(contQ).times(contA.transpose().times(t).exp()),
|
||||
0.0,
|
||||
@@ -104,7 +104,7 @@ class DiscretizationTest {
|
||||
// Q_d = ∫ e^(Aτ) Q e^(Aᵀτ) dτ
|
||||
// 0
|
||||
final var discQIntegrated =
|
||||
RungeKuttaTimeVarying.rungeKuttaTimeVarying(
|
||||
NumericalIntegration.rk4(
|
||||
(Double t, Matrix<N2, N2> x) ->
|
||||
contA.times(t).exp().times(contQ).times(contA.transpose().times(t).exp()),
|
||||
0.0,
|
||||
|
||||
@@ -6,6 +6,7 @@ package edu.wpi.first.math.system;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
|
||||
import edu.wpi.first.math.MatBuilder;
|
||||
import edu.wpi.first.math.Matrix;
|
||||
import edu.wpi.first.math.Nat;
|
||||
import edu.wpi.first.math.VecBuilder;
|
||||
@@ -30,6 +31,28 @@ class NumericalIntegrationTest {
|
||||
assertEquals(Math.exp(0.1) - Math.exp(0.0), y1.get(0, 0), 1e-3);
|
||||
}
|
||||
|
||||
// Tests RK4 with a time varying solution. From
|
||||
// http://www2.hawaii.edu/~jmcfatri/math407/RungeKuttaTest.html:
|
||||
// x' = x (2/(eᵗ + 1) - 1)
|
||||
//
|
||||
// The true (analytical) solution is:
|
||||
//
|
||||
// x(t) = 12eᵗ/(eᵗ + 1)²
|
||||
@Test
|
||||
void testRK4TimeVarying() {
|
||||
final var y0 = VecBuilder.fill(12.0 * Math.exp(5.0) / Math.pow(Math.exp(5.0) + 1.0, 2.0));
|
||||
|
||||
final var y1 =
|
||||
NumericalIntegration.rk4(
|
||||
(Double t, Matrix<N1, N1> y) ->
|
||||
MatBuilder.fill(
|
||||
Nat.N1(), Nat.N1(), y.get(0, 0) * (2.0 / (Math.exp(t) + 1.0) - 1.0)),
|
||||
5.0,
|
||||
y0,
|
||||
1.0);
|
||||
assertEquals(12.0 * Math.exp(6.0) / Math.pow(Math.exp(6.0) + 1.0, 2.0), y1.get(0, 0), 1e-3);
|
||||
}
|
||||
|
||||
@Test
|
||||
void testZeroRKDP() {
|
||||
var y1 =
|
||||
@@ -56,4 +79,28 @@ class NumericalIntegrationTest {
|
||||
|
||||
assertEquals(Math.exp(0.1) - Math.exp(0.0), y1.get(0, 0), 1e-3);
|
||||
}
|
||||
|
||||
// Tests RKDP with a time varying solution. From
|
||||
// http://www2.hawaii.edu/~jmcfatri/math407/RungeKuttaTest.html:
|
||||
//
|
||||
// dx/dt = x(2/(eᵗ + 1) - 1)
|
||||
//
|
||||
// The true (analytical) solution is:
|
||||
//
|
||||
// x(t) = 12eᵗ/(eᵗ + 1)²
|
||||
@Test
|
||||
void testRKDPTimeVarying() {
|
||||
final var y0 = VecBuilder.fill(12.0 * Math.exp(5.0) / Math.pow(Math.exp(5.0) + 1.0, 2.0));
|
||||
|
||||
final var y1 =
|
||||
NumericalIntegration.rkdp(
|
||||
(Double t, Matrix<N1, N1> y) ->
|
||||
MatBuilder.fill(
|
||||
Nat.N1(), Nat.N1(), y.get(0, 0) * (2.0 / (Math.exp(t) + 1.0) - 1.0)),
|
||||
5.0,
|
||||
y0,
|
||||
1.0,
|
||||
1e-12);
|
||||
assertEquals(12.0 * Math.exp(6.0) / Math.pow(Math.exp(6.0) + 1.0, 2.0), y1.get(0, 0), 1e-3);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,40 +0,0 @@
|
||||
// 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.
|
||||
|
||||
package edu.wpi.first.math.system;
|
||||
|
||||
import edu.wpi.first.math.Matrix;
|
||||
import edu.wpi.first.math.Num;
|
||||
import java.util.function.BiFunction;
|
||||
|
||||
public final class RungeKuttaTimeVarying {
|
||||
private RungeKuttaTimeVarying() {
|
||||
// Utility class
|
||||
}
|
||||
|
||||
/**
|
||||
* Performs 4th order Runge-Kutta integration of dx/dt = f(t, y) for dt.
|
||||
*
|
||||
* @param <Rows> Rows in y.
|
||||
* @param <Cols> Columns in y.
|
||||
* @param f The function to integrate. It must take two arguments t and y.
|
||||
* @param t The initial value of t.
|
||||
* @param y The initial value of y.
|
||||
* @param dtSeconds The time over which to integrate.
|
||||
*/
|
||||
public static <Rows extends Num, Cols extends Num> Matrix<Rows, Cols> rungeKuttaTimeVarying(
|
||||
BiFunction<Double, Matrix<Rows, Cols>, Matrix<Rows, Cols>> f,
|
||||
double t,
|
||||
Matrix<Rows, Cols> y,
|
||||
double dtSeconds) {
|
||||
final var h = dtSeconds;
|
||||
|
||||
Matrix<Rows, Cols> k1 = f.apply(t, y);
|
||||
Matrix<Rows, Cols> k2 = f.apply(t + dtSeconds * 0.5, y.plus(k1.times(h * 0.5)));
|
||||
Matrix<Rows, Cols> k3 = f.apply(t + dtSeconds * 0.5, y.plus(k2.times(h * 0.5)));
|
||||
Matrix<Rows, Cols> k4 = f.apply(t + dtSeconds, y.plus(k3.times(h)));
|
||||
|
||||
return y.plus((k1.plus(k2.times(2.0)).plus(k3.times(2.0)).plus(k4)).times(h / 6.0));
|
||||
}
|
||||
}
|
||||
@@ -1,42 +0,0 @@
|
||||
// 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.
|
||||
|
||||
package edu.wpi.first.math.system;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
|
||||
import edu.wpi.first.math.MatBuilder;
|
||||
import edu.wpi.first.math.Matrix;
|
||||
import edu.wpi.first.math.Nat;
|
||||
import edu.wpi.first.math.numbers.N1;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
class RungeKuttaTimeVaryingTest {
|
||||
private static Matrix<N1, N1> rungeKuttaTimeVaryingSolution(double t) {
|
||||
return MatBuilder.fill(
|
||||
Nat.N1(), Nat.N1(), 12.0 * Math.exp(t) / Math.pow(Math.exp(t) + 1.0, 2.0));
|
||||
}
|
||||
|
||||
// Tests RK4 with a time varying solution. From
|
||||
// http://www2.hawaii.edu/~jmcfatri/math407/RungeKuttaTest.html:
|
||||
// x' = x (2/(eᵗ + 1) - 1)
|
||||
//
|
||||
// The true (analytical) solution is:
|
||||
//
|
||||
// x(t) = 12eᵗ/(eᵗ + 1)²
|
||||
@Test
|
||||
void rungeKuttaTimeVaryingTest() {
|
||||
final var y0 = rungeKuttaTimeVaryingSolution(5.0);
|
||||
|
||||
final var y1 =
|
||||
RungeKuttaTimeVarying.rungeKuttaTimeVarying(
|
||||
(Double t, Matrix<N1, N1> x) ->
|
||||
MatBuilder.fill(
|
||||
Nat.N1(), Nat.N1(), x.get(0, 0) * (2.0 / (Math.exp(t) + 1.0) - 1.0)),
|
||||
5.0,
|
||||
y0,
|
||||
1.0);
|
||||
assertEquals(rungeKuttaTimeVaryingSolution(6.0).get(0, 0), y1.get(0, 0), 1e-3);
|
||||
}
|
||||
}
|
||||
@@ -10,7 +10,6 @@
|
||||
#include "frc/EigenCore.h"
|
||||
#include "frc/system/Discretization.h"
|
||||
#include "frc/system/NumericalIntegration.h"
|
||||
#include "frc/system/RungeKuttaTimeVarying.h"
|
||||
|
||||
// Check that for a simple second-order system that we can easily analyze
|
||||
// analytically,
|
||||
@@ -62,15 +61,15 @@ TEST(DiscretizationTest, DiscretizeSlowModelAQ) {
|
||||
// T
|
||||
// Q_d ≈ ∫ e^(Aτ) Q e^(Aᵀτ) dτ
|
||||
// 0
|
||||
frc::Matrixd<2, 2> discQIntegrated = frc::RungeKuttaTimeVarying<
|
||||
std::function<frc::Matrixd<2, 2>(units::second_t,
|
||||
const frc::Matrixd<2, 2>&)>,
|
||||
frc::Matrixd<2, 2>>(
|
||||
[&](units::second_t t, const frc::Matrixd<2, 2>&) {
|
||||
return frc::Matrixd<2, 2>((contA * t.value()).exp() * contQ *
|
||||
(contA.transpose() * t.value()).exp());
|
||||
},
|
||||
0_s, frc::Matrixd<2, 2>::Zero(), dt);
|
||||
frc::Matrixd<2, 2> discQIntegrated =
|
||||
frc::RKDP<std::function<frc::Matrixd<2, 2>(units::second_t,
|
||||
const frc::Matrixd<2, 2>&)>,
|
||||
frc::Matrixd<2, 2>>(
|
||||
[&](units::second_t t, const frc::Matrixd<2, 2>&) {
|
||||
return frc::Matrixd<2, 2>((contA * t.value()).exp() * contQ *
|
||||
(contA.transpose() * t.value()).exp());
|
||||
},
|
||||
0_s, frc::Matrixd<2, 2>::Zero(), dt);
|
||||
|
||||
frc::Matrixd<2, 2> discA;
|
||||
frc::Matrixd<2, 2> discQ;
|
||||
@@ -94,15 +93,15 @@ TEST(DiscretizationTest, DiscretizeFastModelAQ) {
|
||||
// T
|
||||
// Q_d = ∫ e^(Aτ) Q e^(Aᵀτ) dτ
|
||||
// 0
|
||||
frc::Matrixd<2, 2> discQIntegrated = frc::RungeKuttaTimeVarying<
|
||||
std::function<frc::Matrixd<2, 2>(units::second_t,
|
||||
const frc::Matrixd<2, 2>&)>,
|
||||
frc::Matrixd<2, 2>>(
|
||||
[&](units::second_t t, const frc::Matrixd<2, 2>&) {
|
||||
return frc::Matrixd<2, 2>((contA * t.value()).exp() * contQ *
|
||||
(contA.transpose() * t.value()).exp());
|
||||
},
|
||||
0_s, frc::Matrixd<2, 2>::Zero(), dt);
|
||||
frc::Matrixd<2, 2> discQIntegrated =
|
||||
frc::RKDP<std::function<frc::Matrixd<2, 2>(units::second_t,
|
||||
const frc::Matrixd<2, 2>&)>,
|
||||
frc::Matrixd<2, 2>>(
|
||||
[&](units::second_t t, const frc::Matrixd<2, 2>&) {
|
||||
return frc::Matrixd<2, 2>((contA * t.value()).exp() * contQ *
|
||||
(contA.transpose() * t.value()).exp());
|
||||
},
|
||||
0_s, frc::Matrixd<2, 2>::Zero(), dt);
|
||||
|
||||
frc::Matrixd<2, 2> discA;
|
||||
frc::Matrixd<2, 2> discQ;
|
||||
|
||||
@@ -9,7 +9,7 @@
|
||||
#include "frc/EigenCore.h"
|
||||
#include "frc/system/NumericalIntegration.h"
|
||||
|
||||
// Tests that integrating dx/dt = e^x works.
|
||||
// Test that integrating dx/dt = eˣ works
|
||||
TEST(NumericalIntegrationTest, Exponential) {
|
||||
frc::Vectord<1> y0{0.0};
|
||||
|
||||
@@ -19,7 +19,7 @@ TEST(NumericalIntegrationTest, Exponential) {
|
||||
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 that integrating dx/dt = eˣ works when we provide a u
|
||||
TEST(NumericalIntegrationTest, ExponentialWithU) {
|
||||
frc::Vectord<1> y0{0.0};
|
||||
|
||||
@@ -31,6 +31,27 @@ TEST(NumericalIntegrationTest, ExponentialWithU) {
|
||||
EXPECT_NEAR(y1(0), std::exp(0.1) - std::exp(0), 1e-3);
|
||||
}
|
||||
|
||||
// Tests RK4 with a time varying solution. From
|
||||
// http://www2.hawaii.edu/~jmcfatri/math407/RungeKuttaTest.html:
|
||||
//
|
||||
// dx/dt = x (2 / (eᵗ + 1) - 1)
|
||||
//
|
||||
// The true (analytical) solution is:
|
||||
//
|
||||
// x(t) = 12eᵗ/(eᵗ + 1)²
|
||||
TEST(NumericalIntegrationTest, RK4TimeVarying) {
|
||||
frc::Vectord<1> y0{12.0 * std::exp(5.0) / std::pow(std::exp(5.0) + 1.0, 2.0)};
|
||||
|
||||
frc::Vectord<1> y1 = frc::RK4(
|
||||
[](units::second_t t, const frc::Vectord<1>& x) {
|
||||
return frc::Vectord<1>{x(0) *
|
||||
(2.0 / (std::exp(t.value()) + 1.0) - 1.0)};
|
||||
},
|
||||
5_s, y0, 1_s);
|
||||
EXPECT_NEAR(y1(0), 12.0 * std::exp(6.0) / std::pow(std::exp(6.0) + 1.0, 2.0),
|
||||
1e-3);
|
||||
}
|
||||
|
||||
// Tests that integrating dx/dt = 0 works with RKDP
|
||||
TEST(NumericalIntegrationTest, ZeroRKDP) {
|
||||
frc::Vectord<1> y1 = frc::RKDP(
|
||||
@@ -41,7 +62,7 @@ TEST(NumericalIntegrationTest, ZeroRKDP) {
|
||||
EXPECT_NEAR(y1(0), 0.0, 1e-3);
|
||||
}
|
||||
|
||||
// Tests that integrating dx/dt = e^x works with RKDP
|
||||
// Tests that integrating dx/dt = eˣ works with RKDP
|
||||
TEST(NumericalIntegrationTest, ExponentialRKDP) {
|
||||
frc::Vectord<1> y0{0.0};
|
||||
|
||||
@@ -52,3 +73,24 @@ TEST(NumericalIntegrationTest, ExponentialRKDP) {
|
||||
y0, frc::Vectord<1>{0.0}, 0.1_s);
|
||||
EXPECT_NEAR(y1(0), std::exp(0.1) - std::exp(0), 1e-3);
|
||||
}
|
||||
|
||||
// Tests RKDP with a time varying solution. From
|
||||
// http://www2.hawaii.edu/~jmcfatri/math407/RungeKuttaTest.html:
|
||||
//
|
||||
// dx/dt = x(2/(eᵗ + 1) - 1)
|
||||
//
|
||||
// The true (analytical) solution is:
|
||||
//
|
||||
// x(t) = 12eᵗ/(eᵗ + 1)²
|
||||
TEST(NumericalIntegrationTest, RKDPTimeVarying) {
|
||||
frc::Vectord<1> y0{12.0 * std::exp(5.0) / std::pow(std::exp(5.0) + 1.0, 2.0)};
|
||||
|
||||
frc::Vectord<1> y1 = frc::RKDP(
|
||||
[](units::second_t t, const frc::Vectord<1>& x) {
|
||||
return frc::Vectord<1>{x(0) *
|
||||
(2.0 / (std::exp(t.value()) + 1.0) - 1.0)};
|
||||
},
|
||||
5_s, y0, 1_s, 1e-12);
|
||||
EXPECT_NEAR(y1(0), 12.0 * std::exp(6.0) / std::pow(std::exp(6.0) + 1.0, 2.0),
|
||||
1e-3);
|
||||
}
|
||||
|
||||
@@ -1,35 +0,0 @@
|
||||
// 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 <cmath>
|
||||
|
||||
#include <gtest/gtest.h>
|
||||
|
||||
#include "frc/EigenCore.h"
|
||||
#include "frc/system/RungeKuttaTimeVarying.h"
|
||||
|
||||
namespace {
|
||||
frc::Vectord<1> RungeKuttaTimeVaryingSolution(double t) {
|
||||
return frc::Vectord<1>{12.0 * std::exp(t) / std::pow(std::exp(t) + 1.0, 2.0)};
|
||||
}
|
||||
} // namespace
|
||||
|
||||
// Tests RK4 with a time varying solution. From
|
||||
// http://www2.hawaii.edu/~jmcfatri/math407/RungeKuttaTest.html:
|
||||
// x' = x (2/(eᵗ + 1) - 1)
|
||||
//
|
||||
// The true (analytical) solution is:
|
||||
//
|
||||
// x(t) = 12eᵗ/((eᵗ + 1)²)
|
||||
TEST(RungeKuttaTimeVaryingTest, RungeKuttaTimeVarying) {
|
||||
frc::Vectord<1> y0 = RungeKuttaTimeVaryingSolution(5.0);
|
||||
|
||||
frc::Vectord<1> y1 = frc::RungeKuttaTimeVarying(
|
||||
[](units::second_t t, const frc::Vectord<1>& x) {
|
||||
return frc::Vectord<1>{x(0) *
|
||||
(2.0 / (std::exp(t.value()) + 1.0) - 1.0)};
|
||||
},
|
||||
5_s, y0, 1_s);
|
||||
EXPECT_NEAR(y1(0), RungeKuttaTimeVaryingSolution(6.0)(0), 1e-3);
|
||||
}
|
||||
@@ -1,33 +0,0 @@
|
||||
// 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.
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <array>
|
||||
|
||||
#include "units/time.h"
|
||||
|
||||
namespace frc {
|
||||
|
||||
/**
|
||||
* Performs 4th order Runge-Kutta integration of dy/dt = f(t, y) for dt.
|
||||
*
|
||||
* @param f The function to integrate. It must take two arguments t and y.
|
||||
* @param t The initial value of t.
|
||||
* @param y The initial value of y.
|
||||
* @param dt The time over which to integrate.
|
||||
*/
|
||||
template <typename F, typename T>
|
||||
T RungeKuttaTimeVarying(F&& f, units::second_t t, T y, units::second_t dt) {
|
||||
const auto h = dt.value();
|
||||
|
||||
T k1 = f(t, y);
|
||||
T k2 = f(t + dt * 0.5, y + h * k1 * 0.5);
|
||||
T k3 = f(t + dt * 0.5, y + h * k2 * 0.5);
|
||||
T k4 = f(t + dt, y + h * k3);
|
||||
|
||||
return y + h / 6.0 * (k1 + 2.0 * k2 + 2.0 * k3 + k4);
|
||||
}
|
||||
|
||||
} // namespace frc
|
||||
Reference in New Issue
Block a user