|
| 1 | +using ControlSystems |
| 2 | +""" |
| 3 | +Example for designing an LQG speed controller for an electrical DC motor. |
| 4 | +""" |
| 5 | + |
| 6 | +# Constants |
| 7 | +Ke = 0.006156 # electromotive force constant in V/rpm |
| 8 | +Kt = 0.0728 # Torque constant (Nm/A) |
| 9 | +J = 2.8*700e-6; # Inertia of motor plus load (kgm^2) |
| 10 | +Rel = 0.11; # DC motor resistance (Ω) |
| 11 | +L = 34e-6; # DC motor inductance (H) |
| 12 | + |
| 13 | +# helper to convert a scalar into an 1x1 Matrix |
| 14 | +mat(sc) = reshape([sc],1,1) |
| 15 | + |
| 16 | +# Create an instance of a DC motor model in state space form. |
| 17 | +# Ke: electromotive force constant in V/rpm |
| 18 | +# Kt: torque constant (Nm/A) |
| 19 | +# L: Inductance; R: Resistance, J: Inertia; b: viscous friction coefficient |
| 20 | +function motor(Ke, Kt, L, R, J, b=1e-3) |
| 21 | + Ke1 = Ke*60.0/2π # change unit to rad/s |
| 22 | + A = [-b/J Kt/J |
| 23 | + -Ke1/L -R/L]; |
| 24 | + B = [0 |
| 25 | + 1/L]; |
| 26 | + C = [1 0]; |
| 27 | + D = 0; |
| 28 | + sys = ss(A, B, C, D); |
| 29 | +end |
| 30 | + |
| 31 | +p60 = motor(Ke, Kt, L, Rel, J) |
| 32 | +stepplot(p60, 0.2, 0.001) |
| 33 | +bodeplot(p60) |
| 34 | + |
| 35 | +# LQR control |
| 36 | +Q = [1. 0; |
| 37 | + 0 1] |
| 38 | + |
| 39 | +R = 20. |
| 40 | +K = lqr(p60.A, p60.B, Q, R) |
| 41 | +# needs to be modified if Nbar is not a scalar |
| 42 | +Nbar = 1. / (p60.D - (p60.C - p60.D*K) * inv(p60.A - p60.B*K) * p60.B) |
| 43 | + |
| 44 | +# Kalman filter based observer |
| 45 | +Vd = [10. 0 # covariance of the speed estimation |
| 46 | + 0 10]; # covariance of the current estimation |
| 47 | +Vn = 0.04; # covariance for the speed measurement (radians/s)^2 |
| 48 | +G = LQG(p60, Q, mat(R), Vd, mat(Vn)) |
| 49 | +Gcl = G[:cl] |
| 50 | +T = G[:T] |
| 51 | +S = G[:S]; |
| 52 | +f1 = sigmaplot([S,T],logspace(-3,3,1000)) |
| 53 | +f2 = stepplot(Gcl, label=["Closed loop system using LQG"]) |
| 54 | +Plots.plot(f1,f2) |
0 commit comments