本文包含较多数学公式。若公式未正常渲染,请刷新页面后重试。
平面上的一个点绕原点转过一定角度,新坐标怎么求?如果点还要平移,又该怎样把两步写成一次矩阵乘法?
这篇博客从三角函数的和角公式推出旋转矩阵,再用齐次矩阵把旋转和平移写在一起。最后算一个换坐标系的例子,看看同一个点在三个坐标系里分别怎么写。

比如图里的视觉工位。假设已知橙色定位点在相机坐标系下的三维坐标,也知道相机相对工作台的位置和朝向。怎样求这个点在工作台坐标系下的坐标?
旋转前坐标为 x,y,z,旋转后为 x',y',z'。列向量左乘矩阵,输出写在等号左边。三维采用右手系。
从二维旋转到绕三维坐标轴旋转
绕原点旋转
P=(x,y),绕原点逆时针旋转 \theta 到 P'。

旋转前:
x=r\cos\alpha,\qquad y=r\sin\alpha.
旋转后:
x'=r\cos(\alpha+\theta),\qquad y'=r\sin(\alpha+\theta).
和角公式
\begin{aligned}
\cos(\alpha+\theta)&=\cos\alpha\cos\theta-\sin\alpha\sin\theta,\\
\sin(\alpha+\theta)&=\sin\alpha\cos\theta+\cos\alpha\sin\theta.
\end{aligned}
代入,得到
\begin{aligned}
x'&=x\cos\theta-y\sin\theta,\\
y'&=x\sin\theta+y\cos\theta.
\end{aligned}
矩阵形式:
\begin{bmatrix}x'\\y'\end{bmatrix}
=\underbrace{\begin{bmatrix}\cos\theta&-\sin\theta\\\sin\theta&\cos\theta\end{bmatrix}}_{R(\theta)\;:\;2\times2}
\underbrace{\begin{bmatrix}x\\y\end{bmatrix}}_{2\times1}.
三维:绕 z 旋转,z 坐标不变
\begin{bmatrix}x'\\y'\\z'\end{bmatrix}
=\begin{bmatrix}\cos\theta&-\sin\theta&0\\\sin\theta&\cos\theta&0\\0&0&1\end{bmatrix}
\begin{bmatrix}x\\y\\z\end{bmatrix}.
对应分量:
\begin{cases}
x'=x\cos\theta-y\sin\theta,\\
y'=x\sin\theta+y\cos\theta,\\
z'=z.
\end{cases}
记为
R_z(\theta)=\begin{bmatrix}\cos\theta&-\sin\theta&0\\\sin\theta&\cos\theta&0\\0&0&1\end{bmatrix}.
绕 x 旋转

旋转矩阵:
R_x(\theta)=\begin{bmatrix}1&0&0\\0&\cos\theta&-\sin\theta\\0&\sin\theta&\cos\theta\end{bmatrix}.
x 不变,考虑 yz 分量:
y=r\cos\alpha,\qquad z=r\sin\alpha,
y'=r\cos(\alpha+\theta),\qquad z'=r\sin(\alpha+\theta).
所以
\begin{aligned}
y'&=y\cos\theta-z\sin\theta,\\
z'&=z\cos\theta+y\sin\theta
=y\sin\theta+z\cos\theta.
\end{aligned}
先写成两维,再补回 x:
\begin{bmatrix}y'\\z'\end{bmatrix}
=\begin{bmatrix}\cos\theta&-\sin\theta\\\sin\theta&\cos\theta\end{bmatrix}
\begin{bmatrix}y\\z\end{bmatrix},
\begin{bmatrix}x'\\y'\\z'\end{bmatrix}
=\begin{bmatrix}1&0&0\\0&\cos\theta&-\sin\theta\\0&\sin\theta&\cos\theta\end{bmatrix}
\begin{bmatrix}x\\y\\z\end{bmatrix}.
r 是 yz 分量的长度,即到 x 轴的距离。
绕 y 轴的方向,以及旋转的乘法顺序
绕 y 旋转:先改正方向
× 将绕 +y 的正转写成 \alpha+\theta。

✓ 在 x 向右、z 向上的投影中,绕 +y 正转表现为顺时针,角度减为 \alpha-\theta。

