PX4姿态控制数学基础:欧拉角变化率与角速度的本质关系深度解析

摘要

在多旋翼飞控系统的姿态控制中,欧拉角变化率与角速度是两个容易混淆但本质不同的概念。本文从李群微分几何、刚体动力学和工程实现三个层面,系统阐述两者的数学物理关系,揭示PX4采用四元数+角速度架构的深层原因,为飞控系统设计提供理论支撑。

关键词: 姿态控制、李群SO(3)、欧拉角、角速度、四元数、PX4


1. 引言:核心问题与常见误区

1.1 三个关键概念的辨析

在姿态控制系统中,我们经常遇到三个相关但不同的概念:

概念数学符号类型物理意义测量方式
欧拉角变化率[ϕ˙,θ˙,ψ˙][\dot{\phi}, \dot{\theta}, \dot{\psi}][ϕ˙,θ˙,ψ˙]矢量欧拉角参数的时间导数间接计算
角速度[ωx,ωy,ωz][\omega_x, \omega_y, \omega_z][ωx,ωy,ωz]矢量机体相对惯性系的瞬时旋转速度陀螺仪直接测量
角速率ω=∣ω∣\omega = |\boldsymbol{\omega}|ω=ω标量角速度矢量的模长计算得出

1.2 常见误区澄清

误区1: 俯仰角变化率等于俯仰角速度,即 θ˙=ωy\dot{\theta} = \omega_yθ˙=ωy

真相: 一般情况下 ωy≠θ˙\omega_y \neq \dot{\theta}ωy=θ˙,两者通过复杂的非线性变换相关:
ωy=cos⁡ϕ⋅θ˙+sin⁡ϕcos⁡θ⋅ψ˙\omega_y = \cos\phi \cdot \dot{\theta} + \sin\phi\cos\theta \cdot \dot{\psi}ωy=cosϕθ˙+sinϕcosθψ˙

误区2: 角速率等于姿态角变化率的模长

真相:
∥ω∥≠∥[ϕ˙,θ˙,ψ˙]∥\|\boldsymbol{\omega}\| \neq \|[\dot{\phi}, \dot{\theta}, \dot{\psi}]\|ω=[ϕ˙,θ˙,ψ˙]
两者之间的关系依赖于当前姿态,不存在简单的代数关系。

误区3: 三个姿态通道可以独立控制

真相: 由于欧拉角序贯旋转的本质,三个通道高度耦合,无法独立处理。


2. 数学理论基础:李群SO(3)的微分结构

2.1 旋转群SO(3)的几何本质

特殊正交群SO(3)定义为保持欧几里得内积不变的旋转变换集合:

SO(3)={R∈R3×3∣RTR=I,det⁡(R)=1}\text{SO}(3) = \{R \in \mathbb{R}^{3\times 3} \mid R^T R = I, \det(R) = 1\}SO(3)={RR3×3RTR=I,det(R)=1}

这个定义蕴含深刻的几何意义:

  • 保内积: 旋转不改变向量长度和夹角
  • 行列式为1: 排除反射变换,保证方向性
  • 流形结构: SO(3)是三维光滑流形,同胚于实射影空间RP3\mathbb{RP}^3RP3

SO(3)的拓扑性质直接导致了姿态表示的固有困难:

  • 基本群 π1(SO(3))=Z2\pi_1(\text{SO}(3)) = \mathbb{Z}_2π1(SO(3))=Z2 暗示了双覆盖性质
  • 不存在全局坐标系: 任何参数化(如欧拉角)都必然存在奇异点
  • 旋转非交换性: R1R2≠R2R1R_1 R_2 \neq R_2 R_1R1R2=R2R1 (一般情况)

2.2 李代数so(3)与角速度的同构

李代数so(3)是SO(3)在单位元处的切空间,由所有3×3反对称矩阵构成:

so(3)={Ω∈R3×3∣ΩT=−Ω}\text{so}(3) = \{\Omega \in \mathbb{R}^{3\times 3} \mid \Omega^T = -\Omega\}so(3)={ΩR3×3ΩT=Ω}

帽子映射(hat map)建立了角速度向量与李代数的自然同构:

 ^:R3→so(3),ω=[ωxωyωz]↦[ω]×=[0−ωzωyωz0−ωx−ωyωx0]\hat{\ } : \mathbb{R}^3 \to \text{so}(3), \quad \boldsymbol{\omega} = \begin{bmatrix} \omega_x \\ \omega_y \\ \omega_z \end{bmatrix} \mapsto [\boldsymbol{\omega}]_\times = \begin{bmatrix} 0 & -\omega_z & \omega_y \\ \omega_z & 0 & -\omega_x \\ -\omega_y & \omega_x & 0 \end{bmatrix} ^:R3so(3),ω=ωxωyωz[ω]×=0ωzωyωz0ωxωyωx0

