|
| 1 | +# SPDX-FileCopyrightText: 2026 Bart van de Lint |
| 2 | +# SPDX-License-Identifier: LGPL-3.0-only |
| 3 | + |
| 4 | +""" |
| 5 | +Custom winch model: cascaded length-velocity control with a v_max cap. |
| 6 | +
|
| 7 | +`set_value` is interpreted as a target tether length [m]. An outer |
| 8 | +proportional law turns the length error into a velocity reference, |
| 9 | +hard-clamped at ±v_max. An inner P-on-velocity controller with load |
| 10 | +feedforward tracks that reference. The feedforward (`+friction - |
| 11 | +ratio·force`) cancels the load disturbance so tether spring-mass |
| 12 | +bouncing doesn't perturb `vel`. No integrator → no windup at |
| 13 | +saturation → no overshoot. Closed-loop velocity response is 1st-order |
| 14 | +with time constant `I / (ratio · K_p)`. |
| 15 | +""" |
| 16 | + |
| 17 | +using Pkg |
| 18 | +if Base.active_project() != joinpath(@__DIR__, "Project.toml") |
| 19 | + Pkg.activate(@__DIR__) |
| 20 | +end |
| 21 | + |
| 22 | +using GLMakie |
| 23 | +using KiteUtils: init!, next_step! |
| 24 | +using ModelingToolkit |
| 25 | +using ModelingToolkit: t_nounits as t, D_nounits as D |
| 26 | +using SymbolicAWEModels |
| 27 | +using SymbolicAWEModels: SystemStructure, |
| 28 | + get_winch_gear_ratio, get_winch_drum_radius, |
| 29 | + get_winch_f_coulomb, get_winch_c_vf, |
| 30 | + get_winch_inertia_total, get_winch_friction_epsilon |
| 31 | +import SymbolicAWEModels: Point # resolve ambiguity with GLMakie |
| 32 | + |
| 33 | +set_data_path(joinpath(dirname(@__DIR__), "data")) |
| 34 | +set = Settings("base/system.yaml") |
| 35 | +set.v_wind = 0.0 |
| 36 | + |
| 37 | +function make_length_to_velocity_winch(; |
| 38 | + v_max::Float64, K_pos::Float64, K_p::Float64) |
| 39 | + return function (sys_struct::SystemStructure, winch_idx::Int; name) |
| 40 | + SST = typeof(sys_struct) |
| 41 | + @parameters (psys::SST = sys_struct), [tunable = false] |
| 42 | + @variables begin |
| 43 | + vel(t) |
| 44 | + len(t) |
| 45 | + force(t) |
| 46 | + set_value(t) |
| 47 | + brake(t) |
| 48 | + acc(t) |
| 49 | + friction(t) |
| 50 | + ω_motor(t) |
| 51 | + vel_unclamped(t) |
| 52 | + vel_ref(t) |
| 53 | + tau_cmd(t) |
| 54 | + tau_net(t) |
| 55 | + end |
| 56 | + |
| 57 | + gear_ratio = get_winch_gear_ratio(psys, winch_idx) |
| 58 | + drum_radius = get_winch_drum_radius(psys, winch_idx) |
| 59 | + f_coulomb = get_winch_f_coulomb(psys, winch_idx) |
| 60 | + c_vf = get_winch_c_vf(psys, winch_idx) |
| 61 | + inertia_total = get_winch_inertia_total(psys, winch_idx) |
| 62 | + friction_eps = get_winch_friction_epsilon(psys, winch_idx) |
| 63 | + smooth_sign(x, eps) = x / sqrt(x * x + eps * eps) |
| 64 | + ratio = drum_radius / gear_ratio |
| 65 | + |
| 66 | + eqs = [ |
| 67 | + ω_motor ~ vel / ratio |
| 68 | + friction ~ smooth_sign(ω_motor, friction_eps) * |
| 69 | + f_coulomb * ratio + |
| 70 | + c_vf * ω_motor * ratio^2 |
| 71 | + vel_unclamped ~ K_pos * (set_value - len) |
| 72 | + vel_ref ~ max(-v_max, min(v_max, vel_unclamped)) |
| 73 | + tau_cmd ~ K_p * (vel_ref - vel) + friction - ratio * force |
| 74 | + tau_net ~ tau_cmd + ratio * force - friction |
| 75 | + acc ~ ifelse(brake > 0.5, 0.0, |
| 76 | + ratio * tau_net / inertia_total) |
| 77 | + ] |
| 78 | + return System(eqs, t, |
| 79 | + [vel, len, force, set_value, brake, acc, friction, |
| 80 | + ω_motor, vel_unclamped, vel_ref, tau_cmd, tau_net], |
| 81 | + [psys]; name) |
| 82 | + end |
| 83 | +end |
| 84 | + |
| 85 | +points = [ |
| 86 | + Point(:ground, [0.0, 0.0, 0.0], STATIC), |
| 87 | + Point(:mass, [0.0, 0.0, -50.0], DYNAMIC; extra_mass=10.0), |
| 88 | +] |
| 89 | +segments = [ |
| 90 | + Segment(:line, :ground, :mass, 50_000.0, 500.0, 0.005; l0=50.0), |
| 91 | +] |
| 92 | +tethers = [Tether(:main, [:line], 50.0)] |
| 93 | +winches = [Winch(:winch, set, [:main]; winch_point=:ground, |
| 94 | + model=make_length_to_velocity_winch( |
| 95 | + v_max=1.0, K_pos=50.0, K_p=10.0))] |
| 96 | +winches[1].inertia_total = 0.001 # tiny rotor → near-instant velocity tracking |
| 97 | + |
| 98 | +sys_struct = SystemStructure("custom_winch_vel", set; |
| 99 | + points, segments, tethers, winches) |
| 100 | +sam = SymbolicAWEModel(set, sys_struct) |
| 101 | +init!(sam; remake=true, prn=false) |
| 102 | + |
| 103 | +winch = sam.sys_struct.winches[:winch] |
| 104 | +tether = sam.sys_struct.tethers[:main] |
| 105 | + |
| 106 | +dt = 0.02 |
| 107 | +n_steps = 1500 |
| 108 | +times = Float64[] |
| 109 | +ref_len = Float64[] |
| 110 | +meas_len = Float64[] |
| 111 | +meas_vel = Float64[] |
| 112 | + |
| 113 | +for k in 1:n_steps |
| 114 | + tnow = k * dt |
| 115 | + l_target = |
| 116 | + tnow < 2.0 ? 50.0 : |
| 117 | + tnow < 15.0 ? 45.0 : |
| 118 | + 55.0 |
| 119 | + next_step!(sam; set_values=[l_target], dt=dt, vsm_interval=0) |
| 120 | + push!(times, tnow) |
| 121 | + push!(ref_len, l_target) |
| 122 | + push!(meas_len, tether.len) |
| 123 | + push!(meas_vel, winch.vel) |
| 124 | +end |
| 125 | + |
| 126 | +fig = Figure(size=(900, 600)) |
| 127 | +ax1 = Axis(fig[1, 1]; ylabel="tether length [m]", |
| 128 | + title="Cascaded length→velocity winch (v_max=1.0 m/s)") |
| 129 | +lines!(ax1, times, ref_len; label="target", linestyle=:dash) |
| 130 | +lines!(ax1, times, meas_len; label="measured") |
| 131 | +axislegend(ax1; position=:rb) |
| 132 | + |
| 133 | +ax2 = Axis(fig[2, 1]; xlabel="t [s]", |
| 134 | + ylabel="reel-out velocity [m/s]") |
| 135 | +hlines!(ax2, [+1.0, -1.0]; color=:gray, linestyle=:dot) |
| 136 | +lines!(ax2, times, meas_vel) |
| 137 | + |
| 138 | +display(fig) |
0 commit comments