ROS的tf转为RT矩阵

cpp 复制代码
void // Get the TF as an Eigen matrix
transformAsMatrix (const tf::Transform& bt, Eigen::Matrix4f &out_mat)
{
  double mv[12];
  bt.getBasis ().getOpenGLSubMatrix (mv);

  tf::Vector3 origin = bt.getOrigin ();

  out_mat (0, 0) = mv[0]; out_mat (0, 1) = mv[4]; out_mat (0, 2) = mv[8];
  out_mat (1, 0) = mv[1]; out_mat (1, 1) = mv[5]; out_mat (1, 2) = mv[9];
  out_mat (2, 0) = mv[2]; out_mat (2, 1) = mv[6]; out_mat (2, 2) = mv[10];

  out_mat (3, 0) = out_mat (3, 1) = out_mat (3, 2) = 0; out_mat (3, 3) = 1;
  out_mat (0, 3) = origin.x ();
  out_mat (1, 3) = origin.y ();
  out_mat (2, 3) = origin.z ();
}

这段代码定义了一个函数 transformAsMatrix,它将一个 tf::Transform 对象转换为一个 4x4 的 Eigen 矩阵 (Eigen::Matrix4f)。tf::Transform 是一个表示 3D 空间中的变换的对象,通常包括旋转和平移。下面是这段代码的逐步解释:

函数签名

cpp 复制代码
transformAsMatrix (const tf::Transform& bt, Eigen::Matrix4f &out_mat)
  • const tf::Transform& btbt 是一个 tf::Transform 类型的常量引用,表示输入的变换。
  • Eigen::Matrix4f &out_matout_mat 是一个 Eigen::Matrix4f 类型的引用,表示输出的 4x4 矩阵。

函数内部

  1. 获取旋转矩阵(基础部分)

    cpp 复制代码
    double mv[12];
    bt.getBasis ().getOpenGLSubMatrix (mv);
    • bt.getBasis().getOpenGLSubMatrix(mv):从 tf::Transform 对象 bt 中提取旋转矩阵部分,并以 OpenGL 的行优先顺序存储在 mv 数组中。mv 是一个长度为 12 的数组,包含了旋转矩阵的前 3 行的内容(不包括平移部分)。
  2. 获取平移向量

    cpp 复制代码
    tf::Vector3 origin = bt.getOrigin ();
    • bt.getOrigin():从 tf::Transform 对象中获取平移向量(原点)。这个向量存储了变换的平移部分。
  3. 填充输出矩阵的旋转部分

    cpp 复制代码
    out_mat (0, 0) = mv[0]; out_mat (0, 1) = mv[4]; out_mat (0, 2) = mv[8];
    out_mat (1, 0) = mv[1]; out_mat (1, 1) = mv[5]; out_mat (1, 2) = mv[9];
    out_mat (2, 0) = mv[2]; out_mat (2, 1) = mv[6]; out_mat (2, 2) = mv[10];
    • 这几行代码将 mv 中的元素填入 out_mat 的旋转部分。注意 mv 中的元素是按照行优先顺序存储的。
  4. 填充输出矩阵的平移部分

    cpp 复制代码
    out_mat (0, 3) = origin.x ();
    out_mat (1, 3) = origin.y ();
    out_mat (2, 3) = origin.z ();
    • 这几行代码将平移向量 origin 的 x、y 和 z 分量填入 out_mat 的最后一列。
  5. 填充输出矩阵的齐次坐标部分

    cpp 复制代码
    out_mat (3, 0) = out_mat (3, 1) = out_mat (3, 2) = 0; out_mat (3, 3) = 1;
    • 最后一行 [0, 0, 0, 1] 是为了齐次坐标表示法,确保矩阵是一个有效的 4x4 变换矩阵。前三个元素设置为 0,最后一个元素设置为 1。

总结

  • 该函数将 tf::Transform 对象 bt 中的旋转和平移信息提取出来,并将其填充到 Eigen::Matrix4f 类型的 4x4 矩阵 out_mat 中。这种矩阵可以用来进行3D空间中的坐标变换。
相关推荐
Eric.Lee20212 天前
SLAM 路径规划的安全走廊实现
python·机器人·ros·路径规划·避障·安全走廊
滴啦嘟啦哒4 天前
【机械臂】【LLM】一、接入千问LLM实现自然语言指令解析
深度学习·ros·vla
wzyannn6 天前
Ubuntu24.04下ROS2和MoveIt2控制六轴机械臂(持续更新)
ros·机械臂·ros2
岱宗夫up8 天前
基于ROS的视觉导航系统实战:黑线循迹+激光笔跟随双模态实现(冰达机器人Nano改造)
linux·python·机器人·ros
lq mm11 天前
3d-navi 3D导航模拟仿真项目复现
ros
涅小槃13 天前
Carla仿真学习笔记(版本0.9.16)
开发语言·python·ros·carla
佚明zj15 天前
深入浅出 ROS2 QoS:如何为你的机器人选择通信策略
ros
Mr.Winter`16 天前
轨迹优化 | 微分动态规划DDP与迭代线性二次型调节器iLQR理论推导
人工智能·算法·机器人·自动驾驶·动态规划·ros·具身智能
zzzhpzhpzzz19 天前
从SolidWorks中导出机器人URDF模型
机器人·ros·urdf·solidworks
提伯斯64620 天前
Fast-LIO到MAVROS视觉定位转换
linux·ros·无人机·mid360·fasltlio