tankdemo.jl 1.41 KB
Newer Older
Jacob Wikmark's avatar
Jacob Wikmark committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49
using InteractNext, Mux, Plots, LabProcesses, LabGUI
include("tankgraphic.jl")
include("doubletank.jl")
include("tanklabgui.jl")

R = DoubleTankSimulator(σ = 0.001)

inspectdr(show=false)

manual = false
function prbs_experiment(P, gui, pidcontroller, ;amplitude = 1, duration = 10000)
    y = zeros(0:P.h:duration)
    u = zeros(0:P.h:duration)
    #LabProcesses.initialize(P)
    #initialize(P)
    for (i,t) = enumerate(0:P.h:duration)
        @periodically P.h begin
            y[i]  = measure(P)[P.n]
            r = gui[:r]
            if gui[:tankno]=="Upper"
                P.n = 1
            else
                P.n = 2
            end
            onv = Int.([gui[s]=="On" for s in [:pOn, :iOn, :dOn]])
            rv = pidcontroller(r, y[i], onv)
            if gui[:mode]=="Automatic"
                obs(gui.widgets[:u]).val = clamp(round(rv,2), 0, 1)
                control(P, rv)
            elseif gui[:mode]=="Manual"
                control(P, gui[:u])
                #bumpless transfer:
                pidcontroller.I = gui[:u]-pidcontroller.P-pidcontroller.D
            end
            push!(gui, y[i], 1)
            push!(gui, r, 2)
            push!(gui, pidcontroller.P, 3)
            push!(gui, pidcontroller.I, 4)
            push!(gui, pidcontroller.D, 5)
            push!(gui, pidcontroller.Tot, 6)
        end
    end
    finalize(P)
    y,u
end


g1, r1, tp = makegui(R)
webio_serve(page("/", r1), 8008)