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

formatting

parent f3f7db1e
......@@ -12,7 +12,7 @@ using Parameters
#struct BC
#ar::Array{Float64, 1}
#ar::Array{Float64, 1}
#end
#bc = BC([])
......@@ -55,7 +55,7 @@ end
function (p::PID)(r, y, onv=(1,1,1))
#The PID should operate entirely on normalized values
@unpack b, K, h, Ti, Td, N, y_old, P, I, D, Tot = p
ad = Td/(N*h+Td)
bd = N*K*Td/(N*h+Td)
......@@ -99,13 +99,13 @@ end
mutable struct Pump <: PhysicalProcess
h::Float64
stream::LabStream
u::Float64
v::Float64
u::Float64
v::Float64
pid::PID
venturirange::Array{Float64, 1}
#venturimax::Float64
measure::AnalogInput10V
control::Union{AnalogOutput10V, Void}
control::Union{AnalogOutput10V, Void}
end
function Pump(stream)
......@@ -122,22 +122,22 @@ inputrange(p::Pump) = [(0.,1.)]
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?
p.u = yref
p.u = yref
end
function initialize(p::Pump)
@async while true
@periodically p.h begin
@async while true
@periodically p.h begin
#normalized venturi
#push!(bc.ar, time())
#push!(bc.ar, time())
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))
send(p.control, 10*clamp(p.v, 0.0, 1.0))
end
end
send(p.control, 10*clamp(p.v, 0.0, 1.0))
end
end
end
measure(p::Pump) = read(p.measure)
struct DoubleTank <: PhysicalProcess
......@@ -150,15 +150,15 @@ struct DoubleTank <: PhysicalProcess
end
function DoubleTank(;
h::Float64 = 0.05,
stream::LabStream = ComediStream(),
measure = [AnalogInput10V(0), AnalogInput10V(1)],
uprange = [uppertankempty, uppertankfull],
lowrange = [lowertankempty, lowertankfull],
pump::Pump = Pump(stream)) #Change
h::Float64 = 0.05,
stream::LabStream = ComediStream(),
measure = [AnalogInput10V(0), AnalogInput10V(1)],
uprange = [uppertankempty, uppertankfull],
lowrange = [lowertankempty, lowertankfull],
pump::Pump = Pump(stream)) #Change
p = DoubleTank(Float64(h),stream,measure, uprange, lowrange, pump)
init_devices!(p.stream, p.measure..., pump.measure, pump.control)
initialize(pump)
initialize(pump)
p
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.
"""
function calibrate(p::DoubleTank)
print("Calibrating...\n")
print("Calibrating...\n")
filltime = 40
emptytime = 30
emptytime = 30
calpts = 20
#Override pump control
......@@ -190,8 +190,8 @@ function calibrate(p::DoubleTank)
fulltanks = read.(p.measure)
venturimax = mean(measurements)
print("Upper Full Voltage: $(fulltanks[1])\n")
print("Lower Full Voltage: $(fulltanks[2])\n")
print("Upper Full Voltage: $(fulltanks[1])\n")
print("Lower Full Voltage: $(fulltanks[2])\n")
print("Venturi Avg max: $venturimax\n")
send(c, 0.0)
......@@ -201,9 +201,9 @@ function calibrate(p::DoubleTank)
end
emptytanks = read.(p.measure)
venturimin = mean(measurements)
print("Upper Empty Voltage: $(emptytanks[1])\n")
print("Lower Empty Voltage: $(emptytanks[2])\n")
print("Upper Empty Voltage: $(emptytanks[1])\n")
print("Lower Empty Voltage: $(emptytanks[2])\n")
print("Venturi Avg min: $venturimin\n")
p.uprange[1] = emptytanks[1]
......@@ -214,8 +214,8 @@ function calibrate(p::DoubleTank)
#Return pump control
p.pump.control = c
print("Finished calibration\n")
nothing
print("Finished calibration\n")
nothing
end
function calibrate(p::DoubleTankSimulator)
......@@ -223,13 +223,13 @@ function calibrate(p::DoubleTankSimulator)
end
function measure(p::DoubleTank)
#This should give an array of two values in the range [0,1]
minv = [p.uprange[1], p.lowrange[1]]
maxv = [p.uprange[2], p.lowrange[2]]
#This should give an array of two values in the range [0,1]
minv = [p.uprange[1], p.lowrange[1]]
maxv = [p.uprange[2], p.lowrange[2]]
clamp.((read.(p.measure)-minv)./(maxv-minv), 0.0, 1.0)
end
const AbstractDoubleTank = Union{DoubleTank, DoubleTankSimulator}
......@@ -246,9 +246,10 @@ function qu(p, u)
end
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
x *= scale
#The simulator should operate on physical values but u should be in [0,1]
u = clamp(u, inputrange(p)[1]...)
qut = a1*sqrt(2*g*x[1]) #m^3/s
......@@ -265,8 +266,8 @@ function control(p::DoubleTankSimulator, u::Number)
if(-dA2*h>x[2]*A)
dA2 = -x[2]*A/h
end
x[1] += (dA1*h)/A #m change later
x[2] += (dA2*h)/A #m change later
x[1] += (dA1*h)/A
x[2] += (dA2*h)/A
x /= scale
@pack p = a1, a2, g, x, A, h
......@@ -277,5 +278,8 @@ measure(p::DoubleTankSimulator) = p.x + p.σ*p.scale*randn(2)
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")
include("DoubleTank.jl")
include("tanklabgui.jl")
const simulated = false
const simulated = true
const webservice = true
#Just for testing
......@@ -22,8 +22,10 @@ else
end
if simulated
print("Running simulated process\n")
R = DoubleTankSimulator(σ = 0.001)
else
print("Running physical process\n")
using LabConnections
R = DoubleTank()
end
......@@ -35,7 +37,7 @@ 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)
LabProcesses.initialize(P)
#initialize(P)
for (i,t) = enumerate(0:P.h:duration)
@periodically P.h begin
......
......@@ -4,7 +4,7 @@ function makegui(P, plottingframerate = 10, guiframerate = 10)
boundslower = (-2,2)
maxplotlength = 800
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
rcont, dummygraphic1 = @construct for
......@@ -28,11 +28,11 @@ function makegui(P, plottingframerate = 10, guiframerate = 10)
rbox in textbox(" r "),
expcsv in button("Export Data")
rv = obs(r)[]
rv = obs(r)[]
if !endswith(rbox, ".") && rbox != "0"
try
#obs(r).val = parse(Float64, rbox)
rv = parse(Float64, rbox)
rv = parse(Float64, rbox)
end
if 0.0<rv<1.0
obs(r).val = rv
......@@ -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]])
print("Data exported to datatanklab.csv\n")
end
LabGUI.set!(gui, widgets)
add!(gui, rcont)
add!(gui, w0)
......@@ -129,7 +129,7 @@ function makegui(P, plottingframerate = 10, guiframerate = 10)
gui.widgets[:run],
gui.widgets[:cali],
gui.widgets[:expcsv])
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)
......@@ -139,7 +139,7 @@ function makegui(P, plottingframerate = 10, guiframerate = 10)
left_element = setindex_(left_element, graphic2, 2, 1)
layout = setindex_(layout, left_element, 1, 1)
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
layout[1][2].props[:style][:width] = "25%"
......
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