The polynomial-quadratic regulator (PQR) is an extension to the Linear Quadratic Regulator (LQR). For continuous-time systems on the form,

$$$\dot{x} = f(x, u) = Ax + Bu + A_2 (x \otimes x) + A_3 (x \otimes x \otimes x) + \cdots$$$

PQR finds a state-feedback controller $u = K(x)$ that optimizes the familiar quadratic cost function

$$$J(x, u) = \int_0^\infty \left( x^T Q x + u^T R u \right) dt$$$

where $Q$ and $R$ are symmetric positive definite matrices.

Here, the dynamics $f$ is characterized by being polynomial in $x$ and linear in $u$ (input affine), while the cost is quadratic. If $A_i = 0 \, \forall\, i \geq 2$, PQR reduces to the familiar LQR controller. In many situations, non-polynomial terms in $f$ can be approximated using polynomials using the function poly_approx. Several examples of this will be shown below.

Polynomial-quadratic control problems can be solved using the function pqr, which takes either $f(x, u)$ as a function, or the matrices $A$ and $B$ as well as a vector of matrices $N = \left[ A_2, A_3, \cdots \right]$. pqr returns a PQRSolution which can be passed to build_K_function to perform code generation and return controller functions $u = K(x)$ and $K!(u, x)$. The solution can also be passed into the function predicted_cost to compute the predicted infinite-horizon cost achieved by the controller from a particular initial state.

Example

The following example, which is taken from [1], demonstrates how to use the PQR controller design to solve the Lorenz system with added control inputs:

\begin{aligned} \dot{x}_1(t) &= -10 x_1(t) + 10 x_2(t) + u(t) \\ \dot{x}_2(t) &= 28 x_1(t) - x_2(t) - x_1(t)x_3(t)\\ \dot{x}_3(t) &= x_1(t)x_2(t) - 8/3 x_3(t) \\ \end{aligned}

We start by defining the system dynamics and cost function, and then solve the optimal control problem using pqr. We then use build_K_function to generate a controller function $u = K(x)$ and simulate the closed-loop system using solve from OrdinaryDiffEq. To compute the cost achieved by the controller, we add the cumulative integral of the cost as an additional state to the closed-loop dynamics and let the ODE integrator compute the integral for us. We compare this cost to the cost predicted by predicted_cost which uses the approximative value function obtained from solving the PQR problem (similar to the Ricatti solution obtained when solving an LQR problem).

using JuliaSimControl, Test, Plots, LinearAlgebra, OrdinaryDiffEq, StaticArrays

nx = 3 # Number of state variables
nu = 1 # Number of control inputs
A = [
-10 10 0
28 -1 0
0 0 -8/3
]
B = [1; 0; 0;;]
A2 = zeros(nx, nx^2)
A2[2,3] = A2[2,7] = -1/2
A2[3,2] = A2[3,4] = 1/2

Q = diagm(ones(3)) # State cost matrix
R = diagm(ones(1)) # Control input cost matrix

f = (x, u) -> A * x + B * u + A2 * kron(x, x) # Dynamics for simulation
l = (x, u) -> dot(x, Q, x) + dot(u, R, u)     # Cost function for simulation

function closed_loop(xc, (f,l,K), t)          # Closed-loop dynamics for simulation
x = xc[1:end-1] # Last value is integral of cost
u = K(x)
dx = f(x, u)
dc = l(x, u)
[dx; dc]        # Return state and cost derivatives
end

x0 = Float64[10, 10, 10, 0] # Initial state, last value is integral of cost
tspan = (0.0, 50.0)
prob = ODEProblem(closed_loop, x0, tspan, (f, l, x->x))

# Design controller of degree 2
pqrsol = pqr(A, B, [A2], Q, R, 2) # It's possible to pass in f instead of A, B, A2
K, _ = build_K_function(pqrsol) # Returns u = K(x) and K!(u, x)
c = predicted_cost(pqrsol, x0[1:end-1])
@test c ≈ 7062.15 atol=0.02 # Compare to paper
sol = solve(prob, Rodas5P(), p=(f,l,K), reltol=1e-8, abstol=1e-8);
c = sol.u[end][end]
@test c ≈ 6911.03 rtol=1e-2 # Compare to paper

# Design controller of degree 4
pqrsol = pqr(A, B, [A2], Q, R, 4)
K, _ = build_K_function(pqrsol)
c = predicted_cost(pqrsol, x0[1:end-1])
@test c ≈ 6924.27 atol=0.02
sol = solve(prob, Rodas5P(), p=(f,l,K), reltol=1e-8, abstol=1e-8);
c = sol.u[end][end]
@test c ≈ 6906.21 rtol=1e-2

# Simulate shorter horizon for plotting
sol = solve(prob, Rodas5P(), p=(f,l,K), tspan = (0, 2.5), reltol=1e-8, abstol=1e-8);
plot(sol, layout=4, title=["x1" "x2" "x3" "Cumulative cost"], legend=false, framestyle=:zerolines)

Inspecting the controller $K(x)$ and value function $V(x)$

The controller $K(x)$ generated by solving the PQR problem is a polynomial function of the state:

$$$K(x) = k_1 x + k_2 (x \otimes x) + k_3 (x \otimes x \otimes x) + \cdots$$$

The struct pqrsol::PQRSolution = pqr(...) contains the fields Ks::Vector{Matrix} and Vs::Vector{Matrix}, where each entry in Ks corresponds to the Ks[i] = k_i arrays above. Similarly, Vs contains the matrices in the approximate value function $V(x)$ (an approximate Lyapunov function, sometimes also called cost-to-go):

$$$V(x) = v_1 (x \otimes x) + v_2 (x \otimes x \otimes x) + \cdots$$$

Note, the value function does not have a linear term like the controller does. The value function can be evaluated using the function predicted_cost, which returns the predicted infinite-horizon cost from a particular state $x$.

The controller corresponds to a polynomial, to have a look at this polynomial (rather than the matrices Ks), we can trace through it with symbolic variables. If we have obtained K from build_k_function, we would do

