orocos_kdl学习(一):坐标系变换

KDL坐标变换详解
本文详细介绍了KDL库中坐标变换的基本元素,包括点、坐标系、刚体速度及力/力矩等内容,并通过实例展示了如何创建和使用这些几何元素进行坐标变换。
部署运行你感兴趣的模型镜像

  KDL中提供了点(point)、坐标系(frame)、刚体速度(twist),以及6维力/力矩(wrench)等基本几何元素,具体可以参考 Geometric primitives 文档。

 Creating a Frame, Vector and Rotation

  PyKDL中创建一个坐标系时有下面4种构造函数:

__init__()         # Construct an identity frame

__init__(rot, pos) # Construct a frame from a rotation and a vector
# Parameters:    
# pos (Vector) – the position of the frame origin
# rot (Rotation) – the rotation of the frame

__init__(pos)      # Construct a frame from a vector, with identity rotation
# Parameters:     
# pos (Vector) – the position of the frame origin

__init__(rot)      # Construct a frame from a rotation, with origin at 0, 0, 0
# Parameters:    
# rot (Rotation) – the rotation of the frame

  下面是一个例子:

#! /usr/bin/env python

import PyKDL

# create a vector which describes both a 3D vector and a 3D point in space
v = PyKDL.Vector(1,3,5)

# create a rotation from Roll Pitch, Yaw angles
r1 = PyKDL.Rotation.RPY(1.2, 3.4, 0)

# create a rotation from ZYX Euler angles
r2 = PyKDL.Rotation.EulerZYX(0, 1, 0)

# create a rotation from a rotation matrix
r3 = PyKDL.Rotation(1,0,0, 0,1,0, 0,0,1)

# create a frame from a vector and a rotation
f = PyKDL.Frame(r2, v)

print f

  结果将如下所示,前9个元素为代表坐标系姿态的旋转矩阵,后三个元素为坐标系f的原点在参考坐标系中的坐标。

[[    0.540302,           0,    0.841471;
            0,           1,           0;
    -0.841471,           0,    0.540302]
[           1,           3,           5]]

  根据机器人学导论(Introduction to Robotics Mechanics and Control)附录中的12种欧拉角表示方法,ZYX欧拉角代表的旋转矩阵为:

  代入数据验证,KDL的计算与理论一致。

 

 Extracting information from a Frame, Vector and Rotation

  PyKDL中坐标系类Frame的成员M为其旋转矩阵,p为其原点坐标。

#! /usr/bin/env python

import PyKDL

# frame
f = PyKDL.Frame(PyKDL.Rotation.RPY(0,1,0),
                PyKDL.Vector(3,2,4))

# get the origin (a Vector) of the frame
origin = f.p

# get the x component of the origin
x = origin.x()
x = origin[0]
print x

# get the rotation of the frame
rot = f.M
print rot

# get ZYX Euler angles from the rotation
[Rz, Ry, Rx] = rot.GetEulerZYX()
print Rz,Ry,Rx

# get the RPY (fixed axis) from the rotation
[R, P, Y] = rot.GetRPY()
print R,P,Y

  将上述获取到的数据打印出来,结果如下:

3.0
[    0.540302,           0,    0.841471;
            0,           1,           0;
    -0.841471,           0,    0.540302]
0.0 1.0 0.0
0.0 1.0 0.0

   注意GetEulerZYX和GetRPY的结果是一样的,这其实不是巧合。因为坐标系的旋转有24种方式(本质上只有12种结果):绕自身坐标系旋转有12种方式(欧拉角),绕固定参考坐标系旋转也有12种方式(RPY就是其中一种)。EulerZYX按照Z→Y→X的顺序绕自身坐标轴分别旋转$\alpha$、$\beta$、$\gamma$角;RPY按照X→Y→Z的顺序绕固定坐标系旋转$\gamma$、$\beta$、$\alpha$角,这两种方式最后得到的旋转矩阵是一样的。

 

Transforming a point

  frame既可以用来描述一个坐标系的位置和姿态,也可以用于变换。下面创建了一个frame,然后用其对一个空间向量或点进行坐标变换:

#! /usr/bin/env python

import PyKDL

# define a frame
f = PyKDL.Frame(PyKDL.Rotation.RPY(0,1,0),
                PyKDL.Vector(3,2,4))

# define a point
p = PyKDL.Vector(1, 0, 0)
print p

# transform this point with f
p = f * p
print p

 

Creating from ROS types

  tf_conversions这个package包含了一系列转换函数,用于将tf类型的数据(point, vector, pose, etc) 转换为与其它库同类型的数据,比如KDL和Eigen。用下面的命令在catkin_ws/src中创建一个测试包:

catkin_create_pkg test rospy tf geometry_msgs

  test.py程序如下(注意修改权限chmod +x test.py):

#! /usr/bin/env python
import rospy
import PyKDL
from tf_conversions import posemath
from geometry_msgs.msg import Pose

