我有两个四元数组成了我需要的旋转。但是,其中一个包含偏航旋转,另一个包含俯仰和滚转。
我需要做的是从四元数A中提取偏航,从四元量B中提取俯仰和滚转,然后将它们组合成四元数C。
我有:
#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;
}
这给了我:
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
当我期望的结果是:
44.976 89.9693 65.955
如何以这种方式合并四元数轴?我哪里错了?
乍一看,您的方法有两个问题:
-
第一个是技术性的——你在这里混合欧拉角的顺序:
float rollA = eulerA.x(); float yawA = eulerA.y(); float pitchA = eulerA.z();
-
第二个是概念性的——欧拉角通常是相互依存的,从两个不同的方向提取和合并是不正确的。