|
| 1 | +# Linearize the winch |
| 2 | +# input: set_speed |
| 3 | +# output: speed |
| 4 | +# Same as mwe_02.jl, but using FiniteDiff.jl for Jacobian calculation |
| 5 | + |
| 6 | +using Pkg |
| 7 | +if ! ("ControlPlots" ∈ keys(Pkg.project().dependencies)) |
| 8 | + using TestEnv; TestEnv.activate() |
| 9 | + using Test |
| 10 | +end |
| 11 | +using WinchControllers, WinchModels, KiteUtils, ControlPlots, ControlSystemsBase, FiniteDiff |
| 12 | +import FiniteDiff: finite_difference_jacobian |
| 13 | + |
| 14 | +if isfile("data/system_tuned.yaml") |
| 15 | + set = load_settings("system_tuned.yaml") |
| 16 | +else |
| 17 | + set = load_settings("system.yaml") |
| 18 | +end |
| 19 | +wcs = WCSettings(dt=0.02) |
| 20 | +update(wcs) |
| 21 | +wcs.test = true |
| 22 | + |
| 23 | +winch = Winch(wcs, set) |
| 24 | + |
| 25 | +function finite_jacobian(f, x; ϵ=sqrt(eps(eltype(x)))) |
| 26 | + n = length(x) |
| 27 | + m = length(f(x)) |
| 28 | + J = zeros(m, n) |
| 29 | + for i in 1:n |
| 30 | + Δx = zeros(n) |
| 31 | + Δx[i] = ϵ |
| 32 | + J[:, i] = (f(x + Δx) - f(x - Δx)) ./ (2ϵ) |
| 33 | + end |
| 34 | + J |
| 35 | +end |
| 36 | + |
| 37 | +# find equilibrium speed |
| 38 | +function find_equilibrium_speed(winch, set_speed, force, n=10000) |
| 39 | + last_v_act = 0.0 |
| 40 | + for v_set in range(0.0, 2*set_speed, n) |
| 41 | + lim_speed = minimum([v_set, set_speed]) |
| 42 | + set_force(winch, force) |
| 43 | + set_v_set(winch, lim_speed) |
| 44 | + v_act = get_speed(winch) |
| 45 | + on_timer(winch) |
| 46 | + if v_set > 0 && abs(v_act - last_v_act) < 1e-6 |
| 47 | + return v_act |
| 48 | + end |
| 49 | + last_v_act = v_act |
| 50 | + end |
| 51 | + set_v_set(winch, set_speed) |
| 52 | + on_timer(winch) |
| 53 | + @error "Failed to find equilibrium speed" |
| 54 | +end |
| 55 | + |
| 56 | +function motor_dynamics(x, u) |
| 57 | + # x: state vector, e.g., [v_act] |
| 58 | + # u: input vector, e.g., [v_set, force] |
| 59 | + v_act = x[1] |
| 60 | + v_set, force = u[1], u[2] |
| 61 | + acc = calc_acceleration(winch.wm, v_act, force; set_speed = v_set) |
| 62 | + return [acc] |
| 63 | +end |
| 64 | + |
| 65 | +function linearize(winch, v_set, force) |
| 66 | + v_act = find_equilibrium_speed(winch, v_set, force) |
| 67 | + x0 = [v_act] # State at operating point |
| 68 | + u0 = [v_set, force] # Input at operating point |
| 69 | + A = finite_jacobian(x -> motor_dynamics(x, u0), x0) |
| 70 | + B = finite_jacobian(u -> motor_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 | +v_set = 4.0 |
| 77 | +for force in range(300.0, 3800.0, length=10) |
| 78 | + @info "Linearizing for force: $force N" |
| 79 | + sys_new = linearize(winch, v_set, force) |
| 80 | + # @info "System: $sys_new" |
| 81 | + # @info "Eigenvalues: $(eigvals(sys_new))" |
| 82 | + bode_plot(sys_new; from=0.76, to=2.85, title="Linearized Winch, F=300..3800 N") |
| 83 | +end |
| 84 | + |
| 85 | +# bode_plot(sys_new; to=2, title="Linearized Winch, F=$force N") |
0 commit comments