# you have a Pose message
pose = Pose()

pose.position.x = 1
pose.position.y = 1
pose.position.z = 1
pose.orientation.x = pose.orientation.y = pose.orientation.z = 0
pose.orientation.w = 1

# convert the pose into a kdl frame
f1 = posemath.fromMsg(pose)

# create another kdl frame
f2 = PyKDL.Frame(PyKDL.Rotation.RPY(0,1,0),
                 PyKDL.Vector(3,2,4))

# Combine the two frames
f = f1 * f2
print f

[x, y, z, w] = f.M.GetQuaternion()
print x,y,z,w

# and convert the result back to a pose message
pose = posemath.toMsg(f)

pub = rospy.Publisher('pose', Pose, queue_size=1)
rospy.init_node('test', anonymous=True)
rate = rospy.Rate(1) # 1hz

while not rospy.is_shutdown():
    pub.publish(pose)
    rate.sleep()

  通过posemath.toMsg可以将KDL中的frame转换为geometry_msgs/Pose类型的消息。最后为了验证,创建了一个Publisher将这个消息发布到pose话题上。使用catkin_make编译后运行结果如下:

 

 

参考:

KDL Geometric primitives

从URDF到KDL(C++&Python)

PyKDL 1.0.99 documentation

Orocos Kinematics and Dynamics

kdl / Tutorials / Frame transformations (Python)

转载于:https://www.cnblogs.com/21207-iHome/p/8312854.html

您可能感兴趣的与本文相关的镜像

Stable-Diffusion-3.5

Stable-Diffusion-3.5

图片生成
Stable-Diffusion

Stable Diffusion 3.5 (SD 3.5) 是由 Stability AI 推出的新一代文本到图像生成模型,相比 3.0 版本,它提升了图像质量、运行速度和硬件效率

在解决 `Could not find a package configuration file provided by "orocos_kdl"` 这问题时,核心目标是确保 CMake 能够找到 `orocos_kdlConfig.cmake` 或 `orocos_kdl-config.cmake` 文件。以下为系统性的排查与解决方案: ### 1. 确保 Orocos-KDL 已正确安装 如果使用源码编译安装,则需确认是否执行了完整的构建流程: ```bash git clone https://github.com/orocos/orocos_kinematics_dynamics.git cd orocos_kinematics_dynamics/orocos_kdl mkdir build && cd build cmake -DCMAKE_BUILD_TYPE=Release .. make -j8 sudo make install ``` 默认情况下,`make install` 会将配置文件(如 `orocos_kdlConfig.cmake`)安装到 `/usr/local/lib/cmake/orocos_kdl/` 目录下[^2]。 ### 2. 检查 CMake 的模块路径配置 若上述文件已存在但未被找到,可能是因为 CMake 没有搜索到正确的安装路径。此时应手动设置 `CMAKE_PREFIX_PATH` 或 `orocos_kdl_DIR`: ```bash export CMAKE_PREFIX_PATH=/usr/local:$CMAKE_PREFIX_PATH ``` 或指定具体目录: ```bash export orocos_kdl_DIR=/usr/local/lib/cmake/orocos_kdl ``` ### 3. 验证 oroCos-KDL 是否提供了开发包 在某些 Linux 发行版中,Orocos-KDL 可能以二进制形式提供,但需要额外安装开发包。例如,在 Ubuntu 上可尝试: ```bash sudo apt-get install liborocos-kdl-dev ``` 这通常会自动配置好 CMake 所需的查找路径和配置文件[^3]。 ### 4. 手动复制配置文件(非推荐) 如果 `orocos_kdlConfig.cmake` 仅存在于构建目录而未被安装,可手动将其复制到标准路径中,例如: ```bash cp orocos_kdl/build/orocos_kdlConfig.cmake /usr/local/lib/cmake/orocos_kdl/ ``` 此方法适用于临时修复,建议优先通过修改 CMakeLists.txt 或构建配置来解决问题[^4]。 ### 5. Windows 平台下的特殊处理 在 Windows 上使用 Visual Studio 编译时,除了确保生成 `.lib` 和 `.dll` 文件外,还需要确认是否将构建生成的 `orocos_kdlConfig.cmake` 文件放置在 CMake 可识别的路径中。此外,Debug 和 Release 模式对应的库文件名不同,分别为 `orocos-ldld.lib` 和 `orocos-kdl.lib`,需根据实际情况选择链接[^5]。 ### 6. 使用 `find_package` 时的注意事项 在 `CMakeLists.txt` 中调用 `find_package(orocos_kdl REQUIRED)` 前,可以添加以下内容用于调试查找路径: ```cmake set(orocos_kdl_DIR "/usr/local/lib/cmake/orocos_kdl" CACHE PATH "Path to orocos_kdl cmake modules") message(STATUS "Looking for orocos_kdl in: ${orocos_kdl_DIR}") ``` 这样有助于快速定位路径设置是否正确。 ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值