Skip to content

CoTiMo Planner Code Report

GitHub Repo stars

Collision-Free Smooth Path Generation

Cubic Spline

For a group of certain points (x0,1,x0,2,,x0,m),(x1,1,x1,2,,x1,m),(x2,1,x2,2,,x2,m),,(xn,1,xn,2,,xn,m), we want to find a smooth enough curve that passes all these points to achieve the interpolation goal. To do so, we can use piecewise cubic functions to fitting the points.

Let's talk about a specific dimention k first. To simplify the denotation, we let xi=xi,k. For a set of n+1 points (x0,k,x1,k,x2,k,,xn,k) and for the i-th piece of the spline, we can represent it by

qi(s)=ais3+bis2+cis+di,    s[0,1], i=0,1,,n1

To ensure the smooth of the curve, we write down the constraints,

{qi1(1)=qi(0)=xiqi1(1)=qi(0)qi1(1)=qi(0)

Apply ai,bi,ci,di to the constraints, we get

{ai1+bi1+ci1+di1=di3ai1+2bi1+ci1=ci6ai1+2bi1=2bi

So we can get the following expressions

{ai=xi+22xi+1+xibi=xi+2+2xi+1,kxici=xi+1xidi=xi , i=1,2,,n1

In open loop case, assume stationary boundary q0(0)=0,qn1(1)=0, which is c0=0,3an1+2bn1+cn1=0.

You can also regard xn+2 as xn+1, so that xn+22xn+1+xn=xn+1+xn

Convert the expressions to vector form, we have

[a0a1a2a3an2an1]=[231000001210000012000000100000001210000011][x0x1x2x3xn2xn1xn][b0b1b2b3bn2bn1]=[341000001210000012000000100000001210000011][x0x1x2x3xn2xn1xn][c0c1c2c3cn2cn1]=[000000001100000011000000100000001100000011][x0x1x2x3xn2xn1xn][d0d1d2d3dn2dn1]=[100000001000000010000000100000001000000010][x0x1x2x3xn2xn1xn]

For every dimension in x, we would obtain four vector a,b,c,d

Minimize Stretch Energy

Then, we can define energy function by acceleration

Energy(x0,x1,,xn)=i=1n101||qi(s)||1ds

But in practice, I use the following formula

Energy(x0,x1,,xn)=i=1n1||xi12xi+xi+1||2

Collision Free

We want to avoid our robot get crashed into the obstacle. So that we also need to define a potential function.

First, we need to calculate the distance between the robot and the obstacle. Which is find a point o in obstacle (Aob), to minimize the distance from robot x to point o

min ||ox||2s.t. Aobmin oTo2xTo+xTxs.t. Aob

By SOCP, we can obtain the distance vector

d(x)=[d(x,o1),d(x,o2),,d(x,om)]T

And the potential function would be defined as below

Potential(x0,x1,,xn)=i=0nexp(||d(xi)||)

Loss Function

Loss(x0,x1,,xn)=ηEnergy(x0,x1,,xn)+(1η)Potential(x0,x1,,xn)

Finally, we convert the problem to an optimization problem, we can use Line Search, Quasi Newton, LBFGS, or Newton-CG to solve it.

minx0,x1,,xnLoss(x0,x1,,xn)

In practice, I add some trick to make the optimizer more stable. Additional to five different η, I introduce sigmoid function in update step.

σ(x)=11+ex=ex1+ex=1σ(x)xk+1=xkηk[2σ(Loss(x))1]

Time Optimal Path Parameterization

Continuous Case

After optimizing the control points x0,x1,,xn, we get a spline q uniquely determined by arc-length s.

Then the optimal time T is integrated by

T=0T1 dt=0L1dsdtds

Additional to q(s), dqds and d2qds2 are also known. Because the cubic spline is second order differentiable.

We also have velocity constraint,

vmaxvvmax

acceleration constraint,

amaxaamax

and always move forward constraint

dsdt>0

The true velocity is

dqdt=dqdsdsdt

The true acceleration is

d2qdt2=d2qds2dsdt+dqdsd2sdt2

Denote

a(s)=d2sdt2, b(s)=(dsdt)2

Then the problem is described by

mina(s),b(s) 0L1b(s)dss.t. b(s)=2a(s)q(0)b(0)=v0q(L)b(L)=vLb(s)>0   ()||q(s)b(s)||vmax||q(s)b(s)+q(s)a(s)||amax

Discrete Case

Try to obtain the discretized form aRK,s,bRK+1. K is different from the number of curves or control points N and is the result of resampling the curve. Usually K is greater than N.

mina,bk=1K2(sk+1sk)bk+1+bks.t. bk+1bksk+1sk=2ak,k[1,K]q(s0)b0=v0q(sK+1)bK+1=vLbk0,k[1,K+1]||q(sk)bk||vmax,k[1,K+1]||q(sk)bk+q(sk)ak||amax,k[1,K]

Second-Order Cones

To rewrite the problem to a second-order conic programming form, we first try to bound nonlinear term bk with ck, such that bkck,k[1,K+1]. Going a step further, we introduce one more slack variable dk, which satisfies 1ck+1+ckdk,k[1,K].

mina,bk=1K2(sk+1sk)bk+1+bk mina,b,c,dk=1K2dk(sk+1sk)s.t. ||ck+ck+1dk2||2ck+ck+1+dk,k[1,K]||bk12ck||2bk+1,k[1,K+1]||ck+ck+1dk2||2ck+ck+1+dk[ck+ck+1+dkck+ck+1dk2]Q3||bk12ck||2bk+1[bk+1bk12ck]Q3

Rewriting the constraints of the optimization problem, especially the nonlinear terms bk, we get the final objective function (2D case).

mina,b,c,dk=1K2(sk+1sk)dks.t.[ck+ck+1+dkck+ck+1dk2]Q3,k[1,K][bk+1bk12ck]Q3,k[1,K+1]2(sk+1sk)ak+bkbk+1=0,k[1,K]{[qx(s0)]2+[qy(s0)]2}(b0)2=v02{[qx(sK+1)]2+[qy(sK+1)]2}(bK+1)2=vL2bk0,k[1,K+1][qx(sk)]2bkvmax2,k[1,K+1][qy(sk)]2bkvmax2,k[1,K+1][qx(sk)]2bk+qx(sk)akamax,k[1,K][qy(sk)]2bk+qy(sk)akamax,k[1,K][qx(sk)]2bkqx(sk)akamax,k[1,K][qy(sk)]2bkqy(sk)akamax,k[1,K]

PHR ALM for Symmetric Cones

All constraints are divided into second-order cone constraints, equality constraints and inequality constraints.

minx cTxs.t. Ai+biKiGx=hPxq

Therefore, we can reformulate the problem using the Augmented Lagrangian method for Symmetric Cones.

Lρ(x,μ,λ,η)=cTx+ρ2i=1m||PKi(μiρAixbi)||2+ρ2||Gxhλρ||2+ρ2||max[PxQ+ηρ,0]||2

PK is the projection of a vector on a symmetric cone

PK(v)=argminxK||vx||2

In second order cone case,

PK=Qn(v)={0,v0||v1||2v0+||v1||22||v1||2(||v1||2,v1)T,|v0|<||v1||2v,v0||v1||2

And the gradient of the augmented Lagrangian function is given by

Lρ(x,μ,λ,η)=c+ρi=1mAiTPKi(μiρAixbi)+ρGT(Gxhλρ)+ρP{max[PxQ+ηρ,0]}

So we can use a L-BFGS method to solve this convex and unconstrained function.

xargminxLρ(x,μ,λ,η)μiPKi(μiρ(Aix+bi))λλ+ρ(Gxh)ηmax[η+ρ(Pxq),0]ρmin[(1+γ)ρ,β]

γ is the growth rate of ρ and β is the upper bound of ρ, which is typically 1e3

Model Predictive Control

Swerve Kinematic Model

A simple omnidirectional platform is mecanum wheel chassis. But for a FRC player, a swerve chassis is what we need.

Consider only the translation case. Let's simply denote (x,y) for position, v for velocity, a for acceleration, θ for angle, ω for angular velocity, α for angular acceleration.

[x˙y˙v˙θ˙ω˙]=[vcosθvsinθaωα]

So that, we can use the following equation to update the states in discrete time domian.

{vk+1=vk+aΔtωk+1=ωk+αΔtθk+1=θk+12(ωk+ωk+1)Δtxk+1=xk+12(vkcosθk+vk+1cosθk+1)Δtyk+1=yk+12(vksinθk+vk+1sinθk+1)Δt,  uk=[aα]

Objective Function

Denote the number of control points of optimal control is M, and u¯k=[ukTuk+1Tuk+M1T]TR2M

Loss(u¯k)=i=0M1[(xk+ix^k+1)2+(yk+iy^k+1)2+ωa(ak+i1ak+i)2+ωα(αk+i1αk+1)2]minu¯k Loss(u¯k)s.t. aminaiamax,i[k,k+M1]αminαiαmax,i[k,k+M1]

The method to solve this objective function is similar to the PHR ALM method mentioned above, except that the terms related to the symmetric cone are removed.

Reference

ImageLink
https://mathworld.wolfram.com/images/eps-svg/CubicSpline_700.svg
https://www.tandfonline.com/cms/asset/bc9fed8a-15a8-49ee-96be-ce81ead2a54b/tgis_a_1988088_f0001_b.gif
https://www.freshconsulting.com/wp-content/uploads/2023/02/image-1536x1022.png