这个映射的物理意义在于:

  • [ω]×v=ω×v[\boldsymbol{\omega}]_\times \mathbf{v} = \boldsymbol{\omega} \times \mathbf{v}[ω]×v=ω×v (叉积的矩阵表示)
  • 李括号对应角速度叉积: [[ω1]×,[ω2]×]=[ω1×ω2]×[[\boldsymbol{\omega}_1]_\times, [\boldsymbol{\omega}_2]_\times] = [\boldsymbol{\omega}_1 \times \boldsymbol{\omega}_2]_\times[[ω1]×,[ω2]×]=[ω1×ω2]×
  • 编码了角速度合成的非交换性

2.3 切映射与参数化

欧拉角 (ϕ,θ,ψ)(\phi, \theta, \psi)(ϕ,θ,ψ) 提供了SO(3)的局部坐标卡:

R(ϕ,θ,ψ)=Rz(ψ)Ry(θ)Rx(ϕ)R(\phi, \theta, \psi) = R_z(\psi)R_y(\theta)R_x(\phi)R(ϕ,θ,ψ)=Rz(ψ)Ry(θ)Rx(ϕ)

设参数空间曲线 γ(t)=(ϕ(t),θ(t),ψ(t))\gamma(t) = (\phi(t), \theta(t), \psi(t))γ(t)=(ϕ(t),θ(t),ψ(t)),对应的旋转矩阵曲线为 R(t)=R(γ(t))R(t) = R(\gamma(t))R(t)=R(γ(t))。通过链式法则:

R˙=∂R∂ϕϕ˙+∂R∂θθ˙+∂R∂ψψ˙\dot{R} = \frac{\partial R}{\partial \phi} \dot{\phi} + \frac{\partial R}{\partial \theta} \dot{\theta} + \frac{\partial R}{\partial \psi} \dot{\psi}R˙=ϕRϕ˙+θRθ˙+ψRψ˙

角速度定义为:
[ω]×=RTR˙[\boldsymbol{\omega}]_\times = R^T \dot{R}[ω]×=RTR˙

这就是切映射(tangent map),将参数空间的速度映射到李代数。经过复杂的矩阵运算,可以得到:

ω=W(ϕ,θ)[ϕ˙θ˙ψ˙]\boldsymbol{\omega} = W(\phi, \theta) \begin{bmatrix} \dot{\phi} \\ \dot{\theta} \\ \dot{\psi} \end{bmatrix}ω=W(ϕ,θ)ϕ˙θ˙ψ˙

其中转换矩阵:

W(ϕ,θ)=[10−sin⁡θ0cos⁡ϕsin⁡ϕcos⁡θ0−sin⁡ϕcos⁡ϕcos⁡θ]\boxed{W(\phi, \theta) = \begin{bmatrix} 1 & 0 & -\sin\theta \\ 0 & \cos\phi & \sin\phi\cos\theta \\ 0 & -\sin\phi & \cos\phi\cos\theta \end{bmatrix}}W(ϕ,θ)=1000cosϕsinϕsinθsinϕcosθcosϕcosθ


3. 转换矩阵的物理推导

3.1 序贯旋转的几何意义

ZYX欧拉角(Roll-Pitch-Yaw)的旋转序列:

  1. 世界系Z轴旋转 ψ\psiψ (yaw)
  2. 新Y轴旋转 θ\thetaθ (pitch)
  3. 新X轴旋转 ϕ\phiϕ (roll)

关键洞察:每次旋转都在已旋转的坐标系中进行,这是耦合产生的根源。

3.2 角速度的分量合成

总角速度由三部分贡献:

ωtotal=ωϕ+ωθ+ωψ\boldsymbol{\omega}_{\text{total}} = \boldsymbol{\omega}_\phi + \boldsymbol{\omega}_\theta + \boldsymbol{\omega}_\psiωtotal=ωϕ+ωθ+ωψ

Roll贡献(绕机体X轴):
ωϕ=ϕ˙[100]body\boldsymbol{\omega}_\phi = \dot{\phi} \begin{bmatrix} 1 \\ 0 \\ 0 \end{bmatrix}_{\text{body}}ωϕ=ϕ˙100body

Pitch贡献(绕已roll的Y’轴):

