|
| 1 | +# # Viability tube for quadrotor |
| 2 | + |
| 3 | +#md # [](@__BINDER_ROOT_URL__/generated/Systems and Control/quadrotor.ipynb) |
| 4 | +#md # [](@__NBVIEWER_ROOT_URL__/generated/Systems and Control/quadrotor.ipynb) |
| 5 | +# **Adapted from**: [YAP21, Section V.D] for the model defined in [B12], [M16, Section IV] and [M19, Section 6.1] |
| 6 | +# |
| 7 | +# [B12] Bouffard, Patrick. |
| 8 | +# *On-board model predictive control of a quadrotor helicopter: Design, implementation, and experiments.* |
| 9 | +# CALIFORNIA UNIV BERKELEY DEPT OF COMPUTER SCIENCES, 2012. |
| 10 | +# |
| 11 | +# [M16] Mitchell, Ian M., et al. |
| 12 | +# *Ensuring safety for sampled data systems: An efficient algorithm for filtering potentially unsafe input signals.* |
| 13 | +# 2016 IEEE 55th Conference on Decision and Control (CDC). IEEE, 2016. |
| 14 | +# |
| 15 | +# [M19] Mitchell, Ian M., Jacob Budzis, and Andriy Bolyachevets. |
| 16 | +# *Invariant, viability and discriminating kernel under-approximation via zonotope scaling.* |
| 17 | +# Proceedings of the 22nd ACM International Conference on Hybrid Systems: Computation and Control. 2019. |
| 18 | +# |
| 19 | +# [YAP21] Yin, H., Arcak, M., Packard, A., & Seiler, P. (2021). |
| 20 | +# *Backward reachability for polynomial systems on a finite horizon.* |
| 21 | +# IEEE Transactions on Automatic Control, 66(12), 6025-6032. |
| 22 | + |
| 23 | +using Test #src |
| 24 | +using DynamicPolynomials |
| 25 | +@polyvar x[1:6] |
| 26 | +@polyvar u[1:2] |
| 27 | +sinx5 = -0.166 * x[5]^3 + x[5] |
| 28 | +cosx5 = -0.498 * x[5]^2 + 1 |
| 29 | +gravity = 9.81 |
| 30 | +gain_u1 = 0.89 / 1.4 |
| 31 | +d0 = 70 |
| 32 | +d1 = 17 |
| 33 | +n0 = 55 |
| 34 | +f = [ |
| 35 | + x[3], |
| 36 | + x[4], |
| 37 | + 0, |
| 38 | + -gravity, |
| 39 | + x[6], |
| 40 | + -d0 * x[5] - d1 * x[6], |
| 41 | +] |
| 42 | +n_x = length(f) |
| 43 | +g = [ |
| 44 | + 0 0 |
| 45 | + 0 0 |
| 46 | + gain_u1 * sinx5 0 |
| 47 | + gain_u1 * cosx5 0 |
| 48 | + 0 0 |
| 49 | + 0 n0 |
| 50 | +] |
| 51 | +n_u = size(g, 2) |
| 52 | + |
| 53 | +# The constraints below are the same as [YAP21, M16, M19] except |
| 54 | +# [M16, M19] uses different bounds for `x[2]` and |
| 55 | +# [M16] uses different bounds for `x[5]` |
| 56 | + |
| 57 | +using SumOfSquares |
| 58 | +rectangle = [1.7, 0.85, 0.8, 1, π/12, π/2, 1.5, π/12] |
| 59 | +X = BasicSemialgebraicSet(FullSpace(), typeof(x[1] + 1.0)[]) |
| 60 | +for i in eachindex(x) |
| 61 | + lower = x[i] + rectangle[i] # x[i] >= -rectangle[i] |
| 62 | + upper = rectangle[i] - x[i] # x[i] <= rectangle[i] |
| 63 | + addinequality!(X, lower * upper) # -rectangle[i] <= x[i] <= rectangle[i] |
| 64 | +end |
| 65 | +X |
| 66 | + |
| 67 | +## Controller for the linearized system |
| 68 | + |
| 69 | +# The starting value for the Lyapunov function is the linear state-feedback |
| 70 | +# that maintains the quadrotor at the origin [YAP21, Remark 3]. |
| 71 | + |
| 72 | +using SparseArrays |
| 73 | +x0 = zeros(n_x) |
| 74 | +u0 = [gravity/gain_u1, 0.0] |
| 75 | + |
| 76 | +# The linearization of `f` is given by |
| 77 | + |
| 78 | +x_dot = f + g * u |
| 79 | +A = map(differentiate(x_dot, x)) do a |
| 80 | + a(x => x0, u => u0) |
| 81 | +end |
| 82 | + |
| 83 | +# The linearization of `g` is given by: |
| 84 | + |
| 85 | +B = map(differentiate(x_dot, u)) do b |
| 86 | + b(x => x0, u => u0) |
| 87 | +end |
| 88 | + |
| 89 | +# We can compute the Linear-Quadratic Regulator using the same weight matrices |
| 90 | +# as [YAP21](https://github.com/heyinUCB/Backward-Reachability-Analysis-and-Control-Synthesis) |
| 91 | + |
| 92 | +import MatrixEquations |
| 93 | +S, v, K = MatrixEquations.arec(A, B, 100, 10) |
| 94 | +@test all(c -> real(c) < 0, v) #src |
| 95 | + |
| 96 | +# The corresponding quadratic regulator is: |
| 97 | + |
| 98 | +P, _, _ = MatrixEquations.arec(A - B * K, 0.0, 10.0) |
| 99 | + |
| 100 | +# It corresponds to the following quadratic Lyapunov function: |
| 101 | + |
| 102 | +V0 = x' * P * x |
| 103 | + |
| 104 | +# ## γ-step |
| 105 | + |
| 106 | +# It is a Lyapunov function for the linear system but not necessarily for the nonlinear system as well. |
| 107 | +# We can however say that the γ-sublevel set `{x | x' P x ≤ γ}` is a (trivial) controlled invariant set for `γ = 0` (since it is empty). |
| 108 | +# We can try to see if there a larger `γ` such that the γ-sublevel set is also controlled invariant using |
| 109 | +# the γ step of [YAP21, Algorithm 1] |
| 110 | + |
| 111 | +function _create(model, d, P) |
| 112 | + if d isa Int |
| 113 | + if P == SOSPoly # `d` is the maxdegree while for `SOSPoly`, |
| 114 | + d = div(d, 2) # it is the monomial basis of the squares |
| 115 | + end |
| 116 | + return @variable(model, variable_type = P(monomials(x, 0:d))) |
| 117 | + else |
| 118 | + return d |
| 119 | + end |
| 120 | +end |
| 121 | + |
| 122 | +using LinearAlgebra |
| 123 | +function base_model(solver, V, k, s3, γ) |
| 124 | + model = SOSModel(solver) |
| 125 | + V = _create(model, V, Poly) |
| 126 | + k = _create.(model, k, Poly) |
| 127 | + s3 = _create(model, s3, SOSPoly) |
| 128 | + ∂ = differentiate # Let's use a fancy shortcut |
| 129 | + @constraint(model, ∂(V, x) ⋅ (f + g * k) <= s3 * (V - γ)) # [YAP21, (E.2)] |
| 130 | + for r in inequalities(X) # `{V ≤ γ} ⊆ {r ≥ 0}` iff `r ≤ 0 => V ≥ γ` |
| 131 | + @constraint(model, V >= γ, domain = @set(r <= 0)) # [YAP21, (E.3)] |
| 132 | + end |
| 133 | + return model, V, k, s3 |
| 134 | +end |
| 135 | + |
| 136 | +function γ_step(solver, V, γ_min, degree_k, degree_s3; γ_tol = 1e-1, max_iters = 10, γ_step = 0.5) |
| 137 | + γ0_min = γ_min |
| 138 | + γ_max = Inf |
| 139 | + num_iters = 0 |
| 140 | + k_best = s3_best = nothing |
| 141 | + while true |
| 142 | + if γ_max - γ_min > γ_tol && num_iters < max_iters |
| 143 | + if isfinite(γ_max) |
| 144 | + γ = (γ_min + γ_max) / 2 |
| 145 | + else |
| 146 | + γ = γ0_min + (γ_min - γ0_min + γ_step) * 2 |
| 147 | + end |
| 148 | + elseif isnothing(k_best) |
| 149 | + @assert γ_min == γ0_min |
| 150 | + @info("Bisection finished without a feasible controller, let's find one") |
| 151 | + γ = γ0_min # Last run to compute a controller for the value of `γ` we know is feasible |
| 152 | + else |
| 153 | + break |
| 154 | + end |
| 155 | + model, V, k, s3 = base_model(solver, V, degree_k, degree_s3, γ) |
| 156 | + num_iters += 1 |
| 157 | + @info("Iteration $num_iters/$max_iters : Solving with $(solver_name(model)) for `γ = $γ`") |
| 158 | + optimize!(model) |
| 159 | + @info("After $(solve_time(model)) seconds, terminated with $(termination_status(model)) ($(raw_status(model)))") |
| 160 | + if primal_status(model) == MOI.FEASIBLE_POINT |
| 161 | + @info("Feasible solution found") |
| 162 | + γ_min = γ |
| 163 | + k_best = value.(k) |
| 164 | + s3_best = value(s3) |
| 165 | + elseif dual_status(model) == MOI.INFEASIBILITY_CERTIFICATE |
| 166 | + @info("Infeasibility certificate found") |
| 167 | + if γ == γ0_min # This corresponds to the case above where we reached the tol or max iteration and we just did a last run at the value of `γ_min` provided by the user |
| 168 | + error("The value `$γ0_min` of `γ_min` provided is not feasible") |
| 169 | + end |
| 170 | + γ_max = γ |
| 171 | + else |
| 172 | + @warn("Giving up $(raw_status(model)), $(termination_status(model)), $(primal_status(model)), $(dual_status(model))") |
| 173 | + break |
| 174 | + end |
| 175 | + if γ != γ0_min |
| 176 | + @info("Refined interval : `γ ∈ [$γ_min, $γ_max[`") |
| 177 | + end |
| 178 | + end |
| 179 | + if !isfinite(γ_max) && max_iters > 0 |
| 180 | + @warn("Cannot find any infeasible `γ` after $num_iters iterations") |
| 181 | + end |
| 182 | + return γ_min, k_best, s3_best |
| 183 | +end |
| 184 | + |
| 185 | +import CSDP |
| 186 | +solver = optimizer_with_attributes(CSDP.Optimizer, MOI.Silent() => true) |
| 187 | +γ1, κ1, s3_1 = γ_step(solver, V0, 0.0, [2, 2], 2) |
| 188 | +@test γ1 ≈ 0.5 atol = 1.e-1 #src |
| 189 | + |
| 190 | +# Let's visualize now the controlled invariant set we have found: |
| 191 | + |
| 192 | +using ImplicitPlots |
| 193 | +using Plots |
| 194 | +import ColorSchemes |
| 195 | +function plot_lyapunovs(Vs, J; resolution = 1000, scheme = ColorSchemes.rainbow) |
| 196 | + xmax = rectangle[J[1]] |
| 197 | + ymax = rectangle[J[2]] |
| 198 | + rect_xs = [xmax, -xmax, -xmax, xmax, xmax] |
| 199 | + rect_ys = [ymax, ymax, -ymax, -ymax, ymax] |
| 200 | + p = plot(rect_xs, rect_ys, label="", color=:black) |
| 201 | + eliminated = x[setdiff(1:n_x, J)] |
| 202 | + for i in eachindex(Vs) |
| 203 | + V = subs(Vs[i], eliminated => zeros(length(eliminated))) |
| 204 | + linecolor = get(scheme, (i - 1) / max(length(Vs) - 1, 1)) |
| 205 | + implicit_plot!(p, V; resolution, label="V$i", linecolor) |
| 206 | + end |
| 207 | + xlim = 1.05 * xmax |
| 208 | + ylim = 1.05 * ymax |
| 209 | + plot!(p, xlims=(-xlim, xlim), ylims=(-ylim, ylim), aspect_ratio = xmax / ymax) |
| 210 | + return p |
| 211 | +end |
| 212 | +Vs = [V0 - γ1] |
| 213 | +plot_lyapunovs(Vs, [1, 2]) |
| 214 | + |
| 215 | +# ## V-step |
| 216 | + |
| 217 | +# Let's now fix the control law that we have found and try to find a superset |
| 218 | +# of the current controlled invariant set |
| 219 | +# That corresponds to the V-step of [YAP21, Algorithm 1]: |
| 220 | + |
| 221 | +_degree(d::Int) = d |
| 222 | +_degree(V) = maxdegree(V) |
| 223 | + |
| 224 | +function V_step(solver, V0, γ, k, s3) |
| 225 | + model, V, k, s3 = base_model(solver, _degree(V0), k, s3, γ) |
| 226 | + if !(V0 isa Int) # {V0 ≤ γ} ⊆ {V ≤ γ} iff V0 ≤ γ => V ≤ γ |
| 227 | + @constraint(model, V <= γ, domain = @set(V0 <= γ)) # [YAP21, (E.6)] |
| 228 | + end |
| 229 | + optimize!(model) |
| 230 | + return model, value(V) |
| 231 | +end |
| 232 | + |
| 233 | +model, V1 = V_step(solver, V0, γ1, κ1, s3_1) |
| 234 | +solution_summary(model) |
| 235 | + |
| 236 | +# We can see that the solver found a feasible solution. |
| 237 | +# Let's compare it: |
| 238 | + |
| 239 | +push!(Vs, V1 - γ1) |
| 240 | +plot_lyapunovs(Vs, [1, 2]) |
| 241 | + |
| 242 | +# ## Continue iterating |
| 243 | + |
| 244 | +# We could now find a better state feedback controller with this new Lyapunov. |
| 245 | +# Given the plot, we see that `γ` cannot be much improved, we mostly |
| 246 | +# want to find a better `κ` so let's set `max_iters` to zero |
| 247 | + |
| 248 | +γ2, κ2, s3_2 = γ_step(solver, V0, γ1, [2, 2], 2, max_iters = 0) |
| 249 | + |
| 250 | +# Let's see if this new controller allows to find a better Lyapunov. |
| 251 | + |
| 252 | +model, V2 = V_step(solver, V0, γ2, κ2, s3_2) |
| 253 | +solution_summary(model) |
| 254 | + |
| 255 | +# It does not seem that we gained any improvement so let's stop: |
| 256 | + |
| 257 | +push!(Vs, V2 - γ2) |
| 258 | +plot_lyapunovs(Vs, [1, 2]) |
0 commit comments