Skip to content

Commit 3dcf7a0

Browse files
committed
Add mwe_06.jl
1 parent 51cbf22 commit 3dcf7a0

File tree

1 file changed

+81
-0
lines changed

1 file changed

+81
-0
lines changed

mwes/mwe_06.jl

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
# Linearize the closed loop system consisting of the winch, kite and upper force controller.
2+
3+
using Pkg
4+
if ! ("ControlPlots" keys(Pkg.project().dependencies))
5+
using TestEnv; TestEnv.activate()
6+
using Test
7+
end
8+
using WinchControllers, WinchModels, KiteUtils, ControlPlots, ControlSystemsBase, FiniteDiff
9+
import FiniteDiff: finite_difference_jacobian
10+
11+
if isfile("data/system_tuned.yaml")
12+
set = load_settings("system_tuned.yaml")
13+
else
14+
set = load_settings("system.yaml")
15+
end
16+
wcs = WCSettings(dt=0.02)
17+
update(wcs)
18+
wcs.test = true
19+
20+
winch = Winch(wcs, set)
21+
22+
# find equilibrium speed
23+
function find_equilibrium_speed(winch, set_speed, force, n=10000)
24+
last_v_act = 0.0
25+
for v_set in range(0.0, 2*set_speed, n)
26+
lim_speed = minimum([v_set, set_speed])
27+
set_force(winch, force)
28+
set_v_set(winch, lim_speed)
29+
v_act = get_speed(winch)
30+
on_timer(winch)
31+
if v_set > 0 && abs(v_act - last_v_act) < 1e-6
32+
return v_act
33+
end
34+
last_v_act = v_act
35+
end
36+
set_v_set(winch, set_speed)
37+
on_timer(winch)
38+
@error "Failed to find equilibrium speed"
39+
end
40+
41+
function motor_dynamics(x, u)
42+
# x: state vector, e.g., [v_act]
43+
# u: input vector, e.g., [v_set, force]
44+
v_act = x[1]
45+
v_set, force = u[1], u[2]
46+
acc = calc_acceleration(winch.wm, v_act, force; set_speed = v_set)
47+
return [acc]
48+
end
49+
50+
function calc_force(v_wind, v_ro)
51+
(v_wind - v_ro)^2 * 4000.0 / 16.0
52+
end
53+
54+
function system_dynamics(x, u)
55+
# x: state vector, e.g., [v_act]
56+
# u: input vector, e.g., [v_set, v_wind]
57+
v_act = x[1]
58+
v_set, v_wind = u[1], u[2]
59+
force = calc_force(v_wind, v_act)
60+
acc = calc_acceleration(winch.wm, v_act, force; set_speed = v_set)
61+
return [acc]
62+
end
63+
64+
function linearize(winch, v_set, v_wind)
65+
force = calc_force(v_wind, v_set)
66+
v_act = find_equilibrium_speed(winch, v_set, force)
67+
x0 = [v_act] # State at operating point
68+
u0 = [v_set, v_wind] # Input at operating point
69+
A = finite_difference_jacobian(x -> system_dynamics(x, u0), x0)
70+
B = finite_difference_jacobian(u -> system_dynamics(x0, u), u0)
71+
C = [1.0]
72+
D = [0.0 0.0]
73+
siso_sys = ss(A, B[:, 1], C, D[:, 1])
74+
end
75+
76+
for v_wind in range(1, 9, length=9)
77+
v_set = 0.57*v_wind
78+
@info "Linearizing for v_wind: $v_wind m/s, v_ro: $(round(v_set, digits=2)) m/s"
79+
sys_new = linearize(winch, v_set, v_wind)
80+
bode_plot(sys_new; from=0.76, to=2.85, title="Linearized System, v_wind=1..9 m/s")
81+
end

0 commit comments

Comments
 (0)