欧拉角和四元数的相互转换关系.

10710阅读 0评论2019-03-21 iibull
分类:其他平台

参考 

点击(此处)折叠或打开

  1. Quaterniond toQuaternion( double yaw, double pitch, double roll) // yaw (Z), pitch (Y), roll (X)
  2. {
  3.     // Abbreviations for the various angular functions
  4.     double cy = cos(yaw * 0.5);
  5.     double sy = sin(yaw * 0.5);
  6.     double cp = cos(pitch * 0.5);
  7.     double sp = sin(pitch * 0.5);
  8.     double cr = cos(roll * 0.5);
  9.     double sr = sin(roll * 0.5);

  10.     Quaterniond q;
  11.     q.w() = cy * cp * cr + sy * sp * sr;
  12.     q.x() = cy * cp * sr - sy * sp * cr;
  13.     q.y() = sy * cp * sr + cy * sp * cr;
  14.     q.z() = sy * cp * cr - cy * sp * sr;
  15.     return q;
  16. }


点击(此处)折叠或打开

  1. static void toEulerAngle(const Quaterniond& q, double& roll, double& pitch, double& yaw)
  2. {
  3.     // roll (x-axis rotation)
  4.     double sinr_cosp = +2.0 * (q.w() * q.x() + q.y() * q.z());
  5.     double cosr_cosp = +1.0 - 2.0 * (q.x() * q.x() + q.y() * q.y());
  6.     roll = atan2(sinr_cosp, cosr_cosp);

  7.     // pitch (y-axis rotation)
  8.     double sinp = +2.0 * (q.w() * q.y() - q.z() * q.x());
  9.     if (fabs(sinp) >= 1)
  10.         pitch = copysign(M_PI / 2, sinp); // use 90 degrees if out of range
  11.     else
  12.         pitch = asin(sinp);

  13.     // yaw (z-axis rotation)
  14.     double siny_cosp = +2.0 * (q.w() * q.z() + q.x() * q.y());
  15.     double cosy_cosp = +1.0 - 2.0 * (q.y() * q.y() + q.z() * q.z());
  16.     yaw = atan2(siny_cosp, cosy_cosp);
  17. }

上一篇:STM32 HardFault_Handler处理方法
下一篇:IMU之razor_imu_9dof sparkfun ros AHRS ITG3200/ITG3205/ADXL345