tanklabgui.jl 5.73 KB
Newer Older
Jacob Wikmark's avatar
Jacob Wikmark committed
1
2
3
4
5
6
function makegui(P, plottingframerate = 10, guiframerate = 10)
    tankpid = PID()
    boundsupper = (0,1)
    boundslower = (-2,2)
    maxplotlength = 800
    gui = GUI()
Jacob Wikmark's avatar
Jacob Wikmark committed
7
    ptfr = 3.8 #pump to flow ratio, approximate
Jacob Wikmark's avatar
Jacob Wikmark committed
8

Jacob Wikmark's avatar
Jacob Wikmark committed
9
    #This allows external access to the r value, the rbox string is parsed into this value 
10
11
12
13
14
15
16
17
    rcont, dummygraphic1 = @construct for
        r in slider(0.0:0.01:1.0)
        Node(:div)
    end

    #This contains all settings
    w0, dummygraphic2 = @construct for
        u      in slider(0:0.01:1, label=" um"),
Jacob Wikmark's avatar
Jacob Wikmark committed
18
        run    in button("Run"), #This should only exist in the simulated version
Jacob Wikmark's avatar
Jacob Wikmark committed
19
20
21
22
        cali   in button("Calibrate"),
        K      in slider(0:0.1:50, label="K"),
        Ti     in slider(0:0.1:50, label="Ti"),
        Td     in slider(0:0.1:50, label="Td"),
Jacob Wikmark's avatar
Jacob Wikmark committed
23
24
25
26
        mode   in togglebuttons(["Manual", "Automatic"], label=""),
        tankno in togglebuttons(["Upper", "Lower"], label=""),
        pOn    in togglebuttons(["On", "Off"], label="P"),
        iOn    in togglebuttons(["On", "Off"], label="I"),
27
        dOn    in togglebuttons(["On", "Off"], label="D"),
Jacob Wikmark's avatar
Jacob Wikmark committed
28
29
30
        rbox   in textbox(" r "),
        expcsv in button("Export Data")

Jacob Wikmark's avatar
Jacob Wikmark committed
31
        rv = obs(r)[]
32
33
        if !endswith(rbox, ".") && rbox != "0"
            try 
34
                #obs(r).val = parse(Float64, rbox)
Jacob Wikmark's avatar
Jacob Wikmark committed
35
                rv = parse(Float64, rbox)
36
37
38
            end
            if 0.0<rv<1.0
                obs(r).val = rv
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
            end
        end
        Node(:div)
    end

    widgets, graphic1 = @construct for 
        svgdummy in checkbox(false, label="This button does nothing")
        tankpid.K = obs(K)[]
        tankpid.Ti = obs(Ti)[]
        tankpid.Td = obs(Td)[]

        if obs(tankno)[]=="Upper"
            tankg = tank_construct(measure(P)..., obs(u)[]*ptfr, 1, obs(r).val)
        elseif obs(tankno)[]=="Lower"
            tankg = tank_construct(measure(P)..., obs(u)[]*ptfr, 2, obs(r).val)
Jacob Wikmark's avatar
Jacob Wikmark committed
54
        else
55
            tankg = tank_construct(measure(P)..., obs(u)[]*ptfr)
Jacob Wikmark's avatar
Jacob Wikmark committed
56
        end
Jacob Wikmark's avatar
Jacob Wikmark committed
57
        Node(:div, tankg, id="tanks", attributes=tankattr)
Jacob Wikmark's avatar
Jacob Wikmark committed
58
59
    end

Jacob Wikmark's avatar
Jacob Wikmark committed
60
    glnv = (0:maxplotlength)*P.h
Jacob Wikmark's avatar
Jacob Wikmark committed
61
    w2, graphic2 = @construct for 
62
        plotdummy in checkbox(false, label="This button does nothing")
Jacob Wikmark's avatar
Jacob Wikmark committed
63
64
65
        dataln = length(gui.data[1])
        if dataln>maxplotlength
            start = dataln-maxplotlength
Jacob Wikmark's avatar
Jacob Wikmark committed
66
67
68
        else
            start=1
        end
Jacob Wikmark's avatar
Jacob Wikmark committed
69
70
71
72
73
74
75
76
        lnv = glnv[1:min(dataln, maxplotlength+1)]
        p = plot(layout=(2,1), size=(700, 600), xlabel = "seconds")
        plot!(p[1], lnv, gui.data[1][start:end], label="y", ylim = boundsupper)
        plot!(p[1], lnv, gui.data[2][start:end], label="r")
        plot!(p[2], lnv, gui.data[3][start:end], label="P", ylim = boundslower)
        plot!(p[2], lnv, gui.data[4][start:end], label="I")
        plot!(p[2], lnv, gui.data[5][start:end], label="D")
        plot!(p[2], lnv, gui.data[6][start:end], label="Tot", color=:black)
Jacob Wikmark's avatar
Jacob Wikmark committed
77
78
79
80
81
82
83
84
85
86
87
88
    end

    #-----------------------------------------------#
    #This should only exist in the simulated version#
    on(obs(run)) do val
        if val==1
            @async prbs_experiment(P, gui, tankpid)
        else
            obs(run).listeners[end] = x ->()
        end
    end
    #-----------------------------------------------#
