Euler to Quaternion / Quaternion to Euler using Eigen
Asked Answered
C

6

11

I'm trying to implement a functionality that can convert an Euler angle into an Quaternion and back "YXZ"-convention using Eigen. Later this should be used to let the user give you Euler angles and rotate around as Quaternion and convert Back for the user. In fact i am realy bad at math but tried my best. I have no Idea if this matrices are correct or anything. The code Works, but my results are way to off, i suppose. Any idea where i take the wrong turn? This is what my Quat.cpp looks like:

#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;
}

Input: x = 55.0, y = 80.0, z = 12.0 Quaternion: w:0.872274, x: -0.140211, y:0.447012, z:-0.140211 Return Value: x:-55.5925, y: -6.84901, z:-21.8771 The X-Value seems about right disregarding the prefix, but Y and z are off.

Chaldron answered 23/7, 2015 at 14:10 Comment(0)
G
38

From Euler to 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;

From Quaternion to Euler:

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

Taken from https://eigen.tuxfamily.org/dox/classEigen_1_1AngleAxis.html

Gratin answered 3/4, 2017 at 19:26 Comment(2)
Typo in "eulerAnglers".Palaeolithic
To compile your code I have to provide a template argument (Scalar) when constructing a Quaternion, e.g., Quternion<float> q or Quaternionf q for your case. (with [email protected] and [email protected])Yaelyager
P
5

The Quaternation to Euler solution didnt work for me, so i researched and modified the code, now it works for my purpose:

Vector3f ToEulerAngles(const Eigen::Quaternionf& q) {
    Vector3f angles;    //yaw pitch roll
    const auto x = q.x();
    const auto y = q.y();
    const auto z = q.z();
    const auto w = q.w();

    // roll (x-axis rotation)
    double sinr_cosp = 2 * (w * x + y * z);
    double cosr_cosp = 1 - 2 * (x * x + y * y);
    angles[2] = std::atan2(sinr_cosp, cosr_cosp);

    // pitch (y-axis rotation)
    double sinp = 2 * (w * y - z * x);
    if (std::abs(sinp) >= 1)
        angles[1] = std::copysign(M_PI / 2, sinp); // use 90 degrees if out of range
    else
        angles[1] = std::asin(sinp);

    // yaw (z-axis rotation)
    double siny_cosp = 2 * (w * z + x * y);
    double cosy_cosp = 1 - 2 * (y * y + z * z);
    angles[0] = std::atan2(siny_cosp, cosy_cosp);
    return angles;
}

I was inspired by this wiki entry and did some bench marking with the presented solution here. Checkout the wiki: https://en.wikipedia.org/wiki/Conversion_between_quaternions_and_Euler_angles

Pucker answered 18/4, 2022 at 19:48 Comment(0)
P
3

Here's one approach (not tested):

  Vector3d euler = quaternion.toRotationMatrix().eulerAngles(2, 1, 0);
  yaw = euler[0]; pitch = euler[1]; roll = euler[2];
Partition answered 30/9, 2016 at 10:32 Comment(0)
J
1

When I use

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

It can not work perfectly all the time, the euler angle always has a regular beat (the actual value and the calculated value have a deviation of ±π). For example, read and show yaw angle by rqt picture.

I have no idea about this, but I find ros tf::getYaw() also can achieve "Quaternion to Euler" (because I just need yaw angle).

Jehad answered 26/3, 2019 at 8:52 Comment(1)
I'm seeing the same exact behavior - the quaternions plot smoothly but the conversion to Euler roll/pitch/yaw exhibit the +/- pi instability.Oberg
R
0

Without Eigen (just in case), I did:

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

Though, these can give only two of the 24 configurations possible.

Refugia answered 20/12, 2020 at 20:13 Comment(0)
E
0

My solution (test in robotics)

#include <unsupported/Eigen/EulerAngles>

// [Quaternion2Euler]
Eigen::VectorXd Euler_ZYX = Eigen::EulerAngles<double, Eigen::EulerSystemZYX>(quat.normalized().toRotationMatrix()).angles();

// [Euler2Quaternion]
Eigen::Quaterniond q_test =
    Eigen::AngleAxisd(Euler_ZYX(0), Eigen::Vector3d::UnitZ()) *
    Eigen::AngleAxisd(Euler_ZYX(1), Eigen::Vector3d::UnitY()) *
    Eigen::AngleAxisd(Euler_ZYX(2), Eigen::Vector3d::UnitX());
Ellissa answered 25/8, 2023 at 7:19 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.