├── .gitignore ├── AutonomousParking ├── DualMultWS.jl ├── ParkingConstraints.jl ├── ParkingDist.jl ├── ParkingSignedDist.jl ├── README.md ├── a_star.jl ├── collision_check.jl ├── hybrid_a_star.jl ├── main.jl ├── obstHrep.jl ├── plotTraj.jl ├── reeds_shepp.jl ├── setup.jl └── veloSmooth.jl ├── LICENSE ├── QuadcopterNavigation ├── QuadcopterDist.jl ├── QuadcopterSignedDist.jl ├── README.md ├── a_star_3D.jl ├── constrSatisfaction.jl ├── mainQuadcopter.jl ├── plotTrajQuadcopter.jl └── setupQuadcopter.jl ├── README.md └── images ├── TrajBack_ParkingVideo.gif ├── TrajPar_ParkingVideo.gif ├── TrajQuad_3D_Video.gif └── TrajTrailer_ParkingVideo.gif /.gitignore: -------------------------------------------------------------------------------- 1 | *.jl.cov 2 | *.jl.*.cov 3 | *.jl.mem 4 | deps/deps.jl 5 | Icon? 6 | -------------------------------------------------------------------------------- /AutonomousParking/DualMultWS.jl: -------------------------------------------------------------------------------- 1 | ############### 2 | # OBCA: Optimization-based Collision Avoidance - a path planner for autonomous parking 3 | # Copyright (C) 2018 4 | # Alexander LINIGER [liniger@control.ee.ethz.ch; Automatic Control Lab, ETH Zurich] 5 | # Xiaojing ZHANG [xiaojing.zhang@berkeley.edu; MPC Lab, UC Berkeley] 6 | # 7 | # This program is free software: you can redistribute it and/or modify 8 | # it under the terms of the GNU General Public License as published by 9 | # the Free Software Foundation, either version 3 of the License, or 10 | # (at your option) any later version. 11 | # 12 | # This program is distributed in the hope that it will be useful, 13 | # but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 | # GNU General Public License for more details. 16 | # 17 | # You should have received a copy of the GNU General Public License 18 | # along with this program. If not, see . 19 | ############### 20 | # The paper describing the theory can be found here: 21 | # X. Zhang, A. Liniger and F. Borrelli; "Optimization-Based Collision Avoidance"; Technical Report, 2017, [https://arxiv.org/abs/1711.03449] 22 | ############### 23 | 24 | ############### 25 | # DualMultWS.jl: computes warm starting points for dual multipliers lambda and mu 26 | ############### 27 | # 28 | # 29 | function DualMultWS(N,nOb,vOb, A, b,rx,ry,ryaw) 30 | 31 | x = zeros(3,N+1) 32 | x[1,:] = rx 33 | x[2,:] = ry 34 | x[3,:] = ryaw 35 | 36 | m = Model(solver=IpoptSolver(hessian_approximation="exact",mumps_pivtol=1e-5, 37 | max_iter=100,tol=1e-5, print_level=0, suppress_all_output="yes")) 38 | 39 | W_ev = ego[2]+ego[4] 40 | L_ev = ego[1]+ego[3] 41 | 42 | g = [L_ev/2,W_ev/2,L_ev/2,W_ev/2] 43 | 44 | # ofset from X-Y to the center of the ego set 45 | offset = (ego[1]+ego[3])/2 - ego[3] 46 | 47 | 48 | @variable(m, l[1:sum(vOb),1:(N+1)]) # dual multiplier associated with obstacleShape 49 | @variable(m, n[1:nOb*4,1:(N+1)]) # dual multiplier associated with carShape 50 | @variable(m, d[1:nOb,1:(N+1)]) 51 | 52 | @NLobjective(m, Max,sum(sum(d[i,k] for k=1:N+1) for i=1:nOb )) 53 | 54 | @constraint(m, l .>= 0 ) 55 | @constraint(m, n .>= 0) 56 | 57 | for i in 1:N+1 # iterate over time steps 58 | for j = 1 : nOb # iterate over obstacles 59 | Aj = A[sum(vOb[1:j-1])+1 : sum(vOb[1:j]) ,:] # extract obstacle matrix associated with j-th obstacle 60 | lj = l[sum(vOb[1:j-1])+1 : sum(vOb[1:j]) ,:] # extract lambda dual variables associate j-th obstacle 61 | nj = n[(j-1)*4+1:j*4 ,:] # extract mu dual variables associated with j-th obstacle 62 | bj = b[sum(vOb[1:j-1])+1 : sum(vOb[1:j])] # extract obstacle matrix associated with j-th obstacle 63 | 64 | # norm(A'*lambda) <= 1 65 | @constraint(m, (sum(Aj[k,1]*lj[k,i] for k = 1 : vOb[j]))^2 + (sum(Aj[k,2]*lj[k,i] for k = 1 : vOb[j]))^2 <= 1 ) 66 | 67 | # G'*mu + R'*A*lambda = 0 68 | @constraint(m, (nj[1,i] - nj[3,i]) + cos(x[3,i])*sum(Aj[k,1]*lj[k,i] for k = 1:vOb[j]) + sin(x[3,i])*sum(Aj[k,2]lj[k,i] for k = 1:vOb[j]) == 0 ) 69 | @constraint(m, (nj[2,i] - nj[4,i]) - sin(x[3,i])*sum(Aj[k,1]*lj[k,i] for k = 1:vOb[j]) + cos(x[3,i])*sum(Aj[k,2]lj[k,i] for k = 1:vOb[j]) == 0 ) 70 | 71 | # -g'*mu + (A*t - b)*lambda > 0 72 | @constraint(m, d[j,i] == -sum(g[k]*nj[k,i] for k = 1:4) + (x[1,i]+cos(x[3,i])*offset)*sum(Aj[k,1]*lj[k,i] for k = 1:vOb[j]) 73 | + (x[2,i]+sin(x[3,i])*offset)*sum(Aj[k,2]*lj[k,i] for k=1:vOb[j]) - sum(bj[k]*lj[k,i] for k=1:vOb[j])) 74 | end 75 | end 76 | tic() 77 | solve(m) 78 | time = toq(); 79 | # print("Auxillery Problem time = ",time,"\n") 80 | 81 | lp = getvalue(l)' 82 | np = getvalue(n)' 83 | 84 | return lp,np 85 | 86 | end -------------------------------------------------------------------------------- /AutonomousParking/ParkingConstraints.jl: -------------------------------------------------------------------------------- 1 | ############### 2 | # OBCA: Optimization-based Collision Avoidance - a path planner for autonomous parking 3 | # Copyright (C) 2018 4 | # Alexander LINIGER [liniger@control.ee.ethz.ch; Automatic Control Lab, ETH Zurich] 5 | # Xiaojing ZHANG [xiaojing.zhang@berkeley.edu; MPC Lab, UC Berkeley] 6 | # 7 | # This program is free software: you can redistribute it and/or modify 8 | # it under the terms of the GNU General Public License as published by 9 | # the Free Software Foundation, either version 3 of the License, or 10 | # (at your option) any later version. 11 | # 12 | # This program is distributed in the hope that it will be useful, 13 | # but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 | # GNU General Public License for more details. 16 | # 17 | # You should have received a copy of the GNU General Public License 18 | # along with this program. If not, see . 19 | ############### 20 | # The paper describing the theory can be found here: 21 | # X. Zhang, A. Liniger and F. Borrelli; "Optimization-Based Collision Avoidance"; Technical Report, 2017, [https://arxiv.org/abs/1711.03449] 22 | ############### 23 | 24 | ############### 25 | # Main file: computes Collision-Free and Minimum-Penetration trajectories for parking 26 | ############### 27 | 28 | 29 | function ParkingConstraints(x0,xF,N,Ts,L,ego,XYbounds,nOb,vOb, A, b,x,u,l,n,timeScale,fixTime,sd) 30 | 31 | 32 | # desired safety distance 33 | dmin = 0.05 # anything bigger than 0, e.g. 0.05 34 | 35 | c0 = zeros(5,1) 36 | c1 = zeros(4,1) 37 | c2 = zeros(4,1) 38 | c3 = zeros(4,N) 39 | c4 = zeros(1,1) 40 | c5 = zeros(1,1) 41 | c6 = zeros(4,N+1) 42 | 43 | e = zeros(7,1) 44 | 45 | c0[1] = maximum(abs(u[1,:]))-0.6 # should be <= 0 46 | c0[2] = maximum(abs(u[2,:]))-0.4 # should be <= 0 47 | c0[3] = maximum(abs(timeScale-1))-0.2 # should be <= 0 48 | c0[4] = -minimum(l) # should be <= 0 49 | c0[5] = -minimum(n) # should be <= 0 50 | 51 | #starting point 52 | c1[1] = abs(x[1,1] - x0[1]) # should be <= 0 53 | c1[2] = abs(x[2,1] - x0[2]) # should be <= 0 54 | c1[3] = abs(x[3,1] - x0[3]) # should be <= 0 55 | c1[4] = abs(x[4,1] - x0[4]) # should be <= 0 56 | 57 | #end point 58 | c2[1] = abs(x[1,N+1] - xF[1]) # should be <= 0 59 | c2[2] = abs(x[2,N+1] - xF[2]) # should be <= 0 60 | c2[3] = abs(x[3,N+1] - xF[3]) # should be <= 0 61 | c2[4] = abs(x[4,N+1] - xF[4]) # should be <= 0 62 | 63 | ############################## 64 | # dynamics of the car 65 | ############################## 66 | # - unicycle dynamic with euler forward 67 | # - sampling time scaling, is identical over the horizon 68 | 69 | for i in 1:N 70 | if fixTime == 1 71 | c3[1,i] = x[1,i+1] - (x[1,i] + Ts*(x[4,i] + Ts/2*u[2,i])*cos((x[3,i] + Ts/2*x[4,i]*tan(u[1,i])/L))) 72 | c3[2,i] = x[2,i+1] - (x[2,i] + Ts*(x[4,i] + Ts/2*u[2,i])*sin((x[3,i] + Ts/2*x[4,i]*tan(u[1,i])/L))) 73 | c3[3,i] = x[3,i+1] - (x[3,i] + Ts*(x[4,i] + Ts/2*u[2,i])*tan(u[1,i])/L) 74 | c3[4,i] = x[4,i+1] - (x[4,i] + Ts*u[2,i]) 75 | else 76 | c3[1,i] = x[1,i+1] - (x[1,i] + timeScale[i]*Ts*(x[4,i] + timeScale[i]*Ts/2*u[2,i])*cos((x[3,i] + timeScale[i]*Ts/2*x[4,i]*tan(u[1,i])/L))) 77 | c3[1,i] = x[2,i+1] - (x[2,i] + timeScale[i]*Ts*(x[4,i] + timeScale[i]*Ts/2*u[2,i])*sin((x[3,i] + timeScale[i]*Ts/2*x[4,i]*tan(u[1,i])/L))) 78 | c3[1,i] = x[3,i+1] - (x[3,i] + timeScale[i]*Ts*(x[4,i] + timeScale[i]*Ts/2*u[2,i])*tan(u[1,i])/L) 79 | c3[1,i] = x[4,i+1] - (x[4,i] + timeScale[i]*Ts*u[2,i]) 80 | end 81 | end 82 | 83 | u0 = [0,0] 84 | 85 | if fixTime == 1 86 | c5 = maximum(abs(diff([0. u[1,:]']'))/Ts) - 0.6 87 | c4 = 0 88 | else 89 | c4 = maximum(abs(diff(timeScale))) 90 | c5 = maximum(abs(diff([0 u[1,:]']'))/(timeScale[1]*Ts)) - 0.6 91 | end 92 | 93 | 94 | ############################## 95 | # obstacle avoidance constraints 96 | ############################## 97 | 98 | # width and length of ego set 99 | W_ev = ego[2]+ego[4] 100 | L_ev = ego[1]+ego[3] 101 | 102 | g = [L_ev/2,W_ev/2,L_ev/2,W_ev/2] 103 | 104 | # ofset from X-Y to the center of the ego set 105 | offset = (ego[1]+ego[3])/2 - ego[3] 106 | 107 | 108 | for i in 1:N+1 # iterate over time steps 109 | for j = 1 : nOb # iterate over obstacles 110 | Aj = A[sum(vOb[1:j-1])+1 : sum(vOb[1:j]) ,:] # extract obstacle matrix associated with j-th obstacle 111 | lj = l[sum(vOb[1:j-1])+1 : sum(vOb[1:j]) ,:] # extract lambda dual variables associate j-th obstacle 112 | nj = n[(j-1)*4+1:j*4 ,:] # extract mu dual variables associated with j-th obstacle 113 | bj = b[sum(vOb[1:j-1])+1 : sum(vOb[1:j])] # extract obstacle matrix associated with j-th obstacle 114 | 115 | # norm(A'*lambda) <= 1 116 | if sd == 1 117 | c6[1,i] = abs((sum(Aj[k,1]*lj[k,i] for k = 1 : vOb[j]))^2 + (sum(Aj[k,2]*lj[k,i] for k = 1 : vOb[j]))^2) - 1 118 | else 119 | c6[1,i] = (sum(Aj[k,1]*lj[k,i] for k = 1 : vOb[j]))^2 + (sum(Aj[k,2]*lj[k,i] for k = 1 : vOb[j]))^2 - 1 120 | end 121 | 122 | # G'*mu + R'*A*lambda = 0 123 | c6[2,i] = abs((nj[1,i] - nj[3,i]) + cos(x[3,i])*sum(Aj[k,1]*lj[k,i] for k = 1:vOb[j]) + sin(x[3,i])*sum(Aj[k,2]lj[k,i] for k = 1:vOb[j])) 124 | c6[3,i] = abs((nj[2,i] - nj[4,i]) - sin(x[3,i])*sum(Aj[k,1]*lj[k,i] for k = 1:vOb[j]) + cos(x[3,i])*sum(Aj[k,2]lj[k,i] for k = 1:vOb[j])) 125 | 126 | # -g'*mu + (A*t - b)*lambda > 0 127 | c6[4,i] = -(-sum(g[k]*nj[k,i] for k = 1:4) + (x[1,i]+cos(x[3,i])*offset)*sum(Aj[k,1]*lj[k,i] for k = 1:vOb[j]) 128 | + (x[2,i]+sin(x[3,i])*offset)*sum(Aj[k,2]*lj[k,i] for k=1:vOb[j]) - sum(bj[k]*lj[k,i] for k=1:vOb[j])) + dmin 129 | end 130 | 131 | end 132 | 133 | e[1] = maximum(c0)<= 5e-5 134 | e[2] = maximum(c1)<= 5e-5 135 | e[3] = maximum(c2)<= 5e-5 136 | e[4] = maximum(abs(c3))<= 5e-5 137 | e[5] = c4 <= 5e-5 138 | e[6] = c5 <= 5e-5 139 | e[7] = maximum(c6)<= 5e-5 140 | 141 | # print(e,"\n") 142 | 143 | if sum(e) == 7 144 | return 1 145 | else 146 | return 0 147 | end 148 | 149 | end 150 | -------------------------------------------------------------------------------- /AutonomousParking/ParkingDist.jl: -------------------------------------------------------------------------------- 1 | ############### 2 | # OBCA: Optimization-based Collision Avoidance - a path planner for autonomous parking 3 | # Copyright (C) 2018 4 | # Alexander LINIGER [liniger@control.ee.ethz.ch; Automatic Control Lab, ETH Zurich] 5 | # Xiaojing ZHANG [xiaojing.zhang@berkeley.edu; MPC Lab, UC Berkeley] 6 | # 7 | # This program is free software: you can redistribute it and/or modify 8 | # it under the terms of the GNU General Public License as published by 9 | # the Free Software Foundation, either version 3 of the License, or 10 | # (at your option) any later version. 11 | # 12 | # This program is distributed in the hope that it will be useful, 13 | # but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 | # GNU General Public License for more details. 16 | # 17 | # You should have received a copy of the GNU General Public License 18 | # along with this program. If not, see . 19 | ############### 20 | # The paper describing the theory can be found here: 21 | # X. Zhang, A. Liniger and F. Borrelli; "Optimization-Based Collision Avoidance"; Technical Report, 2017, [https://arxiv.org/abs/1711.03449] 22 | ############### 23 | 24 | ############### 25 | # computes collision-free trajectory by appropriately reformulating the distance function 26 | ############### 27 | 28 | 29 | function ParkingDist(x0,xF,N,Ts,L,ego,XYbounds,nOb,vOb, A, b,rx,ry,ryaw,fixTime,xWS,uWS) 30 | 31 | 32 | # desired safety distance 33 | dmin = 0.05 # anything bigger than 0, e.g. 0.05 34 | 35 | ############################## 36 | # Define JuMP file 37 | ############################## 38 | # Define IPOPT as solver and well as solver settings 39 | ############################## 40 | # seems to work best 41 | m = Model(solver=IpoptSolver(hessian_approximation="exact",mumps_pivtol=1e-6,alpha_for_y="min",recalc_y="yes", 42 | mumps_mem_percent=6000,max_iter=200,tol=1e-5, print_level=0, 43 | min_hessian_perturbation=1e-12,jacobian_regularization_value=1e-7))#,nlp_scaling_method="none" 44 | 45 | # fixTime = 0 46 | ############################## 47 | # defining optimization variables 48 | ############################## 49 | #state 50 | @variable(m, x[1:4,1:(N+1)]) 51 | #scaling on sampling time 52 | if fixTime == 0 53 | @variable(m, timeScale[1:N+1]) 54 | end 55 | # timeScale = ones(1,N+1) 56 | #control 57 | @variable(m, u[1:2,1:(N)]) 58 | # lagrange multipliers for dual dist function 59 | @variable(m, l[1:sum(vOb),1:(N+1)]) # dual multiplier associated with obstacleShape 60 | @variable(m, n[1:nOb*4,1:(N+1)]) # dual multiplier associated with carShape 61 | 62 | 63 | # regularization parameter to improve numerical stability 64 | reg = 1e-7; 65 | ############################## 66 | # cost function 67 | ############################## 68 | # (min control inputs)+ 69 | # (min time)+ 70 | # (regularization dual variables) 71 | ############################## 72 | # @NLobjective(m, Min,sum(0.1*u[1,i]^2 + 1*u[2,i]^2 for i = 1:N) + 73 | # sum(0.5*timeScale[i] + 1*timeScale[i]^2 for i = 1:N+1)+ 74 | # 0*sum(sum(reg*n[j,i]^2 for i = 1:N+1) for j = 1:4) + 75 | # 0*sum(sum(reg*l[j,i]^2 for i = 1:N+1) for j = 1:sum(vOb)) ) 76 | u0 = [0,0] 77 | #fix time objective 78 | if fixTime == 1 79 | @NLobjective(m, Min,sum(0.01*u[1,i]^2 + 0.5*u[2,i]^2 for i = 1:N) + 80 | sum(0.1*((u[1,i+1]-u[1,i])/Ts)^2 + 0.1*((u[2,i+1]-u[2,i])/Ts)^2 for i = 1:N-1)+ 81 | (0.1*((u[1,1]-u0[1])/(Ts))^2 + 0.1*((u[2,1]-u0[2])/(Ts))^2) + 82 | sum(0.0001*x[4,i]^2 for i=1:N+1)+ 83 | sum(0.001*(x[1,i]-rx[i])^2 + 0.001*(x[2,i]-ry[i])^2 + 0.01*(x[3,i]-ryaw[i])^2 for i=1:N+1)) 84 | # sum(0.1*(x[1,i]-rx[i])^2 + 0.1*(x[2,i]-ry[i])^2 + 0.0001*(x[3,i]-ryaw[i])^2 for i=1:N+1) ) 85 | else 86 | #varo time objective 87 | @NLobjective(m, Min,sum(0.01*u[1,i]^2 + 0.5*u[2,i]^2 for i = 1:N) + 88 | sum(0.1*((u[1,i+1]-u[1,i])/(timeScale[i]*Ts))^2 + 0.1*((u[2,i+1]-u[2,i])/(timeScale[i]*Ts))^2 for i = 1:N-1) + 89 | (0.1*((u[1,1]-u0[1]) /(timeScale[1]*Ts))^2 + 0.1*((u[2,1]-u0[2]) /(timeScale[1]*Ts))^2) + 90 | sum(0.5*timeScale[i] + 1*timeScale[i]^2 for i = 1:N+1)+ 91 | sum(0.0001*x[4,i]^2 for i=1:N+1)+ 92 | sum(0.001*(x[1,i]-rx[i])^2 + 0.001*(x[2,i]-ry[i])^2 + 0.0001*(x[3,i]-ryaw[i])^2 for i=1:N+1) ) 93 | # 94 | end 95 | 96 | ############################## 97 | # bounds on states, inputs, 98 | # and dual multipliers. 99 | ############################## 100 | #input constraints 101 | @constraint(m, [i=1:N], -0.6 <= u[1,i] <= 0.6) 102 | @constraint(m, [i=1:N], -0.4 <= u[2,i] <= 0.4) 103 | 104 | #state constraints 105 | @constraint(m, [i=1:N+1], XYbounds[1] <= x[1,i] <= XYbounds[2]) 106 | @constraint(m, [i=1:N+1], XYbounds[3] <= x[2,i] <= XYbounds[4]) 107 | @constraint(m, [i=1:N+1], -1 <= x[4,i] <= 2) 108 | 109 | # bounds on time scaling 110 | if fixTime == 0 111 | @constraint(m, 0.8 .<= timeScale .<= 1.2) 112 | end 113 | 114 | 115 | # positivity constraints on dual multipliers 116 | @constraint(m, l .>= 0) 117 | @constraint(m, n .>= 0) 118 | 119 | ############################## 120 | # start and finish point 121 | ############################## 122 | 123 | #starting point 124 | @constraint(m, x[1,1] == x0[1]) 125 | @constraint(m, x[2,1] == x0[2]) 126 | @constraint(m, x[3,1] == x0[3]) 127 | @constraint(m, x[4,1] == x0[4]) 128 | 129 | #end point 130 | @constraint(m, x[1,N+1] == xF[1]) 131 | @constraint(m, x[2,N+1] == xF[2]) 132 | @constraint(m, x[3,N+1] == xF[3]) 133 | @constraint(m, x[4,N+1] == xF[4]) 134 | 135 | ############################## 136 | # dynamics of the car 137 | ############################## 138 | # - unicycle dynamic with euler forward 139 | # - sampling time scaling, is identical over the horizon 140 | 141 | for i in 1:N 142 | if fixTime == 1 143 | @NLconstraint(m, x[1,i+1] == x[1,i] + Ts*(x[4,i] + Ts/2*u[2,i])*cos((x[3,i] + Ts/2*x[4,i]*tan(u[1,i])/L))) 144 | @NLconstraint(m, x[2,i+1] == x[2,i] + Ts*(x[4,i] + Ts/2*u[2,i])*sin((x[3,i] + Ts/2*x[4,i]*tan(u[1,i])/L))) 145 | @NLconstraint(m, x[3,i+1] == x[3,i] + Ts*(x[4,i] + Ts/2*u[2,i])*tan(u[1,i])/L) 146 | @NLconstraint(m, x[4,i+1] == x[4,i] + Ts*u[2,i]) 147 | else 148 | @NLconstraint(m, x[1,i+1] == x[1,i] + timeScale[i]*Ts*(x[4,i] + timeScale[i]*Ts/2*u[2,i])*cos((x[3,i] + timeScale[i]*Ts/2*x[4,i]*tan(u[1,i])/L))) 149 | @NLconstraint(m, x[2,i+1] == x[2,i] + timeScale[i]*Ts*(x[4,i] + timeScale[i]*Ts/2*u[2,i])*sin((x[3,i] + timeScale[i]*Ts/2*x[4,i]*tan(u[1,i])/L))) 150 | @NLconstraint(m, x[3,i+1] == x[3,i] + timeScale[i]*Ts*(x[4,i] + timeScale[i]*Ts/2*u[2,i])*tan(u[1,i])/L) 151 | @NLconstraint(m, x[4,i+1] == x[4,i] + timeScale[i]*Ts*u[2,i]) 152 | end 153 | 154 | if fixTime == 0 155 | @constraint(m, timeScale[i] == timeScale[i+1]) 156 | end 157 | end 158 | 159 | u0 = [0,0] 160 | if fixTime == 1 161 | for i in 1:N 162 | if i==1 163 | @constraint(m,-0.6<=(u0[1]-u[1,i])/Ts <= 0.6) 164 | else 165 | @constraint(m,-0.6<=(u[1,i-1]-u[1,i])/Ts <= 0.6) 166 | end 167 | end 168 | else 169 | for i in 1:N 170 | if i==1 171 | @NLconstraint(m,-0.6<=(u0[1]-u[1,i])/(timeScale[i]*Ts) <= 0.6) 172 | else 173 | @NLconstraint(m,-0.6<=(u[1,i-1]-u[1,i])/(timeScale[i]*Ts) <= 0.6) 174 | end 175 | end 176 | end 177 | 178 | 179 | ############################## 180 | # obstacle avoidance constraints 181 | ############################## 182 | 183 | # width and length of ego set 184 | W_ev = ego[2]+ego[4] 185 | L_ev = ego[1]+ego[3] 186 | 187 | g = [L_ev/2,W_ev/2,L_ev/2,W_ev/2] 188 | 189 | # ofset from X-Y to the center of the ego set 190 | offset = (ego[1]+ego[3])/2 - ego[3] 191 | 192 | for i in 1:N+1 # iterate over time steps 193 | for j = 1 : nOb # iterate over obstacles 194 | Aj = A[sum(vOb[1:j-1])+1 : sum(vOb[1:j]) ,:] # extract obstacle matrix associated with j-th obstacle 195 | lj = l[sum(vOb[1:j-1])+1 : sum(vOb[1:j]) ,:] # extract lambda dual variables associate j-th obstacle 196 | nj = n[(j-1)*4+1:j*4 ,:] # extract mu dual variables associated with j-th obstacle 197 | bj = b[sum(vOb[1:j-1])+1 : sum(vOb[1:j])] # extract obstacle matrix associated with j-th obstacle 198 | 199 | # norm(A'*lambda) <= 1 200 | @NLconstraint(m, (sum(Aj[k,1]*lj[k,i] for k = 1 : vOb[j]))^2 + (sum(Aj[k,2]*lj[k,i] for k = 1 : vOb[j]))^2 <= 1 ) 201 | 202 | # G'*mu + R'*A*lambda = 0 203 | @NLconstraint(m, (nj[1,i] - nj[3,i]) + cos(x[3,i])*sum(Aj[k,1]*lj[k,i] for k = 1:vOb[j]) + sin(x[3,i])*sum(Aj[k,2]lj[k,i] for k = 1:vOb[j]) == 0 ) 204 | @NLconstraint(m, (nj[2,i] - nj[4,i]) - sin(x[3,i])*sum(Aj[k,1]*lj[k,i] for k = 1:vOb[j]) + cos(x[3,i])*sum(Aj[k,2]lj[k,i] for k = 1:vOb[j]) == 0 ) 205 | 206 | # -g'*mu + (A*t - b)*lambda > 0 207 | @NLconstraint(m, (-sum(g[k]*nj[k,i] for k = 1:4) + (x[1,i]+cos(x[3,i])*offset)*sum(Aj[k,1]*lj[k,i] for k = 1:vOb[j]) 208 | + (x[2,i]+sin(x[3,i])*offset)*sum(Aj[k,2]*lj[k,i] for k=1:vOb[j]) - sum(bj[k]*lj[k,i] for k=1:vOb[j])) >= dmin ) 209 | end 210 | end 211 | 212 | ############################## 213 | # set initial guesses 214 | ############################## 215 | if fixTime == 0 216 | setvalue(timeScale,1*ones(N+1,1)) 217 | end 218 | setvalue(x,xWS') 219 | setvalue(u,uWS[1:N,:]') 220 | 221 | lWS,nWS = DualMultWS(N,nOb,vOb, A, b,rx,ry,ryaw) 222 | 223 | setvalue(l,lWS') 224 | setvalue(n,nWS') 225 | 226 | ############################## 227 | # solve problem 228 | ############################## 229 | # ipopt has sometimes problems in the restoration phase, 230 | # it turns out that restarting ipopt with the previous solution 231 | # as an initial guess works well to achieve a high success rate. 232 | ############################## 233 | 234 | # at most three attempts considered 235 | time1 = 0 236 | time2 = 0 237 | 238 | exitflag = 0 239 | 240 | tic() 241 | status = solve(m; suppress_warnings=true) 242 | time1 = toq(); 243 | 244 | # we allow for two resolving attempts if restoration error 245 | if status == :Optimal 246 | exitflag = 1 247 | elseif status ==:Error || status ==:UserLimit || status ==:Infeasible # #|| status ==:Infeasible 248 | 249 | xp = getvalue(x) 250 | up = getvalue(u) 251 | if fixTime == 1 252 | timeScalep = ones(1,N+1) 253 | else 254 | timeScalep = getvalue(timeScale) 255 | end 256 | lp = getvalue(l) 257 | np = getvalue(n) 258 | Feasible = 0 259 | Feasible = ParkingConstraints(x0,xF,N,Ts,L,ego,XYbounds,nOb,vOb, A, b,xp,up,lp,np,timeScalep,fixTime,0) 260 | if Feasible == 0 261 | tic() 262 | status = solve(m; suppress_warnings=true) 263 | time2 = toq(); 264 | if status == :Optimal 265 | exitflag = 1 266 | elseif status ==:Error || status ==:UserLimit 267 | xp = getvalue(x) 268 | up = getvalue(u) 269 | if fixTime == 1 270 | timeScalep = ones(1,N+1) 271 | else 272 | timeScalep = getvalue(timeScale) 273 | end 274 | lp = getvalue(l) 275 | np = getvalue(n) 276 | Feasible = 0 277 | Feasible = ParkingConstraints(x0,xF,N,Ts,L,ego,XYbounds,nOb,vOb, A, b,xp,up,lp,np,timeScalep,fixTime,0) 278 | if Feasible == 0 279 | exitflag = 1 280 | else 281 | exitflag = 0 282 | end 283 | end 284 | else 285 | exitflag = 1 286 | end 287 | else 288 | exitflag = 0 289 | end 290 | 291 | ############################## 292 | # return values 293 | ############################## 294 | 295 | # computation times is the sum of all trials 296 | time = time1+time2 297 | # print(" elapsed time: ") 298 | # print(time) 299 | # println(" seconds") 300 | 301 | xp = getvalue(x) 302 | up = getvalue(u) 303 | if fixTime == 1 304 | timeScalep = ones(1,N+1) 305 | else 306 | timeScalep = getvalue(timeScale) 307 | end 308 | # 309 | 310 | lp = getvalue(l) 311 | np = getvalue(n) 312 | 313 | return xp, up, timeScalep, exitflag, time, lp, np 314 | 315 | end 316 | -------------------------------------------------------------------------------- /AutonomousParking/ParkingSignedDist.jl: -------------------------------------------------------------------------------- 1 | ############### 2 | # OBCA: Optimization-based Collision Avoidance - a path planner for autonomous parking 3 | # Copyright (C) 2018 4 | # Alexander LINIGER [liniger@control.ee.ethz.ch; Automatic Control Lab, ETH Zurich] 5 | # Xiaojing ZHANG [xiaojing.zhang@berkeley.edu; MPC Lab, UC Berkeley] 6 | # 7 | # This program is free software: you can redistribute it and/or modify 8 | # it under the terms of the GNU General Public License as published by 9 | # the Free Software Foundation, either version 3 of the License, or 10 | # (at your option) any later version. 11 | # 12 | # This program is distributed in the hope that it will be useful, 13 | # but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 | # GNU General Public License for more details. 16 | # 17 | # You should have received a copy of the GNU General Public License 18 | # along with this program. If not, see . 19 | ############### 20 | # The paper describing the theory can be found here: 21 | # X. Zhang, A. Liniger and F. Borrelli; "Optimization-Based Collision Avoidance"; Technical Report, 2017, [https://arxiv.org/abs/1711.03449] 22 | ############### 23 | 24 | ############### 25 | # Main file: computes Collision-Free and Minimum-Penetration trajectories for parking 26 | ############### 27 | 28 | 29 | function ParkingSignedDist(x0,xF,N,Ts,L,ego,XYbounds,nOb,vOb, A, b,rx,ry,ryaw,fixTime,xWS,uWS) 30 | 31 | 32 | # desired safety distance 33 | dmin = 0.05 # anything bigger than 0, e.g. 0.05 34 | 35 | ############################## 36 | # Define JuMP file 37 | ############################## 38 | # Define IPOPT as solver and well as solver settings 39 | ############################## 40 | # seems to work best 41 | m = Model(solver=IpoptSolver(hessian_approximation="exact",mumps_pivtol=1e-6,alpha_for_y="min",recalc_y="yes", 42 | mumps_mem_percent=6000,max_iter=200,tol=1e-5, print_level=0, 43 | min_hessian_perturbation=1e-12,jacobian_regularization_value=1e-7))#,nlp_scaling_method="none" 44 | 45 | ############################## 46 | # defining optimization variables 47 | ############################## 48 | #state 49 | @variable(m, x[1:4,1:(N+1)]) 50 | #scaling on sampling time 51 | if fixTime == 0 52 | @variable(m, timeScale[1:N+1]) 53 | end 54 | #control 55 | @variable(m, u[1:2,1:(N)]) 56 | # lagrange multipliers for dual dist function 57 | @variable(m, l[1:sum(vOb),1:(N+1)]) # dual multiplier associated with obstacleShape 58 | @variable(m, n[1:nOb*4,1:(N+1)]) # dual multiplier associated with carShape 59 | @variable(m, sl[1:nOb,1:(N+1)]) # slack variable to avoid infeasibilities 60 | 61 | 62 | # regularization parameter to improve numerical stability 63 | reg = 1e-7; 64 | ############################## 65 | # cost function 66 | ############################## 67 | # (min control inputs)+ 68 | # (min time)+ 69 | # (regularization dual variables) 70 | ############################## 71 | # @NLobjective(m, Min,sum(0.1*u[1,i]^2 + 1*u[2,i]^2 for i = 1:N) + 72 | # sum(0.5*timeScale[i] + 1*timeScale[i]^2 for i = 1:N+1)+ 73 | # 0*sum(sum(reg*n[j,i]^2 for i = 1:N+1) for j = 1:4) + 74 | # 0*sum(sum(reg*l[j,i]^2 for i = 1:N+1) for j = 1:sum(vOb)) ) 75 | u0 = [0,0] 76 | # fix time objective 77 | if fixTime == 1 78 | @NLobjective(m, Min,sum(0.01*u[1,i]^2 + 0.5*u[2,i]^2 for i = 1:N) + 79 | sum(0.1*((u[1,i+1]-u[1,i])/Ts)^2 + 0.1*((u[2,i+1]-u[2,i])/Ts)^2 for i = 1:N-1)+ 80 | (0.1*((u[1,1]-u0[1])/(Ts))^2 + 0.1*((u[2,1]-u0[2])/(Ts))^2) + 81 | sum(0.0001*x[4,i]^2 for i=1:N+1)+ 82 | sum(0.001*(x[1,i]-rx[i])^2 + 0.001*(x[2,i]-ry[i])^2 + 0.01*(x[3,i]-ryaw[i])^2 for i=1:N+1) + 83 | sum(sum(1e2*sl[j,i] + 1e4*sl[j,i]^2 for i = 1:N+1) for j = 1 : nOb) ) 84 | else 85 | # variable time objective 86 | @NLobjective(m, Min,sum(0.01*u[1,i]^2 + 0.1*u[2,i]^2 for i = 1:N) + 87 | sum(0.1*((u[1,i+1]-u[1,i])/(timeScale[i]*Ts))^2 + 0.1*((u[2,i+1]-u[2,i])/(timeScale[i]*Ts))^2 for i = 1:N-1) + 88 | (0.1*((u[1,1]-u0[1]) /(timeScale[1]*Ts))^2 + 0.1*((u[2,1]-u0[2]) /(timeScale[1]*Ts))^2) + 89 | sum(0.5*timeScale[i] + 1*timeScale[i]^2 for i = 1:N+1)+ 90 | sum(0.0001*x[4,i]^2 for i=1:N+1)+ 91 | sum(0.001*(x[1,i]-rx[i])^2 + 0.001*(x[2,i]-ry[i])^2 + 0.0001*(x[3,i]-ryaw[i])^2 for i=1:N+1) + 92 | sum(sum(1e2*sl[j,i] + 1e4*sl[j,i]^2 for i = 1:N+1) for j = 1 : nOb) ) # 93 | end 94 | 95 | ############################## 96 | # bounds on states, inputs, 97 | # and dual multipliers. 98 | ############################## 99 | #input constraints 100 | @constraint(m, [i=1:N], -0.6 <= u[1,i] <= 0.6) 101 | @constraint(m, [i=1:N], -0.4 <= u[2,i] <= 0.4) 102 | 103 | #state constraints 104 | @constraint(m, [i=1:N+1], XYbounds[1] <= x[1,i] <= XYbounds[2]) 105 | @constraint(m, [i=1:N+1], XYbounds[3] <= x[2,i] <= XYbounds[4]) 106 | @constraint(m, [i=1:N+1], -1 <= x[4,i] <= 2) 107 | 108 | # bounds on time scaling 109 | if fixTime == 0 110 | @constraint(m, 0.8 .<= timeScale .<= 1.2) 111 | end 112 | 113 | # positivity constraints on dual multipliers 114 | @constraint(m, l .>= 0) 115 | @constraint(m, n .>= 0) 116 | 117 | ############################## 118 | # start and finish point 119 | ############################## 120 | 121 | #starting point 122 | @constraint(m, x[1,1] == x0[1]) 123 | @constraint(m, x[2,1] == x0[2]) 124 | @constraint(m, x[3,1] == x0[3]) 125 | @constraint(m, x[4,1] == x0[4]) 126 | 127 | #end point 128 | @constraint(m, x[1,N+1] == xF[1]) 129 | @constraint(m, x[2,N+1] == xF[2]) 130 | @constraint(m, x[3,N+1] == xF[3]) 131 | @constraint(m, x[4,N+1] == xF[4]) 132 | 133 | ############################## 134 | # dynamics of the car 135 | ############################## 136 | # - unicycle dynamic with euler forward 137 | # - sampling time scaling, is identical over the horizon 138 | 139 | for i in 1:N 140 | 141 | if fixTime == 1 142 | @NLconstraint(m, x[1,i+1] == x[1,i] + Ts*(x[4,i] + Ts/2*u[2,i])*cos((x[3,i] + Ts/2*x[4,i]*tan(u[1,i])/L))) 143 | @NLconstraint(m, x[2,i+1] == x[2,i] + Ts*(x[4,i] + Ts/2*u[2,i])*sin((x[3,i] + Ts/2*x[4,i]*tan(u[1,i])/L))) 144 | @NLconstraint(m, x[3,i+1] == x[3,i] + Ts*(x[4,i] + Ts/2*u[2,i])*tan(u[1,i])/L) 145 | @NLconstraint(m, x[4,i+1] == x[4,i] + Ts*u[2,i]) 146 | else 147 | @NLconstraint(m, x[1,i+1] == x[1,i] + timeScale[i]*Ts*(x[4,i] + timeScale[i]*Ts/2*u[2,i])*cos((x[3,i] + timeScale[i]*Ts/2*x[4,i]*tan(u[1,i])/L))) 148 | @NLconstraint(m, x[2,i+1] == x[2,i] + timeScale[i]*Ts*(x[4,i] + timeScale[i]*Ts/2*u[2,i])*sin((x[3,i] + timeScale[i]*Ts/2*x[4,i]*tan(u[1,i])/L))) 149 | @NLconstraint(m, x[3,i+1] == x[3,i] + timeScale[i]*Ts*(x[4,i] + timeScale[i]*Ts/2*u[2,i])*tan(u[1,i])/L) 150 | @NLconstraint(m, x[4,i+1] == x[4,i] + timeScale[i]*Ts*u[2,i]) 151 | end 152 | if fixTime == 0 153 | @constraint(m, timeScale[i] == timeScale[i+1]) 154 | end 155 | end 156 | 157 | u0 = [0,0] 158 | if fixTime == 1 159 | for i in 1:N 160 | if i==1 161 | @constraint(m,-0.6<=(u0[1]-u[1,i])/Ts <= 0.6) 162 | else 163 | @constraint(m,-0.6<=(u[1,i-1]-u[1,i])/Ts <= 0.6) 164 | end 165 | end 166 | else 167 | for i in 1:N 168 | if i==1 169 | @NLconstraint(m,-0.6<=(u0[1]-u[1,i])/(timeScale[i]*Ts) <= 0.6) 170 | else 171 | @NLconstraint(m,-0.6<=(u[1,i-1]-u[1,i])/(timeScale[i]*Ts) <= 0.6) 172 | end 173 | end 174 | end 175 | 176 | 177 | ############################## 178 | # obstacle avoidance constraints 179 | ############################## 180 | 181 | # width and length of ego set 182 | W_ev = ego[2]+ego[4] 183 | L_ev = ego[1]+ego[3] 184 | 185 | g = [L_ev/2,W_ev/2,L_ev/2,W_ev/2] 186 | 187 | # ofset from X-Y to the center of the ego set 188 | offset = (ego[1]+ego[3])/2 - ego[3] 189 | 190 | for i in 1:N+1 # iterate over time steps 191 | for j = 1 : nOb # iterate over obstacles 192 | Aj = A[sum(vOb[1:j-1])+1 : sum(vOb[1:j]) ,:] # extract obstacle matrix associated with j-th obstacle 193 | lj = l[sum(vOb[1:j-1])+1 : sum(vOb[1:j]) ,:] # extract lambda dual variables associate j-th obstacle 194 | nj = n[(j-1)*4+1:j*4 ,:] # extract mu dual variables associated with j-th obstacle 195 | bj = b[sum(vOb[1:j-1])+1 : sum(vOb[1:j])] # extract obstacle matrix associated with j-th obstacle 196 | 197 | # norm(A'*lambda) <= 1 198 | @NLconstraint(m, (sum(Aj[k,1]*lj[k,i] for k = 1 : vOb[j]))^2 + (sum(Aj[k,2]*lj[k,i] for k = 1 : vOb[j]))^2 == 1 ) 199 | 200 | # G'*mu + R'*A*lambda = 0 201 | @NLconstraint(m, (nj[1,i] - nj[3,i]) + cos(x[3,i])*sum(Aj[k,1]*lj[k,i] for k = 1:vOb[j]) + sin(x[3,i])*sum(Aj[k,2]lj[k,i] for k = 1:vOb[j]) == 0 ) 202 | @NLconstraint(m, (nj[2,i] - nj[4,i]) - sin(x[3,i])*sum(Aj[k,1]*lj[k,i] for k = 1:vOb[j]) + cos(x[3,i])*sum(Aj[k,2]lj[k,i] for k = 1:vOb[j]) == 0 ) 203 | 204 | # -g'*mu + (A*t - b)*lambda > 0 205 | @NLconstraint(m, -sum(g[k]*nj[k,i] for k = 1:4) + (x[1,i]+cos(x[3,i])*offset)*sum(Aj[k,1]*lj[k,i] for k = 1:vOb[j]) 206 | + (x[2,i]+sin(x[3,i])*offset)*sum(Aj[k,2]*lj[k,i] for k=1:vOb[j]) - sum(bj[k]*lj[k,i] for k=1:vOb[j]) + sl[j,i] >= 1*dmin ) 207 | end 208 | end 209 | 210 | ############################## 211 | # set initial guesses 212 | ############################## 213 | if fixTime == 0 214 | setvalue(timeScale,1*ones(N+1,1)) 215 | end 216 | setvalue(x,xWS') 217 | setvalue(u,uWS[1:N,:]') 218 | 219 | lWS,nWS = DualMultWS(N,nOb,vOb, A, b,rx,ry,ryaw) 220 | 221 | setvalue(l,lWS') 222 | setvalue(n,nWS') 223 | 224 | 225 | ############################## 226 | # solve problem 227 | ############################## 228 | # ipopt has sometimes problems in the restoration phase, 229 | # it turns out that restarting ipopt with the previous solution 230 | # as an initial guess works well to achieve a high success rate. 231 | ############################## 232 | 233 | # at most three attempts considered 234 | time1 = 0 235 | time2 = 0 236 | 237 | exitflag = 0 238 | 239 | tic() 240 | status = solve(m; suppress_warnings=true) 241 | time1 = toq(); 242 | 243 | # tmp check 244 | xp = getvalue(x) 245 | up = getvalue(u) 246 | if fixTime == 1 247 | timeScalep = ones(1,N+1) 248 | else 249 | timeScalep = getvalue(timeScale) 250 | end 251 | lp = getvalue(l) 252 | np = getvalue(n) 253 | tmp_useless = ParkingConstraints(x0,xF,N,Ts,L,ego,XYbounds,nOb,vOb, A, b,xp,up,lp,np,timeScalep,fixTime,1) 254 | 255 | 256 | if status == :Optimal 257 | exitflag = 1 258 | elseif status ==:Error || status ==:UserLimit# || status ==:Infeasible 259 | Feasible = 0 260 | if Feasible == 0 261 | tic() 262 | status = solve(m; suppress_warnings=true) 263 | time2 = toq(); 264 | 265 | if status == :Optimal 266 | exitflag = 1 267 | elseif status ==:Error || status ==:UserLimit 268 | xp = getvalue(x) 269 | up = getvalue(u) 270 | if fixTime == 1 271 | timeScalep = ones(1,N+1) 272 | else 273 | timeScalep = getvalue(timeScale) 274 | end 275 | lp = getvalue(l) 276 | np = getvalue(n) 277 | Feasible = 0 278 | Feasible = ParkingConstraints(x0,xF,N,Ts,L,ego,XYbounds,nOb,vOb, A, b,xp,up,lp,np,timeScalep,fixTime,1) 279 | if Feasible == 1 280 | exitflag = 1 281 | else 282 | exitflag = 0 283 | end 284 | end 285 | else 286 | exitflag = 1 287 | end 288 | else 289 | exitflag = 0 290 | end 291 | 292 | ############################## 293 | # return values 294 | ############################## 295 | 296 | # computation times is the sum of all trials 297 | time = time1+time2 298 | # print(" elapsed time: ") 299 | # print(time) 300 | # println(" seconds") 301 | 302 | xp = getvalue(x) 303 | up = getvalue(u) 304 | if fixTime == 1 305 | timeScalep = ones(1,N+1) 306 | else 307 | timeScalep = getvalue(timeScale) 308 | end 309 | 310 | lp = getvalue(l) 311 | np = getvalue(n) 312 | 313 | return xp, up, timeScalep, exitflag, time, lp, np 314 | end 315 | -------------------------------------------------------------------------------- /AutonomousParking/README.md: -------------------------------------------------------------------------------- 1 | # OBCA - Autonomous Parking 2 | Optimization-Based Collision Avoidance - an application towards autonomous parking 3 | 4 | Paper describing the theory can be found [here](http://arxiv.org/abs/1711.03449). 5 | 6 | ## How to run the Parking code: 7 | 8 | ### First steps 9 | 10 | 1. Change to the directory 11 | 12 | 2. Install Julia from https://julialang.org/downloads/ (code tested on version 0.5 and 0.6) 13 | 14 | 3. Open Julia in terminal 15 | 16 | 4. Install Julia package JuMP using Pkg.add("JuMP") 17 | 18 | 5. Install Julia package Ipopt using Pkg.add("Ipopt") 19 | 20 | 6. Install Julia package PyPlot using Pkg.add("PyPlot") 21 | 22 | 7. Install Julia package PyPlot using Pkg.add("NearestNeighbors") 23 | 24 | 25 | ### Running the parking example 26 | 27 | 1. Start Julia in terminal 28 | 29 | 2. Type in terminal: include("setup.jl") 30 | 31 | 3. Type in terminal: include("main.jl") 32 | 33 | 34 | ### modifying the code 35 | 36 | 1. To play with start points, change x0 in main.jl and run 37 | the code by include("main.jl") 38 | 39 | 2. If you change anything in one of the collision avoidance 40 | problems, you need to activate the changes by running 41 | include("setup.jl") 42 | 43 | 44 | ### Note 45 | 1. This code has been tested on Julia 0.5 and 0.6, and might not run on any other Julia versions. 46 | 47 | 2. For best results, run code in Julia terminal 48 | -------------------------------------------------------------------------------- /AutonomousParking/a_star.jl: -------------------------------------------------------------------------------- 1 | ############### 2 | # OBCA: Optimization-based Collision Avoidance - a path planner for autonomous parking 3 | # Copyright (C) 2018 4 | # Atsushi SAKAI [atsushisakai@global.komatsu; Komatsu Ltd / MPC Lab] 5 | # Alexander LINIGER [liniger@control.ee.ethz.ch; Automatic Control Lab, ETH Zurich] 6 | # Xiaojing ZHANG [xiaojing.zhang@berkeley.edu; MPC Lab, UC Berkeley] 7 | # 8 | # This program is free software: you can redistribute it and/or modify 9 | # it under the terms of the GNU General Public License as published by 10 | # the Free Software Foundation, either version 3 of the License, or 11 | # (at your option) any later version. 12 | # 13 | # This program is distributed in the hope that it will be useful, 14 | # but WITHOUT ANY WARRANTY; without even the implied warranty of 15 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 16 | # GNU General Public License for more details. 17 | # 18 | # You should have received a copy of the GNU General Public License 19 | # along with this program. If not, see . 20 | ############### 21 | # The paper describing the theory can be found here: 22 | # X. Zhang, A. Liniger and F. Borrelli; "Optimization-Based Collision Avoidance"; Technical Report, 2017 23 | ############### 24 | 25 | ############### 26 | # Grid based A* shorest path planning 27 | ############### 28 | 29 | module a_star 30 | 31 | using PyPlot 32 | using NearestNeighbors 33 | using DataStructures 34 | 35 | const VEHICLE_RADIUS = 5.0 #[m] 36 | const GRID_RESOLUTION = 1.0 #[m] 37 | 38 | 39 | type Node 40 | x::Int64 #x index 41 | y::Int64 #y index 42 | cost::Float64 # cost 43 | pind::Int64 # parent index 44 | end 45 | 46 | 47 | function calc_dist_policy(gx::Float64, gy::Float64, 48 | ox::Array{Float64}, oy::Array{Float64}, 49 | reso::Float64, vr::Float64) 50 | """ 51 | gx: goal x position [m] 52 | gx: goal x position [m] 53 | ox: x position list of Obstacles [m] 54 | oy: y position list of Obstacles [m] 55 | reso: grid resolution [m] 56 | vr: vehicle radius[m] 57 | """ 58 | 59 | ngoal = Node(round(Int64, gx/reso),round(Int64, gy/reso),0.0, -1) 60 | 61 | ox = [iox/reso for iox in ox] 62 | oy = [ioy/reso for ioy in oy] 63 | 64 | obmap, minx, miny, maxx, maxy, xw, yw = calc_obstacle_map(ox, oy, reso, vr) 65 | 66 | #open, closed set 67 | open, closed = Dict{Int64, Node}(), Dict{Int64, Node}() 68 | open[calc_index(ngoal, xw, minx, miny)] = ngoal 69 | 70 | motion = get_motion_model() 71 | nmotion = length(motion[:,1]) 72 | pq = PriorityQueue() 73 | enqueue!(pq, calc_index(ngoal, xw, minx, miny), ngoal.cost) 74 | 75 | while true 76 | if length(open) == 0 77 | # println("Finish Search") 78 | break 79 | end 80 | 81 | c_id = dequeue!(pq) 82 | current = open[c_id] 83 | 84 | delete!(open, c_id) 85 | closed[c_id] = current 86 | 87 | for i in 1:nmotion # expand search grid based on motion model 88 | node = Node(current.x+motion[i,1], current.y+motion[i,2], current.cost+motion[i,3], c_id) 89 | 90 | if !verify_node(node, minx, miny, xw, yw, obmap) 91 | continue 92 | end 93 | 94 | node_ind = calc_index(node, xw, minx, miny) 95 | 96 | # If it is already in the closed set, skip it 97 | if haskey(closed,node_ind) continue end 98 | 99 | if haskey(open, node_ind) 100 | if open[node_ind].cost > node.cost 101 | # If so, update the node to have a new parent 102 | open[node_ind].cost = node.cost 103 | open[node_ind].pind = c_id 104 | end 105 | else # add to open set 106 | open[node_ind] = node 107 | enqueue!(pq, calc_index(node, xw, minx, miny), node.cost) 108 | end 109 | end 110 | end 111 | 112 | pmap = calc_policy_map(closed, xw, yw, minx, miny) 113 | 114 | return pmap 115 | end 116 | 117 | 118 | function calc_policy_map(closed, xw, yw, minx, miny) 119 | 120 | pmap = fill(Inf, (xw,yw)) 121 | 122 | for n in values(closed) 123 | pmap[n.x-minx, n.y-miny] = n.cost 124 | end 125 | # println(pmap) 126 | 127 | return pmap 128 | end 129 | 130 | 131 | function calc_astar_path(sx::Float64, sy::Float64, gx::Float64, gy::Float64, 132 | ox::Array{Float64}, oy::Array{Float64}, reso::Float64, vr::Float64) 133 | """ 134 | sx: start x position [m] 135 | sy: start y position [m] 136 | gx: goal x position [m] 137 | gx: goal x position [m] 138 | ox: x position list of Obstacles [m] 139 | oy: y position list of Obstacles [m] 140 | reso: grid resolution [m] 141 | """ 142 | 143 | nstart = Node(round(Int64,sx/reso),round(Int64, sy/reso),0.0, -1) 144 | ngoal = Node(round(Int64, gx/reso),round(Int64, gy/reso),0.0, -1) 145 | 146 | ox = [iox/reso for iox in ox] 147 | oy = [ioy/reso for ioy in oy] 148 | 149 | obmap, minx, miny, maxx, maxy, xw, yw = calc_obstacle_map(ox, oy, reso, vr) 150 | 151 | #open, closed set 152 | open, closed = Dict{Int64, Node}(), Dict{Int64, Node}() 153 | open[calc_index(nstart, xw, minx, miny)] = nstart 154 | 155 | motion = get_motion_model() 156 | nmotion = length(motion[:,1]) 157 | pq = PriorityQueue() 158 | enqueue!(pq, calc_index(nstart, xw, minx, miny), calc_cost(nstart, ngoal)) 159 | 160 | while true 161 | if length(open) == 0 162 | println("Error: No open set") 163 | break 164 | end 165 | 166 | c_id = dequeue!(pq) 167 | current = open[c_id] 168 | 169 | if current.x == ngoal.x && current.y == ngoal.y # check goal 170 | # println("Goal!!") 171 | closed[c_id] = current 172 | break 173 | end 174 | 175 | delete!(open, c_id) 176 | closed[c_id] = current 177 | 178 | for i in 1:nmotion # expand search grid based on motion model 179 | node = Node(current.x+motion[i,1], current.y+motion[i,2], current.cost+motion[i,3], c_id) 180 | 181 | if !verify_node(node, minx, miny, xw, yw, obmap) 182 | continue 183 | end 184 | 185 | node_ind = calc_index(node, xw, minx, miny) 186 | 187 | # If it is already in the closed set, skip it 188 | if haskey(closed,node_ind) continue end 189 | 190 | if haskey(open, node_ind) 191 | if open[node_ind].cost > node.cost 192 | # If so, update the node to have a new parent 193 | open[node_ind].cost = node.cost 194 | open[node_ind].pind = c_id 195 | end 196 | else # add to open set 197 | open[node_ind] = node 198 | enqueue!(pq, calc_index(node, xw, minx, miny), calc_cost(node, ngoal)) 199 | end 200 | end 201 | end 202 | 203 | rx, ry = get_final_path(closed, ngoal, nstart, xw, minx, miny, reso) 204 | 205 | return rx, ry 206 | end 207 | 208 | 209 | function verify_node(node::Node, minx::Int64, miny::Int64, xw::Int64, yw::Int64, obmap::Array{Bool,2}) 210 | 211 | if (node.x - minx) >= xw 212 | return false 213 | elseif (node.x - minx) <= 0 214 | return false 215 | end 216 | if (node.y - miny) >= yw 217 | return false 218 | elseif (node.y - miny) <= 0 219 | return false 220 | end 221 | 222 | #collision check 223 | if obmap[node.x-minx, node.y-miny] 224 | return false 225 | end 226 | 227 | return true 228 | end 229 | 230 | 231 | function calc_cost(n::Node, ngoal::Node) 232 | return (n.cost + h(n.x - ngoal.x, n.y - ngoal.y)) 233 | end 234 | 235 | 236 | function get_motion_model() 237 | # dx, dy, cost 238 | motion=[1 0 1; 239 | 0 1 1; 240 | -1 0 1; 241 | 0 -1 1; 242 | -1 -1 sqrt(2); 243 | -1 1 sqrt(2); 244 | 1 -1 sqrt(2); 245 | 1 1 sqrt(2);] 246 | 247 | return motion 248 | end 249 | 250 | 251 | function calc_index(node::Node, xwidth::Int64, xmin::Int64, ymin::Int64) 252 | return (node.y - ymin)*xwidth + (node.x - xmin) 253 | end 254 | 255 | 256 | function calc_obstacle_map(ox::Array{Float64}, oy::Array{Float64}, reso::Float64, vr::Float64) 257 | 258 | minx = round(Int64, minimum(ox)) 259 | miny = round(Int64, minimum(oy)) 260 | maxx = round(Int64, maximum(ox)) 261 | maxy = round(Int64, maximum(oy)) 262 | 263 | xwidth = round(Int64, maxx - minx) 264 | ywidth = round(Int64, maxy - miny) 265 | 266 | obmap = fill(false, (xwidth,ywidth)) 267 | 268 | kdtree = KDTree(hcat(ox, oy)') 269 | for ix in 1:xwidth 270 | x = ix + minx 271 | for iy in 1:ywidth 272 | y = iy + miny 273 | idxs, onedist = knn(kdtree, [x, y] , 1) 274 | if onedist[1] <= vr/reso 275 | obmap[ix,iy] = true 276 | end 277 | end 278 | end 279 | 280 | return obmap, minx, miny, maxx, maxy, xwidth, ywidth 281 | end 282 | 283 | 284 | function get_final_path(closed::Dict{Int64, Node}, 285 | ngoal::Node, 286 | nstart::Node, 287 | xw::Int64, 288 | minx::Int64, 289 | miny::Int64, 290 | reso::Float64) 291 | 292 | rx, ry = [ngoal.x],[ngoal.y] 293 | nid = calc_index(ngoal, xw, minx, miny) 294 | while true 295 | n = closed[nid] 296 | push!(rx, n.x) 297 | push!(ry, n.y) 298 | nid = n.pind 299 | 300 | if rx[end] == nstart.x && ry[end] == nstart.y 301 | # println("done") 302 | break 303 | end 304 | end 305 | 306 | rx = reverse(rx) .* reso 307 | ry = reverse(ry) .* reso 308 | 309 | return rx, ry 310 | end 311 | 312 | 313 | function search_min_cost_node(open::Dict{Int64, Node}, ngoal::Node) 314 | mnode = nothing 315 | mcost = Inf 316 | for n in values(open) 317 | # println(n) 318 | cost = n.cost + h(n.x - ngoal.x, n.y - ngoal.y) 319 | if mcost > cost 320 | mnode = n 321 | mcost = cost 322 | end 323 | end 324 | # println("minnode:", mnode) 325 | 326 | return mnode 327 | end 328 | 329 | 330 | function h(x::Int64, y::Int64) 331 | """ 332 | Heuristic cost function 333 | """ 334 | return sqrt(x^2+y^2); 335 | end 336 | 337 | 338 | function main() 339 | # println(PROGRAM_FILE," start!!") 340 | 341 | sx = 10.0 # [m] 342 | sy = 10.0 # [m] 343 | gx = 50.0 # [m] 344 | gy = 50.0 # [m] 345 | 346 | ox = Float64[] 347 | oy = Float64[] 348 | 349 | for i in 0:60 350 | push!(ox, Float64(i)) 351 | push!(oy, 0.0) 352 | end 353 | for i in 0:60 354 | push!(ox, 60.0) 355 | push!(oy, Float64(i)) 356 | end 357 | for i in 0:60 358 | push!(ox, Float64(i)) 359 | push!(oy, 60.0) 360 | end 361 | for i in 0:60 362 | push!(ox, 0.0) 363 | push!(oy, Float64(i)) 364 | end 365 | for i in 0:40 366 | push!(ox, 20.0) 367 | push!(oy, Float64(i)) 368 | end 369 | for i in 0:40 370 | push!(ox, 40.0) 371 | push!(oy, 60.0-Float64(i)) 372 | end 373 | 374 | @time rx, ry = calc_astar_path(sx, sy, gx, gy, ox, oy, GRID_RESOLUTION, VEHICLE_RADIUS) 375 | 376 | plot(ox, oy, ".k",label="obstacles") 377 | plot(sx, sy, "xr",label="start") 378 | plot(gx, gy, "xb",label="goal") 379 | plot(rx, ry, "-r",label="A* path") 380 | legend() 381 | grid(true) 382 | axis("equal") 383 | show() 384 | 385 | # println(PROGRAM_FILE," Done!!") 386 | end 387 | 388 | 389 | if length(PROGRAM_FILE)!=0 && 390 | contains(@__FILE__, PROGRAM_FILE) 391 | 392 | main() 393 | end 394 | 395 | 396 | end #module 397 | 398 | -------------------------------------------------------------------------------- /AutonomousParking/collision_check.jl: -------------------------------------------------------------------------------- 1 | ############### 2 | # H-OBCA: Hierarchical Optimization-based Collision Avoidance - a path planner for autonomous parking 3 | # Copyright (C) 2018 4 | # Atsushi SAKAI [atsushisakai@global.komatsu; Komatsu Ltd / MPC Lab] 5 | # Alexander LINIGER [liniger@control.ee.ethz.ch; Automatic Control Lab, ETH Zurich] 6 | # Xiaojing ZHANG [xiaojing.zhang@berkeley.edu; MPC Lab, UC Berkeley] 7 | # 8 | # This program is free software: you can redistribute it and/or modify 9 | # it under the terms of the GNU General Public License as published by 10 | # the Free Software Foundation, either version 3 of the License, or 11 | # (at your option) any later version. 12 | # 13 | # This program is distributed in the hope that it will be useful, 14 | # but WITHOUT ANY WARRANTY; without even the implied warranty of 15 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 16 | # GNU General Public License for more details. 17 | # 18 | # You should have received a copy of the GNU General Public License 19 | # along with this program. If not, see . 20 | ############### 21 | # The paper describing the theory can be found here: 22 | # X. Zhang, A. Liniger and F. Borrelli; "Optimization-Based Collision Avoidance"; Technical Report, 2017, [https://arxiv.org/abs/1711.03449] 23 | # X. Zhang, A. Liniger, A. Sakai and F. Borrelli; "Autonomous Parking using Optimization-Based Collision Avoidance"; Technical Report, 2018 [add URL] 24 | ############### 25 | 26 | module collision_check 27 | 28 | using NearestNeighbors 29 | using PyPlot 30 | 31 | const B = 1.0 #[m] distance from rear to vehicle back end 32 | const C = 3.7 #[m] distance from rear to vehicle front end 33 | const I = 2.0 #[m] width of vehicle 34 | const WBUBBLE_DIST = (B+C)/2.0-B #[m] distance from rear and the center of whole bubble 35 | const WBUBBLE_R = (B+C)/2.0 #[m] whole bubble radius 36 | 37 | const vrx = [C, C, -B, -B, C ] 38 | const vry = [-I/2.0, I/2.0, I/2.0, -I/2.0, -I/2.0] 39 | 40 | function check_collision(x, y, yaw, kdtree, ox, oy) 41 | 42 | for (ix, iy, iyaw) in zip(x, y, yaw) 43 | cx = ix + WBUBBLE_DIST*cos(iyaw) 44 | cy = iy + WBUBBLE_DIST*sin(iyaw) 45 | 46 | # Whole bubble check 47 | ids = inrange(kdtree, [cx, cy], WBUBBLE_R, true) 48 | if length(ids) == 0 continue end 49 | 50 | if !rect_check(ix, iy, iyaw, ox[ids], oy[ids]) 51 | return false #collision 52 | end 53 | end 54 | 55 | return true #OK 56 | 57 | end 58 | 59 | 60 | function rect_check(ix, iy, iyaw, ox, oy) 61 | 62 | c = cos(-iyaw) 63 | s = sin(-iyaw) 64 | 65 | for (iox, ioy) in zip(ox, oy) 66 | tx = iox - ix 67 | ty = ioy - iy 68 | lx = (c*tx - s*ty) 69 | ly = (s*tx + c*ty) 70 | 71 | sumangle = 0.0 72 | for i in 1:length(vrx)-1 73 | x1 = vrx[i] - lx 74 | y1 = vry[i] - ly 75 | x2 = vrx[i+1] - lx 76 | y2 = vry[i+1] - ly 77 | d1 = hypot(x1,y1) 78 | d2 = hypot(x2,y2) 79 | theta1 = atan2(y1,x1) 80 | tty = (-sin(theta1)*x2 + cos(theta1)*y2) 81 | 82 | tmp = (x1*x2+y1*y2)/(d1*d2) 83 | if tmp >= 1.0 tmp = 1.0 end 84 | 85 | if tty >= 0.0 86 | sumangle += acos(tmp) 87 | else 88 | sumangle -= acos(tmp) 89 | end 90 | end 91 | 92 | if sumangle >= pi 93 | return false 94 | end 95 | end 96 | 97 | return true #OK 98 | end 99 | 100 | 101 | function main() 102 | 103 | ox = rand(3)*30.0 - 30.0/2.0 104 | oy = rand(3)*30.0 - 30.0/2.0 105 | 106 | kdtree = KDTree(hcat(ox, oy)') 107 | 108 | x = [10.0, 5.0] 109 | y = [10.0, 5.0] 110 | yaw = [deg2rad(10.0), deg2rad(0.0)] 111 | 112 | flag = check_collision(x, y, yaw, kdtree, ox, oy) 113 | if flag 114 | # println("OK") 115 | else 116 | # println("Collision") 117 | end 118 | 119 | plot(ox, oy, ".r") 120 | grid(true) 121 | axis("equal") 122 | show() 123 | 124 | end 125 | 126 | 127 | if length(PROGRAM_FILE)!=0 && 128 | contains(@__FILE__, PROGRAM_FILE) 129 | 130 | @time main() 131 | end 132 | 133 | 134 | end #module 135 | 136 | 137 | -------------------------------------------------------------------------------- /AutonomousParking/hybrid_a_star.jl: -------------------------------------------------------------------------------- 1 | ############### 2 | # OBCA: Optimization-based Collision Avoidance - a path planner for autonomous parking 3 | # Copyright (C) 2018 4 | # Atsushi SAKAI [atsushisakai@global.komatsu; Komatsu Ltd / MPC Lab] 5 | # Alexander LINIGER [liniger@control.ee.ethz.ch; Automatic Control Lab, ETH Zurich] 6 | # Xiaojing ZHANG [xiaojing.zhang@berkeley.edu; MPC Lab, UC Berkeley] 7 | # 8 | # This program is free software: you can redistribute it and/or modify 9 | # it under the terms of the GNU General Public License as published by 10 | # the Free Software Foundation, either version 3 of the License, or 11 | # (at your option) any later version. 12 | # 13 | # This program is distributed in the hope that it will be useful, 14 | # but WITHOUT ANY WARRANTY; without even the implied warranty of 15 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 16 | # GNU General Public License for more details. 17 | # 18 | # You should have received a copy of the GNU General Public License 19 | # along with this program. If not, see . 20 | ############### 21 | # The paper describing the theory can be found here: 22 | # X. Zhang, A. Liniger and F. Borrelli; "Optimization-Based Collision Avoidance"; Technical Report, 2017 23 | # X. Zhang, A. Liniger, A. Sakai and F. Borrelli; "Autonomous Parking using Optimization-Based Collision Avoidance"; Technical Report, 2018 [add URL] 24 | ############### 25 | 26 | ############### 27 | # Hybrid A star: Julia implementation of Hybrid A* algorithm 28 | ############### 29 | 30 | module hybrid_a_star 31 | 32 | using PyPlot 33 | using DataFrames 34 | using NearestNeighbors 35 | using DataStructures 36 | 37 | include("./reeds_shepp.jl") 38 | include("./a_star.jl") 39 | include("./collision_check.jl") 40 | 41 | 42 | const VEHICLE_RADIUS = 1.0 #[m]; radius of rear ball; 7.0 43 | const BUBBLE_DIST = 1.7 #[m]; distance to "forward bubble"; 7.0 44 | 45 | ##### Fast Comp Time values from Alex Liniger ###### 46 | const OB_MAP_RESOLUTION = 0.1 #[m]; obstacle resolution 47 | const YAW_GRID_RESOLUTION = deg2rad(5.0) #[m]; 10.0 /// 5.0 48 | const N_STEER = 5.0 # number of steer command; 10.0 seems OK /// 5 49 | ## For Backwards Parking 50 | # const XY_GRID_RESOLUTION = 1. #[m]; 51 | # const MOTION_RESOLUTION = 0.3 #[m]; 52 | ## For Parallel Parking 53 | const XY_GRID_RESOLUTION = 0.3 #[m]; 54 | const MOTION_RESOLUTION = 0.1 #[m]; 55 | ################################################### 56 | 57 | const USE_HOLONOMIC_WITH_OBSTACLE_HEURISTIC = true 58 | const USE_NONHOLONOMIC_WITHOUT_OBSTACLE_HEURISTIC = false 59 | 60 | const SB_COST = 10.0 # switch back penalty cost 61 | const BACK_COST = 0.0 # backward penalty cost 62 | const STEER_CHANGE_COST = 10.0 # steer angle change penalty cost 63 | const STEER_COST = 0.0 # steer angle penalty cost 64 | const H_COST = 1. # Heuristic cost; higher -> heuristic; 1.0 65 | 66 | const WB = 2.7 #[m]; 7.0 67 | const MAX_STEER = 0.6#deg2rad(35.0) #[rad] 68 | 69 | type Node 70 | xind::Int64 #x index 71 | yind::Int64 #y index 72 | yawind::Int64 #yaw index 73 | direction::Bool # moving direction forword:true, backword:false 74 | x::Array{Float64} # x position [m] 75 | y::Array{Float64} # y position [m] 76 | yaw::Array{Float64} # yaw angle [rad] 77 | steer::Float64 # steer input 78 | cost::Float64 # cost 79 | pind::Int64 # parent index 80 | end 81 | 82 | type Config 83 | minx::Int64 84 | miny::Int64 85 | minyaw::Int64 86 | maxx::Int64 87 | maxy::Int64 88 | maxyaw::Int64 89 | xw::Int64 90 | yw::Int64 91 | yaww::Int64 92 | xyreso::Float64 93 | yawreso::Float64 94 | obminx::Int64 95 | obminy::Int64 96 | obmaxx::Int64 97 | obmaxy::Int64 98 | obxw::Int64 99 | obyw::Int64 100 | obreso::Float64 101 | end 102 | 103 | 104 | function calc_hybrid_astar_path(sx::Float64, sy::Float64, syaw::Float64, 105 | gx::Float64, gy::Float64, gyaw::Float64, 106 | ox::Array{Float64}, oy::Array{Float64}, 107 | xyreso::Float64, yawreso::Float64, 108 | obreso::Float64) 109 | """ 110 | Calc hybrid astar path 111 | sx: start x position [m] 112 | sy: start y position [m] 113 | gx: goal x position [m] 114 | gx: goal x position [m] 115 | ox: x position list of Obstacles [m] 116 | oy: y position list of Obstacles [m] 117 | xyreso: grid resolution [m] 118 | yawreso: yaw angle resolution [rad] 119 | """ 120 | 121 | syaw, gyaw = pi_2_pi(syaw), pi_2_pi(gyaw) 122 | 123 | const c = calc_config(ox, oy, xyreso, yawreso, obreso) 124 | kdtree = KDTree(hcat(ox, oy)') 125 | obmap, gkdtree = calc_obstacle_map(ox, oy, c) 126 | nstart = Node(round(Int64,sx/xyreso), round(Int64,sy/xyreso), round(Int64, syaw/yawreso),true,[sx],[sy],[syaw],0.0,0.0, -1) 127 | ngoal = Node(round(Int64,gx/xyreso), round(Int64,gy/xyreso), round(Int64,gyaw/yawreso),true,[gx],[gy],[gyaw],0.0,0.0, -1) 128 | 129 | if USE_HOLONOMIC_WITH_OBSTACLE_HEURISTIC 130 | h_dp = calc_holonomic_with_obstacle_heuristic(ngoal, ox, oy, xyreso) 131 | else 132 | h_dp = Array{Float64}() 133 | end 134 | if USE_NONHOLONOMIC_WITHOUT_OBSTACLE_HEURISTIC 135 | h_rs = calc_nonholonomic_without_obstacle_heuristic(ngoal, c) 136 | else 137 | h_rs = Array{Float64}() 138 | end 139 | 140 | open, closed = Dict{Int64, Node}(), Dict{Int64, Node}() 141 | open[calc_index(nstart, c)] = nstart 142 | pq = PriorityQueue() 143 | enqueue!(pq, calc_index(nstart, c), calc_cost(nstart, h_rs, h_dp, ngoal, c)) 144 | 145 | u, d = calc_motion_inputs() 146 | nmotion = length(u) 147 | 148 | while true 149 | if length(open) == 0 150 | println("Error: Cannot find path, No open set") 151 | return nothing, nothing, nothing 152 | end 153 | 154 | c_id = dequeue!(pq) 155 | current = open[c_id] 156 | 157 | isupdated, current = update_node_with_analystic_expantion(current, ngoal, obmap, c, kdtree, ox, oy) 158 | if isupdated 159 | closed[calc_index(ngoal, c)] = current 160 | break #goal 161 | end 162 | 163 | #move current node from open to closed 164 | delete!(open, c_id) 165 | closed[c_id] = current 166 | 167 | for i in 1:nmotion 168 | node = calc_next_node(current, c_id, u[i], d[i], c, gkdtree) 169 | 170 | if !verify_index(node, obmap, c, kdtree, ox, oy) continue end 171 | 172 | node_ind = calc_index(node, c) 173 | 174 | # If it is already in the closed set, skip it 175 | if haskey(closed, node_ind) continue end 176 | 177 | if !haskey(open, node_ind) 178 | open[node_ind] = node 179 | enqueue!(pq, calc_index(node, c), calc_cost(node, h_rs, h_dp, ngoal, c)) 180 | end 181 | end 182 | 183 | end 184 | 185 | # println("final expand node:", length(open) + length(closed)) 186 | 187 | rx, ry, ryaw = get_final_path(closed, ngoal, nstart, c) 188 | 189 | return rx, ry, ryaw 190 | end 191 | 192 | 193 | function update_node_with_analystic_expantion(current::Node, 194 | ngoal::Node, 195 | obmap::Array{Bool,2}, 196 | c::Config, 197 | kdtree::NearestNeighbors.KDTree, 198 | ox::Array{Float64}, 199 | oy::Array{Float64} 200 | ) 201 | 202 | apath = analystic_expantion(current, ngoal, obmap, c, kdtree, ox, oy) 203 | if apath != nothing 204 | # println("Find path! with analystic_expantion") 205 | current.x = vcat(current.x, apath.x[2:end-1]) 206 | current.y = vcat(current.y, apath.y[2:end-1]) 207 | current.yaw = vcat(current.yaw, apath.yaw[2:end-1]) 208 | current.cost += calc_rs_path_cost(apath) 209 | return true, current 210 | end 211 | 212 | return false, current #no update 213 | end 214 | 215 | 216 | function calc_rs_path_cost(rspath::hybrid_a_star.reeds_shepp.Path) 217 | 218 | cost = 0.0 219 | for l in rspath.lengths 220 | if l >= 0 # forward 221 | cost += l 222 | else # back 223 | cost += abs(l) * BACK_COST 224 | end 225 | end 226 | 227 | # swich back penalty 228 | for i in 1:length(rspath.lengths) - 1 229 | if rspath.lengths[i] * rspath.lengths[i+1] < 0.0 # switch back 230 | cost += SB_COST 231 | end 232 | end 233 | 234 | # steer penalyty 235 | for ctype in rspath.ctypes 236 | if ctype != "S" # curve 237 | cost += STEER_COST*abs(MAX_STEER) 238 | end 239 | end 240 | 241 | # ==steer change penalty 242 | # calc steer profile 243 | nctypes = length(rspath.ctypes) 244 | ulist = fill(0.0, nctypes) 245 | for i in 1:nctypes 246 | if rspath.ctypes[i] == "R" 247 | ulist[i] = - MAX_STEER 248 | elseif rspath.ctypes[i] == "L" 249 | ulist[i] = MAX_STEER 250 | end 251 | end 252 | 253 | for i in 1:length(rspath.ctypes) - 1 254 | cost += STEER_CHANGE_COST*abs(ulist[i+1] - ulist[i]) 255 | end 256 | 257 | # println("RS cost is ", cost) 258 | return cost 259 | end 260 | 261 | 262 | function analystic_expantion(n::Node, ngoal::Node, obmap::Array{Bool,2}, c::Config, 263 | kdtree::NearestNeighbors.KDTree, 264 | ox::Array{Float64}, 265 | oy::Array{Float64} 266 | ) 267 | 268 | sx = n.x[end] 269 | sy = n.y[end] 270 | syaw = n.yaw[end] 271 | 272 | max_curvature = tan(MAX_STEER)/WB 273 | path = reeds_shepp.calc_shortest_path(sx, sy, syaw, 274 | ngoal.x[end], ngoal.y[end], ngoal.yaw[end], 275 | max_curvature, step_size=MOTION_RESOLUTION) 276 | 277 | if path == nothing 278 | return nothing 279 | end 280 | 281 | if !collision_check.check_collision(path.x, path.y, path.yaw, kdtree, ox, oy) 282 | return nothing 283 | end 284 | 285 | # println(paths) 286 | return path # find good path 287 | end 288 | 289 | 290 | function calc_motion_inputs() 291 | 292 | up = [i for i in MAX_STEER/N_STEER:MAX_STEER/N_STEER:MAX_STEER] 293 | u = vcat([0.0], [i for i in up], [-i for i in up]) 294 | d = vcat([1.0 for i in 1:length(u)], [-1.0 for i in 1:length(u)]) 295 | u = vcat(u,u) 296 | 297 | return u, d 298 | end 299 | 300 | 301 | function verify_index(node::Node, obmap::Array{Bool,2}, c::Config, 302 | kdtree::NearestNeighbors.KDTree, 303 | ox::Array{Float64}, 304 | oy::Array{Float64} 305 | )::Bool 306 | 307 | # overflow map 308 | if (node.xind - c.minx) >= c.xw 309 | return false 310 | elseif (node.xind - c.minx) <= 0 311 | return false 312 | end 313 | if (node.yind - c.miny) >= c.yw 314 | return false 315 | elseif (node.yind - c.miny) <= 0 316 | return false 317 | end 318 | 319 | # check collisiton 320 | # rectangle check 321 | if !collision_check.check_collision(node.x, node.y,node.yaw, kdtree, ox, oy) 322 | return false 323 | end 324 | 325 | return true #index is ok" 326 | end 327 | 328 | 329 | function pi_2_pi(iangle::Float64) 330 | while (iangle > pi) 331 | iangle -= 2.0 * pi 332 | end 333 | while (iangle < -pi) 334 | iangle += 2.0 * pi 335 | end 336 | 337 | return iangle 338 | end 339 | 340 | 341 | function calc_next_node(current::Node, c_id::Int64, 342 | u::Float64, d::Float64, 343 | c::Config, 344 | gkdtree::NearestNeighbors.KDTree) 345 | 346 | 347 | arc_l = XY_GRID_RESOLUTION 348 | 349 | nlist = round(Int64, arc_l/MOTION_RESOLUTION)+1 350 | xlist = fill(0.0, nlist) 351 | ylist = fill(0.0, nlist) 352 | yawlist = fill(0.0, nlist) 353 | xlist[1] = current.x[end] + d * MOTION_RESOLUTION*cos(current.yaw[end]) 354 | ylist[1] = current.y[end] + d * MOTION_RESOLUTION*sin(current.yaw[end]) 355 | yawlist[1] = pi_2_pi(current.yaw[end] + d*MOTION_RESOLUTION/WB * tan(u)) 356 | 357 | 358 | for i in 1:(nlist - 1) 359 | xlist[i+1] = xlist[i] + d * MOTION_RESOLUTION*cos(yawlist[i]) 360 | ylist[i+1] = ylist[i] + d * MOTION_RESOLUTION*sin(yawlist[i]) 361 | yawlist[i+1] = pi_2_pi(yawlist[i] + d*MOTION_RESOLUTION/WB * tan(u)) 362 | end 363 | 364 | xind = round(Int64, xlist[end]/c.xyreso) 365 | yind = round(Int64, ylist[end]/c.xyreso) 366 | yawind = round(Int64, yawlist[end]/c.yawreso) 367 | 368 | addedcost = 0.0 369 | if d > 0 370 | direction = true 371 | addedcost += abs(arc_l) 372 | else 373 | direction = false 374 | addedcost += abs(arc_l) * BACK_COST 375 | end 376 | 377 | # swich back penalty 378 | if direction != current.direction # switch back penalty 379 | addedcost += SB_COST 380 | end 381 | 382 | # steer penalyty 383 | addedcost += STEER_COST*abs(u) 384 | 385 | # steer change penalty 386 | addedcost += STEER_CHANGE_COST*abs(current.steer - u) 387 | 388 | cost = current.cost + addedcost 389 | node = Node(xind, yind, yawind, direction, xlist, ylist, yawlist, u, cost, c_id) 390 | # println(node) 391 | 392 | return node 393 | end 394 | 395 | 396 | function is_same_grid(node1::Node,node2::Node) 397 | 398 | if node1.xind != node2.xind 399 | return false 400 | end 401 | if node1.yind != node2.yind 402 | return false 403 | end 404 | if node1.yawind != node2.yawind 405 | return false 406 | end 407 | 408 | return true 409 | 410 | end 411 | 412 | 413 | function calc_index(node::Node, c::Config) 414 | ind = (node.yawind - c.minyaw)*c.xw*c.yw+(node.yind - c.miny)*c.xw + (node.xind - c.minx) 415 | if ind <= 0 416 | println("Error(calc_index):", ind) 417 | end 418 | return ind 419 | end 420 | 421 | 422 | function calc_holonomic_with_obstacle_heuristic(gnode::Node, ox::Array{Float64}, oy::Array{Float64}, xyreso::Float64) 423 | # println("Calc distance policy") 424 | h_dp = a_star.calc_dist_policy(gnode.x[end], gnode.y[end], ox, oy, xyreso, VEHICLE_RADIUS) 425 | return h_dp 426 | end 427 | 428 | 429 | function calc_nonholonomic_without_obstacle_heuristic(ngoal::Node, 430 | c::Config) 431 | 432 | h_rs = fill(0.0, (c.xw,c.yw,c.yaww)) 433 | max_curvature = tan(MAX_STEER)/WB 434 | 435 | for ix in 1:c.xw 436 | for iy in 1:c.yw 437 | for iyaw in 1:c.yaww 438 | sx = (ix + c.minx)*c.xyreso 439 | sy = (iy + c.miny)*c.xyreso 440 | syaw = pi_2_pi((iyaw + c.minyaw)*c.yawreso) 441 | L = reeds_shepp.calc_shortest_path_length(sx, sy, syaw, 442 | ngoal.x[end], ngoal.y[end], ngoal.yaw[end], 443 | max_curvature, step_size=MOTION_RESOLUTION) 444 | h_rs[ix, iy, iyaw] = L 445 | end 446 | end 447 | end 448 | 449 | # println(h_rs[:,:,1]) 450 | 451 | return h_rs 452 | end 453 | 454 | 455 | function calc_config(ox::Array{Float64}, oy::Array{Float64}, xyreso::Float64, yawreso::Float64, obreso::Float64) 456 | minx = round(Int64, minimum(ox)/xyreso) 457 | miny = round(Int64, minimum(oy)/xyreso) 458 | maxx = round(Int64, maximum(ox)/xyreso) 459 | maxy = round(Int64, maximum(oy)/xyreso) 460 | obminx = round(Int64, minimum(ox)/obreso) 461 | obminy = round(Int64, minimum(oy)/obreso) 462 | obmaxx = round(Int64, maximum(ox)/obreso) 463 | obmaxy = round(Int64, maximum(oy)/obreso) 464 | 465 | xw = round(Int64,(maxx - minx)) 466 | yw = round(Int64,(maxy - miny)) 467 | obxw = round(Int64,(obmaxx - obminx)) 468 | obyw = round(Int64,(obmaxy - obminy)) 469 | 470 | minyaw = round(Int64, - pi/yawreso) - 1 471 | maxyaw = round(Int64, pi/yawreso) 472 | yaww = round(Int64,(maxyaw - minyaw)) 473 | 474 | config = Config(minx, miny, minyaw, maxx, maxy, maxyaw, xw, yw, yaww, 475 | xyreso, yawreso, obminx, obminy, obmaxx, obmaxy, obxw, obyw, obreso) 476 | 477 | return config 478 | end 479 | 480 | 481 | function calc_obstacle_map(ox::Array{Float64}, 482 | oy::Array{Float64}, 483 | c::Config) 484 | 485 | ox = [iox/c.obreso for iox in ox] 486 | oy = [ioy/c.obreso for ioy in oy] 487 | 488 | obmap = fill(false, (c.obxw, c.obyw)) 489 | 490 | gkdtree = KDTree(hcat(ox, oy)') 491 | for ix in 1:c.obxw 492 | x = ix + c.obminx 493 | for iy in 1:c.obyw 494 | y = iy + c.obminy 495 | idxs, onedist = knn(gkdtree, [x, y] , 1) 496 | if onedist[1] <= VEHICLE_RADIUS/c.obreso 497 | obmap[ix,iy] = true 498 | end 499 | end 500 | end 501 | 502 | return obmap, gkdtree 503 | end 504 | 505 | 506 | function get_final_path(closed::Dict{Int64, Node}, 507 | ngoal::Node, 508 | nstart::Node, 509 | c::Config) 510 | 511 | rx, ry, ryaw = Array{Float64}(ngoal.x),Array{Float64}(ngoal.y),Array{Float64}(ngoal.yaw) 512 | nid = calc_index(ngoal, c) 513 | # println("Fianl cost is ", closed[nid].cost) 514 | while true 515 | n = closed[nid] 516 | rx = vcat(rx, reverse(n.x)) 517 | ry = vcat(ry, reverse(n.y)) 518 | ryaw = vcat(ryaw, reverse(n.yaw)) 519 | nid = n.pind 520 | if is_same_grid(n, nstart) 521 | # println("done") 522 | break 523 | end 524 | end 525 | 526 | rx = reverse(rx) 527 | ry = reverse(ry) 528 | ryaw = reverse(ryaw) 529 | 530 | dist = sum([sqrt(idx^2+idy^2) for (idx,idy) in zip(diff(rx), diff(ry))]) 531 | # println("Fianl path distance is ", dist) 532 | 533 | return rx, ry, ryaw 534 | end 535 | 536 | 537 | function calc_cost(n::Node, h_rs::Array{Float64}, h_dp::Array{Float64}, ngoal::Node, c::Config) 538 | 539 | if length(h_rs) > 1 && length(h_dp) > 1 # Both heuristic cost are activated 540 | c_h_dp = h_dp[n.xind - c.minx, n.yind - c.miny] 541 | c_h_rs = h_rs[n.xind - c.minx, n.yind - c.miny, n.yawind - c.minyaw] 542 | return (n.cost + H_COST*max(c_h_dp, c_h_rs)) 543 | elseif length(h_dp) > 1 # Distance policy heuristics is activated 544 | return (n.cost + H_COST*h_dp[n.xind - c.minx, n.yind - c.miny]) 545 | elseif length(h_rs) > 1 # Reed Sheep path heuristics is activated 546 | return (n.cost + H_COST*h_rs[n.xind - c.minx, n.yind - c.miny, n.yawind - c.minyaw]) 547 | end 548 | 549 | return (n.cost + H_COST*calc_euclid_dist(n.x[end] - ngoal.x[end],n.y[end] - ngoal.y[end], n.yaw[end] - ngoal.yaw[end])) 550 | end 551 | 552 | 553 | function calc_euclid_dist(x::Float64, y::Float64, yaw::Float64) 554 | """ 555 | Heuristic cost function 556 | """ 557 | if yaw >= pi 558 | yaw -= pi 559 | else yaw <= -pi 560 | yaw += pi 561 | end 562 | return sqrt(x^2+y^2+yaw^2) 563 | end 564 | 565 | 566 | function main() 567 | # println(PROGRAM_FILE," start!!") 568 | 569 | sx = 20.0 # [m] 570 | sy = 20.0 # [m] 571 | syaw = deg2rad(90.0) 572 | gx = 180.0 # [m] 573 | gy = 100.0 # [m] 574 | gyaw = deg2rad(-90.0) 575 | 576 | ox = Float64[] 577 | oy = Float64[] 578 | 579 | for i in 0:200 580 | push!(ox, Float64(i)) 581 | push!(oy, 0.0) 582 | end 583 | for i in 0:120 584 | push!(ox, 200.0) 585 | push!(oy, Float64(i)) 586 | end 587 | for i in 0:200 588 | push!(ox, Float64(i)) 589 | push!(oy, 120.0) 590 | end 591 | for i in 0:120 592 | push!(ox, 0.0) 593 | push!(oy, Float64(i)) 594 | end 595 | for i in 0:80 596 | push!(ox, 40.0) 597 | push!(oy, Float64(i)) 598 | end 599 | for i in 0:80 600 | push!(ox, 80.0) 601 | push!(oy, 120.0-Float64(i)) 602 | end 603 | for i in 0:40 604 | push!(ox, 120.0) 605 | push!(oy, 120.0-Float64(i)) 606 | push!(ox, 120.0) 607 | push!(oy, Float64(i)) 608 | end 609 | for i in 0:80 610 | push!(ox, 160.0) 611 | push!(oy, 120.0-Float64(i)) 612 | end 613 | 614 | @time rx, ry, ryaw = calc_hybrid_astar_path(sx, sy, syaw, gx, gy, gyaw, ox, oy, XY_GRID_RESOLUTION, YAW_GRID_RESOLUTION, OB_MAP_RESOLUTION) 615 | 616 | plot(ox, oy, ".k",label="obstacles") 617 | if rx != nothing 618 | plot(rx, ry, "-r",label="Hybrid A* path") 619 | end 620 | 621 | legend() 622 | grid(true) 623 | axis("equal") 624 | 625 | show() 626 | 627 | # println(PROGRAM_FILE," Done!!") 628 | end 629 | 630 | 631 | if length(PROGRAM_FILE)!=0 && 632 | contains(@__FILE__, PROGRAM_FILE) 633 | 634 | main() 635 | end 636 | 637 | 638 | end #module 639 | 640 | -------------------------------------------------------------------------------- /AutonomousParking/main.jl: -------------------------------------------------------------------------------- 1 | ############### 2 | # OBCA: Optimization-based Collision Avoidance - a path planner for autonomous parking 3 | # Copyright (C) 2018 4 | # Alexander LINIGER [liniger@control.ee.ethz.ch; Automatic Control Lab, ETH Zurich] 5 | # Xiaojing ZHANG [xiaojing.zhang@berkeley.edu; MPC Lab, UC Berkeley] 6 | # 7 | # This program is free software: you can redistribute it and/or modify 8 | # it under the terms of the GNU General Public License as published by 9 | # the Free Software Foundation, either version 3 of the License, or 10 | # (at your option) any later version. 11 | # 12 | # This program is distributed in the hope that it will be useful, 13 | # but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 | # GNU General Public License for more details. 16 | # 17 | # You should have received a copy of the GNU General Public License 18 | # along with this program. If not, see . 19 | ############### 20 | # The paper describing the theory can be found here: 21 | # X. Zhang, A. Liniger and F. Borrelli; "Optimization-Based Collision Avoidance"; Technical Report, 2017, [https://arxiv.org/abs/1711.03449] 22 | ############### 23 | 24 | ############### 25 | # Main file: computes Collision-Free and Minimum-Penetration trajectories for parking 26 | ############### 27 | 28 | # function defined in setup.jl 29 | clear() 30 | using PyCall 31 | close("all") 32 | 33 | ################################################## 34 | 35 | # choose one of two predefined scenarios 36 | scenario = "parallel" 37 | scenario = "backwards" 38 | 39 | # fixed or variable time 1/0 40 | fixTime = 0 # default: 0 (variable time steps) 41 | 42 | #### problem parameters #### 43 | TsPF = 0.05 44 | if scenario == "backwards" 45 | # nominal sampling time 46 | sampleN = 3 47 | if fixTime == 1 48 | Ts = 0.55/3*sampleN # 0.55/3 must be compatible with motion resolution of Hybrid A* algorithm 49 | else 50 | Ts = 0.6/3*sampleN # 0.6/3 must be compatible with motion resolution of Hybrid A* algorithm 51 | end 52 | else 53 | sampleN = 3 54 | if fixTime == 1 55 | Ts = 0.95/3*sampleN # 0.95/3 must be compatible with motion resolution of Hybrid A* algorithm 56 | else 57 | Ts = 0.9/3*sampleN # 0.9/3 must be compatible with motion resolution of Hybrid A* algorithm 58 | end 59 | end 60 | 61 | 62 | #wheelbase 63 | L = 2.7 64 | 65 | # step length of Hybrid A*", 66 | motionStep = 0.1 67 | 68 | 69 | # "nominal" shape of ego/controlled car, ego object is later rotated around the car center 70 | # center of rear wheel axis is reference point 71 | # size of car is: (x_upper + x_lower) + (y_upper + y_lower) 72 | # [x_upper, y_upper, -x_lower, -y_lower ] 73 | ego = [ 3.7 , 1 , 1 , 1 ] 74 | 75 | 76 | if scenario == "backwards" 77 | println("Backwards Parking") 78 | elseif scenario == "parallel" 79 | println("Parallel Parking") 80 | else 81 | println("ERROR: please specify parking scenario") 82 | end 83 | 84 | 85 | if scenario == "backwards" 86 | ##### define obstacles; for simplicity, only polyhedral obstacles are supported at this point 87 | # obstacles are defined by vertices, which are assumed to be enumerated in clock-wise direction 88 | # [ [[obst1_x1;obst1_y1],[obst1_x2;obst1_y2],[obst1_x3;obst1_y4],...,[obst1_x1;obst1_y1]] , [[obst2_x1;obst2_y1],[obst2_x2;obst2_y2],[obst2_x3;obst2_y4],...,[obst2_x1;obst2_y1]] , ... ] 89 | 90 | # obstacles for plotting 91 | nObPlot = 3 # number of obstacles 92 | vObPlot = [4 4 4] # number of vertices of each obstacle, vector of dimenion nOb 93 | # obstacles for plotting 94 | lObPlot = [ [ [-20;5], [-1.3;5], [-1.3;-5], [-20;-5], [-20;5] ] , 95 | [ [1.3;5], [20;5], [20;-5], [1.3;-5], [1.3;5] ] , 96 | [ [-20;15], [20;15], [20;11], [-20,11], [-20;15] ] ] #vetices given in CLOCK-WISE direction 97 | 98 | # obstacles for optimization problem 99 | nOb = 3 # number of obstacles 100 | vOb = [3 3 2] # number of vertices of each obstacle, vector of dimenion nOb 101 | vObMPC = vOb-1 102 | lOb = [ [ [-20;5], [-1.3;5], [-1.3;-5]] , 103 | [ [1.3;-5] , [1.3;5] , [20;5] ] , 104 | [ [20;11], [-20;11]] ] #vetices given in CLOCK-WISE direction 105 | 106 | 107 | # final state 108 | xF = [ 0 1.3 pi/2 0] 109 | 110 | 111 | # build obstacles for Hybrid A* algorithm 112 | ox = Float64[] 113 | oy = Float64[] 114 | # obstacle 1 115 | for i = -12:0.1:-1.3 116 | push!(ox, Float64(i)) 117 | push!(oy, 5.0) 118 | end 119 | for i in -2:5 120 | push!(ox, -1.3) 121 | push!(oy, Float64(i)) 122 | end 123 | # obstacle 2 124 | for i in -2:5 125 | push!(ox, 1.3) 126 | push!(oy, Float64(i)) 127 | end 128 | for i = 1.3:0.1:12 129 | push!(ox, Float64(i)) 130 | push!(oy, 5.0) 131 | end 132 | # obstacle 3 133 | for i = -12:12 134 | push!(ox, Float64(i)) 135 | push!(oy, 11.0) 136 | end 137 | 138 | elseif scenario == "parallel" 139 | ##### define obstacles; for simplicity, only polyhedral obstacles are supported at this point 140 | # obstacles are defined by vertices, which are assumed to be enumerated in clock-wise direction 141 | # define obstacles for plotting 142 | nObPlot = 4 # number of obstacles 143 | vObPlot = [4 4 4 4] # number of vertices of each obstacle, vector of dimenion nOb 144 | # [ [[obst1_x1;obst1_y1],[obst1_x2;obst1_y2],[obst1_x3;obst1_y4],...,[obst1_x1;obst1_y1]] , [[obst2_x1;obst2_y1],[obst2_x2;obst2_y2],[obst2_x3;obst2_y4],...,[obst2_x1;obst2_y1]] , ... ] 145 | lObPlot = [ [ [-15;5], [-3;5], [-3;0], [-15;0], [-15;5] ] , 146 | [ [3;5], [15;5], [15;0], [3;0], [3;5] ] , 147 | [ [-3;0], [-3;2.5], [3;2.5], [3,0], [-3;0] ] , 148 | [ [-15;15], [15;15], [15;11], [-15,11], [-15;15] ] ] 149 | 150 | # define obstacles for optimization problem 151 | nOb = 4 # number of obstacles 152 | vOb = [3 3 2 2] # number of vertices of each obstacle, vector of dimenion nOb 153 | vObMPC = vOb-1 154 | lOb = [ [ [-20;5], [-3.;5], [-3.;0]] , 155 | [ [3.;0] , [3.;5] , [20;5] ] , 156 | [ [-3;2.5], [ 3;2.5]] , 157 | [ [ 20;11 ], [-20;11]] ] #vetices given in CLOCK-WISE direction 158 | 159 | # [ [ 3;11 ], [-3;11]] 160 | 161 | # final state 162 | xF = [-L/2 4 0 0] 163 | 164 | # range of initial points 165 | x0X_range = -9 : 1 : 9 # 19 points 166 | x0X_range = -10 : 1 : 10 # 21 points 167 | x0Y_range = 6.5 : 1.5 : 9.5 # 3 168 | x0Y_range = 6.5 : 1 : 9.5 # 3 169 | 170 | 171 | 172 | ox = Float64[] 173 | oy = Float64[] 174 | 175 | # obstacles for Hybrid A* algorithms 176 | # obstacle 1 177 | for i in -12:0.1: -3. 178 | push!(ox,Float64(i)) 179 | push!(oy,5.0) 180 | end 181 | 182 | for i in -2 : 5 183 | push!(ox,-3.0) 184 | push!(oy,Float64(i)) 185 | end 186 | # obstacle 2 187 | for i in -3 : 3 188 | push!(ox,Float64(i)) 189 | push!(oy,2.5) 190 | end 191 | # obstacle 3 192 | for i in -2 : 5 193 | push!(ox,3.0) 194 | push!(oy,Float64(i)) 195 | end 196 | 197 | for i in 3 :0.1: 12 198 | push!(ox,Float64(i)) 199 | push!(oy,5.0) 200 | end 201 | # obstacle 4 202 | for i in -12 : 12 203 | push!(ox,Float64(i)) 204 | push!(oy,11.5) # 11.0 205 | end 206 | end 207 | 208 | 209 | # [x_lower, x_upper, -y_lower, y_upper ] 210 | XYbounds = [ -15 , 15 , 1 , 10 ] # constraints are on (X,Y) 211 | 212 | # set initial state 213 | x0 = [-6 9.5 0.0 0.0] 214 | 215 | # call Hybrid A* 216 | tic() 217 | rx, ry, ryaw = hybrid_a_star.calc_hybrid_astar_path(x0[1], x0[2], x0[3], xF[1], xF[2], xF[3], ox, oy, hybrid_a_star.XY_GRID_RESOLUTION, hybrid_a_star.YAW_GRID_RESOLUTION, hybrid_a_star.OB_MAP_RESOLUTION) 218 | timeHybAstar = toq(); 219 | 220 | 221 | ### extract (smooth) velocity profile from Hybrid A* solution #### 222 | rv = zeros(length(rx),1) 223 | for i=1:length(rx) 224 | if i < length(rx) 225 | rv[i] = (rx[i+1] - rx[i])/(Ts/sampleN)*cos(ryaw[i]) + (ry[i+1]-ry[i])/(Ts/sampleN)*sin(ryaw[i]) 226 | else 227 | rv[i] = 0 228 | end 229 | end 230 | ### Smoothen velocity 0.3 m/s^2 max acceleration ### 231 | v,a = veloSmooth(rv,0.3,Ts/sampleN) 232 | ### compute steering angle ### 233 | delta = atan(diff(ryaw)*L/motionStep.*sign(v[1:end-1])); 234 | 235 | 236 | ### Down-sample for Warmstart ########## 237 | rx_sampled = rx[1:sampleN:end] # : 5 238 | ry_sampled = ry[1:sampleN:end] 239 | ryaw_sampled = ryaw[1:sampleN:end] 240 | rv_sampled = rv[1:sampleN:end] 241 | v_sampled = v[1:sampleN:end] 242 | 243 | a_sampled = a[1:sampleN:end] 244 | delta_sampled = delta[1:sampleN:end] 245 | 246 | ## initialize warm start solution 247 | xWS = [rx_sampled ry_sampled ryaw_sampled v_sampled] 248 | uWS = [delta_sampled a_sampled] 249 | 250 | ### prepare for OBCA ### 251 | N = length(rx_sampled)-1 252 | AOb, bOb = obstHrep(nOb, vOb, lOb) 253 | 254 | 255 | ###### park using Distance Approach ###### 256 | println("Parking using Distance Approach (A* warm start)") 257 | # believe it's correct; in "ParkingDist1.jl" 258 | xp20, up20, scaleTime20, exitflag20, time20, lp20, np20 = ParkingDist(x0,xF,N,Ts,L,ego,XYbounds,nOb,vObMPC,AOb,bOb,rx_sampled,ry_sampled,ryaw_sampled,fixTime,xWS,uWS) 259 | if exitflag20==1 260 | println(" --> Distance: SUCCESSFUL.") 261 | plotTraj(xp20',up20',length(rx_sampled)-1,ego,L,nObPlot,vObPlot,lObPlot,"Distance Approach (Collision Avoidance )",2) 262 | else 263 | println(" --> WARNING: Problem could not be solved.") 264 | end 265 | 266 | 267 | ###### park using Signed Distance Approach ###### 268 | println("Parking using Signed Distance Approach (A* warm start)") 269 | xp10, up10, scaleTime10, exitflag10, time10, lp10, np10 = ParkingSignedDist(x0,xF,N,Ts,L,ego,XYbounds,nOb,vObMPC,AOb,bOb,rx_sampled,ry_sampled,ryaw_sampled,fixTime,xWS,uWS) 270 | if exitflag10==1 271 | println(" --> Signed Distance: SUCCESSFUL.") 272 | plotTraj(xp10',up10',length(rx_sampled)-1,ego,L,nObPlot,vObPlot,lObPlot,"Signed Distance Approach (Min. Penetration)",1) 273 | 274 | else 275 | println(" --> WARNING: Problem could not be solved.") 276 | end 277 | 278 | 279 | 280 | println("********************* summary *********************") 281 | println(" Time Hybrid A*: ", timeHybAstar, " s") 282 | println(" Time Distance approach: ", time20, " s") 283 | println(" Time Signed Distance approach: ", time10, " s") 284 | 285 | println("********************* DONE *********************") 286 | 287 | 288 | 289 | -------------------------------------------------------------------------------- /AutonomousParking/obstHrep.jl: -------------------------------------------------------------------------------- 1 | ############### 2 | # OBCA: Optimization-based Collision Avoidance - a path planner for autonomous parking 3 | # Copyright (C) 2018 4 | # Alexander LINIGER [liniger@control.ee.ethz.ch; Automatic Control Lab, ETH Zurich] 5 | # Xiaojing ZHANG [xiaojing.zhang@berkeley.edu; MPC Lab, UC Berkeley] 6 | # 7 | # This program is free software: you can redistribute it and/or modify 8 | # it under the terms of the GNU General Public License as published by 9 | # the Free Software Foundation, either version 3 of the License, or 10 | # (at your option) any later version. 11 | # 12 | # This program is distributed in the hope that it will be useful, 13 | # but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 | # GNU General Public License for more details. 16 | # 17 | # You should have received a copy of the GNU General Public License 18 | # along with this program. If not, see . 19 | ############### 20 | # The paper describing the theory can be found here: 21 | # X. Zhang, A. Liniger and F. Borrelli; "Optimization-Based Collision Avoidance"; Technical Report, 2017, [https://arxiv.org/abs/1711.03449] 22 | ############### 23 | 24 | ############### 25 | # Function computes H-representation for obstacles given their vertices 26 | # it is assumed that the vertices are given in CLOCK-WISE, and that the first vertex is repeated at the end of the vertex list 27 | ############### 28 | 29 | 30 | 31 | function obstHrep(nOb, vOb, lOb) 32 | 33 | # do simple checks 34 | if nOb != length(lOb) 35 | println("ERROR in number of obstacles") 36 | end 37 | 38 | # these matrices contain the H-rep 39 | A_all = zeros(sum(vOb)-nOb,2) 40 | b_all = zeros(sum(vOb)-nOb,1) 41 | 42 | # counter for lazy people 43 | lazyCounter = 1; 44 | 45 | for i = 1 : nOb # building H-rep 46 | A_i = zeros(vOb[i]-1,2) 47 | b_i = zeros(vOb[i]-1,1) 48 | 49 | # take two subsequent vertices, and compute hyperplane 50 | for j = 1 : vOb[i]-1 51 | 52 | # extract two vertices 53 | v1 = lOb[i][j] # vertex 1 54 | v2 = lOb[i][j+1] # vertex 2 55 | 56 | # find hyperplane passing through v1 and v2 57 | if v1[1] == v2[1] # perpendicular hyperplane, not captured by general formula 58 | if v2[2] < v1[2] # line goes "down" 59 | A_tmp = [1 0] 60 | b_tmp = v1[1] 61 | else 62 | A_tmp = [-1 0] 63 | b_tmp = -v1[1] 64 | end 65 | elseif v1[2] == v2[2] # horizontal hyperplane, captured by general formula but included for numerical stability 66 | if v1[1] < v2[1] 67 | A_tmp = [0 1] 68 | b_tmp = v1[2] 69 | else 70 | A_tmp = [0 -1] 71 | b_tmp = -v1[2] 72 | end 73 | else # general formula for non-horizontal and non-vertical hyperplanes 74 | ab = [v1[1] 1 ; v2[1] 1] \ [v1[2] ; v2[2]] 75 | a = ab[1] 76 | b = ab[2] 77 | 78 | if v1[1] < v2[1] # v1 --> v2 (line moves right) 79 | A_tmp = [-a 1] 80 | b_tmp = b 81 | else # v2 <-- v1 (line moves left) 82 | A_tmp = [a -1] 83 | b_tmp = -b 84 | 85 | end 86 | end 87 | # store vertices 88 | A_i[j,:] = A_tmp 89 | b_i[j] = b_tmp 90 | end 91 | 92 | # store everything 93 | A_all[lazyCounter : lazyCounter+vOb[i]-2,:] = A_i 94 | b_all[lazyCounter : lazyCounter+vOb[i]-2] = b_i 95 | 96 | # update counter 97 | lazyCounter = lazyCounter + vOb[i]-1 98 | end 99 | 100 | return A_all, b_all 101 | 102 | end 103 | -------------------------------------------------------------------------------- /AutonomousParking/plotTraj.jl: -------------------------------------------------------------------------------- 1 | ############### 2 | # OBCA: Optimization-based Collision Avoidance - a path planner for autonomous parking 3 | # Copyright (C) 2018 4 | # Alexander LINIGER [liniger@control.ee.ethz.ch; Automatic Control Lab, ETH Zurich] 5 | # Xiaojing ZHANG [xiaojing.zhang@berkeley.edu; MPC Lab, UC Berkeley] 6 | # 7 | # This program is free software: you can redistribute it and/or modify 8 | # it under the terms of the GNU General Public License as published by 9 | # the Free Software Foundation, either version 3 of the License, or 10 | # (at your option) any later version. 11 | # 12 | # This program is distributed in the hope that it will be useful, 13 | # but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 | # GNU General Public License for more details. 16 | # 17 | # You should have received a copy of the GNU General Public License 18 | # along with this program. If not, see . 19 | ############### 20 | # The paper describing the theory can be found here: 21 | # X. Zhang, A. Liniger and F. Borrelli; "Optimization-Based Collision Avoidance"; Technical Report, 2017, [https://arxiv.org/abs/1711.03449] 22 | ############### 23 | 24 | ############### 25 | # function plots trajectory 26 | ############### 27 | 28 | 29 | function plotTraj(xp,up,N,ego,L,nOb,vOb,lOb,disp_title,plotNumb) 30 | 31 | W_ev = ego[2]+ego[4] 32 | L_ev = ego[1]+ego[3] 33 | 34 | up = [up ; zeros(1,2)] # final position no input 35 | 36 | w = W_ev/2; 37 | offset = L_ev/2 - ego[3] 38 | 39 | # initial state 40 | x0_s = xp[1,:] 41 | Rot0 = [cos(x0_s[3]) -sin(x0_s[3]); sin(x0_s[3]) cos(x0_s[3])] 42 | x0 = [x0_s[1]; x0_s[2]] 43 | centerCar0 = x0 + Rot0*[offset;0] 44 | 45 | # end state 46 | xF_s = xp[end,:] 47 | RotF = [cos(xF_s[3]) -sin(xF_s[3]); sin(xF_s[3]) cos(xF_s[3])] 48 | xF = [xF_s[1]; xF_s[2]] 49 | centerCarF = xF + RotF*[offset;0] 50 | 51 | for i = 1:1:N+1 52 | 53 | figure(plotNumb) 54 | plot(xp[1:i,1],xp[1:i,2],"b") # plot trajectory so far 55 | title(disp_title) 56 | hold(1) 57 | 58 | # plot trajectory 59 | for j = 1 : nOb 60 | for k = 1 : vOb[j] 61 | plot([lOb[j][k][1],lOb[j][k+1][1]] , [lOb[j][k][2],lOb[j][k+1][2]] ,"k") 62 | end 63 | end 64 | 65 | Rot = [cos(xp[i,3]) -sin(xp[i,3]);sin(xp[i,3]) cos(xp[i,3])] 66 | 67 | x_cur = [xp[i,1]; 68 | xp[i,2]] 69 | 70 | centerCar = x_cur + Rot*[offset;0] 71 | 72 | carBox(centerCar,xp[i,3],W_ev/2,L_ev/2) 73 | carBox(x_cur + (Rot*[L;w-0.15]), xp[i,3] + up[i,1],0.15,0.3) 74 | carBox(x_cur + (Rot*[L;-w+0.15]),xp[i,3] + up[i,1],0.15,0.3) 75 | carBox(x_cur + (Rot*[0; w-0.15]) ,xp[i,3],0.15,0.3) 76 | carBox(x_cur + (Rot*[0;-w+0.15]) ,xp[i,3],0.15,0.3) 77 | 78 | # plot start position 79 | plot(x0[1],x0[2],"ob") 80 | carBox(centerCar0,x0_s[3],W_ev/2,L_ev/2) 81 | carBox(x0 + (Rot0*[L;w-0.15]) ,x0_s[3],0.15,0.3) 82 | carBox(x0 + (Rot0*[L;-w+0.15]) ,x0_s[3],0.15,0.3) 83 | carBox(x0 + (Rot0*[0; w-0.15]) ,x0_s[3], 0.15,0.3) 84 | carBox(x0 + (Rot0*[0;-w+0.15]) ,x0_s[3], 0.15,0.3) 85 | 86 | # plot end position 87 | carBox_dashed(centerCarF,xF_s[3],W_ev/2,L_ev/2) 88 | carBox_dashed(xF + (RotF*[L;w-0.15]) ,xF_s[3],0.15,0.3) 89 | carBox_dashed(xF + (RotF*[L;-w+0.15]) ,xF_s[3],0.15,0.3) 90 | carBox_dashed(xF + (RotF*[0; w-0.15]) ,xF_s[3], 0.15,0.3) 91 | carBox_dashed(xF + (RotF*[0;-w+0.15]) ,xF_s[3], 0.15,0.3) 92 | if i == N+1 93 | plot(xF[1],xF[2],"ob") 94 | end 95 | 96 | axis("equal") 97 | 98 | hold(0) 99 | 100 | sleep(0.05) 101 | end 102 | end 103 | 104 | # plot cars 105 | function carBox(x0,phi,w,l) 106 | car1 = x0[1:2] + [cos(phi)*l;sin(phi)*l] + [sin(phi)*w;-cos(phi)*w]; 107 | car2 = x0[1:2] + [cos(phi)*l;sin(phi)*l] - [sin(phi)*w;-cos(phi)*w]; 108 | car3 = x0[1:2] - [cos(phi)*l;sin(phi)*l] + [sin(phi)*w;-cos(phi)*w]; 109 | car4 = x0[1:2] - [cos(phi)*l;sin(phi)*l] - [sin(phi)*w;-cos(phi)*w]; 110 | plot([car1[1],car2[1],car4[1],car3[1],car1[1]],[car1[2],car2[2],car4[2],car3[2],car1[2]],"k") 111 | end 112 | 113 | # plot cars 114 | function carBox_dashed(x0,phi,w,l) 115 | car1 = x0[1:2] + [cos(phi)*l;sin(phi)*l] + [sin(phi)*w;-cos(phi)*w]; 116 | car2 = x0[1:2] + [cos(phi)*l;sin(phi)*l] - [sin(phi)*w;-cos(phi)*w]; 117 | car3 = x0[1:2] - [cos(phi)*l;sin(phi)*l] + [sin(phi)*w;-cos(phi)*w]; 118 | car4 = x0[1:2] - [cos(phi)*l;sin(phi)*l] - [sin(phi)*w;-cos(phi)*w]; 119 | plot([car1[1],car2[1],car4[1],car3[1],car1[1]],[car1[2],car2[2],car4[2],car3[2],car1[2]],":k") 120 | end 121 | -------------------------------------------------------------------------------- /AutonomousParking/reeds_shepp.jl: -------------------------------------------------------------------------------- 1 | ############### 2 | # OBCA: Optimization-based Collision Avoidance - a path planner for autonomous parking 3 | # Copyright (C) 2018 4 | # Atsushi SAKAI [atsushisakai@global.komatsu; Komatsu Ltd / MPC Lab] 5 | # Alexander LINIGER [liniger@control.ee.ethz.ch; Automatic Control Lab, ETH Zurich] 6 | # Xiaojing ZHANG [xiaojing.zhang@berkeley.edu; MPC Lab, UC Berkeley] 7 | # 8 | # This program is free software: you can redistribute it and/or modify 9 | # it under the terms of the GNU General Public License as published by 10 | # the Free Software Foundation, either version 3 of the License, or 11 | # (at your option) any later version. 12 | # 13 | # This program is distributed in the hope that it will be useful, 14 | # but WITHOUT ANY WARRANTY; without even the implied warranty of 15 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 16 | # GNU General Public License for more details. 17 | # 18 | # You should have received a copy of the GNU General Public License 19 | # along with this program. If not, see . 20 | ############### 21 | # The paper describing the theory can be found here: 22 | # X. Zhang, A. Liniger and F. Borrelli; "Optimization-Based Collision Avoidance"; Technical Report, 2017 23 | # X. Zhang, A. Liniger, A. Sakai and F. Borrelli; "Autonomous Parking using Optimization-Based Collision Avoidance"; Technical Report, 2018 [add URL] 24 | ############### 25 | 26 | ############### 27 | # Reeds Shepp path planner 28 | ############### 29 | 30 | 31 | module reeds_shepp 32 | 33 | using PyPlot 34 | 35 | const STEP_SIZE = 0.1 36 | 37 | type Path 38 | lengths::Array{Float64} #lengths of each part of the path +: forward, -: backward 39 | ctypes::Array{String} # type of each part of the path 40 | L::Float64 # total path length 41 | x::Array{Float64} # final x positions [m] 42 | y::Array{Float64} # final y positions [m] 43 | yaw::Array{Float64} # final yaw angles [rad] 44 | directions::Array{Int8} # forward:1, backward:-1 45 | end 46 | 47 | function pi_2_pi(iangle::Float64)::Float64 48 | while (iangle > pi) 49 | iangle -= 2.0 * pi 50 | end 51 | while (iangle < -pi) 52 | iangle += 2.0 * pi 53 | end 54 | 55 | return iangle 56 | end 57 | 58 | 59 | function calc_shortest_path(sx::Float64, sy::Float64, syaw::Float64, 60 | gx::Float64, gy::Float64, gyaw::Float64, 61 | maxc::Float64; 62 | step_size::Float64 = STEP_SIZE) 63 | # println("Find Shortest Path") 64 | paths = calc_paths(sx,sy,syaw,gx,gy,gyaw,maxc,step_size=step_size) 65 | 66 | minL = Inf 67 | best_path_index = -1 68 | for i in 1:length(paths) 69 | if paths[i].L <= minL 70 | minL = paths[i].L 71 | best_path_index = i 72 | end 73 | end 74 | 75 | return paths[best_path_index] 76 | end 77 | 78 | 79 | function calc_shortest_path_length(sx::Float64, sy::Float64, syaw::Float64, 80 | gx::Float64, gy::Float64, gyaw::Float64, 81 | maxc::Float64; 82 | step_size::Float64 = STEP_SIZE) 83 | q0 = [sx, sy, syaw] 84 | q1 = [gx, gy, gyaw] 85 | paths = generate_path(q0, q1, maxc) 86 | 87 | minL = Inf 88 | for i in 1:length(paths) 89 | L = paths[i].L/maxc 90 | if L <= minL 91 | minL = L 92 | end 93 | end 94 | 95 | return minL 96 | end 97 | 98 | 99 | function calc_paths(sx::Float64, sy::Float64, syaw::Float64, 100 | gx::Float64, gy::Float64, gyaw::Float64, 101 | maxc::Float64; step_size::Float64 = STEP_SIZE)::Array{Path} 102 | q0 = [sx, sy, syaw] 103 | q1 = [gx, gy, gyaw] 104 | 105 | paths = generate_path(q0, q1, maxc) 106 | for path in paths 107 | x, y, yaw, directions = generate_local_course(path.L, path.lengths, path.ctypes, maxc, step_size*maxc) 108 | 109 | # convert global coordinate 110 | path.x = [cos(-q0[3]) * ix + sin(-q0[3]) * iy + q0[1] for (ix, iy) in zip(x, y)] 111 | path.y = [-sin(-q0[3]) * ix + cos(-q0[3]) * iy + q0[2] for (ix, iy) in zip(x, y)] 112 | path.yaw = pi_2_pi.([iyaw + q0[3] for iyaw in yaw]) 113 | path.directions = directions 114 | path.lengths = [l/maxc for l in path.lengths] 115 | path.L = path.L/maxc 116 | 117 | end 118 | 119 | return paths 120 | end 121 | 122 | 123 | function get_label(path::Path) 124 | label ="" 125 | 126 | for (m,l) in zip(path.ctypes, path.lengths) 127 | label = string(label, m) 128 | if l > 0.0 129 | label = string(label, "+") 130 | else 131 | label = string(label, "-") 132 | end 133 | end 134 | 135 | return label 136 | end 137 | 138 | 139 | function polar(x::Float64, y::Float64) 140 | r = sqrt(x^2+y^2) 141 | theta = atan2(y, x) 142 | return r, theta 143 | end 144 | 145 | 146 | function mod2pi(x::Float64) 147 | v = mod(x, 2.0*pi) 148 | if v < -pi 149 | v += 2.0*pi; 150 | else 151 | if v > pi 152 | v -= 2.0*pi 153 | end 154 | end 155 | return v 156 | end 157 | 158 | 159 | function LSL(x::Float64, y::Float64, phi::Float64) 160 | u, t = polar(x - sin(phi), y - 1.0 + cos(phi)) 161 | if t >= 0.0 162 | v = mod2pi(phi - t) 163 | if (v >= 0.0) 164 | return true, t, u, v 165 | end 166 | end 167 | 168 | return false, 0.0, 0.0, 0.0 169 | end 170 | 171 | 172 | function LSR(x::Float64, y::Float64, phi::Float64) 173 | u1, t1 = polar(x + sin(phi), y - 1.0 - cos(phi)) 174 | u1 = u1^2; 175 | if u1 >= 4.0 176 | u = sqrt(u1 - 4.0) 177 | theta = atan2(2.0, u) 178 | t = mod2pi(t1 + theta) 179 | v = mod2pi(t - phi) 180 | 181 | if t >= 0.0 && v >= 0.0 182 | return true, t, u, v 183 | end 184 | end 185 | 186 | return false, 0.0, 0.0, 0.0 187 | end 188 | 189 | 190 | function LRL(x::Float64, y::Float64, phi::Float64) 191 | u1, t1 = polar(x - sin(phi), y - 1.0 + cos(phi)) 192 | 193 | if u1 <= 4.0 194 | u = -2.0*asin(0.25 * u1) 195 | t = mod2pi(t1 + 0.5 * u + pi); 196 | v = mod2pi(phi - t + u); 197 | 198 | if t >= 0.0 && u <= 0.0 199 | return true, t, u, v 200 | end 201 | end 202 | 203 | return false, 0.0, 0.0, 0.0 204 | end 205 | 206 | 207 | function set_path(paths::Array{Path}, lengths::Array{Float64}, ctypes::Array{String}) 208 | 209 | path = Path([],[],0.0,[],[],[],[]) 210 | path.ctypes = ctypes 211 | path.lengths = lengths 212 | 213 | # check same path exist 214 | for tpath in paths 215 | typeissame = (tpath.ctypes == path.ctypes) 216 | if typeissame 217 | if sum(tpath.lengths - path.lengths) <= 0.01 218 | return paths # not insert path 219 | end 220 | end 221 | end 222 | 223 | path.L = sum([abs(i) for i in lengths]) 224 | 225 | Base.Test.@test path.L >= 0.01 226 | 227 | push!(paths, path) 228 | 229 | return paths 230 | end 231 | 232 | 233 | function SCS(x::Float64, y::Float64, phi::Float64, paths::Array{Path})::Array{Path} 234 | flag, t, u, v = SLS(x, y, phi) 235 | if flag 236 | # println("SCS1") 237 | paths = set_path(paths, [t, u, v], ["S","L","S"]) 238 | end 239 | flag, t, u, v = SLS(x, -y, -phi) 240 | if flag 241 | # println("SCS2") 242 | paths = set_path(paths, [t, u, v], ["S","R","S"]) 243 | end 244 | 245 | return paths 246 | end 247 | 248 | 249 | function SLS(x::Float64, y::Float64, phi::Float64) 250 | # println(x,",", y,",", phi, ",", mod2pi(phi)) 251 | phi = mod2pi(phi) 252 | if y > 0.0 && phi > 0.0 && phi < pi*0.99 253 | xd = - y/tan(phi) + x 254 | t = xd - tan(phi/2.0) 255 | u = phi 256 | v = sqrt((x-xd)^2+y^2)-tan(phi/2.0) 257 | # println("1,",t,",",u,",",v) 258 | return true, t, u, v 259 | elseif y < 0.0 && phi > 0.0 && phi < pi*0.99 260 | xd = - y/tan(phi) + x 261 | t = xd - tan(phi/2.0) 262 | u = phi 263 | v = -sqrt((x-xd)^2+y^2)-tan(phi/2.0) 264 | # println("2,",t,",",u,",",v) 265 | return true, t, u, v 266 | end 267 | 268 | return false, 0.0, 0.0, 0.0 269 | end 270 | 271 | 272 | function CSC(x::Float64, y::Float64, phi::Float64, paths::Array{Path}) 273 | flag, t, u, v = LSL(x, y, phi) 274 | if flag 275 | # println("CSC1") 276 | paths = set_path(paths, [t, u, v], ["L","S","L"]) 277 | end 278 | flag, t, u, v = LSL(-x, y, -phi) 279 | if flag 280 | # println("CSC2") 281 | paths = set_path(paths, [-t, -u, -v], ["L","S","L"]) 282 | end 283 | flag, t, u, v = LSL(x, -y, -phi) 284 | if flag 285 | # println("CSC3") 286 | paths = set_path(paths, [t, u, v], ["R","S","R"]) 287 | end 288 | flag, t, u, v = LSL(-x, -y, phi) 289 | if flag 290 | # println("CSC4") 291 | paths = set_path(paths, [-t, -u, -v], ["R","S","R"]) 292 | end 293 | flag, t, u, v = LSR(x, y, phi) 294 | if flag 295 | # println("CSC5") 296 | paths = set_path(paths, [t, u, v], ["L","S","R"]) 297 | end 298 | flag, t, u, v = LSR(-x, y, -phi) 299 | if flag 300 | # println("CSC6") 301 | paths = set_path(paths, [-t, -u, -v], ["L","S","R"]) 302 | end 303 | flag, t, u, v = LSR(x, -y, -phi) 304 | if flag 305 | # println("CSC7") 306 | paths = set_path(paths, [t, u, v], ["R","S","L"]) 307 | end 308 | flag, t, u, v = LSR(-x, -y, phi) 309 | if flag 310 | # println("CSC8") 311 | paths = set_path(paths, [-t, -u, -v], ["R","S","L"]) 312 | end 313 | 314 | return paths 315 | end 316 | 317 | 318 | function CCC(x::Float64, y::Float64, phi::Float64, paths::Array{Path}) 319 | 320 | flag, t, u, v = LRL(x, y, phi) 321 | if flag 322 | # println("CCC1") 323 | paths = set_path(paths, [t, u, v], ["L","R","L"]) 324 | end 325 | flag, t, u, v = LRL(-x, y, -phi) 326 | if flag 327 | # println("CCC2") 328 | paths = set_path(paths, [-t, -u, -v], ["L","R","L"]) 329 | end 330 | flag, t, u, v = LRL(x, -y, -phi) 331 | if flag 332 | # println("CCC3") 333 | paths = set_path(paths, [t, u, v], ["R","L","R"]) 334 | end 335 | flag, t, u, v = LRL(-x, -y, phi) 336 | if flag 337 | # println("CCC4") 338 | paths = set_path(paths, [-t, -u, -v], ["R","L","R"]) 339 | end 340 | 341 | # backwards 342 | xb = x*cos(phi) + y*sin(phi) 343 | yb = x*sin(phi) - y*cos(phi) 344 | # println(xb, ",", yb,",",x,",",y) 345 | 346 | flag, t, u, v = LRL(xb, yb, phi) 347 | if flag 348 | # println("CCC5") 349 | paths = set_path(paths, [v, u, t], ["L","R","L"]) 350 | end 351 | flag, t, u, v = LRL(-xb, yb, -phi) 352 | if flag 353 | # println("CCC6") 354 | paths = set_path(paths, [-v, -u, -t], ["L","R","L"]) 355 | end 356 | flag, t, u, v = LRL(xb, -yb, -phi) 357 | if flag 358 | # println("CCC7") 359 | paths = set_path(paths, [v, u, t], ["R","L","R"]) 360 | end 361 | flag, t, u, v = LRL(-xb, -yb, phi) 362 | if flag 363 | # println("CCC8") 364 | paths = set_path(paths, [-v, -u, -t], ["R","L","R"]) 365 | end 366 | 367 | return paths 368 | end 369 | 370 | 371 | function calc_tauOmega(u::Float64, v::Float64, xi::Float64, eta::Float64, phi::Float64) 372 | delta = mod2pi(u-v) 373 | A = sin(u) - sin(delta) 374 | B = cos(u) - cos(delta) - 1.0 375 | 376 | t1 = atan2(eta*A - xi*B, xi*A + eta*B) 377 | t2 = 2.0 * (cos(delta) - cos(v) - cos(u)) + 3.0; 378 | 379 | if t2 < 0 380 | tau = mod2pi(t1+pi) 381 | else 382 | tau = mod2pi(t1) 383 | end 384 | omega = mod2pi(tau - u + v - phi) 385 | 386 | return tau, omega 387 | end 388 | 389 | 390 | function LRLRn(x::Float64, y::Float64, phi::Float64) 391 | xi = x + sin(phi) 392 | eta = y - 1.0 - cos(phi) 393 | rho = 0.25 * (2.0 + sqrt(xi*xi + eta*eta)) 394 | 395 | if rho <= 1.0 396 | u = acos(rho) 397 | t, v = calc_tauOmega(u, -u, xi, eta, phi); 398 | if t >= 0.0 && v <= 0.0 399 | return true, t, u, v 400 | end 401 | end 402 | 403 | return false, 0.0, 0.0, 0.0 404 | end 405 | 406 | 407 | function LRLRp(x::Float64, y::Float64, phi::Float64) 408 | xi = x + sin(phi) 409 | eta = y - 1.0 - cos(phi) 410 | rho = (20.0 - xi*xi - eta*eta) / 16.0; 411 | # println(xi,",",eta,",",rho) 412 | 413 | if (rho>=0.0 && rho<=1.0) 414 | u = -acos(rho); 415 | if (u >= -0.5 * pi) 416 | t, v = calc_tauOmega(u, u, xi, eta, phi); 417 | if t >= 0.0 && v >= 0.0 418 | return true, t, u, v 419 | end 420 | end 421 | end 422 | 423 | return false, 0.0, 0.0, 0.0 424 | end 425 | 426 | 427 | function CCCC(x::Float64, y::Float64, phi::Float64, paths::Array{Path}) 428 | 429 | flag, t, u, v = LRLRn(x, y, phi) 430 | if flag 431 | # println("CCCC1") 432 | paths = set_path(paths, [t, u, -u, v], ["L","R","L","R"]) 433 | end 434 | 435 | flag, t, u, v = LRLRn(-x, y, -phi) 436 | if flag 437 | # println("CCCC2") 438 | paths = set_path(paths, [-t, -u, u, -v], ["L","R","L","R"]) 439 | end 440 | 441 | flag, t, u, v = LRLRn(x, -y, -phi) 442 | if flag 443 | # println("CCCC3") 444 | paths = set_path(paths, [t, u, -u, v], ["R","L","R","L"]) 445 | end 446 | 447 | flag, t, u, v = LRLRn(-x, -y, phi) 448 | if flag 449 | # println("CCCC4") 450 | paths = set_path(paths, [-t, -u, u, -v], ["R","L","R","L"]) 451 | end 452 | 453 | flag, t, u, v = LRLRp(x, y, phi) 454 | if flag 455 | # println("CCCC5") 456 | paths = set_path(paths, [t, u, u, v], ["L","R","L","R"]) 457 | end 458 | 459 | flag, t, u, v = LRLRp(-x, y, -phi) 460 | if flag 461 | # println("CCCC6") 462 | paths = set_path(paths, [-t, -u, -u, -v], ["L","R","L","R"]) 463 | end 464 | 465 | flag, t, u, v = LRLRp(x, -y, -phi) 466 | if flag 467 | # println("CCCC7") 468 | paths = set_path(paths, [t, u, u, v], ["R","L","R","L"]) 469 | end 470 | 471 | flag, t, u, v = LRLRp(-x, -y, phi) 472 | if flag 473 | # println("CCCC8") 474 | paths = set_path(paths, [-t, -u, -u, -v], ["R","L","R","L"]) 475 | end 476 | 477 | return paths 478 | end 479 | 480 | 481 | function LRSR(x::Float64, y::Float64, phi::Float64) 482 | 483 | xi = x + sin(phi) 484 | eta = y - 1.0 - cos(phi) 485 | rho, theta = polar(-eta, xi) 486 | 487 | if rho >= 2.0 488 | t = theta 489 | u = 2.0 - rho 490 | v = mod2pi(t + 0.5*pi - phi) 491 | if t >= 0.0 && u <= 0.0 && v <=0.0 492 | return true, t, u, v 493 | end 494 | end 495 | 496 | return false, 0.0, 0.0, 0.0 497 | end 498 | 499 | 500 | function LRSL(x::Float64, y::Float64, phi::Float64) 501 | xi = x - sin(phi) 502 | eta = y - 1.0 + cos(phi) 503 | rho, theta = polar(xi, eta) 504 | 505 | if rho >= 2.0 506 | r = sqrt(rho*rho - 4.0); 507 | u = 2.0 - r; 508 | t = mod2pi(theta + atan2(r, -2.0)); 509 | v = mod2pi(phi - 0.5*pi - t); 510 | if t >= 0.0 && u<=0.0 && v<=0.0 511 | return true, t, u, v 512 | end 513 | end 514 | 515 | return false, 0.0, 0.0, 0.0 516 | end 517 | 518 | 519 | function CCSC(x::Float64, y::Float64, phi::Float64, paths::Array{Path}) 520 | 521 | flag, t, u, v = LRSL(x, y, phi) 522 | if flag 523 | # println("CCSC1") 524 | paths = set_path(paths, [t, -0.5*pi, u, v], ["L","R","S","L"]) 525 | end 526 | 527 | flag, t, u, v = LRSL(-x, y, -phi) 528 | if flag 529 | # println("CCSC2") 530 | paths = set_path(paths, [-t, 0.5*pi, -u, -v], ["L","R","S","L"]) 531 | end 532 | 533 | flag, t, u, v = LRSL(x, -y, -phi) 534 | if flag 535 | # println("CCSC3") 536 | paths = set_path(paths, [t, -0.5*pi, u, v], ["R","L","S","R"]) 537 | end 538 | 539 | flag, t, u, v = LRSL(-x, -y, phi) 540 | if flag 541 | # println("CCSC4") 542 | paths = set_path(paths, [-t, 0.5*pi, -u, -v], ["R","L","S","R"]) 543 | end 544 | 545 | flag, t, u, v = LRSR(x, y, phi) 546 | if flag 547 | # println("CCSC5") 548 | paths = set_path(paths, [t, -0.5*pi, u, v], ["L","R","S","R"]) 549 | end 550 | 551 | flag, t, u, v = LRSR(-x, y, -phi) 552 | if flag 553 | # println("CCSC6") 554 | paths = set_path(paths, [-t, 0.5*pi, -u, -v], ["L","R","S","R"]) 555 | end 556 | 557 | flag, t, u, v = LRSR(x, -y, -phi) 558 | if flag 559 | # println("CCSC7") 560 | paths = set_path(paths, [t, -0.5*pi, u, v], ["R","L","S","L"]) 561 | end 562 | 563 | flag, t, u, v = LRSR(-x, -y, phi) 564 | if flag 565 | # println("CCSC8") 566 | paths = set_path(paths, [-t, 0.5*pi, -u, -v], ["R","L","S","L"]) 567 | end 568 | 569 | # backwards 570 | xb = x*cos(phi) + y*sin(phi) 571 | yb = x*sin(phi) - y*cos(phi) 572 | flag, t, u, v = LRSL(xb, yb, phi) 573 | if flag 574 | # println("CCSC9") 575 | paths = set_path(paths, [v, u, -0.5*pi, t], ["L","S","R","L"]) 576 | end 577 | 578 | flag, t, u, v = LRSL(-xb, yb, -phi) 579 | if flag 580 | # println("CCSC10") 581 | paths = set_path(paths, [-v, -u, 0.5*pi, -t], ["L","S","R","L"]) 582 | end 583 | 584 | flag, t, u, v = LRSL(xb, -yb, -phi) 585 | if flag 586 | # println("CCSC11") 587 | paths = set_path(paths, [v, u, -0.5*pi, t], ["R","S","L","R"]) 588 | end 589 | 590 | flag, t, u, v = LRSL(-xb, -yb, phi) 591 | if flag 592 | # println("CCSC12") 593 | paths = set_path(paths, [-v, -u, 0.5*pi, -t], ["R","S","L","R"]) 594 | end 595 | 596 | flag, t, u, v = LRSR(xb, yb, phi) 597 | if flag 598 | # println("CCSC13") 599 | paths = set_path(paths, [v, u, -0.5*pi, t], ["R","S","R","L"]) 600 | end 601 | 602 | flag, t, u, v = LRSR(-xb, yb, -phi) 603 | if flag 604 | # println("CCSC14") 605 | paths = set_path(paths, [-v, -u, 0.5*pi, -t], ["R","S","R","L"]) 606 | end 607 | 608 | flag, t, u, v = LRSR(xb, -yb, -phi) 609 | if flag 610 | # println("CCSC15") 611 | paths = set_path(paths, [v, u, -0.5*pi, t], ["L","S","L","R"]) 612 | end 613 | 614 | flag, t, u, v = LRSR(-xb, -yb, phi) 615 | if flag 616 | # println("CCSC16") 617 | paths = set_path(paths, [-v, -u, 0.5*pi, -t], ["L","S","L","R"]) 618 | end 619 | 620 | return paths 621 | end 622 | 623 | 624 | function LRSLR(x::Float64, y::Float64, phi::Float64) 625 | # formula 8.11 *** TYPO IN PAPER *** 626 | xi = x + sin(phi) 627 | eta = y - 1.0 - cos(phi) 628 | rho, theta = polar(xi, eta) 629 | if rho >= 2.0 630 | u = 4.0 - sqrt(rho*rho - 4.0) 631 | if u <= 0.0 632 | t = mod2pi(atan2((4.0-u)*xi -2.0*eta, -2.0*xi + (u-4.0)*eta)); 633 | v = mod2pi(t - phi); 634 | 635 | if t >= 0.0 && v >=0.0 636 | return true, t, u, v 637 | end 638 | end 639 | end 640 | 641 | return false, 0.0, 0.0, 0.0 642 | end 643 | 644 | 645 | function CCSCC(x::Float64, y::Float64, phi::Float64, paths::Array{Path}) 646 | flag, t, u, v = LRSLR(x, y, phi) 647 | if flag 648 | # println("CCSCC1") 649 | paths = set_path(paths, [t, -0.5*pi, u, -0.5*pi, v], ["L","R","S","L","R"]) 650 | end 651 | flag, t, u, v = LRSLR(-x, y, -phi) 652 | if flag 653 | # println("CCSCC2") 654 | paths = set_path(paths, [-t, 0.5*pi, -u, 0.5*pi, -v], ["L","R","S","L","R"]) 655 | end 656 | 657 | flag, t, u, v = LRSLR(x, -y, -phi) 658 | if flag 659 | # println("CCSCC3") 660 | paths = set_path(paths, [t, -0.5*pi, u, -0.5*pi, v], ["R","L","S","R","L"]) 661 | end 662 | 663 | flag, t, u, v = LRSLR(-x, -y, phi) 664 | if flag 665 | # println("CCSCC4") 666 | paths = set_path(paths, [-t, 0.5*pi, -u, 0.5*pi, -v], ["R","L","S","R","L"]) 667 | end 668 | 669 | return paths 670 | end 671 | 672 | 673 | function generate_local_course(L::Float64, 674 | lengths::Array{Float64}, 675 | mode::Array{String}, 676 | maxc::Float64, 677 | step_size::Float64) 678 | npoint = trunc(Int64, L/step_size) + length(lengths)+3 679 | # println(npoint, ",", L, ",", step_size, ",", L/step_size) 680 | 681 | px = fill(0.0, npoint) 682 | py = fill(0.0, npoint) 683 | pyaw = fill(0.0, npoint) 684 | directions = fill(0, npoint) 685 | ind = 2 686 | 687 | if lengths[1] > 0.0 688 | directions[1] = 1 689 | else 690 | directions[1] = -1 691 | end 692 | 693 | if lengths[1] > 0.0 694 | d = step_size 695 | else 696 | d = -step_size 697 | end 698 | 699 | pd = d 700 | ll = 0.0 701 | 702 | for (m, l, i) in zip(mode, lengths, 1:length(mode)) 703 | 704 | if l > 0.0 705 | d = step_size 706 | else 707 | d = -step_size 708 | end 709 | 710 | # set prigin state 711 | ox, oy, oyaw = px[ind], py[ind], pyaw[ind] 712 | 713 | ind -= 1 714 | if i >= 2 && (lengths[i-1]*lengths[i])>0 715 | pd = - d - ll 716 | else 717 | pd = d - ll 718 | end 719 | 720 | while abs(pd) <= abs(l) 721 | ind += 1 722 | px, py, pyaw, directions = interpolate(ind, pd, m, maxc, ox, oy, oyaw, px, py, pyaw, directions) 723 | pd += d 724 | end 725 | 726 | ll = l - pd - d # calc remain length 727 | 728 | ind += 1 729 | px, py, pyaw, directions = interpolate(ind, l, m, maxc, ox, oy, oyaw, px, py, pyaw, directions) 730 | end 731 | 732 | #remove unused data 733 | while px[end] == 0.0 734 | pop!(px) 735 | pop!(py) 736 | pop!(pyaw) 737 | pop!(directions) 738 | end 739 | 740 | return px, py, pyaw, directions 741 | end 742 | 743 | 744 | function interpolate(ind::Int64, l::Float64, m::String, maxc::Float64, 745 | ox::Float64, oy::Float64, oyaw::Float64, 746 | px::Array{Float64}, py::Array{Float64}, pyaw::Array{Float64}, 747 | directions::Array{Int64}) 748 | 749 | if m == "S" 750 | px[ind] = ox + l / maxc * cos(oyaw) 751 | py[ind] = oy + l / maxc * sin(oyaw) 752 | pyaw[ind] = oyaw 753 | else # curve 754 | ldx = sin(l) / maxc 755 | if m == "L" # left turn 756 | ldy = (1.0 - cos(l)) / maxc 757 | elseif m == "R" # right turn 758 | ldy = (1.0 - cos(l)) / -maxc 759 | end 760 | gdx = cos(-oyaw) * ldx + sin(-oyaw) * ldy 761 | gdy = -sin(-oyaw) * ldx + cos(-oyaw) * ldy 762 | px[ind] = ox + gdx 763 | py[ind] = oy + gdy 764 | end 765 | 766 | if m == "L" # left turn 767 | pyaw[ind] = oyaw + l 768 | elseif m == "R" # right turn 769 | pyaw[ind] = oyaw - l 770 | end 771 | 772 | if l > 0.0 773 | directions[ind] = 1 774 | else 775 | directions[ind] = -1 776 | end 777 | 778 | return px, py, pyaw, directions 779 | end 780 | 781 | 782 | function generate_path(q0::Array{Float64}, q1::Array{Float64}, maxc::Float64)::Array{Path} 783 | dx = q1[1] - q0[1] 784 | dy = q1[2] - q0[2] 785 | dth = q1[3] - q0[3] 786 | c = cos(q0[3]) 787 | s = sin(q0[3]); 788 | x = (c*dx + s*dy)*maxc 789 | y = (-s*dx + c*dy)*maxc 790 | 791 | paths = Path[] 792 | paths = SCS(x, y, dth, paths) 793 | paths = CSC(x, y, dth, paths) 794 | paths = CCC(x, y, dth, paths) 795 | paths = CCCC(x, y, dth, paths) 796 | paths = CCSC(x, y, dth, paths) 797 | paths = CCSCC(x, y, dth, paths) 798 | 799 | return paths 800 | end 801 | 802 | 803 | function calc_curvature(x,y,yaw, directions) 804 | 805 | c = Float64[] 806 | ds = Float64[] 807 | 808 | for i in 2:length(x)-1 809 | dxn = x[i]-x[i-1] 810 | dxp = x[i+1]-x[i] 811 | dyn = y[i]-y[i-1] 812 | dyp = y[i+1]-y[i] 813 | dn =sqrt(dxn^2.0+dyn^2.0) 814 | dp =sqrt(dxp^2.0+dyp^2.0) 815 | dx = 1.0/(dn+dp)*(dp/dn*dxn+dn/dp*dxp) 816 | ddx = 2.0/(dn+dp)*(dxp/dp-dxn/dn) 817 | dy = 1.0/(dn+dp)*(dp/dn*dyn+dn/dp*dyp) 818 | ddy = 2.0/(dn+dp)*(dyp/dp-dyn/dn) 819 | curvature = (ddy*dx-ddx*dy)/(dx^2+dy^2) 820 | d = (dn+dp)/2.0 821 | 822 | if isnan(curvature) 823 | curvature = 0.0 824 | end 825 | 826 | if directions[i] <= 0.0 827 | curvature = -curvature 828 | end 829 | 830 | if length(c) == 0 831 | push!(ds, d) 832 | push!(c, curvature) 833 | end 834 | 835 | push!(ds, d) 836 | push!(c, curvature) 837 | end 838 | 839 | push!(ds, ds[end]) 840 | push!(c, c[end] ) 841 | 842 | return c, ds 843 | end 844 | 845 | 846 | function check_path(start_x, start_y, start_yaw, end_x, end_y, end_yaw, max_curvature) 847 | # println("Test") 848 | # println(start_x,",", start_y, "," ,start_yaw, ",", max_curvature) 849 | paths = calc_paths(start_x, start_y, start_yaw, end_x, end_y, end_yaw, max_curvature) 850 | 851 | Base.Test.@test length(paths) >= 1 852 | 853 | for path in paths 854 | Base.Test.@test abs(path.x[1] - start_x) <= 0.01 855 | Base.Test.@test abs(path.y[1] - start_y) <= 0.01 856 | Base.Test.@test abs(path.yaw[1] - start_yaw) <= 0.01 857 | Base.Test.@test abs(path.x[end] - end_x) <= 0.01 858 | Base.Test.@test abs(path.y[end] - end_y) <= 0.01 859 | Base.Test.@test abs(path.yaw[end] - end_yaw) <= 0.01 860 | 861 | #course distance check 862 | d = [sqrt(dx^2+dy^2) for (dx, dy) in zip(diff(path.x[1:end-1]), diff(path.y[1:end-1]))] 863 | 864 | for i in length(d) 865 | Base.Test.@test abs(d[i] - STEP_SIZE) <= 0.001 866 | end 867 | end 868 | 869 | end 870 | 871 | function test() 872 | println("Test1") 873 | start_x = 0.0 # [m] 874 | start_y = 0.0 # [m] 875 | start_yaw = deg2rad(10.0) # [rad] 876 | end_x = 7.0 # [m] 877 | end_y = -8.0 # [m] 878 | end_yaw = deg2rad(50.0) # [rad] 879 | max_curvature = 2.0 880 | 881 | check_path(start_x, start_y, start_yaw, end_x, end_y, end_yaw, max_curvature) 882 | 883 | start_x = 0.0 # [m] 884 | start_y = 0.0 # [m] 885 | start_yaw = deg2rad(10.0) # [rad] 886 | end_x = 7.0 # [m] 887 | end_y = -8.0 # [m] 888 | end_yaw = deg2rad(-50.0) # [rad] 889 | max_curvature = 2.0 890 | 891 | check_path(start_x, start_y, start_yaw, end_x, end_y, end_yaw, max_curvature) 892 | 893 | start_x = 0.0 # [m] 894 | start_y = 10.0 # [m] 895 | start_yaw = deg2rad(-10.0) # [rad] 896 | end_x = -7.0 # [m] 897 | end_y = -8.0 # [m] 898 | end_yaw = deg2rad(-50.0) # [rad] 899 | max_curvature = 2.0 900 | 901 | check_path(start_x, start_y, start_yaw, end_x, end_y, end_yaw, max_curvature) 902 | 903 | start_x = 0.0 # [m] 904 | start_y = 10.0 # [m] 905 | start_yaw = deg2rad(-10.0) # [rad] 906 | end_x = -7.0 # [m] 907 | end_y = -8.0 # [m] 908 | end_yaw = deg2rad(150.0) # [rad] 909 | max_curvature = 1.0 910 | 911 | check_path(start_x, start_y, start_yaw, end_x, end_y, end_yaw, max_curvature) 912 | 913 | start_x = 0.0 # [m] 914 | start_y = 10.0 # [m] 915 | start_yaw = deg2rad(-10.0) # [rad] 916 | end_x = 7.0 # [m] 917 | end_y = 8.0 # [m] 918 | end_yaw = deg2rad(150.0) # [rad] 919 | max_curvature = 2.0 920 | check_path(start_x, start_y, start_yaw, end_x, end_y, end_yaw, max_curvature) 921 | 922 | start_x = -40.0 # [m] 923 | start_y = 549.0 # [m] 924 | start_yaw = 2.44346 # [rad] 925 | end_x = 36.0 # [m] 926 | end_y = 446.0 # [m] 927 | end_yaw = -0.698132 928 | max_curvature = 0.05890904077226434 929 | check_path(start_x, start_y, start_yaw, end_x, end_y, end_yaw, max_curvature) 930 | 931 | # Random test 932 | for i in 1:100 933 | start_x = rand()*100.0 - 50.0 934 | start_y = rand()*100.0 - 50.0 935 | start_yaw = deg2rad(rand()*360.0 - 180.0) 936 | end_x = rand()*100.0 - 50.0 937 | end_y = rand()*100.0 - 50.0 938 | end_yaw = deg2rad(rand()*360.0 - 180.0) 939 | max_curvature = rand()/10.0 940 | # println(i, ",", start_x, ",", start_y,",", start_yaw,",",end_x,",",end_y,",", end_yaw) 941 | check_path(start_x, start_y, start_yaw, end_x, end_y, end_yaw, max_curvature) 942 | end 943 | end 944 | 945 | 946 | function main() 947 | println(PROGRAM_FILE," start!!") 948 | test() 949 | 950 | start_x = 3.0 # [m] 951 | start_y = 10.0 # [m] 952 | start_yaw = deg2rad(40.0) # [rad] 953 | end_x = 0.0 # [m] 954 | end_y = 1.0 # [m] 955 | end_yaw = deg2rad(0.0) # [rad] 956 | max_curvature = 0.1 957 | 958 | @time bpath = calc_shortest_path( 959 | start_x, start_y, start_yaw, end_x, end_y, end_yaw, max_curvature) 960 | 961 | rc, rds = calc_curvature(bpath.x, bpath.y, bpath.yaw, bpath.directions) 962 | 963 | subplots(1) 964 | plot(bpath.x, bpath.y,"-r", label=get_label(bpath)) 965 | 966 | plot(start_x, start_y) 967 | plot(end_x, end_y) 968 | 969 | legend() 970 | grid(true) 971 | axis("equal") 972 | 973 | subplots(1) 974 | plot(rc, ".r", label="reeds shepp") 975 | grid(true) 976 | title("Curvature") 977 | 978 | show() 979 | 980 | println(PROGRAM_FILE," Done!!") 981 | end 982 | 983 | 984 | if length(PROGRAM_FILE)!=0 && 985 | contains(@__FILE__, PROGRAM_FILE) 986 | 987 | main() 988 | end 989 | 990 | end #module 991 | 992 | -------------------------------------------------------------------------------- /AutonomousParking/setup.jl: -------------------------------------------------------------------------------- 1 | ############### 2 | # OBCA: Optimization-based Collision Avoidance - a path planner for autonomous parking 3 | # Copyright (C) 2018 4 | # Alexander LINIGER [liniger@control.ee.ethz.ch; Automatic Control Lab, ETH Zurich] 5 | # Xiaojing ZHANG [xiaojing.zhang@berkeley.edu; MPC Lab, UC Berkeley] 6 | # 7 | # This program is free software: you can redistribute it and/or modify 8 | # it under the terms of the GNU General Public License as published by 9 | # the Free Software Foundation, either version 3 of the License, or 10 | # (at your option) any later version. 11 | # 12 | # This program is distributed in the hope that it will be useful, 13 | # but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 | # GNU General Public License for more details. 16 | # 17 | # You should have received a copy of the GNU General Public License 18 | # along with this program. If not, see . 19 | ############### 20 | # The paper describing the theory can be found here: 21 | # X. Zhang, A. Liniger and F. Borrelli; "Optimization-Based Collision Avoidance"; Technical Report, 2017, [https://arxiv.org/abs/1711.03449] 22 | ############### 23 | 24 | ############### 25 | # run this file before running main.jl 26 | ############### 27 | 28 | ############################## 29 | # include JuMP -> Optimization modeling tool 30 | # include IPOPT -> IP based NLP solver 31 | # include PyPlot -> Ploting library (matplotlib python) 32 | ############################## 33 | using JuMP, Ipopt, PyPlot, NearestNeighbors 34 | ############################## 35 | # register Distance and Signeddistance 36 | include("ParkingDist.jl") # should be the correct one 37 | include("ParkingSignedDist.jl") # good 38 | ############################## 39 | # register constraint satisfaction check 40 | include("ParkingConstraints.jl") 41 | ############################## 42 | # register polytope converter 43 | include("obstHrep.jl") 44 | ############################## 45 | # register ploting function 46 | include("plotTraj.jl") 47 | include("hybrid_a_star.jl") 48 | ############################## 49 | include("DualMultWS.jl") 50 | include("veloSmooth.jl") 51 | # function that clears terminal output 52 | clear() = run(@static is_unix() ? `clear` : `cmd /c cls`) 53 | -------------------------------------------------------------------------------- /AutonomousParking/veloSmooth.jl: -------------------------------------------------------------------------------- 1 | ############### 2 | # OBCA: Optimization-based Collision Avoidance - a path planner for autonomous parking 3 | # Copyright (C) 2018 4 | # Alexander LINIGER [liniger@control.ee.ethz.ch; Automatic Control Lab, ETH Zurich] 5 | # Xiaojing ZHANG [xiaojing.zhang@berkeley.edu; MPC Lab, UC Berkeley] 6 | # 7 | # This program is free software: you can redistribute it and/or modify 8 | # it under the terms of the GNU General Public License as published by 9 | # the Free Software Foundation, either version 3 of the License, or 10 | # (at your option) any later version. 11 | # 12 | # This program is distributed in the hope that it will be useful, 13 | # but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 | # GNU General Public License for more details. 16 | # 17 | # You should have received a copy of the GNU General Public License 18 | # along with this program. If not, see . 19 | ############### 20 | # The paper describing the theory can be found here: 21 | # X. Zhang, A. Liniger and F. Borrelli; "Optimization-Based Collision Avoidance"; Technical Report, 2017, [https://arxiv.org/abs/1711.03449] 22 | ############### 23 | 24 | ############### 25 | # veloSmooth: a velocity smoother 26 | ############### 27 | 28 | 29 | function veloSmooth(v,amax,Ts) 30 | v_ex = zeros(length(v)+40,1) 31 | v_bar = zeros(4,length(v)+40) 32 | v_bar2 = zeros(4,length(v)+40) 33 | v_barMM = zeros(1,length(v)) 34 | 35 | for i = 1:length(v) 36 | for j = 1:4 37 | v_bar[j,i+19] = v[i]; 38 | v_ex[i+19] = v[i]; 39 | end 40 | end 41 | 42 | v_cut1 = 0.25*abs(v[1]) 43 | v_cut2 = 0.25*abs(v[1])+abs(v[1]) 44 | 45 | accPhase = Int(round(abs(v[1])/amax/Ts)) 46 | 47 | index1 = find((diff(v_ex).>v_cut1) & (diff(v_ex).v_cut2) 49 | 50 | index3 = find((diff(v_ex).<-v_cut1) & (diff(v_ex).>-v_cut2)) 51 | index4 = find(diff(v_ex).<-v_cut2) 52 | 53 | if length(index1) >=1 && index1[1]==19 54 | index1[1] = index1[1]+1 55 | end 56 | if length(index3) >=1 && index3[1]==19 57 | index3[1] = index3[1]+1 58 | end 59 | 60 | 61 | for j = 1:length(index1) 62 | if v_ex[index1[j]] > v_cut1 || v_ex[index1[j]+1] > v_cut1 63 | v_bar[1,index1[j]:index1[j]+accPhase] = linspace(0,abs(v[1]),accPhase+1)'' 64 | elseif v_ex[index1[j]] < -v_cut1 || v_ex[index1[j]+1] < -v_cut1 65 | v_bar[1,index1[j]-accPhase+1:index1[j]+1] = linspace(-abs(v[1]),0,accPhase+1)'' 66 | end 67 | end 68 | 69 | for j = 1:length(index3) 70 | if v_ex[index3[j]] > v_cut1 || v_ex[index3[j]+1] > v_cut1 71 | v_bar[2,index3[j]-accPhase+1:index3[j]+1] = linspace(abs(v[1]),0,accPhase+1)'' 72 | elseif v_ex[index3[j]] < -v_cut1 || v_ex[index3[j]+1] < -v_cut1 73 | v_bar[2,index3[j]:index3[j]+accPhase] = linspace(0,-abs(v[1]),accPhase+1)'' 74 | end 75 | end 76 | 77 | for j = 1:length(index2) 78 | v_bar[3,index2[j]-accPhase:index2[j]+accPhase] = linspace(-abs(v[1]),abs(v[1]),2*accPhase+1)'' 79 | end 80 | 81 | for j = 1:length(index4) 82 | v_bar[4,index4[j]-accPhase:index4[j]+accPhase] = linspace(abs(v[1]),-abs(v[1]),2*accPhase+1)'' 83 | end 84 | 85 | for i = 20:length(v)+19 86 | for j = 1:4 87 | if v_bar[j,i] == 0 88 | v_bar2[j,i] = v_bar[j,i] 89 | elseif sign(v_ex[i]) != sign(v_bar[j,i]) 90 | v_bar2[j,i] = v_ex[i] 91 | else 92 | v_bar2[j,i] = v_bar[j,i] 93 | end 94 | end 95 | end 96 | 97 | for i = 20:length(v)+19 98 | if v_ex[i] > 0 99 | v_barMM[i-19] = minimum(v_bar2[:,i]) 100 | else 101 | v_barMM[i-19] = maximum(v_bar2[:,i]) 102 | end 103 | end 104 | 105 | a = diff(v_barMM')./Ts 106 | 107 | return v_barMM', a 108 | 109 | end 110 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | GNU GENERAL PUBLIC LICENSE 2 | Version 3, 29 June 2007 3 | 4 | Copyright (C) 2007 Free Software Foundation, Inc. 5 | Everyone is permitted to copy and distribute verbatim copies 6 | of this license document, but changing it is not allowed. 7 | 8 | Preamble 9 | 10 | The GNU General Public License is a free, copyleft license for 11 | software and other kinds of works. 12 | 13 | The licenses for most software and other practical works are designed 14 | to take away your freedom to share and change the works. By contrast, 15 | the GNU General Public License is intended to guarantee your freedom to 16 | share and change all versions of a program--to make sure it remains free 17 | software for all its users. We, the Free Software Foundation, use the 18 | GNU General Public License for most of our software; it applies also to 19 | any other work released this way by its authors. You can apply it to 20 | your programs, too. 21 | 22 | When we speak of free software, we are referring to freedom, not 23 | price. Our General Public Licenses are designed to make sure that you 24 | have the freedom to distribute copies of free software (and charge for 25 | them if you wish), that you receive source code or can get it if you 26 | want it, that you can change the software or use pieces of it in new 27 | free programs, and that you know you can do these things. 28 | 29 | To protect your rights, we need to prevent others from denying you 30 | these rights or asking you to surrender the rights. Therefore, you have 31 | certain responsibilities if you distribute copies of the software, or if 32 | you modify it: responsibilities to respect the freedom of others. 33 | 34 | For example, if you distribute copies of such a program, whether 35 | gratis or for a fee, you must pass on to the recipients the same 36 | freedoms that you received. You must make sure that they, too, receive 37 | or can get the source code. And you must show them these terms so they 38 | know their rights. 39 | 40 | Developers that use the GNU GPL protect your rights with two steps: 41 | (1) assert copyright on the software, and (2) offer you this License 42 | giving you legal permission to copy, distribute and/or modify it. 43 | 44 | For the developers' and authors' protection, the GPL clearly explains 45 | that there is no warranty for this free software. For both users' and 46 | authors' sake, the GPL requires that modified versions be marked as 47 | changed, so that their problems will not be attributed erroneously to 48 | authors of previous versions. 49 | 50 | Some devices are designed to deny users access to install or run 51 | modified versions of the software inside them, although the manufacturer 52 | can do so. This is fundamentally incompatible with the aim of 53 | protecting users' freedom to change the software. The systematic 54 | pattern of such abuse occurs in the area of products for individuals to 55 | use, which is precisely where it is most unacceptable. Therefore, we 56 | have designed this version of the GPL to prohibit the practice for those 57 | products. If such problems arise substantially in other domains, we 58 | stand ready to extend this provision to those domains in future versions 59 | of the GPL, as needed to protect the freedom of users. 60 | 61 | Finally, every program is threatened constantly by software patents. 62 | States should not allow patents to restrict development and use of 63 | software on general-purpose computers, but in those that do, we wish to 64 | avoid the special danger that patents applied to a free program could 65 | make it effectively proprietary. To prevent this, the GPL assures that 66 | patents cannot be used to render the program non-free. 67 | 68 | The precise terms and conditions for copying, distribution and 69 | modification follow. 70 | 71 | TERMS AND CONDITIONS 72 | 73 | 0. Definitions. 74 | 75 | "This License" refers to version 3 of the GNU General Public License. 76 | 77 | "Copyright" also means copyright-like laws that apply to other kinds of 78 | works, such as semiconductor masks. 79 | 80 | "The Program" refers to any copyrightable work licensed under this 81 | License. Each licensee is addressed as "you". "Licensees" and 82 | "recipients" may be individuals or organizations. 83 | 84 | To "modify" a work means to copy from or adapt all or part of the work 85 | in a fashion requiring copyright permission, other than the making of an 86 | exact copy. The resulting work is called a "modified version" of the 87 | earlier work or a work "based on" the earlier work. 88 | 89 | A "covered work" means either the unmodified Program or a work based 90 | on the Program. 91 | 92 | To "propagate" a work means to do anything with it that, without 93 | permission, would make you directly or secondarily liable for 94 | infringement under applicable copyright law, except executing it on a 95 | computer or modifying a private copy. Propagation includes copying, 96 | distribution (with or without modification), making available to the 97 | public, and in some countries other activities as well. 98 | 99 | To "convey" a work means any kind of propagation that enables other 100 | parties to make or receive copies. Mere interaction with a user through 101 | a computer network, with no transfer of a copy, is not conveying. 102 | 103 | An interactive user interface displays "Appropriate Legal Notices" 104 | to the extent that it includes a convenient and prominently visible 105 | feature that (1) displays an appropriate copyright notice, and (2) 106 | tells the user that there is no warranty for the work (except to the 107 | extent that warranties are provided), that licensees may convey the 108 | work under this License, and how to view a copy of this License. If 109 | the interface presents a list of user commands or options, such as a 110 | menu, a prominent item in the list meets this criterion. 111 | 112 | 1. Source Code. 113 | 114 | The "source code" for a work means the preferred form of the work 115 | for making modifications to it. "Object code" means any non-source 116 | form of a work. 117 | 118 | A "Standard Interface" means an interface that either is an official 119 | standard defined by a recognized standards body, or, in the case of 120 | interfaces specified for a particular programming language, one that 121 | is widely used among developers working in that language. 122 | 123 | The "System Libraries" of an executable work include anything, other 124 | than the work as a whole, that (a) is included in the normal form of 125 | packaging a Major Component, but which is not part of that Major 126 | Component, and (b) serves only to enable use of the work with that 127 | Major Component, or to implement a Standard Interface for which an 128 | implementation is available to the public in source code form. A 129 | "Major Component", in this context, means a major essential component 130 | (kernel, window system, and so on) of the specific operating system 131 | (if any) on which the executable work runs, or a compiler used to 132 | produce the work, or an object code interpreter used to run it. 133 | 134 | The "Corresponding Source" for a work in object code form means all 135 | the source code needed to generate, install, and (for an executable 136 | work) run the object code and to modify the work, including scripts to 137 | control those activities. However, it does not include the work's 138 | System Libraries, or general-purpose tools or generally available free 139 | programs which are used unmodified in performing those activities but 140 | which are not part of the work. For example, Corresponding Source 141 | includes interface definition files associated with source files for 142 | the work, and the source code for shared libraries and dynamically 143 | linked subprograms that the work is specifically designed to require, 144 | such as by intimate data communication or control flow between those 145 | subprograms and other parts of the work. 146 | 147 | The Corresponding Source need not include anything that users 148 | can regenerate automatically from other parts of the Corresponding 149 | Source. 150 | 151 | The Corresponding Source for a work in source code form is that 152 | same work. 153 | 154 | 2. Basic Permissions. 155 | 156 | All rights granted under this License are granted for the term of 157 | copyright on the Program, and are irrevocable provided the stated 158 | conditions are met. This License explicitly affirms your unlimited 159 | permission to run the unmodified Program. The output from running a 160 | covered work is covered by this License only if the output, given its 161 | content, constitutes a covered work. This License acknowledges your 162 | rights of fair use or other equivalent, as provided by copyright law. 163 | 164 | You may make, run and propagate covered works that you do not 165 | convey, without conditions so long as your license otherwise remains 166 | in force. You may convey covered works to others for the sole purpose 167 | of having them make modifications exclusively for you, or provide you 168 | with facilities for running those works, provided that you comply with 169 | the terms of this License in conveying all material for which you do 170 | not control copyright. Those thus making or running the covered works 171 | for you must do so exclusively on your behalf, under your direction 172 | and control, on terms that prohibit them from making any copies of 173 | your copyrighted material outside their relationship with you. 174 | 175 | Conveying under any other circumstances is permitted solely under 176 | the conditions stated below. Sublicensing is not allowed; section 10 177 | makes it unnecessary. 178 | 179 | 3. Protecting Users' Legal Rights From Anti-Circumvention Law. 180 | 181 | No covered work shall be deemed part of an effective technological 182 | measure under any applicable law fulfilling obligations under article 183 | 11 of the WIPO copyright treaty adopted on 20 December 1996, or 184 | similar laws prohibiting or restricting circumvention of such 185 | measures. 186 | 187 | When you convey a covered work, you waive any legal power to forbid 188 | circumvention of technological measures to the extent such circumvention 189 | is effected by exercising rights under this License with respect to 190 | the covered work, and you disclaim any intention to limit operation or 191 | modification of the work as a means of enforcing, against the work's 192 | users, your or third parties' legal rights to forbid circumvention of 193 | technological measures. 194 | 195 | 4. Conveying Verbatim Copies. 196 | 197 | You may convey verbatim copies of the Program's source code as you 198 | receive it, in any medium, provided that you conspicuously and 199 | appropriately publish on each copy an appropriate copyright notice; 200 | keep intact all notices stating that this License and any 201 | non-permissive terms added in accord with section 7 apply to the code; 202 | keep intact all notices of the absence of any warranty; and give all 203 | recipients a copy of this License along with the Program. 204 | 205 | You may charge any price or no price for each copy that you convey, 206 | and you may offer support or warranty protection for a fee. 207 | 208 | 5. Conveying Modified Source Versions. 209 | 210 | You may convey a work based on the Program, or the modifications to 211 | produce it from the Program, in the form of source code under the 212 | terms of section 4, provided that you also meet all of these conditions: 213 | 214 | a) The work must carry prominent notices stating that you modified 215 | it, and giving a relevant date. 216 | 217 | b) The work must carry prominent notices stating that it is 218 | released under this License and any conditions added under section 219 | 7. This requirement modifies the requirement in section 4 to 220 | "keep intact all notices". 221 | 222 | c) You must license the entire work, as a whole, under this 223 | License to anyone who comes into possession of a copy. This 224 | License will therefore apply, along with any applicable section 7 225 | additional terms, to the whole of the work, and all its parts, 226 | regardless of how they are packaged. This License gives no 227 | permission to license the work in any other way, but it does not 228 | invalidate such permission if you have separately received it. 229 | 230 | d) If the work has interactive user interfaces, each must display 231 | Appropriate Legal Notices; however, if the Program has interactive 232 | interfaces that do not display Appropriate Legal Notices, your 233 | work need not make them do so. 234 | 235 | A compilation of a covered work with other separate and independent 236 | works, which are not by their nature extensions of the covered work, 237 | and which are not combined with it such as to form a larger program, 238 | in or on a volume of a storage or distribution medium, is called an 239 | "aggregate" if the compilation and its resulting copyright are not 240 | used to limit the access or legal rights of the compilation's users 241 | beyond what the individual works permit. Inclusion of a covered work 242 | in an aggregate does not cause this License to apply to the other 243 | parts of the aggregate. 244 | 245 | 6. Conveying Non-Source Forms. 246 | 247 | You may convey a covered work in object code form under the terms 248 | of sections 4 and 5, provided that you also convey the 249 | machine-readable Corresponding Source under the terms of this License, 250 | in one of these ways: 251 | 252 | a) Convey the object code in, or embodied in, a physical product 253 | (including a physical distribution medium), accompanied by the 254 | Corresponding Source fixed on a durable physical medium 255 | customarily used for software interchange. 256 | 257 | b) Convey the object code in, or embodied in, a physical product 258 | (including a physical distribution medium), accompanied by a 259 | written offer, valid for at least three years and valid for as 260 | long as you offer spare parts or customer support for that product 261 | model, to give anyone who possesses the object code either (1) a 262 | copy of the Corresponding Source for all the software in the 263 | product that is covered by this License, on a durable physical 264 | medium customarily used for software interchange, for a price no 265 | more than your reasonable cost of physically performing this 266 | conveying of source, or (2) access to copy the 267 | Corresponding Source from a network server at no charge. 268 | 269 | c) Convey individual copies of the object code with a copy of the 270 | written offer to provide the Corresponding Source. This 271 | alternative is allowed only occasionally and noncommercially, and 272 | only if you received the object code with such an offer, in accord 273 | with subsection 6b. 274 | 275 | d) Convey the object code by offering access from a designated 276 | place (gratis or for a charge), and offer equivalent access to the 277 | Corresponding Source in the same way through the same place at no 278 | further charge. You need not require recipients to copy the 279 | Corresponding Source along with the object code. If the place to 280 | copy the object code is a network server, the Corresponding Source 281 | may be on a different server (operated by you or a third party) 282 | that supports equivalent copying facilities, provided you maintain 283 | clear directions next to the object code saying where to find the 284 | Corresponding Source. Regardless of what server hosts the 285 | Corresponding Source, you remain obligated to ensure that it is 286 | available for as long as needed to satisfy these requirements. 287 | 288 | e) Convey the object code using peer-to-peer transmission, provided 289 | you inform other peers where the object code and Corresponding 290 | Source of the work are being offered to the general public at no 291 | charge under subsection 6d. 292 | 293 | A separable portion of the object code, whose source code is excluded 294 | from the Corresponding Source as a System Library, need not be 295 | included in conveying the object code work. 296 | 297 | A "User Product" is either (1) a "consumer product", which means any 298 | tangible personal property which is normally used for personal, family, 299 | or household purposes, or (2) anything designed or sold for incorporation 300 | into a dwelling. In determining whether a product is a consumer product, 301 | doubtful cases shall be resolved in favor of coverage. For a particular 302 | product received by a particular user, "normally used" refers to a 303 | typical or common use of that class of product, regardless of the status 304 | of the particular user or of the way in which the particular user 305 | actually uses, or expects or is expected to use, the product. A product 306 | is a consumer product regardless of whether the product has substantial 307 | commercial, industrial or non-consumer uses, unless such uses represent 308 | the only significant mode of use of the product. 309 | 310 | "Installation Information" for a User Product means any methods, 311 | procedures, authorization keys, or other information required to install 312 | and execute modified versions of a covered work in that User Product from 313 | a modified version of its Corresponding Source. The information must 314 | suffice to ensure that the continued functioning of the modified object 315 | code is in no case prevented or interfered with solely because 316 | modification has been made. 317 | 318 | If you convey an object code work under this section in, or with, or 319 | specifically for use in, a User Product, and the conveying occurs as 320 | part of a transaction in which the right of possession and use of the 321 | User Product is transferred to the recipient in perpetuity or for a 322 | fixed term (regardless of how the transaction is characterized), the 323 | Corresponding Source conveyed under this section must be accompanied 324 | by the Installation Information. But this requirement does not apply 325 | if neither you nor any third party retains the ability to install 326 | modified object code on the User Product (for example, the work has 327 | been installed in ROM). 328 | 329 | The requirement to provide Installation Information does not include a 330 | requirement to continue to provide support service, warranty, or updates 331 | for a work that has been modified or installed by the recipient, or for 332 | the User Product in which it has been modified or installed. Access to a 333 | network may be denied when the modification itself materially and 334 | adversely affects the operation of the network or violates the rules and 335 | protocols for communication across the network. 336 | 337 | Corresponding Source conveyed, and Installation Information provided, 338 | in accord with this section must be in a format that is publicly 339 | documented (and with an implementation available to the public in 340 | source code form), and must require no special password or key for 341 | unpacking, reading or copying. 342 | 343 | 7. Additional Terms. 344 | 345 | "Additional permissions" are terms that supplement the terms of this 346 | License by making exceptions from one or more of its conditions. 347 | Additional permissions that are applicable to the entire Program shall 348 | be treated as though they were included in this License, to the extent 349 | that they are valid under applicable law. If additional permissions 350 | apply only to part of the Program, that part may be used separately 351 | under those permissions, but the entire Program remains governed by 352 | this License without regard to the additional permissions. 353 | 354 | When you convey a copy of a covered work, you may at your option 355 | remove any additional permissions from that copy, or from any part of 356 | it. (Additional permissions may be written to require their own 357 | removal in certain cases when you modify the work.) You may place 358 | additional permissions on material, added by you to a covered work, 359 | for which you have or can give appropriate copyright permission. 360 | 361 | Notwithstanding any other provision of this License, for material you 362 | add to a covered work, you may (if authorized by the copyright holders of 363 | that material) supplement the terms of this License with terms: 364 | 365 | a) Disclaiming warranty or limiting liability differently from the 366 | terms of sections 15 and 16 of this License; or 367 | 368 | b) Requiring preservation of specified reasonable legal notices or 369 | author attributions in that material or in the Appropriate Legal 370 | Notices displayed by works containing it; or 371 | 372 | c) Prohibiting misrepresentation of the origin of that material, or 373 | requiring that modified versions of such material be marked in 374 | reasonable ways as different from the original version; or 375 | 376 | d) Limiting the use for publicity purposes of names of licensors or 377 | authors of the material; or 378 | 379 | e) Declining to grant rights under trademark law for use of some 380 | trade names, trademarks, or service marks; or 381 | 382 | f) Requiring indemnification of licensors and authors of that 383 | material by anyone who conveys the material (or modified versions of 384 | it) with contractual assumptions of liability to the recipient, for 385 | any liability that these contractual assumptions directly impose on 386 | those licensors and authors. 387 | 388 | All other non-permissive additional terms are considered "further 389 | restrictions" within the meaning of section 10. If the Program as you 390 | received it, or any part of it, contains a notice stating that it is 391 | governed by this License along with a term that is a further 392 | restriction, you may remove that term. If a license document contains 393 | a further restriction but permits relicensing or conveying under this 394 | License, you may add to a covered work material governed by the terms 395 | of that license document, provided that the further restriction does 396 | not survive such relicensing or conveying. 397 | 398 | If you add terms to a covered work in accord with this section, you 399 | must place, in the relevant source files, a statement of the 400 | additional terms that apply to those files, or a notice indicating 401 | where to find the applicable terms. 402 | 403 | Additional terms, permissive or non-permissive, may be stated in the 404 | form of a separately written license, or stated as exceptions; 405 | the above requirements apply either way. 406 | 407 | 8. Termination. 408 | 409 | You may not propagate or modify a covered work except as expressly 410 | provided under this License. Any attempt otherwise to propagate or 411 | modify it is void, and will automatically terminate your rights under 412 | this License (including any patent licenses granted under the third 413 | paragraph of section 11). 414 | 415 | However, if you cease all violation of this License, then your 416 | license from a particular copyright holder is reinstated (a) 417 | provisionally, unless and until the copyright holder explicitly and 418 | finally terminates your license, and (b) permanently, if the copyright 419 | holder fails to notify you of the violation by some reasonable means 420 | prior to 60 days after the cessation. 421 | 422 | Moreover, your license from a particular copyright holder is 423 | reinstated permanently if the copyright holder notifies you of the 424 | violation by some reasonable means, this is the first time you have 425 | received notice of violation of this License (for any work) from that 426 | copyright holder, and you cure the violation prior to 30 days after 427 | your receipt of the notice. 428 | 429 | Termination of your rights under this section does not terminate the 430 | licenses of parties who have received copies or rights from you under 431 | this License. If your rights have been terminated and not permanently 432 | reinstated, you do not qualify to receive new licenses for the same 433 | material under section 10. 434 | 435 | 9. Acceptance Not Required for Having Copies. 436 | 437 | You are not required to accept this License in order to receive or 438 | run a copy of the Program. Ancillary propagation of a covered work 439 | occurring solely as a consequence of using peer-to-peer transmission 440 | to receive a copy likewise does not require acceptance. However, 441 | nothing other than this License grants you permission to propagate or 442 | modify any covered work. These actions infringe copyright if you do 443 | not accept this License. Therefore, by modifying or propagating a 444 | covered work, you indicate your acceptance of this License to do so. 445 | 446 | 10. Automatic Licensing of Downstream Recipients. 447 | 448 | Each time you convey a covered work, the recipient automatically 449 | receives a license from the original licensors, to run, modify and 450 | propagate that work, subject to this License. You are not responsible 451 | for enforcing compliance by third parties with this License. 452 | 453 | An "entity transaction" is a transaction transferring control of an 454 | organization, or substantially all assets of one, or subdividing an 455 | organization, or merging organizations. If propagation of a covered 456 | work results from an entity transaction, each party to that 457 | transaction who receives a copy of the work also receives whatever 458 | licenses to the work the party's predecessor in interest had or could 459 | give under the previous paragraph, plus a right to possession of the 460 | Corresponding Source of the work from the predecessor in interest, if 461 | the predecessor has it or can get it with reasonable efforts. 462 | 463 | You may not impose any further restrictions on the exercise of the 464 | rights granted or affirmed under this License. For example, you may 465 | not impose a license fee, royalty, or other charge for exercise of 466 | rights granted under this License, and you may not initiate litigation 467 | (including a cross-claim or counterclaim in a lawsuit) alleging that 468 | any patent claim is infringed by making, using, selling, offering for 469 | sale, or importing the Program or any portion of it. 470 | 471 | 11. Patents. 472 | 473 | A "contributor" is a copyright holder who authorizes use under this 474 | License of the Program or a work on which the Program is based. The 475 | work thus licensed is called the contributor's "contributor version". 476 | 477 | A contributor's "essential patent claims" are all patent claims 478 | owned or controlled by the contributor, whether already acquired or 479 | hereafter acquired, that would be infringed by some manner, permitted 480 | by this License, of making, using, or selling its contributor version, 481 | but do not include claims that would be infringed only as a 482 | consequence of further modification of the contributor version. For 483 | purposes of this definition, "control" includes the right to grant 484 | patent sublicenses in a manner consistent with the requirements of 485 | this License. 486 | 487 | Each contributor grants you a non-exclusive, worldwide, royalty-free 488 | patent license under the contributor's essential patent claims, to 489 | make, use, sell, offer for sale, import and otherwise run, modify and 490 | propagate the contents of its contributor version. 491 | 492 | In the following three paragraphs, a "patent license" is any express 493 | agreement or commitment, however denominated, not to enforce a patent 494 | (such as an express permission to practice a patent or covenant not to 495 | sue for patent infringement). To "grant" such a patent license to a 496 | party means to make such an agreement or commitment not to enforce a 497 | patent against the party. 498 | 499 | If you convey a covered work, knowingly relying on a patent license, 500 | and the Corresponding Source of the work is not available for anyone 501 | to copy, free of charge and under the terms of this License, through a 502 | publicly available network server or other readily accessible means, 503 | then you must either (1) cause the Corresponding Source to be so 504 | available, or (2) arrange to deprive yourself of the benefit of the 505 | patent license for this particular work, or (3) arrange, in a manner 506 | consistent with the requirements of this License, to extend the patent 507 | license to downstream recipients. "Knowingly relying" means you have 508 | actual knowledge that, but for the patent license, your conveying the 509 | covered work in a country, or your recipient's use of the covered work 510 | in a country, would infringe one or more identifiable patents in that 511 | country that you have reason to believe are valid. 512 | 513 | If, pursuant to or in connection with a single transaction or 514 | arrangement, you convey, or propagate by procuring conveyance of, a 515 | covered work, and grant a patent license to some of the parties 516 | receiving the covered work authorizing them to use, propagate, modify 517 | or convey a specific copy of the covered work, then the patent license 518 | you grant is automatically extended to all recipients of the covered 519 | work and works based on it. 520 | 521 | A patent license is "discriminatory" if it does not include within 522 | the scope of its coverage, prohibits the exercise of, or is 523 | conditioned on the non-exercise of one or more of the rights that are 524 | specifically granted under this License. You may not convey a covered 525 | work if you are a party to an arrangement with a third party that is 526 | in the business of distributing software, under which you make payment 527 | to the third party based on the extent of your activity of conveying 528 | the work, and under which the third party grants, to any of the 529 | parties who would receive the covered work from you, a discriminatory 530 | patent license (a) in connection with copies of the covered work 531 | conveyed by you (or copies made from those copies), or (b) primarily 532 | for and in connection with specific products or compilations that 533 | contain the covered work, unless you entered into that arrangement, 534 | or that patent license was granted, prior to 28 March 2007. 535 | 536 | Nothing in this License shall be construed as excluding or limiting 537 | any implied license or other defenses to infringement that may 538 | otherwise be available to you under applicable patent law. 539 | 540 | 12. No Surrender of Others' Freedom. 541 | 542 | If conditions are imposed on you (whether by court order, agreement or 543 | otherwise) that contradict the conditions of this License, they do not 544 | excuse you from the conditions of this License. If you cannot convey a 545 | covered work so as to satisfy simultaneously your obligations under this 546 | License and any other pertinent obligations, then as a consequence you may 547 | not convey it at all. For example, if you agree to terms that obligate you 548 | to collect a royalty for further conveying from those to whom you convey 549 | the Program, the only way you could satisfy both those terms and this 550 | License would be to refrain entirely from conveying the Program. 551 | 552 | 13. Use with the GNU Affero General Public License. 553 | 554 | Notwithstanding any other provision of this License, you have 555 | permission to link or combine any covered work with a work licensed 556 | under version 3 of the GNU Affero General Public License into a single 557 | combined work, and to convey the resulting work. The terms of this 558 | License will continue to apply to the part which is the covered work, 559 | but the special requirements of the GNU Affero General Public License, 560 | section 13, concerning interaction through a network will apply to the 561 | combination as such. 562 | 563 | 14. Revised Versions of this License. 564 | 565 | The Free Software Foundation may publish revised and/or new versions of 566 | the GNU General Public License from time to time. Such new versions will 567 | be similar in spirit to the present version, but may differ in detail to 568 | address new problems or concerns. 569 | 570 | Each version is given a distinguishing version number. If the 571 | Program specifies that a certain numbered version of the GNU General 572 | Public License "or any later version" applies to it, you have the 573 | option of following the terms and conditions either of that numbered 574 | version or of any later version published by the Free Software 575 | Foundation. If the Program does not specify a version number of the 576 | GNU General Public License, you may choose any version ever published 577 | by the Free Software Foundation. 578 | 579 | If the Program specifies that a proxy can decide which future 580 | versions of the GNU General Public License can be used, that proxy's 581 | public statement of acceptance of a version permanently authorizes you 582 | to choose that version for the Program. 583 | 584 | Later license versions may give you additional or different 585 | permissions. However, no additional obligations are imposed on any 586 | author or copyright holder as a result of your choosing to follow a 587 | later version. 588 | 589 | 15. Disclaimer of Warranty. 590 | 591 | THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY 592 | APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT 593 | HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY 594 | OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, 595 | THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 596 | PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE PROGRAM 597 | IS WITH YOU. SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF 598 | ALL NECESSARY SERVICING, REPAIR OR CORRECTION. 599 | 600 | 16. Limitation of Liability. 601 | 602 | IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING 603 | WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MODIFIES AND/OR CONVEYS 604 | THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY 605 | GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE 606 | USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED TO LOSS OF 607 | DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD 608 | PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS), 609 | EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF 610 | SUCH DAMAGES. 611 | 612 | 17. Interpretation of Sections 15 and 16. 613 | 614 | If the disclaimer of warranty and limitation of liability provided 615 | above cannot be given local legal effect according to their terms, 616 | reviewing courts shall apply local law that most closely approximates 617 | an absolute waiver of all civil liability in connection with the 618 | Program, unless a warranty or assumption of liability accompanies a 619 | copy of the Program in return for a fee. 620 | 621 | END OF TERMS AND CONDITIONS 622 | 623 | How to Apply These Terms to Your New Programs 624 | 625 | If you develop a new program, and you want it to be of the greatest 626 | possible use to the public, the best way to achieve this is to make it 627 | free software which everyone can redistribute and change under these terms. 628 | 629 | To do so, attach the following notices to the program. It is safest 630 | to attach them to the start of each source file to most effectively 631 | state the exclusion of warranty; and each file should have at least 632 | the "copyright" line and a pointer to where the full notice is found. 633 | 634 | 635 | Copyright (C) 636 | 637 | This program is free software: you can redistribute it and/or modify 638 | it under the terms of the GNU General Public License as published by 639 | the Free Software Foundation, either version 3 of the License, or 640 | (at your option) any later version. 641 | 642 | This program is distributed in the hope that it will be useful, 643 | but WITHOUT ANY WARRANTY; without even the implied warranty of 644 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 645 | GNU General Public License for more details. 646 | 647 | You should have received a copy of the GNU General Public License 648 | along with this program. If not, see . 649 | 650 | Also add information on how to contact you by electronic and paper mail. 651 | 652 | If the program does terminal interaction, make it output a short 653 | notice like this when it starts in an interactive mode: 654 | 655 | Copyright (C) 656 | This program comes with ABSOLUTELY NO WARRANTY; for details type `show w'. 657 | This is free software, and you are welcome to redistribute it 658 | under certain conditions; type `show c' for details. 659 | 660 | The hypothetical commands `show w' and `show c' should show the appropriate 661 | parts of the General Public License. Of course, your program's commands 662 | might be different; for a GUI interface, you would use an "about box". 663 | 664 | You should also get your employer (if you work as a programmer) or school, 665 | if any, to sign a "copyright disclaimer" for the program, if necessary. 666 | For more information on this, and how to apply and follow the GNU GPL, see 667 | . 668 | 669 | The GNU General Public License does not permit incorporating your program 670 | into proprietary programs. If your program is a subroutine library, you 671 | may consider it more useful to permit linking proprietary applications with 672 | the library. If this is what you want to do, use the GNU Lesser General 673 | Public License instead of this License. But first, please read 674 | . 675 | -------------------------------------------------------------------------------- /QuadcopterNavigation/QuadcopterDist.jl: -------------------------------------------------------------------------------- 1 | ############### 2 | # OBCA: Optimization-based Collision Avoidance - a path planner for autonomous parking 3 | # Copyright (C) 2018 4 | # Alexander LINIGER [liniger@control.ee.ethz.ch; Automatic Control Lab, ETH Zurich] 5 | # Xiaojing ZHANG [xiaojing.zhang@berkeley.edu; MPC Lab, UC Berkeley] 6 | # 7 | # This program is free software: you can redistribute it and/or modify 8 | # it under the terms of the GNU General Public License as published by 9 | # the Free Software Foundation, either version 3 of the License, or 10 | # (at your option) any later version. 11 | # 12 | # This program is distributed in the hope that it will be useful, 13 | # but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 | # GNU General Public License for more details. 16 | # 17 | # You should have received a copy of the GNU General Public License 18 | # along with this program. If not, see . 19 | ############### 20 | # The paper describing the theory can be found here: 21 | # X. Zhang, A. Liniger and F. Borrelli; "Optimization-Based Collision Avoidance"; Technical Report, 2017, [https://arxiv.org/abs/1711.03449] 22 | ############### 23 | 24 | 25 | function QuadcopterDist(x0,xF,N,Ts,R,ob1,ob2,ob3,ob4,ob5,xWS,uWS,timeWS) 26 | 27 | m = Model(solver=IpoptSolver(hessian_approximation="exact",mumps_pivtol=5e-7,mumps_pivtolmax=0.1,mumps_mem_percent=10000, 28 | recalc_y="no",alpha_for_y="min",required_infeasibility_reduction=0.65, 29 | min_hessian_perturbation=1e-10,jacobian_regularization_value=1e-7,tol=1e-5, 30 | print_level=0))#state 31 | 32 | 33 | @variable(m, x[1:12,1:(N+1)]) 34 | @variable(m, timeScale[1:N+1]) 35 | #control 36 | @variable(m, u[1:4,1:(N)]) 37 | # lagrange multipliers for dual dist function 38 | @variable(m, l1[1:6,1:(N+1)]) 39 | 40 | @variable(m, l2[1:6,1:(N+1)]) 41 | 42 | @variable(m, l3[1:6,1:(N+1)]) 43 | 44 | @variable(m, l4[1:6,1:(N+1)]) 45 | 46 | @variable(m, l5[1:6,1:(N+1)]) 47 | 48 | mass = 0.5; 49 | g = 9.81; 50 | reg = 0; 51 | reg2 = 1e-4; 52 | reg3 = 0.0001; 53 | 54 | k_F = 0.0611;#6.11*1e-8 #[N/rpm^2] 55 | k_M = 0.0015;#1.5*1e-9 #[Nm/rpm^2] 56 | I = [3.9,4.4,4.9]*1e-3 #[kg/m2] 57 | L = 0.225 #[m] 58 | 59 | w_H = sqrt((mass*g)/(k_F*4)) 60 | 61 | # cost function 62 | @NLobjective(m, Min,1e-3*sum( sum((w_H-u[j,i])^2 for j=1:4) for i = 1:N) + 63 | 1e-2*sum( sum((u[j,i]-u[j,i+1])^2 for j=1:4) for i = 1:N-1) + 64 | 1*sum(sum(reg3*x[j,i]^2 for i = 1:N+1) for j = [10,11,12]) + 65 | sum(0.25*timeScale[i] + 5*timeScale[i]^2 for i = 1:N+1) + 66 | 1*sum(sum(reg2*l1[j,i]^2 + reg2*l2[j,i]^2 + reg2*l3[j,i]^2 + reg2*l4[j,i]^2+ reg2*l5[j,i]^2 for i = 1:N+1) for j = 1:6)); 67 | 68 | #input constraints 69 | @constraint(m, [i=1:N], 1.200 <= u[1,i] <= 7.800) 70 | @constraint(m, [i=1:N], 1.200 <= u[2,i] <= 7.800) 71 | @constraint(m, [i=1:N], 1.200 <= u[3,i] <= 7.800) 72 | @constraint(m, [i=1:N], 1.200 <= u[4,i] <= 7.800) 73 | 74 | #state constraints 75 | #X,Y,Z 76 | @constraint(m, [i=1:N+1], 0 <= x[1,i] <= 10) 77 | @constraint(m, [i=1:N+1], 0 <= x[2,i] <= 10) # -0.1 <= x[2,i] <= 20 78 | @constraint(m, [i=1:N+1], 0 <= x[3,i] <= 5) 79 | # pitch, roll 80 | @constraint(m, [i=1:N+1], -3 <= x[4,i] <= 3) 81 | @constraint(m, [i=1:N+1], -0.2 <= x[5,i] <= 0.2) #pm 0.2 82 | @constraint(m, [i=1:N+1], -0.2 <= x[6,i] <= 0.2) 83 | #v_x, v_y, v_z 84 | @constraint(m, [i=1:N+1],-1 <= x[7,i] <= 1) 85 | @constraint(m, [i=1:N+1],-1 <= x[8,i] <= 1) 86 | @constraint(m, [i=1:N+1],-1 <= x[9,i] <= 1) 87 | # pitch_rate, roll_rate 88 | @constraint(m, [i=1:N+1],-1.5 <= x[10,i] <= 3) #pm 1 89 | @constraint(m, [i=1:N+1],-1 <= x[11,i] <= 1) 90 | @constraint(m, [i=1:N+1],-1 <= x[12,i] <= 1) 91 | 92 | @constraint(m, 0.5 .<= timeScale .<= 2) # original: 0.5 <= .... <=2 93 | # positivity constraints on lambda 94 | @constraint(m, l1.>= 0) 95 | @constraint(m, l2.>= 0) 96 | @constraint(m, l3.>= 0) 97 | @constraint(m, l4.>= 0) 98 | @constraint(m, l5.>= 0) 99 | 100 | 101 | #starting point 102 | @constraint(m, x[1,1] == x0[1]) 103 | @constraint(m, x[2,1] == x0[2]) 104 | @constraint(m, x[3,1] == x0[3]) 105 | @constraint(m, x[4,1] == x0[4]) 106 | @constraint(m, x[5,1] == x0[5]) 107 | @constraint(m, x[6,1] == x0[6]) 108 | @constraint(m, x[7,1] == x0[7]) 109 | @constraint(m, x[8,1] == x0[8]) 110 | @constraint(m, x[9,1] == x0[9]) 111 | @constraint(m, x[10,1] == x0[10]) 112 | @constraint(m, x[11,1] == x0[11]) 113 | @constraint(m, x[12,1] == x0[12]) 114 | 115 | 116 | #end point 117 | @constraint(m, x[1,N+1] == xF[1]) 118 | @constraint(m, x[2,N+1] == xF[2]) 119 | @constraint(m, x[3,N+1] == xF[3]) 120 | @constraint(m, x[4,N+1] == xF[4]) 121 | @constraint(m, x[5,N+1] == xF[5]) 122 | @constraint(m, x[6,N+1] == xF[6]) 123 | @constraint(m, x[7,N+1] == xF[7]) 124 | @constraint(m, x[8,N+1] == xF[8]) 125 | @constraint(m, x[9,N+1] == xF[9]) 126 | @constraint(m, x[10,N+1] == xF[10]) 127 | @constraint(m, x[11,N+1] == xF[11]) 128 | @constraint(m, x[12,N+1] == xF[12]) 129 | 130 | for i in 1:N 131 | #X,Y,Z 132 | @NLconstraint(m, x[1,i+1] == x[1,i] + timeScale[i]*Ts*x[7,i]) 133 | @NLconstraint(m, x[2,i+1] == x[2,i] + timeScale[i]*Ts*x[8,i]) 134 | @NLconstraint(m, x[3,i+1] == x[3,i] + timeScale[i]*Ts*x[9,i]) 135 | 136 | # pitch, roll, yaw 137 | @NLconstraint(m, x[4,i+1] == x[4,i] + timeScale[i]*Ts*( cos(x[5,i]) *x[10,i] +sin(x[5,i]) *x[12,i])) 138 | @NLconstraint(m, x[5,i+1] == x[5,i] + timeScale[i]*Ts*( sin(x[5,i])*tan(x[4,i])*x[10,i]+x[11,i]-cos(x[5,i])*tan(x[4,i])*x[12,i])) 139 | @NLconstraint(m, x[6,i+1] == x[6,i] + timeScale[i]*Ts*(-sin(x[5,i])*sec(x[4,i])*x[10,i] +cos(x[5,i])*sec(x[4,i])*x[12,i])) 140 | 141 | #v_x, v_y, v_z 142 | @NLconstraint(m, x[7,i+1] == x[7,i] + timeScale[i]*Ts*1/mass*(sum(k_F*u[j,i]^2 for j=1:4)*( sin(x[4,i])*cos(x[5,i])*sin(x[6,i]) + sin(x[5,i])*cos(x[6,i]) ))) 143 | @NLconstraint(m, x[8,i+1] == x[8,i] + timeScale[i]*Ts*1/mass*(sum(k_F*u[j,i]^2 for j=1:4)*(-sin(x[4,i])*cos(x[5,i])*cos(x[6,i]) + sin(x[5,i])*sin(x[6,i]) ))) 144 | @NLconstraint(m, x[9,i+1] == x[9,i] + timeScale[i]*Ts*1/mass*(sum(k_F*u[j,i]^2 for j=1:4)*( cos(x[4,i])*cos(x[5,i])) - mass*g )) 145 | 146 | # pitch_rate, roll_rate 147 | @NLconstraint(m, x[10,i+1] == x[10,i] + timeScale[i]*Ts*1/I[1]*(L*k_F*(u[2,i]^2 - u[4,i]^2) - (I[3] - I[2])*x[11]*x[12])) 148 | @NLconstraint(m, x[11,i+1] == x[11,i] + timeScale[i]*Ts*1/I[2]*(L*k_F*(u[3,i]^2 - u[1,i]^2) - (I[1] - I[3])*x[10]*x[12])) 149 | @NLconstraint(m, x[12,i+1] == x[12,i] + timeScale[i]*Ts*1/I[3]*(k_M*(u[1,i]^2 - u[2,i]^2 + u[3,i]^2 - u[4,i]^2) - (I[2] - I[1])*x[10]*x[11])) 150 | 151 | @constraint(m, timeScale[i] == timeScale[i+1]) 152 | end 153 | 154 | 155 | 156 | A = [eye(3); 157 | -eye(3)]; 158 | 159 | for i in 1:N+1 160 | # rotation matrix 161 | 162 | b1 = ob1 163 | @NLconstraint(m, (l1[1,i]-l1[4,i])^2 + (l1[2,i]-l1[5,i])^2 + (l1[3,i]-l1[6,i])^2 == 1) # == (sd), <= (d) 164 | @NLconstraint(m,sum(-b1[j]*l1[j,i] for j = 1:6) + x[1,i]*sum(A[j,1]*l1[j,i] for j=1:6) + 165 | x[2,i]*sum(A[j,2]*l1[j,i] for j=1:6) + x[3,i]*sum(A[j,3]*l1[j,i] for j=1:6) >=R) 166 | 167 | ###################### 168 | b2 = ob2 169 | @NLconstraint(m, (l2[1,i]-l2[4,i])^2 + (l2[2,i]-l2[5,i])^2 + (l2[3,i]-l2[6,i])^2 == 1) # == 170 | @NLconstraint(m,sum(-b2[j]*l2[j,i] for j = 1:6) + x[1,i]*sum(A[j,1]*l2[j,i] for j=1:6) + 171 | x[2,i]*sum(A[j,2]*l2[j,i] for j=1:6) + x[3,i]*sum(A[j,3]*l2[j,i] for j=1:6) >=R) 172 | 173 | ######################### 174 | b3 = ob3 175 | @NLconstraint(m, (l3[1,i]-l3[4,i])^2 + (l3[2,i]-l3[5,i])^2 + (l3[3,i]-l3[6,i])^2 == 1) # == 176 | @NLconstraint(m,sum(-b3[j]*l3[j,i] for j = 1:6) + x[1,i]*sum(A[j,1]*l3[j,i] for j=1:6) + 177 | x[2,i]*sum(A[j,2]*l3[j,i] for j=1:6) + x[3,i]*sum(A[j,3]*l3[j,i] for j=1:6) >=R) 178 | 179 | ######################### 180 | b4 = ob4 181 | @NLconstraint(m, (l4[1,i]-l4[4,i])^2 + (l4[2,i]-l4[5,i])^2 + (l4[3,i]-l4[6,i])^2 == 1) # == 182 | @NLconstraint(m,sum(-b4[j]*l4[j,i] for j = 1:6) + x[1,i]*sum(A[j,1]*l4[j,i] for j=1:6) + 183 | x[2,i]*sum(A[j,2]*l4[j,i] for j=1:6) + x[3,i]*sum(A[j,3]*l4[j,i] for j=1:6) >=R) 184 | 185 | ######################### 186 | b5 = ob5 187 | @NLconstraint(m, (l5[1,i]-l5[4,i])^2 + (l5[2,i]-l5[5,i])^2 + (l5[3,i]-l5[6,i])^2 == 1) # == 188 | @NLconstraint(m,sum(-b5[j]*l5[j,i] for j = 1:6) + x[1,i]*sum(A[j,1]*l5[j,i] for j=1:6) + 189 | x[2,i]*sum(A[j,2]*l5[j,i] for j=1:6) + x[3,i]*sum(A[j,3]*l5[j,i] for j=1:6) >=R) 190 | 191 | end 192 | 193 | setvalue(timeScale,timeWS*ones(N+1,1)) 194 | 195 | setvalue(x,xWS) 196 | setvalue(u,w_H*ones(4,N)) # faster not to warm-start 197 | 198 | setvalue(l1,0.05*ones(6,N+1)) 199 | setvalue(l2,0.05*ones(6,N+1)) 200 | setvalue(l3,0.05*ones(6,N+1)) 201 | setvalue(l4,0.05*ones(6,N+1)) 202 | setvalue(l5,0.05*ones(6,N+1)) 203 | 204 | 205 | time1 = 0 206 | time2 = 0 207 | time3 = 0 208 | time4 = 0 209 | 210 | 211 | tic() 212 | status = solve(m; suppress_warnings=true) 213 | time1 = toq() 214 | 215 | # println(time1) 216 | 217 | flag = 1; 218 | 219 | # println("solver status after 1 trial: ", status) 220 | if flag == 1 221 | if status == :Optimal 222 | exitflag = 1 223 | else 224 | exitflag = 0 225 | end 226 | elseif flag == 2 227 | # println("flag 1: ", flag) 228 | if status == :Optimal 229 | exitflag = 1 230 | elseif status ==:Error || status ==:UserLimit 231 | tic() 232 | status = solve(m; suppress_warnings=true) 233 | time2 = toq() 234 | # println("time2: ", time2) 235 | 236 | if status == :Optimal 237 | exitflag = 1 238 | elseif status ==:Error || status ==:UserLimit 239 | tic() 240 | status = solve(m; suppress_warnings=true) 241 | time3 = toq() 242 | 243 | if status == :Optimal 244 | exitflag = 1 245 | elseif status ==:Error || status ==:UserLimit 246 | 247 | tic() 248 | status = solve(m; suppress_warnings=true) 249 | time4 = toq() 250 | 251 | if status == :Optimal 252 | exitflag = 1 253 | else 254 | exitflag = 0 255 | end 256 | else 257 | exitflag = 0 258 | end 259 | else 260 | exitflag = 0 261 | end 262 | else 263 | exitflag = 0 264 | end 265 | end 266 | 267 | time = time1+time2+time3+time4 268 | 269 | xp = getvalue(x) 270 | up = getvalue(u) 271 | timeScalep = getvalue(timeScale) 272 | l1p = getvalue(l1) 273 | l2p = getvalue(l2) 274 | l3p = getvalue(l3) 275 | l4p = getvalue(l4) 276 | l5p = getvalue(l5) 277 | lp = [l1p ; l2p ; l3p ; l4p ; l5p] 278 | 279 | 280 | return xp, up, timeScalep, exitflag, time, lp, string(status) 281 | 282 | end 283 | 284 | -------------------------------------------------------------------------------- /QuadcopterNavigation/QuadcopterSignedDist.jl: -------------------------------------------------------------------------------- 1 | ############### 2 | # OBCA: Optimization-based Collision Avoidance - a path planner for autonomous parking 3 | # Copyright (C) 2018 4 | # Alexander LINIGER [liniger@control.ee.ethz.ch; Automatic Control Lab, ETH Zurich] 5 | # Xiaojing ZHANG [xiaojing.zhang@berkeley.edu; MPC Lab, UC Berkeley] 6 | # 7 | # This program is free software: you can redistribute it and/or modify 8 | # it under the terms of the GNU General Public License as published by 9 | # the Free Software Foundation, either version 3 of the License, or 10 | # (at your option) any later version. 11 | # 12 | # This program is distributed in the hope that it will be useful, 13 | # but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 | # GNU General Public License for more details. 16 | # 17 | # You should have received a copy of the GNU General Public License 18 | # along with this program. If not, see . 19 | ############### 20 | # The paper describing the theory can be found here: 21 | # X. Zhang, A. Liniger and F. Borrelli; "Optimization-Based Collision Avoidance"; Technical Report, 2017, [https://arxiv.org/abs/1711.03449] 22 | ############### 23 | 24 | 25 | function QuadcopterSignedDist(x0,xF,N,Ts,R,ob1,ob2,ob3,ob4,ob5,xWS,uWS,timeWS) 26 | 27 | # define solver 28 | m = Model(solver=IpoptSolver(hessian_approximation="exact",mumps_pivtol=5e-7,mumps_pivtolmax=0.1,mumps_mem_percent=10000, 29 | recalc_y="no",alpha_for_y="min",required_infeasibility_reduction=0.6, 30 | min_hessian_perturbation=1e-10,jacobian_regularization_value=1e-7,tol=1e-5, 31 | print_level=0))#state 32 | 33 | 34 | @variable(m, x[1:12,1:(N+1)]) 35 | @variable(m, timeScale[1:N+1]) 36 | #control 37 | @variable(m, u[1:4,1:(N)]) 38 | # lagrange multipliers for dual dist function 39 | @variable(m, l1[1:6,1:(N+1)]) 40 | 41 | @variable(m, l2[1:6,1:(N+1)]) 42 | 43 | @variable(m, l3[1:6,1:(N+1)]) 44 | 45 | @variable(m, l4[1:6,1:(N+1)]) 46 | 47 | @variable(m, l5[1:6,1:(N+1)]) 48 | 49 | @variable(m, slack[1:5,1:(N+1)]) 50 | 51 | mass = 0.5; 52 | g = 9.81; 53 | reg = 0; 54 | reg2 = 1e-4; 55 | reg3 = 0.0001; 56 | 57 | k_F = 0.0611;#6.11*1e-8 #[N/rpm^2] 58 | k_M = 0.0015;#1.5*1e-9 #[Nm/rpm^2] 59 | I = [3.9,4.4,4.9]*1e-3 #[kg/m2] 60 | L = 0.225 #[m] 61 | 62 | w_H = sqrt((mass*g)/(k_F*4)) 63 | 64 | # cost function 65 | @NLobjective(m, Min,1e-3*sum( sum((w_H-u[j,i])^2 for j=1:4) for i = 1:N) + 66 | 1e-2*sum( sum((u[j,i]-u[j,i+1])^2 for j=1:4) for i = 1:N-1) + 67 | 1*sum(sum(reg3*x[j,i]^2 for i = 1:N+1) for j = [10,11,12]) + 68 | sum(0.25*timeScale[i] + 5*timeScale[i]^2 for i = 1:N+1) + 69 | sum(sum(1e2*slack[j,i] + 1e3*slack[j,i]^2 for i = 1:N+1) for j = 1:5) + 70 | # sum(sum(8e0*slack[j,i] + 1e2*slack[j,i]^2 for i = 1:N+1) for j = 1:5) + 71 | 1*sum(sum(reg2*l1[j,i]^2 + reg2*l2[j,i]^2 + reg2*l3[j,i]^2 + reg2*l4[j,i]^2+ reg2*l5[j,i]^2 for i = 1:N+1) for j = 1:6)); 72 | 73 | #input constraints 74 | @constraint(m, [i=1:N], 1.200 <= u[1,i] <= 7.800) 75 | @constraint(m, [i=1:N], 1.200 <= u[2,i] <= 7.800) 76 | @constraint(m, [i=1:N], 1.200 <= u[3,i] <= 7.800) 77 | @constraint(m, [i=1:N], 1.200 <= u[4,i] <= 7.800) 78 | #state constraints 79 | #X,Y,Z 80 | @constraint(m, [i=1:N+1], 0 <= x[1,i] <= 10) 81 | @constraint(m, [i=1:N+1], 0 <= x[2,i] <= 10) # -0.1 <= x[2,i] <= 20 82 | @constraint(m, [i=1:N+1], 0 <= x[3,i] <= 5) 83 | # pitch, roll 84 | @constraint(m, [i=1:N+1], -3 <= x[4,i] <= 3) 85 | @constraint(m, [i=1:N+1], -0.2 <= x[5,i] <= 0.2) #pm 0.2 86 | @constraint(m, [i=1:N+1], -0.2 <= x[6,i] <= 0.2) 87 | #v_x, v_y, v_z 88 | @constraint(m, [i=1:N+1],-1 <= x[7,i] <= 1) 89 | @constraint(m, [i=1:N+1],-1 <= x[8,i] <= 1) 90 | @constraint(m, [i=1:N+1],-1 <= x[9,i] <= 1) 91 | # pitch_rate, roll_rate 92 | @constraint(m, [i=1:N+1],-1 <= x[10,i] <= 1) #pm 1 93 | @constraint(m, [i=1:N+1],-1 <= x[11,i] <= 1) 94 | @constraint(m, [i=1:N+1],-1 <= x[12,i] <= 1) 95 | 96 | @constraint(m, 0.5 .<= timeScale .<= 2) 97 | # positivity constraints on lambda 98 | @constraint(m, l1.>= 0) 99 | @constraint(m, l2.>= 0) 100 | @constraint(m, l3.>= 0) 101 | @constraint(m, l4.>= 0) 102 | @constraint(m, l5.>= 0) 103 | 104 | 105 | @constraint(m, slack.>= 0) 106 | 107 | #starting point 108 | @constraint(m, x[1,1] == x0[1]) 109 | @constraint(m, x[2,1] == x0[2]) 110 | @constraint(m, x[3,1] == x0[3]) 111 | @constraint(m, x[4,1] == x0[4]) 112 | @constraint(m, x[5,1] == x0[5]) 113 | @constraint(m, x[6,1] == x0[6]) 114 | @constraint(m, x[7,1] == x0[7]) 115 | @constraint(m, x[8,1] == x0[8]) 116 | @constraint(m, x[9,1] == x0[9]) 117 | @constraint(m, x[10,1] == x0[10]) 118 | @constraint(m, x[11,1] == x0[11]) 119 | @constraint(m, x[12,1] == x0[12]) 120 | 121 | 122 | #end point 123 | @constraint(m, x[1,N+1] == xF[1]) 124 | @constraint(m, x[2,N+1] == xF[2]) 125 | @constraint(m, x[3,N+1] == xF[3]) 126 | @constraint(m, x[4,N+1] == xF[4]) 127 | @constraint(m, x[5,N+1] == xF[5]) 128 | @constraint(m, x[6,N+1] == xF[6]) 129 | @constraint(m, x[7,N+1] == xF[7]) 130 | @constraint(m, x[8,N+1] == xF[8]) 131 | @constraint(m, x[9,N+1] == xF[9]) 132 | @constraint(m, x[10,N+1] == xF[10]) 133 | @constraint(m, x[11,N+1] == xF[11]) 134 | @constraint(m, x[12,N+1] == xF[12]) 135 | 136 | for i in 1:N 137 | #X,Y,Z 138 | @NLconstraint(m, x[1,i+1] == x[1,i] + timeScale[i]*Ts*x[7,i]) 139 | @NLconstraint(m, x[2,i+1] == x[2,i] + timeScale[i]*Ts*x[8,i]) 140 | @NLconstraint(m, x[3,i+1] == x[3,i] + timeScale[i]*Ts*x[9,i]) 141 | 142 | # pitch, roll 143 | @NLconstraint(m, x[4,i+1] == x[4,i] + timeScale[i]*Ts*( cos(x[5,i]) *x[10,i] +sin(x[5,i]) *x[12,i])) 144 | @NLconstraint(m, x[5,i+1] == x[5,i] + timeScale[i]*Ts*( sin(x[5,i])*tan(x[4,i])*x[10,i]+x[11,i]-cos(x[5,i])*tan(x[4,i])*x[12,i])) 145 | @NLconstraint(m, x[6,i+1] == x[6,i] + timeScale[i]*Ts*(-sin(x[5,i])*sec(x[4,i])*x[10,i] +cos(x[5,i])*sec(x[4,i])*x[12,i])) 146 | 147 | #v_x, v_y, v_z 148 | @NLconstraint(m, x[7,i+1] == x[7,i] + timeScale[i]*Ts*1/mass*(sum(k_F*u[j,i]^2 for j=1:4)*( sin(x[4,i])*cos(x[5,i])*sin(x[6,i]) + sin(x[5,i])*cos(x[6,i]) ))) 149 | @NLconstraint(m, x[8,i+1] == x[8,i] + timeScale[i]*Ts*1/mass*(sum(k_F*u[j,i]^2 for j=1:4)*(-sin(x[4,i])*cos(x[5,i])*cos(x[6,i]) + sin(x[5,i])*sin(x[6,i]) ))) 150 | @NLconstraint(m, x[9,i+1] == x[9,i] + timeScale[i]*Ts*1/mass*(sum(k_F*u[j,i]^2 for j=1:4)*( cos(x[4,i])*cos(x[5,i])) - mass*g )) 151 | 152 | # pitch_rate, roll_rate 153 | @NLconstraint(m, x[10,i+1] == x[10,i] + timeScale[i]*Ts*1/I[1]*(L*k_F*(u[2,i]^2 - u[4,i]^2) - (I[3] - I[2])*x[11]*x[12])) 154 | @NLconstraint(m, x[11,i+1] == x[11,i] + timeScale[i]*Ts*1/I[2]*(L*k_F*(u[3,i]^2 - u[1,i]^2) - (I[1] - I[3])*x[10]*x[12])) 155 | @NLconstraint(m, x[12,i+1] == x[12,i] + timeScale[i]*Ts*1/I[3]*(k_M*(u[1,i]^2 - u[2,i]^2 + u[3,i]^2 - u[4,i]^2) - (I[2] - I[1])*x[10]*x[11])) 156 | 157 | @constraint(m, timeScale[i] == timeScale[i+1]) 158 | end 159 | 160 | 161 | 162 | A = [eye(3); 163 | -eye(3)]; 164 | 165 | for i in 1:N+1 166 | # rotation matrix 167 | 168 | b1 = ob1 169 | @NLconstraint(m, (l1[1,i]-l1[4,i])^2 + (l1[2,i]-l1[5,i])^2 + (l1[3,i]-l1[6,i])^2 == 1) 170 | @NLconstraint(m,sum(-b1[j]*l1[j,i] for j = 1:6) + x[1,i]*sum(A[j,1]*l1[j,i] for j=1:6) + 171 | x[2,i]*sum(A[j,2]*l1[j,i] for j=1:6) + x[3,i]*sum(A[j,3]*l1[j,i] for j=1:6) + 0.01*slack[1,i]>=R) 172 | 173 | ###################### 174 | b2 = ob2 175 | @NLconstraint(m, (l2[1,i]-l2[4,i])^2 + (l2[2,i]-l2[5,i])^2 + (l2[3,i]-l2[6,i])^2 == 1) 176 | @NLconstraint(m,sum(-b2[j]*l2[j,i] for j = 1:6) + x[1,i]*sum(A[j,1]*l2[j,i] for j=1:6) + 177 | x[2,i]*sum(A[j,2]*l2[j,i] for j=1:6) + x[3,i]*sum(A[j,3]*l2[j,i] for j=1:6) + 0.01*slack[2,i]>=R) 178 | 179 | ######################### 180 | b3 = ob3 181 | @NLconstraint(m, (l3[1,i]-l3[4,i])^2 + (l3[2,i]-l3[5,i])^2 + (l3[3,i]-l3[6,i])^2 == 1) 182 | @NLconstraint(m,sum(-b3[j]*l3[j,i] for j = 1:6) + x[1,i]*sum(A[j,1]*l3[j,i] for j=1:6) + 183 | x[2,i]*sum(A[j,2]*l3[j,i] for j=1:6) + x[3,i]*sum(A[j,3]*l3[j,i] for j=1:6) + 0.01*slack[3,i]>=R) 184 | 185 | ######################### 186 | b4 = ob4 187 | @NLconstraint(m, (l4[1,i]-l4[4,i])^2 + (l4[2,i]-l4[5,i])^2 + (l4[3,i]-l4[6,i])^2 == 1) 188 | @NLconstraint(m,sum(-b4[j]*l4[j,i] for j = 1:6) + x[1,i]*sum(A[j,1]*l4[j,i] for j=1:6) + 189 | x[2,i]*sum(A[j,2]*l4[j,i] for j=1:6) + x[3,i]*sum(A[j,3]*l4[j,i] for j=1:6) + 0.01*slack[4,i]>=R) 190 | 191 | ######################### 192 | b5 = ob5 193 | @NLconstraint(m, (l5[1,i]-l5[4,i])^2 + (l5[2,i]-l5[5,i])^2 + (l5[3,i]-l5[6,i])^2 == 1) 194 | @NLconstraint(m,sum(-b5[j]*l5[j,i] for j = 1:6) + x[1,i]*sum(A[j,1]*l5[j,i] for j=1:6) + 195 | x[2,i]*sum(A[j,2]*l5[j,i] for j=1:6) + x[3,i]*sum(A[j,3]*l5[j,i] for j=1:6) + 0.01*slack[5,i]>=R) 196 | 197 | end 198 | 199 | setvalue(timeScale,timeWS*ones(N+1,1)) 200 | 201 | setvalue(x,xWS) 202 | setvalue(u,w_H*ones(4,N)) # faster not to warm-start 203 | 204 | setvalue(l1,0.05*ones(6,N+1)) 205 | setvalue(l2,0.05*ones(6,N+1)) 206 | setvalue(l3,0.05*ones(6,N+1)) 207 | setvalue(l4,0.05*ones(6,N+1)) 208 | setvalue(l5,0.05*ones(6,N+1)) 209 | 210 | setvalue(slack,1*ones(5,N+1)) # setvalue(slack,0.1*ones(5,N+1)) 211 | # setvalue(slack,zeros(5,N+1)) # slows down solver very much 212 | 213 | 214 | time1 = 0 215 | time2 = 0 216 | time3 = 0 217 | time4 = 0 218 | 219 | 220 | tic() 221 | # status = solve(m; suppress_warnings=true) 222 | status = solve(m) 223 | time1 = toq() 224 | 225 | # println(time1) 226 | 227 | flag = 1; 228 | 229 | # println("solver status after 1 trial: ", status) 230 | if flag == 1 231 | if status == :Optimal 232 | exitflag = 1 233 | else 234 | exitflag = 0 235 | end 236 | elseif flag == 2 237 | if status == :Optimal 238 | exitflag = 1 239 | elseif status ==:Error || status ==:UserLimit 240 | tic() 241 | status = solve(m; suppress_warnings=true) 242 | time2 = toq() 243 | 244 | if status == :Optimal 245 | exitflag = 1 246 | elseif status ==:Error || status ==:UserLimit 247 | tic() 248 | status = solve(m; suppress_warnings=true) 249 | time3 = toq() 250 | 251 | if status == :Optimal 252 | exitflag = 1 253 | elseif status ==:Error || status ==:UserLimit 254 | 255 | tic() 256 | status = solve(m; suppress_warnings=true) 257 | time4 = toq() 258 | 259 | if status == :Optimal 260 | exitflag = 1 261 | else 262 | exitflag = 0 263 | end 264 | else 265 | exitflag = 0 266 | end 267 | else 268 | exitflag = 0 269 | end 270 | else 271 | exitflag = 0 272 | end 273 | end 274 | 275 | time = time1+time2+time3+time4 276 | 277 | xp = getvalue(x) 278 | up = getvalue(u) 279 | timeScalep = getvalue(timeScale) 280 | 281 | slackp = getvalue(slack) 282 | 283 | sumSlack = sum(slackp) 284 | # println(sumSlack) 285 | if exitflag == 1 && sumSlack > 1e-3 286 | println("sum-slack condition not satisfied") 287 | exitflag = 2 288 | end 289 | 290 | l1p = getvalue(l1) 291 | l2p = getvalue(l2) 292 | l3p = getvalue(l3) 293 | l4p = getvalue(l4) 294 | l5p = getvalue(l5) 295 | lp = [l1p ; l2p ; l3p ; l4p ; l5p] 296 | 297 | 298 | return xp, up, timeScalep, exitflag, time, lp, string(status) 299 | 300 | end 301 | 302 | -------------------------------------------------------------------------------- /QuadcopterNavigation/README.md: -------------------------------------------------------------------------------- 1 | # OBCA - Quadcopter Path Planning 2 | Optimization-Based Collision Avoidance - an application in quadcopter path planning 3 | 4 | Paper describing the theory can be found [here](http://arxiv.org/abs/1711.03449). 5 | 6 | ## How to run the code: 7 | 8 | ### First steps 9 | 10 | 1. Change to the directory 11 | 12 | 2. Install Julia from https://julialang.org/downloads/ (code tested on version 0.5 and 0.6) 13 | 14 | 3. Open Julia in terminal 15 | 16 | 4. Install Julia package JuMP using Pkg.add("JuMP") 17 | 18 | 5. Install Julia package Ipopt using Pkg.add("Ipopt") 19 | 20 | 6. Install Julia package PyPlot using Pkg.add("PyPlot") 21 | 22 | 7. Install Julia package PyPlot using Pkg.add("NearestNeighbors") 23 | 24 | 25 | ### Running the parking example 26 | 27 | 1. Start Julia in terminal 28 | 29 | 2. Type in terminal: include("setupQuadcopter.jl") 30 | 31 | 3. Type in terminal: include("mainQuadcopter.jl") 32 | 33 | 34 | ### modifying the code 35 | 36 | 1. To play with start points, change xF (or x0) in mainQuadcopter.jl and run 37 | the code by include("mainQuadcopter.jl") 38 | 39 | 2. If you change anything in one of the collision avoidance 40 | problems, you need to activate the changes by running 41 | include("setupQuadcopter.jl") 42 | 43 | 44 | ### Note 45 | 1. This code has been tested on Julia 0.5 and 0.6, and might not run on any other Julia versions. 46 | 47 | 2. For best results, run code in Julia terminal 48 | -------------------------------------------------------------------------------- /QuadcopterNavigation/a_star_3D.jl: -------------------------------------------------------------------------------- 1 | ############### 2 | # OBCA: Optimization-based Collision Avoidance - a path planner for autonomous parking 3 | # Copyright (C) 2018 4 | # Atsushi SAKAI [atsushisakai@global.komatsu; Komatsu Ltd / MPC Lab] 5 | # Alexander LINIGER [liniger@control.ee.ethz.ch; Automatic Control Lab, ETH Zurich] 6 | # Xiaojing ZHANG [xiaojing.zhang@berkeley.edu; MPC Lab, UC Berkeley] 7 | # 8 | # This program is free software: you can redistribute it and/or modify 9 | # it under the terms of the GNU General Public License as published by 10 | # the Free Software Foundation, either version 3 of the License, or 11 | # (at your option) any later version. 12 | # 13 | # This program is distributed in the hope that it will be useful, 14 | # but WITHOUT ANY WARRANTY; without even the implied warranty of 15 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 16 | # GNU General Public License for more details. 17 | # 18 | # You should have received a copy of the GNU General Public License 19 | # along with this program. If not, see . 20 | ############### 21 | # The paper describing the theory can be found here: 22 | # X. Zhang, A. Liniger and F. Borrelli; "Optimization-Based Collision Avoidance"; Technical Report, 2017, [https://arxiv.org/abs/1711.03449] 23 | ############### 24 | 25 | module a_star # new scope, good for defining global variables 26 | 27 | using NearestNeighbors, JuMP, Ipopt 28 | using PyPlot 29 | 30 | const VEHICLE_RADIUS = 2.5# const GRID_RESOLUTION = 1.0 #[m], def 2.0 31 | const H_WEIGHT = 1.1 # weight for heuristic function 32 | 33 | type Node 34 | x::Int64 #x index 35 | y::Int64 #y index 36 | z::Int64 #z index (added) 37 | cost::Float64 # cost 38 | pind::Int64 # parent index 39 | end 40 | 41 | # Only thing you need to do is that using calc_astar_path() with inputs. 42 | # sx, sy, sz: start point 43 | # gx, gy, gz: goal point 44 | # ox, oy, oz: obstacle position list 45 | # reso: grid resolution of A*. 46 | 47 | # (sx, sy, sz, gx, gy, gz, ox, oy, oz, xmin, ymin, zmin, xmax, ymax, zmax, GRID_RESOLUTION) 48 | 49 | function calc_astar_path(sx::Float64, sy::Float64, sz::Float64, gx::Float64, gy::Float64, gz::Float64, 50 | ox::Array{Float64}, oy::Array{Float64}, oz::Array{Float64}, 51 | xmin::Float64, ymin::Float64, zmin::Float64, 52 | xmax::Float64, ymax::Float64, zmax::Float64, 53 | reso::Float64) 54 | """ 55 | sx: start x position [m] 56 | sy: start y position [m] 57 | sz: start z position [m] 58 | gx: goal x position [m] 59 | gy: goal y position [m] 60 | gz: goal z position [m] 61 | ox: x position list of Obstacles [m] 62 | oy: y position list of Obstacles [m] 63 | oz: z position list of Obstacles [m] 64 | reso: grid resolution [m] 65 | """ 66 | tic() 67 | 68 | nstart = Node(Int(round(sx/reso)),Int(round(sy/reso)),Int(round(sz/reso)),0.0, -1) 69 | ngoal = Node(Int(round(gx/reso)),Int(round(gy/reso)),Int(round(gz/reso)),0.0, -1) 70 | 71 | 72 | ox = [iox/reso for iox in ox] 73 | oy = [ioy/reso for ioy in oy] 74 | oz = [ioz/reso for ioz in oz] 75 | 76 | 77 | obmap, minx, miny, minz, maxx, maxy, maxz, xw, yw, zw = calc_obstacle_map(ox, oy, oz, xmin, ymin, zmin, xmax, ymax, zmax, reso) 78 | miniTime = toq(); 79 | # print("MiniTime ",miniTime,"\n") 80 | tic() 81 | #open, closed set 82 | open, closed = Dict{Int64, Node}(), Dict{Int64, Node}() 83 | 84 | pqOpen = Collections.PriorityQueue(Int64,Float64) 85 | 86 | open[calc_index(nstart, xw, zw, minx, miny, minz)] = nstart # ??????; weird if start is minx miny minz -> zero indexing! 87 | Collections.enqueue!(pqOpen,calc_index(nstart, xw, zw, minx, miny, minz),nstart.cost+H_WEIGHT*h(nstart.x - ngoal.x, nstart.y - ngoal.y, nstart.z-ngoal.z)) 88 | 89 | 90 | motion = get_motion_model() 91 | nmotion = length(motion[:,1]) 92 | 93 | tmpCounter = 1 94 | while true 95 | if length(open) == 0 96 | println("Error: No open set") 97 | break 98 | end 99 | 100 | c_id = Collections.dequeue!(pqOpen) 101 | current = open[c_id] 102 | 103 | 104 | if current.x == ngoal.x && current.y == ngoal.y && current.z == ngoal.z # check goal 105 | # println("Path found by A star!!") 106 | closed[c_id] = current 107 | break 108 | end 109 | 110 | delete!(open, c_id) 111 | closed[c_id] = current 112 | 113 | for i in 1:nmotion # expand search grid based on motion model 114 | node = Node(current.x+motion[i,1], current.y+motion[i,2], current.z+motion[i,3], current.cost+motion[i,4], c_id) 115 | 116 | # check boundary 117 | if (node.x - minx) >= xw continue end 118 | if (node.x - minx) <= 0 continue end 119 | if (node.y - miny) >= yw continue end 120 | if (node.y - miny) <= 0 continue end 121 | if (node.z - minz) >= zw continue end 122 | if (node.z - minz) <= 0 continue end 123 | 124 | #collision check 125 | if obmap[node.x-minx+1, node.y-miny+1, node.z-minz+1] continue end 126 | 127 | node_ind = calc_index(node, xw, zw, minx, miny, minz) 128 | 129 | # If it is already in the closed set, skip it 130 | if haskey(closed,node_ind) continue end 131 | 132 | if haskey(open, node_ind) # check if in open set 133 | if open[node_ind].cost > node.cost 134 | # If so, update the node to have a new parent 135 | open[node_ind].cost = node.cost 136 | open[node_ind].pind = c_id 137 | pqOpen[node_ind] = node.cost+H_WEIGHT*h(node.x - ngoal.x, node.y - ngoal.y, node.z-ngoal.z) 138 | 139 | end 140 | else # add to open set 141 | open[node_ind] = node 142 | Collections.enqueue!(pqOpen,node_ind,node.cost+H_WEIGHT*h(node.x - ngoal.x, node.y - ngoal.y, node.z-ngoal.z)) 143 | end 144 | end # end nmotion 145 | 146 | 147 | tmpCounter = tmpCounter + 1 148 | 149 | end 150 | runTime = toq() 151 | rx, ry, rz = get_final_path(closed, ngoal, nstart, xw, zw, minx, miny, minz, reso) 152 | 153 | return rx, ry, rz, runTime 154 | end 155 | 156 | 157 | function get_motion_model() 158 | # dx, dy, dz, cost 159 | motion=[ -1 -1 -1 sqrt(3); 160 | -1 -1 0 sqrt(2); 161 | -1 -1 1 sqrt(3); 162 | -1 0 -1 sqrt(2); 163 | -1 0 0 1; 164 | -1 0 1 sqrt(2); 165 | -1 1 -1 sqrt(3); 166 | -1 1 0 sqrt(2); 167 | -1 1 1 sqrt(3); 168 | 0 -1 -1 sqrt(2); 169 | 0 -1 0 1; 170 | 0 -1 1 sqrt(2); 171 | 0 0 -1 1; 172 | 0 0 1 1; 173 | 0 1 -1 sqrt(2); 174 | 0 1 0 1; 175 | 0 1 1 sqrt(2); 176 | 1 -1 -1 sqrt(3); 177 | 1 -1 0 sqrt(2); 178 | 1 -1 1 sqrt(3); 179 | 1 0 -1 sqrt(2); 180 | 1 0 0 1; 181 | 1 0 1 sqrt(2); 182 | 1 1 -1 sqrt(3); 183 | 1 1 0 sqrt(2); 184 | 1 1 1 sqrt(3) ] 185 | 186 | return motion 187 | end 188 | 189 | function calc_index(node::Node, xwidth::Int, zwidth::Int, xmin::Int, ymin::Int64, zmin::Int64) 190 | return (node.y - ymin)*xwidth*zwidth + (node.x - xmin)*zwidth + (node.z-zmin) 191 | end 192 | 193 | function calc_obstacle_map( ox::Array{Float64}, oy::Array{Float64}, oz::Array{Float64}, 194 | xmin::Float64, ymin::Float64, zmin::Float64, 195 | xmax::Float64, ymax::Float64, zmax::Float64, reso::Float64) 196 | # for easier handling 197 | push!(ox,xmin,xmax) 198 | push!(oy,ymin,ymax) 199 | push!(oz,zmin,zmax) 200 | 201 | minx = Int(round(minimum(ox))) 202 | miny = Int(round(minimum(oy))) 203 | minz = Int(round(minimum(oz))) 204 | maxx = Int(round(maximum(ox))) 205 | maxy = Int(round(maximum(oy))) 206 | maxz = Int(round(maximum(oz))) 207 | 208 | xwidth = Int(maxx - minx) 209 | ywidth = Int(maxy - miny) 210 | zwidth = Int(maxz - minz) 211 | 212 | obmap = fill(false, (xwidth,ywidth,zwidth)) 213 | 214 | kdtree = KDTree(hcat(ox, oy, oz)') 215 | for ix in 1:xwidth 216 | x = (ix-1) + minx 217 | for iy in 1:ywidth 218 | y = (iy-1) + miny 219 | for iz in 1:zwidth 220 | z = (iz-1) + minz 221 | 222 | idxs, onedist = knn(kdtree, [x, y, z] , 1) 223 | if onedist[1] <= VEHICLE_RADIUS/reso 224 | obmap[ix,iy,iz] = true 225 | end 226 | end 227 | end 228 | end 229 | 230 | return obmap, minx, miny, minz, maxx, maxy, maxz, xwidth, ywidth, zwidth 231 | end 232 | 233 | function get_final_path(closed::Dict{Int64, Node}, 234 | ngoal::Node, 235 | nstart::Node, 236 | xw::Int64, 237 | zw::Int64, # new 238 | minx::Int64, 239 | miny::Int64, 240 | minz::Int64, # new 241 | reso::Float64) 242 | 243 | rx, ry ,rz = [ngoal.x],[ngoal.y], [ngoal.z] 244 | 245 | nid = calc_index(ngoal, xw, zw, minx, miny, minz) 246 | while true 247 | n = closed[nid] 248 | push!(rx, n.x) 249 | push!(ry, n.y) 250 | push!(rz, n.z) 251 | nid = n.pind 252 | 253 | if rx[end] == nstart.x && ry[end] == nstart.y && rz[end] == nstart.z 254 | # println("done reconstructing path") 255 | break 256 | end 257 | end 258 | 259 | rx = reverse(rx) .* reso 260 | ry = reverse(ry) .* reso 261 | rz = reverse(rz) .* reso 262 | 263 | return rx, ry, rz 264 | end 265 | 266 | 267 | function search_min_cost_node(open::Dict{Int64, Node}, ngoal::Node,Hmat) 268 | mnode = nothing 269 | mcost = Inf 270 | 271 | # find best node in open set 272 | for n in values(open) 273 | # println("candidate node:", n) 274 | cost = n.cost + H_WEIGHT*Hmat[Int(n.x+1), Int(n.y+1), Int(n.z+1)] # compute gScore + hScore (cost from start to n + heuristics) 275 | if mcost > cost 276 | mnode = n 277 | mcost = cost 278 | end 279 | end 280 | 281 | return mnode 282 | end 283 | 284 | 285 | function h(x::Int, y::Int, z::Int) 286 | """ 287 | Heuristic cost function 288 | """ 289 | return sqrt(x^2 + y^2 + z^2); 290 | end 291 | 292 | 293 | # Only thing you need to do is that using calc_astar_path() with inputs. 294 | # sx, sy is start point 295 | # gx, gy is a goal point 296 | # ox, oy is obstacle position lists 297 | # and reso means grid resolution of A*. 298 | 299 | function main() 300 | close("all") 301 | println(PROGRAM_FILE," start A-star!!") 302 | i = 0 303 | horizonLengths = ones(100,1) 304 | for yy = 10 : 10 : 10 # 90 305 | for zz = 10 : 10 : 10 # 40 306 | i = i+1 307 | 308 | # all FLOAT for performance 309 | # everthing in [m] for convenience 310 | xmin = 0.0 311 | ymin = 0.0 312 | zmin = 0.0 313 | xmax = 105.0 314 | ymax = 105.0 315 | zmax = 55.0 316 | 317 | sx = 10.0 # [m] 318 | sy = 10.0 # [m] 319 | sz = 30.0 # [m] 320 | 321 | gx = 90.0 # [m] 322 | gy = 80.0 # [m] 323 | gy = Float64(yy) 324 | gz = 40.0 # [m] 325 | gz = Float64(zz) 326 | # build obstacles 327 | 328 | println("gy: ", gy) 329 | println("gz: ", gz) 330 | 331 | ox = Float64[] 332 | oy = Float64[] 333 | oz = Float64[] 334 | 335 | # first obstacle 336 | for xx in 20 : 25 337 | for yy in 0 : 105 338 | for zz in 6 : 55 339 | push!(ox,Float64(xx)) 340 | push!(oy,Float64(yy)) 341 | push!(oz,Float64(zz)) 342 | end 343 | end 344 | end 345 | 346 | # second obstacle 347 | ox1 = Float64[] 348 | oy1 = Float64[] 349 | oz1 = Float64[] 350 | 351 | for xx = 70 : 75 352 | # left piece 353 | for yy = 0 : 40 354 | for zz = 0 : 55 355 | push!(ox, Float64(xx)) 356 | push!(oy, Float64(yy)) 357 | push!(oz, Float64(zz)) 358 | 359 | push!(ox1, Float64(xx)) 360 | push!(oy1, Float64(yy)) 361 | push!(oz1, Float64(zz)) 362 | end 363 | end 364 | # right piece 365 | for yy = 50 : 105 366 | for zz = 0 : 55 367 | push!(ox, Float64(xx)) 368 | push!(oy,Float64(yy)) 369 | push!(oz,Float64(zz)) 370 | 371 | push!(ox1, Float64(xx)) 372 | push!(oy1,Float64(yy)) 373 | push!(oz1,Float64(zz)) 374 | end 375 | end 376 | # top piece 377 | for yy = 40 : 50 378 | for zz = 30 : 55 379 | push!(ox, Float64(xx)) 380 | push!(oy,Float64(yy)) 381 | push!(oz,Float64(zz)) 382 | 383 | push!(ox1, Float64(xx)) 384 | push!(oy1,Float64(yy)) 385 | push!(oz1,Float64(zz)) 386 | end 387 | end 388 | # right piece 389 | for yy = 40 : 50 390 | for zz = 0 : 20 391 | push!(ox, Float64(xx)) 392 | push!(oy,Float64(yy)) 393 | push!(oz,Float64(zz)) 394 | 395 | push!(ox1, Float64(xx)) 396 | push!(oy1,Float64(yy)) 397 | push!(oz1,Float64(zz)) 398 | end 399 | end 400 | end 401 | 402 | rx, ry, rz = calc_astar_path( sx, sy, sz, # start 403 | gx, gy, gz, # goal 404 | ox, oy, oz, # list of obstacles 405 | xmin, ymin, zmin, # box constraint 406 | xmax, ymax, zmax, # box constraint 407 | 1.0 ) # other relevant arguments 408 | 409 | # plot problem setup 410 | fig = figure() 411 | hold(1) 412 | title("Test") 413 | ax = gca(projection="3d") 414 | plot3D(ox,oy,oz,".b") 415 | plot3D(ox1,oy1,oz1,".k") 416 | plot3D([sx],[sy],[sz],"xr") 417 | plot3D([gx],[gy],[gz],"xb") 418 | plot3D(rx,ry,rz,"--g") 419 | xlim([xmin, xmax]) 420 | ylim([ymin, ymax]) 421 | zlim([zmin, zmax]) 422 | xlabel("X [m]") 423 | ylabel("Y [m]") 424 | zlabel("Z [m]") 425 | 426 | rx_smooth, ry_smooth, rz_smooth = smoothenPath(rx,ry,rz) 427 | 428 | 429 | end # end for-zz 430 | end # end for-xx 431 | # println("*** horizonLengths: ", horizonLengths) 432 | # println("*** min horizon: ", minimum(horizonLengths[1:i])) 433 | # println("*** max horizon: ", maximum(horizonLengths[1:i])) 434 | 435 | println(PROGRAM_FILE," Done!!") 436 | end 437 | 438 | if length(PROGRAM_FILE)!=0 && 439 | contains(@__FILE__, PROGRAM_FILE) 440 | 441 | main() 442 | end 443 | 444 | 445 | # if contains(@__FILE__, PROGRAM_FILE) 446 | # main() 447 | # end 448 | 449 | 450 | end #module 451 | 452 | -------------------------------------------------------------------------------- /QuadcopterNavigation/constrSatisfaction.jl: -------------------------------------------------------------------------------- 1 | ############### 2 | # OBCA: Optimization-based Collision Avoidance - a path planner for autonomous parking 3 | # Copyright (C) 2018 4 | # Alexander LINIGER [liniger@control.ee.ethz.ch; Automatic Control Lab, ETH Zurich] 5 | # Xiaojing ZHANG [xiaojing.zhang@berkeley.edu; MPC Lab, UC Berkeley] 6 | # 7 | # This program is free software: you can redistribute it and/or modify 8 | # it under the terms of the GNU General Public License as published by 9 | # the Free Software Foundation, either version 3 of the License, or 10 | # (at your option) any later version. 11 | # 12 | # This program is distributed in the hope that it will be useful, 13 | # but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 | # GNU General Public License for more details. 16 | # 17 | # You should have received a copy of the GNU General Public License 18 | # along with this program. If not, see . 19 | ############### 20 | # The paper describing the theory can be found here: 21 | # X. Zhang, A. Liniger and F. Borrelli; "Optimization-Based Collision Avoidance"; Technical Report, 2017, [https://arxiv.org/abs/1711.03449] 22 | ############### 23 | 24 | 25 | function constrSatisfaction(x, u, timeScale,x0,xF,Ts,lambda,ob1,ob2,ob3,ob4,ob5,R) 26 | 27 | # xp: 12 x (N+1) 28 | # up: 4 x N 29 | # timeScalep : 1x(N+1) 30 | 31 | mass = 0.5; 32 | g = 9.81; 33 | reg = 0; 34 | reg2 = 1e-4; 35 | reg3 = 0.0001; 36 | 37 | k_F = 0.0611;#6.11*1e-8 #[N/rpm^2] 38 | k_M = 0.0015;#1.5*1e-9 #[Nm/rpm^2] 39 | I = [3.9,4.4,4.9]*1e-3 #[kg/m2] 40 | L = 0.225 #[m] 41 | 42 | 43 | 44 | # unwrap dual variables: lp = [l1p ; l2p ; l3p ; l4p ; l5p] 45 | l1 = lambda[1:6,:] 46 | l2 = lambda[7:12,:] 47 | l3 = lambda[13:18,:] 48 | l4 = lambda[19:24,:] 49 | l5 = lambda[25:30,:] 50 | 51 | 52 | w_H = sqrt((mass*g)/(k_F*4)) 53 | 54 | N = size(x,2)-1 55 | 56 | tmp_slack = x[:,1] - x0' 57 | if norm(tmp_slack, Inf)>1e-3 58 | # println("initial state not satisfied") 59 | # println(tmp_slack) 60 | return false 61 | end 62 | 63 | tmp_slack = x[:,end] - xF' 64 | if norm(tmp_slack, Inf)>1e-3 65 | # println("final state not satisfied") 66 | # println(tmp_slack) 67 | return false 68 | end 69 | 70 | A = [eye(3); 71 | -eye(3)]; 72 | 73 | b1 = ob1 74 | b2 = ob2 75 | b3 = ob3 76 | b4 = ob4 77 | b5 = ob5 78 | 79 | 80 | for i = 1 : N 81 | # check input constraints 82 | tmp_slack = [1.2 ; 1.2 ; 1.2 ; 1.2] - u[:,i] # must be <= 0 83 | if maximum(tmp_slack) > 0 84 | # println("input constraint 1 not satisfied at i = ", i) 85 | # println(tmp_slack) 86 | return false 87 | end 88 | # for j = 1 : 4 89 | # if tmp_slack[j] >0 90 | # println("input constraint 1 not satisfied at i = ", i) 91 | # println(tmp_slack) 92 | # end 93 | # end 94 | tmp_slack = u[:,i] - [7.8 ; 7.8 ; 7.8 ; 7.8] # must be <= 0 95 | if maximum(tmp_slack)>0 96 | # println("input constraint 2 not satisfied at i = ", i) 97 | # println(tmp_slack) 98 | return false 99 | end 100 | # for j = 1 : 4 101 | # if tmp_slack[j] >0 102 | # println("input constraint 2 not satisfied at i = ", i) 103 | # println(tmp_slack) 104 | # end 105 | # end 106 | # check state box constraints 107 | tmp_slack = [0;0;0;-3;-0.2;-0.2;-1;-1;-1;-1.5;-1;-1] - x[:,i] 108 | if maximum(tmp_slack)>0 109 | # println("state constraint 1 not satisfied at i = ", i) 110 | # println(tmp_slack) 111 | return false 112 | end 113 | # for j = 1 : 7 114 | # if tmp_slack[j] >0 115 | # println("state constraint 1 not satisfied at i = ", i) 116 | # println(tmp_slack) 117 | # end 118 | # end 119 | tmp_slack = x[:,i] - [10;10;5;3;0.2;0.2;1;1;1;3;1;1] 120 | if maximum(tmp_slack)>0 121 | # println("state constraint 2 not satisfied at i = ", i) 122 | # println(tmp_slack) 123 | return false 124 | end 125 | # for j = 1 : 7 126 | # if tmp_slack[j] >0 127 | # println("state constraint 2 not satisfied at i = ", i) 128 | # println(tmp_slack) 129 | # end 130 | # end 131 | 132 | # check state dynamic constraints 133 | #X,Y,Z 134 | 135 | tmp_slack = zeros(13,1) 136 | tmp_slack[1] = x[1,i+1] - ( x[1,i] + timeScale[i]*Ts*x[7,i] ) 137 | tmp_slack[2] = x[2,i+1] - ( x[2,i] + timeScale[i]*Ts*x[8,i] ) 138 | tmp_slack[3] = x[3,i+1] - ( x[3,i] + timeScale[i]*Ts*x[9,i] ) 139 | # pitch, roll, yaw 140 | tmp_slack[4] = x[4,i+1] - ( x[4,i] + timeScale[i]*Ts*( cos(x[5,i]) *x[10,i] +sin(x[5,i]) *x[12,i]) ) 141 | tmp_slack[5] = x[5,i+1] - ( x[5,i] + timeScale[i]*Ts*( sin(x[5,i])*tan(x[4,i])*x[10,i]+x[11,i]-cos(x[5,i])*tan(x[4,i])*x[12,i]) ) 142 | tmp_slack[6] = x[6,i+1] - ( x[6,i] + timeScale[i]*Ts*(-sin(x[5,i])*sec(x[4,i])*x[10,i] +cos(x[5,i])*sec(x[4,i])*x[12,i]) ) 143 | #v_x, v_y, v_z 144 | tmp_slack[7] = x[7,i+1] - ( x[7,i] + timeScale[i]*Ts*1/mass*(sum(k_F*u[j,i]^2 for j=1:4)*( sin(x[4,i])*cos(x[5,i])*sin(x[6,i]) + sin(x[5,i])*cos(x[6,i]) )) ) 145 | tmp_slack[8] = x[8,i+1] - ( x[8,i] + timeScale[i]*Ts*1/mass*(sum(k_F*u[j,i]^2 for j=1:4)*(-sin(x[4,i])*cos(x[5,i])*cos(x[6,i]) + sin(x[5,i])*sin(x[6,i]) )) ) 146 | # tmp_slack[9] = x[9,i+1] - ( x[9,i] + timeScale[i]*Ts*1/mass*(sum(k_F*u[j,i]^2 for j=1:4)*( cos(x[4,i])*cos(x[5,i])) - mass*g ) ) 147 | tmp_slack[9] = x[9,i+1] - ( x[9,i] + timeScale[i]*Ts*1/mass*(sum(k_F*u[j,i]^2 for j=1:4)*( cos(x[4,i])*cos(x[5,i])) - mass*g ) ) 148 | 149 | 150 | # pitch_rate, roll_rate 151 | tmp_slack[10] = x[10,i+1] - ( x[10,i] + timeScale[i]*Ts*1/I[1]*(L*k_F*(u[2,i]^2 - u[4,i]^2) - (I[3] - I[2])*x[11]*x[12]) ) 152 | tmp_slack[11] = x[11,i+1] - ( x[11,i] + timeScale[i]*Ts*1/I[2]*(L*k_F*(u[3,i]^2 - u[1,i]^2) - (I[1] - I[3])*x[10]*x[12]) ) 153 | tmp_slack[12] = x[12,i+1] - ( x[12,i] + timeScale[i]*Ts*1/I[3]*(k_M*(u[1,i]^2 - u[2,i]^2 + u[3,i]^2 - u[4,i]^2) - (I[2] - I[1])*x[10]*x[11]) ) 154 | tmp_slack[13] = timeScale[i] - timeScale[i+1] 155 | 156 | if norm(tmp_slack, Inf)>1e-3 157 | # println("state dynamics / timeScale inside verification not satisfied at i = ", i) 158 | # println(tmp_slack) 159 | return false 160 | end 161 | 162 | if minimum(minimum(lambda)) < -1e-3 163 | # println("dual variables constraints ", i) 164 | # println(lambda) 165 | return false 166 | end 167 | 168 | 169 | # checking of obstacle avoidance constraints 170 | 171 | tmp_slack = zeros(5,1) 172 | tmp_slack[1] = (l1[1,i]-l1[4,i])^2 + (l1[2,i]-l1[5,i])^2 + (l1[3,i]-l1[6,i])^2 - 1 # should be <= 0 173 | tmp_slack[2] = (l2[1,i]-l2[4,i])^2 + (l2[2,i]-l2[5,i])^2 + (l2[3,i]-l2[6,i])^2 - 1 174 | tmp_slack[3] = (l3[1,i]-l3[4,i])^2 + (l3[2,i]-l3[5,i])^2 + (l3[3,i]-l3[6,i])^2 - 1 175 | tmp_slack[4] = (l4[1,i]-l4[4,i])^2 + (l4[2,i]-l4[5,i])^2 + (l4[3,i]-l4[6,i])^2 - 1 176 | tmp_slack[5] = (l5[1,i]-l5[4,i])^2 + (l5[2,i]-l5[5,i])^2 + (l5[3,i]-l5[6,i])^2 - 1 177 | 178 | if maximum(tmp_slack) > 1e-3 179 | # println("obstacle avoidance constraints 1 violated") 180 | # println(tmp_slack) 181 | return false 182 | end 183 | 184 | tmp_slack = zeros(5,1) 185 | tmp_slack[1] = sum(-b1[j]*l1[j,i] for j = 1:6) + x[1,i]*sum(A[j,1]*l1[j,i] for j=1:6) + 186 | x[2,i]*sum(A[j,2]*l1[j,i] for j=1:6) + x[3,i]*sum(A[j,3]*l1[j,i] for j=1:6) - R # should be >=0 187 | tmp_slack[2] = sum(-b2[j]*l2[j,i] for j = 1:6) + x[1,i]*sum(A[j,1]*l2[j,i] for j=1:6) + 188 | x[2,i]*sum(A[j,2]*l2[j,i] for j=1:6) + x[3,i]*sum(A[j,3]*l2[j,i] for j=1:6) - R 189 | tmp_slack[3] = sum(-b3[j]*l3[j,i] for j = 1:6) + x[1,i]*sum(A[j,1]*l3[j,i] for j=1:6) + 190 | x[2,i]*sum(A[j,2]*l3[j,i] for j=1:6) + x[3,i]*sum(A[j,3]*l3[j,i] for j=1:6) - R 191 | tmp_slack[4] = sum(-b4[j]*l4[j,i] for j = 1:6) + x[1,i]*sum(A[j,1]*l4[j,i] for j=1:6) + 192 | x[2,i]*sum(A[j,2]*l4[j,i] for j=1:6) + x[3,i]*sum(A[j,3]*l4[j,i] for j=1:6) - R 193 | tmp_slack[5] = sum(-b5[j]*l5[j,i] for j = 1:6) + x[1,i]*sum(A[j,1]*l5[j,i] for j=1:6) + 194 | x[2,i]*sum(A[j,2]*l5[j,i] for j=1:6) + x[3,i]*sum(A[j,3]*l5[j,i] for j=1:6) - R 195 | if minimum(tmp_slack) < -1e-3 196 | # println("obstacle avoidance constraints 2 violated") 197 | # println(tmp_slack) 198 | return false 199 | end 200 | end 201 | 202 | # all constraints satisfied 203 | return true 204 | end 205 | 206 | -------------------------------------------------------------------------------- /QuadcopterNavigation/mainQuadcopter.jl: -------------------------------------------------------------------------------- 1 | ############### 2 | # OBCA: Optimization-based Collision Avoidance - a path planner for autonomous parking 3 | # Copyright (C) 2018 4 | # Alexander LINIGER [liniger@control.ee.ethz.ch; Automatic Control Lab, ETH Zurich] 5 | # Xiaojing ZHANG [xiaojing.zhang@berkeley.edu; MPC Lab, UC Berkeley] 6 | # 7 | # This program is free software: you can redistribute it and/or modify 8 | # it under the terms of the GNU General Public License as published by 9 | # the Free Software Foundation, either version 3 of the License, or 10 | # (at your option) any later version. 11 | # 12 | # This program is distributed in the hope that it will be useful, 13 | # but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 | # GNU General Public License for more details. 16 | # 17 | # You should have received a copy of the GNU General Public License 18 | # along with this program. If not, see . 19 | ############### 20 | # The paper describing the theory can be found here: 21 | # X. Zhang, A. Liniger and F. Borrelli; "Optimization-Based Collision Avoidance"; Technical Report, 2017, [https://arxiv.org/abs/1711.03449] 22 | ############### 23 | 24 | ############### 25 | # Main file: computes Collision-Free and Minimum-Penetration trajectories for parking 26 | # run setup.jl before running this file 27 | ############### 28 | 29 | 30 | 31 | 32 | # function defined in setup.jl 33 | clear() 34 | close("all") 35 | 36 | egoR = 0.25 37 | 38 | ob12 = [2.5,12,7, -2,2,-0.6] 39 | 40 | ob22 = [7.5,12,7, -7,-5,2] 41 | ob32 = [7.5,4, 7, -7, 2,2] 42 | 43 | ob42 = [7.5,5, 2, -7,-4,2] 44 | ob52 = [7.5,5, 7, -7,-4,-3] 45 | 46 | ob6 = [10,10, 5, 0,0, 0] 47 | 48 | # x0 = [1,Y,Z,0,0,0] Y and Z can be modified 49 | x0 = [1 1 3 0 0 0 0 0 0 0 0 0] 50 | # xF = [9,Y,Z,0,0,0] Y and Z can be modified 51 | xF = [9 3 2 0 0 0 0 0 0 0 0 0] 52 | 53 | # reshape the state 54 | Ts = 0.25 55 | timeScalep = 0.5; 56 | 57 | ################## construct environment for hybrid A-star ################## 58 | ### note: environment scaled by 10x for convenience ### 59 | ox = Float64[] 60 | oy = Float64[] 61 | oz = Float64[] 62 | # first wall 63 | for xx in 20 : 25 64 | for yy in 0 : 105 65 | for zz in 6 : 55 66 | push!(ox,Float64(xx)) 67 | push!(oy,Float64(yy)) 68 | push!(oz,Float64(zz)) 69 | end 70 | end 71 | end 72 | # second wall; only for plotting purposes 73 | for xx = 70 : 75 74 | # left piece 75 | for yy = 0 : 40 76 | for zz = 0 : 55 77 | push!(ox, Float64(xx)) 78 | push!(oy, Float64(yy)) 79 | push!(oz, Float64(zz)) 80 | end 81 | end 82 | # right piece 83 | for yy = 50 : 105 84 | for zz = 0 : 55 85 | push!(ox, Float64(xx)) 86 | push!(oy,Float64(yy)) 87 | push!(oz,Float64(zz)) 88 | end 89 | end 90 | # top piece 91 | for yy = 40 : 50 92 | for zz = 30 : 55 93 | push!(ox, Float64(xx)) 94 | push!(oy,Float64(yy)) 95 | push!(oz,Float64(zz)) 96 | end 97 | end 98 | # right piece 99 | for yy = 40 : 50 100 | for zz = 0 : 20 101 | push!(ox, Float64(xx)) 102 | push!(oy,Float64(yy)) 103 | push!(oz,Float64(zz)) 104 | end 105 | end 106 | end 107 | # room size 108 | xmin = 0.0 109 | ymin = 0.0 110 | zmin = 0.0 111 | xmax = 105.0 112 | ymax = 105.0 113 | zmax = 55.0 114 | # extract start and end position 115 | sx = x0[1]*10.0 # [m] 116 | sy = x0[2]*10.0 # [m] 117 | sz = x0[3]*10.0 # [m] 118 | gx = xF[1]*10.0 # [m] 119 | gy = xF[2]*10.0 # [m] 120 | gz = xF[3]*10.0 # [m] 121 | # call A* algorithm 122 | rx, ry, rz, rtime = a_star.calc_astar_path( sx, sy, sz, # start 123 | gx, gy, gz, # goal 124 | ox, oy, oz, # list of obstacles 125 | xmin, ymin, zmin, # box constraint 126 | xmax, ymax, zmax, # box constraint 127 | 1.0 ) # (scaled) grid resolution 128 | 129 | N_as = length(rx)-1 # length of Astar 130 | # nominal sampling time 131 | Ts_as = round((Ts*80/N_as)*100)/100 # scale sampling time for Astar 132 | 133 | ###### stitch together Astar solution for warm starting ###### 134 | rxryrz=[rx'/10 ; ry'/10 ; rz'/10] 135 | vWS = zeros(3,N_as+1) 136 | xWS_as = [rx'/10 ; ry'/10 ; rz'/10 ; zeros(3,N_as+1) ; vWS ; zeros(3,N_as+1) ]; 137 | uWS_as = 0.5*ones(4,N_as); # same length as horizon 138 | timeWS_as = 1 139 | # not plotting might get rid of IPOPT restoration failure messages... 140 | # plotTrajQuadcopter(xWS_as',uWS_as',N_as,egoR,ob12,ob22,ob32,ob42,ob52,ob6,egoR,"Warm Start (A*) ",0) 141 | 142 | 143 | ######### trajectory with Distance Approach 144 | println("Trajectory using Distance Approach (Collision Avoidance, A star)") 145 | xp1_as,up1_as,scaleTime1_as,exitflag1_as,time1_as,l1_as,exitstatus1_as = QuadcopterDist(x0,xF,N_as,Ts_as,egoR,ob12,ob22,ob32,ob42,ob52,xWS_as,uWS_as,timeWS_as) 146 | plotTrajQuadcopter(xp1_as',up1_as',N_as,egoR,ob12,ob22,ob32,ob42,ob52,ob6,egoR,"Collision Avoidance with distance approach ",10) 147 | trajFeas1_as = constrSatisfaction(xp1_as,up1_as,scaleTime1_as,x0,xF,Ts_as,l1_as,ob12,ob22,ob32,ob42,ob52,egoR) 148 | 149 | 150 | ######### trajectory with Signed Distance Approach 151 | println("Trajectory using Signed Distance Approach (Minimum Penetration, A star)") 152 | xp2_as,up2_as,scaleTime2_as,exitflag2_as,time2_as,l2_as,exitstatus2_as = QuadcopterSignedDist(x0,xF,N_as,Ts_as,egoR,ob12,ob22,ob32,ob42,ob52,xWS_as,uWS_as,timeWS_as) 153 | plotTrajQuadcopter(xp2_as',up2_as',N_as,egoR,ob12,ob22,ob32,ob42,ob52,ob6,egoR,"Minimum Penetration with signed-distance approach",20) 154 | trajFeas2_as = constrSatisfaction(xp2_as,up2_as,scaleTime2_as,x0,xF,Ts_as,l2_as,ob12,ob22,ob32,ob42,ob52,egoR) 155 | 156 | println("---- Done ----") 157 | 158 | -------------------------------------------------------------------------------- /QuadcopterNavigation/plotTrajQuadcopter.jl: -------------------------------------------------------------------------------- 1 | ############### 2 | # OBCA: Optimization-based Collision Avoidance - a path planner for autonomous parking 3 | # Copyright (C) 2018 4 | # Alexander LINIGER [liniger@control.ee.ethz.ch; Automatic Control Lab, ETH Zurich] 5 | # Xiaojing ZHANG [xiaojing.zhang@berkeley.edu; MPC Lab, UC Berkeley] 6 | # 7 | # This program is free software: you can redistribute it and/or modify 8 | # it under the terms of the GNU General Public License as published by 9 | # the Free Software Foundation, either version 3 of the License, or 10 | # (at your option) any later version. 11 | # 12 | # This program is distributed in the hope that it will be useful, 13 | # but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 | # GNU General Public License for more details. 16 | # 17 | # You should have received a copy of the GNU General Public License 18 | # along with this program. If not, see . 19 | ############### 20 | # The paper describing the theory can be found here: 21 | # X. Zhang, A. Liniger and F. Borrelli; "Optimization-Based Collision Avoidance"; Technical Report, 2017, [https://arxiv.org/abs/1711.03449] 22 | ############### 23 | 24 | 25 | function plotTrajQuadcopter(xp,up,N,ego,ob1,ob2,ob3,ob4,ob5,ob6,R,disp_title,figNumOffset) 26 | ########### 27 | for i = 1:2 28 | if ob1[i] >= 10 29 | ob1[i] = 10 30 | end 31 | end 32 | if ob1[3] >= 5 33 | ob1[3] = 5 34 | end 35 | for i = 4:5 36 | if ob1[i] >= 0 37 | ob1[i] = 0 38 | end 39 | end 40 | if ob1[6] >= 0 41 | ob1[6] = 0 42 | end 43 | ########### 44 | for i = 1:2 45 | if ob2[i] >= 10 46 | ob2[i] = 10 47 | end 48 | end 49 | if ob2[3] >= 5 50 | ob2[3] = 5 51 | end 52 | for i = 4:5 53 | if ob2[i] >= 0 54 | ob2[i] = 0 55 | end 56 | end 57 | if ob2[6] >= 0 58 | ob2[6] = 0 59 | end 60 | ########### 61 | for i = 1:2 62 | if ob3[i] >= 10 63 | ob3[i] = 10 64 | end 65 | end 66 | if ob3[3] >= 5 67 | ob3[3] = 5 68 | end 69 | for i = 4:5 70 | if ob3[i] >= 0 71 | ob3[i] = 0 72 | end 73 | end 74 | if ob3[6] >= 0 75 | ob3[6] = 0 76 | end 77 | ########### 78 | for i = 1:2 79 | if ob4[i] >= 10 80 | ob4[i] = 10 81 | end 82 | end 83 | if ob4[3] >= 5 84 | ob4[3] = 5 85 | end 86 | for i = 4:5 87 | if ob4[i] >= 0 88 | ob4[i] = 0 89 | end 90 | end 91 | if ob4[6] >= 0 92 | ob4[6] = 0 93 | end 94 | ########### 95 | for i = 1:2 96 | if ob5[i] >= 10 97 | ob5[i] = 10 98 | end 99 | end 100 | if ob5[3] >= 5 101 | ob5[3] = 5 102 | end 103 | for i = 4:5 104 | if ob5[i] >= 0 105 | ob5[i] = 0 106 | end 107 | end 108 | if ob5[6] >= 0 109 | ob5[6] = 0 110 | end 111 | 112 | # println(ob1) 113 | # println(ob2) 114 | # println(ob3) 115 | # println(ob4) 116 | # println(ob5) 117 | # println(ob6) 118 | 119 | obcenter1 = [(ob1[1]+ob1[4])/2-ob1[4]; 120 | (ob1[2]+ob1[5])/2-ob1[5]; 121 | (ob1[3]+ob1[6])/2-ob1[6]] 122 | 123 | obcenter2 = [(ob2[1]+ob2[4])/2-ob2[4]; 124 | (ob2[2]+ob2[5])/2-ob2[5]; 125 | (ob2[3]+ob2[6])/2-ob2[6]] 126 | 127 | obcenter3 = [(ob3[1]+ob3[4])/2-ob3[4]; 128 | (ob3[2]+ob3[5])/2-ob3[5]; 129 | (ob3[3]+ob3[6])/2-ob3[6]] 130 | 131 | obcenter4 = [(ob4[1]+ob4[4])/2-ob4[4]; 132 | (ob4[2]+ob4[5])/2-ob4[5]; 133 | (ob4[3]+ob4[6])/2-ob4[6]] 134 | 135 | obcenter5 = [(ob5[1]+ob5[4])/2-ob5[4]; 136 | (ob5[2]+ob5[5])/2-ob5[5]; 137 | (ob5[3]+ob5[6])/2-ob5[6]] 138 | 139 | obcenter6 = [(ob6[1]+ob6[4])/2-ob6[4]; 140 | (ob6[2]+ob6[5])/2-ob6[5]; 141 | (ob6[3]+ob6[6])/2-ob6[6]] 142 | 143 | 144 | 145 | L_tv1 = ob1[1]+ob1[4] 146 | W_tv1 = ob1[2]+ob1[5] 147 | H_tv1 = ob1[3]+ob1[6] 148 | 149 | L_tv2 = ob2[1]+ob2[4] 150 | W_tv2 = ob2[2]+ob2[5] 151 | H_tv2 = ob2[3]+ob2[6] 152 | 153 | L_tv3 = ob3[1]+ob3[4] 154 | W_tv3 = ob3[2]+ob3[5] 155 | H_tv3 = ob3[3]+ob3[6] 156 | 157 | L_tv4 = ob4[1]+ob4[4] 158 | W_tv4 = ob4[2]+ob4[5] 159 | H_tv4 = ob4[3]+ob4[6] 160 | 161 | L_tv5 = ob5[1]+ob5[4] 162 | W_tv5 = ob5[2]+ob5[5] 163 | H_tv5 = ob5[3]+ob5[6] 164 | 165 | L_tv6 = ob6[1]+ob6[4] 166 | W_tv6 = ob6[2]+ob6[5] 167 | H_tv6 = ob6[3]+ob6[6] 168 | 169 | for i = 1:1:N 170 | ######### X-Y plot ############# 171 | figure(1+figNumOffset) 172 | # subplot(3,1,1) 173 | carBox(obcenter1,0,W_tv1/2,L_tv1/2) 174 | title("X-Y plot") 175 | hold(1) 176 | carBox(obcenter2,0,W_tv2/2,L_tv2/2) 177 | carBox(obcenter3,0,W_tv3/2,L_tv3/2) 178 | carBox(obcenter4,0,W_tv4/2,L_tv4/2) 179 | carBox(obcenter5,0,W_tv5/2,L_tv5/2) 180 | carBox(obcenter6,0,W_tv6/2,L_tv6/2) 181 | 182 | 183 | x0 = [xp[i,1]; 184 | xp[i,2]] 185 | 186 | plot(xp[1:i,1],xp[1:i,2],"b") 187 | hold(1) 188 | quadCircle(x0,R) 189 | 190 | axis("equal") 191 | axis([-2,12,-2,12]) 192 | hold(0) 193 | 194 | ######### X-Z plot ############# 195 | figure(2+figNumOffset) 196 | carBox(obcenter1[[1,3]],0,H_tv1/2,L_tv1/2) 197 | title("X-Z plot") 198 | hold(1) 199 | carBox(obcenter2[[1,3]],0,H_tv2/2,L_tv2/2) 200 | carBox(obcenter3[[1,3]],0,H_tv3/2,L_tv3/2) 201 | carBox(obcenter4[[1,3]],0,H_tv4/2,L_tv4/2) 202 | carBox(obcenter5[[1,3]],0,H_tv5/2,L_tv5/2) 203 | carBox(obcenter6[[1,3]],0,H_tv6/2,L_tv6/2) 204 | 205 | 206 | x0 = [xp[i,1]; 207 | xp[i,3]] 208 | 209 | plot(xp[1:i,1],xp[1:i,3],"b") 210 | hold(1) 211 | quadCircle(x0,R) 212 | 213 | axis("equal") 214 | axis([-2,12,-1,6]) 215 | hold(0) 216 | 217 | # 3D plots 218 | figure(3+figNumOffset) 219 | 220 | x0 = [xp[i,1]; 221 | xp[i,2]; 222 | xp[i,3]] 223 | 224 | plot3D(xp[1:i,1],xp[1:i,2],xp[1:i,3],"b") 225 | title(disp_title) 226 | hold(1) 227 | Box3D(obcenter1,L_tv1/2,W_tv1/2,H_tv1/2) 228 | Box3D(obcenter2,L_tv2/2,W_tv2/2,H_tv2/2) 229 | Box3D(obcenter3,L_tv3/2,W_tv3/2,H_tv3/2) 230 | Box3D(obcenter4,L_tv4/2,W_tv4/2,H_tv4/2) 231 | Box3D(obcenter5,L_tv5/2,W_tv5/2,H_tv5/2) 232 | Box3D(obcenter6,L_tv6/2,W_tv6/2,H_tv6/2) 233 | quadBall(x0,R) 234 | 235 | axis("equal") 236 | axis([0,10,0,10]) 237 | zlim([0,5]) 238 | hold(0) 239 | 240 | sleep(0.001) 241 | end 242 | 243 | # for i = 1:1:N 244 | # figure(3+figNumOffset) 245 | # 246 | # x0 = [xp[i,1]; 247 | # xp[i,2]; 248 | # xp[i,3]] 249 | # 250 | # plot3D(xp[1:i,1],xp[1:i,2],xp[1:i,3],"b") 251 | # title(disp_title) 252 | # hold(1) 253 | # Box3D(obcenter1,L_tv1/2,W_tv1/2,H_tv1/2) 254 | # Box3D(obcenter2,L_tv2/2,W_tv2/2,H_tv2/2) 255 | # Box3D(obcenter3,L_tv3/2,W_tv3/2,H_tv3/2) 256 | # Box3D(obcenter4,L_tv4/2,W_tv4/2,H_tv4/2) 257 | # Box3D(obcenter5,L_tv5/2,W_tv5/2,H_tv5/2) 258 | # Box3D(obcenter6,L_tv6/2,W_tv6/2,H_tv6/2) 259 | # quadBall(x0,R) 260 | # 261 | # axis("equal") 262 | # axis([0,10,0,10]) 263 | # zlim([0,5]) 264 | # hold(0) 265 | # 266 | # sleep(0.01) 267 | # end 268 | 269 | 270 | end 271 | 272 | 273 | function carBox(x0,phi,w,l) 274 | 275 | car1 = x0[1:2] + [cos(phi)*l;sin(phi)*l] + [sin(phi)*w;-cos(phi)*w]; 276 | car2 = x0[1:2] + [cos(phi)*l;sin(phi)*l] - [sin(phi)*w;-cos(phi)*w]; 277 | car3 = x0[1:2] - [cos(phi)*l;sin(phi)*l] + [sin(phi)*w;-cos(phi)*w]; 278 | car4 = x0[1:2] - [cos(phi)*l;sin(phi)*l] - [sin(phi)*w;-cos(phi)*w]; 279 | 280 | plot([car1[1],car2[1],car4[1],car3[1],car1[1]],[car1[2],car2[2],car4[2],car3[2],car1[2]],"k") 281 | 282 | end 283 | 284 | function Box3D(x0,l,w,h) 285 | 286 | X = x0[1] + [l,l,-l,-l,l] 287 | Y = x0[2] + [-w,w,w,-w,-w] 288 | Z = x0[3] + [-h,-h,-h,-h,-h] 289 | 290 | plot3D(X,Y,Z,"k") 291 | 292 | Z = x0[3] + [h,h,h,h,h] 293 | plot3D(X,Y,Z,"k") 294 | 295 | X = x0[1] + [l,l] 296 | Y = x0[2] + [-w,-w] 297 | Z = x0[3] + [-h, h] 298 | plot3D(X,Y,Z,"k") 299 | 300 | X = x0[1] + [l,l] 301 | Y = x0[2] + [w,w] 302 | Z = x0[3] + [-h, h] 303 | plot3D(X,Y,Z,"k") 304 | 305 | X = x0[1] + [-l,-l] 306 | Y = x0[2] + [-w,-w] 307 | Z = x0[3] + [-h, h] 308 | plot3D(X,Y,Z,"k") 309 | 310 | X = x0[1] + [-l,-l] 311 | Y = x0[2] + [w,w] 312 | Z = x0[3] + [-h, h] 313 | plot3D(X,Y,Z,"k") 314 | 315 | end 316 | 317 | function quadCircle(x0,R) 318 | phi = linspace(0,2*pi,30); 319 | X = x0[1] + R*cos(phi) 320 | Y = x0[2] + R*sin(phi) 321 | plot(X,Y,"k") 322 | 323 | end 324 | 325 | function quadBall(x0,R) 326 | phi = linspace(0,2*pi,30); 327 | 328 | X = x0[1] + R*cos(phi) 329 | Y = x0[2] + R*sin(phi) 330 | Z = x0[3] 331 | plot3D(X,Y,Z,"k") 332 | 333 | X = x0[1] + R*cos(phi) 334 | Y = x0[2] + zeros(30,1) 335 | Z = x0[3] + R*sin(phi) 336 | plot3D(X,Y,Z,"k") 337 | 338 | X = x0[1] + zeros(30,1) 339 | Y = x0[2] + R*cos(phi) 340 | Z = x0[3] + R*sin(phi) 341 | plot3D(X,Y,Z,"k") 342 | 343 | end -------------------------------------------------------------------------------- /QuadcopterNavigation/setupQuadcopter.jl: -------------------------------------------------------------------------------- 1 | ############### 2 | # OBCA: Optimization-based Collision Avoidance - a path planner for autonomous parking 3 | # Copyright (C) 2018 4 | # Alexander LINIGER [liniger@control.ee.ethz.ch; Automatic Control Lab, ETH Zurich] 5 | # Xiaojing ZHANG [xiaojing.zhang@berkeley.edu; MPC Lab, UC Berkeley] 6 | # 7 | # This program is free software: you can redistribute it and/or modify 8 | # it under the terms of the GNU General Public License as published by 9 | # the Free Software Foundation, either version 3 of the License, or 10 | # (at your option) any later version. 11 | # 12 | # This program is distributed in the hope that it will be useful, 13 | # but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 | # GNU General Public License for more details. 16 | # 17 | # You should have received a copy of the GNU General Public License 18 | # along with this program. If not, see . 19 | ############### 20 | # The paper describing the theory can be found here: 21 | # X. Zhang, A. Liniger and F. Borrelli; "Optimization-Based Collision Avoidance"; Technical Report, 2017, [https://arxiv.org/abs/1711.03449] 22 | ############### 23 | 24 | ############### 25 | # run this file before running main.jl 26 | ############### 27 | 28 | using JuMP, Ipopt, PyPlot 29 | include("QuadcopterDist.jl") 30 | include("QuadcopterSignedDist.jl") 31 | include("plotTrajQuadcopter.jl") 32 | include("a_star_3D.jl") 33 | include("constrSatisfaction.jl") 34 | 35 | 36 | clear() = run(@static is_unix() ? `clear` : `cmd /c cls`) -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # OBCA 2 | Optimization-Based Collision Avoidance - a path planner for autonomous navigation 3 | 4 | Paper describing the theory can be found [here](http://arxiv.org/abs/1711.03449). 5 | 6 | *Note*: An OBCA version specialized towards autonomous parking can be found at [H-OBCA](https://github.com/XiaojingGeorgeZhang/H-OBCA). 7 | 8 | ## Short Description 9 | OBCA is a novel method for formulating collision avoidance constraints. It provides a smooth reformulation of collision avoidance constraints, allowing the use of generic non-linear optimization solvers. 10 | 11 | OBCA can be used to in path planning algorithms to generate *high-quality paths* that satisfy the system dynamics as well as satefy constraints. We provide [Julia](https://julialang.org/)-based implementations for a quadcopter navigation problem and for autonomous parking problems. 12 | 13 | ## Examples 14 | 15 | 16 | ### OBCA for Quadcopter Navigation 17 | 18 | 19 | ### OBCA for Autonomous Parking 20 | 21 | #### Backwards Parking 22 | 23 | 24 | #### Parallel Parking 25 | 26 | 27 | #### Parking of Truck with Trailer 28 | 29 | 30 | -------------------------------------------------------------------------------- /images/TrajBack_ParkingVideo.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/XiaojingGeorgeZhang/OBCA/bc9d799e5c85a77ab0c5c3f87dec9a90d2e3e9a2/images/TrajBack_ParkingVideo.gif -------------------------------------------------------------------------------- /images/TrajPar_ParkingVideo.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/XiaojingGeorgeZhang/OBCA/bc9d799e5c85a77ab0c5c3f87dec9a90d2e3e9a2/images/TrajPar_ParkingVideo.gif -------------------------------------------------------------------------------- /images/TrajQuad_3D_Video.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/XiaojingGeorgeZhang/OBCA/bc9d799e5c85a77ab0c5c3f87dec9a90d2e3e9a2/images/TrajQuad_3D_Video.gif -------------------------------------------------------------------------------- /images/TrajTrailer_ParkingVideo.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/XiaojingGeorgeZhang/OBCA/bc9d799e5c85a77ab0c5c3f87dec9a90d2e3e9a2/images/TrajTrailer_ParkingVideo.gif --------------------------------------------------------------------------------