using JuliaSimControl.DynamicPolynomials
@polyvar x[1:nx]
K(x)

if we haven't built a function, we could also do

using JuliaSimControl.DynamicPolynomials
@polyvar x[1:nx] # Alternatively: Symbolics.@variables x[1:nx]
JuliaSimControl.Kfun(pqrsol, 2)(x) # Look at the second-degree controller
1-element Vector{DynamicPolynomials.Polynomial{DynamicPolynomials.Commutative{DynamicPolynomials.CreationOrder}, MultivariatePolynomials.Graded{MultivariatePolynomials.LexOrder}, Float64}}:
-18.49064811180869x₂ - 23.71166406840945x₁ + 0.06701176224936817x₂x₃ + 0.5811664706751598x₁x₃

The controller is a now a vector of polynomials, in this case, there is only one entry since the system has a single input.

Example with non-zero reference

The following example uses the same dynamical system as Example: Dynamical extremum seeking

\begin{aligned} \dot{x}_1(t) &= x_1(t)^2 + x_2(t) + u(t) \\ \dot{x}_1(t) &= x_1(t)^2 - x_2(t)\\ \end{aligned}

In the extremum-seeking example, the goal was to reach the unknown state and control input that optimized an performance function. In this example, we assume that the optimizing operating point is known, and design a polynomial-quadratic controller that stabilizes the system around this operating point. We thus create two versions of the dynamics, $f$ operates in absolute coordinates, while $fr$ operates in coordinates relative to the operating point $\Delta x = x - x_r$ and $\Delta u = u - u_r$. The controller is designed using $f_r$ and operates in relative coordinates, but the closed-loop system is simulated using $f$ and operates in absolute coordinates.

xr = [0.5, 0.25]
ur = [-0.5]
f = function (x, u) # Operates in standard coordinates
x1,x2 = x
[
x1^2 + x2 + u[]
x1^2 - x2
]
end

fr = (Δx,Δu) -> f(Δx + xr, Δu + ur) # Operates in Δx, Δu coordinates

@assert f(xr, ur) ≈ [0, 0]                # Verify that xr, ur is an equilibrium point
@assert fr(zeros(2), zeros(1)) ≈ [0, 0]

function closed_loop_r(xc, (f,l,K), t) # Closed-loop dynamics for simulation
x = xc[1:end-1] # Last value is integral of cost
Δx = x - xr
Δu = K(Δx)      # K operates on Δx
u = Δu + ur
dx = f(x, u)
dc = l(Δx, Δu)
[dx; dc]        # Return state and cost derivatives
end

Q = I(2)
R = I(1)

