|
2 | 2 | #define VORTEX_UTILS_HPP |
3 | 3 |
|
4 | 4 | #include <cmath> |
| 5 | +#include <eigen3/Eigen/Dense> |
| 6 | + |
| 7 | +typedef Eigen::Matrix<double, 6, 6> Matrix6d; |
| 8 | +typedef Eigen::Matrix<double, 3, 3> Matrix3d; |
| 9 | +typedef Eigen::Matrix<double, 6, 1> Vector6d; |
5 | 10 |
|
6 | 11 | namespace vortex_utils |
7 | 12 | { |
8 | 13 | // @brief Function to calculate the smallest signed angle between two angles |
9 | 14 | double ssa(const double angle); |
| 15 | + |
| 16 | + struct Eta { |
| 17 | + double x = 0.0; |
| 18 | + double y = 0.0; |
| 19 | + double z = 0.0; |
| 20 | + double roll = 0.0; |
| 21 | + double pitch = 0.0; |
| 22 | + double yaw = 0.0; |
| 23 | + |
| 24 | + Eta operator-(const Eta& other) const { |
| 25 | + Eta eta; |
| 26 | + eta.x = x - other.x; |
| 27 | + eta.y = y - other.y; |
| 28 | + eta.z = z - other.z; |
| 29 | + eta.roll = roll - other.roll; |
| 30 | + eta.pitch = pitch - other.pitch; |
| 31 | + eta.yaw = yaw - other.yaw; |
| 32 | + return eta; |
| 33 | + } |
| 34 | + |
| 35 | + Vector6d to_vector() const { |
| 36 | + Vector6d eta; |
| 37 | + eta << x, y, z, roll, pitch, yaw; |
| 38 | + return eta; |
| 39 | + } |
| 40 | + |
| 41 | + // @brief Make the rotation matrix according to eq. 2.31 in Fossen, 2021 |
| 42 | + Matrix3d as_rotation_matrix() const { |
| 43 | + Eigen::AngleAxisd rollAngle(roll, Eigen::Vector3d::UnitX()); |
| 44 | + Eigen::AngleAxisd pitchAngle(pitch, Eigen::Vector3d::UnitY()); |
| 45 | + Eigen::AngleAxisd yawAngle(yaw, Eigen::Vector3d::UnitZ()); |
| 46 | + |
| 47 | + Eigen::Quaterniond q = yawAngle * pitchAngle * rollAngle; |
| 48 | + Matrix3d rotation_matrix = q.toRotationMatrix(); |
| 49 | + |
| 50 | + return rotation_matrix; |
| 51 | + } |
| 52 | + |
| 53 | + // @brief Make the transformation matrix according to eq. 2.41 in Fossen, |
| 54 | + // 2021 |
| 55 | + Matrix3d as_transformation_matrix() const { |
| 56 | + double cphi = cos(roll); |
| 57 | + double sphi = sin(roll); |
| 58 | + double ctheta = cos(pitch); |
| 59 | + double stheta = sin(pitch); |
| 60 | + |
| 61 | + if (ctheta == 0) { |
| 62 | + throw std::runtime_error( |
| 63 | + "Division by zero in transformation matrix."); |
| 64 | + } |
| 65 | + |
| 66 | + double t11 = 1; |
| 67 | + double t12 = sphi * stheta / ctheta; |
| 68 | + double t13 = cphi * stheta / ctheta; |
| 69 | + double t21 = 0; |
| 70 | + double t22 = cphi; |
| 71 | + double t23 = -sphi; |
| 72 | + double t31 = 0; |
| 73 | + double t32 = sphi / ctheta; |
| 74 | + double t33 = cphi / ctheta; |
| 75 | + |
| 76 | + Matrix3d transformation_matrix; |
| 77 | + |
| 78 | + transformation_matrix << t11, t12, t13, |
| 79 | + t21, t22, t23, |
| 80 | + t31, t32, t33; |
| 81 | + |
| 82 | + return transformation_matrix; |
| 83 | + } |
| 84 | + }; |
| 85 | + |
| 86 | + struct Nu { |
| 87 | + double u = 0.0; |
| 88 | + double v = 0.0; |
| 89 | + double w = 0.0; |
| 90 | + double p = 0.0; |
| 91 | + double q = 0.0; |
| 92 | + double r = 0.0; |
| 93 | + |
| 94 | + Nu operator-(const Nu& other) const { |
| 95 | + Nu nu; |
| 96 | + nu.u = u - other.u; |
| 97 | + nu.v = v - other.v; |
| 98 | + nu.w = w - other.w; |
| 99 | + nu.p = p - other.p; |
| 100 | + nu.q = q - other.q; |
| 101 | + nu.r = r - other.r; |
| 102 | + return nu; |
| 103 | + } |
| 104 | + |
| 105 | + Vector6d to_vector() const { |
| 106 | + Vector6d nu; |
| 107 | + nu << u, v, w, p, q, r; |
| 108 | + return nu; |
| 109 | + } |
| 110 | + }; |
10 | 111 | } // namespace vortex_utils |
11 | 112 |
|
12 | 113 | #endif // VORTEX_UTILS_HPP |
0 commit comments