Skip to content

Commit fb013c1

Browse files
committed
Good system simulation
1 parent 945d4ba commit fb013c1

File tree

1 file changed

+87
-0
lines changed

1 file changed

+87
-0
lines changed

mwes/mwe_05.jl

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

0 commit comments

Comments
 (0)