Commit 0e06fc60 authored by Jacob Wikmark's avatar Jacob Wikmark
Browse files

script => function

parent 962bce05
using InteractNext, WebIO, Plots, LabProcesses, LabGUI
using InteractNext, WebIO, Plots, LabProcesses, LabGUI, Mux, LabConnections#, Blink
include("tankgraphic.jl")
include("DoubleTank.jl")
include("tanklabgui.jl")
const simulated = true
const webservice = true
"""
Alias for runprocess with simulated=true.
#Just for testing
global port
try
port += 1
catch
port = 8000
end
"""
runsimulation(;webservice=true, port=8000, σ = 0.001, duration = 14400) = runprocess(simulated=true, webservice=webservice, port=port, σ=σ, duration=duration)
if webservice
using Mux
else
using Mux #change later
using Blink
end
"""
if simulated
print("Running simulated process\n")
R = DoubleTankSimulator(σ = 0.001)
else
print("Running physical process\n")
using LabConnections
R = DoubleTank()
end
runprocess(;simulated=false, webservice=true, port=8000, σ = 0.001, duration = 14400)
inspectdr(show=false)
Run the doubletank process with a GUI.
"""
manual = false
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
function runprocess(;simulated=false, webservice=true, port=8000, σ = 0.001, duration = 14400)
#Just for testing
if simulated
print("Running simulated process\n")
R = DoubleTankSimulator(σ = σ)
else
print("Running physical process\n")
R = DoubleTank()
end
inspectdr(show=false)
function run_process(P, gui, pidcontroller, duration = duration) #four hours
y = 0.0
u = 0.0
n = 1
LabProcesses.initialize(P)
for t in 0:P.h:duration
@periodically P.h begin
y = 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, onv)
if gui[:mode]=="Automatic"
gui[:u] = clamp(round(rv,2), 0, 1) #u's constructor has a dummy graphic so this is fine
control(P, rv)
elseif gui[:mode]=="Manual"
control(P, gui[:u])
#bumpless transfer:
pidcontroller.I = gui[:u]-(pidcontroller.Tot-pidcontroller.I)
end
push!(gui, y, 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)
push!(gui, t, 7)
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
print("The lab time is over, go home! 😊\n") #Easter egg, unlikely to be shown
LabProcesses.finalize(P)
end
finalize(P)
y,u
end
g1, r1, tp = makegui(R)
g1, r1, tp = makegui(R, run_process)
if webservice
webio_serve(page("/", r1), port)
else
webio_serve(page("/", r1), port)
w = Window()
size(w, 1900, 900)
loadurl(w, "http:0.0.0.0:$port")
if webservice
webio_serve(page("/", r1), port)
else
webio_serve(page("/", r1), port)
w = Window()
size(w, 1900, 900)
loadurl(w, "http:0.0.0.0:$port")
end
end
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment