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.

178 lines
5.0 KiB

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