moved supervisor unit/facility files out of sessions folder

This commit is contained in:
Mikayla Fischler
2023-02-18 17:36:44 -05:00
parent c18e7ef4d0
commit 9f95801bfc
5 changed files with 6 additions and 3 deletions

View File

@@ -1,853 +0,0 @@
local log = require("scada-common.log")
local rsio = require("scada-common.rsio")
local types = require("scada-common.types")
local util = require("scada-common.util")
local rsctl = require("supervisor.session.rsctl")
local unit = require("supervisor.session.unit")
local PROCESS = types.PROCESS
local PROCESS_NAMES = types.PROCESS_NAMES
-- 7.14 kJ per blade for 1 mB of fissile fuel<br/>
-- 2856 FE per blade per 1 mB, 285.6 FE per blade per 0.1 mB (minimum)
local POWER_PER_BLADE = util.joules_to_fe(7140)
local FLOW_STABILITY_DELAY_S = unit.FLOW_STABILITY_DELAY_MS / 1000
-- background radiation 0.0000001 Sv/h (99.99 nSv/h)
-- "green tint" radiation 0.00001 Sv/h (10 uSv/h)
-- damaging radiation 0.00006 Sv/h (60 uSv/h)
local RADIATION_ALARM_LEVEL = 0.00001
local HIGH_CHARGE = 1.0
local RE_ENABLE_CHARGE = 0.95
local AUTO_SCRAM = {
NONE = 0,
MATRIX_DC = 1,
MATRIX_FILL = 2,
CRIT_ALARM = 3,
RADIATION = 4,
GEN_FAULT = 5
}
local START_STATUS = {
OK = 0,
NO_UNITS = 1,
BLADE_MISMATCH = 2
}
local charge_Kp = 0.275
local charge_Ki = 0.0
local charge_Kd = 4.5
local rate_Kp = 2.45
local rate_Ki = 0.4825
local rate_Kd = -1.0
---@class facility_management
local facility = {}
-- create a new facility management object
---@param num_reactors integer number of reactor units
---@param cooling_conf table cooling configurations of reactor units
function facility.new(num_reactors, cooling_conf)
local self = {
units = {},
redstone = {},
induction = {},
envd = {},
status_text = { "START UP", "initializing..." },
all_sys_ok = false,
rtu_conn_count = 0,
-- process control
units_ready = false,
mode = PROCESS.INACTIVE,
last_mode = PROCESS.INACTIVE,
return_mode = PROCESS.INACTIVE,
mode_set = PROCESS.MAX_BURN,
start_fail = START_STATUS.OK,
max_burn_combined = 0.0, -- maximum burn rate to clamp at
burn_target = 0.1, -- burn rate target for aggregate burn mode
charge_setpoint = 0, -- FE charge target setpoint
gen_rate_setpoint = 0, -- FE/t charge rate target setpoint
group_map = { 0, 0, 0, 0 }, -- units -> group IDs
prio_defs = { {}, {}, {}, {} }, -- priority definitions (each level is a table of units)
at_max_burn = false,
ascram = false,
ascram_reason = AUTO_SCRAM.NONE,
---@class ascram_status
ascram_status = {
matrix_dc = false,
matrix_fill = false,
crit_alarm = false,
radiation = false,
gen_fault = false
},
-- closed loop control
charge_conversion = 1.0,
time_start = 0.0,
initial_ramp = true,
waiting_on_ramp = false,
waiting_on_stable = false,
accumulator = 0.0,
saturated = false,
last_update = 0,
last_error = 0.0,
last_time = 0.0,
-- statistics
im_stat_init = false,
avg_charge = util.mov_avg(3, 0.0),
avg_inflow = util.mov_avg(6, 0.0),
avg_outflow = util.mov_avg(6, 0.0)
}
-- create units
for i = 1, num_reactors do
table.insert(self.units, unit.new(i, cooling_conf[i].BOILERS, cooling_conf[i].TURBINES))
end
-- init redstone RTU I/O controller
local rs_rtu_io_ctl = rsctl.new(self.redstone)
-- unlink disconnected units
---@param sessions table
local function _unlink_disconnected_units(sessions)
util.filter_table(sessions, function (u) return u.is_connected() end)
end
-- check if all auto-controlled units completed ramping
local function _all_units_ramped()
local all_ramped = true
for i = 1, #self.prio_defs do
local units = self.prio_defs[i]
for u = 1, #units do
all_ramped = all_ramped and units[u].a_ramp_complete()
end
end
return all_ramped
end
-- split a burn rate among the reactors
---@param burn_rate number burn rate assignment
---@param ramp boolean true to ramp, false to set right away
---@param abort_on_fault boolean? true to exit if one device has an effective burn rate different than its limit
---@return integer unallocated_br100, boolean? aborted
local function _allocate_burn_rate(burn_rate, ramp, abort_on_fault)
local unallocated = math.floor(burn_rate * 100)
-- go through all priority groups
for i = 1, #self.prio_defs do
local units = self.prio_defs[i]
if #units > 0 then
local split = math.floor(unallocated / #units)
local splits = {}
for u = 1, #units do splits[u] = split end
splits[#units] = splits[#units] + (unallocated % #units)
-- go through all reactor units in this group
for id = 1, #units do
local u = units[id] ---@type reactor_unit
local ctl = u.get_control_inf()
local lim_br100 = u.a_get_effective_limit()
if abort_on_fault and (lim_br100 ~= ctl.lim_br100) then
-- effective limit differs from set limit, unit is degraded
return unallocated, true
end
local last = ctl.br100
if splits[id] <= lim_br100 then
ctl.br100 = splits[id]
else
ctl.br100 = lim_br100
if id < #units then
local remaining = #units - id
split = math.floor(unallocated / remaining)
for x = (id + 1), #units do splits[x] = split end
splits[#units] = splits[#units] + (unallocated % remaining)
end
end
unallocated = math.max(0, unallocated - ctl.br100)
if last ~= ctl.br100 then
log.debug("unit " .. u.get_id() .. ": set to " .. ctl.br100 .. " (was " .. last .. ")")
u.a_commit_br100(ramp)
end
end
end
end
return unallocated, false
end
-- PUBLIC FUNCTIONS --
---@class facility
local public = {}
-- ADD/LINK DEVICES --
-- link a redstone RTU session
---@param rs_unit unit_session
function public.add_redstone(rs_unit)
table.insert(self.redstone, rs_unit)
end
-- link an imatrix RTU session
---@param imatrix unit_session
function public.add_imatrix(imatrix)
table.insert(self.induction, imatrix)
end
-- link an environment detector RTU session
---@param envd unit_session
function public.add_envd(envd)
table.insert(self.envd, envd)
end
-- purge devices associated with the given RTU session ID
---@param session integer RTU session ID
function public.purge_rtu_devices(session)
util.filter_table(self.redstone, function (s) return s.get_session_id() ~= session end)
util.filter_table(self.induction, function (s) return s.get_session_id() ~= session end)
util.filter_table(self.envd, function (s) return s.get_session_id() ~= session end)
end
-- UPDATE --
-- supervisor sessions reporting the list of active RTU sessions
---@param rtu_sessions table session list of all connected RTUs
function public.report_rtus(rtu_sessions)
self.rtu_conn_count = #rtu_sessions
end
-- update (iterate) the facility management
function public.update()
-- unlink RTU unit sessions if they are closed
_unlink_disconnected_units(self.redstone)
_unlink_disconnected_units(self.induction)
_unlink_disconnected_units(self.envd)
-- current state for process control
local charge_update = 0
local rate_update = 0
-- calculate moving averages for induction matrix
if self.induction[1] ~= nil then
local matrix = self.induction[1] ---@type unit_session
local db = matrix.get_db() ---@type imatrix_session_db
charge_update = db.tanks.last_update
rate_update = db.state.last_update
if (charge_update > 0) and (rate_update > 0) then
if self.im_stat_init then
self.avg_charge.record(util.joules_to_fe(db.tanks.energy), charge_update)
self.avg_inflow.record(util.joules_to_fe(db.state.last_input), rate_update)
self.avg_outflow.record(util.joules_to_fe(db.state.last_output), rate_update)
else
self.im_stat_init = true
self.avg_charge.reset(util.joules_to_fe(db.tanks.energy))
self.avg_inflow.reset(util.joules_to_fe(db.state.last_input))
self.avg_outflow.reset(util.joules_to_fe(db.state.last_output))
end
end
else
self.im_stat_init = false
end
self.all_sys_ok = true
for i = 1, #self.units do
self.all_sys_ok = self.all_sys_ok and not self.units[i].get_control_inf().degraded
end
-------------------------
-- Run Process Control --
-------------------------
local avg_charge = self.avg_charge.compute()
local avg_inflow = self.avg_inflow.compute()
local now = util.time_s()
local state_changed = self.mode ~= self.last_mode
local next_mode = self.mode
-- once auto control is started, sort the priority sublists by limits
if state_changed then
self.saturated = false
log.debug("FAC: state changed from " .. PROCESS_NAMES[self.last_mode + 1] .. " to " .. PROCESS_NAMES[self.mode + 1])
if (self.last_mode == PROCESS.INACTIVE) or (self.last_mode == PROCESS.GEN_RATE_FAULT_IDLE) then
self.start_fail = START_STATUS.OK
if (self.mode ~= PROCESS.MATRIX_FAULT_IDLE) and (self.mode ~= PROCESS.SYSTEM_ALARM_IDLE) then
-- auto clear ASCRAM
self.ascram = false
end
local blade_count = nil
self.max_burn_combined = 0.0
for i = 1, #self.prio_defs do
table.sort(self.prio_defs[i],
---@param a reactor_unit
---@param b reactor_unit
function (a, b) return a.get_control_inf().lim_br100 < b.get_control_inf().lim_br100 end
)
for _, u in pairs(self.prio_defs[i]) do
local u_blade_count = u.get_control_inf().blade_count
if blade_count == nil then
blade_count = u_blade_count
elseif (u_blade_count ~= blade_count) and (self.mode == PROCESS.GEN_RATE) then
log.warning("FAC: cannot start GEN_RATE process with inconsistent unit blade counts")
next_mode = PROCESS.INACTIVE
self.start_fail = START_STATUS.BLADE_MISMATCH
end
if self.start_fail == START_STATUS.OK then u.a_engage() end
self.max_burn_combined = self.max_burn_combined + (u.get_control_inf().lim_br100 / 100.0)
end
end
if blade_count == nil then
-- no units
log.warning("FAC: cannot start process control with 0 units assigned")
next_mode = PROCESS.INACTIVE
self.start_fail = START_STATUS.NO_UNITS
else
self.charge_conversion = blade_count * POWER_PER_BLADE
end
elseif self.mode == PROCESS.INACTIVE then
for i = 1, #self.prio_defs do
-- SCRAM reactors and disengage auto control
-- use manual SCRAM since inactive was requested, and automatic SCRAM trips an alarm
for _, u in pairs(self.prio_defs[i]) do
u.scram()
u.a_disengage()
end
end
log.info("FAC: disengaging auto control (now inactive)")
end
self.initial_ramp = true
self.waiting_on_ramp = false
self.waiting_on_stable = false
else
self.initial_ramp = false
end
-- update unit ready state
local assign_count = 0
self.units_ready = true
for i = 1, #self.prio_defs do
for _, u in pairs(self.prio_defs[i]) do
assign_count = assign_count + 1
self.units_ready = self.units_ready and u.get_control_inf().ready
end
end
-- perform mode-specific operations
if self.mode == PROCESS.INACTIVE then
if not self.units_ready then
self.status_text = { "NOT READY", "assigned units not ready" }
elseif self.start_fail == START_STATUS.NO_UNITS and assign_count == 0 then
self.status_text = { "START FAILED", "no units were assigned" }
elseif self.start_fail == START_STATUS.BLADE_MISMATCH then
self.status_text = { "START FAILED", "turbine blade count mismatch" }
else
self.status_text = { "IDLE", "control disengaged" }
end
elseif self.mode == PROCESS.MAX_BURN then
-- run units at their limits
if state_changed then
self.time_start = now
self.saturated = true
self.status_text = { "MONITORED MODE", "running reactors at limit" }
log.info(util.c("FAC: MAX_BURN process mode started"))
end
_allocate_burn_rate(self.max_burn_combined, true)
elseif self.mode == PROCESS.BURN_RATE then
-- a total aggregate burn rate
if state_changed then
self.time_start = now
self.status_text = { "BURN RATE MODE", "running" }
log.info(util.c("FAC: BURN_RATE process mode started"))
end
local unallocated = _allocate_burn_rate(self.burn_target, true)
self.saturated = self.burn_target == self.max_burn_combined or unallocated > 0
elseif self.mode == PROCESS.CHARGE then
-- target a level of charge
if state_changed then
self.time_start = now
self.last_time = now
self.last_error = 0
self.accumulator = 0
self.status_text = { "CHARGE MODE", "running control loop" }
log.info(util.c("FAC: CHARGE mode starting PID control"))
elseif self.last_update ~= charge_update then
-- convert to kFE to make constants not microscopic
local error = util.round((self.charge_setpoint - avg_charge) / 1000) / 1000
-- stop accumulator when saturated to avoid windup
if not self.saturated then
self.accumulator = self.accumulator + (error * (now - self.last_time))
end
local runtime = now - self.time_start
local integral = self.accumulator
local derivative = (error - self.last_error) / (now - self.last_time)
local P = (charge_Kp * error)
local I = (charge_Ki * integral)
local D = (charge_Kd * derivative)
local output = P + I + D
-- clamp at range -> output clamped (out_c)
local out_c = math.max(0, math.min(output, self.max_burn_combined))
self.saturated = output ~= out_c
log.debug(util.sprintf("CHARGE[%f] { CHRG[%f] ERR[%f] INT[%f] => OUT[%f] OUT_C[%f] <= P[%f] I[%f] D[%d] }",
runtime, avg_charge, error, integral, output, out_c, P, I, D))
_allocate_burn_rate(out_c, true)
self.last_time = now
self.last_error = error
end
self.last_update = charge_update
elseif self.mode == PROCESS.GEN_RATE then
-- target a rate of generation
if state_changed then
-- estimate an initial output
local output = self.gen_rate_setpoint / self.charge_conversion
local unallocated = _allocate_burn_rate(output, true)
self.saturated = output >= self.max_burn_combined or unallocated > 0
self.waiting_on_ramp = true
self.status_text = { "GENERATION MODE", "starting up" }
log.info(util.c("FAC: GEN_RATE process mode initial ramp started (initial target is ", output, " mB/t)"))
elseif self.waiting_on_ramp then
if _all_units_ramped() then
self.waiting_on_ramp = false
self.waiting_on_stable = true
self.time_start = now
self.status_text = { "GENERATION MODE", "holding ramped rate" }
log.info("FAC: GEN_RATE process mode initial ramp completed, holding for stablization time")
end
elseif self.waiting_on_stable then
if (now - self.time_start) > FLOW_STABILITY_DELAY_S then
self.waiting_on_stable = false
self.time_start = now
self.last_time = now
self.last_error = 0
self.accumulator = 0
self.status_text = { "GENERATION MODE", "running control loop" }
log.info("FAC: GEN_RATE process mode initial hold completed, starting PID control")
end
elseif self.last_update ~= rate_update then
-- convert to MFE (in rounded kFE) to make constants not microscopic
local error = util.round((self.gen_rate_setpoint - avg_inflow) / 1000) / 1000
-- stop accumulator when saturated to avoid windup
if not self.saturated then
self.accumulator = self.accumulator + (error * (now - self.last_time))
end
local runtime = now - self.time_start
local integral = self.accumulator
local derivative = (error - self.last_error) / (now - self.last_time)
local P = (rate_Kp * error)
local I = (rate_Ki * integral)
local D = (rate_Kd * derivative)
-- velocity (rate) (derivative of charge level => rate) feed forward
local FF = self.gen_rate_setpoint / self.charge_conversion
local output = P + I + D + FF
-- clamp at range -> output clamped (sp_c)
local out_c = math.max(0, math.min(output, self.max_burn_combined))
self.saturated = output ~= out_c
log.debug(util.sprintf("GEN_RATE[%f] { RATE[%f] ERR[%f] INT[%f] => OUT[%f] OUT_C[%f] <= P[%f] I[%f] D[%f] }",
runtime, avg_inflow, error, integral, output, out_c, P, I, D))
_allocate_burn_rate(out_c, false)
self.last_time = now
self.last_error = error
end
self.last_update = rate_update
elseif self.mode == PROCESS.MATRIX_FAULT_IDLE then
-- exceeded charge, wait until condition clears
if self.ascram_reason == AUTO_SCRAM.NONE then
next_mode = self.return_mode
log.info("FAC: exiting matrix fault idle state due to fault resolution")
elseif self.ascram_reason == AUTO_SCRAM.CRIT_ALARM then
next_mode = PROCESS.INACTIVE
log.info("FAC: exiting matrix fault idle state due to critical unit alarm")
end
elseif self.mode == PROCESS.SYSTEM_ALARM_IDLE then
-- do nothing, wait for user to confirm (stop and reset)
elseif self.mode == PROCESS.GEN_RATE_FAULT_IDLE then
-- system faulted (degraded/not ready) while running generation rate mode
-- mode will need to be fully restarted once everything is OK to re-ramp to feed-forward
if self.units_ready then
log.info("FAC: system ready after faulting out of GEN_RATE process mode, switching back...")
next_mode = PROCESS.GEN_RATE
end
elseif self.mode ~= PROCESS.INACTIVE then
log.error(util.c("FAC: unsupported process mode ", self.mode, ", switching to inactive"))
next_mode = PROCESS.INACTIVE
end
------------------------------
-- Evaluate Automatic SCRAM --
------------------------------
local astatus = self.ascram_status
if (self.mode ~= PROCESS.INACTIVE) and (self.mode ~= PROCESS.SYSTEM_ALARM_IDLE) then
if self.induction[1] ~= nil then
local matrix = self.induction[1] ---@type unit_session
local db = matrix.get_db() ---@type imatrix_session_db
-- clear matrix disconnected
if astatus.matrix_dc then
astatus.matrix_dc = false
log.info("FAC: induction matrix reconnected, clearing ASCRAM condition")
end
-- check matrix fill too high
local was_fill = astatus.matrix_fill
astatus.matrix_fill = (db.tanks.energy_fill >= HIGH_CHARGE) or (astatus.matrix_fill and db.tanks.energy_fill > RE_ENABLE_CHARGE)
if was_fill and not astatus.matrix_fill then
log.info("FAC: charge state of induction matrix entered acceptable range <= " .. (RE_ENABLE_CHARGE * 100) .. "%")
end
-- check for critical unit alarms
for i = 1, #self.units do
local u = self.units[i] ---@type reactor_unit
if u.has_critical_alarm() then
log.info(util.c("FAC: emergency exit of process control due to critical unit alarm (unit ", u.get_id(), ")"))
break
end
end
-- check for facility radiation
if self.envd[1] ~= nil then
local envd = self.envd[1] ---@type unit_session
local e_db = envd.get_db() ---@type envd_session_db
astatus.radiation = e_db.radiation_raw > RADIATION_ALARM_LEVEL
else
-- don't clear, if it is true then we lost it with high radiation, so just keep alarming
-- operator can restart the system or hit the stop/reset button
end
-- system not ready, will need to restart GEN_RATE mode
-- clears when we enter the fault waiting state
astatus.gen_fault = self.mode == PROCESS.GEN_RATE and not self.units_ready
else
astatus.matrix_dc = true
end
-- log.debug(util.c("dc: ", astatus.matrix_dc, " fill: ", astatus.matrix_fill, " crit: ", astatus.crit_alarm, " gen: ", astatus.gen_fault))
local scram = astatus.matrix_dc or astatus.matrix_fill or astatus.crit_alarm or astatus.gen_fault
if scram and not self.ascram then
-- SCRAM all units
for i = 1, #self.prio_defs do
for _, u in pairs(self.prio_defs[i]) do
u.a_scram()
end
end
if astatus.crit_alarm then
-- highest priority alarm
next_mode = PROCESS.SYSTEM_ALARM_IDLE
self.ascram_reason = AUTO_SCRAM.CRIT_ALARM
self.status_text = { "AUTOMATIC SCRAM", "critical unit alarm tripped" }
log.info("FAC: automatic SCRAM due to critical unit alarm")
elseif astatus.radiation then
next_mode = PROCESS.SYSTEM_ALARM_IDLE
self.ascram_reason = AUTO_SCRAM.RADIATION
self.status_text = { "AUTOMATIC SCRAM", "facility radiation high" }
log.info("FAC: automatic SCRAM due to high facility radiation")
elseif astatus.matrix_dc then
next_mode = PROCESS.MATRIX_FAULT_IDLE
self.ascram_reason = AUTO_SCRAM.MATRIX_DC
self.status_text = { "AUTOMATIC SCRAM", "induction matrix disconnected" }
if self.mode ~= PROCESS.MATRIX_FAULT_IDLE then self.return_mode = self.mode end
log.info("FAC: automatic SCRAM due to induction matrix disconnection")
elseif astatus.matrix_fill then
next_mode = PROCESS.MATRIX_FAULT_IDLE
self.ascram_reason = AUTO_SCRAM.MATRIX_FILL
self.status_text = { "AUTOMATIC SCRAM", "induction matrix fill high" }
if self.mode ~= PROCESS.MATRIX_FAULT_IDLE then self.return_mode = self.mode end
log.info("FAC: automatic SCRAM due to induction matrix high charge")
elseif astatus.gen_fault then
-- lowest priority alarm
next_mode = PROCESS.GEN_RATE_FAULT_IDLE
self.ascram_reason = AUTO_SCRAM.GEN_FAULT
self.status_text = { "GENERATION MODE IDLE", "paused: system not ready" }
log.info("FAC: automatic SCRAM due to unit problem while in GEN_RATE mode, will resume once all units are ready")
end
end
self.ascram = scram
if not self.ascram then
self.ascram_reason = AUTO_SCRAM.NONE
-- reset PLC RPS trips if we should
for i = 1, #self.units do
local u = self.units[i] ---@type reactor_unit
u.a_cond_rps_reset()
end
end
end
-- update last mode and set next mode
self.last_mode = self.mode
self.mode = next_mode
end
-- call the update function of all units in the facility
function public.update_units()
for i = 1, #self.units do
local u = self.units[i] ---@type reactor_unit
u.update()
end
end
-- COMMANDS --
-- SCRAM all reactor units
function public.scram_all()
for i = 1, #self.units do
local u = self.units[i] ---@type reactor_unit
u.scram()
end
end
-- ack all alarms on all reactor units
function public.ack_all()
for i = 1, #self.units do
local u = self.units[i] ---@type reactor_unit
u.ack_all()
end
end
-- stop auto control
function public.auto_stop()
self.mode = PROCESS.INACTIVE
end
-- set automatic control configuration and start the process
---@param config coord_auto_config configuration
---@return table response ready state (successfully started) and current configuration (after updating)
function public.auto_start(config)
local ready = false
-- load up current limits
local limits = {}
for i = 1, num_reactors do
local u = self.units[i] ---@type reactor_unit
limits[i] = u.get_control_inf().lim_br100 * 100
end
-- only allow changes if not running
if self.mode == PROCESS.INACTIVE then
if (type(config.mode) == "number") and (config.mode > PROCESS.INACTIVE) and (config.mode <= PROCESS.GEN_RATE) then
self.mode_set = config.mode
end
if (type(config.burn_target) == "number") and config.burn_target >= 0.1 then
self.burn_target = config.burn_target
end
if (type(config.charge_target) == "number") and config.charge_target >= 0 then
self.charge_setpoint = config.charge_target * 1000000 -- convert MFE to FE
end
if (type(config.gen_target) == "number") and config.gen_target >= 0 then
self.gen_rate_setpoint = config.gen_target * 1000 -- convert kFE to FE
end
if (type(config.limits) == "table") and (#config.limits == num_reactors) then
for i = 1, num_reactors do
local limit = config.limits[i]
if (type(limit) == "number") and (limit >= 0.1) then
limits[i] = limit
self.units[i].set_burn_limit(limit)
end
end
end
ready = self.mode_set > 0
if (self.mode_set == PROCESS.CHARGE) and (self.charge_setpoint <= 0) then
ready = false
elseif (self.mode_set == PROCESS.GEN_RATE) and (self.gen_rate_setpoint <= 0) then
ready = false
elseif (self.mode_set == PROCESS.BURN_RATE) and (self.burn_target < 0.1) then
ready = false
end
ready = ready and self.units_ready
if ready then self.mode = self.mode_set end
end
return { ready, self.mode_set, self.burn_target, self.charge_setpoint, self.gen_rate_setpoint, limits }
end
-- SETTINGS --
-- set the automatic control group of a unit
---@param unit_id integer unit ID
---@param group integer group ID or 0 for independent
function public.set_group(unit_id, group)
if group >= 0 and group <= 4 and self.mode == PROCESS.INACTIVE then
-- remove from old group if previously assigned
local old_group = self.group_map[unit_id]
if old_group ~= 0 then
util.filter_table(self.prio_defs[old_group], function (u) return u.get_id() ~= unit_id end)
end
self.group_map[unit_id] = group
-- add to group if not independent
if group > 0 then
table.insert(self.prio_defs[group], self.units[unit_id])
end
end
end
-- READ STATES/PROPERTIES --
-- get build properties of all machines
function public.get_build()
local build = {}
build.induction = {}
for i = 1, #self.induction do
local matrix = self.induction[i] ---@type unit_session
build.induction[matrix.get_device_idx()] = { matrix.get_db().formed, matrix.get_db().build }
end
return build
end
-- get automatic process control status
function public.get_control_status()
local astat = self.ascram_status
return {
self.all_sys_ok,
self.units_ready,
self.mode,
self.waiting_on_ramp or self.waiting_on_stable,
self.at_max_burn or self.saturated,
self.ascram,
astat.matrix_dc,
astat.matrix_fill,
astat.crit_alarm,
astat.radiation,
astat.gen_fault or self.mode == PROCESS.GEN_RATE_FAULT_IDLE,
self.status_text[1],
self.status_text[2],
self.group_map
}
end
-- get RTU statuses
function public.get_rtu_statuses()
local status = {}
-- total count of all connected RTUs in the facility
status.count = self.rtu_conn_count
-- power averages from induction matricies
status.power = {
self.avg_charge.compute(),
self.avg_inflow.compute(),
self.avg_outflow.compute()
}
-- status of induction matricies (including tanks)
status.induction = {}
for i = 1, #self.induction do
local matrix = self.induction[i] ---@type unit_session
status.induction[matrix.get_device_idx()] = {
matrix.is_faulted(),
matrix.get_db().formed,
matrix.get_db().state,
matrix.get_db().tanks
}
end
-- radiation monitors (environment detectors)
status.rad_mon = {}
for i = 1, #self.envd do
local envd = self.envd[i] ---@type unit_session
status.rad_mon[envd.get_device_idx()] = {
envd.is_faulted(),
envd.get_db().radiation
}
end
return status
end
function public.get_units()
return self.units
end
return public
end
return facility

View File

@@ -3,8 +3,8 @@ local mqueue = require("scada-common.mqueue")
local util = require("scada-common.util")
local config = require("supervisor.config")
local facility = require("supervisor.facility")
local facility = require("supervisor.session.facility")
local svqtypes = require("supervisor.session.svqtypes")
local coordinator = require("supervisor.session.coordinator")

View File

@@ -1,761 +0,0 @@
local log = require("scada-common.log")
local rsio = require("scada-common.rsio")
local types = require("scada-common.types")
local util = require("scada-common.util")
local logic = require("supervisor.session.unitlogic")
local plc = require("supervisor.session.plc")
local rsctl = require("supervisor.session.rsctl")
---@class reactor_control_unit
local unit = {}
local WASTE_MODE = types.WASTE_MODE
local ALARM = types.ALARM
local PRIO = types.ALARM_PRIORITY
local ALARM_STATE = types.ALARM_STATE
local TRI_FAIL = types.TRI_FAIL
local DUMPING_MODE = types.DUMPING_MODE
local PLC_S_CMDS = plc.PLC_S_CMDS
local IO = rsio.IO
local FLOW_STABILITY_DELAY_MS = 15000
local DT_KEYS = {
ReactorBurnR = "RBR",
ReactorTemp = "RTP",
ReactorFuel = "RFL",
ReactorWaste = "RWS",
ReactorCCool = "RCC",
ReactorHCool = "RHC",
BoilerWater = "BWR",
BoilerSteam = "BST",
BoilerCCool = "BCC",
BoilerHCool = "BHC",
TurbineSteam = "TST",
TurbinePower = "TPR"
}
---@alias ALARM_INT_STATE integer
local AISTATE = {
INACTIVE = 0,
TRIPPING = 1,
TRIPPED = 2,
ACKED = 3,
RING_BACK = 4,
RING_BACK_TRIPPING = 5
}
unit.FLOW_STABILITY_DELAY_MS = FLOW_STABILITY_DELAY_MS
---@class alarm_def
---@field state ALARM_INT_STATE internal alarm state
---@field trip_time integer time (ms) when first tripped
---@field hold_time integer time (s) to hold before tripping
---@field id ALARM alarm ID
---@field tier integer alarm urgency tier (0 = highest)
-- create a new reactor unit
---@param for_reactor integer reactor unit number
---@param num_boilers integer number of boilers expected
---@param num_turbines integer number of turbines expected
function unit.new(for_reactor, num_boilers, num_turbines)
---@class _unit_self
local self = {
r_id = for_reactor,
plc_s = nil, ---@class plc_session_struct
plc_i = nil, ---@class plc_session
num_boilers = num_boilers,
num_turbines = num_turbines,
types = { DT_KEYS = DT_KEYS, AISTATE = AISTATE },
defs = { FLOW_STABILITY_DELAY_MS = FLOW_STABILITY_DELAY_MS },
redstone = {},
boilers = {},
turbines = {},
envd = {},
-- auto control
ramp_target_br100 = 0,
-- state tracking
deltas = {},
last_heartbeat = 0,
damage_initial = 0,
damage_start = 0,
damage_last = 0,
damage_est_last = 0,
waste_mode = WASTE_MODE.AUTO,
status_text = { "UNKNOWN", "awaiting connection..." },
-- logic for alarms
had_reactor = false,
last_rate_change_ms = 0,
---@type rps_status
last_rps_trips = {
dmg_crit = false,
high_temp = false,
no_cool = false,
ex_waste = false,
ex_hcool = false,
no_fuel = false,
fault = false,
timeout = false,
manual = false,
automatic = false,
sys_fail = false,
force_dis = false
},
plc_cache = {
active = false,
ok = false,
rps_trip = false,
---@type rps_status
rps_status = {
dmg_crit = false,
high_temp = false,
no_cool = false,
ex_waste = false,
ex_hcool = false,
no_fuel = false,
fault = false,
timeout = false,
manual = false,
automatic = false,
sys_fail = false,
force_dis = false
},
damage = 0,
temp = 0,
waste = 0
},
---@class alarm_monitors
alarms = {
-- reactor lost under the condition of meltdown imminent
ContainmentBreach = { state = AISTATE.INACTIVE, trip_time = 0, hold_time = 0, id = ALARM.ContainmentBreach, tier = PRIO.CRITICAL },
-- radiation monitor alarm for this unit
ContainmentRadiation = { state = AISTATE.INACTIVE, trip_time = 0, hold_time = 0, id = ALARM.ContainmentRadiation, tier = PRIO.CRITICAL },
-- reactor offline after being online
ReactorLost = { state = AISTATE.INACTIVE, trip_time = 0, hold_time = 0, id = ALARM.ReactorLost, tier = PRIO.URGENT },
-- damage >100%
CriticalDamage = { state = AISTATE.INACTIVE, trip_time = 0, hold_time = 0, id = ALARM.CriticalDamage, tier = PRIO.CRITICAL },
-- reactor damage increasing
ReactorDamage = { state = AISTATE.INACTIVE, trip_time = 0, hold_time = 0, id = ALARM.ReactorDamage, tier = PRIO.EMERGENCY },
-- reactor >1200K
ReactorOverTemp = { state = AISTATE.INACTIVE, trip_time = 0, hold_time = 0, id = ALARM.ReactorOverTemp, tier = PRIO.URGENT },
-- reactor >1100K
ReactorHighTemp = { state = AISTATE.INACTIVE, trip_time = 0, hold_time = 2, id = ALARM.ReactorHighTemp, tier = PRIO.TIMELY },
-- waste = 100%
ReactorWasteLeak = { state = AISTATE.INACTIVE, trip_time = 0, hold_time = 0, id = ALARM.ReactorWasteLeak, tier = PRIO.EMERGENCY },
-- waste >85%
ReactorHighWaste = { state = AISTATE.INACTIVE, trip_time = 0, hold_time = 2, id = ALARM.ReactorHighWaste, tier = PRIO.TIMELY },
-- RPS trip occured
RPSTransient = { state = AISTATE.INACTIVE, trip_time = 0, hold_time = 1, id = ALARM.RPSTransient, tier = PRIO.URGENT },
-- BoilRateMismatch, CoolantFeedMismatch, SteamFeedMismatch, MaxWaterReturnFeed
RCSTransient = { state = AISTATE.INACTIVE, trip_time = 0, hold_time = 5, id = ALARM.RCSTransient, tier = PRIO.TIMELY },
-- "It's just a routine turbin' trip!" -Bill Gibson, "The China Syndrome"
TurbineTrip = { state = AISTATE.INACTIVE, trip_time = 0, hold_time = 1, id = ALARM.TurbineTrip, tier = PRIO.URGENT }
},
---@class unit_db
db = {
---@class annunciator
annunciator = {
-- reactor
PLCOnline = false,
PLCHeartbeat = false, -- alternate true/false to blink, each time there is a keep_alive
RadiationMonitor = 1,
AutoControl = false,
ReactorSCRAM = false,
ManualReactorSCRAM = false,
AutoReactorSCRAM = false,
RadiationWarning = false,
RCPTrip = false,
RCSFlowLow = false,
CoolantLevelLow = false,
ReactorTempHigh = false,
ReactorHighDeltaT = false,
FuelInputRateLow = false,
WasteLineOcclusion = false,
HighStartupRate = false,
-- cooling
RCSFault = false,
EmergencyCoolant = 1,
CoolantFeedMismatch = false,
BoilRateMismatch = false,
SteamFeedMismatch = false,
MaxWaterReturnFeed = false,
-- boilers
BoilerOnline = {},
HeatingRateLow = {},
WaterLevelLow = {},
-- turbines
TurbineOnline = {},
SteamDumpOpen = {},
TurbineOverSpeed = {},
TurbineTrip = {}
},
---@class alarms
alarm_states = {
ALARM_STATE.INACTIVE,
ALARM_STATE.INACTIVE,
ALARM_STATE.INACTIVE,
ALARM_STATE.INACTIVE,
ALARM_STATE.INACTIVE,
ALARM_STATE.INACTIVE,
ALARM_STATE.INACTIVE,
ALARM_STATE.INACTIVE,
ALARM_STATE.INACTIVE,
ALARM_STATE.INACTIVE,
ALARM_STATE.INACTIVE,
ALARM_STATE.INACTIVE
},
-- fields for facility control
---@class unit_control
control = {
ready = false,
degraded = false,
blade_count = 0,
br100 = 0,
lim_br100 = 0
}
}
}
-- init redstone RTU I/O controller
local rs_rtu_io_ctl = rsctl.new(self.redstone)
-- init boiler table fields
for _ = 1, num_boilers do
table.insert(self.db.annunciator.BoilerOnline, false)
table.insert(self.db.annunciator.HeatingRateLow, false)
end
-- init turbine table fields
for _ = 1, num_turbines do
table.insert(self.db.annunciator.TurbineOnline, false)
table.insert(self.db.annunciator.SteamDumpOpen, TRI_FAIL.OK)
table.insert(self.db.annunciator.TurbineOverSpeed, false)
table.insert(self.db.annunciator.TurbineTrip, false)
end
-- PRIVATE FUNCTIONS --
--#region time derivative utility functions
-- compute a change with respect to time of the given value
---@param key string value key
---@param value number value
---@param time number timestamp for value
local function _compute_dt(key, value, time)
if self.deltas[key] then
local data = self.deltas[key]
if time > data.last_t then
data.dt = (value - data.last_v) / (time - data.last_t)
data.last_v = value
data.last_t = time
end
else
self.deltas[key] = {
last_t = time,
last_v = value,
dt = 0.0
}
end
end
-- clear a delta
---@param key string value key
local function _reset_dt(key) self.deltas[key] = nil end
-- get the delta t of a value
---@param key string value key
---@return number
function self._get_dt(key)
if self.deltas[key] then return self.deltas[key].dt else return 0.0 end
end
-- update all delta computations
local function _dt__compute_all()
if self.plc_i ~= nil then
local plc_db = self.plc_i.get_db()
local last_update_s = plc_db.last_status_update / 1000.0
_compute_dt(DT_KEYS.ReactorBurnR, plc_db.mek_status.act_burn_rate, last_update_s)
_compute_dt(DT_KEYS.ReactorTemp, plc_db.mek_status.temp, last_update_s)
_compute_dt(DT_KEYS.ReactorFuel, plc_db.mek_status.fuel, last_update_s)
_compute_dt(DT_KEYS.ReactorWaste, plc_db.mek_status.waste, last_update_s)
_compute_dt(DT_KEYS.ReactorCCool, plc_db.mek_status.ccool_amnt, last_update_s)
_compute_dt(DT_KEYS.ReactorHCool, plc_db.mek_status.hcool_amnt, last_update_s)
end
for i = 1, #self.boilers do
local boiler = self.boilers[i] ---@type unit_session
local db = boiler.get_db() ---@type boilerv_session_db
local last_update_s = db.tanks.last_update / 1000.0
_compute_dt(DT_KEYS.BoilerWater .. boiler.get_device_idx(), db.tanks.water.amount, last_update_s)
_compute_dt(DT_KEYS.BoilerSteam .. boiler.get_device_idx(), db.tanks.steam.amount, last_update_s)
_compute_dt(DT_KEYS.BoilerCCool .. boiler.get_device_idx(), db.tanks.ccool.amount, last_update_s)
_compute_dt(DT_KEYS.BoilerHCool .. boiler.get_device_idx(), db.tanks.hcool.amount, last_update_s)
end
for i = 1, #self.turbines do
local turbine = self.turbines[i] ---@type unit_session
local db = turbine.get_db() ---@type turbinev_session_db
local last_update_s = db.tanks.last_update / 1000.0
_compute_dt(DT_KEYS.TurbineSteam .. turbine.get_device_idx(), db.tanks.steam.amount, last_update_s)
---@todo unused currently?
_compute_dt(DT_KEYS.TurbinePower .. turbine.get_device_idx(), db.tanks.energy, last_update_s)
end
end
--#endregion
--#region redstone I/O
local __rs_w = rs_rtu_io_ctl.digital_write
local __rs_r = rs_rtu_io_ctl.digital_read
-- valves
local waste_pu = { open = function () __rs_w(IO.WASTE_PU, true) end, close = function () __rs_w(IO.WASTE_PU, false) end }
local waste_sna = { open = function () __rs_w(IO.WASTE_PO, true) end, close = function () __rs_w(IO.WASTE_PO, false) end }
local waste_po = { open = function () __rs_w(IO.WASTE_POPL, true) end, close = function () __rs_w(IO.WASTE_POPL, false) end }
local waste_sps = { open = function () __rs_w(IO.WASTE_AM, true) end, close = function () __rs_w(IO.WASTE_AM, false) end }
local emer_cool = { open = function () __rs_w(IO.U_EMER_COOL, true) end, close = function () __rs_w(IO.U_EMER_COOL, false) end }
--#endregion
-- unlink disconnected units
---@param sessions table
local function _unlink_disconnected_units(sessions)
util.filter_table(sessions, function (u) return u.is_connected() end)
end
-- PUBLIC FUNCTIONS --
---@class reactor_unit
local public = {}
-- ADD/LINK DEVICES --
--#region
-- link the PLC
---@param plc_session plc_session_struct
function public.link_plc_session(plc_session)
self.had_reactor = true
self.plc_s = plc_session
self.plc_i = plc_session.instance
-- reset deltas
_reset_dt(DT_KEYS.ReactorTemp)
_reset_dt(DT_KEYS.ReactorFuel)
_reset_dt(DT_KEYS.ReactorWaste)
_reset_dt(DT_KEYS.ReactorCCool)
_reset_dt(DT_KEYS.ReactorHCool)
end
-- link a redstone RTU session
---@param rs_unit unit_session
function public.add_redstone(rs_unit)
table.insert(self.redstone, rs_unit)
-- send or re-send waste settings
public.set_waste(self.waste_mode)
end
-- link a turbine RTU session
---@param turbine unit_session
function public.add_turbine(turbine)
if #self.turbines < num_turbines and turbine.get_device_idx() <= num_turbines then
table.insert(self.turbines, turbine)
-- reset deltas
_reset_dt(DT_KEYS.TurbineSteam .. turbine.get_device_idx())
_reset_dt(DT_KEYS.TurbinePower .. turbine.get_device_idx())
return true
else
return false
end
end
-- link a boiler RTU session
---@param boiler unit_session
function public.add_boiler(boiler)
if #self.boilers < num_boilers and boiler.get_device_idx() <= num_boilers then
table.insert(self.boilers, boiler)
-- reset deltas
_reset_dt(DT_KEYS.BoilerWater .. boiler.get_device_idx())
_reset_dt(DT_KEYS.BoilerSteam .. boiler.get_device_idx())
_reset_dt(DT_KEYS.BoilerCCool .. boiler.get_device_idx())
_reset_dt(DT_KEYS.BoilerHCool .. boiler.get_device_idx())
return true
else
return false
end
end
-- link an environment detector RTU session
---@param envd unit_session
function public.add_envd(envd)
table.insert(self.envd, envd)
end
-- purge devices associated with the given RTU session ID
---@param session integer RTU session ID
function public.purge_rtu_devices(session)
util.filter_table(self.redstone, function (s) return s.get_session_id() ~= session end)
util.filter_table(self.boilers, function (s) return s.get_session_id() ~= session end)
util.filter_table(self.turbines, function (s) return s.get_session_id() ~= session end)
util.filter_table(self.envd, function (s) return s.get_session_id() ~= session end)
end
--#endregion
-- UPDATE SESSION --
-- update (iterate) this unit
function public.update()
-- unlink PLC if session was closed
if self.plc_s ~= nil and not self.plc_s.open then
self.plc_s = nil
self.plc_i = nil
self.db.control.br100 = 0
self.db.control.lim_br100 = 0
end
-- unlink RTU unit sessions if they are closed
_unlink_disconnected_units(self.redstone)
_unlink_disconnected_units(self.boilers)
_unlink_disconnected_units(self.turbines)
_unlink_disconnected_units(self.envd)
-- update degraded state for auto control
self.db.control.degraded = (#self.boilers ~= num_boilers) or (#self.turbines ~= num_turbines) or (self.plc_i == nil)
-- check boilers formed/faulted
for i = 1, #self.boilers do
local sess = self.boilers[i] ---@type unit_session
local boiler = sess.get_db() ---@type boilerv_session_db
if sess.is_faulted() or not boiler.formed then
self.db.control.degraded = true
end
end
-- check turbines formed/faulted
for i = 1, #self.turbines do
local sess = self.turbines[i] ---@type unit_session
local turbine = sess.get_db() ---@type turbinev_session_db
if sess.is_faulted() or not turbine.formed then
self.db.control.degraded = true
end
end
-- check plc formed/faulted
if self.plc_i ~= nil then
local rps = self.plc_i.get_rps()
if rps.fault or rps.sys_fail then
self.db.control.degraded = true
end
end
-- update deltas
_dt__compute_all()
-- update annunciator logic
logic.update_annunciator(self)
-- update alarm status
logic.update_alarms(self)
-- update status text
logic.update_status_text(self)
-- check if emergency coolant is needed
if self.plc_cache.rps_status.no_cool then
emer_cool.open()
elseif not self.plc_cache.rps_trip then
-- can't turn off on sufficient coolant level since it might drop again
-- turn off once system is OK again
emer_cool.close()
end
end
-- AUTO CONTROL OPERATIONS --
--#region
-- engage automatic control
function public.a_engage()
self.db.annunciator.AutoControl = true
if self.plc_i ~= nil then
self.plc_i.auto_lock(true)
end
end
-- disengage automatic control
function public.a_disengage()
self.db.annunciator.AutoControl = false
if self.plc_i ~= nil then
self.plc_i.auto_lock(false)
self.db.control.br100 = 0
end
end
-- get the actual limit of this unit
--
-- if it is degraded or not ready, the limit will be 0
---@return integer lim_br100
function public.a_get_effective_limit()
if not self.db.control.ready or self.db.control.degraded or self.plc_cache.rps_trip then
self.db.control.br100 = 0
return 0
else
return self.db.control.lim_br100
end
end
-- set the automatic burn rate based on the last set burn rate in 100ths
---@param ramp boolean true to ramp to rate, false to set right away
function public.a_commit_br100(ramp)
if self.db.annunciator.AutoControl then
if self.plc_i ~= nil then
self.plc_i.auto_set_burn(self.db.control.br100 / 100, ramp)
if ramp then self.ramp_target_br100 = self.db.control.br100 end
end
end
end
-- check if ramping is complete (burn rate is same as target)
---@return boolean complete
function public.a_ramp_complete()
if self.plc_i ~= nil then
return self.plc_i.is_ramp_complete() or
(self.plc_i.get_status().act_burn_rate == 0 and self.db.control.br100 == 0) or
public.a_get_effective_limit() == 0
else return true end
end
-- perform an automatic SCRAM
function public.a_scram()
if self.plc_s ~= nil then
self.db.control.br100 = 0
self.plc_s.in_queue.push_command(PLC_S_CMDS.ASCRAM)
end
end
-- queue a command to clear timeout/auto-scram if set
function public.a_cond_rps_reset()
if self.plc_s ~= nil and self.plc_i ~= nil then
local rps = self.plc_i.get_rps()
if rps.timeout or rps.automatic then
self.plc_i.auto_lock(true) -- if it timed out/restarted, auto lock was lost, so re-lock it
self.plc_s.in_queue.push_command(PLC_S_CMDS.RPS_AUTO_RESET)
end
end
end
--#endregion
-- OPERATIONS --
--#region
-- queue a command to SCRAM the reactor
function public.scram()
if self.plc_s ~= nil then
self.plc_s.in_queue.push_command(PLC_S_CMDS.SCRAM)
end
end
-- acknowledge all alarms (if possible)
function public.ack_all()
for i = 1, #self.db.alarm_states do
if self.db.alarm_states[i] == ALARM_STATE.TRIPPED then
self.db.alarm_states[i] = ALARM_STATE.ACKED
end
end
end
-- acknowledge an alarm (if possible)
---@param id ALARM alarm ID
function public.ack_alarm(id)
if (type(id) == "number") and (self.db.alarm_states[id] == ALARM_STATE.TRIPPED) then
self.db.alarm_states[id] = ALARM_STATE.ACKED
end
end
-- reset an alarm (if possible)
---@param id ALARM alarm ID
function public.reset_alarm(id)
if (type(id) == "number") and (self.db.alarm_states[id] == ALARM_STATE.RING_BACK) then
self.db.alarm_states[id] = ALARM_STATE.INACTIVE
end
end
-- route reactor waste
---@param mode WASTE_MODE waste handling mode
function public.set_waste(mode)
if mode == WASTE_MODE.AUTO then
---@todo automatic waste routing
self.waste_mode = mode
elseif mode == WASTE_MODE.PLUTONIUM then
-- route through plutonium generation
self.waste_mode = mode
waste_pu.open()
waste_sna.close()
waste_po.close()
waste_sps.close()
elseif mode == WASTE_MODE.POLONIUM then
-- route through polonium generation into pellets
self.waste_mode = mode
waste_pu.close()
waste_sna.open()
waste_po.open()
waste_sps.close()
elseif mode == WASTE_MODE.ANTI_MATTER then
-- route through polonium generation into SPS
self.waste_mode = mode
waste_pu.close()
waste_sna.open()
waste_po.close()
waste_sps.open()
else
log.debug(util.c("invalid waste mode setting ", mode))
end
end
-- set the automatic control max burn rate for this unit
---@param limit number burn rate limit for auto control
function public.set_burn_limit(limit)
if limit > 0 then
self.db.control.lim_br100 = math.floor(limit * 100)
if self.plc_i ~= nil then
if limit > self.plc_i.get_struct().max_burn then
self.db.control.lim_br100 = math.floor(self.plc_i.get_struct().max_burn * 100)
end
end
end
end
--#endregion
-- READ STATES/PROPERTIES --
--#region
-- check if a critical alarm is tripped
function public.has_critical_alarm()
for _, data in pairs(self.alarms) do
if data.tier == PRIO.CRITICAL and (data.state == AISTATE.TRIPPED or data.state == AISTATE.ACKED) then
return true
end
end
return false
end
-- get build properties of all machines
function public.get_build()
local build = {}
if self.plc_i ~= nil then
build.reactor = self.plc_i.get_struct()
end
build.boilers = {}
for i = 1, #self.boilers do
local boiler = self.boilers[i] ---@type unit_session
build.boilers[boiler.get_device_idx()] = { boiler.get_db().formed, boiler.get_db().build }
end
build.turbines = {}
for i = 1, #self.turbines do
local turbine = self.turbines[i] ---@type unit_session
build.turbines[turbine.get_device_idx()] = { turbine.get_db().formed, turbine.get_db().build }
end
return build
end
-- get reactor status
function public.get_reactor_status()
local status = {}
if self.plc_i ~= nil then
status = { self.plc_i.get_status(), self.plc_i.get_rps(), self.plc_i.get_general_status() }
end
return status
end
-- get RTU statuses
function public.get_rtu_statuses()
local status = {}
-- status of boilers (including tanks)
status.boilers = {}
for i = 1, #self.boilers do
local boiler = self.boilers[i] ---@type unit_session
status.boilers[boiler.get_device_idx()] = {
boiler.is_faulted(),
boiler.get_db().formed,
boiler.get_db().state,
boiler.get_db().tanks
}
end
-- status of turbines (including tanks)
status.turbines = {}
for i = 1, #self.turbines do
local turbine = self.turbines[i] ---@type unit_session
status.turbines[turbine.get_device_idx()] = {
turbine.is_faulted(),
turbine.get_db().formed,
turbine.get_db().state,
turbine.get_db().tanks
}
end
-- radiation monitors (environment detectors)
status.rad_mon = {}
for i = 1, #self.envd do
local envd = self.envd[i] ---@type unit_session
status.rad_mon[envd.get_device_idx()] = {
envd.is_faulted(),
envd.get_db().radiation
}
end
return status
end
-- get the annunciator status
function public.get_annunciator() return self.db.annunciator end
-- get the alarm states
function public.get_alarms() return self.db.alarm_states end
-- get information required for automatic reactor control
function public.get_control_inf() return self.db.control end
-- get unit state
function public.get_state()
return { self.status_text[1], self.status_text[2], self.waste_mode, self.db.control.ready, self.db.control.degraded }
end
-- get the reactor ID
function public.get_id() return self.r_id end
--#endregion
return public
end
return unit

View File

@@ -1,623 +0,0 @@
local log = require("scada-common.log")
local rsio = require("scada-common.rsio")
local types = require("scada-common.types")
local util = require("scada-common.util")
local ALARM_STATE = types.ALARM_STATE
local TRI_FAIL = types.TRI_FAIL
local DUMPING_MODE = types.DUMPING_MODE
local IO = rsio.IO
local aistate_string = {
"INACTIVE",
"TRIPPING",
"TRIPPED",
"ACKED",
"RING_BACK",
"RING_BACK_TRIPPING"
}
-- background radiation 0.0000001 Sv/h (99.99 nSv/h)
-- "green tint" radiation 0.00001 Sv/h (10 uSv/h)
-- damaging radiation 0.00006 Sv/h (60 uSv/h)
local RADIATION_ALERT_LEVEL = 0.00001 -- 10 uSv/h
local RADIATION_ALARM_LEVEL = 0.00005 -- 50 uSv/h, not yet damaging but this isn't good
---@class unit_logic_extension
local logic = {}
-- update the annunciator
---@param self _unit_self
function logic.update_annunciator(self)
local DT_KEYS = self.types.DT_KEYS
local _get_dt = self._get_dt
local num_boilers = self.num_boilers
local num_turbines = self.num_turbines
self.db.annunciator.RCSFault = false
-- variables for boiler, or reactor if no boilers used
local total_boil_rate = 0.0
-------------
-- REACTOR --
-------------
-- check PLC status
self.db.annunciator.PLCOnline = self.plc_i ~= nil
local plc_ready = self.db.annunciator.PLCOnline
if self.db.annunciator.PLCOnline then
local plc_db = self.plc_i.get_db()
-- update ready state
-- - can't be tripped
-- - must have received status at least once
-- - must have received struct at least once
plc_ready = plc_db.formed and (not plc_db.no_reactor) and (not plc_db.rps_tripped) and
(next(self.plc_i.get_status()) ~= nil) and (next(self.plc_i.get_struct()) ~= nil)
-- update auto control limit
if (self.db.control.lim_br100 == 0) or ((self.db.control.lim_br100 / 100) > plc_db.mek_struct.max_burn) then
self.db.control.lim_br100 = math.floor(plc_db.mek_struct.max_burn * 100)
end
-- some alarms wait until the burn rate has stabilized, so keep track of that
if math.abs(_get_dt(DT_KEYS.ReactorBurnR)) > 0 then
self.last_rate_change_ms = util.time_ms()
end
-- record reactor stats
self.plc_cache.active = plc_db.mek_status.status
self.plc_cache.ok = not (plc_db.rps_status.fault or plc_db.rps_status.sys_fail or plc_db.rps_status.force_dis)
self.plc_cache.rps_trip = plc_db.rps_tripped
self.plc_cache.rps_status = plc_db.rps_status
self.plc_cache.damage = plc_db.mek_status.damage
self.plc_cache.temp = plc_db.mek_status.temp
self.plc_cache.waste = plc_db.mek_status.waste_fill
-- track damage
if plc_db.mek_status.damage > 0 then
if self.damage_start == 0 then
self.damage_start = util.time_s()
self.damage_initial = plc_db.mek_status.damage
end
else
self.damage_start = 0
self.damage_initial = 0
self.damage_last = 0
self.damage_est_last = 0
end
-- heartbeat blink about every second
if self.last_heartbeat + 1000 < plc_db.last_status_update then
self.db.annunciator.PLCHeartbeat = not self.db.annunciator.PLCHeartbeat
self.last_heartbeat = plc_db.last_status_update
end
-- update other annunciator fields
self.db.annunciator.ReactorSCRAM = plc_db.rps_tripped
self.db.annunciator.ManualReactorSCRAM = plc_db.rps_trip_cause == types.rps_status_t.manual
self.db.annunciator.AutoReactorSCRAM = plc_db.rps_trip_cause == types.rps_status_t.automatic
self.db.annunciator.RCPTrip = plc_db.rps_tripped and (plc_db.rps_status.ex_hcool or plc_db.rps_status.no_cool)
self.db.annunciator.RCSFlowLow = plc_db.mek_status.ccool_fill < 0.75 or plc_db.mek_status.hcool_fill > 0.25
self.db.annunciator.ReactorTempHigh = plc_db.mek_status.temp > 1000
self.db.annunciator.ReactorHighDeltaT = _get_dt(DT_KEYS.ReactorTemp) > 100
self.db.annunciator.FuelInputRateLow = _get_dt(DT_KEYS.ReactorFuel) < -1.0 or plc_db.mek_status.fuel_fill <= 0.01
self.db.annunciator.WasteLineOcclusion = _get_dt(DT_KEYS.ReactorWaste) > 1.0 or plc_db.mek_status.waste_fill >= 0.85
---@todo this is dependent on setup, i.e. how much coolant is buffered and the turbine setup
self.db.annunciator.HighStartupRate = not plc_db.mek_status.status and plc_db.mek_status.burn_rate > 40
-- if no boilers, use reactor heating rate to check for boil rate mismatch
if num_boilers == 0 then
total_boil_rate = plc_db.mek_status.heating_rate
end
else
self.plc_cache.ok = false
end
---------------
-- MISC RTUs --
---------------
self.db.annunciator.RadiationMonitor = 1
self.db.annunciator.RadiationWarning = false
for i = 1, #self.envd do
local envd = self.envd[i] ---@type unit_session
self.db.annunciator.RadiationMonitor = util.trinary(envd.is_faulted(), 2, 3)
self.db.annunciator.RadiationWarning = envd.get_db().radiation_raw > RADIATION_ALERT_LEVEL
break
end
self.db.annunciator.EmergencyCoolant = 1
for i = 1, #self.redstone do
local db = self.redstone[i].get_db() ---@type redstone_session_db
local io = db.io[IO.U_EMER_COOL] ---@type rs_db_dig_io|nil
if io ~= nil then
self.db.annunciator.EmergencyCoolant = util.trinary(io.read(), 3, 2)
break
end
end
-------------
-- BOILERS --
-------------
local boilers_ready = num_boilers == #self.boilers
-- clear boiler online flags
for i = 1, num_boilers do self.db.annunciator.BoilerOnline[i] = false end
-- aggregated statistics
local boiler_steam_dt_sum = 0.0
local boiler_water_dt_sum = 0.0
if num_boilers > 0 then
-- go through boilers for stats and online
for i = 1, #self.boilers do
local session = self.boilers[i] ---@type unit_session
local boiler = session.get_db() ---@type boilerv_session_db
self.db.annunciator.RCSFault = self.db.annunciator.RCSFault or (not boiler.formed) or session.is_faulted()
-- update ready state
-- - must be formed
-- - must have received build, state, and tanks at least once
boilers_ready = boilers_ready and boiler.formed and
(boiler.build.last_update > 0) and
(boiler.state.last_update > 0) and
(boiler.tanks.last_update > 0)
total_boil_rate = total_boil_rate + boiler.state.boil_rate
boiler_steam_dt_sum = _get_dt(DT_KEYS.BoilerSteam .. self.boilers[i].get_device_idx())
boiler_water_dt_sum = _get_dt(DT_KEYS.BoilerWater .. self.boilers[i].get_device_idx())
self.db.annunciator.BoilerOnline[session.get_device_idx()] = true
end
-- check heating rate low
if self.plc_i ~= nil and #self.boilers > 0 then
local r_db = self.plc_i.get_db()
-- check for inactive boilers while reactor is active
for i = 1, #self.boilers do
local boiler = self.boilers[i] ---@type unit_session
local idx = boiler.get_device_idx()
local db = boiler.get_db() ---@type boilerv_session_db
if r_db.mek_status.status then
self.db.annunciator.HeatingRateLow[idx] = db.state.boil_rate == 0
else
self.db.annunciator.HeatingRateLow[idx] = false
end
end
end
else
boiler_steam_dt_sum = _get_dt(DT_KEYS.ReactorHCool)
boiler_water_dt_sum = _get_dt(DT_KEYS.ReactorCCool)
end
---------------------------
-- COOLANT FEED MISMATCH --
---------------------------
-- check coolant feed mismatch if using boilers, otherwise calculate with reactor
local cfmismatch = false
if num_boilers > 0 then
for i = 1, #self.boilers do
local boiler = self.boilers[i] ---@type unit_session
local idx = boiler.get_device_idx()
local db = boiler.get_db() ---@type boilerv_session_db
local gaining_hc = _get_dt(DT_KEYS.BoilerHCool .. idx) > 10.0 or db.tanks.hcool_fill == 1
-- gaining heated coolant
cfmismatch = cfmismatch or gaining_hc
-- losing cooled coolant
cfmismatch = cfmismatch or _get_dt(DT_KEYS.BoilerCCool .. idx) < -10.0 or (gaining_hc and db.tanks.ccool_fill == 0)
end
elseif self.plc_i ~= nil then
local r_db = self.plc_i.get_db()
local gaining_hc = _get_dt(DT_KEYS.ReactorHCool) > 10.0 or r_db.mek_status.hcool_fill == 1
-- gaining heated coolant (steam)
cfmismatch = cfmismatch or gaining_hc
-- losing cooled coolant (water)
cfmismatch = cfmismatch or _get_dt(DT_KEYS.ReactorCCool) < -10.0 or (gaining_hc and r_db.mek_status.ccool_fill == 0)
end
self.db.annunciator.CoolantFeedMismatch = cfmismatch
--------------
-- TURBINES --
--------------
local turbines_ready = num_turbines == #self.turbines
-- clear turbine online flags
for i = 1, num_turbines do self.db.annunciator.TurbineOnline[i] = false end
-- aggregated statistics
local total_flow_rate = 0
local total_input_rate = 0
local max_water_return_rate = 0
-- recompute blade count on the chance that it may have changed
self.db.control.blade_count = 0
-- go through turbines for stats and online
for i = 1, #self.turbines do
local session = self.turbines[i] ---@type unit_session
local turbine = session.get_db() ---@type turbinev_session_db
self.db.annunciator.RCSFault = self.db.annunciator.RCSFault or (not turbine.formed) or session.is_faulted()
-- update ready state
-- - must be formed
-- - must have received build, state, and tanks at least once
turbines_ready = turbines_ready and turbine.formed and
(turbine.build.last_update > 0) and
(turbine.state.last_update > 0) and
(turbine.tanks.last_update > 0)
total_flow_rate = total_flow_rate + turbine.state.flow_rate
total_input_rate = total_input_rate + turbine.state.steam_input_rate
max_water_return_rate = max_water_return_rate + turbine.build.max_water_output
self.db.control.blade_count = self.db.control.blade_count + turbine.build.blades
self.db.annunciator.TurbineOnline[session.get_device_idx()] = true
end
-- check for boil rate mismatch (> 4% error) either between reactor and turbine or boiler and turbine
self.db.annunciator.BoilRateMismatch = math.abs(total_boil_rate - total_input_rate) > (0.04 * total_boil_rate)
-- check for steam feed mismatch and max return rate
local sfmismatch = math.abs(total_flow_rate - total_input_rate) > 10
sfmismatch = sfmismatch or boiler_steam_dt_sum > 2.0 or boiler_water_dt_sum < -2.0
self.db.annunciator.SteamFeedMismatch = sfmismatch
self.db.annunciator.MaxWaterReturnFeed = max_water_return_rate == total_flow_rate and total_flow_rate ~= 0
-- check if steam dumps are open
for i = 1, #self.turbines do
local turbine = self.turbines[i] ---@type unit_session
local db = turbine.get_db() ---@type turbinev_session_db
local idx = turbine.get_device_idx()
if db.state.dumping_mode == DUMPING_MODE.IDLE then
self.db.annunciator.SteamDumpOpen[idx] = TRI_FAIL.OK
elseif db.state.dumping_mode == DUMPING_MODE.DUMPING_EXCESS then
self.db.annunciator.SteamDumpOpen[idx] = TRI_FAIL.PARTIAL
else
self.db.annunciator.SteamDumpOpen[idx] = TRI_FAIL.FULL
end
end
-- check if turbines are at max speed but not keeping up
for i = 1, #self.turbines do
local turbine = self.turbines[i] ---@type unit_session
local db = turbine.get_db() ---@type turbinev_session_db
local idx = turbine.get_device_idx()
self.db.annunciator.TurbineOverSpeed[idx] = (db.state.flow_rate == db.build.max_flow_rate) and (_get_dt(DT_KEYS.TurbineSteam .. idx) > 0.0)
end
--[[
Turbine Trip
a turbine trip is when the turbine stops, which means we are no longer receiving water and lose the ability to cool.
this can be identified by these conditions:
- the current flow rate is 0 mB/t and it should not be
- can initially catch this by detecting a 0 flow rate with a non-zero input rate, but eventually the steam will fill up
- can later identified by presence of steam in tank with a 0 flow rate
]]--
for i = 1, #self.turbines do
local turbine = self.turbines[i] ---@type unit_session
local db = turbine.get_db() ---@type turbinev_session_db
local has_steam = db.state.steam_input_rate > 0 or db.tanks.steam_fill > 0.01
self.db.annunciator.TurbineTrip[turbine.get_device_idx()] = has_steam and db.state.flow_rate == 0
end
-- update auto control ready state for this unit
self.db.control.ready = plc_ready and boilers_ready and turbines_ready
end
-- update an alarm state given conditions
---@param self _unit_self unit instance
---@param tripped boolean if the alarm condition is still active
---@param alarm alarm_def alarm table
local function _update_alarm_state(self, tripped, alarm)
local AISTATE = self.types.AISTATE
local int_state = alarm.state
local ext_state = self.db.alarm_states[alarm.id]
-- alarm inactive
if int_state == AISTATE.INACTIVE then
if tripped then
alarm.trip_time = util.time_ms()
if alarm.hold_time > 0 then
alarm.state = AISTATE.TRIPPING
self.db.alarm_states[alarm.id] = ALARM_STATE.INACTIVE
else
alarm.state = AISTATE.TRIPPED
self.db.alarm_states[alarm.id] = ALARM_STATE.TRIPPED
log.info(util.c("UNIT ", self.r_id, " ALARM ", alarm.id, " (", types.alarm_string[alarm.id], "): TRIPPED [PRIORITY ",
types.alarm_prio_string[alarm.tier + 1],"]"))
end
else
alarm.trip_time = util.time_ms()
self.db.alarm_states[alarm.id] = ALARM_STATE.INACTIVE
end
-- alarm condition met, but not yet for required hold time
elseif (int_state == AISTATE.TRIPPING) or (int_state == AISTATE.RING_BACK_TRIPPING) then
if tripped then
local elapsed = util.time_ms() - alarm.trip_time
if elapsed > (alarm.hold_time * 1000) then
alarm.state = AISTATE.TRIPPED
self.db.alarm_states[alarm.id] = ALARM_STATE.TRIPPED
log.info(util.c("UNIT ", self.r_id, " ALARM ", alarm.id, " (", types.alarm_string[alarm.id], "): TRIPPED [PRIORITY ",
types.alarm_prio_string[alarm.tier + 1],"]"))
end
elseif int_state == AISTATE.RING_BACK_TRIPPING then
alarm.trip_time = 0
alarm.state = AISTATE.RING_BACK
self.db.alarm_states[alarm.id] = ALARM_STATE.RING_BACK
else
alarm.trip_time = 0
alarm.state = AISTATE.INACTIVE
self.db.alarm_states[alarm.id] = ALARM_STATE.INACTIVE
end
-- alarm tripped and alarming
elseif int_state == AISTATE.TRIPPED then
if tripped then
if ext_state == ALARM_STATE.ACKED then
-- was acked by coordinator
alarm.state = AISTATE.ACKED
end
else
alarm.state = AISTATE.RING_BACK
self.db.alarm_states[alarm.id] = ALARM_STATE.RING_BACK
end
-- alarm acknowledged but still tripped
elseif int_state == AISTATE.ACKED then
if not tripped then
alarm.state = AISTATE.RING_BACK
self.db.alarm_states[alarm.id] = ALARM_STATE.RING_BACK
end
-- alarm no longer tripped, operator must reset to clear
elseif int_state == AISTATE.RING_BACK then
if tripped then
alarm.trip_time = util.time_ms()
if alarm.hold_time > 0 then
alarm.state = AISTATE.RING_BACK_TRIPPING
else
alarm.state = AISTATE.TRIPPED
self.db.alarm_states[alarm.id] = ALARM_STATE.TRIPPED
end
elseif ext_state == ALARM_STATE.INACTIVE then
-- was reset by coordinator
alarm.state = AISTATE.INACTIVE
alarm.trip_time = 0
end
else
log.error(util.c("invalid alarm state for unit ", self.r_id, " alarm ", alarm.id), true)
end
-- check for state change
if alarm.state ~= int_state then
local change_str = util.c(aistate_string[int_state + 1], " -> ", aistate_string[alarm.state + 1])
log.debug(util.c("UNIT ", self.r_id, " ALARM ", alarm.id, " (", types.alarm_string[alarm.id], "): ", change_str))
end
end
-- evaluate alarm conditions
---@param self _unit_self unit instance
function logic.update_alarms(self)
local annunc = self.db.annunciator
local plc_cache = self.plc_cache
-- Containment Breach
-- lost plc with critical damage (rip plc, you will be missed)
_update_alarm_state(self, (not plc_cache.ok) and (plc_cache.damage > 99), self.alarms.ContainmentBreach)
-- Containment Radiation
local rad_alarm = false
for i = 1, #self.envd do
rad_alarm = self.envd[i].get_db().radiation_raw > RADIATION_ALARM_LEVEL
break
end
_update_alarm_state(self, rad_alarm, self.alarms.ContainmentRadiation)
-- Reactor Lost
_update_alarm_state(self, self.had_reactor and self.plc_i == nil, self.alarms.ReactorLost)
-- Critical Damage
_update_alarm_state(self, plc_cache.damage >= 100, self.alarms.CriticalDamage)
-- Reactor Damage
local rps_dmg_90 = plc_cache.rps_status.dmg_crit and not self.last_rps_trips.dmg_crit
_update_alarm_state(self, (plc_cache.damage > 0) or rps_dmg_90, self.alarms.ReactorDamage)
-- Over-Temperature
local rps_high_temp = plc_cache.rps_status.high_temp and not self.last_rps_trips.high_temp
_update_alarm_state(self, (plc_cache.temp >= 1200) or rps_high_temp, self.alarms.ReactorOverTemp)
-- High Temperature
_update_alarm_state(self, plc_cache.temp > 1150, self.alarms.ReactorHighTemp)
-- Waste Leak
_update_alarm_state(self, plc_cache.waste >= 0.99, self.alarms.ReactorWasteLeak)
-- High Waste
local rps_high_waste = plc_cache.rps_status.ex_waste and not self.last_rps_trips.ex_waste
_update_alarm_state(self, (plc_cache.waste > 0.50) or rps_high_waste, self.alarms.ReactorHighWaste)
-- RPS Transient (excludes timeouts and manual trips)
local rps_alarm = false
if plc_cache.rps_status.manual ~= nil then
if plc_cache.rps_trip then
for key, val in pairs(plc_cache.rps_status) do
if key ~= "manual" and key ~= "timeout" then rps_alarm = rps_alarm or val end
end
end
end
_update_alarm_state(self, rps_alarm, self.alarms.RPSTransient)
-- RCS Transient
local any_low = annunc.CoolantLevelLow
local any_over = false
for i = 1, #annunc.WaterLevelLow do any_low = any_low or annunc.WaterLevelLow[i] end
for i = 1, #annunc.TurbineOverSpeed do any_over = any_over or annunc.TurbineOverSpeed[i] end
local rcs_trans = any_low or any_over or annunc.RCPTrip or annunc.RCSFlowLow or annunc.MaxWaterReturnFeed
-- annunciator indicators for these states may not indicate a real issue when:
-- > flow is ramping up right after reactor start
-- > flow is ramping down after reactor shutdown
if ((util.time_ms() - self.last_rate_change_ms) > self.defs.FLOW_STABILITY_DELAY_MS) and plc_cache.active then
rcs_trans = rcs_trans or annunc.BoilRateMismatch or annunc.CoolantFeedMismatch or annunc.SteamFeedMismatch
end
_update_alarm_state(self, rcs_trans, self.alarms.RCSTransient)
-- Turbine Trip
local any_trip = false
for i = 1, #annunc.TurbineTrip do any_trip = any_trip or annunc.TurbineTrip[i] end
_update_alarm_state(self, any_trip, self.alarms.TurbineTrip)
-- update last trips table
for key, val in pairs(plc_cache.rps_status) do
self.last_rps_trips[key] = val
end
end
-- update the two unit status text messages
---@param self _unit_self unit instance
function logic.update_status_text(self)
local AISTATE = self.types.AISTATE
-- check if an alarm is active (tripped or ack'd)
---@param alarm table alarm entry
---@return boolean active
local function is_active(alarm)
return alarm.state == AISTATE.TRIPPED or alarm.state == AISTATE.ACKED
end
-- update status text (what the reactor doin?)
if is_active(self.alarms.ContainmentBreach) then
-- boom? or was boom disabled
if self.plc_i ~= nil and self.plc_i.get_rps().force_dis then
self.status_text = { "REACTOR FORCE DISABLED", "meltdown would have occured" }
else
self.status_text = { "CORE MELTDOWN", "reactor destroyed" }
end
elseif is_active(self.alarms.CriticalDamage) then
-- so much for it being a "routine turbin' trip"...
self.status_text = { "MELTDOWN IMMINENT", "evacuate facility immediately" }
elseif is_active(self.alarms.ReactorDamage) then
-- attempt to determine when a chance of a meltdown will occur
self.status_text[1] = "CONTAINMENT TAKING DAMAGE"
if self.plc_cache.damage >= 100 then
self.status_text[2] = "damage critical"
elseif (self.plc_cache.damage - self.damage_initial) > 0 then
if self.plc_cache.damage > self.damage_last then
self.damage_last = self.plc_cache.damage
local rate = (self.plc_cache.damage - self.damage_initial) / (util.time_s() - self.damage_start)
self.damage_est_last = (100 - self.plc_cache.damage) / rate
end
self.status_text[2] = util.c("damage critical in ", util.sprintf("%.1f", self.damage_est_last), "s")
else
self.status_text[2] = "estimating time to critical..."
end
elseif is_active(self.alarms.ContainmentRadiation) then
self.status_text = { "RADIATION DETECTED", "radiation levels above normal" }
-- elseif is_active(self.alarms.RPSTransient) then
-- RPS status handled when checking reactor status
elseif is_active(self.alarms.RCSTransient) then
self.status_text = { "RCS TRANSIENT", "check coolant system" }
elseif is_active(self.alarms.ReactorOverTemp) then
self.status_text = { "CORE OVER TEMP", "reactor core temperature >=1200K" }
elseif is_active(self.alarms.ReactorWasteLeak) then
self.status_text = { "WASTE LEAK", "radioactive waste leak detected" }
elseif is_active(self.alarms.ReactorHighTemp) then
self.status_text = { "CORE TEMP HIGH", "reactor core temperature >1150K" }
elseif is_active(self.alarms.ReactorHighWaste) then
self.status_text = { "WASTE LEVEL HIGH", "waste accumulating in reactor" }
elseif is_active(self.alarms.TurbineTrip) then
self.status_text = { "TURBINE TRIP", "turbine stall occured" }
-- connection dependent states
elseif self.plc_i ~= nil then
local plc_db = self.plc_i.get_db()
if plc_db.mek_status.status then
self.status_text[1] = "ACTIVE"
if self.db.annunciator.ReactorHighDeltaT then
self.status_text[2] = "core temperature rising"
elseif self.db.annunciator.ReactorTempHigh then
self.status_text[2] = "core temp high, system nominal"
elseif self.db.annunciator.FuelInputRateLow then
self.status_text[2] = "insufficient fuel input rate"
elseif self.db.annunciator.WasteLineOcclusion then
self.status_text[2] = "insufficient waste output rate"
elseif (util.time_ms() - self.last_rate_change_ms) <= self.defs.FLOW_STABILITY_DELAY_MS then
self.status_text[2] = "awaiting flow stability"
else
self.status_text[2] = "system nominal"
end
elseif plc_db.rps_tripped then
local cause = "unknown"
if plc_db.rps_trip_cause == "ok" then
-- hmm...
elseif plc_db.rps_trip_cause == "dmg_crit" then
cause = "core damage critical"
elseif plc_db.rps_trip_cause == "high_temp" then
cause = "core temperature high"
elseif plc_db.rps_trip_cause == "no_coolant" then
cause = "insufficient coolant"
elseif plc_db.rps_trip_cause == "full_waste" then
cause = "excess waste"
elseif plc_db.rps_trip_cause == "heated_coolant_backup" then
cause = "excess heated coolant"
elseif plc_db.rps_trip_cause == "no_fuel" then
cause = "insufficient fuel"
elseif plc_db.rps_trip_cause == "fault" then
cause = "hardware fault"
elseif plc_db.rps_trip_cause == "timeout" then
cause = "connection timed out"
elseif plc_db.rps_trip_cause == "manual" then
cause = "manual operator SCRAM"
elseif plc_db.rps_trip_cause == "automatic" then
cause = "automated system SCRAM"
elseif plc_db.rps_trip_cause == "sys_fail" then
cause = "PLC system failure"
elseif plc_db.rps_trip_cause == "force_disabled" then
cause = "reactor force disabled"
end
self.status_text = { "RPS SCRAM", cause }
else
self.status_text[1] = "IDLE"
local temp = plc_db.mek_status.temp
if temp < 350 then
self.status_text[2] = "core cold"
elseif temp < 600 then
self.status_text[2] = "core warm"
else
self.status_text[2] = "core hot"
end
end
else
self.status_text = { "Reactor Off-line", "awaiting connection..." }
end
end
return logic