您的位置:首页 > 其它

ROS中使用Eigen库

2018-02-12 10:57 1521 查看
——参考书《A Systematic Approach to Learning Robot Programming with ROS》



ROS中的数据操作需要线性代数,Eigen库是C++中的线性代数计算库。它独立于ROS,但是在ROS中可以使用。在CMakeLists.txt文件中要做以下配置

#uncomment the following 4 lines to use the Eigen library
find_package(cmake_modules REQUIRED)
find_package(Eigen3 REQUIRED)
include_directories(${EIGEN3_INCLUDE_DIR})
add_definitions(${EIGEN_DEFINITIONS})


在源代码中要包含以下头文件

#include <Eigen/Eigen>
#include <Eigen/Dense>
#include <Eigen/Geometry>
#include <Eigen/Eigenvalues>


如果需要使用其他的功能,也需要用到其他的头文件。可以参考这个网址http://eigen.tuxfamily.org/dox/group__QuickRefPage.html

定义一个向量:

Eigen::Vector3d normal_vec(1,2,3); // here is an arbitrary normal vector, initialized to (1,2,3) upon instantiation


将向量变成单位向量

normal_vec/=normal_vec.norm(); // make this vector unit length


定义一个矩阵的形式是:

Eigen::Matrix3d Rot_z;
Rot_z.row(0)<<0,-1,0;  // populate the first row--shorthand method
Rot_z.row(1)<<1, 0,0;  //second row
Rot_z.row(2)<<0, 0,1;  // yada, yada
cout<<"Rot_z: "<<endl;


可以按行赋值。

矩阵与向量的乘法

v1 = Rot_z*normal_vec;
//here is how to multiply a matrix times a vector
//although Rot_z and normal_vec are both objects (with associated data and member methods), multiplication is defined,
//resulting in the data members being altered or generated as expected for matrix-vector multiplies


行了,用到了再说吧,头疼
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  ROS 矩阵 Eigen