Skip to content

Commit b94a759

Browse files
authored
Merge pull request #135 from ufechner7/doc
Fixed error in docstring, improved example in docstring of LQG.
2 parents 0914a80 + 048696f commit b94a759

2 files changed

Lines changed: 59 additions & 2 deletions

File tree

example/dc_motor_lqg_design.jl

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
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)

src/types/lqg.jl

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@ import Base.getindex
66
G = LQG(sys, args...; kwargs...)
77
88
Return an LQG object that describes the closed control loop around the process `sys=ss(A,B,C,D)`
9-
where the controller is of LQG-type. The controller is specified by weight matrices `R1,R2`
9+
where the controller is of LQG-type. The controller is specified by weight matrices `Q1,Q2`
1010
that penalizes state deviations and control signal variance respectively, and covariance
11-
matrices `Q1,Q2` which specify state drift and measurement covariance respectively.
11+
matrices `R1,R2` which specify state drift and measurement covariance respectively.
1212
This constructor calls [`lqr`](@ref) and [`kalman`](@ref) and forms the closed-loop system.
1313
1414
If `integrator=true`, the resulting controller will have intregral action.
@@ -50,6 +50,9 @@ It is also possible to access all fileds using the `G[:symbol]` syntax, the fiel
5050
# Example
5151
5252
```julia
53+
s = tf("s")
54+
P = [1/(s+1) 2/(s+2); 1/(s+3) 1/(s-1)]
55+
sys = ss(P)
5356
eye(n) = Matrix{Float64}(I,n,n) # For convinience
5457
5558
qQ = 1

0 commit comments

Comments
 (0)