eigen旋转矩阵与欧拉角的转换

欧拉角转旋转矩阵(zyx)

// 使用eigen库,欧拉角转旋转矩阵
	Eigen::Matrix3d rotation_matrix1, rotation_matrix2;
	rotation_matrix1 =
		Eigen::AngleAxisd(euler_angle[2], Eigen::Vector3d::UnitZ()) *
		Eigen::AngleAxisd(euler_angle[1], Eigen::Vector3d::UnitY()) *
		Eigen::AngleAxisd(euler_angle[0], Eigen::Vector3d::UnitX());
	cout << "\nrotation matrix1 =\n" << rotation_matrix1 << endl << endl;

旋转矩阵转欧拉角

// 使用egigen将旋转矩阵转换为欧拉角
Eigen::Vector3d eulerAngle1 = 
	rotation_matrix1.eulerAngles(2, 1, 0); //zyx顺序
cout << "roll_2 pitch_2 yaw_2 = " << eulerAngle1[2] 
	<< " " << eulerAngle1[1]
	<< " " << eulerAngle1[0] << endl << endl;

完整代码

#include<iostream>
#include<Eigen/Core>
#include<Eigen/Geometry>

using namespace std;

Eigen::Matrix3d eulerAnglesToRotationMatrix(Eigen::Vector3d& theta);
bool isRotationMatrix(Eigen::Matrix3d R);
Eigen::Vector3d rotationMatrixToEulerAngles(Eigen::Matrix3d& R);

const double ARC_TO_DEG = 57.29577951308238;
const double DEG_TO_ARC = 0.0174532925199433;

int main()
{
	// 设定车体欧拉角,绕固定轴
	double roll_deg = 0.5; // 绕x轴
	double pitch_deg = 0.8; // 绕y轴
	double yaw_deg = 108.5; // 绕z轴

	// 转换为弧度
	double roll_arc = roll_deg * DEG_TO_ARC;
	double pitch_arc = pitch_deg * DEG_TO_ARC; 
	double yaw_arc = yaw_deg * DEG_TO_ARC; 

	cout << endl;
	cout << "roll_arc = " << roll_arc << endl;
	cout << "pitch_arc = " << pitch_arc << endl;
	cout << "yaw_arc = " << yaw_arc << endl;

	// 初始化欧拉角
	Eigen::Vector3d euler_angle(roll_arc, pitch_arc, yaw_arc);

	// 使用eigen库,欧拉角转旋转矩阵
	Eigen::Matrix3d rotation_matrix1, rotation_matrix2;
	rotation_matrix1 =
		Eigen::AngleAxisd(euler_angle[2], Eigen::Vector3d::UnitZ()) *
		Eigen::AngleAxisd(euler_angle[1], Eigen::Vector3d::UnitY()) *
		Eigen::AngleAxisd(euler_angle[0], Eigen::Vector3d::UnitX());
	cout << "\nrotation matrix1 =\n" << rotation_matrix1 << endl << endl;

	// 使用自定义函数,欧拉角转旋转矩阵
	rotation_matrix2 = eulerAnglesToRotationMatrix(euler_angle);
	cout << "rotation matrix2 = \n" << rotation_matrix2 << endl << endl;

	// 使用egigen将旋转矩阵转换为欧拉角
	Eigen::Vector3d eulerAngle1 = rotation_matrix1.eulerAngles(2, 1, 0); //zyx顺序
	cout << "roll_2 pitch_2 yaw_2 = " << eulerAngle1[2] << " " << eulerAngle1[1]
		<< " " << eulerAngle1[0] << endl << endl;

	// 使用自定义函数将旋转矩阵转换为欧拉角
	Eigen::Vector3d eulerAngle2 = rotationMatrixToEulerAngles(rotation_matrix1);
	cout << "roll_2 pitch_2 yaw_2 = " << eulerAngle2[0] << " " << eulerAngle2[1]
		<< " " << eulerAngle2[2] << endl << endl;

	return 0;
}

