2022-12-10 18:58:17 +00:00
|
|
|
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-05-18 17:28:43 +00:00
|
|
|
|
2023-02-18 22:36:44 +00:00
|
|
|
local logic = require("supervisor.unitlogic")
|
|
|
|
|
2023-01-03 21:50:31 +00:00
|
|
|
local plc = require("supervisor.session.plc")
|
2022-12-10 18:58:17 +00:00
|
|
|
local rsctl = require("supervisor.session.rsctl")
|
2022-12-01 04:31:14 +00:00
|
|
|
|
|
|
|
---@class reactor_control_unit
|
2022-05-09 13:35:39 +00:00
|
|
|
local unit = {}
|
|
|
|
|
2023-07-15 17:16:36 +00:00
|
|
|
local WASTE_MODE = types.WASTE_MODE
|
|
|
|
local WASTE = types.WASTE_PRODUCT
|
|
|
|
local ALARM = types.ALARM
|
|
|
|
local PRIO = types.ALARM_PRIORITY
|
|
|
|
local ALARM_STATE = types.ALARM_STATE
|
|
|
|
local TRI_FAIL = types.TRI_FAIL
|
|
|
|
local RTU_UNIT_TYPE = types.RTU_UNIT_TYPE
|
2023-02-25 04:36:16 +00:00
|
|
|
|
2023-01-03 21:50:31 +00:00
|
|
|
local PLC_S_CMDS = plc.PLC_S_CMDS
|
|
|
|
|
2022-12-01 04:31:14 +00:00
|
|
|
local IO = rsio.IO
|
|
|
|
|
2022-05-21 16:24:43 +00:00
|
|
|
local DT_KEYS = {
|
2023-02-05 18:04:42 +00:00
|
|
|
ReactorBurnR = "RBR",
|
2022-08-28 16:12:30 +00:00
|
|
|
ReactorTemp = "RTP",
|
|
|
|
ReactorFuel = "RFL",
|
2022-05-21 16:24:43 +00:00
|
|
|
ReactorWaste = "RWS",
|
|
|
|
ReactorCCool = "RCC",
|
|
|
|
ReactorHCool = "RHC",
|
2022-08-28 16:12:30 +00:00
|
|
|
BoilerWater = "BWR",
|
|
|
|
BoilerSteam = "BST",
|
|
|
|
BoilerCCool = "BCC",
|
|
|
|
BoilerHCool = "BHC",
|
2022-09-19 02:25:59 +00:00
|
|
|
TurbineSteam = "TST",
|
|
|
|
TurbinePower = "TPR"
|
2022-05-18 17:28:43 +00:00
|
|
|
}
|
|
|
|
|
2023-02-21 17:40:34 +00:00
|
|
|
---@enum ALARM_INT_STATE
|
2022-11-26 21:18:31 +00:00
|
|
|
local AISTATE = {
|
2023-02-21 17:40:34 +00:00
|
|
|
INACTIVE = 1,
|
|
|
|
TRIPPING = 2,
|
|
|
|
TRIPPED = 3,
|
|
|
|
ACKED = 4,
|
|
|
|
RING_BACK = 5,
|
|
|
|
RING_BACK_TRIPPING = 6
|
2022-11-26 21:18:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
---@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)
|
|
|
|
|
2022-05-11 16:31:19 +00:00
|
|
|
-- create a new reactor unit
|
2023-02-25 04:36:16 +00:00
|
|
|
---@nodiscard
|
|
|
|
---@param reactor_id integer reactor unit number
|
2022-05-18 17:28:43 +00:00
|
|
|
---@param num_boilers integer number of boilers expected
|
|
|
|
---@param num_turbines integer number of turbines expected
|
2023-02-25 04:36:16 +00:00
|
|
|
function unit.new(reactor_id, num_boilers, num_turbines)
|
2023-01-03 21:50:31 +00:00
|
|
|
---@class _unit_self
|
2022-05-09 13:35:39 +00:00
|
|
|
local self = {
|
2023-02-25 04:36:16 +00:00
|
|
|
r_id = reactor_id,
|
2022-09-05 15:49:23 +00:00
|
|
|
plc_s = nil, ---@class plc_session_struct
|
|
|
|
plc_i = nil, ---@class plc_session
|
2023-01-03 21:50:31 +00:00
|
|
|
num_boilers = num_boilers,
|
|
|
|
num_turbines = num_turbines,
|
|
|
|
types = { DT_KEYS = DT_KEYS, AISTATE = AISTATE },
|
2023-02-19 05:14:27 +00:00
|
|
|
-- rtus
|
2023-07-15 17:16:36 +00:00
|
|
|
rtu_list = {},
|
2022-05-09 13:35:39 +00:00
|
|
|
redstone = {},
|
2023-02-14 03:11:31 +00:00
|
|
|
boilers = {},
|
|
|
|
turbines = {},
|
2023-07-15 17:16:36 +00:00
|
|
|
tanks = {},
|
|
|
|
snas = {},
|
2023-02-14 03:11:31 +00:00
|
|
|
envd = {},
|
2023-07-08 20:57:13 +00:00
|
|
|
sna_prod_rate = 0,
|
2023-02-19 05:14:27 +00:00
|
|
|
-- redstone control
|
|
|
|
io_ctl = nil, ---@type rs_controller
|
|
|
|
valves = {}, ---@type unit_valves
|
2023-02-20 17:08:51 +00:00
|
|
|
emcool_opened = false,
|
2023-01-03 21:50:31 +00:00
|
|
|
-- auto control
|
2023-02-20 17:08:51 +00:00
|
|
|
auto_engaged = false,
|
|
|
|
auto_was_alarmed = false,
|
2023-02-09 01:26:13 +00:00
|
|
|
ramp_target_br100 = 0,
|
2022-12-05 21:17:09 +00:00
|
|
|
-- state tracking
|
2022-05-21 16:24:43 +00:00
|
|
|
deltas = {},
|
2022-09-08 14:25:00 +00:00
|
|
|
last_heartbeat = 0,
|
2023-03-04 07:05:36 +00:00
|
|
|
last_radiation = 0,
|
2023-03-04 18:38:41 +00:00
|
|
|
damage_decreasing = false,
|
2022-12-05 21:17:09 +00:00
|
|
|
damage_initial = 0,
|
|
|
|
damage_start = 0,
|
2022-12-07 04:39:35 +00:00
|
|
|
damage_last = 0,
|
|
|
|
damage_est_last = 0,
|
2023-07-06 05:36:06 +00:00
|
|
|
waste_product = WASTE.PLUTONIUM, ---@type WASTE_PRODUCT
|
2022-12-18 18:56:04 +00:00
|
|
|
status_text = { "UNKNOWN", "awaiting connection..." },
|
2022-11-26 21:18:31 +00:00
|
|
|
-- logic for alarms
|
|
|
|
had_reactor = false,
|
2023-02-05 18:04:42 +00:00
|
|
|
last_rate_change_ms = 0,
|
2023-02-07 22:51:55 +00:00
|
|
|
---@type rps_status
|
|
|
|
last_rps_trips = {
|
2023-03-05 03:32:13 +00:00
|
|
|
high_dmg = false,
|
2023-02-07 22:51:55 +00:00
|
|
|
high_temp = false,
|
2023-03-05 03:19:53 +00:00
|
|
|
low_cool = false,
|
2023-02-07 22:51:55 +00:00
|
|
|
ex_waste = false,
|
|
|
|
ex_hcool = false,
|
|
|
|
no_fuel = false,
|
|
|
|
fault = false,
|
|
|
|
timeout = false,
|
|
|
|
manual = false,
|
|
|
|
automatic = false,
|
|
|
|
sys_fail = false,
|
|
|
|
force_dis = false
|
|
|
|
},
|
2022-11-26 21:18:31 +00:00
|
|
|
plc_cache = {
|
2022-12-07 04:39:35 +00:00
|
|
|
active = false,
|
2022-11-26 21:18:31 +00:00
|
|
|
ok = false,
|
2023-02-13 23:20:48 +00:00
|
|
|
rps_trip = false,
|
2023-02-07 22:51:55 +00:00
|
|
|
---@type rps_status
|
|
|
|
rps_status = {
|
2023-03-05 03:32:13 +00:00
|
|
|
high_dmg = false,
|
2023-02-07 22:51:55 +00:00
|
|
|
high_temp = false,
|
2023-03-05 03:19:53 +00:00
|
|
|
low_cool = false,
|
2023-02-07 22:51:55 +00:00
|
|
|
ex_waste = false,
|
|
|
|
ex_hcool = false,
|
|
|
|
no_fuel = false,
|
|
|
|
fault = false,
|
|
|
|
timeout = false,
|
|
|
|
manual = false,
|
|
|
|
automatic = false,
|
|
|
|
sys_fail = false,
|
|
|
|
force_dis = false
|
|
|
|
},
|
2022-11-26 21:18:31 +00:00
|
|
|
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
|
2023-02-20 17:08:51 +00:00
|
|
|
ReactorLost = { state = AISTATE.INACTIVE, trip_time = 0, hold_time = 0, id = ALARM.ReactorLost, tier = PRIO.TIMELY },
|
2022-11-26 21:18:31 +00:00
|
|
|
-- 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 },
|
2023-02-26 19:17:35 +00:00
|
|
|
-- reactor >=1150K
|
2023-02-20 17:08:51 +00:00
|
|
|
ReactorHighTemp = { state = AISTATE.INACTIVE, trip_time = 0, hold_time = 1, id = ALARM.ReactorHighTemp, tier = PRIO.TIMELY },
|
2022-11-26 21:18:31 +00:00
|
|
|
-- waste = 100%
|
|
|
|
ReactorWasteLeak = { state = AISTATE.INACTIVE, trip_time = 0, hold_time = 0, id = ALARM.ReactorWasteLeak, tier = PRIO.EMERGENCY },
|
|
|
|
-- waste >85%
|
2023-02-20 17:08:51 +00:00
|
|
|
ReactorHighWaste = { state = AISTATE.INACTIVE, trip_time = 0, hold_time = 2, id = ALARM.ReactorHighWaste, tier = PRIO.URGENT },
|
2022-11-26 21:18:31 +00:00
|
|
|
-- RPS trip occured
|
2023-02-20 17:08:51 +00:00
|
|
|
RPSTransient = { state = AISTATE.INACTIVE, trip_time = 0, hold_time = 2, id = ALARM.RPSTransient, tier = PRIO.TIMELY },
|
2023-04-02 13:57:57 +00:00
|
|
|
-- CoolantLevelLow, WaterLevelLow, TurbineOverSpeed, MaxWaterReturnFeed, RCPTrip, RCSFlowLow, BoilRateMismatch, CoolantFeedMismatch,
|
|
|
|
-- SteamFeedMismatch, MaxWaterReturnFeed, RCS hardware fault
|
2022-11-26 21:18:31 +00:00
|
|
|
RCSTransient = { state = AISTATE.INACTIVE, trip_time = 0, hold_time = 5, id = ALARM.RCSTransient, tier = PRIO.TIMELY },
|
2022-12-05 21:17:09 +00:00
|
|
|
-- "It's just a routine turbin' trip!" -Bill Gibson, "The China Syndrome"
|
2023-02-20 17:08:51 +00:00
|
|
|
TurbineTrip = { state = AISTATE.INACTIVE, trip_time = 0, hold_time = 2, id = ALARM.TurbineTrip, tier = PRIO.URGENT }
|
2022-11-26 21:18:31 +00:00
|
|
|
},
|
2022-12-01 04:31:14 +00:00
|
|
|
---@class unit_db
|
2022-05-09 13:35:39 +00:00
|
|
|
db = {
|
2022-05-11 16:31:19 +00:00
|
|
|
---@class annunciator
|
2022-05-09 13:35:39 +00:00
|
|
|
annunciator = {
|
|
|
|
-- reactor
|
|
|
|
PLCOnline = false,
|
2022-08-28 16:12:30 +00:00
|
|
|
PLCHeartbeat = false, -- alternate true/false to blink, each time there is a keep_alive
|
2023-02-16 00:52:28 +00:00
|
|
|
RadiationMonitor = 1,
|
2023-01-03 21:50:31 +00:00
|
|
|
AutoControl = false,
|
2022-09-08 14:25:00 +00:00
|
|
|
ReactorSCRAM = false,
|
|
|
|
ManualReactorSCRAM = false,
|
2022-11-11 21:15:44 +00:00
|
|
|
AutoReactorSCRAM = false,
|
2023-02-16 00:52:28 +00:00
|
|
|
RadiationWarning = false,
|
2022-05-09 13:35:39 +00:00
|
|
|
RCPTrip = false,
|
|
|
|
RCSFlowLow = false,
|
2022-11-26 21:18:31 +00:00
|
|
|
CoolantLevelLow = false,
|
2022-05-09 13:35:39 +00:00
|
|
|
ReactorTempHigh = false,
|
|
|
|
ReactorHighDeltaT = false,
|
2022-05-21 16:24:43 +00:00
|
|
|
FuelInputRateLow = false,
|
|
|
|
WasteLineOcclusion = false,
|
2022-05-09 13:35:39 +00:00
|
|
|
HighStartupRate = false,
|
2023-02-16 00:52:28 +00:00
|
|
|
-- cooling
|
|
|
|
RCSFault = false,
|
|
|
|
EmergencyCoolant = 1,
|
|
|
|
CoolantFeedMismatch = false,
|
|
|
|
BoilRateMismatch = false,
|
|
|
|
SteamFeedMismatch = false,
|
|
|
|
MaxWaterReturnFeed = false,
|
|
|
|
-- boilers
|
2022-08-28 16:12:30 +00:00
|
|
|
BoilerOnline = {},
|
2022-05-21 16:24:43 +00:00
|
|
|
HeatingRateLow = {},
|
2022-11-26 21:18:31 +00:00
|
|
|
WaterLevelLow = {},
|
2023-02-16 00:52:28 +00:00
|
|
|
-- turbines
|
2022-08-28 16:12:30 +00:00
|
|
|
TurbineOnline = {},
|
2022-05-21 16:24:43 +00:00
|
|
|
SteamDumpOpen = {},
|
|
|
|
TurbineOverSpeed = {},
|
2023-04-02 13:57:57 +00:00
|
|
|
GeneratorTrip = {},
|
2022-05-21 16:24:43 +00:00
|
|
|
TurbineTrip = {}
|
2022-11-26 21:18:31 +00:00
|
|
|
},
|
|
|
|
---@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
|
2023-01-03 21:50:31 +00:00
|
|
|
},
|
|
|
|
-- fields for facility control
|
|
|
|
---@class unit_control
|
|
|
|
control = {
|
2023-02-03 03:58:51 +00:00
|
|
|
ready = false,
|
|
|
|
degraded = false,
|
2023-01-03 21:50:31 +00:00
|
|
|
blade_count = 0,
|
2023-02-09 01:26:13 +00:00
|
|
|
br100 = 0,
|
2023-07-08 20:57:13 +00:00
|
|
|
lim_br100 = 0,
|
|
|
|
waste_mode = WASTE_MODE.AUTO ---@type WASTE_MODE
|
2022-05-09 13:35:39 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-07-15 17:16:36 +00:00
|
|
|
-- list for RTU session management
|
|
|
|
self.rtu_list = { self.redstone, self.boilers, self.turbines, self.tanks, self.snas, 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
|
|
|
|
2022-05-21 16:24:43 +00:00
|
|
|
-- init boiler table fields
|
2022-08-28 16:12:30 +00:00
|
|
|
for _ = 1, num_boilers do
|
|
|
|
table.insert(self.db.annunciator.BoilerOnline, false)
|
2022-05-21 16:24:43 +00:00
|
|
|
table.insert(self.db.annunciator.HeatingRateLow, false)
|
|
|
|
end
|
|
|
|
|
|
|
|
-- init turbine table fields
|
2022-08-28 16:12:30 +00:00
|
|
|
for _ = 1, num_turbines do
|
|
|
|
table.insert(self.db.annunciator.TurbineOnline, false)
|
2022-05-21 16:24:43 +00:00
|
|
|
table.insert(self.db.annunciator.SteamDumpOpen, TRI_FAIL.OK)
|
|
|
|
table.insert(self.db.annunciator.TurbineOverSpeed, false)
|
2023-04-02 13:57:57 +00:00
|
|
|
table.insert(self.db.annunciator.GeneratorTrip, false)
|
2022-05-21 16:24:43 +00:00
|
|
|
table.insert(self.db.annunciator.TurbineTrip, false)
|
|
|
|
end
|
|
|
|
|
2022-05-11 16:31:19 +00:00
|
|
|
-- PRIVATE FUNCTIONS --
|
|
|
|
|
2022-12-01 04:31:14 +00:00
|
|
|
--#region time derivative utility functions
|
|
|
|
|
2022-05-21 16:24:43 +00:00
|
|
|
-- compute a change with respect to time of the given value
|
|
|
|
---@param key string value key
|
|
|
|
---@param value number value
|
2022-10-07 15:43:18 +00:00
|
|
|
---@param time number timestamp for value
|
|
|
|
local function _compute_dt(key, value, time)
|
2022-05-21 16:24:43 +00:00
|
|
|
if self.deltas[key] then
|
|
|
|
local data = self.deltas[key]
|
|
|
|
|
2022-11-17 16:20:53 +00:00
|
|
|
if time > data.last_t then
|
2022-10-07 15:43:18 +00:00
|
|
|
data.dt = (value - data.last_v) / (time - data.last_t)
|
2022-05-21 16:24:43 +00:00
|
|
|
|
2022-10-07 15:43:18 +00:00
|
|
|
data.last_v = value
|
|
|
|
data.last_t = time
|
|
|
|
end
|
2022-05-21 16:24:43 +00:00
|
|
|
else
|
|
|
|
self.deltas[key] = {
|
2022-10-07 15:43:18 +00:00
|
|
|
last_t = time,
|
2022-05-21 16:24:43 +00:00
|
|
|
last_v = value,
|
|
|
|
dt = 0.0
|
|
|
|
}
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
-- clear a delta
|
|
|
|
---@param key string value key
|
2022-09-05 15:49:23 +00:00
|
|
|
local function _reset_dt(key) self.deltas[key] = nil end
|
2022-05-21 16:24:43 +00:00
|
|
|
|
|
|
|
-- get the delta t of a value
|
2023-02-25 04:36:16 +00:00
|
|
|
---@nodiscard
|
2022-05-21 16:24:43 +00:00
|
|
|
---@param key string value key
|
2023-02-19 05:14:27 +00:00
|
|
|
---@return number value value or 0 if not known
|
|
|
|
function self._get_dt(key) if self.deltas[key] then return self.deltas[key].dt else return 0.0 end end
|
2022-12-01 04:31:14 +00:00
|
|
|
|
2022-05-21 16:24:43 +00:00
|
|
|
-- update all delta computations
|
2022-05-31 19:36:17 +00:00
|
|
|
local function _dt__compute_all()
|
2023-01-03 21:50:31 +00:00
|
|
|
if self.plc_i ~= nil then
|
2022-09-05 15:49:23 +00:00
|
|
|
local plc_db = self.plc_i.get_db()
|
2022-05-21 16:24:43 +00:00
|
|
|
|
2022-10-07 15:43:18 +00:00
|
|
|
local last_update_s = plc_db.last_status_update / 1000.0
|
|
|
|
|
2023-02-05 18:04:42 +00:00
|
|
|
_compute_dt(DT_KEYS.ReactorBurnR, plc_db.mek_status.act_burn_rate, last_update_s)
|
2022-10-07 15:43:18 +00:00
|
|
|
_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)
|
2022-05-21 16:24:43 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
for i = 1, #self.boilers do
|
|
|
|
local boiler = self.boilers[i] ---@type unit_session
|
2022-09-19 02:25:59 +00:00
|
|
|
local db = boiler.get_db() ---@type boilerv_session_db
|
2022-05-21 16:24:43 +00:00
|
|
|
|
2022-10-07 15:43:18 +00:00
|
|
|
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)
|
2022-05-21 16:24:43 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
for i = 1, #self.turbines do
|
|
|
|
local turbine = self.turbines[i] ---@type unit_session
|
2022-09-19 02:25:59 +00:00
|
|
|
local db = turbine.get_db() ---@type turbinev_session_db
|
2022-05-21 16:24:43 +00:00
|
|
|
|
2022-10-07 15:43:18 +00:00
|
|
|
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)
|
|
|
|
_compute_dt(DT_KEYS.TurbinePower .. turbine.get_device_idx(), db.tanks.energy, last_update_s)
|
2022-05-21 16:24:43 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-12-01 04:31:14 +00:00
|
|
|
--#endregion
|
|
|
|
|
2023-01-03 21:50:31 +00:00
|
|
|
--#region redstone I/O
|
2022-11-26 21:18:31 +00:00
|
|
|
|
2023-08-20 03:24:20 +00:00
|
|
|
-- create a generic valve interface
|
|
|
|
---@nodiscard
|
|
|
|
---@param port IO_PORT
|
|
|
|
local function _make_valve_iface(port)
|
|
|
|
---@class unit_valve_iface
|
|
|
|
local iface = {
|
|
|
|
open = function () self.io_ctl.digital_write(port, true) end,
|
|
|
|
close = function () self.io_ctl.digital_write(port, false) end,
|
|
|
|
-- check valve state
|
|
|
|
---@nodiscard
|
|
|
|
---@return 0|1|2 0 for not connected, 1 for inactive, 2 for active
|
|
|
|
check = function () return util.trinary(self.io_ctl.is_connected(port), util.trinary(self.io_ctl.digital_read(port), 2, 1), 0) end
|
|
|
|
}
|
|
|
|
return iface
|
|
|
|
end
|
2022-11-26 21:18:31 +00:00
|
|
|
|
2023-02-13 23:20:48 +00:00
|
|
|
-- valves
|
2023-08-20 03:24:20 +00:00
|
|
|
local waste_pu = _make_valve_iface(IO.WASTE_PU)
|
|
|
|
local waste_sna = _make_valve_iface(IO.WASTE_PO)
|
|
|
|
local waste_po = _make_valve_iface(IO.WASTE_POPL)
|
|
|
|
local waste_sps = _make_valve_iface(IO.WASTE_AM)
|
|
|
|
local emer_cool = _make_valve_iface(IO.U_EMER_COOL)
|
2022-11-26 21:18:31 +00:00
|
|
|
|
2023-02-19 05:14:27 +00:00
|
|
|
---@class unit_valves
|
|
|
|
self.valves = {
|
|
|
|
waste_pu = waste_pu,
|
|
|
|
waste_sna = waste_sna,
|
|
|
|
waste_po = waste_po,
|
|
|
|
waste_sps = waste_sps,
|
|
|
|
emer_cool = emer_cool
|
|
|
|
}
|
|
|
|
|
2023-07-06 05:36:06 +00:00
|
|
|
-- route reactor waste for a given waste product
|
|
|
|
---@param product WASTE_PRODUCT waste product to route valves for
|
|
|
|
local function _set_waste_valves(product)
|
|
|
|
self.waste_product = product
|
|
|
|
|
|
|
|
if product == WASTE.PLUTONIUM then
|
|
|
|
-- route through plutonium generation
|
|
|
|
waste_pu.open()
|
|
|
|
waste_sna.close()
|
|
|
|
waste_po.close()
|
|
|
|
waste_sps.close()
|
|
|
|
elseif product == WASTE.POLONIUM then
|
|
|
|
-- route through polonium generation into pellets
|
|
|
|
waste_pu.close()
|
|
|
|
waste_sna.open()
|
|
|
|
waste_po.open()
|
|
|
|
waste_sps.close()
|
|
|
|
elseif product == WASTE.ANTI_MATTER then
|
|
|
|
-- route through polonium generation into SPS
|
|
|
|
waste_pu.close()
|
|
|
|
waste_sna.open()
|
|
|
|
waste_po.close()
|
|
|
|
waste_sps.open()
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-12-01 04:31:14 +00:00
|
|
|
--#endregion
|
|
|
|
|
2022-05-11 16:31:19 +00:00
|
|
|
-- PUBLIC FUNCTIONS --
|
|
|
|
|
2022-12-10 18:58:17 +00:00
|
|
|
---@class reactor_unit
|
|
|
|
local public = {}
|
|
|
|
|
2022-09-19 02:25:59 +00:00
|
|
|
-- ADD/LINK DEVICES --
|
2023-01-15 18:11:46 +00:00
|
|
|
--#region
|
2022-09-19 02:25:59 +00:00
|
|
|
|
2022-05-11 16:31:19 +00:00
|
|
|
-- link the PLC
|
|
|
|
---@param plc_session plc_session_struct
|
2022-05-31 19:36:17 +00:00
|
|
|
function public.link_plc_session(plc_session)
|
2022-11-26 21:18:31 +00:00
|
|
|
self.had_reactor = true
|
2022-05-09 13:35:39 +00:00
|
|
|
self.plc_s = plc_session
|
2022-09-05 15:49:23 +00:00
|
|
|
self.plc_i = plc_session.instance
|
2022-05-21 16:24:43 +00:00
|
|
|
|
|
|
|
-- 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)
|
2022-05-09 13:35:39 +00:00
|
|
|
end
|
|
|
|
|
2022-12-01 04:31:14 +00:00
|
|
|
-- link a redstone RTU session
|
|
|
|
---@param rs_unit unit_session
|
|
|
|
function public.add_redstone(rs_unit)
|
|
|
|
table.insert(self.redstone, rs_unit)
|
2023-01-24 01:47:45 +00:00
|
|
|
|
|
|
|
-- send or re-send waste settings
|
2023-07-06 05:36:06 +00:00
|
|
|
_set_waste_valves(self.waste_product)
|
2022-12-01 04:31:14 +00:00
|
|
|
end
|
|
|
|
|
2022-05-18 17:28:43 +00:00
|
|
|
-- link a turbine RTU session
|
|
|
|
---@param turbine unit_session
|
2023-07-15 17:16:36 +00:00
|
|
|
---@return boolean linked turbine accepted to associated device slot
|
2022-05-31 19:36:17 +00:00
|
|
|
function public.add_turbine(turbine)
|
2022-08-28 16:12:30 +00:00
|
|
|
if #self.turbines < num_turbines and turbine.get_device_idx() <= num_turbines then
|
2022-05-21 16:24:43 +00:00
|
|
|
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
|
2023-07-15 17:16:36 +00:00
|
|
|
else return false end
|
2022-05-09 13:35:39 +00:00
|
|
|
end
|
|
|
|
|
2022-05-18 17:28:43 +00:00
|
|
|
-- link a boiler RTU session
|
|
|
|
---@param boiler unit_session
|
2023-07-15 17:16:36 +00:00
|
|
|
---@return boolean linked boiler accepted to associated device slot
|
2022-05-31 19:36:17 +00:00
|
|
|
function public.add_boiler(boiler)
|
2022-08-28 16:12:30 +00:00
|
|
|
if #self.boilers < num_boilers and boiler.get_device_idx() <= num_boilers then
|
2022-05-21 16:24:43 +00:00
|
|
|
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
|
2023-07-15 17:16:36 +00:00
|
|
|
else return false end
|
|
|
|
end
|
|
|
|
|
|
|
|
-- link a dynamic tank RTU session
|
|
|
|
---@param dynamic_tank unit_session
|
|
|
|
---@return boolean linked dynamic tank accepted (max 1)
|
|
|
|
function public.add_tank(dynamic_tank)
|
|
|
|
if #self.tanks == 0 then
|
|
|
|
table.insert(self.tanks, dynamic_tank)
|
|
|
|
return true
|
|
|
|
else return false end
|
2022-05-09 13:35:39 +00:00
|
|
|
end
|
|
|
|
|
2023-07-06 05:36:06 +00:00
|
|
|
-- link a solar neutron activator RTU session
|
|
|
|
---@param sna unit_session
|
2023-07-15 17:16:36 +00:00
|
|
|
function public.add_sna(sna) table.insert(self.snas, sna) end
|
2023-07-06 05:36:06 +00:00
|
|
|
|
2023-02-14 03:11:31 +00:00
|
|
|
-- link an environment detector RTU session
|
|
|
|
---@param envd unit_session
|
2023-07-15 17:16:36 +00:00
|
|
|
---@return boolean linked environment detector accepted (max 1)
|
2023-02-14 03:11:31 +00:00
|
|
|
function public.add_envd(envd)
|
2023-07-15 17:16:36 +00:00
|
|
|
if #self.envd == 0 then
|
|
|
|
table.insert(self.envd, envd)
|
|
|
|
return true
|
|
|
|
else return false end
|
2023-02-14 03:11:31 +00:00
|
|
|
end
|
|
|
|
|
2022-11-12 06:35:31 +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-11-12 06:35:31 +00:00
|
|
|
end
|
|
|
|
|
2023-01-15 18:11:46 +00:00
|
|
|
--#endregion
|
|
|
|
|
2023-02-07 05:32:50 +00:00
|
|
|
-- 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
|
2023-02-09 01:26:13 +00:00
|
|
|
self.db.control.br100 = 0
|
|
|
|
self.db.control.lim_br100 = 0
|
2023-02-07 05:32:50 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
-- 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-02-07 05:32:50 +00:00
|
|
|
|
|
|
|
-- 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)
|
|
|
|
|
2023-02-20 17:08:51 +00:00
|
|
|
-- if in auto mode, SCRAM on certain alarms
|
|
|
|
logic.update_auto_safety(public, self)
|
|
|
|
|
2023-02-07 05:32:50 +00:00
|
|
|
-- update status text
|
|
|
|
logic.update_status_text(self)
|
2023-02-13 23:20:48 +00:00
|
|
|
|
2023-02-19 05:14:27 +00:00
|
|
|
-- handle redstone I/O
|
|
|
|
if #self.redstone > 0 then
|
|
|
|
logic.handle_redstone(self)
|
2023-02-20 17:08:51 +00:00
|
|
|
elseif not self.plc_cache.rps_trip then
|
|
|
|
self.emcool_opened = false
|
2023-02-13 23:20:48 +00:00
|
|
|
end
|
2023-02-07 05:32:50 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
-- AUTO CONTROL OPERATIONS --
|
2023-01-15 18:11:46 +00:00
|
|
|
--#region
|
2023-01-03 21:50:31 +00:00
|
|
|
|
|
|
|
-- engage automatic control
|
2023-06-03 21:59:20 +00:00
|
|
|
function public.auto_engage()
|
2023-02-20 17:08:51 +00:00
|
|
|
self.auto_engaged = true
|
2023-01-03 21:50:31 +00:00
|
|
|
if self.plc_i ~= nil then
|
|
|
|
self.plc_i.auto_lock(true)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
-- disengage automatic control
|
2023-06-03 21:59:20 +00:00
|
|
|
function public.auto_disengage()
|
2023-02-20 17:08:51 +00:00
|
|
|
self.auto_engaged = false
|
2023-01-03 21:50:31 +00:00
|
|
|
if self.plc_i ~= nil then
|
|
|
|
self.plc_i.auto_lock(false)
|
2023-02-09 01:26:13 +00:00
|
|
|
self.db.control.br100 = 0
|
2023-01-03 21:50:31 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2023-02-25 04:36:16 +00:00
|
|
|
-- get the actual limit of this unit<br>
|
2023-02-07 05:32:50 +00:00
|
|
|
-- if it is degraded or not ready, the limit will be 0
|
2023-02-25 04:36:16 +00:00
|
|
|
---@nodiscard
|
2023-02-09 01:26:13 +00:00
|
|
|
---@return integer lim_br100
|
2023-06-03 21:59:20 +00:00
|
|
|
function public.auto_get_effective_limit()
|
2023-02-25 19:11:40 +00:00
|
|
|
if (not self.db.control.ready) or self.db.control.degraded or self.plc_cache.rps_trip then
|
2023-02-09 01:26:13 +00:00
|
|
|
self.db.control.br100 = 0
|
2023-02-07 05:32:50 +00:00
|
|
|
return 0
|
|
|
|
else
|
2023-02-09 01:26:13 +00:00
|
|
|
return self.db.control.lim_br100
|
2023-02-07 05:32:50 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2023-02-09 01:26:13 +00:00
|
|
|
-- set the automatic burn rate based on the last set burn rate in 100ths
|
2023-01-03 21:50:31 +00:00
|
|
|
---@param ramp boolean true to ramp to rate, false to set right away
|
2023-06-03 21:59:20 +00:00
|
|
|
function public.auto_commit_br100(ramp)
|
2023-02-20 17:08:51 +00:00
|
|
|
if self.auto_engaged then
|
2023-01-03 21:50:31 +00:00
|
|
|
if self.plc_i ~= nil then
|
2023-02-09 01:26:13 +00:00
|
|
|
self.plc_i.auto_set_burn(self.db.control.br100 / 100, ramp)
|
2023-01-03 21:50:31 +00:00
|
|
|
|
2023-02-09 01:26:13 +00:00
|
|
|
if ramp then self.ramp_target_br100 = self.db.control.br100 end
|
2023-01-03 21:50:31 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
-- check if ramping is complete (burn rate is same as target)
|
2023-02-25 04:36:16 +00:00
|
|
|
---@nodiscard
|
2023-01-03 21:50:31 +00:00
|
|
|
---@return boolean complete
|
2023-06-03 21:59:20 +00:00
|
|
|
function public.auto_ramp_complete()
|
2023-01-03 21:50:31 +00:00
|
|
|
if self.plc_i ~= nil then
|
2023-02-07 05:32:50 +00:00
|
|
|
return self.plc_i.is_ramp_complete() or
|
2023-02-09 01:26:13 +00:00
|
|
|
(self.plc_i.get_status().act_burn_rate == 0 and self.db.control.br100 == 0) or
|
2023-06-03 21:59:20 +00:00
|
|
|
public.auto_get_effective_limit() == 0
|
2023-02-03 03:58:51 +00:00
|
|
|
else return true end
|
2023-01-03 21:50:31 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
-- perform an automatic SCRAM
|
2023-06-03 21:59:20 +00:00
|
|
|
function public.auto_scram()
|
2023-01-03 21:50:31 +00:00
|
|
|
if self.plc_s ~= nil then
|
2023-02-11 19:27:29 +00:00
|
|
|
self.db.control.br100 = 0
|
2023-01-03 21:50:31 +00:00
|
|
|
self.plc_s.in_queue.push_command(PLC_S_CMDS.ASCRAM)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2023-02-07 05:32:50 +00:00
|
|
|
-- queue a command to clear timeout/auto-scram if set
|
2023-06-03 21:59:20 +00:00
|
|
|
function public.auto_cond_rps_reset()
|
2023-02-20 17:08:51 +00:00
|
|
|
if self.plc_s ~= nil and self.plc_i ~= nil and (not self.auto_was_alarmed) and (not self.emcool_opened) then
|
2023-02-07 05:32:50 +00:00
|
|
|
local rps = self.plc_i.get_rps()
|
|
|
|
if rps.timeout or rps.automatic then
|
2023-02-10 03:52:10 +00:00
|
|
|
self.plc_i.auto_lock(true) -- if it timed out/restarted, auto lock was lost, so re-lock it
|
2023-02-07 05:32:50 +00:00
|
|
|
self.plc_s.in_queue.push_command(PLC_S_CMDS.RPS_AUTO_RESET)
|
|
|
|
end
|
2022-05-09 13:35:39 +00:00
|
|
|
end
|
2022-11-26 21:18:31 +00:00
|
|
|
end
|
|
|
|
|
2023-07-06 05:36:06 +00:00
|
|
|
-- set automatic waste product if mode is set to auto
|
|
|
|
---@param product WASTE_PRODUCT waste product to generate
|
|
|
|
function public.auto_set_waste(product)
|
2023-07-08 20:57:13 +00:00
|
|
|
if self.db.control.waste_mode == WASTE_MODE.AUTO then
|
2023-07-06 05:36:06 +00:00
|
|
|
self.waste_product = product
|
|
|
|
_set_waste_valves(product)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2023-02-07 05:32:50 +00:00
|
|
|
--#endregion
|
|
|
|
|
2022-12-01 04:31:14 +00:00
|
|
|
-- OPERATIONS --
|
2023-02-07 05:32:50 +00:00
|
|
|
--#region
|
2022-11-26 21:18:31 +00:00
|
|
|
|
2023-01-26 23:26:26 +00:00
|
|
|
-- 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
|
|
|
|
|
2023-02-19 05:14:27 +00:00
|
|
|
-- queue a SCRAM command only if a manual SCRAM has not already occured
|
|
|
|
function public.cond_scram()
|
|
|
|
if self.plc_s ~= nil and not self.plc_cache.rps_status.manual then
|
|
|
|
self.plc_s.in_queue.push_command(PLC_S_CMDS.SCRAM)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-11-26 21:18:31 +00:00
|
|
|
-- 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)
|
2023-02-25 04:36:16 +00:00
|
|
|
if type(id) == "number" and self.db.alarm_states[id] == ALARM_STATE.TRIPPED then
|
2022-11-26 21:18:31 +00:00
|
|
|
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)
|
2023-02-25 04:36:16 +00:00
|
|
|
if type(id) == "number" and self.db.alarm_states[id] == ALARM_STATE.RING_BACK then
|
2022-11-26 21:18:31 +00:00
|
|
|
self.db.alarm_states[id] = ALARM_STATE.INACTIVE
|
|
|
|
end
|
2022-09-19 02:25:59 +00:00
|
|
|
end
|
|
|
|
|
2023-07-06 05:36:06 +00:00
|
|
|
-- set waste processing mode
|
|
|
|
---@param mode WASTE_MODE processing mode
|
|
|
|
function public.set_waste_mode(mode)
|
2023-07-08 20:57:13 +00:00
|
|
|
self.db.control.waste_mode = mode
|
2023-07-06 05:36:06 +00:00
|
|
|
|
|
|
|
if mode == WASTE_MODE.MANUAL_PLUTONIUM then
|
|
|
|
_set_waste_valves(WASTE.PLUTONIUM)
|
|
|
|
elseif mode == WASTE_MODE.MANUAL_POLONIUM then
|
|
|
|
_set_waste_valves(WASTE.POLONIUM)
|
|
|
|
elseif mode == WASTE_MODE.MANUAL_ANTI_MATTER then
|
|
|
|
_set_waste_valves(WASTE.ANTI_MATTER)
|
|
|
|
elseif mode > WASTE_MODE.MANUAL_ANTI_MATTER then
|
2022-12-05 21:17:09 +00:00
|
|
|
log.debug(util.c("invalid waste mode setting ", mode))
|
2022-12-01 04:31:14 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-12-18 18:56:04 +00:00
|
|
|
-- 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)
|
2023-01-15 18:11:46 +00:00
|
|
|
if limit > 0 then
|
2023-02-09 01:26:13 +00:00
|
|
|
self.db.control.lim_br100 = math.floor(limit * 100)
|
2022-12-18 18:56:04 +00:00
|
|
|
|
|
|
|
if self.plc_i ~= nil then
|
|
|
|
if limit > self.plc_i.get_struct().max_burn then
|
2023-02-09 01:26:13 +00:00
|
|
|
self.db.control.lim_br100 = math.floor(self.plc_i.get_struct().max_burn * 100)
|
2022-12-18 18:56:04 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2023-02-07 05:32:50 +00:00
|
|
|
--#endregion
|
|
|
|
|
2022-09-19 02:25:59 +00:00
|
|
|
-- READ STATES/PROPERTIES --
|
2023-02-07 05:32:50 +00:00
|
|
|
--#region
|
|
|
|
|
2023-03-04 16:46:59 +00:00
|
|
|
-- check if an alarm of at least a certain priority level is tripped
|
2023-02-25 04:36:16 +00:00
|
|
|
---@nodiscard
|
2023-03-04 16:46:59 +00:00
|
|
|
---@param min_prio ALARM_PRIORITY alarms with this priority or higher will be checked
|
2023-02-25 04:36:16 +00:00
|
|
|
---@return boolean tripped
|
2023-03-04 16:46:59 +00:00
|
|
|
function public.has_alarm_min_prio(min_prio)
|
2023-02-20 17:08:51 +00:00
|
|
|
for _, alarm in pairs(self.alarms) do
|
2023-03-04 16:46:59 +00:00
|
|
|
if alarm.tier <= min_prio and (alarm.state == AISTATE.TRIPPED or alarm.state == AISTATE.ACKED) then
|
2023-02-07 05:32:50 +00:00
|
|
|
return true
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
return false
|
|
|
|
end
|
2022-09-19 02:25:59 +00:00
|
|
|
|
2023-07-29 22:16:59 +00:00
|
|
|
-- check if the reactor is connected, is stopped, the RPS is not tripped, and no alarms are active
|
|
|
|
---@nodiscard
|
|
|
|
function public.is_safe_idle()
|
|
|
|
-- can't be disconnected
|
|
|
|
if self.plc_i == nil then return false end
|
|
|
|
|
2023-07-30 04:13:26 +00:00
|
|
|
-- reactor must be stopped and RPS can't be tripped
|
|
|
|
if self.plc_i.get_status().status or self.plc_i.get_db().rps_tripped then return false end
|
|
|
|
|
2023-07-29 22:16:59 +00:00
|
|
|
-- alarms must be inactive and not tripping
|
|
|
|
for _, alarm in pairs(self.alarms) do
|
|
|
|
if not (alarm.state == AISTATE.INACTIVE or alarm.state == AISTATE.RING_BACK) then return false end
|
|
|
|
end
|
|
|
|
|
|
|
|
return true
|
|
|
|
end
|
|
|
|
|
2023-08-21 02:56:51 +00:00
|
|
|
-- check if emergency coolant activation has been tripped
|
|
|
|
---@nodiscard
|
|
|
|
function public.is_emer_cool_tripped() return self.emcool_opened end
|
|
|
|
|
2023-07-15 17:16:36 +00:00
|
|
|
-- get build properties of machines
|
|
|
|
--
|
|
|
|
-- filter options
|
|
|
|
-- - nil to include all builds
|
|
|
|
-- - -1 to include only PLC build
|
|
|
|
-- - RTU_UNIT_TYPE to include all builds of machines of that type
|
2023-02-25 04:36:16 +00:00
|
|
|
---@nodiscard
|
2023-07-15 17:16:36 +00:00
|
|
|
---@param filter -1|RTU_UNIT_TYPE? filter as described above
|
|
|
|
function public.get_build(filter)
|
|
|
|
local all = filter == nil
|
2022-05-21 16:24:43 +00:00
|
|
|
local build = {}
|
|
|
|
|
2023-07-15 17:16:36 +00:00
|
|
|
if all or (filter == -1) then
|
2023-02-20 05:49:37 +00:00
|
|
|
if self.plc_i ~= nil then
|
|
|
|
build.reactor = self.plc_i.get_struct()
|
|
|
|
end
|
2022-05-21 16:24:43 +00:00
|
|
|
end
|
|
|
|
|
2023-07-15 17:16:36 +00:00
|
|
|
if all or (filter == RTU_UNIT_TYPE.BOILER_VALVE) then
|
2023-02-20 05:49:37 +00:00
|
|
|
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
|
2022-05-21 16:24:43 +00:00
|
|
|
end
|
|
|
|
|
2023-07-15 17:16:36 +00:00
|
|
|
if all or (filter == RTU_UNIT_TYPE.TURBINE_VALVE) then
|
2023-02-20 05:49:37 +00:00
|
|
|
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
|
2022-05-21 16:24:43 +00:00
|
|
|
end
|
|
|
|
|
2023-07-15 17:16:36 +00:00
|
|
|
if all or (filter == RTU_UNIT_TYPE.DYNAMIC_VALVE) then
|
|
|
|
build.tanks = {}
|
|
|
|
for i = 1, #self.tanks do
|
|
|
|
local tank = self.tanks[i] ---@type unit_session
|
|
|
|
build.tanks[tank.get_device_idx()] = { tank.get_db().formed, tank.get_db().build }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-05-21 16:24:43 +00:00
|
|
|
return build
|
|
|
|
end
|
|
|
|
|
|
|
|
-- get reactor status
|
2023-02-25 04:36:16 +00:00
|
|
|
---@nodiscard
|
2022-05-31 19:36:17 +00:00
|
|
|
function public.get_reactor_status()
|
2022-05-21 16:24:43 +00:00
|
|
|
local status = {}
|
2023-01-03 21:50:31 +00:00
|
|
|
if self.plc_i ~= nil then
|
|
|
|
status = { self.plc_i.get_status(), self.plc_i.get_rps(), self.plc_i.get_general_status() }
|
2022-05-21 16:24:43 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
return status
|
|
|
|
end
|
|
|
|
|
2023-07-08 20:57:13 +00:00
|
|
|
-- get the current burn rate (actual rate)
|
|
|
|
---@nodiscard
|
|
|
|
function public.get_burn_rate()
|
|
|
|
local rate = 0
|
|
|
|
if self.plc_i ~= nil then rate = self.plc_i.get_status().act_burn_rate end
|
|
|
|
return rate or 0
|
|
|
|
end
|
|
|
|
|
2022-05-21 16:24:43 +00:00
|
|
|
-- get RTU statuses
|
2023-02-25 04:36:16 +00:00
|
|
|
---@nodiscard
|
2022-05-31 19:36:17 +00:00
|
|
|
function public.get_rtu_statuses()
|
2022-05-21 16:24:43 +00:00
|
|
|
local status = {}
|
|
|
|
|
|
|
|
-- status of boilers (including tanks)
|
|
|
|
status.boilers = {}
|
|
|
|
for i = 1, #self.boilers do
|
2023-07-15 17:16:36 +00:00
|
|
|
local boiler = self.boilers[i] ---@type unit_session
|
|
|
|
local db = boiler.get_db() ---@type boilerv_session_db
|
|
|
|
status.boilers[boiler.get_device_idx()] = { boiler.is_faulted(), db.formed, db.state, db.tanks }
|
2022-05-21 16:24:43 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
-- status of turbines (including tanks)
|
|
|
|
status.turbines = {}
|
|
|
|
for i = 1, #self.turbines do
|
2023-07-08 20:57:13 +00:00
|
|
|
local turbine = self.turbines[i] ---@type unit_session
|
2023-07-15 17:16:36 +00:00
|
|
|
local db = turbine.get_db() ---@type turbinev_session_db
|
|
|
|
status.turbines[turbine.get_device_idx()] = { turbine.is_faulted(), db.formed, db.state, db.tanks }
|
2022-05-21 16:24:43 +00:00
|
|
|
end
|
|
|
|
|
2023-07-15 17:16:36 +00:00
|
|
|
-- 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
|
2023-07-15 17:31:48 +00:00
|
|
|
status.tanks[tank.get_device_idx()] = { tank.is_faulted(), db.formed, db.state, db.tanks }
|
2023-07-15 17:16:36 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
-- basic SNA statistical information
|
2023-08-20 00:06:37 +00:00
|
|
|
local total_peak = 0
|
|
|
|
for i = 1, #self.snas do
|
|
|
|
local db = self.snas[i].get_db() ---@type sna_session_db
|
|
|
|
total_peak = total_peak + db.state.peak_production
|
|
|
|
end
|
|
|
|
status.sna = { #self.snas, public.get_sna_rate(), total_peak }
|
2023-07-08 20:57:13 +00:00
|
|
|
|
2023-02-14 03:11:31 +00:00
|
|
|
-- radiation monitors (environment detectors)
|
|
|
|
status.rad_mon = {}
|
|
|
|
for i = 1, #self.envd do
|
2023-07-15 17:16:36 +00:00
|
|
|
local envd = self.envd[i] ---@type unit_session
|
|
|
|
status.rad_mon[envd.get_device_idx()] = { envd.is_faulted(), envd.get_db().radiation }
|
2023-02-14 03:11:31 +00:00
|
|
|
end
|
2022-09-03 14:50:14 +00:00
|
|
|
|
2022-05-21 16:24:43 +00:00
|
|
|
return status
|
|
|
|
end
|
|
|
|
|
2023-07-08 20:57:13 +00:00
|
|
|
-- get the current total [max] production rate is
|
|
|
|
---@nodiscard
|
|
|
|
---@return number total_avail_rate
|
|
|
|
function public.get_sna_rate()
|
|
|
|
local total_avail_rate = 0
|
|
|
|
|
2023-07-15 17:16:36 +00:00
|
|
|
for i = 1, #self.snas do
|
|
|
|
local db = self.snas[i].get_db() ---@type sna_session_db
|
2023-07-08 20:57:13 +00:00
|
|
|
total_avail_rate = total_avail_rate + db.state.production_rate
|
|
|
|
end
|
|
|
|
|
|
|
|
return total_avail_rate
|
|
|
|
end
|
|
|
|
|
2022-05-11 16:31:19 +00:00
|
|
|
-- get the annunciator status
|
2023-02-25 04:36:16 +00:00
|
|
|
---@nodiscard
|
2022-05-31 19:36:17 +00:00
|
|
|
function public.get_annunciator() return self.db.annunciator end
|
2022-05-09 13:35:39 +00:00
|
|
|
|
2022-11-26 21:18:31 +00:00
|
|
|
-- get the alarm states
|
2023-02-25 04:36:16 +00:00
|
|
|
---@nodiscard
|
2022-11-26 21:18:31 +00:00
|
|
|
function public.get_alarms() return self.db.alarm_states end
|
|
|
|
|
2023-01-03 21:50:31 +00:00
|
|
|
-- get information required for automatic reactor control
|
2023-02-25 04:36:16 +00:00
|
|
|
---@nodiscard
|
2023-01-03 21:50:31 +00:00
|
|
|
function public.get_control_inf() return self.db.control end
|
|
|
|
|
2023-02-03 03:58:51 +00:00
|
|
|
-- get unit state
|
2023-02-25 04:36:16 +00:00
|
|
|
---@nodiscard
|
2022-12-05 21:17:09 +00:00
|
|
|
function public.get_state()
|
2023-07-06 05:36:06 +00:00
|
|
|
return {
|
|
|
|
self.status_text[1],
|
|
|
|
self.status_text[2],
|
|
|
|
self.db.control.ready,
|
|
|
|
self.db.control.degraded,
|
2023-07-08 20:57:13 +00:00
|
|
|
self.db.control.waste_mode,
|
2023-07-06 05:36:06 +00:00
|
|
|
self.waste_product
|
|
|
|
}
|
2022-12-05 21:17:09 +00:00
|
|
|
end
|
|
|
|
|
2023-08-20 03:24:20 +00:00
|
|
|
-- get valve states
|
|
|
|
---@nodiscard
|
|
|
|
function public.get_valves()
|
|
|
|
local v = self.valves
|
|
|
|
return {
|
|
|
|
v.waste_pu.check(),
|
|
|
|
v.waste_sna.check(),
|
|
|
|
v.waste_po.check(),
|
|
|
|
v.waste_sps.check(),
|
|
|
|
v.emer_cool.check()
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
2022-08-28 16:57:36 +00:00
|
|
|
-- get the reactor ID
|
2023-02-25 04:36:16 +00:00
|
|
|
---@nodiscard
|
2022-08-28 16:57:36 +00:00
|
|
|
function public.get_id() return self.r_id end
|
|
|
|
|
2023-02-07 05:32:50 +00:00
|
|
|
--#endregion
|
|
|
|
|
2022-05-09 13:35:39 +00:00
|
|
|
return public
|
|
|
|
end
|
|
|
|
|
|
|
|
return unit
|