forked from JuliaControl/ControlSystems.jl
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dc_motor_lqg_design.jl
64 lines (54 loc) · 1.93 KB
/
dc_motor_lqg_design.jl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
using ControlSystems, Plots
"""
Example for designing an LQG speed controller for an electrical DC motor.
"""
# Constants
Ke = 0.006156 # electromotive force constant in V/rpm
Kt = 0.0728 # Torque constant (Nm/A)
J = 2.8*700e-6; # Inertia of motor plus load (kgm^2)
Rel = 0.11; # DC motor resistance (Ω)
L = 34e-6; # DC motor inductance (H)
# helper to convert a scalar into an 1x1 Matrix
mat(sc) = reshape([sc],1,1)
# Create an instance of a DC motor model in state space form.
# Ke: electromotive force constant in V/rpm
# Kt: torque constant (Nm/A)
# L: Inductance; R: Resistance, J: Inertia; b: viscous friction coefficient
function motor(Ke, Kt, L, R, J, b=1e-3)
Ke1 = Ke*60.0/2π # change unit to rad/s
A = [-b/J Kt/J
-Ke1/L -R/L];
B = [0
1/L];
C = [1 0];
D = 0;
sys = ss(A, B, C, D);
end
p60 = motor(Ke, Kt, L, Rel, J)
f1 = plot(step(p60, 1))
f2 = bodeplot(p60)
# LQR control
Q = [1. 0;
0 1]
R = 20.
K = lqr(p60, Q, R)
# needs to be modified if Nbar is not a scalar
Nbar = 1. ./ (p60.D - (p60.C - p60.D*K) * inv(p60.A - p60.B*K) * p60.B)
# Kalman filter based observer
Vd = [10. 0 # covariance of the speed estimation
0 10]; # covariance of the current estimation
Vn = 0.04; # covariance for the speed measurement (radians/s)^2
Ka = kalman(p60, Vd, mat(Vn))
C = ControlSystems.observer_controller(p60, K, Ka)
Gcl = let (A,B,C,D) = ssdata(p60)
Acl = [A-B*K B*K; zero(A) A-Ka*C]
Bcl = [B * Nbar; zero(B)]
Ccl = [C zero(C)]
ss(Acl, Bcl, Ccl, 0)
end
T = Gcl
S = 1-T
# 1000 logarithmically spaced values from -3 to 3
f3 = bodeplot([Gcl, S, T], exp10.(range(-3, stop=3, length=1000)))
f4 = plot(step(Gcl, 1), label="Closed loop system using LQG")
plot(f1, f2, f3, f4, layout=(2,2), size=(800, 600))