Austin Schuh | a647d60 | 2018-02-18 14:05:15 -0800 | [diff] [blame] | 1 | #ifndef FRC971_CONTROL_LOOPS_JACOBIAN_H_ |
| 2 | #define FRC971_CONTROL_LOOPS_JACOBIAN_H_ |
| 3 | |
| 4 | #include <Eigen/Dense> |
| 5 | |
| 6 | namespace frc971 { |
| 7 | namespace control_loops { |
| 8 | |
| 9 | template <int num_states, int num_inputs, typename F> |
| 10 | ::Eigen::Matrix<double, num_states, num_inputs> NumericalJacobian( |
| 11 | const F &fn, ::Eigen::Matrix<double, num_inputs, 1> input) { |
Austin Schuh | cb09171 | 2018-02-21 20:01:55 -0800 | [diff] [blame] | 12 | constexpr double kEpsilon = 1e-5; |
Austin Schuh | a647d60 | 2018-02-18 14:05:15 -0800 | [diff] [blame] | 13 | ::Eigen::Matrix<double, num_states, num_inputs> result = |
| 14 | ::Eigen::Matrix<double, num_states, num_inputs>::Zero(); |
| 15 | |
| 16 | // It's more expensive, but +- epsilon will be more accurate |
| 17 | for (int i = 0; i < num_inputs; ++i) { |
| 18 | ::Eigen::Matrix<double, num_inputs, 1> dX_plus = input; |
| 19 | dX_plus(i, 0) += kEpsilon; |
| 20 | ::Eigen::Matrix<double, num_inputs, 1> dX_minus = input; |
| 21 | dX_minus(i, 0) -= kEpsilon; |
| 22 | result.col(i) = (fn(dX_plus) - fn(dX_minus)) / (kEpsilon * 2.0); |
| 23 | } |
| 24 | return result; |
| 25 | } |
| 26 | |
| 27 | // Implements a numerical jacobian with respect to X for f(X, U, ...). |
| 28 | template <int num_states, int num_u, typename F, typename... Args> |
| 29 | ::Eigen::Matrix<double, num_states, num_states> NumericalJacobianX( |
| 30 | const F &fn, ::Eigen::Matrix<double, num_states, 1> X, |
Philipp Schrader | 790cb54 | 2023-07-05 21:06:52 -0700 | [diff] [blame^] | 31 | ::Eigen::Matrix<double, num_u, 1> U, Args &&...args) { |
Austin Schuh | a647d60 | 2018-02-18 14:05:15 -0800 | [diff] [blame] | 32 | return NumericalJacobian<num_states, num_states>( |
| 33 | [&](::Eigen::Matrix<double, num_states, 1> X) { |
| 34 | return fn(X, U, args...); |
| 35 | }, |
| 36 | X); |
| 37 | } |
| 38 | |
| 39 | // Implements a numerical jacobian with respect to U for f(X, U, ...). |
| 40 | template <int num_states, int num_u, typename F, typename... Args> |
| 41 | ::Eigen::Matrix<double, num_states, num_u> NumericalJacobianU( |
| 42 | const F &fn, ::Eigen::Matrix<double, num_states, 1> X, |
Philipp Schrader | 790cb54 | 2023-07-05 21:06:52 -0700 | [diff] [blame^] | 43 | ::Eigen::Matrix<double, num_u, 1> U, Args &&...args) { |
Austin Schuh | a647d60 | 2018-02-18 14:05:15 -0800 | [diff] [blame] | 44 | return NumericalJacobian<num_states, num_u>( |
| 45 | [&](::Eigen::Matrix<double, num_u, 1> U) { return fn(X, U, args...); }, |
| 46 | U); |
| 47 | } |
| 48 | |
| 49 | } // namespace control_loops |
| 50 | } // namespace frc971 |
| 51 | |
| 52 | #endif // FRC971_CONTROL_LOOPS_JACOBIAN_H_ |