Frenet坐标系在无人驾驶领域被普遍使用,特别是在城市、高速等道路交通环境下无人驾驶的路径规划系统中。Frenet坐标系使用道路的中心线作为Base frame,使用参考线的切线向量和法线向量建立坐标系。相比笛卡尔坐标系,Frenet坐标系简化了路径规划问题。网上关于Frenet坐标系的博客或资料很多,此处不再赘述。本文先给出简单的转换公式推导过程,然后给出三对Frenet坐标系与Cartesian坐标系互转的python代码与使用示例方便自己查阅使用。详细推导参见博客Apollo项目坐标系研究。note:下图中Frenet的q坐标轴也可以用d或l表示。
下图显示了一条3D空间中一条连续可微的曲线K,P为曲线K上的一个点,黄色平面为曲线K在点P处的运动平面。 T ⃗ \vec{T} T为曲线K在点P处的切向量, N ⃗ \vec{N} N为K在P处的法向量( N ⃗ \vec{N} N与 T ⃗ \vec{T} T在同一运动平面), B ⃗ \vec{B} B为曲线K在P处的副法向量( B ⃗ \vec{B} B垂直于运动平面)。
有Frenet公式:
{ d T ⃗ d s = k N ⃗ d N ⃗ d s = − k T ⃗ + τ B ⃗ d B ⃗ d s = − τ N ⃗ \left\{\begin{matrix} \frac{d\vec{T}}{ds}= & k\vec{N} \\ \frac{d\vec{N}}{ds}= & -k\vec{T}+\tau\vec{B} \\ \frac{d\vec{B}}{ds}= & -\tau\vec{N} \end{matrix}\right. ⎩ ⎨ ⎧dsdT=dsdN=dsdB=kN−kT+τB−τN
其中, d d s \frac{d}{ds} dsd表示某一方向向量对弧长 s s s的导数, k k k为曲率( curvature), τ \tau τ为挠率( torsion)。
在大地表面,局部路面可看作一个平面,对于无人驾驶路径规划应用,挠率可设定为0。从而Frenet公式进一步简化为:
{ d T ⃗ d s = k N ⃗ d N ⃗ d s = − k T ⃗ (1) \left\{\begin{matrix} \frac{d\vec{T}}{ds}= & k\vec{N} \\ \frac{d\vec{N}}{ds}= & -k\vec{T} \end{matrix}\right. \tag{1} {dsdT=dsdN=kN−kT(1)
引博客Apollo项目坐标系研究中的图如下:
Frenet坐标系: [ s , s ˙ , s ¨ , l , l ˙ , l ¨ , l ′ , l ′ ′ ] [s, \dot{s}, \ddot{s}, l, \dot{l}, \ddot{l}, l', l''] [s,s˙,s¨,l,l˙,l¨,l′,l′′],Cartesian坐标系: [ x ⃗ , v x , a a , θ x , k x ] [\vec{x},v_{x}, a_{a}, \theta_{x},k_{x}] [x,vx,aa,θx,kx],释义如下:
求 s s s
首先,找到曲线上离 [ x x , y x ] [x_{x},y_{x}] [xx,yx]最近的参考点 r ⃗ = [ x r , y r ] \vec{r}=[x_{r}, y_{r}] r=[xr,yr],该参考点处的 s s s即为 [ x x , y x ] [x_{x}, y_{x}] [xx,yx]在Frenet坐标系下的 s s s。
求 l l l
x ⃗ , r ⃗ \vec{x},\vec{r} x,r分别为参考点与待转换点在Cartesian坐标系下的向量,我们有 x ⃗ = r ⃗ + l N r ⃗ \vec{x}=\vec{r}+l\vec{N_{r}} x=r+lNr,变换可得:
l = ( x ⃗ − r ⃗ ) T N r ⃗ = ∣ ∣ x ⃗ − r ⃗ ∣ ∣ 2 cos ( θ x − r − ( θ r + π 2 ) ) = ∣ ∣ x ⃗ − r ⃗ ∣ ∣ 2 ( sin ( θ x − r ) cos ( θ r ) − cos ( θ x − r ) sin ( θ r ) ) l=(\vec{x}-\vec{r})^{T}\vec{N_{r}}=||\vec{x}-\vec{r}||_{2}\cos(\theta_{x-r}-(\theta_{r}+\frac{\pi}{2}))=||\vec{x}-\vec{r}||_{2}(\sin(\theta_{x-r})\cos(\theta_{r})-\cos(\theta_{x-r})\sin(\theta_{r})) l=(x−r)TNr=∣∣x−r∣∣2cos(θx−r−(θr+2π))=∣∣x−r∣∣2(sin(θx−r)cos(θr)−cos(θx−r)sin(θr))
备注:上式中, θ x − r \theta_{x-r} θx−r为向量 x ⃗ − r ⃗ \vec{x}-\vec{r} x−r的方向角度, θ r + π 2 \theta_{r}+\frac{\pi}{2} θr+2π为单位向量 N ⃗ r \vec{N}_{r} Nr的方向角度(对比上一张图可知),则上式可利用向量点乘公式 a ⃗ × b ⃗ = ∣ a ∣ ∣ b ∣ cos θ \vec{a}\times \vec{b}=|a||b|\cos{\theta} a×b=∣a∣∣b∣cosθ得到。此处, θ \theta θ为向量 a ⃗ \vec{a} a与向量 b ⃗ \vec{b} b之间的夹角。
假定 x ⃗ = ( x x , y x ) , r ⃗ = ( x r , y r ) \vec{x}=(x_{x}, y_{x}), \vec{r}=(x_{r}, y_{r}) x=(xx,yx),r=(xr,yr),则 ∣ ∣ x ⃗ − r ⃗ ∣ ∣ 2 = ( x x − x r ) 2 + ( y x − y r ) 2 ||\vec{x}-\vec{r}||_{2}=\sqrt{(x_{x}-x_{r})^2+(y_{x}-y_{r})^2} ∣∣x−r∣∣2=(xx−xr)2+(yx−yr)2
在Frenet坐标系下,每个点到base frame上参考点的向量都与该参考点的法向量 N ⃗ \vec{N} N同向或反向,因此, sin ( θ x − r ) cos ( θ r ) − cos ( θ x − r ) sin ( θ r ) = 1 或 − 1 \sin(\theta_{x-r})\cos(\theta_{r})-\cos(\theta_{x-r})\sin(\theta_{r})=1 或 -1 sin(θx−r)cos(θr)−cos(θx−r)sin(θr)=1或−1。 θ x − r \theta_{x-r} θx−r为向量 x ⃗ − r ⃗ \vec{x}-\vec{r} x−r的角度,因此, sin ( θ x − r ) cos ( θ x − r ) = y x − y r x x − x r \frac{\sin(\theta_{x-r})}{\cos(\theta_{x-r})}=\frac{y_{x}-y_{r}}{x_{x}-x_{r}} cos(θx−r)sin(θx−r)=xx−xryx−yr。可以根据 ( x x − x r ) 2 + ( y x − y r ) 2 \sqrt{(x_{x}-x_{r})^2+(y_{x}-y_{r})^2} (xx−xr)2+(yx−yr)2来确定 l l l的绝对取值, ( y x − y r ) cos ( θ r ) − ( x x − x r ) sin ( θ r ) (y_{x}-y_{r})\cos(\theta_{r})-(x_{x}-x_{r})\sin(\theta_{r}) (yx−yr)cos(θr)−(xx−xr)sin(θr)的正负来确定 l l l的正负号,公式如下:
l = s i g n ( ( y x − y r ) cos ( θ r ) − ( x x − x r ) sin ( θ r ) ) ( x x − x r ) 2 + ( y x − y r ) 2 (2) l = sign\left((y_{x}-y_{r})\cos(\theta_{r})-(x_{x}-x_{r})\sin(\theta_{r})\right)\sqrt{(x_{x}-x_{r})^2+(y_{x}-y_{r})^2} \tag{2} l=sign((yx−yr)cos(θr)−(xx−xr)sin(θr))(xx−xr)2+(yx−yr)2(2)
由Frenet公式(见上式(1))有 N r ⃗ ′ = d N r ⃗ d s = − k r T r ⃗ \vec{N_{r}}'=\frac{d\vec{N_{r}}}{ds}=-k_{r}\vec{T_{r}} Nr′=dsdNr=−krTr,于是有 N r ⃗ ˙ = d s d t d N r ⃗ d s = − k r s ˙ T r ⃗ \dot{\vec{N_{r}}}=\frac{ds}{dt}\frac{d\vec{N_{r}}}{ds}=-k_{r}\dot{s}\vec{T_{r}} Nr˙=dtdsdsdNr=−krs˙Tr,代入上式可得:
l ˙ = v x T x ⃗ T N r ⃗ − s ˙ T r ⃗ T N r ⃗ − l s ˙ k r N r ⃗ T T r ⃗ \dot{l}=v_{x}\vec{T_{x}}^{T}\vec{N_{r}}-\dot{s}\vec{T_{r}}^{T}\vec{N_{r}}-l\dot{s}k_{r}\vec{N_{r}}^{T}\vec{T_{r}} l˙=vxTxTNr−s˙TrTNr−ls˙krNrTTr
由于 N r ⃗ \vec{N_{r}} Nr与 T r ⃗ \vec{T_{r}} Tr正交 ⇒ N r ⃗ T T r ⃗ = T r ⃗ T N r ⃗ = 0 \Rightarrow \vec{N_{r}}^{T}\vec{T_{r}}=\vec{T_{r}}^{T}\vec{N_{r}}=0 ⇒NrTTr=TrTNr=0, T x ⃗ T N r ⃗ = cos ( θ x − θ r − π 2 ) = sin ( θ x − θ r ) \vec{T_{x}}^{T}\vec{N_{r}}=\cos(\theta_{x}-\theta_{r}-\frac{\pi}{2})=\sin(\theta_{x}-\theta_{r}) TxTNr=cos(θx−θr−2π)=sin(θx−θr),代入上式得:
l ˙ = v x sin ( θ x − θ r ) (3) \dot{l}=v_{x}\sin(\theta_{x}-\theta_{r}) \tag{3} l˙=vxsin(θx−θr)(3)
上式两边左乘 T r ⃗ \vec{T_r} Tr得到 v x cos ( θ x − θ r ) = s ˙ ( 1 − k r l ) v_x\cos(\theta_x-\theta_r)=\dot{s}(1-k_r l) vxcos(θx−θr)=s˙(1−krl),整理可得:
s ˙ = v x cos ( θ x − θ r ) 1 − k r l (4) \dot{s}=\frac{v_x \cos(\theta_x-\theta_r)}{1-k_r l} \tag{4} s˙=1−krlvxcos(θx−θr)(4)
l ′ = d l d s = l ˙ s ˙ l'=\frac{dl}{ds}=\frac{\dot{l}}{\dot{s}} l′=dsdl=s˙l˙ ,将 l ˙ \dot{l} l˙与 s ˙ \dot{s} s˙代入得:
l ′ = ( 1 − k r l ) tan ( θ x − θ r ) (5) l'=(1-k_{r}l)\tan(\theta_{x}-\theta_{r}) \tag{5} l′=(1−krl)tan(θx−θr)(5)
由 l ′ = ( 1 − k r l ) tan ( θ x − θ r ) l'=(1-k_{r}l)\tan(\theta_{x}-\theta_{r}) l′=(1−krl)tan(θx−θr),可得 l ′ ′ = d l ′ d s = ( 1 − k r l ) ′ tan ( θ x − θ r ) + ( 1 − k r l ) ( θ x − θ r ) ′ cos 2 ( θ x − θ r ) l''=\frac{dl'}{ds}=(1-k_{r}l)'\tan(\theta_{x}-\theta_{r})+(1-k_{r}l)\frac{(\theta_{x}-\theta_{r})'}{\cos^{2}(\theta_{x}-\theta_{r})} l′′=dsdl′=(1−krl)′tan(θx−θr)+(1−krl)cos2(θx−θr)(θx−θr)′
假定 s x s_{x} sx为为车辆当前轨迹 x ⃗ \vec{x} x的弧长,有:
d d s = d s x d t d t d s d d s x = v x s ˙ d d s x = 1 − k r l cos ( θ x − θ r ) d d s x \frac{d}{ds}=\frac{ds_{x}}{dt}\frac{dt}{ds}\frac{d}{ds_{x}}=\frac{v_{x}}{\dot{s}}\frac{d}{ds_{x}}=\frac{1-k_{r}l}{\cos(\theta_{x}-\theta_{r})}\frac{d}{ds_{x}} dsd=dtdsxdsdtdsxd=s˙vxdsxd=cos(θx−θr)1−krldsxd
又有曲率: k r = d θ r d s k_{r}=\frac{d\theta_{r}}{ds} kr=dsdθr, k x = d θ x d s x k_{x}=\frac{d\theta_{x}}{ds_{x}} kx=dsxdθx
故: l ′ ′ = d l ′ d s = − ( k r ′ l + k r l ′ ) tan ( θ x − θ r ) + ( 1 − k r l ) cos 2 ( θ x − θ r ) ( d θ x d s − d θ r d s ) l''=\frac{dl'}{ds}=-(k_{r}'l+k_{r}l')\tan(\theta_{x}-\theta_{r})+\frac{(1-k_{r}l)}{\cos^2(\theta_{x}-\theta_{r})}(\frac{d\theta_{x}}{ds}-\frac{d\theta_{r}}{ds}) l′′=dsdl′=−(kr′l+krl′)tan(θx−θr)+cos2(θx−θr)(1−krl)(dsdθx−dsdθr)
由于: d θ x d s = 1 − k r l cos ( θ x − θ r ) d θ x d s x = 1 − k r l cos ( θ x − θ r ) k x \frac{d\theta_{x}}{ds}=\frac{1-k_{r}l}{\cos(\theta_{x}-\theta_{r})}\frac{d\theta_{x}}{ds_{x}}=\frac{1-k_{r}l}{\cos(\theta_{x}-\theta_{r})}k_{x} dsdθx=cos(θx−θr)1−krldsxdθx=cos(θx−θr)1−krlkx
综上可得:
l ′ ′ = − ( k r ′ l + k r l ′ ) tan ( θ x − θ r ) + ( 1 − k r l ) cos 2 ( θ x − θ r ) ( 1 − k r l cos ( θ x − θ r ) k x − k r ) (6) l''=-(k_{r}'l+k_{r}l')\tan(\theta_{x}-\theta_{r})+\frac{(1-k_{r}l)}{\cos^2(\theta_{x}-\theta_{r})}(\frac{1-k_{r}l}{\cos(\theta_{x}-\theta_{r})}k_{x}-k_{r})\tag{6} l′′=−(kr′l+krl′)tan(θx−θr)+cos2(θx−θr)(1−krl)(cos(θx−θr)1−krlkx−kr)(6)
a x = d v x d t = s ¨ 1 − k r l cos ( θ x − θ r ) + s ˙ 2 cos ( θ x − θ r ) [ l ′ ( k x 1 − k r l cos ( θ x − θ r ) − k r ) − ( k r ′ l + k r l ′ ) ] a_{x}=\frac{dv_{x}}{dt}=\ddot{s}\frac{1-k_{r}l}{\cos(\theta_{x}-\theta_{r})}+\frac{\dot{s}^{2}}{\cos(\theta_{x}-\theta_{r})}\left[l'(k_{x}\frac{1-k_{r}l}{\cos(\theta_{x}-\theta_{r})}-k_{r})-(k_{r}'l+k_{r}l')\right] ax=dtdvx=s¨cos(θx−θr)1−krl+cos(θx−θr)s˙2[l′(kxcos(θx−θr)1−krl−kr)−(kr′l+krl′)]
s ¨ = a x cos ( θ x − θ r ) − s ˙ 2 [ l ′ ( k x 1 − k r l cos ( θ x − θ r ) − k r ) − ( k r ′ l + k r l ′ ) ] 1 − k r l (7) \ddot{s}=\frac{a_{x}\cos(\theta_{x}-\theta_{r})-\dot{s}^{2}\left[l'(k_{x}\frac{1-k_{r}l}{\cos(\theta_{x}-\theta_{r})}-k_{r})-(k_{r}'l+k_{r}l')\right]}{1-k_{r}l}\tag{7} s¨=1−krlaxcos(θx−θr)−s˙2[l′(kxcos(θx−θr)1−krl−kr)−(kr′l+krl′)](7)
Cartesian转 Frenet
{ s = s r s ˙ = v x cos ( θ x − θ r ) 1 − k r l s ¨ = a x cos ( θ x − θ r ) − s ˙ 2 [ l ′ ( k x 1 − k r l cos ( θ x − θ r ) − k r ) − ( k r ′ l + k r l ′ ) ] 1 − k r l l = s i g n ( ( y x − y r ) cos ( θ r ) − ( x x − x r ) sin ( θ r ) ) ( x x − x r ) 2 + ( y x − y r ) 2 l ′ = ( 1 − k r l ) tan ( θ x − θ r ) l ′ ′ = − ( k r ′ l + k r l ′ ) tan ( θ x − θ r ) + ( 1 − k r l ) cos 2 ( θ x − θ r ) ( 1 − k r l cos ( θ x − θ r ) k x − k r ) (8) \left\{\begin{matrix}s=& s_{r} \\ \dot{s}=& \frac{v_{x}\cos(\theta_{x}-\theta_{r})}{1-k_{r}l} \\ \ddot{s}=& \frac{a_{x}\cos(\theta_{x}-\theta_{r})-\dot{s}^{2}\left[l'(k_{x}\frac{1-k_{r}l}{\cos(\theta_{x}-\theta_{r})}-k_{r})-(k_{r}'l+k_{r}l')\right]}{1-k_{r}l} \\ l=& sign\left((y_{x}-y_{r})\cos(\theta_{r})-(x_{x}-x_{r})\sin(\theta_{r})\right)\sqrt{(x_{x}-x_{r})^2+(y_{x}-y_{r})^2} \\ l'=& (1-k_{r}l)\tan(\theta_{x}-\theta_{r}) \\ l''= &-(k_{r}'l+k_{r}l')\tan(\theta_{x}-\theta_{r})+\frac{(1-k_{r}l)}{\cos^2(\theta_{x}-\theta_{r})}(\frac{1-k_{r}l}{\cos(\theta_{x}-\theta_{r})}k_{x}-k_{r}) \end{matrix}\right. \tag{8} ⎩ ⎨ ⎧s=s˙=s¨=l=l′=l′′=sr1−krlvxcos(θx−θr)1−krlaxcos(θx−θr)−s˙2[l′(kxcos(θx−θr)1−krl−kr)−(kr′l+krl′)]sign((yx−yr)cos(θr)−(xx−xr)sin(θr))(xx−xr)2+(yx−yr)2(1−krl)tan(θx−θr)−(kr′l+krl′)tan(θx−θr)+cos2(θx−θr)(1−krl)(cos(θx−θr)1−krlkx−kr)(8)
Frenet转Cartesian
{ x x = x r − l sin ( θ r ) y x = y r + l cos ( θ r ) θ x = arctan ( l ′ 1 − k r l ) + θ r ∈ [ − π , π ] v x = [ s ˙ ( 1 − k r l ) ] 2 + ( s ˙ l ′ ) 2 a x = s ¨ 1 − k r l cos ( θ x − θ r ) + s ˙ 2 cos ( θ x − θ r ) [ l ′ ( k x 1 − k r l cos ( θ x − θ r ) − k r ) − ( k r ′ l + k r l ′ ) ] k x = ( ( l ′ ′ + ( k r ′ l + k r l ′ ) tan ( θ x − θ r ) ) cos 2 ( θ x − θ r ) 1 − k r l + k r ) cos ( θ x − θ r ) 1 − k r l (9) \left\{\begin{matrix} x_{x}= & x_{r}-l\sin(\theta_{r}) \\ y_{x}=& y_{r}+l\cos(\theta_{r}) \\ \theta_{x}=& \arctan(\frac{l'}{1-k_{r}l})+\theta_{r} \in [-\pi, \pi] \\ v_{x}= &\sqrt{[\dot{s}(1-k_{r}l)]^{2}+(\dot{s}l')^{2}} \\ a_{x}= &\ddot{s}\frac{1-k_{r}l}{\cos(\theta_{x}-\theta_{r})}+\frac{\dot{s}^{2}}{\cos(\theta_{x}-\theta_{r})}\left[l'(k_{x}\frac{1-k_{r}l}{\cos(\theta_{x}-\theta_{r})}-k_{r})-(k_{r}'l+k_{r}l')\right] \\ k_{x}= & ((l''+(k_{r}'l+k_{r}l')\tan(\theta_{x}-\theta_{r}))\frac{\cos^{2}(\theta_{x}-\theta_{r})}{1-k_{r}l}+k_{r})\frac{\cos(\theta_{x}-\theta_{r})}{1-k_{r}l} \end{matrix}\right. \tag{9} ⎩ ⎨ ⎧xx=yx=θx=vx=ax=kx=xr−lsin(θr)yr+lcos(θr)arctan(1−krll′)+θr∈[−π,π][s˙(1−krl)]2+(s˙l′)2s¨cos(θx−θr)1−krl+cos(θx−θr)s˙2[l′(kxcos(θx−θr)1−krl−kr)−(kr′l+krl′)]((l′′+(kr′l+krl′)tan(θx−θr))1−krlcos2(θx−θr)+kr)1−krlcos(θx−θr)(9)
问题:已知 ( x x , y x , θ x , v x , a x , k x ) , ( s r , x r , y r , θ r , k r , k r ′ ) , (x_x, y_x, \theta_x, v_x, a_x, k_x), (s_r, x_r, y_r, \theta_r, k_r, k_r'), (xx,yx,θx,vx,ax,kx),(sr,xr,yr,θr,kr,kr′),求 s , s ˙ , s ¨ , l , l ′ , l ′ ′ s, \dot{s}, \ddot{s},l,l', l'' s,s˙,s¨,l,l′,l′′
step 1:计算 d x = x x − x r , d y = y x − y r , d θ = θ x − θ r d_x=x_x-x_r,d_y=y_x-y_r,d_{\theta}=\theta_x-\theta_r dx=xx−xr,dy=yx−yr,dθ=θx−θr;
step 2:计算 C d r d x = d y cos ( θ r ) − d x sin ( θ r ) C_{drdx}=d_y\cos(\theta_r)-d_x\sin(\theta_r) Cdrdx=dycos(θr)−dxsin(θr);
step 3:计算 l = s i g n ( C d r d x ) d x 2 + d y 2 l=sign(C_drdx)\sqrt{d_x^2+d_y^2} l=sign(Cdrdx)dx2+dy2;
step 4:计算 C 1 − k r l = 1 − k r l C_{1-k_r l}=1-k_r l C1−krl=1−krl;
step 5:计算 l ′ = C 1 − k r l tan ( d θ ) l'=C_{1-k_r l}\tan(d_{\theta}) l′=C1−krltan(dθ);
step 6:计算 C k r l ′ = k r ′ l + k r l ′ C_{k_rl'}=k'_rl+k_rl' Ckrl′=kr′l+krl′;
step 7:计算 l ′ ′ = − C k r l ′ tan ( d θ ) + C 1 − k r l cos ( d θ ) ( k x C 1 − k r l cos ( d θ ) − k r ) l''=-C_{k_r l'}\tan(d_{\theta})+\frac{C_{1-k_r l}}{\cos(d_{\theta})}(\frac{k_xC_{1-k_r l}}{\cos(d_{\theta})}-k_r) l′′=−Ckrl′tan(dθ)+cos(dθ)C1−krl(cos(dθ)kxC1−krl−kr);
step 8:计算 s = s r s=s_r s=sr;
step 9:计算 s ˙ = v x cos ( d θ ) C 1 − k r l \dot{s}=\frac{v_x \cos(d_{\theta})}{C_{1-k_r l}} s˙=C1−krlvxcos(dθ);
step 10:计算 C d θ ′ = C 1 − k r l k x cos ( d θ ) − k r C_{d_{\theta}'}=\frac{C_{1-k_r l}}{k_x\cos(d_{\theta})}-k_r Cdθ′=kxcos(dθ)C1−krl−kr;
step 11:计算 s ¨ = a x cos ( d θ ) − s ˙ 2 ( l ′ C d θ ′ − C k r l ′ ) C 1 − k r l \ddot{s}=\frac{a_x\cos(d_{\theta})-\dot{s}^2(l'C_{d_{\theta}'}-C_{k_r l'})}{C_{1-k_r l}} s¨=C1−krlaxcos(dθ)−s˙2(l′Cdθ′−Ckrl′)
Frenet坐标系与Cartesian坐标系互转(二):Python代码函数实现
Frenet坐标系与Cartesian坐标系互转(三):应用示例
END
by windSeS
first update 2020.9.23
modified at 2022.5.19