旋转前:
x=r\cos\alpha,\qquad z=r\sin\alpha.
旋转后:
x'=r\cos(\alpha-\theta),\qquad z'=r\sin(\alpha-\theta).
展开:
\begin{aligned}
x'&=r\cos\alpha\cos\theta+r\sin\alpha\sin\theta,\\
z'&=r\sin\alpha\cos\theta-r\cos\alpha\sin\theta.
\end{aligned}
回代:
x'=x\cos\theta+z\sin\theta,\qquad
z'=-x\sin\theta+z\cos\theta.
\begin{bmatrix}x'\\z'\end{bmatrix}
=\begin{bmatrix}\cos\theta&\sin\theta\\-\sin\theta&\cos\theta\end{bmatrix}
\begin{bmatrix}x\\z\end{bmatrix}.
补回不变的 y:
\begin{bmatrix}x'\\y'\\z'\end{bmatrix}
=\underbrace{\begin{bmatrix}\cos\theta&0&\sin\theta\\0&1&0\\-\sin\theta&0&\cos\theta\end{bmatrix}}_{R_y(\theta)}
\begin{bmatrix}x\\y\\z\end{bmatrix}.
代入 \theta=90^\circ 检查。
R_y(90^\circ)\begin{bmatrix}1\\0\\0\end{bmatrix}
=\begin{bmatrix}0\\0\\-1\end{bmatrix}.
原本指向 +x 的向量,绕 +y 正转 90°,指向 −z。
旋转顺序:左乘与右乘(ROS tf)

