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)

orocos_kdl学习(一):坐标系变换的更多相关文章

  1. 5. svg学习笔记-坐标系变换

    之前我们编写图形元素的时候,编写好了位置大小就是固定的,通过坐标系变换,可以移动缩放,旋转图形,但必须声明的是,进行变换时是图形相对于坐标系的变化,就是图形是不发生变化的,而是坐标系发生了变化,比如缩 ...

  2. ROS Learning-014 learning_tf(编程) 坐标系变换(tf)广播员 (Python版)

    ROS Indigo learning_tf-01 坐标系变换(tf)广播员 (Python版) 我使用的虚拟机软件:VMware Workstation 11 使用的Ubuntu系统:Ubuntu ...

  3. 【转】QPainter中坐标系变换问题

    转自:http://blog.sina.com.cn/s/blog_67cf08270100ww0p.html 一.坐标系简介. Qt中每一个窗口都有一个坐标系,默认的,窗口左上角为坐标原点,然后水平 ...

  4. BZOJ3210: 花神的浇花集会(坐标系变换)

    题面 传送门 题解 坐标系变换把切比雪夫距离转化为曼哈顿距离 那么对于所有的\(x\)坐标中,肯定是中位数最优了,\(y\)坐标同理 然而有可能这个新的点不合法,也就是说不存在\((x+y,x-y)\ ...

  5. OSG数学基础:坐标系变换

    三维实体对象需要经过一系列的坐标变换才能正确.真实地显示在屏幕上.在一个场景中,当读者对场景中的物体进行各种变换及相关操作时,坐标系变换是非常频繁的. 坐标系变换通常包括:世界坐标系-物体坐标系变换. ...

  6. CSS学习笔记2-2d变换和过渡属性

    前言:今天又是一个周末,心情不错,趁着闲暇之余,把剩下来的CSS3学习的内容全部整理出来,练习用的源码也稍微整理了一下. 2D转换 transform:translate||rotate||scale ...

  7. svg坐标系变换

    svg的坐标变换有三个属性来决定:viewport, viewBox, 和 preserveAspectRatio,我发现三篇比较详细的博客,转载如下: 理解SVG坐标系和变换:视窗,viewBox和 ...

  8. orocos_kdl学习(二):KDL Tree与机器人运动学

    KDL(Kinematics and Dynamics Library)中定义了一个树来代表机器人的运动学和动力学参数,ROS中的kdl_parser提供了工具能将机器人描述文件URDF转换为KDL ...

  9. 洛谷P3964 [TJOI2013]松鼠聚会(坐标系变换)

    题面 传送门 题解 对于两个点\((x_i,y_i)\)和\(x_j,y_j\),我们定义它们之间的曼哈顿距离为 \[|x_i-x_j|+|y_i-y_j|\] 定义它们的切比雪夫距离为 \[\max ...

随机推荐

  1. python多进程和多线程

    多任务才有多进程和线程: 线程是最小的执行单元,而进程由至少一个线程组成.如何调度进程和线程,完全由操作系统决定,程序自己不能决定什么时候执行,执行多长时间. 多进程和多线程的程序涉及到同步.数据共享 ...

  2. HDU3031 To Be Or Not To Be 左偏树 可并堆

    欢迎访问~原文出处——博客园-zhouzhendong 去博客园看该题解 题目传送门 - HDU3031 题意概括 喜羊羊和灰太狼要比赛. 有R次比赛. 对于每次比赛,首先输入n,m,n表示喜羊羊和灰 ...

  3. C# 反编译破解软件方法

    我们有时在使用一些小工具软件时,会提示购买License(注册码之类的东东)后才能正常使用.在这里我们来尝试直接绕过License验证直接使用软件,实现简单的软件破解. 主要实现方式: 通过反编译工具 ...

  4. Springboot中实现策略模式+工厂模式

    策略模式和工厂模式相信大家都比较熟悉,但是大家有没有在springboot中实现策略和工厂模式? 具体策略模式和工厂模式的UML我就不给出来了,使用这个这两个模式主要是防止程序中出现大量的IF ELS ...

  5. ReportNG报表显示中文乱码和TestNG显示中文乱码实力解决办法

    最近在进军测试自动化框架学习阶段,但无意间总是会伴随小问题的困扰,比如中文乱码,而导致显示总是不舒服,个人觉得,就一定要解决,似乎有点点强迫症.所以遇到ReportNG报表显示中文乱码和TestNG显 ...

  6. 获取img的高

    我们可以通过css设置图片的width,然后通过 clientWidth获取图片的宽,但是这个宽似乎是css里面定义的width值,但是对于图片的高,使用 clientHeight 来获取似乎是有些问 ...

  7. [ 转载 ] Handler详解

    带着问题学习 Android Handler 消息机制 Marker_Sky 关注  0.4 2018.02.06 18:04* 字数 3992 阅读 541评论 0喜欢 13   学习 Androi ...

  8. Web大前端面试题-Day9

    1. 请用至少3中方式实现数组去重? 方法一: indexOfvar arr1=[1,2,3,4,5,4,3,2,1];  function repeat1(arr){    for(var i=0, ...

  9. Web前端性能优化进阶——完结篇

    前言 在之前的文章 如何优化网站性能,提高页面加载速度 中,我们简单介绍了网站性能优化的重要性以及几种网站性能优化的方法(没有看过的可以狂戳 链接 移步过去看一下),那么今天我们深入讨论如何进一步优化 ...

  10. PLSQL Developer的使用之对象浏览器

    PLSQL Developer的使用之对象浏览器 (转自https://www.cnblogs.com/mq0036/p/6437834.html#point11) 能够显示与 PL/SQL 开发相关 ...