Eigen::Matrix3d eulerAnglesToRotationMatrix(Eigen::Vector3d& theta)
{
	Eigen::Matrix3d R_x;
	R_x <<
		1, 0, 0,
	0, cos(theta[0]), -sin(theta[0]),
		0, sin(theta[0]), cos(theta[0]);

	Eigen::Matrix3d R_y;
	R_y <<
		cos(theta[1]), 0, sin(theta[1]),
		0, 1, 0,
		-sin(theta[1]), 0, cos(theta[1]);

	Eigen::Matrix3d R_z;
	R_z <<
		cos(theta[2]), -sin(theta[2]), 0,
		sin(theta[2]), cos(theta[2]), 0,
		0, 0, 1;
	Eigen::Matrix3d R = R_z * R_y * R_x;
	return R;
}


bool isRotationMatrix(Eigen::Matrix3d R)
{
	double err = 1e-6;
	Eigen::Matrix3d shouldIdentity;
	shouldIdentity = R * R.transpose();
	Eigen::Matrix3d I = Eigen::Matrix3d::Identity();
	return (shouldIdentity - I).norm() < err;
}

Eigen::Vector3d rotationMatrixToEulerAngles(Eigen::Matrix3d& R)
{
	assert(isRotationMatrix(R));
	double sy = sqrt(R(0, 0) * R(0, 0) + R(1, 0) * R(1, 0));
	bool singular = sy < 1e-6;
	double x, y, z;
	if (!singular) {
		x = atan2(R(2, 1), R(2, 2));
		y = atan2(-R(2, 0), sy);
		z = atan2(R(1, 0), R(0, 0));
	}
	else {
		x = atan2(-R(1, 2), R(1, 1));
		y = atan2(-R(2, 0), sy);
		z = 0;
	}
	return { x,y,z };
}

结果

roll_arc = 0.00872665
pitch_arc = 0.0139626
yaw_arc = 1.89368

rotation matrix1 =
 -0.317274  -0.948326 0.00384548
  0.948231  -0.317177  0.0160091
-0.0139622 0.00872568   0.999864

rotation matrix2 =
 -0.317274  -0.948326 0.00384548
  0.948231  -0.317177  0.0160091
-0.0139622 0.00872568   0.999864

roll_2 pitch_2 yaw_2 = 0.00872665 0.0139626 1.89368

roll_2 pitch_2 yaw_2 = 0.00872665 0.0139626 1.89368
  • 4
    点赞
  • 22
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Eigen是一个C++的线性代数库,它提供了许多数学工具和函数来进行矩阵和向量的运算。Eigen库中也提供了四元数与欧拉角转换函数。 四元数是一种扩展了复数概念的数学工具,用来表示三维空间中的旋。它由一个实部和三个虚部组成,可以表达旋的角度和轴向。 欧拉角是一种常见的旋表示方法,它将旋分解为绕三个坐标轴(通常是X、Y、Z轴)的旋角度。 在Eigen库中,我们可以使用Quaternion类来表示四元数,并使用AngleAxis类来表示旋角度和轴向。要将四元数转换欧拉角,可以使用toRotationMatrix函数将四元数转换旋转矩阵,然后使用matrix()函数提取旋转矩阵中的欧拉角。 具体的代码示例如下: ```cpp #include <Eigen/Geometry> int main() { Eigen::Quaterniond quat(0.707, 0, 0.707, 0); // 示例四元数 Eigen::Matrix3d rotation_matrix = quat.toRotationMatrix(); // 将四元数转换旋转矩阵 Eigen::Vector3d euler_angles = rotation_matrix.eulerAngles(2, 1, 0); // 获取旋转矩阵中的欧拉角 return 0; } ``` 在上述代码中,我们首先定义了一个示例的四元数,然后使用toRotationMatrix函数将其转换旋转矩阵。最后,使用eulerAngles函数从旋转矩阵中提取出欧拉角。这里的eulerAngles函数的三个参数分别表示在Z轴、Y轴和X轴旋的顺序。 通过上述代码,我们可以得到四元数转换欧拉角的结果。注意,在具体应用中,可能还需要考虑一些余弦函数的特殊情况处理以及单位转换等问题。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值