B′、C 是同一刚体从 B 出发的两种后续姿态。左乘绕 world,右乘绕自己。
用朝向矩阵 {}^W R_B 表示 b-link 在 world 中的朝向。左乘:
{}^W R_{B'}=R_x(\theta)\,{}^W R_B.
绕 world 的 x 轴旋转 \theta。
C=\text{b-link}\,R_x(\alpha) 写成矩阵形式:
{}^W R_C={}^W R_B\,R_x(\alpha).
绕 B 自己的 x 轴旋转 \alpha。
两条分支均从 B 出发。坐标架错开放置,便于比较朝向。
用齐次矩阵表示旋转和平移
平移
设
p=\begin{bmatrix}x\\y\\z\end{bmatrix},\qquad
t=\begin{bmatrix}a\\b\\c\end{bmatrix}.
沿三个方向分别平移后:
p'=p+t=\begin{bmatrix}x+a\\y+b\\z+c\end{bmatrix}.
旋转 + 平移

p''=Rp+t.
R 表示旋转,t 表示平移。图中结果记为 p'';后面的齐次变换部分用 p' 表示变换结果。
给杯子固定一个坐标系
如果只使用世界坐标系,杯子运动时,需要重新计算每个点在世界中的位置。

给杯子固定一个坐标系 B,杯子上的固定点相对 B 的坐标不变;更新 B 相对世界 W 的位姿即可。
{}^W p={}^W R_B\,{}^B p+{}^W t_B。其中 {}^W t_B 是 B 原点在 W 中的位置。
齐次矩阵(Homogeneous Matrix)
把旋转和平移统一成矩阵乘法。
给三维点增加一个分量:
p_h=\begin{bmatrix}x\\y\\z\\1\end{bmatrix}.
构造 4\times4 矩阵:
T=\begin{bmatrix}
r_{11}&r_{12}&r_{13}&t_x\\
r_{21}&r_{22}&r_{23}&t_y\\
r_{31}&r_{32}&r_{33}&t_z\\
0&0&0&1
\end{bmatrix}
=\begin{bmatrix}R&t\\0&1\end{bmatrix}.
乘刚才的四维坐标:
p'_h=Tp_h
=\begin{bmatrix}R&t\\0&1\end{bmatrix}\begin{bmatrix}p\\1\end{bmatrix}
=\begin{bmatrix}Rp+t\\1\end{bmatrix}.
例子
p=\begin{bmatrix}1\\0\\0\end{bmatrix}.
绕 z 轴旋转 90°,再沿 x 轴平移 2 个单位:
R=R_z(90^\circ)=\begin{bmatrix}0&-1&0\\1&0&0\\0&0&1\end{bmatrix},\qquad
t=\begin{bmatrix}2\\0\\0\end{bmatrix}.
T=\begin{bmatrix}0&-1&0&2\\1&0&0&0\\0&0&1&0\\0&0&0&1\end{bmatrix},
p'_h=Tp_h
=\begin{bmatrix}0&-1&0&2\\1&0&0&0\\0&0&1&0\\0&0&0&1\end{bmatrix}
\begin{bmatrix}1\\0\\0\\1\end{bmatrix}
=\begin{bmatrix}2\\1\\0\\1\end{bmatrix}.
所以 p'=(2,1,0)^\mathsf T。
变换顺序与逆变换
两个特例
只旋转、不平移,t=0:
T_R=\begin{bmatrix}R&0\\0&1\end{bmatrix}.
只平移、不旋转,R=I:
T_t=\begin{bmatrix}I&t\\0&1\end{bmatrix}.
先旋转、再平移:
p'_h=T_tT_Rp_h.
先平移、再旋转:
p'_h=T_RT_tp_h.
仍取 p=(1,0,0)^\mathsf T、R=R_z(90^\circ)、t=(2,0,0)^\mathsf T,这次先平移再旋转:
T_tp_h=\begin{bmatrix}3\\0\\0\\1\end{bmatrix},
T_R(T_tp_h)
=\begin{bmatrix}0&-1&0&0\\1&0&0&0\\0&0&1&0\\0&0&0&1\end{bmatrix}
\begin{bmatrix}3\\0\\0\\1\end{bmatrix}
=\begin{bmatrix}0\\3\\0\\1\end{bmatrix}.
得到 p'=(0,3,0)^\mathsf T。
矩阵乘法满足结合律,可以改变括号。一般不能交换顺序;靠近列向量的矩阵先作用。齐次中间向量的末项为 1。
逆矩阵:撤销之前的操作
旋转矩阵满足
R^{-1}=R^\mathsf T.
同轴的反向旋转:
R_i(\theta)^\mathsf T=R_i(-\theta).
齐次变换从
p'=Rp+t
开始,先撤销平移:
p'-t=Rp.
再撤销旋转:
R^{-1}(p'-t)=p,
p=R^\mathsf T(p'-t)
=\underbrace{R^\mathsf Tp'}_{\text{旋转回去}}
+\underbrace{(-R^\mathsf Tt)}_{\text{逆变换的平移项}}.
块矩阵形式:
\boxed{T^{-1}=\begin{bmatrix}R^\mathsf T&-R^\mathsf Tt\\0&1\end{bmatrix}.}
物体转动与换坐标系
主动旋转与被动旋转
换坐标系时,需要使用相应反向的旋转矩阵。
固定点 P 在 A 中的坐标:
{}^A p=\begin{bmatrix}1\\0\\0\end{bmatrix}.
B 由 A 绕共同 +z 轴逆时针旋转 90° 得到,原点重合:
{}^A R_B=R_z(90^\circ),\qquad
{}^A p={}^A R_B\,{}^B p.
所以换成 B 的坐标:
{}^B p=R_z(-90^\circ)\,{}^A p
=\begin{bmatrix}0\\-1\\0\end{bmatrix}.
被动变换保持点不动,换坐标系。主动旋转固定坐标系、转动点,前几页的 p'=Rp 属于这种情况。
数值例子
{}^A p=\begin{bmatrix}-2\\1\\1\end{bmatrix},\qquad
{}^A R_B=R_z(90^\circ).
单位圆上对应的坐标为 (\cos\theta,\sin\theta);在 -90^\circ 处为 (0,-1),所以

\cos(-90^\circ)=0,\qquad \sin(-90^\circ)=-1.
代入:
{}^B p=R_z(-90^\circ)\,{}^A p
=\begin{bmatrix}0&1&0\\-1&0&0\\0&0&1\end{bmatrix}
\begin{bmatrix}-2\\1\\1\end{bmatrix}
=\begin{bmatrix}1\\2\\1\end{bmatrix}.
再加入 C 坐标系
给定齐次矩阵 T_2={}^B T_C,坐标从 C 转换到 B。
T_2=\begin{bmatrix}0&0&-1&1\\1&0&0&2\\0&-1&0&0\\0&0&0&1\end{bmatrix}.
要求 {}^C p_h,因此
{}^C p_h=T_2^{-1}\,{}^B p_h.
先计算逆矩阵,再求点的坐标。
R=\begin{bmatrix}0&0&-1\\1&0&0\\0&-1&0\end{bmatrix},\qquad
R^\mathsf T=\begin{bmatrix}0&1&0\\0&0&-1\\-1&0&0\end{bmatrix},\qquad
t=\begin{bmatrix}1\\2\\0\end{bmatrix}.
R^\mathsf Tt
=\begin{bmatrix}0&1&0\\0&0&-1\\-1&0&0\end{bmatrix}\begin{bmatrix}1\\2\\0\end{bmatrix}
=\begin{bmatrix}2\\0\\-1\end{bmatrix},\qquad
-R^\mathsf Tt=\begin{bmatrix}-2\\0\\1\end{bmatrix}.
T_2^{-1}=\begin{bmatrix}R^\mathsf T&-R^\mathsf Tt\\0&1\end{bmatrix}
=\begin{bmatrix}0&1&0&-2\\0&0&-1&0\\-1&0&0&1\\0&0&0&1\end{bmatrix}.
接回计算:
{}^C p_h
=\begin{bmatrix}0&1&0&-2\\0&0&-1&0\\-1&0&0&1\\0&0&0&1\end{bmatrix}
\begin{bmatrix}1\\2\\1\\1\end{bmatrix}
=\begin{bmatrix}0\\-1\\0\\1\end{bmatrix}.
其中,第一行 2-2=0,第三行 -1+1=0。最终
{}^C p=\begin{bmatrix}0\\-1\\0\end{bmatrix}.
回到开头的视觉工位。如果已经知道相机相对夹具、夹具相对工作台的位姿,就可以像上面的例子一样,经夹具坐标系把点的坐标换算到工作台坐标系。
接下来,把同一套坐标变换用到平面二连杆机械臂的正运动学,从关节角算出末端位置和朝向。