Austin Schuh | acd335a | 2017-01-01 16:20:54 -0800 | [diff] [blame] | 1 | #ifndef FRC971_CONTROL_LOOPS_RUNGE_KUTTA_H_ |
| 2 | #define FRC971_CONTROL_LOOPS_RUNGE_KUTTA_H_ |
| 3 | |
Austin Schuh | 99f7c6a | 2024-06-25 22:07:44 -0700 | [diff] [blame] | 4 | #include "absl/log/check.h" |
| 5 | #include "absl/log/log.h" |
Austin Schuh | acd335a | 2017-01-01 16:20:54 -0800 | [diff] [blame] | 6 | #include <Eigen/Dense> |
| 7 | |
Austin Schuh | b0bfaf8 | 2024-06-19 19:47:23 -0700 | [diff] [blame] | 8 | #include "frc971/control_loops/runge_kutta_helpers.h" |
| 9 | |
Stephan Pleines | d99b1ee | 2024-02-02 20:56:44 -0800 | [diff] [blame] | 10 | namespace frc971::control_loops { |
Austin Schuh | acd335a | 2017-01-01 16:20:54 -0800 | [diff] [blame] | 11 | |
| 12 | // Implements Runge Kutta integration (4th order). fn is the function to |
| 13 | // integrate. It must take 1 argument of type T. The integration starts at an |
| 14 | // initial value X, and integrates for dt. |
| 15 | template <typename F, typename T> |
| 16 | T RungeKutta(const F &fn, T X, double dt) { |
| 17 | const double half_dt = dt * 0.5; |
Austin Schuh | 92ebcbb | 2018-01-23 11:17:08 -0800 | [diff] [blame] | 18 | T k1 = fn(X); |
| 19 | T k2 = fn(X + half_dt * k1); |
| 20 | T k3 = fn(X + half_dt * k2); |
| 21 | T k4 = fn(X + dt * k3); |
Austin Schuh | acd335a | 2017-01-01 16:20:54 -0800 | [diff] [blame] | 22 | return X + dt / 6.0 * (k1 + 2.0 * k2 + 2.0 * k3 + k4); |
| 23 | } |
| 24 | |
milind-u | e53bf55 | 2021-12-11 14:42:00 -0800 | [diff] [blame] | 25 | // Implements Runge Kutta integration (4th order) split up into steps steps. fn |
| 26 | // is the function to integrate. It must take 1 argument of type T. The |
| 27 | // integration starts at an initial value X, and integrates for dt. |
| 28 | template <typename F, typename T> |
| 29 | T RungeKuttaSteps(const F &fn, T X, double dt, int steps) { |
| 30 | dt = dt / steps; |
| 31 | for (int i = 0; i < steps; ++i) { |
| 32 | X = RungeKutta(fn, X, dt); |
| 33 | } |
| 34 | return X; |
| 35 | } |
| 36 | |
Austin Schuh | f746673 | 2023-02-20 22:11:41 -0800 | [diff] [blame] | 37 | // Implements Runge Kutta integration (4th order). This integrates dy/dt = |
| 38 | // fn(t, y). It must have the call signature of fn(double t, T y). The |
Austin Schuh | ca52a24 | 2018-12-23 09:19:29 +1100 | [diff] [blame] | 39 | // integration starts at an initial value y, and integrates for dt. |
| 40 | template <typename F, typename T> |
| 41 | T RungeKutta(const F &fn, T y, double t, double dt) { |
| 42 | const double half_dt = dt * 0.5; |
| 43 | T k1 = dt * fn(t, y); |
| 44 | T k2 = dt * fn(t + half_dt, y + k1 / 2.0); |
| 45 | T k3 = dt * fn(t + half_dt, y + k2 / 2.0); |
| 46 | T k4 = dt * fn(t + dt, y + k3); |
| 47 | |
| 48 | return y + (k1 + 2.0 * k2 + 2.0 * k3 + k4) / 6.0; |
| 49 | } |
| 50 | |
Austin Schuh | f746673 | 2023-02-20 22:11:41 -0800 | [diff] [blame] | 51 | template <typename F, typename T> |
| 52 | T RungeKuttaSteps(const F &fn, T X, double t, double dt, int steps) { |
| 53 | dt = dt / steps; |
| 54 | for (int i = 0; i < steps; ++i) { |
| 55 | X = RungeKutta(fn, X, t + dt * i, dt); |
| 56 | } |
| 57 | return X; |
| 58 | } |
| 59 | |
Austin Schuh | 268a94f | 2018-02-17 17:10:19 -0800 | [diff] [blame] | 60 | // Implements Runge Kutta integration (4th order). fn is the function to |
| 61 | // integrate. It must take 1 argument of type T. The integration starts at an |
| 62 | // initial value X, and integrates for dt. |
| 63 | template <typename F, typename T, typename Tu> |
Austin Schuh | 9edb5df | 2018-12-23 09:03:15 +1100 | [diff] [blame] | 64 | T RungeKuttaU(const F &fn, T X, Tu U, double dt) { |
Austin Schuh | 268a94f | 2018-02-17 17:10:19 -0800 | [diff] [blame] | 65 | const double half_dt = dt * 0.5; |
| 66 | T k1 = fn(X, U); |
| 67 | T k2 = fn(X + half_dt * k1, U); |
| 68 | T k3 = fn(X + half_dt * k2, U); |
| 69 | T k4 = fn(X + dt * k3, U); |
| 70 | return X + dt / 6.0 * (k1 + 2.0 * k2 + 2.0 * k3 + k4); |
| 71 | } |
| 72 | |
Austin Schuh | b0bfaf8 | 2024-06-19 19:47:23 -0700 | [diff] [blame] | 73 | // Integrates f(t, y) from t0 to t0 + dt using an explicit Runge Kutta 5(4) to |
| 74 | // implement an adaptive step size. Translated from Scipy. |
| 75 | // |
| 76 | // This uses the Dormand-Prince pair of formulas. The error is controlled |
| 77 | // assuming accuracy of the fourth-order method accuracy, but steps are taken |
| 78 | // using the fifth-order accurate formula (local extrapolation is done). A |
| 79 | // quartic interpolation polynomial is used for the dense output. |
| 80 | // |
| 81 | // fn(t, y) is the function to integrate. y0 is the initial y, t0 is the |
| 82 | // initial time, dt is the duration to integrate, rtol is the relative |
| 83 | // tolerance, and atol is the absolute tolerance. |
| 84 | template <typename F, typename T> |
| 85 | T AdaptiveRungeKutta(const F &fn, T y0, double t0, double dt, |
| 86 | double rtol = 1e-3, double atol = 1e-6) { |
| 87 | // Multiply steps computed from asymptotic behaviour of errors by this. |
| 88 | constexpr double SAFETY = 0.9; |
| 89 | // Minimum allowed decrease in a step size. |
| 90 | constexpr double MIN_FACTOR = 0.2; |
| 91 | // Maximum allowed increase in a step size. |
| 92 | constexpr double MAX_FACTOR = 10; |
| 93 | |
| 94 | // Final time |
| 95 | const double t_bound = t0 + dt; |
| 96 | |
| 97 | constexpr int order = 5; |
| 98 | constexpr int error_estimator_order = 4; |
| 99 | constexpr int n_stages = 6; |
| 100 | constexpr int states = y0.rows(); |
| 101 | const double sqrt_rows = std::sqrt(static_cast<double>(states)); |
| 102 | const Eigen::Matrix<double, 1, n_stages> C = |
| 103 | (Eigen::Matrix<double, 1, n_stages>() << 0, 1.0 / 5.0, 3.0 / 10.0, |
| 104 | 4.0 / 5.0, 8.0 / 9.0, 1.0) |
| 105 | .finished(); |
| 106 | |
| 107 | const Eigen::Matrix<double, n_stages, order> A = |
| 108 | (Eigen::Matrix<double, n_stages, order>() << 0.0, 0.0, 0.0, 0.0, 0.0, |
| 109 | 1.0 / 5.0, 0.0, 0.0, 0.0, 0.0, 3.0 / 40.0, 9.0 / 40.0, 0.0, 0.0, 0.0, |
| 110 | 44.0 / 45.0, -56.0 / 15.0, 32.0 / 9.0, 0.0, 0.0, 19372.0 / 6561.0, |
| 111 | -25360.0 / 2187.0, 64448.0 / 6561.0, -212.0 / 729.0, 0.0, |
| 112 | 9017.0 / 3168.0, -355.0 / 33.0, 46732.0 / 5247.0, 49.0 / 176.0, |
| 113 | -5103.0 / 18656.0) |
| 114 | .finished(); |
| 115 | |
| 116 | const Eigen::Matrix<double, 1, n_stages> B = |
| 117 | (Eigen::Matrix<double, 1, n_stages>() << 35.0 / 384.0, 0.0, |
| 118 | 500.0 / 1113.0, 125.0 / 192.0, -2187.0 / 6784.0, 11.0 / 84.0) |
| 119 | .finished(); |
| 120 | |
| 121 | const Eigen::Matrix<double, 1, n_stages + 1> E = |
| 122 | (Eigen::Matrix<double, 1, n_stages + 1>() << -71.0 / 57600.0, 0.0, |
| 123 | 71.0 / 16695.0, -71.0 / 1920.0, 17253.0 / 339200.0, -22.0 / 525.0, |
| 124 | 1.0 / 40.0) |
| 125 | .finished(); |
| 126 | |
| 127 | T f = fn(t0, y0); |
| 128 | double h_abs = SelectRungeKuttaInitialStep(fn, t0, y0, f, |
| 129 | error_estimator_order, rtol, atol); |
| 130 | Eigen::Matrix<double, n_stages + 1, states> K; |
| 131 | |
| 132 | Eigen::Matrix<double, states, 1> y = y0; |
| 133 | const double error_exponent = -1.0 / (error_estimator_order + 1.0); |
| 134 | |
| 135 | double t = t0; |
| 136 | while (true) { |
| 137 | if (t >= t_bound) { |
| 138 | return y; |
| 139 | } |
| 140 | |
| 141 | // Step |
| 142 | double min_step = |
| 143 | 10 * (std::nextafter(t, std::numeric_limits<double>::infinity()) - t); |
| 144 | |
| 145 | // TODO(austin): max_step if we care. |
| 146 | if (h_abs < min_step) { |
| 147 | h_abs = min_step; |
| 148 | } |
| 149 | |
| 150 | bool step_accepted = false; |
| 151 | bool step_rejected = false; |
| 152 | |
| 153 | double t_new; |
| 154 | Eigen::Matrix<double, states, 1> y_new; |
| 155 | Eigen::Matrix<double, states, 1> f_new; |
| 156 | while (!step_accepted) { |
| 157 | // TODO(austin): Tell the user rather than just explode? |
| 158 | CHECK_GE(h_abs, min_step); |
| 159 | |
| 160 | double h = h_abs; |
| 161 | t_new = t + h; |
| 162 | if (t_new >= t_bound) { |
| 163 | t_new = t_bound; |
| 164 | } |
| 165 | h = t_new - t; |
| 166 | h_abs = std::abs(h); |
| 167 | |
| 168 | std::tie(y_new, f_new) = |
| 169 | RKStep<states, n_stages, order>(fn, t, y, f, h, A, B, C, K); |
| 170 | |
| 171 | const Eigen::Matrix<double, states, 1> scale = |
| 172 | atol + y.array().abs().max(y_new.array().abs()) * rtol; |
| 173 | |
| 174 | double error_norm = |
| 175 | (((K.transpose() * E.transpose()) * h).array() / scale.array()) |
| 176 | .matrix() |
| 177 | .norm() / |
| 178 | sqrt_rows; |
| 179 | |
| 180 | if (error_norm < 1) { |
| 181 | double factor; |
| 182 | if (error_norm == 0) { |
| 183 | factor = MAX_FACTOR; |
| 184 | } else { |
| 185 | factor = std::min(MAX_FACTOR, |
| 186 | SAFETY * std::pow(error_norm, error_exponent)); |
| 187 | } |
| 188 | |
| 189 | if (step_rejected) { |
| 190 | factor = std::min(1.0, factor); |
| 191 | } |
| 192 | |
| 193 | h_abs *= factor; |
| 194 | |
| 195 | step_accepted = true; |
| 196 | } else { |
| 197 | h_abs *= |
| 198 | std::max(MIN_FACTOR, SAFETY * std::pow(error_norm, error_exponent)); |
| 199 | step_rejected = true; |
| 200 | } |
| 201 | } |
| 202 | |
| 203 | t = t_new; |
| 204 | y = y_new; |
| 205 | f = f_new; |
| 206 | } |
| 207 | |
| 208 | return y; |
| 209 | } |
| 210 | |
Stephan Pleines | d99b1ee | 2024-02-02 20:56:44 -0800 | [diff] [blame] | 211 | } // namespace frc971::control_loops |
Austin Schuh | acd335a | 2017-01-01 16:20:54 -0800 | [diff] [blame] | 212 | |
| 213 | #endif // FRC971_CONTROL_LOOPS_RUNGE_KUTTA_H_ |