fly_ruler_std.libs.rotation
本页由
fr-docs-dev gen-api从源码 docstring 生成,请勿手工编辑;重新生成执行just api。
Signal 优先的四元数与旋转矩阵工具。
所有函数构造 FlyRuler Signal 表达式,供 @fr.block 声明使用;四元数按 scalar-first 顺序存储为 [w, x, y, z]。RPY (rad) 表示机体系到 NED 的 Rz(psi) Ry(theta) Rx(phi) 主动旋转。四元数姿态操作假设单位四元数;角速度为机体系分量。
公开成员
| 名称 | 类型 | 定义位置 |
|---|---|---|
Quaternion | 类 | 本页 |
RotationMatrix | 类 | 本页 |
定义
Quaternion
python
class Quaternion四元数运算的静态方法集合,供 @fr.block 声明使用。
identity
python
def identity() -> Signal单位四元数 [1, 0, 0, 0]。
返回
| 类型 | 说明 |
|---|---|
Signal | 单位四元数 (4,)。 |
python
import fly_ruler_core as fr
from fly_ruler_std.libs.rotation import Quaternion
@fr.block
def Attitude():
fr.output("quat_unit", Quaternion.identity(), dim=(4,))实现位置:packages/fly_ruler/python/fly_ruler_std/libs/rotation.py:22
normalize
python
def normalize(q: Signal) -> Signal把非零四元数归一化为单位长度。
零四元数没有物理姿态,本函数不为其提供静默回退值。
参数
| 名称 | 类型 | 默认值 | 说明 |
|---|---|---|---|
q | Signal | — | 待归一化的四元数 (4, scalar-first)。 |
返回
| 类型 | 说明 |
|---|---|
Signal | 单位四元数 (4,)。 |
python
import fly_ruler_core as fr
from fly_ruler_std.libs.rotation import Quaternion
@fr.block
def Attitude():
quat = fr.input("quat", (4,))
fr.output("quat_unit", Quaternion.normalize(quat), dim=(4,))实现位置:packages/fly_ruler/python/fly_ruler_std/libs/rotation.py:41
conjugate
python
def conjugate(q: Signal) -> Signal计算四元数共轭 [w, -x, -y, -z]。
参数
| 名称 | 类型 | 默认值 | 说明 |
|---|---|---|---|
q | Signal | — | 输入四元数 (4, scalar-first)。 |
返回
| 类型 | 说明 |
|---|---|
Signal | 共轭四元数 (4, scalar-first)。 |
python
import fly_ruler_core as fr
from fly_ruler_std.libs.rotation import Quaternion
@fr.block
def InverseRotation():
quat = fr.input("quat", (4,))
fr.output("quat_conj", Quaternion.conjugate(quat), dim=(4,))实现位置:packages/fly_ruler/python/fly_ruler_std/libs/rotation.py:66
multiply
python
def multiply(q1: Signal, q2: Signal) -> Signal计算 Hamilton 乘积 q1 ⊗ q2。
参数
| 名称 | 类型 | 默认值 | 说明 |
|---|---|---|---|
q1 | Signal | — | 左乘四元数 (4, scalar-first)。 |
q2 | Signal | — | 右乘四元数 (4, scalar-first)。 |
返回
| 类型 | 说明 |
|---|---|
Signal | 乘积四元数 (4, scalar-first)。 |
python
import fly_ruler_core as fr
from fly_ruler_std.libs.rotation import Quaternion
@fr.block
def Compose():
q1 = fr.input("q1", (4,))
q2 = fr.input("q2", (4,))
fr.output("q12", Quaternion.multiply(q1, q2), dim=(4,))实现位置:packages/fly_ruler/python/fly_ruler_std/libs/rotation.py:89
inverse
python
def inverse(q: Signal) -> Signal计算四元数逆 q^{-1} = q^* / ||q||^2。
参数
| 名称 | 类型 | 默认值 | 说明 |
|---|---|---|---|
q | Signal | — | 非零输入四元数 (4, scalar-first)。 |
返回
| 类型 | 说明 |
|---|---|
Signal | 逆四元数 (4, scalar-first)。 |
python
import fly_ruler_core as fr
from fly_ruler_std.libs.rotation import Quaternion
@fr.block
def InverseRotation():
quat = fr.input("quat", (4,))
fr.output("quat_inv", Quaternion.inverse(quat), dim=(4,))实现位置:packages/fly_ruler/python/fly_ruler_std/libs/rotation.py:123
derivative
python
def derivative(q: Signal, omega: Signal) -> Signal计算四元数运动学导数 q_dot = 0.5 * q ⊗ [0, omega]。
参数
| 名称 | 类型 | 默认值 | 说明 |
|---|---|---|---|
q | Signal | — | 当前姿态四元数 (4, scalar-first)。 |
omega | Signal | — | 机体系角速度 (3,) (rad/s)。 |
返回
| 类型 | 说明 |
|---|---|
Signal | 四元数导数 (4, scalar-first)。 |
python
import fly_ruler_core as fr
from fly_ruler_std.libs.rotation import Quaternion
@fr.block
def Kinematics():
quat = fr.input("quat", (4,))
omega = fr.input("omega", (3,), unit="rad/s")
fr.output("quat_dot", Quaternion.derivative(quat, omega), dim=(4,))实现位置:packages/fly_ruler/python/fly_ruler_std/libs/rotation.py:146
rotate_vector
python
def rotate_vector(q_ab: Signal, v_a: Signal) -> Signal用四元数 q_ab 把向量从 A 系旋转到 B 系。
等价于 v_b = q_ab ⊗ [0, v_a] ⊗ q_ab^*。
参数
| 名称 | 类型 | 默认值 | 说明 |
|---|---|---|---|
q_ab | Signal | — | A 系到 B 系的旋转四元数 (4, scalar-first)。 |
v_a | Signal | — | A 系中的向量 (3,)。 |
返回
| 类型 | 说明 |
|---|---|
Signal | B 系中的向量 (3,)。 |
python
import fly_ruler_core as fr
from fly_ruler_std.libs.rotation import Quaternion
@fr.block
def ToBody():
q_ab = fr.input("q_ab", (4,))
v_a = fr.input("v_a", (3,))
fr.output("v_b", Quaternion.rotate_vector(q_ab, v_a), dim=(3,))实现位置:packages/fly_ruler/python/fly_ruler_std/libs/rotation.py:173
from_euler
python
def from_euler(rpy: Signal) -> Signal把 Roll-Pitch-Yaw 角 (rad) 转为四元数 (scalar-first)。
参数
| 名称 | 类型 | 默认值 | 说明 |
|---|---|---|---|
rpy | Signal | — | Roll-Pitch-Yaw 角 (3,) (rad),顺序为 [phi, theta, psi]。 |
返回
| 类型 | 说明 |
|---|---|
Signal | 单位四元数 (4, scalar-first)。 |
python
import fly_ruler_core as fr
from fly_ruler_std.libs.rotation import Quaternion
@fr.block
def Attitude():
rpy = fr.input("rpy", (3,), unit="rad")
fr.output("quat", Quaternion.from_euler(rpy), dim=(4,))实现位置:packages/fly_ruler/python/fly_ruler_std/libs/rotation.py:205
to_euler
python
def to_euler(q: Signal) -> Signal把四元数 (scalar-first) 转为 Roll-Pitch-Yaw 角 (rad)。
参数
| 名称 | 类型 | 默认值 | 说明 |
|---|---|---|---|
q | Signal | — | 单位输入四元数 (4, scalar-first)。 |
返回
| 类型 | 说明 |
|---|---|
Signal | Roll-Pitch-Yaw 角 (3,) (rad)。 |
NOTE
返回 RPY 主值。俯仰 ±π/2 时欧拉角表示不唯一,Jacobian 奇异;梯度类算法应使用四元数或旋转矩阵。
python
import fly_ruler_core as fr
from fly_ruler_std.libs.rotation import Quaternion
@fr.block
def EulerAngles():
quat = fr.input("quat", (4,))
fr.output("rpy", Quaternion.to_euler(quat), dim=(3,), unit="rad")实现位置:packages/fly_ruler/python/fly_ruler_std/libs/rotation.py:242
from_matrix
python
def from_matrix(dcm: Signal) -> Signal把旋转矩阵 (3×3) 转为四元数 (scalar-first)。
参数
| 名称 | 类型 | 默认值 | 说明 |
|---|---|---|---|
dcm | Signal | — | 旋转矩阵 (3, 3)。 |
返回
| 类型 | 说明 |
|---|---|
Signal | 单位四元数 (4, scalar-first)。 |
NOTE
输入必须是有效 SO(3) 矩阵。由对角线元素可得 c_w = 1 + tr(R)、c_x = 1 + R00 - R11 - R22、 c_y = 1 - R00 + R11 - R22、c_z = 1 - R00 - R11 + R22,分别等于 4w²、4x²、4y²、4z²。算法选择最大候选分量,再由矩阵的对称/反对称元素求出其余分量,从而避免在半转附近除以趋近于零的分量。输出符号规范为 w >= 0;当 w == 0 时,x/y/z 中首个非零分量为正。符号切换边界不保证连续或可导,候选最大值切换处也不保证可导。该稳定分支法与 Shepperd (1978) 摘要描述的矩阵转四元数算法思路一致;符号规范可参照 SciPy as_quat。
python
import fly_ruler_core as fr
from fly_ruler_std.libs.rotation import Quaternion
@fr.block
def Attitude():
dcm = fr.input("dcm", (3, 3))
fr.output("quat", Quaternion.from_matrix(dcm), dim=(4,))实现位置:packages/fly_ruler/python/fly_ruler_std/libs/rotation.py:273
to_matrix
python
def to_matrix(q: Signal) -> Signal把四元数 (scalar-first) 转为旋转矩阵 (3×3)。
参数
| 名称 | 类型 | 默认值 | 说明 |
|---|---|---|---|
q | Signal | — | 单位输入四元数 (4, scalar-first)。 |
返回
| 类型 | 说明 |
|---|---|
Signal | 旋转矩阵 (3, 3)。 |
python
import fly_ruler_core as fr
from fly_ruler_std.libs.rotation import Quaternion
@fr.block
def BodyToNed():
quat = fr.input("quat", (4,))
fr.output("dcm", Quaternion.to_matrix(quat), dim=(3, 3))实现位置:packages/fly_ruler/python/fly_ruler_std/libs/rotation.py:396
实现位置:packages/fly_ruler/python/fly_ruler_std/libs/rotation.py:19
RotationMatrix
python
class RotationMatrix旋转矩阵运算的静态方法集合,供 @fr.block 声明使用。
identity
python
def identity() -> Signal单位旋转矩阵。
返回
| 类型 | 说明 |
|---|---|
Signal | 单位旋转矩阵 (3, 3)。 |
python
import fly_ruler_core as fr
from fly_ruler_std.libs.rotation import RotationMatrix
@fr.block
def Identity():
fr.output("dcm_unit", RotationMatrix.identity(), dim=(3, 3))实现位置:packages/fly_ruler/python/fly_ruler_std/libs/rotation.py:442
transpose
python
def transpose(rotation_matrix: Signal) -> Signal计算旋转矩阵的转置。
参数
| 名称 | 类型 | 默认值 | 说明 |
|---|---|---|---|
rotation_matrix | Signal | — | 旋转矩阵 (3, 3)。 |
返回
| 类型 | 说明 |
|---|---|
Signal | 转置旋转矩阵 (3, 3)。 |
python
import fly_ruler_core as fr
from fly_ruler_std.libs.rotation import RotationMatrix
@fr.block
def Transpose():
dcm = fr.input("dcm", (3, 3))
fr.output("dcm_T", RotationMatrix.transpose(dcm), dim=(3, 3))实现位置:packages/fly_ruler/python/fly_ruler_std/libs/rotation.py:461
inverse
python
def inverse(rotation_matrix: Signal) -> Signal计算旋转矩阵的逆。
参数
| 名称 | 类型 | 默认值 | 说明 |
|---|---|---|---|
rotation_matrix | Signal | — | 旋转矩阵 (3, 3)。 |
返回
| 类型 | 说明 |
|---|---|
Signal | 逆旋转矩阵 (3, 3)。 |
python
import fly_ruler_core as fr
from fly_ruler_std.libs.rotation import RotationMatrix
@fr.block
def Inverse():
dcm = fr.input("dcm", (3, 3))
fr.output("dcm_inv", RotationMatrix.inverse(dcm), dim=(3, 3))实现位置:packages/fly_ruler/python/fly_ruler_std/libs/rotation.py:484
multiply
python
def multiply(rotation_matrix1: Signal, rotation_matrix2: Signal) -> Signal计算旋转矩阵乘积。
参数
| 名称 | 类型 | 默认值 | 说明 |
|---|---|---|---|
rotation_matrix1 | Signal | — | 左乘旋转矩阵 (3, 3)。 |
rotation_matrix2 | Signal | — | 右乘旋转矩阵 (3, 3)。 |
返回
| 类型 | 说明 |
|---|---|
Signal | 乘积旋转矩阵 (3, 3)。 |
python
import fly_ruler_core as fr
from fly_ruler_std.libs.rotation import RotationMatrix
@fr.block
def Compose():
dcm1 = fr.input("dcm1", (3, 3))
dcm2 = fr.input("dcm2", (3, 3))
fr.output("dcm12", RotationMatrix.multiply(dcm1, dcm2), dim=(3, 3))实现位置:packages/fly_ruler/python/fly_ruler_std/libs/rotation.py:507
derivative
python
def derivative(rotation_matrix: Signal, omega: Signal) -> Signal计算旋转矩阵运动学导数 R_dot = R * skew(omega)。
参数
| 名称 | 类型 | 默认值 | 说明 |
|---|---|---|---|
rotation_matrix | Signal | — | 当前姿态旋转矩阵 (3, 3)。 |
omega | Signal | — | 机体系角速度 (3,) (rad/s)。 |
返回
| 类型 | 说明 |
|---|---|
Signal | 旋转矩阵导数 (3, 3)。 |
python
import fly_ruler_core as fr
from fly_ruler_std.libs.rotation import RotationMatrix
@fr.block
def Kinematics():
dcm = fr.input("dcm", (3, 3))
omega = fr.input("omega", (3,), unit="rad/s")
fr.output("dcm_dot", RotationMatrix.derivative(dcm, omega), dim=(3, 3))实现位置:packages/fly_ruler/python/fly_ruler_std/libs/rotation.py:532
rotate_vector
python
def rotate_vector(rotation_matrix: Signal, vector: Signal) -> Signal用机体系到 NED 的旋转矩阵旋转向量。
参数
| 名称 | 类型 | 默认值 | 说明 |
|---|---|---|---|
rotation_matrix | Signal | — | 机体系到 NED 的旋转矩阵 (3, 3)。 |
vector | Signal | — | 机体系中的向量 (3,)。 |
返回
| 类型 | 说明 |
|---|---|
Signal | NED 系中的向量 (3,)。 |
python
import fly_ruler_core as fr
from fly_ruler_std.libs.rotation import RotationMatrix
@fr.block
def RotateVector():
dcm = fr.input("dcm", (3, 3))
vec = fr.input("vec", (3,))
fr.output("rotated", RotationMatrix.rotate_vector(dcm, vec), dim=(3,))实现位置:packages/fly_ruler/python/fly_ruler_std/libs/rotation.py:564
from_euler
python
def from_euler(rpy: Signal) -> Signal把 Roll-Pitch-Yaw 角 (rad) 转为机体系到 NED 的旋转矩阵。
参数
| 名称 | 类型 | 默认值 | 说明 |
|---|---|---|---|
rpy | Signal | — | Roll-Pitch-Yaw 角 (3,) (rad),顺序为 [phi, theta, psi]。 |
返回
| 类型 | 说明 |
|---|---|
Signal | 机体系到 NED 的旋转矩阵 (3, 3)。 |
python
import fly_ruler_core as fr
from fly_ruler_std.libs.rotation import RotationMatrix
@fr.block
def BodyToNed():
rpy = fr.input("rpy", (3,), unit="rad")
fr.output("dcm", RotationMatrix.from_euler(rpy), dim=(3, 3))实现位置:packages/fly_ruler/python/fly_ruler_std/libs/rotation.py:589
to_euler
python
def to_euler(rotation_matrix: Signal) -> Signal把机体系到 NED 的旋转矩阵转为 Roll-Pitch-Yaw 角 (rad)。
参数
| 名称 | 类型 | 默认值 | 说明 |
|---|---|---|---|
rotation_matrix | Signal | — | 机体系到 NED 的旋转矩阵 (3, 3)。 |
返回
| 类型 | 说明 |
|---|---|
Signal | Roll-Pitch-Yaw 角 (3,) (rad)。 |
NOTE
返回 RPY 主值。俯仰 ±π/2 时欧拉角表示不唯一,Jacobian 奇异;梯度类算法应使用四元数或旋转矩阵。
python
import fly_ruler_core as fr
from fly_ruler_std.libs.rotation import RotationMatrix
@fr.block
def EulerAngles():
dcm = fr.input("dcm", (3, 3))
fr.output("rpy", RotationMatrix.to_euler(dcm), dim=(3,), unit="rad")实现位置:packages/fly_ruler/python/fly_ruler_std/libs/rotation.py:622
from_quaternion
python
def from_quaternion(q: Signal) -> Signal把四元数 (scalar-first) 转为机体系到 NED 的旋转矩阵。
参数
| 名称 | 类型 | 默认值 | 说明 |
|---|---|---|---|
q | Signal | — | 输入四元数 (4, scalar-first)。 |
返回
| 类型 | 说明 |
|---|---|
Signal | 机体系到 NED 的旋转矩阵 (3, 3)。 |
python
import fly_ruler_core as fr
from fly_ruler_std.libs.rotation import RotationMatrix
@fr.block
def BodyToNed():
quat = fr.input("quat", (4,))
fr.output("dcm", RotationMatrix.from_quaternion(quat), dim=(3, 3))实现位置:packages/fly_ruler/python/fly_ruler_std/libs/rotation.py:653
to_quaternion
python
def to_quaternion(rotation_matrix: Signal) -> Signal把机体系到 NED 的旋转矩阵转为四元数 (scalar-first)。
参数
| 名称 | 类型 | 默认值 | 说明 |
|---|---|---|---|
rotation_matrix | Signal | — | 机体系到 NED 的旋转矩阵 (3, 3)。 |
返回
| 类型 | 说明 |
|---|---|
Signal | 单位四元数 (4, scalar-first)。 |
python
import fly_ruler_core as fr
from fly_ruler_std.libs.rotation import Quaternion
@fr.block
def Attitude():
dcm = fr.input("dcm", (3, 3))
fr.output("quat", RotationMatrix.to_quaternion(dcm), dim=(4,))实现位置:packages/fly_ruler/python/fly_ruler_std/libs/rotation.py:676
实现位置:packages/fly_ruler/python/fly_ruler_std/libs/rotation.py:439