Skip to content

Commit

Permalink
Update furnace scripts
Browse files Browse the repository at this point in the history
  • Loading branch information
nilsso committed Jul 31, 2021
1 parent 6b58909 commit d04d848
Show file tree
Hide file tree
Showing 7 changed files with 229 additions and 159 deletions.
27 changes: 27 additions & 0 deletions myps/test-scripts/factory/furnace/display-actual.myps
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
tankC = d0
tankH = d1
tTMinDisp = d2
tTMaxDisp = d3
tFDisp = d4
pFDisp = d5

def AdvancedFurnace = 545937711

def BLUE = 0
def RED = 4
def YELLOW = 5

tTMinDisp.On = 1
tTMaxDisp.On = 1
tFDisp.On = 1
pFDisp.On = 1
tTMinDisp.Color = BLUE
tTMaxDisp.Color = RED
tFDisp.Color = YELLOW
pFDisp.Color = YELLOW
loop:
yield()
tTMinDisp.Setting = tankC.Temperature
tTMaxDisp.Setting = tankH.Temperature
tFDisp.Setting = AdvancedFurnace.all.Temperature.avg
pFDisp.Setting = AdvancedFurnace.all.Pressure.avg
129 changes: 78 additions & 51 deletions myps/test-scripts/factory/furnace/furnace.myps
Original file line number Diff line number Diff line change
@@ -1,65 +1,76 @@
#furnace = d0
#nIAnlzr = d5

tankC = d0
tankH = d1
nCPump = d2
nHPump = d3
progressDest = d4

def vF = 1000 # furnace volume
def R = 8.314 # universal gas consant
def dt = 0.5 # time step

# Source volume-mole factor (adjust per setup)
# NOTE: To start operation, write a "value" greater than zero to the housing. This value is taken to
# be a code for the target temperature and pressure with the format `PPPTTT`, where tT=TTT*10$ and
# pT=PPP*100.
#
# e.g. 500050 -> tT = 050*10 = 500 K, pT = 500*100 = 50,000 kPa = 50 MPa
#
# IMPORTANT: Validation for this input is not done here, and the program will attempt to use
# whatever it is given. If the target temperature is less-than the cold source temperature or
# greater than the hot source temperature then wackiness will certainly occurr!
# (I may change this point in the future)
furnace = d0
tankC = d0
tankH = d1
nCPump = d2
nHPump = d3
nIAnalyzer = d5

# Constants
def vF = 1000 # furnace volume
def R = 8.31446261815324 # universal gas consant
def dt = 0.5 # time step

# (NOTE: ADJUST PER SETUP)
# Source volume-mole factor
# e.g. 6100 = 1 furnace (6000) + 2 pipes (200) L
def nCfactor = (6200) / 6000
def nHfactor = (6200) / 6000
# (TODO: Need to implement defines calculated from other defines)
def nCfactor = (6100) / 6000
def nHfactor = (6100) / 6000

# PD coefficients and minimum error (perhaps adjust)
# (NOTE: MAY NEED TO ADJUST)
# PD (proportional, derivative) term coefficients
# (furnace)
def kPF = 0.09
def kDF = 0.06

# (cold source)
def kPC = 0.09
def kDC = 0.06

# (hot source)
def kPH = 0.09
def kDH = 0.06

def ERRORF = 0.01
def ERRORC = 0.03
def ERRORH = 0.03

# Input target temperature/pressure format: PPPTTT
# ```
# input = db.Setting
# tT = input % 1000 * 10 K
# input = trunc(input / 1000)
# pT = input * 100 kPa
# ```
# e.g. 500050 -> tT = 50*10 = 500 K, pT = 500*100 = 50,000 kPa
# Acceptable error amounts for
# (a) the removal of gas from the furnace,
# (b) the transfer of cold CO2 (i.e. removal from source)
# (c) the transfer of hot CO2
def ERRORF = 0.1
def ERRORC = 0.1
def ERRORH = 0.1

db.Setting = -1
loop:
yield()
input = db.Setting
if input < 0:
if input <= 0:
db.Setting = -1
else:
db.Setting = 0
elif input > 0:
# (a) unpack input temperature and pressure

# (b) batch read temperature and pressure from furnace (tF/pF)

# (c) read tank temperatures

# (d) calculate moles to remove and add from C and H (nR/nC/nH)
fix rF, rC, rH, eFPrev, eCPrev, eHPrev
fix rF, rC, rH, eFPrev, eCPrev, eHPrev, eTotal0
tag calcMolesTargets:
# (a) unpack input target temperature and pressure (tT/pT)
input = trunc(input)
tT = (input % 1000) * 10
pT = trunc(input / 1000) * 100

# (b) read furnace moles and temperature (tF/pF)
nF = furnace.TotalMoles
tF = furnace.Temperature

# (c) read tank temperatures (tC/tH)
tC = tankC.Temperature
tH = tankH.Temperature

# (d) calculate moles to remove and add from C and H (nR/nCI/nHI)
nT = pT * vF / (R * tT)
nRC = nT * (tT - tH) / (tH - tF) + nF
nRH = nT * (tT - tC) / (tC - tF) + nF
Expand All @@ -69,6 +80,8 @@ loop:
nHI = nI * (tC - tI) / (tC - tH)
nCI = nI - nHI

