Commit 5c4f62d1 authored by Jacob Wikmark's avatar Jacob Wikmark
Browse files

formatting

parent f3f7db1e
...@@ -12,7 +12,7 @@ using Parameters ...@@ -12,7 +12,7 @@ using Parameters
#struct BC #struct BC
#ar::Array{Float64, 1} #ar::Array{Float64, 1}
#end #end
#bc = BC([]) #bc = BC([])
...@@ -55,7 +55,7 @@ end ...@@ -55,7 +55,7 @@ end
function (p::PID)(r, y, onv=(1,1,1)) function (p::PID)(r, y, onv=(1,1,1))
#The PID should operate entirely on normalized values #The PID should operate entirely on normalized values
@unpack b, K, h, Ti, Td, N, y_old, P, I, D, Tot = p @unpack b, K, h, Ti, Td, N, y_old, P, I, D, Tot = p
ad = Td/(N*h+Td) ad = Td/(N*h+Td)
bd = N*K*Td/(N*h+Td) bd = N*K*Td/(N*h+Td)
...@@ -99,13 +99,13 @@ end ...@@ -99,13 +99,13 @@ end
mutable struct Pump <: PhysicalProcess mutable struct Pump <: PhysicalProcess
h::Float64 h::Float64
stream::LabStream stream::LabStream
u::Float64 u::Float64
v::Float64 v::Float64
pid::PID pid::PID
venturirange::Array{Float64, 1} venturirange::Array{Float64, 1}
#venturimax::Float64 #venturimax::Float64
measure::AnalogInput10V measure::AnalogInput10V
control::Union{AnalogOutput10V, Void} control::Union{AnalogOutput10V, Void}
end end
function Pump(stream) function Pump(stream)
...@@ -122,22 +122,22 @@ inputrange(p::Pump) = [(0.,1.)] ...@@ -122,22 +122,22 @@ inputrange(p::Pump) = [(0.,1.)]
function control(p::Pump, yref) function control(p::Pump, yref)
#In the java program this controller runs 5 times faster than the regular one, how do we accomplish this? Do we need to? #In the java program this controller runs 5 times faster than the regular one, how do we accomplish this? Do we need to?
p.u = yref p.u = yref
end end
function initialize(p::Pump) function initialize(p::Pump)
@async while true @async while true
@periodically p.h begin @periodically p.h begin
#normalized venturi #normalized venturi
#push!(bc.ar, time()) #push!(bc.ar, time())
venturi = (read(p.measure)-p.venturirange[1])/(p.venturirange[2]-p.venturirange[1]) venturi = (read(p.measure)-p.venturirange[1])/(p.venturirange[2]-p.venturirange[1])
flow = sqrt(max(venturi,0.0)) flow = sqrt(max(venturi,0.0))
p.v = p.pid(p.u, flow, (1,1,0)) p.v = p.pid(p.u, flow, (1,1,0))
send(p.control, 10*clamp(p.v, 0.0, 1.0)) send(p.control, 10*clamp(p.v, 0.0, 1.0))
end end
end end
end end
measure(p::Pump) = read(p.measure) measure(p::Pump) = read(p.measure)
struct DoubleTank <: PhysicalProcess struct DoubleTank <: PhysicalProcess
...@@ -150,15 +150,15 @@ struct DoubleTank <: PhysicalProcess ...@@ -150,15 +150,15 @@ struct DoubleTank <: PhysicalProcess
end end
function DoubleTank(; function DoubleTank(;
h::Float64 = 0.05, h::Float64 = 0.05,
stream::LabStream = ComediStream(), stream::LabStream = ComediStream(),
measure = [AnalogInput10V(0), AnalogInput10V(1)], measure = [AnalogInput10V(0), AnalogInput10V(1)],
uprange = [uppertankempty, uppertankfull], uprange = [uppertankempty, uppertankfull],
lowrange = [lowertankempty, lowertankfull], lowrange = [lowertankempty, lowertankfull],
pump::Pump = Pump(stream)) #Change pump::Pump = Pump(stream)) #Change
p = DoubleTank(Float64(h),stream,measure, uprange, lowrange, pump) p = DoubleTank(Float64(h),stream,measure, uprange, lowrange, pump)
init_devices!(p.stream, p.measure..., pump.measure, pump.control) init_devices!(p.stream, p.measure..., pump.measure, pump.control)
initialize(pump) initialize(pump)
p p
end end
...@@ -171,9 +171,9 @@ end ...@@ -171,9 +171,9 @@ end
Calibrates the tanks. Sets the pump to max for filltime seconds, during which the flow is measured calpts times. The tank voltage is then measured, when they are presumably full. The pump is then set to min for emptytime and the flow is again measured calpts time. The tank voltage is then again measured, when they're presumably empty. These values are then used to set the parameters for the DoubleTank measure function and the DoubleTank.pump controller. Calibrates the tanks. Sets the pump to max for filltime seconds, during which the flow is measured calpts times. The tank voltage is then measured, when they are presumably full. The pump is then set to min for emptytime and the flow is again measured calpts time. The tank voltage is then again measured, when they're presumably empty. These values are then used to set the parameters for the DoubleTank measure function and the DoubleTank.pump controller.
""" """
function calibrate(p::DoubleTank) function calibrate(p::DoubleTank)
print("Calibrating...\n") print("Calibrating...\n")
filltime = 40 filltime = 40
emptytime = 30 emptytime = 30
calpts = 20 calpts = 20
#Override pump control #Override pump control
...@@ -190,8 +190,8 @@ function calibrate(p::DoubleTank) ...@@ -190,8 +190,8 @@ function calibrate(p::DoubleTank)
fulltanks = read.(p.measure) fulltanks = read.(p.measure)
venturimax = mean(measurements) venturimax = mean(measurements)
print("Upper Full Voltage: $(fulltanks[1])\n") print("Upper Full Voltage: $(fulltanks[1])\n")
print("Lower Full Voltage: $(fulltanks[2])\n") print("Lower Full Voltage: $(fulltanks[2])\n")
print("Venturi Avg max: $venturimax\n") print("Venturi Avg max: $venturimax\n")
send(c, 0.0) send(c, 0.0)
...@@ -201,9 +201,9 @@ function calibrate(p::DoubleTank) ...@@ -201,9 +201,9 @@ function calibrate(p::DoubleTank)
end end
emptytanks = read.(p.measure) emptytanks = read.(p.measure)
venturimin = mean(measurements) venturimin = mean(measurements)
print("Upper Empty Voltage: $(emptytanks[1])\n") print("Upper Empty Voltage: $(emptytanks[1])\n")
print("Lower Empty Voltage: $(emptytanks[2])\n") print("Lower Empty Voltage: $(emptytanks[2])\n")
print("Venturi Avg min: $venturimin\n") print("Venturi Avg min: $venturimin\n")
p.uprange[1] = emptytanks[1] p.uprange[1] = emptytanks[1]
...@@ -214,8 +214,8 @@ function calibrate(p::DoubleTank) ...@@ -214,8 +214,8 @@ function calibrate(p::DoubleTank)
#Return pump control #Return pump control
p.pump.control = c p.pump.control = c
print("Finished calibration\n") print("Finished calibration\n")
nothing nothing
end end
function calibrate(p::DoubleTankSimulator) function calibrate(p::DoubleTankSimulator)
...@@ -223,13 +223,13 @@ function calibrate(p::DoubleTankSimulator) ...@@ -223,13 +223,13 @@ function calibrate(p::DoubleTankSimulator)
end end
function measure(p::DoubleTank) function measure(p::DoubleTank)
#This should give an array of two values in the range [0,1] #This should give an array of two values in the range [0,1]
minv = [p.uprange[1], p.lowrange[1]] minv = [p.uprange[1], p.lowrange[1]]
maxv = [p.uprange[2], p.lowrange[2]] maxv = [p.uprange[2], p.lowrange[2]]
clamp.((read.(p.measure)-minv)./(maxv-minv), 0.0, 1.0) clamp.((read.(p.measure)-minv)./(maxv-minv), 0.0, 1.0)
end end
const AbstractDoubleTank = Union{DoubleTank, DoubleTankSimulator} const AbstractDoubleTank = Union{DoubleTank, DoubleTankSimulator}
...@@ -246,9 +246,10 @@ function qu(p, u) ...@@ -246,9 +246,10 @@ function qu(p, u)
end end
function control(p::DoubleTankSimulator, u::Number) function control(p::DoubleTankSimulator, u::Number)
#The simulator should operate on physical values but u should be in [0,1]
@unpack a1, a2, g, x, A, h, scale = p @unpack a1, a2, g, x, A, h, scale = p
x *= scale x *= scale
#The simulator should operate on physical values but u should be in [0,1]
u = clamp(u, inputrange(p)[1]...) u = clamp(u, inputrange(p)[1]...)
qut = a1*sqrt(2*g*x[1]) #m^3/s qut = a1*sqrt(2*g*x[1]) #m^3/s
...@@ -265,8 +266,8 @@ function control(p::DoubleTankSimulator, u::Number) ...@@ -265,8 +266,8 @@ function control(p::DoubleTankSimulator, u::Number)
if(-dA2*h>x[2]*A) if(-dA2*h>x[2]*A)
dA2 = -x[2]*A/h dA2 = -x[2]*A/h
end end
x[1] += (dA1*h)/A #m change later x[1] += (dA1*h)/A
x[2] += (dA2*h)/A #m change later x[2] += (dA2*h)/A
x /= scale x /= scale
@pack p = a1, a2, g, x, A, h @pack p = a1, a2, g, x, A, h
...@@ -277,5 +278,8 @@ measure(p::DoubleTankSimulator) = p.x + p.σ*p.scale*randn(2) ...@@ -277,5 +278,8 @@ measure(p::DoubleTankSimulator) = p.x + p.σ*p.scale*randn(2)
LabProcesses.initialize(p::DoubleTankSimulator) = nothing LabProcesses.initialize(p::DoubleTankSimulator) = nothing
#finalize(p::DoubleTankSimulator) = nothing LabProcesses.finalize(p::DoubleTankSimulator) = nothing
LabProcesses.initialize(p::DoubleTank) = nothing #change later
LabProcesses.finalize(p::DoubleTank) = nothing #change later
...@@ -3,7 +3,7 @@ include("tankgraphic.jl") ...@@ -3,7 +3,7 @@ include("tankgraphic.jl")
include("DoubleTank.jl") include("DoubleTank.jl")
include("tanklabgui.jl") include("tanklabgui.jl")
const simulated = false const simulated = true
const webservice = true const webservice = true
#Just for testing #Just for testing
...@@ -22,8 +22,10 @@ else ...@@ -22,8 +22,10 @@ else
end end
if simulated if simulated
print("Running simulated process\n")
R = DoubleTankSimulator(σ = 0.001) R = DoubleTankSimulator(σ = 0.001)
else else
print("Running physical process\n")
using LabConnections using LabConnections
R = DoubleTank() R = DoubleTank()
end end
...@@ -35,7 +37,7 @@ function prbs_experiment(P, gui, pidcontroller, ;amplitude = 1, duration = 10000 ...@@ -35,7 +37,7 @@ function prbs_experiment(P, gui, pidcontroller, ;amplitude = 1, duration = 10000
y = zeros(0:P.h:duration) y = zeros(0:P.h:duration)
u = zeros(0:P.h:duration) u = zeros(0:P.h:duration)
n = 1 n = 1
#LabProcesses.initialize(P) LabProcesses.initialize(P)
#initialize(P) #initialize(P)
for (i,t) = enumerate(0:P.h:duration) for (i,t) = enumerate(0:P.h:duration)
@periodically P.h begin @periodically P.h begin
......
...@@ -4,7 +4,7 @@ function makegui(P, plottingframerate = 10, guiframerate = 10) ...@@ -4,7 +4,7 @@ function makegui(P, plottingframerate = 10, guiframerate = 10)
boundslower = (-2,2) boundslower = (-2,2)
maxplotlength = 800 maxplotlength = 800
gui = GUI() gui = GUI()
ptfr = 3.8 #pump to flow ratio, approximate ptfr = 3.8 #pump to flow ratio, approximate
#This allows external access to the r value, the rbox string is parsed into this value #This allows external access to the r value, the rbox string is parsed into this value
rcont, dummygraphic1 = @construct for rcont, dummygraphic1 = @construct for
...@@ -28,11 +28,11 @@ function makegui(P, plottingframerate = 10, guiframerate = 10) ...@@ -28,11 +28,11 @@ function makegui(P, plottingframerate = 10, guiframerate = 10)
rbox in textbox(" r "), rbox in textbox(" r "),
expcsv in button("Export Data") expcsv in button("Export Data")
rv = obs(r)[] rv = obs(r)[]
if !endswith(rbox, ".") && rbox != "0" if !endswith(rbox, ".") && rbox != "0"
try try
#obs(r).val = parse(Float64, rbox) #obs(r).val = parse(Float64, rbox)
rv = parse(Float64, rbox) rv = parse(Float64, rbox)
end end
if 0.0<rv<1.0 if 0.0<rv<1.0
obs(r).val = rv obs(r).val = rv
...@@ -96,7 +96,7 @@ function makegui(P, plottingframerate = 10, guiframerate = 10) ...@@ -96,7 +96,7 @@ function makegui(P, plottingframerate = 10, guiframerate = 10)
[gui.data[1] gui.data[2] gui.data[3] gui.data[4] gui.data[5] gui.data[6]]) [gui.data[1] gui.data[2] gui.data[3] gui.data[4] gui.data[5] gui.data[6]])
print("Data exported to datatanklab.csv\n") print("Data exported to datatanklab.csv\n")
end end
LabGUI.set!(gui, widgets) LabGUI.set!(gui, widgets)
add!(gui, rcont) add!(gui, rcont)
add!(gui, w0) add!(gui, w0)
...@@ -129,7 +129,7 @@ function makegui(P, plottingframerate = 10, guiframerate = 10) ...@@ -129,7 +129,7 @@ function makegui(P, plottingframerate = 10, guiframerate = 10)
gui.widgets[:run], gui.widgets[:run],
gui.widgets[:cali], gui.widgets[:cali],
gui.widgets[:expcsv]) gui.widgets[:expcsv])
corner_element = setindex_(corner_element, settings_element, 1, 1) corner_element = setindex_(corner_element, settings_element, 1, 1)
corner_element = setindex_(corner_element, p_element, 1, 2) corner_element = setindex_(corner_element, p_element, 1, 2)
corner_element = setindex_(corner_element, i_element, 1, 3) corner_element = setindex_(corner_element, i_element, 1, 3)
...@@ -139,7 +139,7 @@ function makegui(P, plottingframerate = 10, guiframerate = 10) ...@@ -139,7 +139,7 @@ function makegui(P, plottingframerate = 10, guiframerate = 10)
left_element = setindex_(left_element, graphic2, 2, 1) left_element = setindex_(left_element, graphic2, 2, 1)
layout = setindex_(layout, left_element, 1, 1) layout = setindex_(layout, left_element, 1, 1)
layout = setindex_(layout, Node(:div, graphic1, dummygraphic1, dummygraphic2, io_element, layout = setindex_(layout, Node(:div, graphic1, dummygraphic1, dummygraphic2, io_element,
attributes = tankattr), 1, 2) attributes = tankattr), 1, 2)
#This makes it look a bit nicer, reducing the width of the right column #This makes it look a bit nicer, reducing the width of the right column
layout[1][2].props[:style][:width] = "25%" layout[1][2].props[:style][:width] = "25%"
......
Supports Markdown
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