2023-08-21 03:43:07 +00:00
|
|
|
local audio = require("scada-common.audio")
|
2023-08-21 02:56:51 +00:00
|
|
|
local const = require("scada-common.constants")
|
|
|
|
local log = require("scada-common.log")
|
|
|
|
local rsio = require("scada-common.rsio")
|
|
|
|
local types = require("scada-common.types")
|
|
|
|
local util = require("scada-common.util")
|
2022-12-10 18:58:17 +00:00
|
|
|
|
2023-08-21 02:56:51 +00:00
|
|
|
local unit = require("supervisor.unit")
|
2023-02-18 22:36:44 +00:00
|
|
|
|
2023-08-21 02:56:51 +00:00
|
|
|
local qtypes = require("supervisor.session.rtu.qtypes")
|
2022-12-18 18:56:04 +00:00
|
|
|
|
2023-08-21 02:56:51 +00:00
|
|
|
local rsctl = require("supervisor.session.rsctl")
|
|
|
|
|
2023-08-21 03:43:07 +00:00
|
|
|
local TONE = audio.TONE
|
|
|
|
|
|
|
|
local ALARM = types.ALARM
|
|
|
|
local PRIO = types.ALARM_PRIORITY
|
|
|
|
local ALARM_STATE = types.ALARM_STATE
|
|
|
|
local CONTAINER_MODE = types.CONTAINER_MODE
|
2023-08-21 02:56:51 +00:00
|
|
|
local PROCESS = types.PROCESS
|
|
|
|
local PROCESS_NAMES = types.PROCESS_NAMES
|
|
|
|
local RTU_UNIT_TYPE = types.RTU_UNIT_TYPE
|
|
|
|
local WASTE_MODE = types.WASTE_MODE
|
2023-08-21 03:43:07 +00:00
|
|
|
local WASTE = types.WASTE_PRODUCT
|
2022-12-18 18:56:04 +00:00
|
|
|
|
2024-04-12 04:13:05 +00:00
|
|
|
local IO = rsio.IO
|
2023-02-19 05:14:27 +00:00
|
|
|
|
2023-08-21 02:56:51 +00:00
|
|
|
local DTV_RTU_S_DATA = qtypes.DTV_RTU_S_DATA
|
|
|
|
|
2023-02-21 16:05:57 +00:00
|
|
|
-- 7.14 kJ per blade for 1 mB of fissile fuel<br>
|
2023-01-03 21:50:31 +00:00
|
|
|
-- 2856 FE per blade per 1 mB, 285.6 FE per blade per 0.1 mB (minimum)
|
2024-07-27 16:34:01 +00:00
|
|
|
local POWER_PER_BLADE = util.joules_to_fe_rf(7140)
|
2022-12-18 18:56:04 +00:00
|
|
|
|
2023-02-25 07:25:35 +00:00
|
|
|
local FLOW_STABILITY_DELAY_S = const.FLOW_STABILITY_DELAY_MS / 1000
|
2023-02-08 04:47:58 +00:00
|
|
|
|
2023-02-25 07:25:35 +00:00
|
|
|
local ALARM_LIMS = const.ALARM_LIMITS
|
2022-12-18 18:56:04 +00:00
|
|
|
|
2023-01-03 21:50:31 +00:00
|
|
|
local AUTO_SCRAM = {
|
|
|
|
NONE = 0,
|
|
|
|
MATRIX_DC = 1,
|
2023-02-07 05:32:50 +00:00
|
|
|
MATRIX_FILL = 2,
|
2023-02-11 19:27:29 +00:00
|
|
|
CRIT_ALARM = 3,
|
2023-02-15 03:55:40 +00:00
|
|
|
RADIATION = 4,
|
|
|
|
GEN_FAULT = 5
|
2023-01-03 21:50:31 +00:00
|
|
|
}
|
|
|
|
|
2023-02-10 03:52:10 +00:00
|
|
|
local START_STATUS = {
|
|
|
|
OK = 0,
|
|
|
|
NO_UNITS = 1,
|
|
|
|
BLADE_MISMATCH = 2
|
|
|
|
}
|
|
|
|
|
2024-04-10 02:23:43 +00:00
|
|
|
local charge_Kp = 0.15
|
2023-02-08 04:47:58 +00:00
|
|
|
local charge_Ki = 0.0
|
2024-04-10 02:23:43 +00:00
|
|
|
local charge_Kd = 0.6
|
2023-01-03 21:50:31 +00:00
|
|
|
|
2023-02-10 03:52:10 +00:00
|
|
|
local rate_Kp = 2.45
|
2023-02-11 01:26:25 +00:00
|
|
|
local rate_Ki = 0.4825
|
2023-02-10 03:52:10 +00:00
|
|
|
local rate_Kd = -1.0
|
2023-01-03 21:50:31 +00:00
|
|
|
|
2022-12-10 18:58:17 +00:00
|
|
|
---@class facility_management
|
|
|
|
local facility = {}
|
|
|
|
|
|
|
|
-- create a new facility management object
|
2023-02-25 04:36:16 +00:00
|
|
|
---@nodiscard
|
2024-04-12 04:13:05 +00:00
|
|
|
---@param config svr_config supervisor configuration
|
2023-08-19 17:38:05 +00:00
|
|
|
---@param cooling_conf sv_cooling_conf cooling configurations of reactor units
|
2024-04-12 04:13:05 +00:00
|
|
|
function facility.new(config, cooling_conf)
|
2022-12-10 18:58:17 +00:00
|
|
|
local self = {
|
2022-12-18 18:56:04 +00:00
|
|
|
units = {},
|
2023-02-03 03:58:51 +00:00
|
|
|
status_text = { "START UP", "initializing..." },
|
2023-02-04 18:47:00 +00:00
|
|
|
all_sys_ok = false,
|
2023-07-29 22:16:59 +00:00
|
|
|
allow_testing = false,
|
2023-02-19 05:14:27 +00:00
|
|
|
-- rtus
|
2023-02-16 00:52:28 +00:00
|
|
|
rtu_conn_count = 0,
|
2023-07-15 17:16:36 +00:00
|
|
|
rtu_list = {},
|
2023-02-19 05:14:27 +00:00
|
|
|
redstone = {},
|
|
|
|
induction = {},
|
2023-07-08 20:57:13 +00:00
|
|
|
sps = {},
|
2023-07-15 17:16:36 +00:00
|
|
|
tanks = {},
|
2023-02-19 05:14:27 +00:00
|
|
|
envd = {},
|
|
|
|
-- redstone I/O control
|
|
|
|
io_ctl = nil, ---@type rs_controller
|
2022-12-18 18:56:04 +00:00
|
|
|
-- process control
|
2023-02-03 03:58:51 +00:00
|
|
|
units_ready = false,
|
2022-12-18 18:56:04 +00:00
|
|
|
mode = PROCESS.INACTIVE,
|
2023-01-03 21:50:31 +00:00
|
|
|
last_mode = PROCESS.INACTIVE,
|
2023-02-07 05:32:50 +00:00
|
|
|
return_mode = PROCESS.INACTIVE,
|
2023-02-10 03:52:10 +00:00
|
|
|
mode_set = PROCESS.MAX_BURN,
|
|
|
|
start_fail = START_STATUS.OK,
|
2023-01-26 23:26:26 +00:00
|
|
|
max_burn_combined = 0.0, -- maximum burn rate to clamp at
|
|
|
|
burn_target = 0.1, -- burn rate target for aggregate burn mode
|
2023-02-08 04:47:58 +00:00
|
|
|
charge_setpoint = 0, -- FE charge target setpoint
|
|
|
|
gen_rate_setpoint = 0, -- FE/t charge rate target setpoint
|
2023-06-18 19:19:01 +00:00
|
|
|
group_map = {}, -- units -> group IDs
|
2023-01-03 21:50:31 +00:00
|
|
|
prio_defs = { {}, {}, {}, {} }, -- priority definitions (each level is a table of units)
|
2023-02-07 05:32:50 +00:00
|
|
|
at_max_burn = false,
|
2023-01-03 21:50:31 +00:00
|
|
|
ascram = false,
|
|
|
|
ascram_reason = AUTO_SCRAM.NONE,
|
2023-02-15 03:55:40 +00:00
|
|
|
---@class ascram_status
|
2023-02-13 23:08:32 +00:00
|
|
|
ascram_status = {
|
|
|
|
matrix_dc = false,
|
|
|
|
matrix_fill = false,
|
|
|
|
crit_alarm = false,
|
2023-02-15 03:55:40 +00:00
|
|
|
radiation = false,
|
2023-02-13 23:08:32 +00:00
|
|
|
gen_fault = false
|
|
|
|
},
|
2023-01-03 21:50:31 +00:00
|
|
|
-- closed loop control
|
|
|
|
charge_conversion = 1.0,
|
|
|
|
time_start = 0.0,
|
|
|
|
initial_ramp = true,
|
|
|
|
waiting_on_ramp = false,
|
2023-02-08 04:47:58 +00:00
|
|
|
waiting_on_stable = false,
|
2023-01-03 21:50:31 +00:00
|
|
|
accumulator = 0.0,
|
2023-01-26 23:26:26 +00:00
|
|
|
saturated = false,
|
2023-02-08 04:47:58 +00:00
|
|
|
last_update = 0,
|
2023-01-03 21:50:31 +00:00
|
|
|
last_error = 0.0,
|
|
|
|
last_time = 0.0,
|
2023-07-08 20:57:13 +00:00
|
|
|
-- waste processing
|
|
|
|
waste_product = WASTE.PLUTONIUM,
|
|
|
|
current_waste_product = WASTE.PLUTONIUM,
|
|
|
|
pu_fallback = false,
|
2024-04-28 06:01:21 +00:00
|
|
|
sps_low_power = false,
|
|
|
|
disabled_sps = false,
|
2023-07-27 00:48:11 +00:00
|
|
|
-- alarm tones
|
2023-07-29 22:16:59 +00:00
|
|
|
tone_states = {},
|
|
|
|
test_tone_set = false,
|
|
|
|
test_tone_reset = false,
|
|
|
|
test_tone_states = {},
|
|
|
|
test_alarm_states = {},
|
2022-12-18 18:56:04 +00:00
|
|
|
-- statistics
|
|
|
|
im_stat_init = false,
|
2024-04-27 20:27:01 +00:00
|
|
|
avg_charge = util.mov_avg(3), -- 3 seconds
|
|
|
|
avg_inflow = util.mov_avg(6), -- 3 seconds
|
|
|
|
avg_outflow = util.mov_avg(6), -- 3 seconds
|
|
|
|
-- induction matrix charge delta stats
|
|
|
|
avg_net = util.mov_avg(60), -- 60 seconds
|
2024-04-28 17:08:16 +00:00
|
|
|
imtx_last_capacity = 0,
|
|
|
|
imtx_last_charge = 0,
|
2024-05-01 00:28:07 +00:00
|
|
|
imtx_last_charge_t = 0,
|
|
|
|
-- track faulted induction matrix update times to reject
|
|
|
|
imtx_faulted_times = { 0, 0, 0 }
|
2022-12-10 18:58:17 +00:00
|
|
|
}
|
|
|
|
|
2022-12-18 18:56:04 +00:00
|
|
|
-- create units
|
2024-04-12 04:13:05 +00:00
|
|
|
for i = 1, config.UnitCount do
|
|
|
|
table.insert(self.units, unit.new(i, cooling_conf.r_cool[i].BoilerCount, cooling_conf.r_cool[i].TurbineCount, config.ExtChargeIdling))
|
2023-06-18 19:19:01 +00:00
|
|
|
table.insert(self.group_map, 0)
|
2022-12-18 18:56:04 +00:00
|
|
|
end
|
|
|
|
|
2023-07-15 17:16:36 +00:00
|
|
|
-- list for RTU session management
|
|
|
|
self.rtu_list = { self.redstone, self.induction, self.sps, self.tanks, self.envd }
|
|
|
|
|
2022-12-10 18:58:17 +00:00
|
|
|
-- init redstone RTU I/O controller
|
2023-02-19 05:14:27 +00:00
|
|
|
self.io_ctl = rsctl.new(self.redstone)
|
2022-12-10 18:58:17 +00:00
|
|
|
|
2023-07-29 22:16:59 +00:00
|
|
|
-- fill blank alarm/tone states
|
|
|
|
for _ = 1, 12 do table.insert(self.test_alarm_states, false) end
|
|
|
|
for _ = 1, 8 do
|
|
|
|
table.insert(self.tone_states, false)
|
|
|
|
table.insert(self.test_tone_states, false)
|
|
|
|
end
|
|
|
|
|
2024-03-06 02:47:14 +00:00
|
|
|
-- PRIVATE FUNCTIONS --
|
|
|
|
|
2023-01-03 21:50:31 +00:00
|
|
|
-- check if all auto-controlled units completed ramping
|
2023-02-25 04:36:16 +00:00
|
|
|
---@nodiscard
|
2023-01-03 21:50:31 +00:00
|
|
|
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
|
2023-06-03 21:59:20 +00:00
|
|
|
all_ramped = all_ramped and units[u].auto_ramp_complete()
|
2023-01-03 21:50:31 +00:00
|
|
|
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
|
2023-02-11 19:27:29 +00:00
|
|
|
---@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)
|
2023-02-09 01:26:13 +00:00
|
|
|
local unallocated = math.floor(burn_rate * 100)
|
2023-01-03 21:50:31 +00:00
|
|
|
|
2023-02-11 19:27:29 +00:00
|
|
|
-- go through all priority groups
|
2023-02-02 02:55:02 +00:00
|
|
|
for i = 1, #self.prio_defs do
|
2023-01-03 21:50:31 +00:00
|
|
|
local units = self.prio_defs[i]
|
|
|
|
|
2023-02-02 02:55:02 +00:00
|
|
|
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
|
2023-02-07 05:32:50 +00:00
|
|
|
for id = 1, #units do
|
|
|
|
local u = units[id] ---@type reactor_unit
|
|
|
|
|
|
|
|
local ctl = u.get_control_inf()
|
2023-06-03 21:59:20 +00:00
|
|
|
local lim_br100 = u.auto_get_effective_limit()
|
2023-02-07 05:32:50 +00:00
|
|
|
|
2023-02-11 19:27:29 +00:00
|
|
|
if abort_on_fault and (lim_br100 ~= ctl.lim_br100) then
|
|
|
|
-- effective limit differs from set limit, unit is degraded
|
|
|
|
return unallocated, true
|
|
|
|
end
|
|
|
|
|
2023-02-09 01:26:13 +00:00
|
|
|
local last = ctl.br100
|
2023-02-02 02:55:02 +00:00
|
|
|
|
2023-02-09 01:26:13 +00:00
|
|
|
if splits[id] <= lim_br100 then
|
|
|
|
ctl.br100 = splits[id]
|
2023-02-02 02:55:02 +00:00
|
|
|
else
|
2023-02-09 01:26:13 +00:00
|
|
|
ctl.br100 = lim_br100
|
2023-02-02 02:55:02 +00:00
|
|
|
|
2023-02-07 05:32:50 +00:00
|
|
|
if id < #units then
|
|
|
|
local remaining = #units - id
|
2023-02-02 02:55:02 +00:00
|
|
|
split = math.floor(unallocated / remaining)
|
2023-02-07 05:32:50 +00:00
|
|
|
for x = (id + 1), #units do splits[x] = split end
|
2023-02-02 02:55:02 +00:00
|
|
|
splits[#units] = splits[#units] + (unallocated % remaining)
|
|
|
|
end
|
2023-01-03 21:50:31 +00:00
|
|
|
end
|
|
|
|
|
2023-02-09 01:26:13 +00:00
|
|
|
unallocated = math.max(0, unallocated - ctl.br100)
|
2023-01-03 21:50:31 +00:00
|
|
|
|
2023-06-03 21:59:20 +00:00
|
|
|
if last ~= ctl.br100 then u.auto_commit_br100(ramp) end
|
2023-02-02 02:55:02 +00:00
|
|
|
end
|
2023-01-03 21:50:31 +00:00
|
|
|
end
|
|
|
|
end
|
2023-02-07 05:32:50 +00:00
|
|
|
|
2023-02-11 19:27:29 +00:00
|
|
|
return unallocated, false
|
2023-01-03 21:50:31 +00:00
|
|
|
end
|
|
|
|
|
2024-04-10 02:23:43 +00:00
|
|
|
-- set idle state of all assigned reactors
|
|
|
|
---@param idle boolean idle state
|
|
|
|
local function _set_idling(idle)
|
|
|
|
for i = 1, #self.prio_defs do
|
|
|
|
for _, u in pairs(self.prio_defs[i]) do u.auto_set_idle(idle) end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-12-10 18:58:17 +00:00
|
|
|
-- PUBLIC FUNCTIONS --
|
|
|
|
|
|
|
|
---@class facility
|
|
|
|
local public = {}
|
|
|
|
|
2024-03-06 02:47:14 +00:00
|
|
|
--#region Add/Link Devices
|
2022-12-10 18:58:17 +00:00
|
|
|
|
|
|
|
-- link a redstone RTU session
|
|
|
|
---@param rs_unit unit_session
|
2023-11-12 16:54:47 +00:00
|
|
|
function public.add_redstone(rs_unit) table.insert(self.redstone, rs_unit) end
|
2022-12-10 18:58:17 +00:00
|
|
|
|
2023-07-06 05:36:06 +00:00
|
|
|
-- link an induction matrix RTU session
|
2022-12-10 18:58:17 +00:00
|
|
|
---@param imatrix unit_session
|
2023-07-15 17:16:36 +00:00
|
|
|
---@return boolean linked induction matrix accepted (max 1)
|
2022-12-10 18:58:17 +00:00
|
|
|
function public.add_imatrix(imatrix)
|
2023-07-15 17:16:36 +00:00
|
|
|
if #self.induction == 0 then
|
|
|
|
table.insert(self.induction, imatrix)
|
|
|
|
return true
|
|
|
|
else return false end
|
2022-12-10 18:58:17 +00:00
|
|
|
end
|
|
|
|
|
2023-07-08 20:57:13 +00:00
|
|
|
-- link an SPS RTU session
|
|
|
|
---@param sps unit_session
|
2023-07-15 17:16:36 +00:00
|
|
|
---@return boolean linked SPS accepted (max 1)
|
2023-07-08 20:57:13 +00:00
|
|
|
function public.add_sps(sps)
|
2023-07-15 17:16:36 +00:00
|
|
|
if #self.sps == 0 then
|
|
|
|
table.insert(self.sps, sps)
|
|
|
|
return true
|
|
|
|
else return false end
|
|
|
|
end
|
|
|
|
|
|
|
|
-- link a dynamic tank RTU session
|
|
|
|
---@param dynamic_tank unit_session
|
2023-11-12 16:54:47 +00:00
|
|
|
function public.add_tank(dynamic_tank) table.insert(self.tanks, dynamic_tank) end
|
2023-07-08 20:57:13 +00:00
|
|
|
|
2023-02-14 03:11:31 +00:00
|
|
|
-- link an environment detector RTU session
|
|
|
|
---@param envd unit_session
|
2023-11-12 16:54:47 +00:00
|
|
|
function public.add_envd(envd) table.insert(self.envd, envd) end
|
2023-02-14 03:11:31 +00:00
|
|
|
|
2022-12-10 18:58:17 +00:00
|
|
|
-- purge devices associated with the given RTU session ID
|
|
|
|
---@param session integer RTU session ID
|
|
|
|
function public.purge_rtu_devices(session)
|
2023-07-15 17:16:36 +00:00
|
|
|
for _, v in pairs(self.rtu_list) do util.filter_table(v, function (s) return s.get_session_id() ~= session end) end
|
2022-12-10 18:58:17 +00:00
|
|
|
end
|
|
|
|
|
2024-03-06 02:47:14 +00:00
|
|
|
--#endregion
|
2022-12-10 18:58:17 +00:00
|
|
|
|
2024-03-06 02:47:14 +00:00
|
|
|
--#region Update
|
2023-02-16 00:52:28 +00:00
|
|
|
|
2022-12-10 18:58:17 +00:00
|
|
|
-- update (iterate) the facility management
|
|
|
|
function public.update()
|
|
|
|
-- unlink RTU unit sessions if they are closed
|
2023-07-15 17:16:36 +00:00
|
|
|
for _, v in pairs(self.rtu_list) do util.filter_table(v, function (u) return u.is_connected() end) end
|
2023-01-03 21:50:31 +00:00
|
|
|
|
2023-07-29 22:16:59 +00:00
|
|
|
-- check if test routines are allowed right now
|
|
|
|
self.allow_testing = true
|
|
|
|
for i = 1, #self.units do
|
|
|
|
local u = self.units[i] ---@type reactor_unit
|
|
|
|
self.allow_testing = self.allow_testing and u.is_safe_idle()
|
|
|
|
end
|
|
|
|
|
2023-02-10 03:52:10 +00:00
|
|
|
-- current state for process control
|
2023-02-08 04:47:58 +00:00
|
|
|
local charge_update = 0
|
|
|
|
local rate_update = 0
|
2023-02-10 03:52:10 +00:00
|
|
|
|
|
|
|
-- calculate moving averages for induction matrix
|
2023-01-03 21:50:31 +00:00
|
|
|
if self.induction[1] ~= nil then
|
2024-04-20 20:32:18 +00:00
|
|
|
local matrix = self.induction[1] ---@type unit_session
|
|
|
|
local db = matrix.get_db() ---@type imatrix_session_db
|
2023-01-03 21:50:31 +00:00
|
|
|
|
2024-05-01 00:28:07 +00:00
|
|
|
local build_update = db.build.last_update
|
2023-02-08 04:47:58 +00:00
|
|
|
rate_update = db.state.last_update
|
2024-05-01 00:28:07 +00:00
|
|
|
charge_update = db.tanks.last_update
|
2023-02-08 04:47:58 +00:00
|
|
|
|
2024-05-01 00:28:07 +00:00
|
|
|
local has_data = build_update > 0 and rate_update > 0 and charge_update > 0
|
|
|
|
|
|
|
|
if matrix.is_faulted() then
|
|
|
|
-- a fault occured, cannot reliably update stats
|
|
|
|
has_data = false
|
|
|
|
self.im_stat_init = false
|
|
|
|
self.imtx_faulted_times = { build_update, rate_update, charge_update }
|
|
|
|
elseif not self.im_stat_init then
|
|
|
|
-- prevent operation with partially invalid data
|
|
|
|
-- all fields must have updated since the last fault
|
|
|
|
has_data = self.imtx_faulted_times[1] < build_update and
|
|
|
|
self.imtx_faulted_times[2] < rate_update and
|
|
|
|
self.imtx_faulted_times[3] < charge_update
|
|
|
|
end
|
|
|
|
|
|
|
|
if has_data then
|
2024-07-27 16:34:01 +00:00
|
|
|
local energy = util.joules_to_fe_rf(db.tanks.energy)
|
|
|
|
local input = util.joules_to_fe_rf(db.state.last_input)
|
|
|
|
local output = util.joules_to_fe_rf(db.state.last_output)
|
2024-04-27 20:27:01 +00:00
|
|
|
|
2023-01-03 21:50:31 +00:00
|
|
|
if self.im_stat_init then
|
2024-04-27 20:27:01 +00:00
|
|
|
self.avg_charge.record(energy, charge_update)
|
|
|
|
self.avg_inflow.record(input, rate_update)
|
|
|
|
self.avg_outflow.record(output, rate_update)
|
|
|
|
|
2024-04-28 17:08:16 +00:00
|
|
|
if charge_update ~= self.imtx_last_charge_t then
|
|
|
|
local delta = (energy - self.imtx_last_charge) / (charge_update - self.imtx_last_charge_t)
|
2024-04-27 20:27:01 +00:00
|
|
|
|
2024-04-28 17:08:16 +00:00
|
|
|
self.imtx_last_charge = energy
|
|
|
|
self.imtx_last_charge_t = charge_update
|
2024-04-27 20:27:01 +00:00
|
|
|
|
2024-04-28 05:26:44 +00:00
|
|
|
-- if the capacity changed, toss out existing data
|
2024-04-28 17:08:16 +00:00
|
|
|
if db.build.max_energy ~= self.imtx_last_capacity then
|
|
|
|
self.imtx_last_capacity = db.build.max_energy
|
2024-04-28 05:26:44 +00:00
|
|
|
self.avg_net.reset()
|
|
|
|
else
|
|
|
|
self.avg_net.record(delta, charge_update)
|
|
|
|
end
|
2024-04-27 20:27:01 +00:00
|
|
|
end
|
2023-01-03 21:50:31 +00:00
|
|
|
else
|
|
|
|
self.im_stat_init = true
|
2024-04-27 20:27:01 +00:00
|
|
|
|
|
|
|
self.avg_charge.reset(energy)
|
|
|
|
self.avg_inflow.reset(input)
|
|
|
|
self.avg_outflow.reset(output)
|
2024-04-30 22:54:01 +00:00
|
|
|
self.avg_net.reset()
|
2024-04-27 20:27:01 +00:00
|
|
|
|
2024-04-30 22:54:01 +00:00
|
|
|
self.imtx_last_capacity = db.build.max_energy
|
2024-04-28 17:08:16 +00:00
|
|
|
self.imtx_last_charge = energy
|
|
|
|
self.imtx_last_charge_t = charge_update
|
2023-01-03 21:50:31 +00:00
|
|
|
end
|
2024-05-01 00:28:07 +00:00
|
|
|
else
|
|
|
|
-- prevent use by control systems
|
|
|
|
rate_update = 0
|
|
|
|
charge_update = 0
|
2023-01-03 21:50:31 +00:00
|
|
|
end
|
|
|
|
else
|
|
|
|
self.im_stat_init = false
|
|
|
|
end
|
|
|
|
|
2023-02-04 18:47:00 +00:00
|
|
|
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
|
|
|
|
|
2023-01-03 21:50:31 +00:00
|
|
|
-------------------------
|
|
|
|
-- Run Process Control --
|
|
|
|
-------------------------
|
|
|
|
|
2024-03-06 02:47:14 +00:00
|
|
|
--#region
|
2023-07-08 20:57:13 +00:00
|
|
|
|
2024-04-10 02:23:43 +00:00
|
|
|
local avg_charge = self.avg_charge.compute()
|
|
|
|
local avg_inflow = self.avg_inflow.compute()
|
|
|
|
local avg_outflow = self.avg_outflow.compute()
|
2023-01-03 21:50:31 +00:00
|
|
|
|
2024-04-10 02:24:03 +00:00
|
|
|
local now = os.clock()
|
2023-01-03 21:50:31 +00:00
|
|
|
|
|
|
|
local state_changed = self.mode ~= self.last_mode
|
2023-02-07 05:32:50 +00:00
|
|
|
local next_mode = self.mode
|
2023-01-03 21:50:31 +00:00
|
|
|
|
|
|
|
-- once auto control is started, sort the priority sublists by limits
|
|
|
|
if state_changed then
|
2023-02-07 05:32:50 +00:00
|
|
|
self.saturated = false
|
|
|
|
|
2024-02-21 17:58:49 +00:00
|
|
|
log.debug(util.c("FAC: state changed from ", PROCESS_NAMES[self.last_mode + 1], " to ", PROCESS_NAMES[self.mode + 1]))
|
2023-02-07 05:32:50 +00:00
|
|
|
|
2023-02-11 19:27:29 +00:00
|
|
|
if (self.last_mode == PROCESS.INACTIVE) or (self.last_mode == PROCESS.GEN_RATE_FAULT_IDLE) then
|
2023-02-10 03:52:10 +00:00
|
|
|
self.start_fail = START_STATUS.OK
|
|
|
|
|
2023-02-15 03:55:40 +00:00
|
|
|
if (self.mode ~= PROCESS.MATRIX_FAULT_IDLE) and (self.mode ~= PROCESS.SYSTEM_ALARM_IDLE) then
|
2023-02-11 19:27:29 +00:00
|
|
|
-- auto clear ASCRAM
|
|
|
|
self.ascram = false
|
2023-02-19 17:20:16 +00:00
|
|
|
self.ascram_reason = AUTO_SCRAM.NONE
|
2023-02-11 19:27:29 +00:00
|
|
|
end
|
2023-02-07 05:32:50 +00:00
|
|
|
|
2023-02-10 03:52:10 +00:00
|
|
|
local blade_count = nil
|
2023-01-26 23:26:26 +00:00
|
|
|
self.max_burn_combined = 0.0
|
|
|
|
|
2023-01-03 21:50:31 +00:00
|
|
|
for i = 1, #self.prio_defs do
|
|
|
|
table.sort(self.prio_defs[i],
|
|
|
|
---@param a reactor_unit
|
|
|
|
---@param b reactor_unit
|
2023-02-09 01:26:13 +00:00
|
|
|
function (a, b) return a.get_control_inf().lim_br100 < b.get_control_inf().lim_br100 end
|
2023-01-03 21:50:31 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
for _, u in pairs(self.prio_defs[i]) do
|
2023-02-10 03:52:10 +00:00
|
|
|
local u_blade_count = u.get_control_inf().blade_count
|
|
|
|
|
|
|
|
if blade_count == nil then
|
|
|
|
blade_count = u_blade_count
|
2023-02-11 01:26:25 +00:00
|
|
|
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")
|
2023-02-10 03:52:10 +00:00
|
|
|
next_mode = PROCESS.INACTIVE
|
|
|
|
self.start_fail = START_STATUS.BLADE_MISMATCH
|
|
|
|
end
|
|
|
|
|
2023-06-03 21:59:20 +00:00
|
|
|
if self.start_fail == START_STATUS.OK then u.auto_engage() end
|
2023-02-10 03:52:10 +00:00
|
|
|
|
2023-02-09 01:26:13 +00:00
|
|
|
self.max_burn_combined = self.max_burn_combined + (u.get_control_inf().lim_br100 / 100.0)
|
2023-01-03 21:50:31 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2024-02-21 17:58:49 +00:00
|
|
|
log.debug(util.c("FAC: computed a max combined burn rate of ", self.max_burn_combined, "mB/t"))
|
|
|
|
|
2023-02-10 03:52:10 +00:00
|
|
|
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
|
2023-01-03 21:50:31 +00:00
|
|
|
elseif self.mode == PROCESS.INACTIVE then
|
|
|
|
for i = 1, #self.prio_defs do
|
2023-09-19 20:40:11 +00:00
|
|
|
-- disable reactors and disengage auto control
|
2023-01-03 21:50:31 +00:00
|
|
|
for _, u in pairs(self.prio_defs[i]) do
|
2023-09-19 20:37:15 +00:00
|
|
|
u.disable()
|
2024-04-10 02:23:43 +00:00
|
|
|
u.auto_set_idle(false)
|
2023-06-03 21:59:20 +00:00
|
|
|
u.auto_disengage()
|
2023-01-03 21:50:31 +00:00
|
|
|
end
|
|
|
|
end
|
2023-02-02 02:55:02 +00:00
|
|
|
|
2023-02-10 03:52:10 +00:00
|
|
|
log.info("FAC: disengaging auto control (now inactive)")
|
2023-01-03 21:50:31 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
self.initial_ramp = true
|
|
|
|
self.waiting_on_ramp = false
|
2023-02-08 04:47:58 +00:00
|
|
|
self.waiting_on_stable = false
|
2023-01-03 21:50:31 +00:00
|
|
|
else
|
|
|
|
self.initial_ramp = false
|
|
|
|
end
|
|
|
|
|
2023-02-07 05:32:50 +00:00
|
|
|
-- update unit ready state
|
2023-02-11 01:26:25 +00:00
|
|
|
local assign_count = 0
|
2023-02-07 05:32:50 +00:00
|
|
|
self.units_ready = true
|
|
|
|
for i = 1, #self.prio_defs do
|
|
|
|
for _, u in pairs(self.prio_defs[i]) do
|
2023-02-11 01:26:25 +00:00
|
|
|
assign_count = assign_count + 1
|
2023-02-07 05:32:50 +00:00
|
|
|
self.units_ready = self.units_ready and u.get_control_inf().ready
|
2023-02-03 03:58:51 +00:00
|
|
|
end
|
2023-02-07 05:32:50 +00:00
|
|
|
end
|
2023-02-03 03:58:51 +00:00
|
|
|
|
2023-02-07 05:32:50 +00:00
|
|
|
-- perform mode-specific operations
|
|
|
|
if self.mode == PROCESS.INACTIVE then
|
2023-02-10 03:52:10 +00:00
|
|
|
if not self.units_ready then
|
2023-02-03 03:58:51 +00:00
|
|
|
self.status_text = { "NOT READY", "assigned units not ready" }
|
2023-02-10 03:52:10 +00:00
|
|
|
else
|
2023-02-19 17:20:16 +00:00
|
|
|
-- clear ASCRAM once ready
|
|
|
|
self.ascram = false
|
|
|
|
self.ascram_reason = AUTO_SCRAM.NONE
|
|
|
|
|
|
|
|
if 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
|
2023-02-03 03:58:51 +00:00
|
|
|
end
|
2023-02-10 03:52:10 +00:00
|
|
|
elseif self.mode == PROCESS.MAX_BURN then
|
2023-02-07 05:32:50 +00:00
|
|
|
-- run units at their limits
|
2023-01-03 21:50:31 +00:00
|
|
|
if state_changed then
|
|
|
|
self.time_start = now
|
2023-02-07 05:32:50 +00:00
|
|
|
self.saturated = true
|
2023-02-10 03:52:10 +00:00
|
|
|
|
2023-02-07 05:32:50 +00:00
|
|
|
self.status_text = { "MONITORED MODE", "running reactors at limit" }
|
2024-02-21 17:58:49 +00:00
|
|
|
log.info("FAC: MAX_BURN process mode started")
|
2023-01-03 21:50:31 +00:00
|
|
|
end
|
2023-02-07 05:32:50 +00:00
|
|
|
|
|
|
|
_allocate_burn_rate(self.max_burn_combined, true)
|
2023-01-26 23:26:26 +00:00
|
|
|
elseif self.mode == PROCESS.BURN_RATE then
|
|
|
|
-- a total aggregate burn rate
|
|
|
|
if state_changed then
|
|
|
|
self.time_start = now
|
2023-02-02 02:55:02 +00:00
|
|
|
self.status_text = { "BURN RATE MODE", "running" }
|
2024-02-21 17:58:49 +00:00
|
|
|
log.info("FAC: BURN_RATE process mode started")
|
2023-01-26 23:26:26 +00:00
|
|
|
end
|
|
|
|
|
2023-02-10 03:52:10 +00:00
|
|
|
local unallocated = _allocate_burn_rate(self.burn_target, true)
|
|
|
|
self.saturated = self.burn_target == self.max_burn_combined or unallocated > 0
|
2023-01-03 21:50:31 +00:00
|
|
|
elseif self.mode == PROCESS.CHARGE then
|
|
|
|
-- target a level of charge
|
|
|
|
if state_changed then
|
2023-02-11 05:21:00 +00:00
|
|
|
self.time_start = now
|
2023-02-08 04:47:58 +00:00
|
|
|
self.last_time = now
|
2023-02-11 05:21:00 +00:00
|
|
|
self.last_error = 0
|
|
|
|
self.accumulator = 0
|
2023-01-03 21:50:31 +00:00
|
|
|
|
2024-04-10 02:23:43 +00:00
|
|
|
-- enabling idling on all assigned units
|
|
|
|
_set_idling(true)
|
|
|
|
|
2023-02-11 05:21:00 +00:00
|
|
|
self.status_text = { "CHARGE MODE", "running control loop" }
|
2024-02-21 17:58:49 +00:00
|
|
|
log.info("FAC: CHARGE mode starting PID control")
|
2024-05-01 00:28:07 +00:00
|
|
|
elseif self.last_update < charge_update then
|
2023-02-11 05:21:00 +00:00
|
|
|
-- convert to kFE to make constants not microscopic
|
|
|
|
local error = util.round((self.charge_setpoint - avg_charge) / 1000) / 1000
|
2023-01-03 21:50:31 +00:00
|
|
|
|
2023-02-11 05:21:00 +00:00
|
|
|
-- stop accumulator when saturated to avoid windup
|
|
|
|
if not self.saturated then
|
2023-02-08 04:47:58 +00:00
|
|
|
self.accumulator = self.accumulator + (error * (now - self.last_time))
|
2023-01-26 23:26:26 +00:00
|
|
|
end
|
2023-01-03 21:50:31 +00:00
|
|
|
|
2023-02-25 04:36:16 +00:00
|
|
|
-- local runtime = now - self.time_start
|
2023-01-26 23:26:26 +00:00
|
|
|
local integral = self.accumulator
|
2023-02-10 03:52:10 +00:00
|
|
|
local derivative = (error - self.last_error) / (now - self.last_time)
|
2023-01-03 21:50:31 +00:00
|
|
|
|
2024-04-10 02:23:43 +00:00
|
|
|
local P = charge_Kp * error
|
|
|
|
local I = charge_Ki * integral
|
|
|
|
local D = charge_Kd * derivative
|
2023-01-03 21:50:31 +00:00
|
|
|
|
2023-02-10 03:52:10 +00:00
|
|
|
local output = P + I + D
|
2023-01-26 23:26:26 +00:00
|
|
|
|
2023-02-10 03:52:10 +00:00
|
|
|
-- clamp at range -> output clamped (out_c)
|
|
|
|
local out_c = math.max(0, math.min(output, self.max_burn_combined))
|
2023-01-26 23:26:26 +00:00
|
|
|
|
2023-02-10 03:52:10 +00:00
|
|
|
self.saturated = output ~= out_c
|
2023-01-26 23:26:26 +00:00
|
|
|
|
2024-04-12 04:13:05 +00:00
|
|
|
if not config.ExtChargeIdling then
|
|
|
|
-- stop idling early if the output is zero, we are at or above the setpoint, and are not losing charge
|
|
|
|
_set_idling(not ((out_c == 0) and (error <= 0) and (avg_outflow <= 0)))
|
|
|
|
end
|
2024-04-10 02:23:43 +00:00
|
|
|
|
|
|
|
-- log.debug(util.sprintf("CHARGE[%f] { CHRG[%f] ERR[%f] INT[%f] => OUT[%f] OUT_C[%f] <= P[%f] I[%f] D[%f] }",
|
2023-02-25 04:36:16 +00:00
|
|
|
-- runtime, avg_charge, error, integral, output, out_c, P, I, D))
|
2023-01-03 21:50:31 +00:00
|
|
|
|
2023-02-11 05:21:00 +00:00
|
|
|
_allocate_burn_rate(out_c, true)
|
2023-01-03 21:50:31 +00:00
|
|
|
|
2023-02-08 04:47:58 +00:00
|
|
|
self.last_time = now
|
2023-02-11 05:21:00 +00:00
|
|
|
self.last_error = error
|
2023-01-03 21:50:31 +00:00
|
|
|
end
|
2023-02-10 03:52:10 +00:00
|
|
|
|
|
|
|
self.last_update = charge_update
|
2023-01-03 21:50:31 +00:00
|
|
|
elseif self.mode == PROCESS.GEN_RATE then
|
|
|
|
-- target a rate of generation
|
|
|
|
if state_changed then
|
2023-02-10 03:52:10 +00:00
|
|
|
-- estimate an initial output
|
|
|
|
local output = self.gen_rate_setpoint / self.charge_conversion
|
2023-01-03 21:50:31 +00:00
|
|
|
|
2023-02-10 03:52:10 +00:00
|
|
|
local unallocated = _allocate_burn_rate(output, true)
|
2023-01-03 21:50:31 +00:00
|
|
|
|
2023-02-10 03:52:10 +00:00
|
|
|
self.saturated = output >= self.max_burn_combined or unallocated > 0
|
2023-02-08 04:47:58 +00:00
|
|
|
self.waiting_on_ramp = true
|
|
|
|
|
|
|
|
self.status_text = { "GENERATION MODE", "starting up" }
|
2023-02-10 03:52:10 +00:00
|
|
|
log.info(util.c("FAC: GEN_RATE process mode initial ramp started (initial target is ", output, " mB/t)"))
|
2023-02-08 04:47:58 +00:00
|
|
|
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" }
|
2023-02-10 03:52:10 +00:00
|
|
|
log.info("FAC: GEN_RATE process mode initial ramp completed, holding for stablization time")
|
2023-02-08 04:47:58 +00:00
|
|
|
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" }
|
2023-02-10 03:52:10 +00:00
|
|
|
log.info("FAC: GEN_RATE process mode initial hold completed, starting PID control")
|
2023-02-08 04:47:58 +00:00
|
|
|
end
|
2024-05-01 00:28:07 +00:00
|
|
|
elseif self.last_update < rate_update then
|
2023-02-10 03:52:10 +00:00
|
|
|
-- convert to MFE (in rounded kFE) to make constants not microscopic
|
|
|
|
local error = util.round((self.gen_rate_setpoint - avg_inflow) / 1000) / 1000
|
|
|
|
|
2023-02-11 05:21:00 +00:00
|
|
|
-- stop accumulator when saturated to avoid windup
|
2023-01-26 23:26:26 +00:00
|
|
|
if not self.saturated then
|
2023-02-08 04:47:58 +00:00
|
|
|
self.accumulator = self.accumulator + (error * (now - self.last_time))
|
2023-01-26 23:26:26 +00:00
|
|
|
end
|
2023-01-03 21:50:31 +00:00
|
|
|
|
2023-02-25 04:36:16 +00:00
|
|
|
-- local runtime = now - self.time_start
|
2023-01-26 23:26:26 +00:00
|
|
|
local integral = self.accumulator
|
2023-02-08 04:47:58 +00:00
|
|
|
local derivative = (error - self.last_error) / (now - self.last_time)
|
2023-01-03 21:50:31 +00:00
|
|
|
|
2024-04-10 02:23:43 +00:00
|
|
|
local P = rate_Kp * error
|
|
|
|
local I = rate_Ki * integral
|
|
|
|
local D = rate_Kd * derivative
|
2023-01-03 21:50:31 +00:00
|
|
|
|
2023-02-08 04:47:58 +00:00
|
|
|
-- velocity (rate) (derivative of charge level => rate) feed forward
|
|
|
|
local FF = self.gen_rate_setpoint / self.charge_conversion
|
2023-01-03 21:50:31 +00:00
|
|
|
|
2023-02-10 03:52:10 +00:00
|
|
|
local output = P + I + D + FF
|
2023-01-03 21:50:31 +00:00
|
|
|
|
2023-02-08 04:47:58 +00:00
|
|
|
-- clamp at range -> output clamped (sp_c)
|
2023-02-09 01:26:13 +00:00
|
|
|
local out_c = math.max(0, math.min(output, self.max_burn_combined))
|
2023-01-03 21:50:31 +00:00
|
|
|
|
2023-02-09 01:26:13 +00:00
|
|
|
self.saturated = output ~= out_c
|
2023-01-26 23:26:26 +00:00
|
|
|
|
2023-02-25 04:36:16 +00:00
|
|
|
-- 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))
|
2023-02-08 04:47:58 +00:00
|
|
|
|
2023-02-15 03:55:40 +00:00
|
|
|
_allocate_burn_rate(out_c, false)
|
2023-02-08 04:47:58 +00:00
|
|
|
|
|
|
|
self.last_time = now
|
|
|
|
self.last_error = error
|
2023-01-03 21:50:31 +00:00
|
|
|
end
|
2023-02-08 04:47:58 +00:00
|
|
|
|
|
|
|
self.last_update = rate_update
|
2023-02-07 05:32:50 +00:00
|
|
|
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
|
2023-02-19 17:20:16 +00:00
|
|
|
next_mode = PROCESS.SYSTEM_ALARM_IDLE
|
2023-02-07 05:32:50 +00:00
|
|
|
log.info("FAC: exiting matrix fault idle state due to critical unit alarm")
|
|
|
|
end
|
2023-02-15 03:55:40 +00:00
|
|
|
elseif self.mode == PROCESS.SYSTEM_ALARM_IDLE then
|
2023-02-07 05:32:50 +00:00
|
|
|
-- do nothing, wait for user to confirm (stop and reset)
|
2023-02-11 19:27:29 +00:00
|
|
|
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
|
2023-02-02 02:55:02 +00:00
|
|
|
elseif self.mode ~= PROCESS.INACTIVE then
|
|
|
|
log.error(util.c("FAC: unsupported process mode ", self.mode, ", switching to inactive"))
|
2023-02-07 05:32:50 +00:00
|
|
|
next_mode = PROCESS.INACTIVE
|
2023-01-03 21:50:31 +00:00
|
|
|
end
|
|
|
|
|
2023-07-08 20:57:13 +00:00
|
|
|
--#endregion
|
|
|
|
|
2023-01-03 21:50:31 +00:00
|
|
|
------------------------------
|
|
|
|
-- Evaluate Automatic SCRAM --
|
|
|
|
------------------------------
|
|
|
|
|
2024-03-06 02:47:14 +00:00
|
|
|
--#region
|
2023-07-08 20:57:13 +00:00
|
|
|
|
2023-02-13 23:08:32 +00:00
|
|
|
local astatus = self.ascram_status
|
2023-02-11 19:27:29 +00:00
|
|
|
|
2023-02-19 17:20:16 +00:00
|
|
|
if self.induction[1] ~= nil then
|
2024-04-28 05:26:44 +00:00
|
|
|
local db = self.induction[1].get_db() ---@type imatrix_session_db
|
2023-02-07 05:32:50 +00:00
|
|
|
|
2023-02-19 17:20:16 +00:00
|
|
|
-- clear matrix disconnected
|
|
|
|
if astatus.matrix_dc then
|
|
|
|
astatus.matrix_dc = false
|
|
|
|
log.info("FAC: induction matrix reconnected, clearing ASCRAM condition")
|
|
|
|
end
|
2023-02-07 05:32:50 +00:00
|
|
|
|
2023-02-19 17:20:16 +00:00
|
|
|
-- check matrix fill too high
|
|
|
|
local was_fill = astatus.matrix_fill
|
2023-02-25 07:25:35 +00:00
|
|
|
astatus.matrix_fill = (db.tanks.energy_fill >= ALARM_LIMS.CHARGE_HIGH) or (astatus.matrix_fill and db.tanks.energy_fill > ALARM_LIMS.CHARGE_RE_ENABLE)
|
2023-02-07 05:32:50 +00:00
|
|
|
|
2023-02-19 17:20:16 +00:00
|
|
|
if was_fill and not astatus.matrix_fill then
|
2024-02-21 17:58:49 +00:00
|
|
|
log.info(util.c("FAC: charge state of induction matrix entered acceptable range <= ", ALARM_LIMS.CHARGE_RE_ENABLE * 100, "%"))
|
2023-02-19 17:20:16 +00:00
|
|
|
end
|
2023-02-15 03:55:40 +00:00
|
|
|
|
2023-02-19 17:20:16 +00:00
|
|
|
-- check for critical unit alarms
|
|
|
|
astatus.crit_alarm = false
|
|
|
|
for i = 1, #self.units do
|
|
|
|
local u = self.units[i] ---@type reactor_unit
|
2023-02-15 03:55:40 +00:00
|
|
|
|
2023-03-04 16:46:59 +00:00
|
|
|
if u.has_alarm_min_prio(PRIO.CRITICAL) then
|
2023-02-19 17:20:16 +00:00
|
|
|
astatus.crit_alarm = true
|
|
|
|
break
|
2023-02-15 03:55:40 +00:00
|
|
|
end
|
2023-02-19 17:20:16 +00:00
|
|
|
end
|
2023-02-15 03:55:40 +00:00
|
|
|
|
2023-02-19 17:20:16 +00:00
|
|
|
-- check for facility radiation
|
2023-11-12 16:54:47 +00:00
|
|
|
if #self.envd > 0 then
|
|
|
|
local max_rad = 0
|
|
|
|
|
|
|
|
for i = 1, #self.envd do
|
|
|
|
local envd = self.envd[i] ---@type unit_session
|
|
|
|
local e_db = envd.get_db() ---@type envd_session_db
|
|
|
|
if e_db.radiation_raw > max_rad then max_rad = e_db.radiation_raw end
|
|
|
|
end
|
2023-02-19 17:20:16 +00:00
|
|
|
|
2023-11-12 19:21:48 +00:00
|
|
|
astatus.radiation = max_rad >= ALARM_LIMS.FAC_HIGH_RAD
|
2023-01-03 21:50:31 +00:00
|
|
|
else
|
2023-02-19 17:20:16 +00:00
|
|
|
-- 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
|
2023-02-13 23:08:32 +00:00
|
|
|
end
|
2023-02-07 05:32:50 +00:00
|
|
|
|
2023-02-19 17:20:16 +00:00
|
|
|
-- 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
|
2023-02-07 05:32:50 +00:00
|
|
|
|
2023-02-19 17:20:16 +00:00
|
|
|
if (self.mode ~= PROCESS.INACTIVE) and (self.mode ~= PROCESS.SYSTEM_ALARM_IDLE) then
|
2023-02-13 23:08:32 +00:00
|
|
|
local scram = astatus.matrix_dc or astatus.matrix_fill or astatus.crit_alarm or astatus.gen_fault
|
2023-01-03 21:50:31 +00:00
|
|
|
|
2023-02-13 23:08:32 +00:00
|
|
|
if scram and not self.ascram then
|
|
|
|
-- SCRAM all units
|
2023-01-03 21:50:31 +00:00
|
|
|
for i = 1, #self.prio_defs do
|
|
|
|
for _, u in pairs(self.prio_defs[i]) do
|
2023-06-03 21:59:20 +00:00
|
|
|
u.auto_scram()
|
2023-01-03 21:50:31 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2023-02-13 23:08:32 +00:00
|
|
|
if astatus.crit_alarm then
|
|
|
|
-- highest priority alarm
|
2023-02-15 03:55:40 +00:00
|
|
|
next_mode = PROCESS.SYSTEM_ALARM_IDLE
|
2023-02-13 23:08:32 +00:00
|
|
|
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")
|
2023-02-19 17:20:16 +00:00
|
|
|
log.warning("FAC: emergency exit of process control due to critical unit alarm")
|
2023-02-15 03:55:40 +00:00
|
|
|
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")
|
2023-02-13 23:08:32 +00:00
|
|
|
elseif astatus.matrix_dc then
|
|
|
|
next_mode = PROCESS.MATRIX_FAULT_IDLE
|
|
|
|
self.ascram_reason = AUTO_SCRAM.MATRIX_DC
|
2023-02-07 05:32:50 +00:00
|
|
|
self.status_text = { "AUTOMATIC SCRAM", "induction matrix disconnected" }
|
2023-02-13 23:08:32 +00:00
|
|
|
|
|
|
|
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
|
2023-02-07 05:32:50 +00:00
|
|
|
self.status_text = { "AUTOMATIC SCRAM", "induction matrix fill high" }
|
2023-02-13 23:08:32 +00:00
|
|
|
|
|
|
|
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
|
2023-02-11 19:27:29 +00:00
|
|
|
self.status_text = { "GENERATION MODE IDLE", "paused: system not ready" }
|
2023-02-13 23:08:32 +00:00
|
|
|
|
|
|
|
log.info("FAC: automatic SCRAM due to unit problem while in GEN_RATE mode, will resume once all units are ready")
|
2023-02-07 05:32:50 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
self.ascram = scram
|
|
|
|
|
|
|
|
if not self.ascram then
|
|
|
|
self.ascram_reason = AUTO_SCRAM.NONE
|
|
|
|
|
2023-02-13 23:08:32 +00:00
|
|
|
-- reset PLC RPS trips if we should
|
2023-02-07 05:32:50 +00:00
|
|
|
for i = 1, #self.units do
|
|
|
|
local u = self.units[i] ---@type reactor_unit
|
2023-06-03 21:59:20 +00:00
|
|
|
u.auto_cond_rps_reset()
|
2023-02-07 05:32:50 +00:00
|
|
|
end
|
2023-01-03 21:50:31 +00:00
|
|
|
end
|
|
|
|
end
|
2023-02-02 02:55:02 +00:00
|
|
|
|
2023-07-08 20:57:13 +00:00
|
|
|
--#endregion
|
|
|
|
|
2023-02-07 05:32:50 +00:00
|
|
|
-- update last mode and set next mode
|
2023-02-02 02:55:02 +00:00
|
|
|
self.last_mode = self.mode
|
2023-02-07 05:32:50 +00:00
|
|
|
self.mode = next_mode
|
2023-02-19 05:14:27 +00:00
|
|
|
|
|
|
|
-------------------------
|
|
|
|
-- Handle Redstone I/O --
|
|
|
|
-------------------------
|
|
|
|
|
2024-03-06 02:47:14 +00:00
|
|
|
--#region
|
|
|
|
|
2023-02-19 05:14:27 +00:00
|
|
|
if #self.redstone > 0 then
|
|
|
|
-- handle facility SCRAM
|
|
|
|
if self.io_ctl.digital_read(IO.F_SCRAM) then
|
|
|
|
for i = 1, #self.units do
|
|
|
|
local u = self.units[i] ---@type reactor_unit
|
|
|
|
u.cond_scram()
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
-- handle facility ack
|
|
|
|
if self.io_ctl.digital_read(IO.F_ACK) then public.ack_all() end
|
|
|
|
|
2023-10-04 21:26:07 +00:00
|
|
|
-- update facility alarm outputs
|
|
|
|
local has_prio_alarm, has_any_alarm = false, false
|
2023-02-19 05:14:27 +00:00
|
|
|
for i = 1, #self.units do
|
2023-10-04 21:26:07 +00:00
|
|
|
local u = self.units[i] ---@type reactor_unit
|
2023-02-19 05:14:27 +00:00
|
|
|
|
2023-03-04 16:46:59 +00:00
|
|
|
if u.has_alarm_min_prio(PRIO.EMERGENCY) then
|
2023-10-04 21:26:07 +00:00
|
|
|
has_prio_alarm, has_any_alarm = true, true
|
2023-03-04 18:38:41 +00:00
|
|
|
break
|
2023-10-04 21:26:07 +00:00
|
|
|
elseif u.has_alarm_min_prio(PRIO.TIMELY) then
|
|
|
|
has_any_alarm = true
|
2023-02-19 05:14:27 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2023-10-04 21:26:07 +00:00
|
|
|
self.io_ctl.digital_write(IO.F_ALARM, has_prio_alarm)
|
|
|
|
self.io_ctl.digital_write(IO.F_ALARM_ANY, has_any_alarm)
|
2024-04-20 20:32:18 +00:00
|
|
|
|
|
|
|
-- update induction matrix related outputs
|
|
|
|
if self.induction[1] ~= nil then
|
2024-04-28 05:26:44 +00:00
|
|
|
local db = self.induction[1].get_db() ---@type imatrix_session_db
|
2024-04-20 20:32:18 +00:00
|
|
|
|
|
|
|
self.io_ctl.digital_write(IO.F_MATRIX_LOW, db.tanks.energy_fill < const.RS_THRESHOLDS.IMATRIX_CHARGE_LOW)
|
|
|
|
self.io_ctl.digital_write(IO.F_MATRIX_HIGH, db.tanks.energy_fill > const.RS_THRESHOLDS.IMATRIX_CHARGE_HIGH)
|
|
|
|
self.io_ctl.analog_write(IO.F_MATRIX_CHG, db.tanks.energy_fill, 0, 1)
|
|
|
|
end
|
2023-02-19 05:14:27 +00:00
|
|
|
end
|
2023-07-08 20:57:13 +00:00
|
|
|
|
2024-03-06 02:47:14 +00:00
|
|
|
--#endregion
|
|
|
|
|
2023-08-21 02:56:51 +00:00
|
|
|
----------------
|
|
|
|
-- Unit Tasks --
|
|
|
|
----------------
|
2023-07-08 20:57:13 +00:00
|
|
|
|
2024-03-06 02:47:14 +00:00
|
|
|
--#region
|
|
|
|
|
2023-07-08 20:57:13 +00:00
|
|
|
local insufficent_po_rate = false
|
2023-08-21 02:56:51 +00:00
|
|
|
local need_emcool = false
|
|
|
|
|
2023-07-08 20:57:13 +00:00
|
|
|
for i = 1, #self.units do
|
|
|
|
local u = self.units[i] ---@type reactor_unit
|
2023-08-21 02:56:51 +00:00
|
|
|
|
|
|
|
-- update auto waste processing
|
2023-07-08 20:57:13 +00:00
|
|
|
if u.get_control_inf().waste_mode == WASTE_MODE.AUTO then
|
|
|
|
if (u.get_sna_rate() * 10.0) < u.get_burn_rate() then
|
|
|
|
insufficent_po_rate = true
|
|
|
|
end
|
|
|
|
end
|
2023-08-21 02:56:51 +00:00
|
|
|
|
|
|
|
-- check if unit activated emergency coolant & uses facility tanks
|
|
|
|
if (cooling_conf.fac_tank_mode > 0) and u.is_emer_cool_tripped() and (cooling_conf.fac_tank_defs[i] == 2) then
|
|
|
|
need_emcool = true
|
|
|
|
end
|
2023-07-08 20:57:13 +00:00
|
|
|
end
|
|
|
|
|
2023-08-21 02:56:51 +00:00
|
|
|
-- update waste product
|
2024-04-28 06:01:21 +00:00
|
|
|
|
|
|
|
self.current_waste_product = self.waste_product
|
|
|
|
|
|
|
|
if (not self.sps_low_power) and (self.waste_product == WASTE.ANTI_MATTER) and (self.induction[1] ~= nil) then
|
|
|
|
local db = self.induction[1].get_db() ---@type imatrix_session_db
|
|
|
|
|
|
|
|
if db.tanks.energy_fill >= 0.15 then
|
|
|
|
self.disabled_sps = false
|
|
|
|
elseif self.disabled_sps or ((db.tanks.last_update > 0) and (db.tanks.energy_fill < 0.1)) then
|
|
|
|
self.disabled_sps = true
|
|
|
|
self.current_waste_product = WASTE.POLONIUM
|
|
|
|
end
|
|
|
|
else
|
|
|
|
self.disabled_sps = false
|
|
|
|
end
|
|
|
|
|
|
|
|
if self.pu_fallback and insufficent_po_rate then
|
2023-07-08 20:57:13 +00:00
|
|
|
self.current_waste_product = WASTE.PLUTONIUM
|
2024-04-28 06:01:21 +00:00
|
|
|
end
|
2023-08-21 02:56:51 +00:00
|
|
|
|
|
|
|
-- make sure dynamic tanks are allowing outflow if required
|
|
|
|
-- set all, rather than trying to determine which is for which (simpler & safer)
|
|
|
|
-- there should be no need for any to be in fill only mode
|
|
|
|
if need_emcool then
|
|
|
|
for i = 1, #self.tanks do
|
|
|
|
local session = self.tanks[i] ---@type unit_session
|
|
|
|
local tank = session.get_db() ---@type dynamicv_session_db
|
|
|
|
|
|
|
|
if tank.state.container_mode == CONTAINER_MODE.FILL then
|
|
|
|
session.get_cmd_queue().push_data(DTV_RTU_S_DATA.SET_CONT_MODE, CONTAINER_MODE.BOTH)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2023-08-21 03:43:07 +00:00
|
|
|
|
2024-03-06 02:47:14 +00:00
|
|
|
--#endregion
|
|
|
|
|
2023-07-27 00:48:11 +00:00
|
|
|
------------------------
|
|
|
|
-- Update Alarm Tones --
|
|
|
|
------------------------
|
|
|
|
|
2024-03-06 02:47:14 +00:00
|
|
|
--#region
|
|
|
|
|
2023-07-29 22:16:59 +00:00
|
|
|
local allow_test = self.allow_testing and self.test_tone_set
|
2023-07-27 00:48:11 +00:00
|
|
|
|
2023-07-29 22:16:59 +00:00
|
|
|
local alarms = { false, false, false, false, false, false, false, false, false, false, false, false }
|
|
|
|
|
2023-07-30 04:13:26 +00:00
|
|
|
-- reset tone states before re-evaluting
|
|
|
|
for i = 1, #self.tone_states do self.tone_states[i] = false end
|
2023-07-29 22:16:59 +00:00
|
|
|
|
|
|
|
if allow_test then
|
|
|
|
alarms = self.test_alarm_states
|
|
|
|
else
|
|
|
|
-- check all alarms for all units
|
|
|
|
for i = 1, #self.units do
|
|
|
|
local u = self.units[i] ---@type reactor_unit
|
|
|
|
for id, alarm in pairs(u.get_alarms()) do
|
|
|
|
alarms[id] = alarms[id] or (alarm == ALARM_STATE.TRIPPED)
|
|
|
|
end
|
2023-07-27 00:48:11 +00:00
|
|
|
end
|
2023-07-29 22:16:59 +00:00
|
|
|
|
2023-07-30 04:13:26 +00:00
|
|
|
if not self.test_tone_reset then
|
|
|
|
-- clear testing alarms if we aren't using them
|
|
|
|
for i = 1, #self.test_alarm_states do self.test_alarm_states[i] = false end
|
|
|
|
end
|
2023-07-29 22:16:59 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
-- Evaluate Alarms --
|
|
|
|
|
2023-07-27 00:48:11 +00:00
|
|
|
-- containment breach is worst case CRITICAL alarm, this takes priority
|
|
|
|
if alarms[ALARM.ContainmentBreach] then
|
2023-07-30 04:13:26 +00:00
|
|
|
self.tone_states[TONE.T_1800Hz_Int_4Hz] = true
|
2023-07-27 00:48:11 +00:00
|
|
|
else
|
|
|
|
-- critical damage is highest priority CRITICAL level alarm
|
|
|
|
if alarms[ALARM.CriticalDamage] then
|
2023-07-30 04:13:26 +00:00
|
|
|
self.tone_states[TONE.T_660Hz_Int_125ms] = true
|
2023-07-27 00:48:11 +00:00
|
|
|
else
|
|
|
|
-- EMERGENCY level alarms + URGENT over temp
|
|
|
|
if alarms[ALARM.ReactorDamage] or alarms[ALARM.ReactorOverTemp] or alarms[ALARM.ReactorWasteLeak] then
|
2023-07-30 04:13:26 +00:00
|
|
|
self.tone_states[TONE.T_544Hz_440Hz_Alt] = true
|
2023-07-27 00:48:11 +00:00
|
|
|
-- URGENT level turbine trip
|
|
|
|
elseif alarms[ALARM.TurbineTrip] then
|
2023-07-30 04:13:26 +00:00
|
|
|
self.tone_states[TONE.T_745Hz_Int_1Hz] = true
|
2023-07-27 00:48:11 +00:00
|
|
|
-- URGENT level reactor lost
|
|
|
|
elseif alarms[ALARM.ReactorLost] then
|
2023-07-30 04:13:26 +00:00
|
|
|
self.tone_states[TONE.T_340Hz_Int_2Hz] = true
|
2023-07-27 00:48:11 +00:00
|
|
|
-- TIMELY level alarms
|
|
|
|
elseif alarms[ALARM.ReactorHighTemp] or alarms[ALARM.ReactorHighWaste] or alarms[ALARM.RCSTransient] then
|
2023-07-30 04:13:26 +00:00
|
|
|
self.tone_states[TONE.T_800Hz_Int] = true
|
2023-07-27 00:48:11 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
-- check RPS transient URGENT level alarm
|
|
|
|
if alarms[ALARM.RPSTransient] then
|
2023-07-30 04:13:26 +00:00
|
|
|
self.tone_states[TONE.T_1000Hz_Int] = true
|
2023-07-27 00:48:11 +00:00
|
|
|
-- disable really painful audio combination
|
2023-07-30 04:13:26 +00:00
|
|
|
self.tone_states[TONE.T_340Hz_Int_2Hz] = false
|
2023-07-27 00:48:11 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
-- radiation is a big concern, always play this CRITICAL level alarm if active
|
|
|
|
if alarms[ALARM.ContainmentRadiation] then
|
2023-07-30 04:13:26 +00:00
|
|
|
self.tone_states[TONE.T_800Hz_1000Hz_Alt] = true
|
2023-07-27 00:48:11 +00:00
|
|
|
-- we are going to disable the RPS trip alarm audio due to conflict, and if it was enabled
|
|
|
|
-- then we can re-enable the reactor lost alarm audio since it doesn't painfully combine with this one
|
2023-07-30 04:13:26 +00:00
|
|
|
if self.tone_states[TONE.T_1000Hz_Int] and alarms[ALARM.ReactorLost] then self.tone_states[TONE.T_340Hz_Int_2Hz] = true end
|
2023-07-27 00:48:11 +00:00
|
|
|
-- it sounds *really* bad if this is in conjunction with these other tones, so disable them
|
2023-07-30 04:13:26 +00:00
|
|
|
self.tone_states[TONE.T_745Hz_Int_1Hz] = false
|
|
|
|
self.tone_states[TONE.T_800Hz_Int] = false
|
|
|
|
self.tone_states[TONE.T_1000Hz_Int] = false
|
2023-07-27 00:48:11 +00:00
|
|
|
end
|
2023-07-29 22:16:59 +00:00
|
|
|
|
|
|
|
-- add to tone states if testing is active
|
|
|
|
if allow_test then
|
|
|
|
for i = 1, #self.tone_states do
|
|
|
|
self.tone_states[i] = self.tone_states[i] or self.test_tone_states[i]
|
|
|
|
end
|
2023-07-30 04:13:26 +00:00
|
|
|
|
|
|
|
self.test_tone_reset = false
|
|
|
|
else
|
|
|
|
if not self.test_tone_reset then
|
|
|
|
-- clear testing tones if we aren't using them
|
|
|
|
for i = 1, #self.test_tone_states do self.test_tone_states[i] = false end
|
|
|
|
end
|
|
|
|
|
|
|
|
-- flag that tones were reset
|
|
|
|
self.test_tone_set = false
|
|
|
|
self.test_tone_reset = true
|
2023-07-29 22:16:59 +00:00
|
|
|
end
|
2024-03-06 02:47:14 +00:00
|
|
|
|
|
|
|
--#endregion
|
2022-12-10 18:58:17 +00:00
|
|
|
end
|
|
|
|
|
2023-07-08 20:57:13 +00:00
|
|
|
-- call the update function of all units in the facility<br>
|
|
|
|
-- additionally sets the requested auto waste mode if applicable
|
2022-12-18 18:56:04 +00:00
|
|
|
function public.update_units()
|
|
|
|
for i = 1, #self.units do
|
|
|
|
local u = self.units[i] ---@type reactor_unit
|
2023-07-08 20:57:13 +00:00
|
|
|
u.auto_set_waste(self.current_waste_product)
|
2022-12-18 18:56:04 +00:00
|
|
|
u.update()
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2024-03-06 02:47:14 +00:00
|
|
|
--#endregion
|
|
|
|
|
|
|
|
--#region Commands
|
2023-01-26 23:26:26 +00:00
|
|
|
|
|
|
|
-- 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
|
|
|
|
|
2023-02-07 23:44:34 +00:00
|
|
|
-- 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
|
|
|
|
|
2023-01-26 23:26:26 +00:00
|
|
|
-- stop auto control
|
2023-07-08 20:57:13 +00:00
|
|
|
function public.auto_stop() self.mode = PROCESS.INACTIVE end
|
2023-01-26 23:26:26 +00:00
|
|
|
|
|
|
|
-- set automatic control configuration and start the process
|
2024-04-12 04:13:05 +00:00
|
|
|
---@param auto_cfg coord_auto_config configuration
|
2023-01-26 23:26:26 +00:00
|
|
|
---@return table response ready state (successfully started) and current configuration (after updating)
|
2024-04-12 04:13:05 +00:00
|
|
|
function public.auto_start(auto_cfg)
|
2023-07-08 20:57:13 +00:00
|
|
|
local charge_scaler = 1000000 -- convert MFE to FE
|
|
|
|
local gen_scaler = 1000 -- convert kFE to FE
|
|
|
|
local ready = false
|
2023-01-26 23:26:26 +00:00
|
|
|
|
|
|
|
-- load up current limits
|
|
|
|
local limits = {}
|
2024-04-12 04:13:05 +00:00
|
|
|
for i = 1, config.UnitCount do
|
2023-01-26 23:26:26 +00:00
|
|
|
local u = self.units[i] ---@type reactor_unit
|
2023-02-09 01:26:13 +00:00
|
|
|
limits[i] = u.get_control_inf().lim_br100 * 100
|
2023-01-26 23:26:26 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
-- only allow changes if not running
|
|
|
|
if self.mode == PROCESS.INACTIVE then
|
2024-04-12 04:13:05 +00:00
|
|
|
if (type(auto_cfg.mode) == "number") and (auto_cfg.mode > PROCESS.INACTIVE) and (auto_cfg.mode <= PROCESS.GEN_RATE) then
|
|
|
|
self.mode_set = auto_cfg.mode
|
2023-01-26 23:26:26 +00:00
|
|
|
end
|
|
|
|
|
2024-04-12 04:13:05 +00:00
|
|
|
if (type(auto_cfg.burn_target) == "number") and auto_cfg.burn_target >= 0.1 then
|
|
|
|
self.burn_target = auto_cfg.burn_target
|
2023-01-26 23:26:26 +00:00
|
|
|
end
|
|
|
|
|
2024-04-12 04:13:05 +00:00
|
|
|
if (type(auto_cfg.charge_target) == "number") and auto_cfg.charge_target >= 0 then
|
|
|
|
self.charge_setpoint = auto_cfg.charge_target * charge_scaler
|
2023-01-26 23:26:26 +00:00
|
|
|
end
|
|
|
|
|
2024-04-12 04:13:05 +00:00
|
|
|
if (type(auto_cfg.gen_target) == "number") and auto_cfg.gen_target >= 0 then
|
|
|
|
self.gen_rate_setpoint = auto_cfg.gen_target * gen_scaler
|
2023-01-26 23:26:26 +00:00
|
|
|
end
|
|
|
|
|
2024-04-12 04:13:05 +00:00
|
|
|
if (type(auto_cfg.limits) == "table") and (#auto_cfg.limits == config.UnitCount) then
|
|
|
|
for i = 1, config.UnitCount do
|
|
|
|
local limit = auto_cfg.limits[i]
|
2023-01-26 23:26:26 +00:00
|
|
|
|
|
|
|
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
|
|
|
|
|
2023-07-15 17:16:36 +00:00
|
|
|
if (self.mode_set == PROCESS.CHARGE) and (self.charge_setpoint <= 0) or
|
|
|
|
(self.mode_set == PROCESS.GEN_RATE) and (self.gen_rate_setpoint <= 0) or
|
|
|
|
(self.mode_set == PROCESS.BURN_RATE) and (self.burn_target < 0.1) then
|
2023-01-26 23:26:26 +00:00
|
|
|
ready = false
|
|
|
|
end
|
|
|
|
|
2023-02-03 03:58:51 +00:00
|
|
|
ready = ready and self.units_ready
|
|
|
|
|
2023-01-26 23:26:26 +00:00
|
|
|
if ready then self.mode = self.mode_set end
|
|
|
|
end
|
|
|
|
|
2023-07-08 20:57:13 +00:00
|
|
|
return {
|
|
|
|
ready,
|
|
|
|
self.mode_set,
|
|
|
|
self.burn_target,
|
|
|
|
self.charge_setpoint / charge_scaler,
|
|
|
|
self.gen_rate_setpoint / gen_scaler,
|
|
|
|
limits
|
|
|
|
}
|
2023-01-26 23:26:26 +00:00
|
|
|
end
|
|
|
|
|
2024-03-06 02:47:14 +00:00
|
|
|
--#endregion
|
|
|
|
|
|
|
|
--#region Settings
|
2023-01-03 21:50:31 +00:00
|
|
|
|
|
|
|
-- 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)
|
2024-04-12 04:13:05 +00:00
|
|
|
if (group >= 0 and group <= 4) and (unit_id > 0 and unit_id <= config.UnitCount) and self.mode == PROCESS.INACTIVE then
|
2023-01-03 21:50:31 +00:00
|
|
|
-- 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
|
|
|
|
|
2023-02-02 02:55:02 +00:00
|
|
|
self.group_map[unit_id] = group
|
2023-01-03 21:50:31 +00:00
|
|
|
|
|
|
|
-- add to group if not independent
|
|
|
|
if group > 0 then
|
|
|
|
table.insert(self.prio_defs[group], self.units[unit_id])
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2023-07-08 20:57:13 +00:00
|
|
|
-- set waste production
|
|
|
|
---@param product WASTE_PRODUCT target product
|
|
|
|
---@return WASTE_PRODUCT product newly set value, if valid
|
|
|
|
function public.set_waste_product(product)
|
|
|
|
if product == WASTE.PLUTONIUM or product == WASTE.POLONIUM or product == WASTE.ANTI_MATTER then
|
|
|
|
self.waste_product = product
|
|
|
|
end
|
|
|
|
|
|
|
|
return self.waste_product
|
|
|
|
end
|
|
|
|
|
|
|
|
-- enable/disable plutonium fallback
|
|
|
|
---@param enabled boolean requested state
|
|
|
|
---@return boolean enabled newly set value
|
|
|
|
function public.set_pu_fallback(enabled)
|
|
|
|
self.pu_fallback = enabled == true
|
|
|
|
return self.pu_fallback
|
|
|
|
end
|
|
|
|
|
2024-04-28 06:01:21 +00:00
|
|
|
-- enable/disable SPS at low power
|
|
|
|
---@param enabled boolean requested state
|
|
|
|
---@return boolean enabled newly set value
|
|
|
|
function public.set_sps_low_power(enabled)
|
|
|
|
self.sps_low_power = enabled == true
|
|
|
|
return self.sps_low_power
|
|
|
|
end
|
|
|
|
|
2024-03-06 02:47:14 +00:00
|
|
|
--#endregion
|
|
|
|
|
|
|
|
--#region Diagnostic Testing
|
2023-07-29 22:16:59 +00:00
|
|
|
|
|
|
|
-- attempt to set a test tone state
|
2023-07-30 04:13:26 +00:00
|
|
|
---@param id TONE|0 tone ID or 0 to disable all
|
2023-07-29 22:16:59 +00:00
|
|
|
---@param state boolean state
|
|
|
|
---@return boolean allow_testing, table test_tone_states
|
|
|
|
function public.diag_set_test_tone(id, state)
|
|
|
|
if self.allow_testing then
|
|
|
|
self.test_tone_set = true
|
|
|
|
self.test_tone_reset = false
|
|
|
|
|
|
|
|
if id == 0 then
|
|
|
|
for i = 1, #self.test_tone_states do self.test_tone_states[i] = false end
|
|
|
|
else
|
|
|
|
self.test_tone_states[id] = state
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
return self.allow_testing, self.test_tone_states
|
|
|
|
end
|
|
|
|
|
|
|
|
-- attempt to set a test alarm state
|
|
|
|
---@param id ALARM|0 alarm ID or 0 to disable all
|
|
|
|
---@param state boolean state
|
|
|
|
---@return boolean allow_testing, table test_alarm_states
|
|
|
|
function public.diag_set_test_alarm(id, state)
|
|
|
|
if self.allow_testing then
|
|
|
|
self.test_tone_set = true
|
|
|
|
self.test_tone_reset = false
|
|
|
|
|
|
|
|
if id == 0 then
|
|
|
|
for i = 1, #self.test_alarm_states do self.test_alarm_states[i] = false end
|
|
|
|
else
|
|
|
|
self.test_alarm_states[id] = state
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
return self.allow_testing, self.test_alarm_states
|
|
|
|
end
|
|
|
|
|
2024-03-06 02:47:14 +00:00
|
|
|
--#endregion
|
|
|
|
|
|
|
|
--#region Read States/Properties
|
2022-12-10 18:58:17 +00:00
|
|
|
|
2023-07-27 00:48:11 +00:00
|
|
|
-- get current alarm tone on/off states
|
|
|
|
---@nodiscard
|
|
|
|
function public.get_alarm_tones() return self.tone_states end
|
|
|
|
|
2023-07-08 20:57:13 +00:00
|
|
|
-- get build properties of all facility devices
|
2023-02-25 04:36:16 +00:00
|
|
|
---@nodiscard
|
2023-07-08 20:57:13 +00:00
|
|
|
---@param type RTU_UNIT_TYPE? type or nil to include only a particular unit type, or to include all if nil
|
|
|
|
function public.get_build(type)
|
|
|
|
local all = type == nil
|
2022-12-10 18:58:17 +00:00
|
|
|
local build = {}
|
|
|
|
|
2023-07-08 20:57:13 +00:00
|
|
|
if all or type == RTU_UNIT_TYPE.IMATRIX then
|
2023-02-20 05:49:37 +00:00
|
|
|
build.induction = {}
|
|
|
|
for i = 1, #self.induction do
|
|
|
|
local matrix = self.induction[i] ---@type unit_session
|
2023-11-06 15:21:42 +00:00
|
|
|
build.induction[i] = { matrix.get_db().formed, matrix.get_db().build }
|
2023-02-20 05:49:37 +00:00
|
|
|
end
|
2022-12-10 18:58:17 +00:00
|
|
|
end
|
|
|
|
|
2023-07-08 20:57:13 +00:00
|
|
|
if all or type == RTU_UNIT_TYPE.SPS then
|
|
|
|
build.sps = {}
|
|
|
|
for i = 1, #self.sps do
|
2023-11-06 15:21:42 +00:00
|
|
|
local sps = self.sps[i] ---@type unit_session
|
|
|
|
build.sps[i] = { sps.get_db().formed, sps.get_db().build }
|
2023-07-08 20:57:13 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2023-07-15 17:16:36 +00:00
|
|
|
if all or type == RTU_UNIT_TYPE.DYNAMIC_VALVE then
|
|
|
|
build.tanks = {}
|
|
|
|
for i = 1, #self.tanks do
|
2023-11-06 15:21:42 +00:00
|
|
|
local tank = self.tanks[i] ---@type unit_session
|
2023-07-15 17:16:36 +00:00
|
|
|
build.tanks[tank.get_device_idx()] = { tank.get_db().formed, tank.get_db().build }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-12-10 18:58:17 +00:00
|
|
|
return build
|
|
|
|
end
|
|
|
|
|
2023-01-26 23:26:26 +00:00
|
|
|
-- get automatic process control status
|
2023-02-25 04:36:16 +00:00
|
|
|
---@nodiscard
|
2023-01-26 23:26:26 +00:00
|
|
|
function public.get_control_status()
|
2023-02-15 03:55:40 +00:00
|
|
|
local astat = self.ascram_status
|
2023-01-26 23:26:26 +00:00
|
|
|
return {
|
2023-02-04 18:47:00 +00:00
|
|
|
self.all_sys_ok,
|
2023-02-03 03:58:51 +00:00
|
|
|
self.units_ready,
|
2023-01-26 23:26:26 +00:00
|
|
|
self.mode,
|
2023-02-08 04:47:58 +00:00
|
|
|
self.waiting_on_ramp or self.waiting_on_stable,
|
2023-02-07 05:32:50 +00:00
|
|
|
self.at_max_burn or self.saturated,
|
2023-01-26 23:26:26 +00:00
|
|
|
self.ascram,
|
2023-02-15 03:55:40 +00:00
|
|
|
astat.matrix_dc,
|
|
|
|
astat.matrix_fill,
|
|
|
|
astat.crit_alarm,
|
|
|
|
astat.radiation,
|
|
|
|
astat.gen_fault or self.mode == PROCESS.GEN_RATE_FAULT_IDLE,
|
2023-02-02 02:55:02 +00:00
|
|
|
self.status_text[1],
|
|
|
|
self.status_text[2],
|
2023-07-08 20:57:13 +00:00
|
|
|
self.group_map,
|
|
|
|
self.current_waste_product,
|
2024-04-28 06:01:21 +00:00
|
|
|
self.pu_fallback and (self.current_waste_product == WASTE.PLUTONIUM) and (self.waste_product ~= WASTE.PLUTONIUM),
|
|
|
|
self.disabled_sps
|
2023-01-26 23:26:26 +00:00
|
|
|
}
|
|
|
|
end
|
|
|
|
|
2022-12-10 18:58:17 +00:00
|
|
|
-- get RTU statuses
|
2023-02-25 04:36:16 +00:00
|
|
|
---@nodiscard
|
2022-12-10 18:58:17 +00:00
|
|
|
function public.get_rtu_statuses()
|
|
|
|
local status = {}
|
|
|
|
|
2023-02-16 00:52:28 +00:00
|
|
|
-- total count of all connected RTUs in the facility
|
|
|
|
status.count = self.rtu_conn_count
|
|
|
|
|
2023-01-26 23:26:26 +00:00
|
|
|
-- power averages from induction matricies
|
|
|
|
status.power = {
|
2023-02-04 02:05:21 +00:00
|
|
|
self.avg_charge.compute(),
|
|
|
|
self.avg_inflow.compute(),
|
2024-04-27 20:27:01 +00:00
|
|
|
self.avg_outflow.compute(),
|
|
|
|
0
|
2023-01-26 23:26:26 +00:00
|
|
|
}
|
|
|
|
|
2022-12-10 18:58:17 +00:00
|
|
|
-- status of induction matricies (including tanks)
|
|
|
|
status.induction = {}
|
|
|
|
for i = 1, #self.induction do
|
2024-04-27 20:27:01 +00:00
|
|
|
local matrix = self.induction[i] ---@type unit_session
|
|
|
|
local db = matrix.get_db() ---@type imatrix_session_db
|
|
|
|
|
2023-11-06 15:21:42 +00:00
|
|
|
status.induction[i] = { matrix.is_faulted(), db.formed, db.state, db.tanks }
|
2024-04-27 20:27:01 +00:00
|
|
|
|
|
|
|
local fe_per_ms = self.avg_net.compute()
|
2024-07-27 16:34:01 +00:00
|
|
|
local remaining = util.joules_to_fe_rf(util.trinary(fe_per_ms >= 0, db.tanks.energy_need, db.tanks.energy))
|
2024-04-27 20:27:01 +00:00
|
|
|
status.power[4] = remaining / fe_per_ms
|
2022-12-10 18:58:17 +00:00
|
|
|
end
|
|
|
|
|
2023-07-08 20:57:13 +00:00
|
|
|
-- status of sps
|
|
|
|
status.sps = {}
|
|
|
|
for i = 1, #self.sps do
|
2023-07-15 17:16:36 +00:00
|
|
|
local sps = self.sps[i] ---@type unit_session
|
|
|
|
local db = sps.get_db() ---@type sps_session_db
|
2023-11-06 15:21:42 +00:00
|
|
|
status.sps[i] = { sps.is_faulted(), db.formed, db.state, db.tanks }
|
2023-07-15 17:16:36 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
-- status of dynamic tanks
|
|
|
|
status.tanks = {}
|
|
|
|
for i = 1, #self.tanks do
|
|
|
|
local tank = self.tanks[i] ---@type unit_session
|
|
|
|
local db = tank.get_db() ---@type dynamicv_session_db
|
|
|
|
status.tanks[tank.get_device_idx()] = { tank.is_faulted(), db.formed, db.state, db.tanks }
|
2023-07-08 20:57:13 +00:00
|
|
|
end
|
|
|
|
|
2023-02-14 03:11:31 +00:00
|
|
|
-- radiation monitors (environment detectors)
|
2023-11-12 21:55:24 +00:00
|
|
|
status.envds = {}
|
2023-02-14 03:11:31 +00:00
|
|
|
for i = 1, #self.envd do
|
|
|
|
local envd = self.envd[i] ---@type unit_session
|
2023-11-12 16:54:47 +00:00
|
|
|
local db = envd.get_db() ---@type envd_session_db
|
2023-11-12 21:55:24 +00:00
|
|
|
status.envds[envd.get_device_idx()] = { envd.is_faulted(), db.radiation, db.radiation_raw }
|
2023-02-14 03:11:31 +00:00
|
|
|
end
|
2022-12-10 18:58:17 +00:00
|
|
|
|
|
|
|
return status
|
|
|
|
end
|
|
|
|
|
2024-03-06 02:47:14 +00:00
|
|
|
--#endregion
|
|
|
|
|
|
|
|
-- 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
|
|
|
|
|
2023-02-25 04:36:16 +00:00
|
|
|
-- get the units in this facility
|
|
|
|
---@nodiscard
|
|
|
|
function public.get_units() return self.units end
|
2022-12-18 18:56:04 +00:00
|
|
|
|
2022-12-10 18:58:17 +00:00
|
|
|
return public
|
|
|
|
end
|
|
|
|
|
|
|
|
return facility
|