# (e) calculate target moles for each source (rF/rC/rH),
# initial errors (eFPrev/eCPrev/eHPrev), and total initial error (eTotal0)
rF = nF - nR
eFPrev = nF - rF

Expand All @@ -80,38 +93,49 @@ loop:
rH = nH - nHI
eHPrev = nH - rH

# (e) remove nR, mix nC and nH
tag E:
# triple PD loop! controls reaching rF/C/H moles in F/C/H
# if rF is reached, start inputting the nI mixture in advance to (f)
eTotal0 = eFPrev + eCPrev + eHPrev

# (f) reach target moles for each source using PD (proportional, derivative) controls
# NOTE: currently some pumps are bugged in that their Settings are unbounded outside
# their normal range [0,100] when set with logic (i.e. it's possible to set negative values
# and larger than 100 values). Those control values are for now temporarily bounded.
tag PDControl:
tag PDLoop:
yield()
# (f.1) Calculate the furnace output control value (uF) and update error (eFPrev)
nF = furnace.TotalMoles
eF = nF - rF
uF = max(0, min(100, (kPF * eF) + (kDF * (eF - eFPrev) / dt))) # (kPF, kDF)
#uF = (kPF * eF) + (kDF * (eF - eFPrev) / dt) # (kPF, kDF)
eFPrev = eF

# (f.2) Calculate the cold source control value (uC) and update error (eCPrev)
nC = nCfactor * tankC.TotalMoles
eC = nC - rC
#uC = max(0, min(100, (kPC * eC) + (kDC * (eC - eCPrev) / dt))) # (kPC, kDC)
uC = (kPC * eC) + (kDC * (eC - eCPrev) / dt) # (kPC, kDC)
eCPrev = eC

# (f.3) Calculate the hot source control value (uH) and update error (eHPrev)
nH = nHfactor * tankH.TotalMoles
eH = nH - rH
#uH = max(0, min(100, (kPH * eH) + (kDH * (eH - eHPrev) / dt))) # (kPH, kDH)
uH = (kPH * eH) + (kDH * (eH - eHPrev) / dt) # (kPH, kDH)
eHPrev = eH

# (f.4) Set housing to current progress (total error / initial error)
progress = ((eF - ERRORF) + (eC - ERRORC) + (eH - ERRORH)) / eTotal0
db.Setting = progress # set progress

# (f.5) Set rates from control values, and loop if any error is above limit
FNotDone = (eF > ERRORF)
CNotDone = (eC > ERRORC)
HNotDone = (eH > ERRORH)

furnace.SettingOutput = FNotDone ? uF : 0
#furnace.SettingInput = FNotDone ? 0 : 1000
nCPump.Setting = CNotDone ? uC : 0
nHPump.Setting = HNotDone ? uH : 0
furnace.SettingOutput = FNotDone ? uF : 0
furnace.SettingInput = FNotDone ? 0 : 1000
nCPump.Setting = CNotDone ? uC : 0
nHPump.Setting = HNotDone ? uH : 0

nCPump.On = CNotDone
nHPump.On = HNotDone
Expand All @@ -121,10 +145,13 @@ loop:
bnez(HNotDone, PDLoop)

# (g) input the nC and nH gas
# NOTE: As mentioned previously, furnace.SettingInput is currently unbounded when set by
# logic. Using this to cheat here and instantly fill the furnace volume from the mixer
# system by setting the input rate to be equal to the mixer system volume.
furnace.SettingInput = 1000
while nIAnlzr.TotalMoles > 0:
while nIAnalyzer.TotalMoles > 0:
yield()
furnace.SettingInput = 0

# (?) enjoy
db.Setting = 0
db.Setting = -1
75 changes: 0 additions & 75 deletions myps/test-scripts/factory/furnace/progress.myps

This file was deleted.

42 changes: 42 additions & 0 deletions myps/test-scripts/factory/furnace/starter.myps
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
importantStartButton = d0
validatorControl = d1
furnaceControl = d2
tTDisp = d3
pTDisp = d4
progressDisp = d5

def BLUE = 0
def GREEN = 2

tTDisp.On = 0
tTDisp.On = 0
progressDisp.On = 0
tTDisp.Color = GREEN
tTDisp.Color = GREEN
progressDisp.Color = BLUE

loop:
yield()
targetCode = validatorControl.Setting
importantStartButton.On = (targetCode > 0)

if importantStartButton.Setting:
# going to assume that furnaceControl.Setting is -1 by here
tT = (targetCode % 1000) * 10
pT = trunc(targetCode / 1000) * 100

tTDisp.On = 1
pTDisp.On = 1
progressDisp.On = 1
tTDisp.Setting = tT
pTDisp.Setting = pT
progressDisp.Setting = 0

furnaceControl.Setting = targetCode
yield()
while furnaceControl.Setting != -1:
progressDisp.Setting = furnaceControl.Setting
yield()
pTDisp.On = 0
tTDisp.On = 0
progressDisp.On = 0
28 changes: 0 additions & 28 deletions myps/test-scripts/factory/furnace/validator-dispatcher.myps

This file was deleted.

Loading

0 comments on commit d04d848

Please sign in to comment.