James Kuszmaul | 6175066 | 2021-06-21 21:32:33 -0700 | [diff] [blame] | 1 | #include "frc971/control_loops/quaternion_utils.h" |
Brian Silverman | dac0a4b | 2020-06-23 17:03:09 -0700 | [diff] [blame] | 2 | |
| 3 | #include "Eigen/Dense" |
| 4 | #include "Eigen/Geometry" |
| 5 | |
Stephan Pleines | f63bde8 | 2024-01-13 15:59:33 -0800 | [diff] [blame^] | 6 | namespace frc971::controls { |
milind-u | e53bf55 | 2021-12-11 14:42:00 -0800 | [diff] [blame] | 7 | namespace { |
Brian Silverman | dac0a4b | 2020-06-23 17:03:09 -0700 | [diff] [blame] | 8 | |
milind-u | e53bf55 | 2021-12-11 14:42:00 -0800 | [diff] [blame] | 9 | double SinXoverX(double x) { |
| 10 | const double xsquared = x * x; |
Brian Silverman | dac0a4b | 2020-06-23 17:03:09 -0700 | [diff] [blame] | 11 | |
| 12 | // sin(x)/x = 1 |
| 13 | double sinx_x = 1.0; |
| 14 | |
| 15 | // - x^2/3! |
milind-u | e53bf55 | 2021-12-11 14:42:00 -0800 | [diff] [blame] | 16 | double value = xsquared / 6.0; |
Brian Silverman | dac0a4b | 2020-06-23 17:03:09 -0700 | [diff] [blame] | 17 | sinx_x -= value; |
| 18 | |
| 19 | // + x^4/5! |
milind-u | e53bf55 | 2021-12-11 14:42:00 -0800 | [diff] [blame] | 20 | value = value * xsquared / 20.0; |
Brian Silverman | dac0a4b | 2020-06-23 17:03:09 -0700 | [diff] [blame] | 21 | sinx_x += value; |
| 22 | |
| 23 | // - x^6/7! |
milind-u | e53bf55 | 2021-12-11 14:42:00 -0800 | [diff] [blame] | 24 | value = value * xsquared / (6.0 * 7.0); |
Brian Silverman | dac0a4b | 2020-06-23 17:03:09 -0700 | [diff] [blame] | 25 | sinx_x -= value; |
| 26 | |
| 27 | // + x^8/9! |
milind-u | e53bf55 | 2021-12-11 14:42:00 -0800 | [diff] [blame] | 28 | value = value * xsquared / (8.0 * 9.0); |
Brian Silverman | dac0a4b | 2020-06-23 17:03:09 -0700 | [diff] [blame] | 29 | sinx_x += value; |
| 30 | |
| 31 | // - x^10/11! |
milind-u | e53bf55 | 2021-12-11 14:42:00 -0800 | [diff] [blame] | 32 | value = value * xsquared / (10.0 * 11.0); |
Brian Silverman | dac0a4b | 2020-06-23 17:03:09 -0700 | [diff] [blame] | 33 | sinx_x -= value; |
| 34 | |
| 35 | // + x^12/13! |
milind-u | e53bf55 | 2021-12-11 14:42:00 -0800 | [diff] [blame] | 36 | value = value * xsquared / (12.0 * 13.0); |
Brian Silverman | dac0a4b | 2020-06-23 17:03:09 -0700 | [diff] [blame] | 37 | sinx_x += value; |
| 38 | |
| 39 | // - x^14/15! |
milind-u | e53bf55 | 2021-12-11 14:42:00 -0800 | [diff] [blame] | 40 | value = value * xsquared / (14.0 * 15.0); |
Brian Silverman | dac0a4b | 2020-06-23 17:03:09 -0700 | [diff] [blame] | 41 | sinx_x -= value; |
| 42 | |
| 43 | // + x^16/17! |
milind-u | e53bf55 | 2021-12-11 14:42:00 -0800 | [diff] [blame] | 44 | value = value * xsquared / (16.0 * 17.0); |
Brian Silverman | dac0a4b | 2020-06-23 17:03:09 -0700 | [diff] [blame] | 45 | sinx_x += value; |
| 46 | |
milind-u | e53bf55 | 2021-12-11 14:42:00 -0800 | [diff] [blame] | 47 | return sinx_x; |
| 48 | |
Brian Silverman | dac0a4b | 2020-06-23 17:03:09 -0700 | [diff] [blame] | 49 | // To plot the residual in matplotlib, run: |
| 50 | // import numpy |
| 51 | // import scipy |
| 52 | // from matplotlib import pyplot |
| 53 | // x = numpy.arange(-numpy.pi, numpy.pi, 0.01) |
| 54 | // pyplot.plot(x, 1 - x**2 / scipy.misc.factorial(3) + |
| 55 | // x**4 / scipy.misc.factorial(5) - |
| 56 | // x**6 / scipy.misc.factorial(7) + |
| 57 | // x**8 / scipy.misc.factorial(9) - |
| 58 | // x ** 10 / scipy.misc.factorial(11) + |
| 59 | // x ** 12 / scipy.misc.factorial(13) - |
| 60 | // x ** 14 / scipy.misc.factorial(15) + |
| 61 | // x ** 16 / scipy.misc.factorial(17) - |
| 62 | // numpy.sin(x) / x) |
Brian Silverman | dac0a4b | 2020-06-23 17:03:09 -0700 | [diff] [blame] | 63 | } |
| 64 | |
milind-u | e53bf55 | 2021-12-11 14:42:00 -0800 | [diff] [blame] | 65 | } // namespace |
| 66 | |
Brian Silverman | dac0a4b | 2020-06-23 17:03:09 -0700 | [diff] [blame] | 67 | inline Eigen::Matrix<double, 4, 1> MaybeFlipX( |
| 68 | const Eigen::Matrix<double, 4, 1> &X) { |
| 69 | if (X(3, 0) < 0.0) { |
| 70 | return -X; |
| 71 | } else { |
| 72 | return X; |
| 73 | } |
| 74 | } |
| 75 | |
milind-u | e53bf55 | 2021-12-11 14:42:00 -0800 | [diff] [blame] | 76 | Eigen::Matrix<double, 4, 1> ToQuaternionFromRotationVector( |
| 77 | const Eigen::Matrix<double, 3, 1> &X, const double max_angle_cap) { |
| 78 | const double unclipped_angle = X.norm(); |
| 79 | const double angle_scalar = |
| 80 | (unclipped_angle > max_angle_cap) ? max_angle_cap / unclipped_angle : 1.0; |
| 81 | const double angle = unclipped_angle * angle_scalar; |
| 82 | const double half_angle = angle * 0.5; |
| 83 | |
| 84 | const double scalar = SinXoverX(half_angle) * 0.5; |
| 85 | |
| 86 | Eigen::Matrix<double, 4, 1> result; |
| 87 | result.block<3, 1>(0, 0) = X * scalar * angle_scalar; |
| 88 | result(3, 0) = std::cos(half_angle); |
| 89 | return result; |
| 90 | } |
| 91 | |
| 92 | // q = cos(a/2) + i ( x * sin(a/2)) + j (y * sin(a/2)) + k ( z * sin(a/2)) |
| 93 | |
Brian Silverman | dac0a4b | 2020-06-23 17:03:09 -0700 | [diff] [blame] | 94 | Eigen::Matrix<double, 3, 1> ToRotationVectorFromQuaternion( |
| 95 | const Eigen::Matrix<double, 4, 1> &X) { |
| 96 | // TODO(austin): Verify we still need it. |
| 97 | const Eigen::Matrix<double, 4, 1> corrected_X = MaybeFlipX(X); |
| 98 | const double half_angle = |
| 99 | std::atan2(corrected_X.block<3, 1>(0, 0).norm(), corrected_X(3, 0)); |
| 100 | |
milind-u | e53bf55 | 2021-12-11 14:42:00 -0800 | [diff] [blame] | 101 | const double scalar = 2.0 / SinXoverX(half_angle); |
Brian Silverman | dac0a4b | 2020-06-23 17:03:09 -0700 | [diff] [blame] | 102 | |
| 103 | return corrected_X.block<3, 1>(0, 0) * scalar; |
| 104 | } |
| 105 | |
milind-u | e53bf55 | 2021-12-11 14:42:00 -0800 | [diff] [blame] | 106 | Eigen::Matrix<double, 4, 3> QuaternionDerivativeDerivitive( |
| 107 | const Eigen::Vector4d &q_matrix) { |
| 108 | // qa * qb = (a.w() * b.x() + a.x() * b.w() + a.y() * b.z() - a.z() * b.y(), |
| 109 | // a.w() * b.y() + a.y() * b.w() + a.z() * b.x() - a.x() * b.z(), |
| 110 | // a.w() * b.z() + a.z() * b.w() + a.x() * b.y() - a.y() * b.x(), |
| 111 | // a.w() * b.w() - a.x() * b.x() - a.y() * b.y() - a.z() * b.z()) |
| 112 | // |
| 113 | // We want q * omega_q = result * omega. |
| 114 | Eigen::Matrix<double, 4, 3> result; |
| 115 | result(3, 0) = -q_matrix.x() * 0.5; |
| 116 | result(3, 1) = -q_matrix.y() * 0.5; |
| 117 | result(3, 2) = -q_matrix.z() * 0.5; |
| 118 | |
| 119 | result(0, 0) = q_matrix.w() * 0.5; |
| 120 | result(0, 1) = -q_matrix.z() * 0.5; |
| 121 | result(0, 2) = q_matrix.y() * 0.5; |
| 122 | |
| 123 | result(1, 0) = q_matrix.z() * 0.5; |
| 124 | result(1, 1) = q_matrix.w() * 0.5; |
| 125 | result(1, 2) = -q_matrix.x() * 0.5; |
| 126 | |
| 127 | result(2, 0) = -q_matrix.y() * 0.5; |
| 128 | result(2, 1) = q_matrix.x() * 0.5; |
| 129 | result(2, 2) = q_matrix.w() * 0.5; |
| 130 | |
| 131 | return result; |
| 132 | } |
| 133 | |
Stephan Pleines | f63bde8 | 2024-01-13 15:59:33 -0800 | [diff] [blame^] | 134 | } // namespace frc971::controls |