Euler到Quaternion / Quaternion到Euler使用Eigen

时间:2015-07-23 14:10:40

标签: c++ eigen quaternions euler-angles

我正在尝试实现一种功能,可以将欧拉角转换为四元数,然后使用Eigen返回“YXZ”。稍后这应该用于让用户给你Euler角度并作为Quaternion旋转并为用户转换Back。事实上,我真的很擅长数学但是我尽力了。如果这个矩阵是正确的或任何东西,我不知道。代码可以工作,但我认为我的结果是关闭的。知道我哪里走错了吗?这就是我的Quat.cpp的样子:

#include "Quat.h"
#include <Eigen/Geometry>
#include <Eigen/Dense>
#include <cmath>
#include <iostream>

using namespace Eigen;

Vector3f Quat::MyRotation(const Vector3f YPR)
{
    Matrix3f matYaw(3, 3), matRoll(3, 3), matPitch(3, 3), matRotation(3, 3);
    const auto yaw = YPR[2]*M_PI / 180;
    const auto pitch = YPR[0]*M_PI / 180;
    const auto roll = YPR[1]*M_PI / 180;

    matYaw << cos(yaw), sin(yaw), 0.0f,
        -sin(yaw), cos(yaw), 0.0f,  //z
        0.0f, 0.0f, 1.0f;

    matPitch << cos(pitch), 0.0f, -sin(pitch),
        0.0f, 1.0f, 0.0f,   // X
        sin(pitch), 0.0f, cos(pitch);

    matRoll << 1.0f, 0.0f, 0.0f,
        0.0f, cos(roll), sin(roll),   // Y
        0.0f, -sin(roll), cos(roll);

    matRotation = matYaw*matPitch*matRoll;

    Quaternionf quatFromRot(matRotation);

    quatFromRot.normalize(); //Do i need to do this?

    return Quat::toYawPitchRoll(quatFromRot);
}

Vector3f Quat::toYawPitchRoll(const Eigen::Quaternionf& q)
{
    Vector3f retVector;

    const auto x = q.y();
    const auto y = q.z();
    const auto z = q.x();
    const auto w = q.w();

    retVector[2] = atan2(2.0 * (y * z + w * x), w * w - x * x - y * y + z * z);
    retVector[1] = asin(-2.0 * (x * z - w * y));
    retVector[0] = atan2(2.0 * (x * y + w * z), w * w + x * x - y * y - z * z);

#if 1
    retVector[0] = (retVector[0] * (180 / M_PI));
    retVector[1] = (retVector[1] * (180 / M_PI))*-1;
    retVector[2] = retVector[2] * (180 / M_PI);
#endif
    return retVector;
}

输入:x = 55.0,y = 80.0,z = 12.0 四元数:w:0.872274,x:-0.140211,y:0.447012,z:-0.140211 返回值:x:-55.5925,y:-6.84901,z:-21.8771 X-Value似乎正确无视前缀,但Y和z关闭。

4 个答案:

答案 0 :(得分:16)

从Euler到Quaternion:

using namespace Eigen;
//Roll pitch and yaw in Radians
float roll = 1.5707, pitch = 0, yaw = 0.707;    
Quaternionf q;
q = AngleAxisf(roll, Vector3f::UnitX())
    * AngleAxisf(pitch, Vector3f::UnitY())
    * AngleAxisf(yaw, Vector3f::UnitZ());
std::cout << "Quaternion" << std::endl << q.coeffs() << std::endl;

从四元数到欧拉:

auto euler = q.toRotationMatrix().eulerAngles(0, 1, 2);
std::cout << "Euler from quaternion in roll, pitch, yaw"<< std::endl << euler << std::endl;

取自https://eigen.tuxfamily.org/dox/classEigen_1_1AngleAxis.html

答案 1 :(得分:2)

这是一种方法(未经测试):

  Vector3d euler = quaternion.toRotationMatrix().eulerAngles(2, 1, 0);
  yaw = euler[0]; pitch = euler[1]; roll = euler[2];

答案 2 :(得分:0)

当我使用

  

auto euler = q.toRotationMatrix()。eulerAngles(0,1,2)

它不能一直都完美工作,欧拉角总是有规律的拍子(实际值和计算值的偏差为±π)。 例如,通过rqt读取并显示偏航角 picture

对此我一无所知,但是我发现ros tf::getYaw()也可以实现“四元数到Euler”(因为我只需要偏航角)。

答案 3 :(得分:0)

没有 Eigen(以防万一),我做了:

tf2::Matrix3x3 ( quat ) . getEulerYPR( &roll, &pitch, &yaw );
// and
tf2::Matrix3x3 ( quat ) . getRPY( &roll, &pitch, &yaw );

不过,这些只能给出 24 种可能的配置中的两种。