I have two quaternions that make up the rotation that I need. However, one of them contains the Yaw rotation, and the other the Pitch and Roll.
What I need to do is extract the Yaw from Quaternion A, extract the Pitch and Roll from Quaternion B, and then combine them into Quaternion C.
I have:
#include <Eigen/Dense>
#define M_PI 3.14159265358979323846264338327950288
int main()
{
Eigen::Quaternionf quatA(0.775, 0.321, -0.208, 0.503);
Eigen::Quaternionf quatB(0.364, 0.606, 0.364, 0.606);
//A
auto eulerA = quatA.toRotationMatrix().eulerAngles(0, 1, 2);
float rollA = eulerA.x();
float yawA = eulerA.y();
float pitchA = eulerA.z();
std::cout << "rotation A" << std::endl;
std::cout << eulerA.x() * 180 / M_PI << " " << eulerA.y() * 180 / M_PI << " " << eulerA.z() * 180 / M_PI << std::endl;
//B
auto eulerB = quatB.toRotationMatrix().eulerAngles(0, 1, 2);
float rollB = eulerB.x();
float yawB = eulerB.y();
float pitchB = eulerB.z();
std::cout << "rotation B" << std::endl;
std::cout << eulerB.x() * 180 / M_PI << " " << eulerB.y() * 180 / M_PI << " " << eulerB.z() * 180 / M_PI << std::endl;
//create a new quat from just the Yaw.
Eigen::Quaternionf QuatYaw = Eigen::AngleAxisf(0, Eigen::Vector3f::UnitX())
* Eigen::AngleAxisf(yawB, Eigen::Vector3f::UnitY())
* Eigen::AngleAxisf(0, Eigen::Vector3f::UnitZ());
//Test the yaw rotation value
auto testYaw = QuatYaw.toRotationMatrix().eulerAngles(0, 1, 2);
std::cout << "Yaw value: " << testYaw.x() << " " << testYaw.y() * 180 / M_PI << " " << testYaw.z() << std::endl;
//A back to quat
Eigen::Quaternionf quatA_back = Eigen::AngleAxisf(rollA, Eigen::Vector3f::UnitX())
* Eigen::AngleAxisf(yawA, Eigen::Vector3f::UnitY())
* Eigen::AngleAxisf(pitchA, Eigen::Vector3f::UnitZ());
Eigen::Quaternionf resultQuat = quatA_back * QuatYaw;
//result
auto eulerResult = resultQuat.toRotationMatrix().eulerAngles(0, 1, 2);
std::cout << "result" << std::endl;
std::cout << eulerResult.x() * 180 / M_PI << " " << eulerResult.y() * 180 / M_PI << " " << eulerResult.z() * 180 / M_PI << std::endl;
}
This gives me:
rotation A
44.976 0.0301393 65.955
rotation B
-0 89.9693 117.99
Yaw value: -0 89.9693 0
result
134.996 155.955 -89.9807
When I would expect a result of:
44.976 89.9693 65.955
How can i merge quaternion axes in this way? Where am i going wrong?