livetankdemo.jl 1.9 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 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70
using InteractNext, Mux, Plots, LabProcesses, LabConnections, LabGUI
include("tankgraphic.jl")
include("doubletank.jl")
include("tanklabgui.jl")


#using BallAndBeam, ControlSystems, JLD, LabProcesses, Plots
# @load "workspace.jld" # Run this command to restore a saved workspace

#P    = LabProcesses.Beam(bias = 0.)  # Replace for BeamSimulator() to run simulations
#P    = LabProcesses.DoubleTankSimulator()
#h  = 0.05
#x  = [0.08, 0.08]
#n  = 1
#α  = 2.1e-5
#A  = 4.9e-4 #m^2
#a1 = 3.1e-6 #m^2
#a2 = 3.1e-6 #m^2
#g  = 9.81   #m/s^2

R = DoubleTank()
#S = DoubleTankSimulator(h, copy(x), n)

#settling_time  = 1 #?
#nbr_of_periods = 3 #?

inspectdr(show=false)

manual = false
#prbs     = PRBSGenerator()
function prbs_experiment(P, gui, pidcontroller, ;amplitude = 1, duration = 10000)
    y = zeros(0:P.h:duration)
    u = zeros(0:P.h:duration)
	n = 1
    #LabProcesses.initialize(P)
    #initialize(P)
    for (i,t) = enumerate(0:P.h:duration)
        @periodically P.h begin
				y[i]  = measure(P)[n]
            r = gui[:r]
            if gui[:tankno]=="Upper"
                n = 1
            else
                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), 8004)