全局坐标转局部坐标推导

全局坐标转局部坐标

问题定义:

设全局坐标系为 OworldO_{world}Oworld,自车当前状态为:

  • 位置:(xc,yc)(x_c, y_c)(xc,yc)
  • 朝向:θc\theta_cθc(与全局 X 轴的夹角,逆时针为正)

目标点状态为:

  • 位置:(xt,yt)(x_t, y_t)(xt,yt)
  • 朝向:θt\theta_tθt

Step1: 平移

先将原点平移到自车位置,得到目标点在全局系下的相对偏移:

dx dy\]=\[xt−xc yt−yc\] \\begin{bmatrix} dx \\ dy \\end{bmatrix} = \\begin{bmatrix} x_t - x_c \\ y_t - y_c \\end{bmatrix} \[dx dy\]=\[xt−xc yt−yc

Step 2:旋转

全局坐标系旋转 θc\theta_cθc 后与自车局部坐标系对齐。要把全局偏移量转到局部系,需要反向旋转 −θc-\theta_c−θc,即乘以旋转矩阵 R(−θc)R(-\theta_c)R(−θc):

R(θ)=cos⁡θ−sin⁡θ sin⁡θcos⁡θ⇒R(−θc)=cos⁡θcsin⁡θc −sin⁡θccos⁡θc R(\theta) = \begin{bmatrix} \cos\theta & -\sin\theta \ \sin\theta & \cos\theta \end{bmatrix} \quad \Rightarrow \quad R(-\theta_c) = \begin{bmatrix} \cos\theta_c & \sin\theta_c \ -\sin\theta_c & \cos\theta_c \end{bmatrix} R(θ)=cosθ−sinθ sinθcosθ⇒R(−θc)=cosθcsinθc −sinθccosθc

因此:

xlocal ylocal\]=R(−θc)\[dx dy\]=\[cos⁡θcsin⁡θc −sin⁡θccos⁡θc\]\[dx dy\] \\begin{bmatrix} x_{local} \\ y_{local} \\end{bmatrix} = R(-\\theta_c) \\begin{bmatrix} dx \\ dy \\end{bmatrix} = \\begin{bmatrix} \\cos\\theta_c \& \\sin\\theta_c \\ -\\sin\\theta_c \& \\cos\\theta_c \\end{bmatrix} \\begin{bmatrix} dx \\ dy \\end{bmatrix} \[xlocal ylocal\]=R(−θc)\[dx dy\]=\[cosθcsinθc −sinθccosθc\]\[dx dy

展开得:

xlocal=dx⋅cos⁡θc+dy⋅sin⁡θc \boxed{ x_{local} = dx \cdot \cos\theta_c + dy \cdot \sin\theta_c } xlocal=dx⋅cosθc+dy⋅sinθc

ylocal=−dx⋅sin⁡θc+dy⋅cos⁡θc \boxed{ y_{local} = -dx \cdot \sin\theta_c + dy \cdot \cos\theta_c } ylocal=−dx⋅sinθc+dy⋅cosθc

代码实现如下:

python 复制代码
def to_local_coords(target_x, target_y, target_yaw, curr_x, curr_y, curr_yaw):
    """全局坐标转换到自车局部坐标系"""
    curr_yaw = np.deg2rad(curr_yaw)
    target_yaw = np.deg2rad(target_yaw)
    dx = target_x - curr_x
    dy = target_y - curr_y
    local_x = dx * np.cos(curr_yaw) + dy * np.sin(curr_yaw)
    local_y = -dx * np.sin(curr_yaw) + dy * np.cos(curr_yaw)
    local_yaw = target_yaw - curr_yaw
    return np.array([local_x, local_y, np.cos(local_yaw), np.sin(local_yaw)])

旋转矩阵的推导

问题设定

设有一个向量 v⃗\vec{v}v ,其在原坐标系中的坐标为 (x,y)(x, y)(x,y),与 X 轴的夹角为 α\alphaα,模长为 rrr:

x=rcos⁡α,y=rsin⁡α x = r\cos\alpha, \quad y = r\sin\alpha x=rcosα,y=rsinα

现在将坐标系逆时针旋转 θ\thetaθ 角 (等价于向量顺时针旋转 θ\thetaθ 角 ),求新坐标 (x′,y′)(x', y')(x′,y′)。


Step 1:用极坐标表示原向量

v⃗=(rcos⁡α, rsin⁡α) \vec{v} = (r\cos\alpha,\ r\sin\alpha) v =(rcosα, rsinα)


Step 2:旋转后用极坐标表示新向量

旋转后,向量与 X 轴夹角变为 α+θ\alpha + \thetaα+θ,模长不变:

x′=rcos⁡(α+θ) x' = r\cos(\alpha + \theta) x′=rcos(α+θ)

y′=rsin⁡(α+θ) y' = r\sin(\alpha + \theta) y′=rsin(α+θ)


Step 3:展开三角函数

x′=rcos⁡(α+θ)=r(cos⁡αcos⁡θ−sin⁡αsin⁡θ) x' = r\cos(\alpha + \theta) = r(\cos\alpha\cos\theta - \sin\alpha\sin\theta) x′=rcos(α+θ)=r(cosαcosθ−sinαsinθ)

y′=rsin⁡(α+θ)=r(sin⁡αcos⁡θ+cos⁡αsin⁡θ) y' = r\sin(\alpha + \theta) = r(\sin\alpha\cos\theta + \cos\alpha\sin\theta) y′=rsin(α+θ)=r(sinαcosθ+cosαsinθ)

将 rcos⁡α=xr\cos\alpha = xrcosα=x,rsin⁡α=yr\sin\alpha = yrsinα=y 代入:

x′=xcos⁡θ−ysin⁡θ x' = x\cos\theta - y\sin\theta x′=xcosθ−ysinθ

y′=xsin⁡θ+ycos⁡θ y' = x\sin\theta + y\cos\theta y′=xsinθ+ycosθ


Step 4:写成矩阵形式

x′ y′\]=\[cos⁡θ−sin⁡θ sin⁡θcos⁡θ\]⏟R(θ)\[x y\]\\begin{bmatrix} x' \\ y' \\end {bmatrix} =\\underbrace{\\begin{bmatrix} \\cos\\theta \& -\\sin\\theta \\ \\sin\\theta \& \\cos\\theta \\end{bmatrix}}_{R(\\theta)} \\begin{bmatrix} x \\ y \\end{bmatrix} \[x′ y′\]=R(θ) \[cosθ−sinθ sinθcosθ\]\[x y

这就是逆时针旋转 θ\thetaθ 的旋转矩阵 R(θ)R(\theta)R(θ)。

相关推荐
eric-sjq5 分钟前
小思框架 · 30M 级长上下文新架构对比技术报告
人工智能·python·深度学习·语言模型
Kari118 分钟前
Dify vs Coze开源版:企业级AI应用平台对比
人工智能·开源
AI视频工具指南10 分钟前
AI视频的声音和画面怎么配合?先分清谁在传递信息
人工智能·aigc·音视频
云析赢指标公式网414 分钟前
指标开发笔记|主图搭配双副图成套指标逻辑拆解
人工智能·笔记
奈落2421 分钟前
连错一个端口,Agent就“看不见”工具:从 n8n 的8种AI连接类型到 Dify 的插件审批复用,平台设计里的“隐形接线规则”怎么读
人工智能·agent·ai 编程
这是谁的博客?22 分钟前
AI 加速器 RTL 数字系统设计深度解析:从 MAC 阵列 Verilog 实现到时序收敛与时钟功耗的芯片落地全链路
人工智能·macos·ai·verilog·数字ic设计·ai芯片·rtl
无所畏俱的老尹22 分钟前
OpenAI 等支持 robots.txt 共享 AI 训练偏好信号:policy token 一次声明多厂商,落地仍待实现
人工智能·深度学习·机器学习·aigc
zhangfeng113325 分钟前
用世界模型做AI制药:扰动之心发布虚拟细胞推演模型AlphaCell 1.0 Pr
人工智能·ai药物
秋天的怀念_David37 分钟前
算法一千零一夜 · 作品相关:版权与致谢声明
程序人生·算法·学习方法
猫头_1 小时前
MCP 开发实战:把你自己的后端 API 接入 Agent
人工智能·typescript·mcp