Pitch旋转轴在机体系的表示:
ey′=Rx(−ϕ)[010]=[0cos⁡ϕ−sin⁡ϕ]body\mathbf{e}_{y'} = R_x(-\phi) \begin{bmatrix} 0 \\ 1 \\ 0 \end{bmatrix} = \begin{bmatrix} 0 \\ \cos\phi \\ -\sin\phi \end{bmatrix}_{\text{body}}ey=Rx(ϕ)010=0cosϕsinϕbody

因此:
ωθ=θ˙[0cos⁡ϕ−sin⁡ϕ]body\boldsymbol{\omega}_\theta = \dot{\theta} \begin{bmatrix} 0 \\ \cos\phi \\ -\sin\phi \end{bmatrix}_{\text{body}}ωθ=θ˙0cosϕsinϕbody

Yaw贡献(绕世界系Z轴):

世界Z轴在机体系的表示:
ezbody=Rx(−ϕ)Ry(−θ)[001]=[−sin⁡θsin⁡ϕcos⁡θcos⁡ϕcos⁡θ]\mathbf{e}_z^{\text{body}} = R_x(-\phi) R_y(-\theta) \begin{bmatrix} 0 \\ 0 \\ 1 \end{bmatrix} = \begin{bmatrix} -\sin\theta \\ \sin\phi\cos\theta \\ \cos\phi\cos\theta \end{bmatrix}ezbody=Rx(ϕ)Ry(θ)001=sinθsinϕcosθcosϕcosθ

因此:
ωψ=ψ˙[−sin⁡θsin⁡ϕcos⁡θcos⁡ϕcos⁡θ]body\boldsymbol{\omega}_\psi = \dot{\psi} \begin{bmatrix} -\sin\theta \\ \sin\phi\cos\theta \\ \cos\phi\cos\theta \end{bmatrix}_{\text{body}}ωψ=ψ˙sinθsinϕcosθcosϕcosθbody

3.3 完整转换关系

合并三个分量,得到分通道展开形式:

{ωx=ϕ˙−sin⁡θ⋅ψ˙ωy=cos⁡ϕ⋅θ˙+sin⁡ϕcos⁡θ⋅ψ˙ωz=−sin⁡ϕ⋅θ˙+cos⁡ϕcos⁡θ⋅ψ˙\boxed{\begin{cases} \omega_x = \dot{\phi} - \sin\theta \cdot \dot{\psi} \\ \omega_y = \cos\phi \cdot \dot{\theta} + \sin\phi\cos\theta \cdot \dot{\psi} \\ \omega_z = -\sin\phi \cdot \dot{\theta} + \cos\phi\cos\theta \cdot \dot{\psi} \end{cases}}ωx=ϕ˙sinθψ˙ωy=cosϕθ˙+sinϕcosθψ˙ωz=sinϕθ˙+cosϕcosθψ˙

关键观察:

  • 每个角速度分量都受多个欧拉角变化率影响
  • 转换系数依赖于当前姿态角 (ϕ,θ)(\phi, \theta)(ϕ,θ)
  • θ→±90°\theta \to \pm 90°θ±90° 时,WWW 变为奇异(万向锁)

3.4 逆向转换

求解 W−1W^{-1}W1 得到:

{ϕ˙=ωx+sin⁡ϕtan⁡θ⋅ωy+cos⁡ϕtan⁡θ⋅ωzθ˙=cos⁡ϕ⋅ωy−sin⁡ϕ⋅ωzψ˙=sin⁡ϕcos⁡θ⋅ωy+cos⁡ϕcos⁡θ⋅ωz\boxed{\begin{cases} \dot{\phi} = \omega_x + \sin\phi\tan\theta \cdot \omega_y + \cos\phi\tan\theta \cdot \omega_z \\ \dot{\theta} = \cos\phi \cdot \omega_y - \sin\phi \cdot \omega_z \\ \dot{\psi} = \dfrac{\sin\phi}{\cos\theta} \cdot \omega_y + \dfrac{\cos\phi}{\cos\theta} \cdot \omega_z \end{cases}}ϕ˙=ωx+sinϕtanθωy+cosϕtanθωzθ˙=cosϕωysinϕωzψ˙=cosθsinϕωy+cosθcosϕωz

警告: 包含 tan⁡θ\tan\thetatanθsec⁡θ=1/cos⁡θ\sec\theta = 1/\cos\thetasecθ=1/cosθ,在 θ=±90°\theta = \pm 90°θ=±90° 时发散!


4. 动力学方程的表示形式

4.1 欧拉方程:角速度表示

刚体的欧拉方程在机体坐标系下表示为:

Iω˙+ω×(Iω)=τ\boxed{\mathbf{I}\dot{\boldsymbol{\omega}} + \boldsymbol{\omega} \times (\mathbf{I}\boldsymbol{\omega}) = \boldsymbol{\tau}}Iω˙+ω×(Iω)=τ

其中:

  • I=diag(Ixx,Iyy,Izz)\mathbf{I} = \text{diag}(I_{xx}, I_{yy}, I_{zz})I=diag(Ixx,Iyy,Izz) 是惯性张量
  • ω×(Iω)\boldsymbol{\omega} \times (\mathbf{I}\boldsymbol{\omega})ω×(Iω) 是陀螺力矩项
  • τ\boldsymbol{\tau}τ 是外加控制力矩

展开为三个耦合方程:
{Ixxω˙x=(Iyy−Izz)ωyωz+τxIyyω˙y=(Izz−Ixx)ωzωx+τyIzzω˙z=(Ixx−Iyy)ωxωy+τz\begin{cases} I_{xx} \dot{\omega}_x = (I_{yy} - I_{zz})\omega_y \omega_z + \tau_x \\ I_{yy} \dot{\omega}_y = (I_{zz} - I_{xx})\omega_z \omega_x + \tau_y \\ I_{zz} \dot{\omega}_z = (I_{xx} - I_{yy})\omega_x \omega_y + \tau_z \end{cases}Ixxω˙x=(IyyIzz)ωyωz+τxIyyω˙y=(IzzIxx)ωzωx+τyIzzω˙z=(IxxIyy)ωxωy+τz

优势:

  • 形式简洁,物理意义清晰
  • 直接反映角动量守恒
  • 非线性耦合项明确,便于控制补偿

4.2 欧拉-拉格朗日方程:欧拉角表示

用欧拉角参数化时,拉格朗日方法给出:

动能: T=12ωTIω=12η˙TM(η)η˙T = \frac{1}{2}\boldsymbol{\omega}^T \mathbf{I} \boldsymbol{\omega} = \frac{1}{2}\dot{\boldsymbol{\eta}}^T M(\boldsymbol{\eta}) \dot{\boldsymbol{\eta}}T=21ωTIω=21η˙TM(η)η˙

其中广义惯性矩阵:
M(η)=WT(ϕ,θ)IW(ϕ,θ)M(\boldsymbol{\eta}) = W^T(\phi, \theta) \mathbf{I} W(\phi, \theta)M(η)=WT(ϕ,θ)IW(ϕ,θ)

欧拉-拉格朗日方程:
M(η)η¨+C(η,η˙)η˙=WT(ϕ,θ)τBM(\boldsymbol{\eta})\ddot{\boldsymbol{\eta}} + C(\boldsymbol{\eta}, \dot{\boldsymbol{\eta}})\dot{\boldsymbol{\eta}} = W^T(\phi, \theta)\boldsymbol{\tau}_BM(η)η¨+C(η,η˙)η˙=WT(ϕ,θ)τB

其中 CCC 包含科里奥利力和离心力项。

劣势:

  • MMMCCC 高度非线性,依赖姿态
  • 奇异点处 MMM 不可逆
  • 计算复杂度高
  • 物理直观性差

这解释了为什么现代飞控采用欧拉方程而非欧拉-拉格朗日方程。


5. 控制理论视角

5.1 线性化与小角度近似

在悬停点附近 (∣ϕ∣,∣θ∣,∣ψ∣≪1)(|\phi|, |\theta|, |\psi| \ll 1)(ϕ,θ,ψ1):

sin⁡ϕ≈ϕ,cos⁡ϕ≈1,sin⁡θ≈θ,cos⁡θ≈1\sin\phi \approx \phi, \quad \cos\phi \approx 1, \quad \sin\theta \approx \theta, \quad \cos\theta \approx 1sinϕϕ,cosϕ1,sinθθ,cosθ1

转换矩阵简化为:
W≈[10−θ010001]≈IW \approx \begin{bmatrix} 1 & 0 & -\theta \\ 0 & 1 & 0 \\ 0 & 0 & 1 \end{bmatrix} \approx IW100010θ01I

此时:
ωx≈ϕ˙−θψ˙,ωy≈θ˙,ωz≈ψ˙\omega_x \approx \dot{\phi} - \theta\dot{\psi}, \quad \omega_y \approx \dot{\theta}, \quad \omega_z \approx \dot{\psi}ωxϕ˙θψ˙,ωyθ˙,ωzψ˙

这就是为什么简单PID在小角度范围有效。但随着角度增大:

  • 耦合项 −θψ˙-\theta\dot{\psi}θψ˙ 不可忽略
  • 三角函数非线性凸显
  • 简单PID性能退化

5.2 四元数表示的几何优势

单位四元数 S3={q∈H∣∣q∣=1}\mathbb{S}^3 = \{q \in \mathbb{H} \mid |q| = 1\}S3={qHq=1} 提供SO(3)的双覆盖。

核心优势:

  1. 线性微分关系:
    q˙=12q⊗ωq\dot{q} = \frac{1}{2} q \otimes \omega_qq˙=21qωq
    其中 ωq=[0,ωx,ωy,ωz]T\omega_q = [0, \omega_x, \omega_y, \omega_z]^Tωq=[0,ωx,ωy,ωz]T,无需复杂转换矩阵。

  2. 无奇异性:
    整个SO(3)都被平滑覆盖,无万向锁问题。

  3. 姿态误差的几何意义:
    qe=q∗⊗qdq_e = q^* \otimes q_dqe=qqd
    其虚部 2[qe1,qe2,qe3]T2[q_{e1}, q_{e2}, q_{e3}]^T2[qe1,qe2,qe3]T 近似为轴角误差。

  4. 控制律简洁性:
    τ=−Kpsgn(qe0)[qe1qe2qe3]−Kdω\boldsymbol{\tau} = -K_p \text{sgn}(q_{e0}) \begin{bmatrix} q_{e1} \\ q_{e2} \\ q_{e3} \end{bmatrix} - K_d \boldsymbol{\omega}τ=Kpsgn(qe0)qe1qe2qe3Kdω

  5. 数值稳定性:
    只需归一化约束 ∣q∣=1|q| = 1q=1,而旋转矩阵需满足9个正交约束。


6. 传感器与状态估计

6.1 陀螺仪测量原理

MEMS陀螺仪基于科里奥利效应,直接测量角速度:

ωm=ωtrue+bg+ng\boldsymbol{\omega}_m = \boldsymbol{\omega}_{\text{true}} + \mathbf{b}_g + \mathbf{n}_gωm=ωtrue+bg+ng

其中:

  • bg\mathbf{b}_gbg: 缓变偏置(bias)
  • ng\mathbf{n}_gng: 高斯白噪声

关键特性:

  • 带宽: 几百Hz到几kHz
  • 直接输出角速度,无需转换
  • 高更新率支持快速角速率控制环(1-8kHz)

这是级联控制结构(姿态外环+角速率内环)的硬件基础。

6.2 姿态估计的信息融合

传感器互补:

  • 陀螺仪: 高频角速度,短期精确,长期漂移
  • 加速度计: 静态/匀速时测量重力方向 gB=RTgI\mathbf{g}_B = R^T \mathbf{g}_IgB=RTgI,提供roll/pitch
  • 磁力计: 地磁场方向 mB=RTmI\mathbf{m}_B = R^T \mathbf{m}_ImB=RTmI,倾斜补偿后提供yaw

扩展卡尔曼滤波(EKF)框架:

状态预测(四元数):
qk+1=qk⊗exp⁡(12ωkΔt)q_{k+1} = q_k \otimes \exp\left(\frac{1}{2}\boldsymbol{\omega}_k \Delta t\right)qk+1=qkexp(21ωkΔt)

若用欧拉角:
[ϕ,θ,ψ]k+1=[ϕ,θ,ψ]k+W−1(ϕk,θk)ωkΔt[\phi, \theta, \psi]_{k+1} = [\phi, \theta, \psi]_k + W^{-1}(\phi_k, \theta_k) \boldsymbol{\omega}_k \Delta t[ϕ,θ,ψ]k+1=[ϕ,θ,ψ]k+W1(ϕk,θk)ωkΔt

问题: 需计算 W−1W^{-1}W1,在奇异点数值不稳定。

误差状态卡尔曼滤波(ESKF)混合方案:

  • 名义状态用四元数(全局无奇异)
  • 误差状态用小角度 δθ\delta\boldsymbol{\theta}δθ (线性化便利)
  • 结合两者优势,成为主流方案

7. 非线性控制策略

7.1 滑模控制

设计滑模面:
s=ω−ωd+λ(qv−qvd)\mathbf{s} = \boldsymbol{\omega} - \boldsymbol{\omega}_d + \lambda(\mathbf{q}_v - \mathbf{q}_{vd})s=ωωd+λ(qvqvd)

控制律:
τ=−I(ω×(Iω))+I(ω˙d−λωe)−Ksgn(s)\boldsymbol{\tau} = -\mathbf{I}(\boldsymbol{\omega} \times (\mathbf{I}\boldsymbol{\omega})) + \mathbf{I}(\dot{\boldsymbol{\omega}}_d - \lambda\boldsymbol{\omega}_e) - K \text{sgn}(\mathbf{s})τ=I(ω×(Iω))+I(ω˙dλωe)Ksgn(s)

优势: 对参数不确定性和扰动鲁棒

挑战: 符号函数引起抖振,需饱和函数或高阶滑模(如Super-twisting)缓解

7.2 反步法(Backstepping)

步骤1: 姿态运动学层,设计虚拟控制

Lyapunov函数: V1=12∥qv∥2V_1 = \frac{1}{2}\|\mathbf{q}_v\|^2V1=21qv2

虚拟控制: ωd=−K1qv\boldsymbol{\omega}_d = -K_1 \mathbf{q}_vωd=K1qv 使 V˙1≤0\dot{V}_1 \leq 0V˙10

步骤2: 角速度动力学层,设计实际力矩

增广Lyapunov: V2=V1+12zTIzV_2 = V_1 + \frac{1}{2}\mathbf{z}^T \mathbf{I} \mathbf{z}V2=V1+21zTIz,其中 z=ω−ωd\mathbf{z} = \boldsymbol{\omega} - \boldsymbol{\omega}_dz=ωωd

控制律: τ=I(ω˙d−ω×(I−1τ))−K2z−qv\boldsymbol{\tau} = \mathbf{I}(\dot{\boldsymbol{\omega}}_d - \boldsymbol{\omega} \times (\mathbf{I}^{-1}\boldsymbol{\tau})) - K_2 \mathbf{z} - \mathbf{q}_vτ=I(ω˙dω×(I1τ))K2zqv

保证: V˙2≤−K1∥qv∥2−K2∥z∥2<0\dot{V}_2 \leq -K_1\|\mathbf{q}_v\|^2 - K_2\|\mathbf{z}\|^2 < 0V˙2K1qv2K2z2<0 (全局渐近稳定)

扩展: 自适应反步法可在线估计惯性矩阵不确定性。


9. PX4的架构选择

9.1 为什么不用欧拉角变化率?

维度欧拉角变化率方案角速度方案(PX4)
物理定律不符合欧拉方程直接对应欧拉方程
传感器间接计算,噪声大陀螺仪直接测量
奇异性万向锁 θ=±90°\theta=\pm90°θ=±90°四元数无奇异
耦合三通道强耦合机体系独立
转换需计算W(ϕ,θ)W(\phi,\theta)W(ϕ,θ)四元数线性微分
数值三角函数密集仅代数运算
控制设计复杂非线性几何直观

9.2 PX4的实际实现

// 姿态控制器(外环) - 基于四元数
Quatf q_error = q_current.inversed() * q_desired;
Vector3f attitude_error = 2.0f * q_error.canonical().imag();
Vector3f rate_setpoint = attitude_error.emult(_proportional_gain);

// 角速率控制器(内环) - 直接使用角速度
Vector3f angular_velocity = imu.gyro;  // 陀螺仪直接输出
Vector3f rate_error = rate_setpoint - angular_velocity;
Vector3f torque = rate_error.emult(_rate_gain) 
                  + feedforward_compensation;

核心理念: 控制物理量(角速度),而非数学表示(欧拉角变化率)。


10. 代码实现参考

10.1 前向转换:欧拉角变化率→角速度

#include <matrix/math.hpp>
#include <cmath>

using namespace matrix;

Vector3f eulerRateToAngularVelocity(
    float phi, float theta, const Vector3f& euler_rate)
{
    float sphi = sinf(phi);
    float cphi = cosf(phi);
    float stheta = sinf(theta);
    float ctheta = cosf(theta);
    
    Vector3f omega;
    omega(0) = euler_rate(0) - stheta * euler_rate(2);
    omega(1) = cphi * euler_rate(1) + sphi * ctheta * euler_rate(2);
    omega(2) = -sphi * euler_rate(1) + cphi * ctheta * euler_rate(2);
    
    return omega;
}

10.2 逆向转换:角速度→欧拉角变化率

Vector3f angularVelocityToEulerRate(
    float phi, float theta, const Vector3f& omega)
{
    float sphi = sinf(phi);
    float cphi = cosf(phi);
    float ttheta = tanf(theta);
    float sectheta = 1.0f / cosf(theta);  // 警告:奇异性!
    
    Vector3f euler_rate;
    euler_rate(0) = omega(0) + sphi * ttheta * omega(1) 
                    + cphi * ttheta * omega(2);
    euler_rate(1) = cphi * omega(1) - sphi * omega(2);
    euler_rate(2) = sphi * sectheta * omega(1) 
                    + cphi * sectheta * omega(2);
    
    return euler_rate;
}

注意: 当 ∣θ∣→90°|\theta| \to 90°θ90° 时,sectheta 发散,必须检查奇异性!

10.3 使用建议

// ✅ 推荐:直接使用角速度
Vector3f angular_velocity = imu.gyro;
Vector3f rate_sp = attitude_control.update(q);
Vector3f torque = rate_control.update(angular_velocity, rate_sp, dt);

// ❌ 不推荐:绕道欧拉角变化率
Eulerf euler(q);
Vector3f euler_rate = compute_euler_rate(euler, euler_prev, dt);
Vector3f angular_velocity = eulerRateToAngularVelocity(
    euler.phi(), euler.theta(), euler_rate);  // 多此一举

11. 总结与展望

11.1 核心要点回顾

  1. 本质区别:

    • 欧拉角变化率是参数空间的速度
    • 角速度是流形切空间的速度
    • 通过依赖姿态的转换矩阵 W(ϕ,θ)W(\phi,\theta)W(ϕ,θ) 非线性关联
  2. 数学根源:

    • SO(3)的非欧几何结构
    • 李群-李代数对应
    • 欧拉角序贯旋转的非交换性
  3. 工程实践:

    • 四元数+角速度架构避免奇异性
    • 直接对应物理定律和传感器测量
    • 计算高效,数值稳定
  4. 控制设计:

    • 小角度线性化适用PID
    • 大机动需非线性方法(滑模/反步)
    • 四元数提供几何直观的控制律

附录:完整转换公式速查

前向转换(欧拉角变化率→角速度)

[ωxωyωz]=[10−sin⁡θ0cos⁡ϕsin⁡ϕcos⁡θ0−sin⁡ϕcos⁡ϕcos⁡θ][ϕ˙θ˙ψ˙]\begin{bmatrix} \omega_x \\ \omega_y \\ \omega_z \end{bmatrix} = \begin{bmatrix} 1 & 0 & -\sin\theta \\ 0 & \cos\phi & \sin\phi\cos\theta \\ 0 & -\sin\phi & \cos\phi\cos\theta \end{bmatrix} \begin{bmatrix} \dot{\phi} \\ \dot{\theta} \\ \dot{\psi} \end{bmatrix}ωxωyωz=1000cosϕsinϕsinθsinϕcosθcosϕcosθϕ˙θ˙ψ˙

逆向转换(角速度→欧拉角变化率)

[ϕ˙θ˙ψ˙]=[1sin⁡ϕtan⁡θcos⁡ϕtan⁡θ0cos⁡ϕ−sin⁡ϕ0sin⁡ϕsec⁡θcos⁡ϕsec⁡θ][ωxωyωz]\begin{bmatrix} \dot{\phi} \\ \dot{\theta} \\ \dot{\psi} \end{bmatrix} = \begin{bmatrix} 1 & \sin\phi\tan\theta & \cos\phi\tan\theta \\ 0 & \cos\phi & -\sin\phi \\ 0 & \sin\phi\sec\theta & \cos\phi\sec\theta \end{bmatrix} \begin{bmatrix} \omega_x \\ \omega_y \\ \omega_z \end{bmatrix}ϕ˙θ˙ψ˙=100sinϕtanθcosϕsinϕsecθcosϕtanθsinϕcosϕsecθωxωyωz

角速率计算

ω=∥[10−sin⁡θ0cos⁡ϕsin⁡ϕcos⁡θ0−sin⁡ϕcos⁡ϕcos⁡θ][ϕ˙θ˙ψ˙]∥\omega = \left\|\begin{bmatrix} 1 & 0 & -\sin\theta \\ 0 & \cos\phi & \sin\phi\cos\theta \\ 0 & -\sin\phi & \cos\phi\cos\theta \end{bmatrix} \begin{bmatrix} \dot{\phi} \\ \dot{\theta} \\ \dot{\psi} \end{bmatrix}\right\|ω=1000cosϕsinϕsinθsinϕcosθcosϕcosθϕ˙θ˙ψ˙

欧拉角变化率到角速度的分量展开

根据矩阵乘法规则,将转换矩阵与欧拉角变化率向量相乘:

完整展开过程

[ωxωyωz]=[10−sin⁡θ0cos⁡ϕsin⁡ϕcos⁡θ0−sin⁡ϕcos⁡ϕcos⁡θ][ϕ˙θ˙ψ˙]\begin{bmatrix} \omega_x \\ \omega_y \\ \omega_z \end{bmatrix} = \begin{bmatrix} 1 & 0 & -\sin\theta \\ 0 & \cos\phi & \sin\phi\cos\theta \\ 0 & -\sin\phi & \cos\phi\cos\theta \end{bmatrix} \begin{bmatrix} \dot{\phi} \\ \dot{\theta} \\ \dot{\psi} \end{bmatrix}ωxωyωz=1000cosϕsinϕsinθsinϕcosθcosϕcosθϕ˙θ˙ψ˙

分量展开

ωx\omega_xωx (Roll轴角速度)

取转换矩阵第一行与欧拉角变化率向量做内积:

ωx=1⋅ϕ˙+0⋅θ˙+(−sin⁡θ)⋅ψ˙\omega_x = 1 \cdot \dot{\phi} + 0 \cdot \dot{\theta} + (-\sin\theta) \cdot \dot{\psi}ωx=1ϕ˙+0θ˙+(sinθ)ψ˙

ωx=ϕ˙−sin⁡θ⋅ψ˙\boxed{\omega_x = \dot{\phi} - \sin\theta \cdot \dot{\psi}}ωx=ϕ˙sinθψ˙


ωy\omega_yωy (Pitch轴角速度)

取转换矩阵第二行与欧拉角变化率向量做内积:

ωy=0⋅ϕ˙+cos⁡ϕ⋅θ˙+(sin⁡ϕcos⁡θ)⋅ψ˙\omega_y = 0 \cdot \dot{\phi} + \cos\phi \cdot \dot{\theta} + (\sin\phi\cos\theta) \cdot \dot{\psi}ωy=0ϕ˙+cosϕθ˙+(sinϕcosθ)ψ˙

ωy=cos⁡ϕ⋅θ˙+sin⁡ϕcos⁡θ⋅ψ˙\boxed{\omega_y = \cos\phi \cdot \dot{\theta} + \sin\phi\cos\theta \cdot \dot{\psi}}ωy=cosϕθ˙+sinϕcosθψ˙


ωz\omega_zωz (Yaw轴角速度)

取转换矩阵第三行与欧拉角变化率向量做内积:

ωz=0⋅ϕ˙+(−sin⁡ϕ)⋅θ˙+(cos⁡ϕcos⁡θ)⋅ψ˙\omega_z = 0 \cdot \dot{\phi} + (-\sin\phi) \cdot \dot{\theta} + (\cos\phi\cos\theta) \cdot \dot{\psi}ωz=0ϕ˙+(sinϕ)θ˙+(cosϕcosθ)ψ˙

ωz=−sin⁡ϕ⋅θ˙+cos⁡ϕcos⁡θ⋅ψ˙\boxed{\omega_z = -\sin\phi \cdot \dot{\theta} + \cos\phi\cos\theta \cdot \dot{\psi}}ωz=sinϕθ˙+cosϕcosθψ˙


最终结果(三个分量)

{ωx=ϕ˙−sin⁡θ⋅ψ˙ωy=cos⁡ϕ⋅θ˙+sin⁡ϕcos⁡θ⋅ψ˙ωz=−sin⁡ϕ⋅θ˙+cos⁡ϕcos⁡θ⋅ψ˙\boxed{ \begin{cases} \omega_x = \dot{\phi} - \sin\theta \cdot \dot{\psi} \\[10pt] \omega_y = \cos\phi \cdot \dot{\theta} + \sin\phi\cos\theta \cdot \dot{\psi} \\[10pt] \omega_z = -\sin\phi \cdot \dot{\theta} + \cos\phi\cos\theta \cdot \dot{\psi} \end{cases} }ωx=ϕ˙sinθψ˙ωy=cosϕθ˙+sinϕcosθψ˙ωz=sinϕθ˙+cosϕcosθψ˙


物理意义解读

角速度分量主要贡献次要贡献耦合特性
ωx\omega_xωxRoll变化率 ϕ˙\dot{\phi}ϕ˙Yaw变化率 ψ˙\dot{\psi}ψ˙(受俯仰角调制)Pitch角度影响Yaw贡献
ωy\omega_yωyPitch变化率 θ˙\dot{\theta}θ˙Yaw变化率 ψ˙\dot{\psi}ψ˙(受Roll和Pitch调制)两个姿态角共同调制
ωz\omega_zωzYaw变化率 ψ˙\dot{\psi}ψ˙Pitch变化率 θ˙\dot{\theta}θ˙(受Roll调制)Roll角度分配Pitch和Yaw贡献

关键观察

  • 每个角速度分量都受多个欧拉角变化率影响
  • 耦合系数依赖于当前姿态角 (ϕ,θ)(\phi, \theta)(ϕ,θ)
  • 这就是为什么三个通道不能独立控制

C++代码实现

// 方法1:直接计算
void computeAngularVelocity(
    float phi, float theta,           // 当前姿态角
    float dphi, float dtheta, float dpsi,  // 欧拉角变化率
    float& omega_x, float& omega_y, float& omega_z)  // 输出角速度
{
    float sin_phi = sinf(phi);
    float cos_phi = cosf(phi);
    float sin_theta = sinf(theta);
    float cos_theta = cosf(theta);
    
    // Roll轴
    omega_x = dphi - sin_theta * dpsi;
    
    // Pitch轴
    omega_y = cos_phi * dtheta + sin_phi * cos_theta * dpsi;
    
    // Yaw轴
    omega_z = -sin_phi * dtheta + cos_phi * cos_theta * dpsi;
}

// 方法2:使用Matrix库
#include <matrix/math.hpp>

matrix::Vector3f eulerRateToAngularVelocity(
    float phi, float theta, 
    const matrix::Vector3f& euler_rate)
{
    float sphi = sinf(phi);
    float cphi = cosf(phi);
    float stheta = sinf(theta);
    float ctheta = cosf(theta);
    
    matrix::Vector3f omega;
    omega(0) = euler_rate(0) - stheta * euler_rate(2);
    omega(1) = cphi * euler_rate(1) + sphi * ctheta * euler_rate(2);
    omega(2) = -sphi * euler_rate(1) + cphi * ctheta * euler_rate(2);
    
    return omega;
}

Python验证代码

import numpy as np

def euler_rate_to_angular_velocity(phi, theta, dphi, dtheta, dpsi):
    """
    将欧拉角变化率转换为角速度
    
    Args:
        phi: Roll角 (rad)
        theta: Pitch角 (rad)
        dphi: Roll变化率 (rad/s)
        dtheta: Pitch变化率 (rad/s)
        dpsi: Yaw变化率 (rad/s)
    
    Returns:
        omega_x, omega_y, omega_z: 角速度分量 (rad/s)
    """
    sin_phi = np.sin(phi)
    cos_phi = np.cos(phi)
    sin_theta = np.sin(theta)
    cos_theta = np.cos(theta)
    
    # 按公式计算
    omega_x = dphi - sin_theta * dpsi
    omega_y = cos_phi * dtheta + sin_phi * cos_theta * dpsi
    omega_z = -sin_phi * dtheta + cos_phi * cos_theta * dpsi
    
    return omega_x, omega_y, omega_z

# 测试
if __name__ == "__main__":
    # 水平姿态
    phi, theta = 0, 0
    dphi, dtheta, dpsi = 1.0, 1.0, 0.0
    
    wx, wy, wz = euler_rate_to_angular_velocity(phi, theta, dphi, dtheta, dpsi)
    print(f"水平姿态: ωx={wx:.3f}, ωy={wy:.3f}, ωz={wz:.3f}")
    # 预期: ωx=1.0, ωy=1.0, ωz=0.0
    
    # 有俯仰角
    phi, theta = 0, np.radians(45)
    dphi, dtheta, dpsi = 1.0, 0.0, 1.0
    
    wx, wy, wz = euler_rate_to_angular_velocity(phi, theta, dphi, dtheta, dpsi)
    print(f"俯仰45°: ωx={wx:.3f}, ωy={wy:.3f}, ωz={wz:.3f}")
    # ωx受θ影响, ωz受θ影响
Logo

北京人形旗下天工造物具身智能开源社区,聚焦具身天工与慧思开物两大平台

更多推荐