Austin Schuh | c2b0877 | 2018-12-19 18:05:06 +1100 | [diff] [blame] | 1 | #ifndef FRC971_CONTROL_LOOPS_DRIVETRAIN_SPLINE_H_ |
| 2 | #define FRC971_CONTROL_LOOPS_DRIVETRAIN_SPLINE_H_ |
| 3 | |
| 4 | #include "Eigen/Dense" |
| 5 | |
Austin Schuh | f49b4e3 | 2019-01-13 17:26:58 -0800 | [diff] [blame] | 6 | #include "frc971/control_loops/binomial.h" |
| 7 | |
Stephan Pleines | d99b1ee | 2024-02-02 20:56:44 -0800 | [diff] [blame^] | 8 | namespace frc971::control_loops::drivetrain { |
Austin Schuh | c2b0877 | 2018-12-19 18:05:06 +1100 | [diff] [blame] | 9 | |
| 10 | // Class to hold a spline as a function of alpha. Alpha can only range between |
| 11 | // 0.0 and 1.0. |
Austin Schuh | f49b4e3 | 2019-01-13 17:26:58 -0800 | [diff] [blame] | 12 | template <int N> |
| 13 | class NSpline { |
Austin Schuh | c2b0877 | 2018-12-19 18:05:06 +1100 | [diff] [blame] | 14 | public: |
| 15 | EIGEN_MAKE_ALIGNED_OPERATOR_NEW |
| 16 | |
Alex Perry | 7092150 | 2019-01-27 17:31:46 -0800 | [diff] [blame] | 17 | // Computes the characteristic matrix of a given spline order. This is an |
| 18 | // upper triangular matrix rather than lower because our splines are |
| 19 | // represented as rows rather than columns. |
| 20 | // Each row represents the impact of each point with increasing powers of |
| 21 | // alpha. Row i, column j contains the effect point i has with the j'th power |
| 22 | // of alpha. |
| 23 | static ::Eigen::Matrix<double, N, N> SplineMatrix() { |
| 24 | ::Eigen::Matrix<double, N, N> matrix = |
| 25 | ::Eigen::Matrix<double, N, N>::Zero(); |
| 26 | |
Austin Schuh | f49b4e3 | 2019-01-13 17:26:58 -0800 | [diff] [blame] | 27 | for (int i = 0; i < N; ++i) { |
| 28 | // Binomial(N - 1, i) * (1 - t) ^ (N - i - 1) * (t ^ i) * P[i] |
| 29 | const double binomial = Binomial(N - 1, i); |
| 30 | const int one_minus_t_power = N - i - 1; |
Alex Perry | 7092150 | 2019-01-27 17:31:46 -0800 | [diff] [blame] | 31 | |
| 32 | // Then iterate over the powers of t and add the pieces to the matrix. |
Austin Schuh | f49b4e3 | 2019-01-13 17:26:58 -0800 | [diff] [blame] | 33 | for (int j = i; j < N; ++j) { |
Alex Perry | 7092150 | 2019-01-27 17:31:46 -0800 | [diff] [blame] | 34 | // j is the power of t we are placing in the matrix. |
Austin Schuh | f49b4e3 | 2019-01-13 17:26:58 -0800 | [diff] [blame] | 35 | // k is the power of t in the (1 - t) expression that we need to |
| 36 | // evaluate. |
| 37 | const int k = j - i; |
| 38 | const double tscalar = |
| 39 | binomial * Binomial(one_minus_t_power, k) * ::std::pow(-1.0, k); |
Alex Perry | 7092150 | 2019-01-27 17:31:46 -0800 | [diff] [blame] | 40 | matrix(i, j) = tscalar; |
Austin Schuh | f49b4e3 | 2019-01-13 17:26:58 -0800 | [diff] [blame] | 41 | } |
| 42 | } |
Alex Perry | 7092150 | 2019-01-27 17:31:46 -0800 | [diff] [blame] | 43 | return matrix; |
| 44 | } |
| 45 | |
| 46 | // Computes the matrix to multiply by [1, a, a^2, ...] to evaluate the spline. |
| 47 | template <int D> |
| 48 | static ::Eigen::Matrix<double, 2, N - D> SplinePolynomial( |
| 49 | const ::Eigen::Matrix<double, 2, N> &control_points) { |
| 50 | // We use rows for the spline, so the multiplication looks "backwards" |
| 51 | ::Eigen::Matrix<double, 2, N> polynomial = control_points * SplineMatrix(); |
Austin Schuh | f49b4e3 | 2019-01-13 17:26:58 -0800 | [diff] [blame] | 52 | |
| 53 | // Now, compute the derivative requested. |
| 54 | for (int i = D; i < N; ++i) { |
| 55 | // Start with t^i, and multiply i, i-1, i-2 until you have done this |
| 56 | // Derivative times. |
| 57 | double scalar = 1.0; |
| 58 | for (int j = i; j > i - D; --j) { |
| 59 | scalar *= j; |
| 60 | } |
| 61 | polynomial.template block<2, 1>(0, i) = |
| 62 | polynomial.template block<2, 1>(0, i) * scalar; |
| 63 | } |
| 64 | return polynomial.template block<2, N - D>(0, D); |
| 65 | } |
| 66 | |
| 67 | // Computes an order M-1 polynomial matrix in alpha. [1, alpha, alpha^2, ...] |
| 68 | template <int M> |
| 69 | static ::Eigen::Matrix<double, M, 1> AlphaPolynomial(const double alpha) { |
| 70 | ::Eigen::Matrix<double, M, 1> polynomial = |
| 71 | ::Eigen::Matrix<double, M, 1>::Zero(); |
| 72 | polynomial(0) = 1.0; |
| 73 | for (int i = 1; i < M; ++i) { |
| 74 | polynomial(i) = polynomial(i - 1) * alpha; |
| 75 | } |
| 76 | return polynomial; |
| 77 | } |
| 78 | |
Austin Schuh | c2b0877 | 2018-12-19 18:05:06 +1100 | [diff] [blame] | 79 | // Constructs a spline. control_points is a matrix of start, control1, |
Austin Schuh | f49b4e3 | 2019-01-13 17:26:58 -0800 | [diff] [blame] | 80 | // control2, ..., end. |
| 81 | NSpline(::Eigen::Matrix<double, 2, N> control_points) |
| 82 | : control_points_(control_points), |
| 83 | spline_polynomial_(SplinePolynomial<0>(control_points_)), |
| 84 | dspline_polynomial_(SplinePolynomial<1>(control_points_)), |
| 85 | ddspline_polynomial_(SplinePolynomial<2>(control_points_)), |
| 86 | dddspline_polynomial_(SplinePolynomial<3>(control_points_)) {} |
Austin Schuh | c2b0877 | 2018-12-19 18:05:06 +1100 | [diff] [blame] | 87 | |
| 88 | // Returns the xy coordiate of the spline for a given alpha. |
| 89 | ::Eigen::Matrix<double, 2, 1> Point(double alpha) const { |
Austin Schuh | f49b4e3 | 2019-01-13 17:26:58 -0800 | [diff] [blame] | 90 | return spline_polynomial_ * AlphaPolynomial<N>(alpha); |
Austin Schuh | c2b0877 | 2018-12-19 18:05:06 +1100 | [diff] [blame] | 91 | } |
| 92 | |
| 93 | // Returns the dspline/dalpha for a given alpha. |
| 94 | ::Eigen::Matrix<double, 2, 1> DPoint(double alpha) const { |
Austin Schuh | f49b4e3 | 2019-01-13 17:26:58 -0800 | [diff] [blame] | 95 | return dspline_polynomial_ * AlphaPolynomial<N - 1>(alpha); |
Austin Schuh | c2b0877 | 2018-12-19 18:05:06 +1100 | [diff] [blame] | 96 | } |
| 97 | |
| 98 | // Returns the d^2spline/dalpha^2 for a given alpha. |
| 99 | ::Eigen::Matrix<double, 2, 1> DDPoint(double alpha) const { |
Austin Schuh | f49b4e3 | 2019-01-13 17:26:58 -0800 | [diff] [blame] | 100 | return ddspline_polynomial_ * AlphaPolynomial<N - 2>(alpha); |
Austin Schuh | c2b0877 | 2018-12-19 18:05:06 +1100 | [diff] [blame] | 101 | } |
| 102 | |
| 103 | // Returns the d^3spline/dalpha^3 for a given alpha. |
Austin Schuh | f49b4e3 | 2019-01-13 17:26:58 -0800 | [diff] [blame] | 104 | ::Eigen::Matrix<double, 2, 1> DDDPoint(double alpha) const { |
| 105 | return dddspline_polynomial_ * AlphaPolynomial<N - 3>(alpha); |
Austin Schuh | c2b0877 | 2018-12-19 18:05:06 +1100 | [diff] [blame] | 106 | } |
| 107 | |
| 108 | // Returns theta for a given alpha. |
Austin Schuh | f49b4e3 | 2019-01-13 17:26:58 -0800 | [diff] [blame] | 109 | double Theta(double alpha) const { |
| 110 | const ::Eigen::Matrix<double, 2, 1> dp = DPoint(alpha); |
| 111 | return ::std::atan2(dp(1), dp(0)); |
| 112 | } |
Austin Schuh | c2b0877 | 2018-12-19 18:05:06 +1100 | [diff] [blame] | 113 | |
| 114 | // Returns dtheta/dalpha for a given alpha. |
Austin Schuh | f49b4e3 | 2019-01-13 17:26:58 -0800 | [diff] [blame] | 115 | double DTheta(double alpha) const { |
| 116 | const ::Eigen::Matrix<double, N - 1, 1> alpha_polynomial = |
| 117 | AlphaPolynomial<N - 1>(alpha); |
| 118 | |
| 119 | const ::Eigen::Matrix<double, 2, 1> dp = |
| 120 | dspline_polynomial_ * alpha_polynomial; |
| 121 | const ::Eigen::Matrix<double, 2, 1> ddp = |
| 122 | ddspline_polynomial_ * alpha_polynomial.template block<N - 2, 1>(0, 0); |
| 123 | const double dx = dp(0); |
| 124 | const double dy = dp(1); |
| 125 | |
| 126 | const double ddx = ddp(0); |
| 127 | const double ddy = ddp(1); |
| 128 | |
| 129 | return 1.0 / (::std::pow(dx, 2) + ::std::pow(dy, 2)) * |
| 130 | (dx * ddy - dy * ddx); |
| 131 | } |
Austin Schuh | c2b0877 | 2018-12-19 18:05:06 +1100 | [diff] [blame] | 132 | |
| 133 | // Returns d^2 theta/dalpha^2 for a given alpha. |
Austin Schuh | f49b4e3 | 2019-01-13 17:26:58 -0800 | [diff] [blame] | 134 | double DDTheta(double alpha) const { |
| 135 | const ::Eigen::Matrix<double, N - 1, 1> alpha_polynomial = |
| 136 | AlphaPolynomial<N - 1>(alpha); |
| 137 | const ::Eigen::Matrix<double, 2, 1> dp = |
| 138 | dspline_polynomial_ * alpha_polynomial; |
| 139 | const ::Eigen::Matrix<double, 2, 1> ddp = |
| 140 | ddspline_polynomial_ * alpha_polynomial.template block<N - 2, 1>(0, 0); |
| 141 | const ::Eigen::Matrix<double, 2, 1> dddp = |
| 142 | dddspline_polynomial_ * alpha_polynomial.template block<N - 3, 1>(0, 0); |
| 143 | const double dx = dp(0); |
| 144 | const double dy = dp(1); |
| 145 | |
| 146 | const double ddx = ddp(0); |
| 147 | const double ddy = ddp(1); |
| 148 | |
| 149 | const double dddx = dddp(0); |
| 150 | const double dddy = dddp(1); |
| 151 | |
| 152 | const double magdxy2 = ::std::pow(dx, 2) + ::std::pow(dy, 2); |
| 153 | |
| 154 | return -1.0 / (::std::pow(magdxy2, 2)) * (dx * ddy - dy * ddx) * 2.0 * |
| 155 | (dy * ddy + dx * ddx) + |
| 156 | 1.0 / magdxy2 * (dx * dddy - dy * dddx); |
| 157 | } |
Austin Schuh | c2b0877 | 2018-12-19 18:05:06 +1100 | [diff] [blame] | 158 | |
Austin Schuh | b23f525 | 2019-01-13 21:16:23 -0800 | [diff] [blame] | 159 | const ::Eigen::Matrix<double, 2, N> &control_points() const { |
| 160 | return control_points_; |
| 161 | } |
| 162 | |
Austin Schuh | c2b0877 | 2018-12-19 18:05:06 +1100 | [diff] [blame] | 163 | private: |
Austin Schuh | f49b4e3 | 2019-01-13 17:26:58 -0800 | [diff] [blame] | 164 | const ::Eigen::Matrix<double, 2, N> control_points_; |
| 165 | |
| 166 | // Each of these polynomials gets multiplied by [x^(n-1), x^(n-2), ..., x, 1] |
| 167 | // depending on the size of the polynomial. |
| 168 | const ::Eigen::Matrix<double, 2, N> spline_polynomial_; |
| 169 | const ::Eigen::Matrix<double, 2, N - 1> dspline_polynomial_; |
| 170 | const ::Eigen::Matrix<double, 2, N - 2> ddspline_polynomial_; |
| 171 | const ::Eigen::Matrix<double, 2, N - 3> dddspline_polynomial_; |
Austin Schuh | c2b0877 | 2018-12-19 18:05:06 +1100 | [diff] [blame] | 172 | }; |
| 173 | |
Austin Schuh | 280996e | 2019-01-19 17:43:37 -0800 | [diff] [blame] | 174 | typedef NSpline<6> Spline; |
Austin Schuh | f49b4e3 | 2019-01-13 17:26:58 -0800 | [diff] [blame] | 175 | |
Austin Schuh | b23f525 | 2019-01-13 21:16:23 -0800 | [diff] [blame] | 176 | // Converts a 4 control point spline into |
| 177 | ::Eigen::Matrix<double, 2, 6> Spline4To6( |
| 178 | const ::Eigen::Matrix<double, 2, 4> &control_points); |
| 179 | |
Austin Schuh | 280996e | 2019-01-19 17:43:37 -0800 | [diff] [blame] | 180 | template <int N> |
| 181 | ::Eigen::Matrix<double, 2, N> TranslateSpline( |
| 182 | const ::Eigen::Matrix<double, 2, N> &control_points, |
| 183 | const ::Eigen::Matrix<double, 2, 1> translation) { |
| 184 | ::Eigen::Matrix<double, 2, N> ans = control_points; |
| 185 | for (size_t i = 0; i < N; ++i) { |
| 186 | ans.template block<2, 1>(0, i) += translation; |
| 187 | } |
| 188 | return ans; |
| 189 | } |
| 190 | |
Stephan Pleines | d99b1ee | 2024-02-02 20:56:44 -0800 | [diff] [blame^] | 191 | } // namespace frc971::control_loops::drivetrain |
Austin Schuh | c2b0877 | 2018-12-19 18:05:06 +1100 | [diff] [blame] | 192 | |
| 193 | #endif // FRC971_CONTROL_LOOPS_DRIVETRAIN_SPLINE_H_ |