From 0e06fc60c181dbfb6e47060a36f3f15dca23dab3 Mon Sep 17 00:00:00 2001
From: Jacob Wikmark <jacob@wikmark.se>
Date: Tue, 13 Mar 2018 14:12:58 +0100
Subject: [PATCH] script => function

---
 src/tankdemo.jl | 131 +++++++++++++++++++++++-------------------------
 1 file changed, 64 insertions(+), 67 deletions(-)

diff --git a/src/tankdemo.jl b/src/tankdemo.jl
index bbbc219..d8d51b5 100644
--- a/src/tankdemo.jl
+++ b/src/tankdemo.jl
@@ -1,83 +1,80 @@
-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
-- 
GitLab