WPILibC++ 2025.2.1
Loading...
Searching...
No Matches
NumericalIntegration.h
Go to the documentation of this file.
1// Copyright (c) FIRST and other WPILib contributors.
2// Open Source Software; you can modify and/or share it under the terms of
3// the WPILib BSD license file in the root directory of this project.
4
5#pragma once
6
7#include <algorithm>
8#include <array>
9#include <cmath>
10
11#include "units/time.h"
12
13namespace frc {
14
15/**
16 * Performs 4th order Runge-Kutta integration of dx/dt = f(x) for dt.
17 *
18 * @param f The function to integrate. It must take one argument x.
19 * @param x The initial value of x.
20 * @param dt The time over which to integrate.
21 */
22template <typename F, typename T>
23T RK4(F&& f, T x, units::second_t dt) {
24 const auto h = dt.value();
25
26 T k1 = f(x);
27 T k2 = f(x + h * 0.5 * k1);
28 T k3 = f(x + h * 0.5 * k2);
29 T k4 = f(x + h * k3);
30
31 return x + h / 6.0 * (k1 + 2.0 * k2 + 2.0 * k3 + k4);
32}
33
34/**
35 * Performs 4th order Runge-Kutta integration of dx/dt = f(x, u) for dt.
36 *
37 * @param f The function to integrate. It must take two arguments x and u.
38 * @param x The initial value of x.
39 * @param u The value u held constant over the integration period.
40 * @param dt The time over which to integrate.
41 */
42template <typename F, typename T, typename U>
43T RK4(F&& f, T x, U u, units::second_t dt) {
44 const auto h = dt.value();
45
46 T k1 = f(x, u);
47 T k2 = f(x + h * 0.5 * k1, u);
48 T k3 = f(x + h * 0.5 * k2, u);
49 T k4 = f(x + h * k3, u);
50
51 return x + h / 6.0 * (k1 + 2.0 * k2 + 2.0 * k3 + k4);
52}
53
54/**
55 * Performs 4th order Runge-Kutta integration of dy/dt = f(t, y) for dt.
56 *
57 * @param f The function to integrate. It must take two arguments t and y.
58 * @param t The initial value of t.
59 * @param y The initial value of y.
60 * @param dt The time over which to integrate.
61 */
62template <typename F, typename T>
63T RK4(F&& f, units::second_t t, T y, units::second_t dt) {
64 const auto h = dt.to<double>();
65
66 T k1 = f(t, y);
67 T k2 = f(t + dt * 0.5, y + h * k1 * 0.5);
68 T k3 = f(t + dt * 0.5, y + h * k2 * 0.5);
69 T k4 = f(t + dt, y + h * k3);
70
71 return y + h / 6.0 * (k1 + 2.0 * k2 + 2.0 * k3 + k4);
72}
73
74/**
75 * Performs adaptive Dormand-Prince integration of dx/dt = f(x, u) for dt.
76 *
77 * @param f The function to integrate. It must take two arguments x and
78 * u.
79 * @param x The initial value of x.
80 * @param u The value u held constant over the integration period.
81 * @param dt The time over which to integrate.
82 * @param maxError The maximum acceptable truncation error. Usually a small
83 * number like 1e-6.
84 */
85template <typename F, typename T, typename U>
86T RKDP(F&& f, T x, U u, units::second_t dt, double maxError = 1e-6) {
87 // See https://en.wikipedia.org/wiki/Dormand%E2%80%93Prince_method for the
88 // Butcher tableau the following arrays came from.
89
90 constexpr int kDim = 7;
91
92 // clang-format off
93 constexpr double A[kDim - 1][kDim - 1]{
94 { 1.0 / 5.0},
95 { 3.0 / 40.0, 9.0 / 40.0},
96 { 44.0 / 45.0, -56.0 / 15.0, 32.0 / 9.0},
97 {19372.0 / 6561.0, -25360.0 / 2187.0, 64448.0 / 6561.0, -212.0 / 729.0},
98 { 9017.0 / 3168.0, -355.0 / 33.0, 46732.0 / 5247.0, 49.0 / 176.0, -5103.0 / 18656.0},
99 { 35.0 / 384.0, 0.0, 500.0 / 1113.0, 125.0 / 192.0, -2187.0 / 6784.0, 11.0 / 84.0}};
100 // clang-format on
101
102 constexpr std::array<double, kDim> b1{
103 35.0 / 384.0, 0.0, 500.0 / 1113.0, 125.0 / 192.0, -2187.0 / 6784.0,
104 11.0 / 84.0, 0.0};
105 constexpr std::array<double, kDim> b2{5179.0 / 57600.0, 0.0,
106 7571.0 / 16695.0, 393.0 / 640.0,
107 -92097.0 / 339200.0, 187.0 / 2100.0,
108 1.0 / 40.0};
109
110 T newX;
111 double truncationError;
112
113 double dtElapsed = 0.0;
114 double h = dt.value();
115
116 // Loop until we've gotten to our desired dt
117 while (dtElapsed < dt.value()) {
118 do {
119 // Only allow us to advance up to the dt remaining
120 h = (std::min)(h, dt.value() - dtElapsed);
121
122 // clang-format off
123 T k1 = f(x, u);
124 T k2 = f(x + h * (A[0][0] * k1), u);
125 T k3 = f(x + h * (A[1][0] * k1 + A[1][1] * k2), u);
126 T k4 = f(x + h * (A[2][0] * k1 + A[2][1] * k2 + A[2][2] * k3), u);
127 T k5 = f(x + h * (A[3][0] * k1 + A[3][1] * k2 + A[3][2] * k3 + A[3][3] * k4), u);
128 T k6 = f(x + h * (A[4][0] * k1 + A[4][1] * k2 + A[4][2] * k3 + A[4][3] * k4 + A[4][4] * k5), u);
129 // clang-format on
130
131 // Since the final row of A and the array b1 have the same coefficients
132 // and k7 has no effect on newX, we can reuse the calculation.
133 newX = x + h * (A[5][0] * k1 + A[5][1] * k2 + A[5][2] * k3 +
134 A[5][3] * k4 + A[5][4] * k5 + A[5][5] * k6);
135 T k7 = f(newX, u);
136
137 truncationError = (h * ((b1[0] - b2[0]) * k1 + (b1[1] - b2[1]) * k2 +
138 (b1[2] - b2[2]) * k3 + (b1[3] - b2[3]) * k4 +
139 (b1[4] - b2[4]) * k5 + (b1[5] - b2[5]) * k6 +
140 (b1[6] - b2[6]) * k7))
141 .norm();
142
143 if (truncationError == 0.0) {
144 h = dt.value() - dtElapsed;
145 } else {
146 h *= 0.9 * std::pow(maxError / truncationError, 1.0 / 5.0);
147 }
148 } while (truncationError > maxError);
149
150 dtElapsed += h;
151 x = newX;
152 }
153
154 return x;
155}
156
157/**
158 * Performs adaptive Dormand-Prince integration of dy/dt = f(t, y) for dt.
159 *
160 * @param f The function to integrate. It must take two arguments t and
161 * y.
162 * @param t The initial value of t.
163 * @param y The initial value of y.
164 * @param dt The time over which to integrate.
165 * @param maxError The maximum acceptable truncation error. Usually a small
166 * number like 1e-6.
167 */
168template <typename F, typename T>
169T RKDP(F&& f, units::second_t t, T y, units::second_t dt,
170 double maxError = 1e-6) {
171 // See https://en.wikipedia.org/wiki/Dormand%E2%80%93Prince_method for the
172 // Butcher tableau the following arrays came from.
173
174 constexpr int kDim = 7;
175
176 // clang-format off
177 constexpr double A[kDim - 1][kDim - 1]{
178 { 1.0 / 5.0},
179 { 3.0 / 40.0, 9.0 / 40.0},
180 { 44.0 / 45.0, -56.0 / 15.0, 32.0 / 9.0},
181 {19372.0 / 6561.0, -25360.0 / 2187.0, 64448.0 / 6561.0, -212.0 / 729.0},
182 { 9017.0 / 3168.0, -355.0 / 33.0, 46732.0 / 5247.0, 49.0 / 176.0, -5103.0 / 18656.0},
183 { 35.0 / 384.0, 0.0, 500.0 / 1113.0, 125.0 / 192.0, -2187.0 / 6784.0, 11.0 / 84.0}};
184 // clang-format on
185
186 constexpr std::array<double, kDim> b1{
187 35.0 / 384.0, 0.0, 500.0 / 1113.0, 125.0 / 192.0, -2187.0 / 6784.0,
188 11.0 / 84.0, 0.0};
189 constexpr std::array<double, kDim> b2{5179.0 / 57600.0, 0.0,
190 7571.0 / 16695.0, 393.0 / 640.0,
191 -92097.0 / 339200.0, 187.0 / 2100.0,
192 1.0 / 40.0};
193
194 constexpr std::array<double, kDim - 1> c{1.0 / 5.0, 3.0 / 10.0, 4.0 / 5.0,
195 8.0 / 9.0, 1.0, 1.0};
196
197 T newY;
198 double truncationError;
199
200 double dtElapsed = 0.0;
201 double h = dt.to<double>();
202
203 // Loop until we've gotten to our desired dt
204 while (dtElapsed < dt.to<double>()) {
205 do {
206 // Only allow us to advance up to the dt remaining
207 h = std::min(h, dt.to<double>() - dtElapsed);
208
209 // clang-format off
210 T k1 = f(t, y);
211 T k2 = f(t + units::second_t{h} * c[0], y + h * (A[0][0] * k1));
212 T k3 = f(t + units::second_t{h} * c[1], y + h * (A[1][0] * k1 + A[1][1] * k2));
213 T k4 = f(t + units::second_t{h} * c[2], y + h * (A[2][0] * k1 + A[2][1] * k2 + A[2][2] * k3));
214 T k5 = f(t + units::second_t{h} * c[3], y + h * (A[3][0] * k1 + A[3][1] * k2 + A[3][2] * k3 + A[3][3] * k4));
215 T k6 = f(t + units::second_t{h} * c[4], y + h * (A[4][0] * k1 + A[4][1] * k2 + A[4][2] * k3 + A[4][3] * k4 + A[4][4] * k5));
216 // clang-format on
217
218 // Since the final row of A and the array b1 have the same coefficients
219 // and k7 has no effect on newY, we can reuse the calculation.
220 newY = y + h * (A[5][0] * k1 + A[5][1] * k2 + A[5][2] * k3 +
221 A[5][3] * k4 + A[5][4] * k5 + A[5][5] * k6);
222 T k7 = f(t + units::second_t{h} * c[5], newY);
223
224 truncationError = (h * ((b1[0] - b2[0]) * k1 + (b1[1] - b2[1]) * k2 +
225 (b1[2] - b2[2]) * k3 + (b1[3] - b2[3]) * k4 +
226 (b1[4] - b2[4]) * k5 + (b1[5] - b2[5]) * k6 +
227 (b1[6] - b2[6]) * k7))
228 .norm();
229
230 h *= 0.9 * std::pow(maxError / truncationError, 1.0 / 5.0);
231 } while (truncationError > maxError);
232
233 dtElapsed += h;
234 y = newY;
235 }
236
237 return y;
238}
239
240} // namespace frc
Definition CAN.h:11
T RK4(F &&f, T x, units::second_t dt)
Performs 4th order Runge-Kutta integration of dx/dt = f(x) for dt.
Definition NumericalIntegration.h:23
T RKDP(F &&f, T x, U u, units::second_t dt, double maxError=1e-6)
Performs adaptive Dormand-Prince integration of dx/dt = f(x, u) for dt.
Definition NumericalIntegration.h:86