You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

174 lines
5.5 KiB

7 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. #include "MAVLINK.h"
  2. #include <math.h>
  3. /**
  4. * Converts a quaternion to a rotation matrix
  5. *
  6. * @param quaternion a [w, x, y, z] ordered quaternion (null-rotation being 1 0 0 0)
  7. * @param dcm a 3x3 rotation matrix
  8. */
  9. void mavlink_quaternion_to_dcm(const float quaternion[4], float &dcm[3][3]){
  10. float a = quaternion[0];
  11. float b = quaternion[1];
  12. float c = quaternion[2];
  13. float d = quaternion[3];
  14. float aSq = a * a;
  15. float bSq = b * b;
  16. float cSq = c * c;
  17. float dSq = d * d;
  18. dcm[0][0] = aSq + bSq - cSq - dSq;
  19. dcm[0][1] = 2 * (b * c - a * d);
  20. dcm[0][2] = 2 * (a * c + b * d);
  21. dcm[1][0] = 2 * (b * c + a * d);
  22. dcm[1][1] = aSq - bSq + cSq - dSq;
  23. dcm[1][2] = 2 * (c * d - a * b);
  24. dcm[2][0] = 2 * (b * d - a * c);
  25. dcm[2][1] = 2 * (a * b + c * d);
  26. dcm[2][2] = aSq - bSq - cSq + dSq;
  27. };
  28. /**
  29. * Converts a rotation matrix to euler angles
  30. *
  31. * @param dcm a 3x3 rotation matrix
  32. * @param roll the roll angle in radians
  33. * @param pitch the pitch angle in radians
  34. * @param yaw the yaw angle in radians
  35. */
  36. void mavlink_dcm_to_euler(const float dcm[3][3], float* roll, float* pitch, float* yaw){
  37. float phi, theta, psi;
  38. theta = asinf(-dcm[2][0]);
  39. if (fabsf(theta - M_PI_2) < 1.0e-3f) {
  40. phi = 0.0f;
  41. psi = (atan2f(dcm[1][2] - dcm[0][1],
  42. dcm[0][2] + dcm[1][1]) + phi);
  43. } else if (fabsf(theta + M_PI_2) < 1.0e-3f) {
  44. phi = 0.0f;
  45. psi = atan2f(dcm[1][2] - dcm[0][1],
  46. dcm[0][2] + dcm[1][1] - phi);
  47. } else {
  48. phi = atan2f(dcm[2][1], dcm[2][2]);
  49. psi = atan2f(dcm[1][0], dcm[0][0]);
  50. }
  51. *roll = phi;
  52. *pitch = theta;
  53. *yaw = psi;
  54. };
  55. /**
  56. * Converts a quaternion to euler angles
  57. *
  58. * @param quaternion a [w, x, y, z] ordered quaternion (null-rotation being 1 0 0 0)
  59. * @param roll the roll angle in radians
  60. * @param pitch the pitch angle in radians
  61. * @param yaw the yaw angle in radians
  62. */
  63. void mavlink_quaternion_to_euler(const float quaternion[4], float* roll, float* pitch, float* yaw){
  64. float dcm[3][3];
  65. mavlink_quaternion_to_dcm(quaternion, dcm);
  66. mavlink_dcm_to_euler((const float(*)[3])dcm, roll, pitch, yaw);
  67. };
  68. /**
  69. * Converts euler angles to a quaternion
  70. *
  71. * @param roll the roll angle in radians
  72. * @param pitch the pitch angle in radians
  73. * @param yaw the yaw angle in radians
  74. * @param quaternion a [w, x, y, z] ordered quaternion (null-rotation being 1 0 0 0)
  75. */
  76. void mavlink_euler_to_quaternion(float roll, float pitch, float yaw, float quaternion[4]){
  77. float cosPhi_2 = cosf(roll / 2);
  78. float sinPhi_2 = sinf(roll / 2);
  79. float cosTheta_2 = cosf(pitch / 2);
  80. float sinTheta_2 = sinf(pitch / 2);
  81. float cosPsi_2 = cosf(yaw / 2);
  82. float sinPsi_2 = sinf(yaw / 2);
  83. quaternion[0] = (cosPhi_2 * cosTheta_2 * cosPsi_2 +
  84. sinPhi_2 * sinTheta_2 * sinPsi_2);
  85. quaternion[1] = (sinPhi_2 * cosTheta_2 * cosPsi_2 -
  86. cosPhi_2 * sinTheta_2 * sinPsi_2);
  87. quaternion[2] = (cosPhi_2 * sinTheta_2 * cosPsi_2 +
  88. sinPhi_2 * cosTheta_2 * sinPsi_2);
  89. quaternion[3] = (cosPhi_2 * cosTheta_2 * sinPsi_2 -
  90. sinPhi_2 * sinTheta_2 * cosPsi_2);
  91. };
  92. /**
  93. * Converts a rotation matrix to a quaternion
  94. * Reference:
  95. * - Shoemake, Quaternions,
  96. * http://www.cs.ucr.edu/~vbz/resources/quatut.pdf
  97. *
  98. * @param dcm a 3x3 rotation matrix
  99. * @param quaternion a [w, x, y, z] ordered quaternion (null-rotation being 1 0 0 0)
  100. */
  101. void mavlink_dcm_to_quaternion(const float dcm[3][3], float &quaternion[4]){
  102. float tr = dcm[0][0] + dcm[1][1] + dcm[2][2];
  103. if (tr > 0.0f) {
  104. float s = sqrtf(tr + 1.0f);
  105. quaternion[0] = s * 0.5f;
  106. s = 0.5f / s;
  107. quaternion[1] = (dcm[2][1] - dcm[1][2]) * s;
  108. quaternion[2] = (dcm[0][2] - dcm[2][0]) * s;
  109. quaternion[3] = (dcm[1][0] - dcm[0][1]) * s;
  110. } else {
  111. /* Find maximum diagonal element in dcm
  112. * store index in dcm_i */
  113. int dcm_i = 0;
  114. int i;
  115. for (i = 1; i < 3; i++) {
  116. if (dcm[i][i] > dcm[dcm_i][dcm_i]) {
  117. dcm_i = i;
  118. }
  119. }
  120. int dcm_j = (dcm_i + 1) % 3;
  121. int dcm_k = (dcm_i + 2) % 3;
  122. float s = sqrtf((dcm[dcm_i][dcm_i] - dcm[dcm_j][dcm_j] -
  123. dcm[dcm_k][dcm_k]) + 1.0f);
  124. quaternion[dcm_i + 1] = s * 0.5f;
  125. s = 0.5f / s;
  126. quaternion[dcm_j + 1] = (dcm[dcm_i][dcm_j] + dcm[dcm_j][dcm_i]) * s;
  127. quaternion[dcm_k + 1] = (dcm[dcm_k][dcm_i] + dcm[dcm_i][dcm_k]) * s;
  128. quaternion[0] = (dcm[dcm_k][dcm_j] - dcm[dcm_j][dcm_k]) * s;
  129. };
  130. };
  131. /**
  132. * Converts euler angles to a rotation matrix
  133. *
  134. * @param roll the roll angle in radians
  135. * @param pitch the pitch angle in radians
  136. * @param yaw the yaw angle in radians
  137. * @param dcm a 3x3 rotation matrix
  138. */
  139. void mavlink_euler_to_dcm(float roll, float pitch, float yaw, float &dcm[3][3]){
  140. float cosPhi = cosf(roll);
  141. float sinPhi = sinf(roll);
  142. float cosThe = cosf(pitch);
  143. float sinThe = sinf(pitch);
  144. float cosPsi = cosf(yaw);
  145. float sinPsi = sinf(yaw);
  146. dcm[0][0] = cosThe * cosPsi;
  147. dcm[0][1] = -cosPhi * sinPsi + sinPhi * sinThe * cosPsi;
  148. dcm[0][2] = sinPhi * sinPsi + cosPhi * sinThe * cosPsi;
  149. dcm[1][0] = cosThe * sinPsi;
  150. dcm[1][1] = cosPhi * cosPsi + sinPhi * sinThe * sinPsi;
  151. dcm[1][2] = -sinPhi * cosPsi + cosPhi * sinThe * sinPsi;
  152. dcm[2][0] = -sinThe;
  153. dcm[2][1] = sinPhi * cosThe;
  154. dcm[2][2] = cosPhi * cosThe;
  155. };