Jacob Wikmark's avatar
Jacob Wikmark committed
89
90
91
92
93
    # And this should only exist in the real version#
    on(obs(cali)) do val
        calibrate(P)
    end
    #-----------------------------------------------#
lancebeet's avatar
csv    
lancebeet committed
94
    on(obs(expcsv)) do val
Jacob Wikmark's avatar
Jacob Wikmark committed
95
96
        writecsv("datatanklab.csv", 
                 [gui.data[1] gui.data[2] gui.data[3] gui.data[4] gui.data[5] gui.data[6]])
lancebeet's avatar
csv    
lancebeet committed
97
98
        print("Data exported to datatanklab.csv\n")
    end
Jacob Wikmark's avatar
Jacob Wikmark committed
99

100
101
102
103
104
    LabGUI.set!(gui, widgets)
    add!(gui, rcont)
    add!(gui, w0)
    add!(gui, w2)

Jacob Wikmark's avatar
Jacob Wikmark committed
105
106
    layout = make_grid(1, 2)
    left_element  = make_grid(2,1)
Jacob Wikmark's avatar
Jacob Wikmark committed
107
    settings_element = make_grid(4,1)
Jacob Wikmark's avatar
Jacob Wikmark committed
108
109
110
111
    corner_element = make_grid(1,4)


    p_element = make_grid(2,1)
Jacob Wikmark's avatar
Jacob Wikmark committed
112
113
    p_element = setindex_(p_element, dom"div"(gui.widgets[:pOn]), 1, 1)
    p_element = setindex_(p_element, dom"div"(gui.widgets[:K]), 2, 1)
Jacob Wikmark's avatar
Jacob Wikmark committed
114
115

    i_element = make_grid(2,1)
Jacob Wikmark's avatar
Jacob Wikmark committed
116
    i_element = setindex_(i_element, dom"div"(gui.widgets[:iOn]), 1, 1)
Jacob Wikmark's avatar
Jacob Wikmark committed
117
118
119
    i_element = setindex_(i_element, dom"div"(widgets[:Ti]), 2, 1)

    d_element = make_grid(2,1)
Jacob Wikmark's avatar
Jacob Wikmark committed
120
121
122
    d_element = setindex_(d_element, dom"div"(gui.widgets[:dOn]), 1, 1)
    d_element = setindex_(d_element, dom"div"(gui.widgets[:Td]), 2, 1)

Jacob Wikmark's avatar
Jacob Wikmark committed
123
124
125
126
    settings_element = setindex_(settings_element, dom"div"(gui.widgets[:tankno]), 1, 1)
    settings_element = setindex_(settings_element, dom"div"(gui.widgets[:mode]), 2, 1)
    settings_element = setindex_(settings_element, dom"div"(gui.widgets[:rbox]), 3, 1)
    settings_element = setindex_(settings_element, dom"div"(gui.widgets[:u]), 4, 1)
Jacob Wikmark's avatar
Jacob Wikmark committed
127
128
129
130
131

    io_element = Node(:div, attributes=Dict("display"=>"inlineBlock"), 
                      gui.widgets[:run],
                      gui.widgets[:cali],
                      gui.widgets[:expcsv])
Jacob Wikmark's avatar
Jacob Wikmark committed
132

Jacob Wikmark's avatar
Jacob Wikmark committed
133
134
135
136
137
138
139
140
    corner_element = setindex_(corner_element, settings_element, 1, 1)
    corner_element = setindex_(corner_element, p_element, 1, 2)
    corner_element = setindex_(corner_element, i_element, 1, 3)
    corner_element = setindex_(corner_element, d_element, 1, 4)

    left_element = setindex_(left_element, corner_element, 1, 1)
    left_element = setindex_(left_element, graphic2, 2, 1)
    layout = setindex_(layout, left_element, 1, 1)
Jacob Wikmark's avatar
Jacob Wikmark committed
141
    layout = setindex_(layout, Node(:div, graphic1, dummygraphic1, dummygraphic2, io_element,
Jacob Wikmark's avatar
Jacob Wikmark committed
142
                                    attributes = tankattr), 1, 2)
Jacob Wikmark's avatar
Jacob Wikmark committed
143

Jacob Wikmark's avatar
Jacob Wikmark committed
144
145
    LabGUI.set!(gui, layout)

146
147
    LabGUI.animate(gui, :svgdummy, 1/guiframerate)
    LabGUI.animate(gui, :plotdummy, 1/plottingframerate)
Jacob Wikmark's avatar
Jacob Wikmark committed
148
    function responder(req)
Jacob Wikmark's avatar
testing    
Jacob Wikmark committed
149
150
        @async (sleep(1); gui[:rbox] = "0.5") #Inits r textbox, must be done after dom is loaded in browser
        gui()
Jacob Wikmark's avatar
Jacob Wikmark committed
151
152
153
    end
    return (gui, responder, tankpid)
end