pqrsol = pqr(fr, Q, R, 3)
time = 0:0.01:6
plot(layout=4)
for deg = 1:3
K, _ = build_K_function(pqrsol, deg)
prob = ODEProblem(closed_loop_r, zeros(3), (0.0, 6.0), (f, l, K))
sol = solve(prob, Tsit5());
plot!(sol, layout=3, title=["x1" "x2" "Cumulative cost"], lab="Degree deg") u = map(time) do t x = sol(t)[1:end-1] K(x - xr) + ur end u = reduce(vcat, u) plot!(time, u, sp=4, title="u") end hline!(xr', c=:black, ls=:dash, label="Reference") hline!(ur', c=:black, ls=:dash, label="Reference", sp=4) The figure above compares the closed-loop response for different degrees of the controller, in this case, a second-order controller appears sufficient. Example: Quadrotor with non-affine inputs in MTK This example will showcase a more realistic scenario that demonstrates a number of interesting things: • How to use ModelingToolkit to model the system • How to handle non-polynomial terms • How to handle non-affine inputs The system we will consider is a quadrotor with dynamics in a plane, that is, we consider one translation and one rotation degree of freedom. The dynamics are given by \begin{aligned} \dot{y} &= v \\ \dot{v} &= -g + K_u \cos(\phi)u_1 \\ \dot{\phi} &= \omega \\ \dot{\omega} &= -d_0 \phi - d_1 ω + n_0 u_2 \end{aligned} wherey$is the vertical position,$v$is the vertical velocity,$\phi$is the pitch angle, and$\omega$is the pitch rate. The control inputs are$u_1$which is the thrust, and$u_2$which is the pitching torque. The parameters$K_u$,$d_0$,$d_1$, and$n_0$are constants, while$g$is the gravitational constant. This model exhibits the following problematic aspects for the PQR controller design: • The dynamics are non-polynomial due to the$\cos$term • The input dynamics are non-affine due to the multiplication of the input$u_1$with a state-dependent term. We will solve the first problem by approximating the$\cos$term using a third-order polynomial, and the second problem by introducing a low-pass filter on the control input$u_1$. In Exact handling of trigonometric terms below, we show an alternative approach to handle trigonometric terms, by introducing additional state variables. Polynomial approximation To approximate$\cos(\phi)$, we could either linearize it (small-angle approximation), use a Taylor-approximation of higher degree, or perform a least-squares approximation over a certain interval. Here, we will take the latter approach, and use const cospoly = JuliaSimControl.poly_approx(cos, 5, -2, 2) |> splat(tuple) (0.997550275987648, 0.0, -0.48881690784591164, 0.0, 0.03399571980756845, 0.0) to approximate$\cos$with a fifth-order polynomial over the interval$[-2, 2]$rad. Unsurprisingly, we see that the odd terms are zero. We use these coefficients to define a function cos5 that computes the approximate value of cos using the polynomial coefficients. cos5(x) = evalpoly(x, cospoly) cos5 (generic function with 1 method) The approximation is reasonably accurate over the considered interval, much more accurate towards the edges than a Taylor approximation of the same degree: using Plots plot([ cos, cos5, x->1-x^2/2+x^4/24, # 5:th order Taylor approximation ], -2, 2, lab=["cos" "5:th order LS-approx" "5:th order Taylor approx"] ) Handling of non-affine inputs The PQR design method assumes that$u$enters the dynamics linearly, i.e., like$\dot x = \cdots + Bu$where$B$is a matrix. Here, we have a term$K_u \cos(\phi)u_1$which is a nonlinear function of the input and a state variable$\phi$. To handle this, we introduce a first-order low-pass filter on the input, after which the input$u_1$affects the filter state$x_f$linearly$T \dot x_f = -x_f + u_1$, and the filter state instead appears in the equation$K_u \cos(\phi)x_f. This trick can actually be motivated from a physical perspective, since the motor that turns the propeller has its own dynamics and those are not included in the simple model above: a change in voltage across the motor is not going to result in an immediate change in thrust. Here, we model the motor dynamics as a first-order low-pass filter. The full dynamics is now given by \begin{aligned} \dot{y} &= v \\ \dot{v} &= -g + K_u \cos(\phi)x_f \\ \dot{\phi} &= \omega \\ \dot{\omega} &= -d_0 \phi - d_1 ω + n_0 u_2 \\ \dot{x}_f &= (-x_f + u_1) / T \end{aligned} The system model We are now ready to assemble the full model: using JuliaSimControl using OrdinaryDiffEq using ModelingToolkit using ModelingToolkitStandardLibrary: Blocks using ControlSystemsMTK using Plots using LinearAlgebra gravity = 9.81 Ku = 0.89 / 1.4 d0 = 70 d1 = 17 n0 = 55 @named motor_dynamics = Blocks.FirstOrder(T = 0.001) x0 = [0.85, 1, π/12, π/2] @parameters t @variables u(t)[1:2]=0 u = collect(u) @variables y(t)=0.85 v(t)=1 ϕ(t)=π/12 ω(t)=π/2 D = Differential(t) eqs = [ D(y) ~ v, D(v) ~ -gravity + Ku * cos5(ϕ)*(motor_dynamics.output.u + gravity/Ku), D(ϕ) ~ ω, D(ω) ~ -d0 * ϕ - d1 * ω + n0 * u[2], motor_dynamics.input.u ~ u[1] ] @named model = ODESystem(eqs, t; systems=[motor_dynamics]) \begin{align} \frac{\mathrm{d} y\left( t \right)}{\mathrm{d}t} =& v\left( t \right) \\ \frac{\mathrm{d} v\left( t \right)}{\mathrm{d}t} =& -9.81 + 0.63571 \left( 0.99755 + \left( \phi\left( t \right) \right)^{2} \left( -0.48882 + 0.033996 \left( \phi\left( t \right) \right)^{2} \right) \right) \left( 15.431 + \mathrm{motor}_{dynamics_{+}output_{+}u}\left( t \right) \right) \\ \frac{\mathrm{d} \phi\left( t \right)}{\mathrm{d}t} =& \omega\left( t \right) \\ \frac{\mathrm{d} \omega\left( t \right)}{\mathrm{d}t} =& 55 u(t)_2 - 70 \phi\left( t \right) - 17 \omega\left( t \right) \\ \mathrm{motor}_{dynamics_{+}input_{+}u}\left( t \right) =& u(t)_1 \\ \mathrm{motor}_{dynamics_{+}u}\left( t \right) =& \mathrm{motor}_{dynamics_{+}input_{+}u}\left( t \right) \\ \mathrm{motor}_{dynamics_{+}y}\left( t \right) =& \mathrm{motor}_{dynamics_{+}output_{+}u}\left( t \right) \\ \mathrm{motor}_{dynamics_{+}y}\left( t \right) =& \mathrm{motor}_{dynamics_{+}x}\left( t \right) \\ \frac{\mathrm{d} \mathrm{motor}_{dynamics_{+}x}\left( t \right)}{\mathrm{d}t} =& \frac{ - \mathrm{motor}_{dynamics_{+}x}\left( t \right) + motor_{dynamics_{+}k} \mathrm{motor}_{dynamics_{+}u}\left( t \right)}{motor_{dynamics_{+}T}} \end{align} Here, we applied additional steady-state gravity compensation to the control input by adding gravity/Ku to the input. This is not strictly necessary, but it makes the controller tuning simpler and we do not have to worry about integral action. In practice, we should of course handle also this if we intend to accurately control the altitudey$. To obtain the dynamics in the form of a function$\dot x = f(x, u, p, t)$, we construct a FunctionSystem; quadrotor = FunctionSystem(model, u, [y, ϕ]) (; nx, nu) = quadrotor The cost function To assemble the matrix$Q$, we make use of the function ControlSystemsMTK.build_quadratic_cost_matrix. This function takes care of the fact that we have no control over the order in which the state variables appear in the state vector. However, to build the matrix$R$, we can rely on the fact that the control inputs appear in the same order as they are defined when calling the FunctionSystem constructor above. R = diagm([1.0, 10.0]) Q = ControlSystemsMTK.build_quadratic_cost_matrix(model, u, [y => 10, v => 10, ϕ => 1, ω => 0.01] ) 5×5 Matrix{Float64}: 10.0 0.0 0.0 0.0 0.0 0.0 10.0 0.0 0.0 0.0 0.0 0.0 1.0 0.0 0.0 0.0 0.0 0.0 0.01 0.0 0.0 0.0 0.0 0.0 0.0 Simulation To perform a simulation, we first extract the default values of the parameters and initial conditions from the model, and then use these to define a function f that computes the closed-loop dynamics. We then use pqr to solve the PQR problem, and build_K_function to generate a controller function u = K(x) that computes the control input. We can then use solve from OrdinaryDiffEq to simulate the closed-loop system and plot the result. We compare the result for different degrees of the controller, 1 t0 3. tspan = (0.0, 8.0) p = float.([ModelingToolkit.defaults(model)[p] for p in quadrotor.p]) x0sim = float.([ModelingToolkit.defaults(model)[p] for p in quadrotor.x]) f = (x,u)->quadrotor(x,u,p,nothing) l = (x, u) -> dot(x, Q, x) + dot(u, R, u) "Closed-loop dynamics with cumulative cost added as last state variable" function f_closed_loop(xc, K, t) x = xc[1:end-1] u = K(x) ẋ = quadrotor(x, u, p, nothing) [ẋ; l(x, u)] end prob = ODEProblem(f_closed_loop, SVector{nx + 1}(x0sim..., 0), tspan, x->x) pqrsol = pqr(f, Q, R, 3) fig = plot(layout=8) for deg in 1:3 K, _ = build_K_function(pqrsol, deg) @info "Simulating degree$deg"
sol = solve(prob, Tsit5(), p = K)
plot!(sol, title=permutedims([state_names(quadrotor); "Cost"]), lab="Degree $deg") time = range(tspan..., 100) uv = map(time) do t x = sol(t) K(x[1:end-1]) end um = reduce(hcat, uv)' plot!(time, um, title=permutedims(input_names(quadrotor)), sp=(1:2)' .+ (nx+1), lab="Degree$deg")
end
fig

If we start the simulation further away from the origin (5x further away than above), we need to re-tune the weights a bit for the controller to perform well. In this case, it's enough to increase the penalty on the pitch-torque input in order for the controller not to perform too aggressive pitching motions that bring the system too far away from the validity region of our polynomial approximation to $\cos$. This time, we add also a degree 5 controller since we are expecting a higher-order controller to perform better further away from the origin.

R = diagm([1.0, 100.0])
pqrsol = pqr(f, Q, R, 5)
fig = plot(layout=9)
for deg in 1:2:5
K, _ = build_K_function(pqrsol, deg)
sol = solve(prob, Tsit5(), p = K, u0 = SVector{nx + 1}(5x0sim..., 0))
lab = "Degree $deg" plot!(sol; title=permutedims([state_names(quadrotor); "Cost"]), lab) time = range(tspan..., 100) uv = map(time) do t x = sol(t) K(x[1:end-1]) # Reconstruct control input for plotting end um = reduce(hcat, uv)' plot!(time, um; title=permutedims(input_names(quadrotor)), sp=(1:2)' .+ (nx+1), lab) # Evaluate approximate Lyapunov function along trajectory Vt = [predicted_cost(pqrsol, sol(t)[1:nx]) for t in time] plot!(time, Vt; title="Lyapunov function V(x)", sp=9, lab) end fig Exact handling of trigonometric terms In the example with the quadrotor above, we performed a polynomial least-squares approximation of the trigonometric term in order to enable PQR design. Simple trigonometric terms can also be handled exactly by introducing two new state variables:$s = \sin(\phi)$and$c = \cos(\phi). The dynamics are then given by \begin{aligned} \dot{x}_f &= (-x_f + u_1) / \tau \\ \dot{y} &= v \\ \dot{v} &= -g + K_u c x_f \\ \dot{\phi} &= \omega \\ \dot{\omega} &= -d_0 \phi - d_1 ω + n_0 u_2 \\ \dot{s} &= c \omega \\ \dot{c} &= -s \omega \end{aligned} here, we use the same trick as in Handling of non-affine inputs to handle the nonlinear input termK_u \cos(\phi)u_1$. The dynamics are now polynomial in the state and input, and we can use PQR to design a controller. One problem that will arise when adding the two new state variables is that the resulting system is not linearly controllable in the origin, and the solver for the Ricatti equation will fail when trying to find the first-order feedback gain. This is handled automatically by pqr, which will perform model-order reduction and design a feedback gain for the controllable subsystem. Another slight complication is that the state variables$s$and$c$are fictional, and we must thus compute those as part of the controller. Below, we demonstrate the workflow for this approach. We 1. Define the system dynamics in expanded form, polyquad, and use this function for the pqr design. 2. Define the system dynamics standard form, quad, and use this function for simulation. 3. Define the closed-loop system dynamics, f_cl_polyquad, where we compute the extended state xe = [x; sin(x[4]); cos(x[4])] that serves as the input to the controller. 4. Simulate and plot the result. using JuliaSimControl, OrdinaryDiffEq, Plots, LinearAlgebra function polyquad(state, u, p, t) g = 9.81; Ku = 0.89 / 1.4; d0 = 70; d1 = 17; n0 = 55; τ = 0.001 xf, y, v, ϕ, ω, s, c = state [ (-xf + u[1]) / τ v -g + Ku * c * xf ω -d0 * ϕ - d1 * ω + n0 * u[2] c*ω -s*ω ] end function quad(state, u, p, t) g = 9.81; Ku = 0.89 / 1.4; d0 = 70; d1 = 17; n0 = 55; τ = 0.001 xf, y, v, ϕ, ω = state [ (-xf + u[1]) / τ v -g + Ku * cos(ϕ) * xf ω -d0 * ϕ - d1 * ω + n0 * u[2] ] end ϵ = 1e-6 nx = 5 # State dimension of original dynamics nu = 2 # Input dimension Q = diagm(Float64[ϵ, 10, 10, 1, 0.01, ϵ, ϵ]) R = diagm(Float64[1, 100]) xr = [0,0,0,0,0,sin(0), cos(0)] # Reference state ur = [9.81/(0.89 / 1.4), 0] # Reference input (counteract gravity using u1) fr = (Δx,Δu) -> polyquad(Δx + xr, Δu + ur, 0, 0) # Dynamics that operates in Δx, Δu coordinates pqrsol = pqr(fr, Q, R, 3) function f_cl_polyquad(xc, K, t) x = xc[1:end-1] xe = [x; sin(x[4]); cos(x[4])] Δx = xe - xr Δu = K(Δx) u = Δu + ur dx = quad(x, u, 0, t) dc = dot(Δx, Q, Δx) + dot(Δu, R, Δu) # Cost integrand [dx; dc] end x0 = 5*[0, 0.85, 1, π/12, π/2, 0] # Initial state, last value is integral of cost tspan = (0.0, 8.0) prob = ODEProblem(f_cl_polyquad, x0, tspan) fig = plot(layout=nx+3) for deg in [1,3] K, _ = build_K_function(pqrsol, deg) sol = solve(prob, Tsit5(), p = K) t = range(tspan..., 300) u = map(t) do t x = sol(t)[1:end-1] xe = [x; sin(x[4]); cos(x[4])] K(xe-xr) end u = reduce(hcat, u)' if isinteractive() lab = "deg=$deg"
plot!(sol; title=["xf" "y" "v" "ϕ" "ω" "cost"], lab)
plot!(t, u; sp=(1:nu)' .+ (nx+1), title=["u1" "u2"], lab)
end
end
fig

Vector-valued polynomial approximation

Above, we approximated trigonometric functions using low-order polynomials. However, we can also approximate complete vector-valued dynamics functions using polynomials. The function poly_approx takes a vector-valued dynamics function ẋ = f(x, u), lower and upper bounds on the domain of $x$ and $u$ as well as the monomial degrees for each state variables x[i].

In the example below, we will demonstrate PQR control design for a pendulum on a cart.

PQR for pendulum on cart

We choose a coordinate system such that $\phi = 0$ corresponds to the pendulum pointing upwards (the unstable equilibrium). The state is comprised of

$$$x = [p, \phi, v, \dot \phi, x_f]$$$

where $p$ is the position of the cart, $\phi$ is the angle of the pendulum, $v$ is the velocity of the cart, $\dot \phi$ is the angular velocity of the pendulum, and $x_f$ is the state of the motor dynamics. The input is the commanded force to be applied to the cart.

function cartpole(x, u, p, t)
mc, mp, l, g = 1.0, 0.2, 0.5, 9.81
τ = 0.001

ϕ  = x[2] - π # Origin is now at upward equilibrium
qd = x[SA[3, 4]]
xf = x[5] # Filter state
s = sin(ϕ)
c = cos(ϕ)

H = [mc+mp mp*l*c; mp*l*c mp*l^2]
C = [0 -mp*qd[2]*l*s; 0 0]
G = [0, mp * g * l * s]
B = [1, 0]
qdd = -H \ (C * qd + G - B * xf)
dxf = (-xf + u[1]) / τ
return [qd; qdd; dxf]
end

nx = 5
nu = 1

l = (x, u) -> dot(x, Q, x) + dot(u, R, u)
f = (x, u) -> cartpole(x, u, 0, 0)
#37 (generic function with 1 method)

To approximate the function cartpole using polynomials, we choose upper and lower bounds for the state and inputs, $l_x$, $u_x$, $l_u$, and $u_u$. We also choose a degree deg for the polynomial approximation of the state dynamics, deg can be a single integer, or a vector of the same length as the number of state variables. In the latter case, monomials of x[i] including up to degree deg[i] are included. Here, we choose a high degree for the angle state $\phi$ to accurately capture the nonlinearity over a reasonably large domain, but lower orders for the other state variables. We approximate the trigonometric terms over the domain $±0.4\pi$ only, since the accuracy of the approximation appear to be more important close to the unstable equilibrium than far away from it. The function poly_approx returns a function special struct that can be passed to pqr in place of the standard dynamics function.

deg = [1, 5, 2, 2, 1] # p, ϕ, v, dϕ, xf
ux = [4, 0.4pi, 5, 5, 100]
lx = -ux
uu = fill(10, nu)
lu = -uu
cartpole_pol = JuliaSimControl.poly_approx(f; deg, ux, lx, uu, lu)
JuliaSimControl.DecomposedDynamics(DynamicPolynomials.Variable{DynamicPolynomials.Commutative{DynamicPolynomials.CreationOrder}, MultivariatePolynomials.Graded{MultivariatePolynomials.LexOrder}}[x₁, x₂, x₃, x₄, x₅], DynamicPolynomials.Variable{DynamicPolynomials.Commutative{DynamicPolynomials.CreationOrder}, MultivariatePolynomials.Graded{MultivariatePolynomials.LexOrder}}[u₁], DynamicPolynomials.Polynomial{DynamicPolynomials.Commutative{DynamicPolynomials.CreationOrder}, MultivariatePolynomials.Graded{MultivariatePolynomials.LexOrder}, Float64}[x₃, 0.9999999999999991x₄, -0.0017815281990694943 - 5.6408200476502355e-5u₁ + 0.9979126812612161x₅ + 5.147457684264136e-5x₄ - 0.00023039959645118274x₃ + 1.8645155877690602x₂ + 0.00032841492670724915x₁ + 7.3002600159984245e-6x₄x₅ - 5.735128508388896e-6x₄² - 2.102180093165783e-6x₃x₅ - 1.2189741326200306e-5x₃x₄ + 0.00011459311856741478x₃² - 2.8042741276633284e-5x₂x₅ - 0.00031554966777570814x₂x₄ - 0.0003945401267823713x₂x₃ + 0.0034679930574335726x₂² + 3.1850160432587324e-6x₁x₅ - 4.9256607799829215e-5x₁x₄ + 1.9393300742116048e-5x₁x₃ + 0.000404647237569667x₁x₂ - 2.1800764974496244e-6x₄²x₅ - 6.209007533249537e-8x₃x₄x₅ + 8.109171280008978e-6x₃x₄² - 1.775757642347385e-6x₃²x₅ - 1.8775434495138445e-5x₃²x₄ - 1.7671648862155179e-6x₂x₄x₅ - 0.0945826142483198x₂x₄² - 2.09509924470032e-6x₂x₃x₅ - 9.027108452690217e-6x₂x₃x₄ - 4.210200280276567e-5x₂x₃² - 0.17299811130179854x₂²x₅ - 0.0009697147441204209x₂²x₄ + 0.0008702596483502859x₂²x₃ - 1.303275303706061x₂³ - 1.5210103855828756e-6x₁x₄x₅ - 3.0318785049393667e-5x₁x₄² - 1.3489105427219908e-6x₁x₃x₅ + 1.8502660127931498e-6x₁x₃x₄ - 2.83732180517079e-5x₁x₃² - 1.189268566460592e-5x₁x₂x₅ - 2.301295967509348e-5x₁x₂x₄ + 0.0001069669714412329x₁x₂x₃ + 0.0004138663943273265x₁x₂² - 1.1319308679544261e-8x₃x₄²x₅ - 2.9017378923289387e-7x₃²x₄x₅ - 4.671147738081336e-6x₃²x₄² + 1.925748881911166e-7x₂x₄²x₅ + 1.5262030029168133e-8x₂x₃x₄x₅ + 3.5372834631943816e-6x₂x₃x₄² + 1.6801620485365152e-7x₂x₃²x₅ - 8.584104065020948e-6x₂x₃²x₄ - 3.700584872809849e-6x₂²x₄x₅ + 3.246554725701869e-5x₂²x₄² + 3.949872489831553e-6x₂²x₃x₅ - 1.3307080562837061e-5x₂²x₃x₄ - 3.8198005796961675e-5x₂²x₃² + 2.016078929964621e-5x₂³x₅ + 0.00034035354226016364x₂³x₄ + 0.00016796426947661647x₂³x₃ - 0.0021578182809354257x₂⁴ - 1.567974150786243e-7x₁x₄²x₅ - 7.859771112818889e-8x₁x₃x₄x₅ - 3.585573925892365e-6x₁x₃x₄² + 4.785735741931866e-7x₁x₃²x₅ + 2.2766026326219316e-6x₁x₃²x₄ + 1.1912590712932437e-6x₁x₂x₄x₅ - 1.4261624322597368e-5x₁x₂x₄² - 6.423378828025413e-8x₁x₂x₃x₅ + 6.113020405974095e-6x₁x₂x₃x₄ + 4.3269887930571516e-6x₁x₂x₃² - 3.7381054875148575e-6x₁x₂²x₅ + 2.0063603088055123e-5x₁x₂²x₄ - 6.487069947876859e-6x₁x₂²x₃ - 0.00023055098279210328x₁x₂³ + 5.6166013287859054e-8x₃²x₄²x₅ + 4.50331413498433e-8x₂x₃x₄²x₅ + 3.3851165184323196e-8x₂x₃²x₄x₅ - 5.887442634637123e-6x₂x₃²x₄² + 1.7319509857838717e-6x₂²x₄²x₅ - 3.6763841103984084e-8x₂²x₃x₄x₅ + 1.4371068879736702e-5x₂²x₃x₄² + 1.6354555966827813e-6x₂²x₃²x₅ + 5.715472873576698e-6x₂²x₃²x₄ + 1.6556236413297323e-6x₂³x₄x₅ + 0.019872606416240498x₂³x₄² + 1.3225988968630952e-7x₂³x₃x₅ - 1.1095629284121271e-5x₂³x₃x₄ + 0.00010294223626877613x₂³x₃² + 0.04960737371375877x₂⁴x₅ + 0.0007257786062485909x₂⁴x₄ - 0.0007214950167696501x₂⁴x₃ + 0.23032506770740227x₂⁵ + 3.6804822891461685e-8x₁x₃x₄²x₅ + 5.8539727620738516e-8x₁x₃²x₄x₅ + 1.2464883119219103e-6x₁x₃²x₄² - 4.3552898688109436e-8x₁x₂x₄²x₅ + 3.1979258344141505e-7x₁x₂x₃x₄x₅ - 4.5356219081073924e-6x₁x₂x₃x₄² + 1.9235549099455542e-7x₁x₂x₃²x₅ + 2.138773619014517e-6x₁x₂x₃²x₄ + 8.476830188054206e-7x₁x₂²x₄x₅ + 4.610773905879398e-6x₁x₂²x₄² + 4.903235367150695e-7x₁x₂²x₃x₅ - 2.983079660186731e-6x₁x₂²x₃x₄ + 3.133374315657347e-5x₁x₂²x₃² + 8.773317250522431e-6x₁x₂³x₅ - 1.1393718957186507e-5x₁x₂³x₄ - 8.268432012460208e-5x₁x₂³x₃ - 0.0006111386560739623x₁x₂⁴, -0.008690344622827228 - 0.0002745169809100807u₁ + 1.9898671971409738x₅ + 0.00023985235271304465x₄ - 0.0011042805068000063x₃ + 23.039292647202117x₂ + 0.0015738778563258583x₁ + 3.537517164167742e-5x₄x₅ - 2.6194575354680515e-5x₄² - 1.0309819743634905e-5x₃x₅ - 5.815558282654344e-5x₃x₄ + 0.0005604026456838914x₃² - 0.00013437930113022428x₂x₅ - 0.0015136670419713782x₂x₄ - 0.001923972036299519x₂x₃ + 0.016811289323936073x₂² + 1.5358358028963946e-5x₁x₅ - 0.00023646546112850283x₁x₄ + 9.176985998116857e-5x₁x₃ + 0.0019449223295499537x₁x₂ - 1.0529339647100044e-5x₄²x₅ - 3.445100198168781e-7x₃x₄x₅ + 3.900496158679609e-5x₃x₄² - 8.57223803975955e-6x₃²x₅ - 9.088221855951321e-5x₃²x₄ - 8.691115457842768e-6x₂x₄x₅ - 0.1716370914859899x₂x₄² - 9.875377051954423e-6x₂x₃x₅ - 4.043778511966523e-5x₂x₃x₄ - 0.000196528444860908x₂x₃² - 1.2686987787713195x₂²x₅ - 0.004680469945276338x₂²x₄ + 0.004198116322036894x₂²x₃ - 6.610544502908481x₂³ - 7.3979328174509035e-6x₁x₄x₅ - 0.0001466056617012205x₁x₄² - 6.548354769793802e-6x₁x₃x₅ + 9.225686707917473e-6x₁x₃x₄ - 0.0001369681562596393x₁x₃² - 5.730595494802185e-5x₁x₂x₅ - 0.00011605930732205335x₁x₂x₄ + 0.0005206799270269474x₁x₂x₃ + 0.002064032541383814x₁x₂² - 4.99476671570209e-8x₃x₄²x₅ - 1.408841922300419e-6x₃²x₄x₅ - 2.28485430247827e-5x₃²x₄² + 9.180077102787586e-7x₂x₄²x₅ + 6.833944053572707e-8x₂x₃x₄x₅ + 1.75837216598232e-5x₂x₃x₄² + 7.706943845526215e-7x₂x₃²x₅ - 4.190002147749269e-5x₂x₃²x₄ - 1.786869480773485e-5x₂²x₄x₅ + 0.00015963707677746667x₂²x₄² + 1.9181094141358295e-5x₂²x₃x₅ - 6.413419819050747e-5x₂²x₃x₄ - 0.00018813573399349215x₂²x₃² + 9.689617173144176e-5x₂³x₅ + 0.0016387684762114072x₂³x₄ + 0.0008170303207557x₂³x₃ - 0.01044310712953826x₂⁴ - 7.444077299570302e-7x₁x₄²x₅ - 3.805590849974555e-7x₁x₃x₄x₅ - 1.723286854408288e-5x₁x₃x₄² + 2.3153224661172383e-6x₁x₃²x₅ + 1.1014210557065932e-5x₁x₃²x₄ + 5.759764579215318e-6x₁x₂x₄x₅ - 6.800996956856758e-5x₁x₂x₄² - 3.2808435916734533e-7x₁x₂x₃x₅ + 2.8844190409496926e-5x₁x₂x₃x₄ + 2.0802696932947264e-5x₁x₂x₃² - 1.827322527347659e-5x₁x₂²x₅ + 9.852052119184527e-5x₁x₂²x₄ - 2.9437711319336288e-5x₁x₂²x₃ - 0.0011145211379945063x₁x₂³ + 2.6953326882257376e-7x₃²x₄²x₅ + 2.2557066958158658e-7x₂x₃x₄²x₅ + 1.590956078262977e-7x₂x₃²x₄x₅ - 2.8663683781499304e-5x₂x₃²x₄² + 8.380947726072488e-6x₂²x₄²x₅ - 1.4476333433861185e-7x₂²x₃x₄x₅ + 6.873985642610025e-5x₂²x₃x₄² + 7.882313947542217e-6x₂²x₃²x₅ + 2.76946209170922e-5x₂²x₃²x₄ + 8.212210459696286e-6x₂³x₄x₅ + 0.08660527210097424x₂³x₄² + 4.48945201062732e-7x₂³x₃x₅ - 5.766516098348642e-5x₂³x₃x₄ + 0.0004949564390879247x₂³x₃² + 0.21845418355322277x₂⁴x₅ + 0.0035095848519175726x₂⁴x₄ - 0.00347940098704346x₂⁴x₃ + 0.981121391832937x₂⁵ + 1.760662788444956e-7x₁x₃x₄²x₅ + 2.8696089407607815e-7x₁x₃²x₄x₅ + 5.995314608564776e-6x₁x₃²x₄² - 2.2211569549020635e-7x₁x₂x₄²x₅ + 1.5460311791811814e-6x₁x₂x₃x₄x₅ - 2.2325224864767805e-5x₁x₂x₃x₄² + 9.411612317277123e-7x₁x₂x₃²x₅ + 1.0395016403512942e-5x₁x₂x₃²x₄ + 4.05827330040799e-6x₁x₂²x₄x₅ + 2.2499778770709903e-5x₁x₂²x₄² + 2.370633961354397e-6x₁x₂²x₃x₅ - 1.4236933789768512e-5x₁x₂²x₃x₄ + 0.00015168033132444302x₁x₂²x₃² + 4.2222045350077626e-5x₁x₂³x₅ - 5.254579614332449e-5x₁x₂³x₄ - 0.00039859447666188916x₁x₂³x₃ - 0.002994613587998566x₁x₂⁴, 6.933046007650583e-13 + 1000.0000000000003u₁ - 1000.0x₅ + 7.056753711625472e-13x₁ - 3.194846377038197e-13x₂² - 1.3770219744853839e-12x₂³ - 1.0951685491826121e-12x₁x₂² + 9.96083629920748e-13x₂⁵ + 5.210825852351503e-13x₁x₂⁴])

Next, we define the quadratic cost matrices $Q, R$ and solve the PQR problem. We then generate a controller function u = K(x) using build_K_function and simulate the closed-loop system using solve from OrdinaryDiffEq. To compute the cost achieved by the controller, we add the cumulative integral of the cost as an additional state to the closed-loop dynamics and let the ODE integrator compute the integral for us. We also define a function wrap_angle that makes sure that the angle stays within the interval [-π, π].

R = diagm([0.2])
Q = diagm(Float64[2,1,0.1,1,1e-6])
pqrsol = pqr(cartpole_pol, Q, R, maximum(deg))

wrap_angle(x::Number) = mod(x+pi, 2pi)-pi
wrap_angle(x) = [x[1], wrap_angle(x[2]), x[3], x[4], x[5]]

function f_cl_cartpole(xc, (f,l,K), t)
x  = xc[1:end-1] # Last value is integral of cost
Δx = wrap_angle(x)
th = 100 # Input saturation
Δu = clamp.(K(Δx), -th, th)
dx = f(x, Δu)
dc = l(Δx, Δu)
[dx; dc]         # Return state and cost derivatives
end
x0 = [0, pi-deg2rad(15), 0, 0, 0, 0] # Initial state, start 15° from the downward equilibrium. This avoids the the initial chattering close to the downward equilibrium.

tspan = (0.0, 9.0)
prob = ODEProblem(f_cl_cartpole, x0, tspan, (f, l, x->x))

fig = plot(layout=nx+1)
for d = [3, 5]
K, _ = build_K_function(pqrsol, d, simplify=false)
sol = solve(prob, Tsit5(), reltol=1e-5, abstol=1e-5, p=(f, l, K))
plot!(sol, layout=nx+1, title=["x" "ϕ" "dx" "dϕ" "u" "cost"], lab="Deg $d") end fig Controllers of degrees 3 and 5 manage to swing the pendulum up (upward equilibria are located at$\phi = 0 ± 2π\$), but controllers of lower order do not. Note, that the PQR controller does not handle input saturation explicitly, and as a consequence, does not perform nearly as well swinging the pendulum up as if trajectory optimization had been used. An example of pendulum swing-up with these dynamics is provided in Pendulum swing-up.

Index

JuliaSimControl.build_K_functionFunction
K_oop, K_ip = build_K_function(sol::PQRSolution)
K_oop, K_ip = build_K_function(Ks)

Build a function for the controller K(x) obtained from pqr for the given Ks or PQRSolution.

Optionally, the degree of the controller can be specified as the second argument, provided that the chosen degree is no higher than the degree for which the solution sol was obtained from pqr.

Keyword arguments are forwarded to Symbolics.build_function. Enabling common-subexpression elimination through cse=true might be useful for large controllers.

source
JuliaSimControl.poly_approxMethod
poly_approx(f, deg, l, u; N = 1000)

Approximate scalar function f in the least-squares sense on the interval [l, u] using a polynomial of degree deg.

Returns a vector of polynomial coefficients of length deg + 1 starting at order 0.

Arguments:

• f: Scalar function
• deg: Polynomial degree
• l: Lower bound of the domain
• u: Upper bound of the domain
• N: Number of discretization points

Example

The following creates 5:th order approximations of sin and cos on the interval [0, 2pi], and extends the valid domain to all real numbers using periodicity (mod).

using DynamicPolynomials
const sinpol = tuple(JuliaSimControl.poly_approx(sin, 5, 0, 2pi)...)

function sin5(x)
x = mod(x, 2pi) # Extend the domain to all real numbers
evalpoly(x, sinpol)
end

sin5(x::Union{PolyVar, Polynomial}) = evalpoly(x, sinpol) # When evaluated using a polynomial variable, we do not include the call to mod

function cos5(x)
sin5(x+pi/2)
end
source
JuliaSimControl.poly_approxMethod
poly_approx(f; deg = 3, lx::AbstractVector, ux::AbstractVector, lu, uu, N = 100000, verbose = true)

Polynomial approximation of dynamics ẋ = f(x, u) in the least-squares sense.

Arguments:

• deg: An integer or a vector of the same length as x specifying the maximum degree of each state variable. Defaults to 3.
• lx: Lower bounds of the state domain over which to approximate the dynamics.
• ux: Upper bounds of the state domain over which to approximate the dynamics.
• lu: Lower bounds of the input domain over which to approximate the dynamics.
• uu: Upper bounds of the input domain over which to approximate the dynamics.
• N: Number of samples to use for the approximation.
• verbose: Print maximum error after fitting?
source
JuliaSimControl.pqrFunction
sol = pqr(f, Q, R, deg = 3; verbose)
sol = pqr(A, B, As, Q, R, deg = 3; verbose)

Polynomial-quadratic control design for systems of the form

x' = f(x, u) = Ax + Bu + N[1]*kron(x, x) + N[2]*kron(x, x, x) + ...

where N[i] is a matrix. The cost function is

J = x'Qx + u'Ru

Ref: "On Approximating Polynomial-Quadratic Regulator Problems", Jeff Borggaard, Lizette Zietsman

Arguments:

• f: Function f(x, u) for the dynamics. IF a function is supplied, it will be decomposed into the required Kronecker form automatically using decompose_poly.
• A: Linear dynamics matrix
• B: Control input matrix
• As: Array of matrices for nonlinearities
• Q: State cost matrix
• R: Control input cost matrix
• deg: Desired degree of the controller
• verbose: Print progress. Defaults to true for systems with more than 10 states.

Returns

A PQRSolution object with fields

• Ks: Array of matrices K[i] for the controller
• Vs: Array of matrices V[i] for the Lyapunov function

This solution object supports the following functions:

Example

Controlled Lorenz Equations, example 5.1 from the paper referenced above

using JuliaSimControl, Test

nx = 3 # Number of state variables
nu = 1 # Number of control inputs
A = [
-10 10 0
28 -1 0
0 0 -8/3
]
B = [1; 0; 0;;]
A2 = zeros(nx, nx^2)
A2[2,3] = A2[2,7] = -1/2
A2[3,2] = A2[3,4] = 1/2

Q = diagm(ones(3)) # State cost matrix
R = diagm(ones(1)) # Control input cost matrix

f = (x, u) -> A * x + B * u + A2 * kron(x, x) # Dynamics for simulation
l = (x, u) -> dot(x, Q, x) + dot(u, R, u)     # Cost function for simulation

function closed_loop(xc, (f,l,K), t)          # Closed-loop dynamics for simulation
x = xc[1:end-1] # Last value is integral of cost
u = K(x)
dx = f(x, u)
dc = l(x, u)
[dx; dc]        # Return state and cost derivatives
end

x0 = Float64[10, 10, 10, 0] # Initial state, last value is integral of cost
tspan = (0.0, 50.0)
prob = ODEProblem(closed_loop, x0, tspan, (f, l, x->x))

# Design controller of degree 2
pqrsol = pqr(A, B, [A2], Q, R, 2) # It's possible to pass in f instead of A, B, A2
K, _ = build_K_function(pqrsol)
c = predicted_cost(pqrsol, x0[1:end-1])
@test c ≈ 7062.15 atol=0.02 # Compare to paper
sol = solve(prob, Rodas5P(), p=(f,l,K), reltol=1e-8, abstol=1e-8);
c = sol.u[end][end]
@test c ≈ 6911.03 rtol=1e-2 # Compare to paper

# Design controller of degree 4
pqrsol = pqr(A, B, [A2], Q, R, 4)
K, _ = build_K_function(pqrsol)
c = predicted_cost(pqrsol, x0[1:end-1])
@test c ≈ 6924.27 atol=0.02
sol = solve(prob, Rodas5P(), p=(f,l,K), reltol=1e-8, abstol=1e-8);
c = sol.u[end][end]
@test c ≈ 6906.21 rtol=1e-2
source
• 1"On Approximating Polynomial-Quadratic Regulator Problems", Jeff Borggaard, Lizette Zietsman