cc-mek-scada/supervisor/unit.lua

461 lines
16 KiB
Lua
Raw Normal View History

2022-05-18 17:28:43 +00:00
local types = require "scada-common.types"
local util = require "scada-common.util"
local unit = {}
2022-05-21 16:24:43 +00:00
local TRI_FAIL = types.TRI_FAIL
local DUMPING_MODE = types.DUMPING_MODE
local DT_KEYS = {
ReactorTemp = "RTP",
ReactorFuel = "RFL",
ReactorWaste = "RWS",
ReactorCCool = "RCC",
ReactorHCool = "RHC",
BoilerWater = "BWR",
BoilerSteam = "BST",
BoilerCCool = "BCC",
BoilerHCool = "BHC",
TurbineSteam = "TST"
2022-05-18 17:28:43 +00:00
}
2022-05-11 16:31:19 +00:00
-- create a new reactor unit
2022-05-18 17:28:43 +00:00
---@param for_reactor integer reactor unit number
---@param num_boilers integer number of boilers expected
---@param num_turbines integer number of turbines expected
unit.new = function (for_reactor, num_boilers, num_turbines)
local self = {
r_id = for_reactor,
2022-05-18 17:28:43 +00:00
plc_s = nil, ---@class plc_session
counts = { boilers = num_boilers, turbines = num_turbines },
turbines = {},
boilers = {},
redstone = {},
2022-05-21 16:24:43 +00:00
deltas = {},
db = {
2022-05-11 16:31:19 +00:00
---@class annunciator
annunciator = {
-- reactor
PLCOnline = false,
ReactorTrip = false,
ManualReactorTrip = false,
RCPTrip = false,
RCSFlowLow = false,
ReactorTempHigh = false,
ReactorHighDeltaT = false,
2022-05-21 16:24:43 +00:00
FuelInputRateLow = false,
WasteLineOcclusion = false,
HighStartupRate = false,
-- boiler
2022-05-18 17:28:43 +00:00
BoilerOnline = TRI_FAIL.OK,
2022-05-21 16:24:43 +00:00
HeatingRateLow = {},
2022-05-18 17:28:43 +00:00
BoilRateMismatch = false,
CoolantFeedMismatch = false,
-- turbine
2022-05-18 17:28:43 +00:00
TurbineOnline = TRI_FAIL.OK,
SteamFeedMismatch = false,
2022-05-21 16:24:43 +00:00
MaxWaterReturnFeed = false,
SteamDumpOpen = {},
TurbineOverSpeed = {},
TurbineTrip = {}
}
}
}
2022-05-21 16:24:43 +00:00
-- init boiler table fields
for _ = 1, self.num_boilers do
table.insert(self.db.annunciator.HeatingRateLow, false)
end
-- init turbine table fields
for _ = 1, self.num_turbines do
table.insert(self.db.annunciator.SteamDumpOpen, TRI_FAIL.OK)
table.insert(self.db.annunciator.TurbineOverSpeed, false)
table.insert(self.db.annunciator.TurbineTrip, false)
end
2022-05-11 16:31:19 +00:00
---@class reactor_unit
local public = {}
-- PRIVATE 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
local _compute_dt = function (key, value)
if self.deltas[key] then
local data = self.deltas[key]
data.dt = (value - data.last_v) / (util.time_s() - data.last_t)
data.last_v = value
data.last_t = util.time_s()
else
self.deltas[key] = {
last_t = util.time_s(),
last_v = value,
dt = 0.0
}
end
end
-- clear a delta
---@param key string value key
local _reset_dt = function (key)
self.deltas[key] = nil
end
-- get the delta t of a value
---@param key string value key
---@return number
local _get_dt = function (key)
if self.deltas[key] then
return self.deltas[key].dt
else
return 0.0
end
end
-- update all delta computations
local _dt__compute_all = function ()
if self.plc_s ~= nil then
local plc_db = self.plc_s.get_db()
-- @todo Meknaism 10.1+ will change fuel/waste to need _amnt
_compute_dt(DT_KEYS.ReactorTemp, plc_db.mek_status.temp)
_compute_dt(DT_KEYS.ReactorFuel, plc_db.mek_status.fuel)
_compute_dt(DT_KEYS.ReactorWaste, plc_db.mek_status.waste)
_compute_dt(DT_KEYS.ReactorCCool, plc_db.mek_status.ccool_amnt)
_compute_dt(DT_KEYS.ReactorHCool, plc_db.mek_status.hcool_amnt)
end
for i = 1, #self.boilers do
local boiler = self.boilers[i] ---@type unit_session
local db = boiler.get_db() ---@type boiler_session_db
-- @todo Meknaism 10.1+ will change water/steam to need .amount
_compute_dt(DT_KEYS.BoilerWater .. boiler.get_device_idx(), db.tanks.water)
_compute_dt(DT_KEYS.BoilerSteam .. boiler.get_device_idx(), db.tanks.steam)
_compute_dt(DT_KEYS.BoilerCCool .. boiler.get_device_idx(), db.tanks.ccool.amount)
_compute_dt(DT_KEYS.BoilerHCool .. boiler.get_device_idx(), db.tanks.hcool.amount)
end
for i = 1, #self.turbines do
local turbine = self.turbines[i] ---@type unit_session
local db = turbine.get_db() ---@type turbine_session_db
_compute_dt(DT_KEYS.TurbineSteam .. turbine.get_device_idx(), db.tanks.steam)
-- @todo Mekanism 10.1+ needed
-- _compute_dt(DT_KEYS.TurbinePower .. turbine.get_device_idx(), db.?)
end
end
2022-05-11 16:31:19 +00:00
-- update the annunciator
local _update_annunciator = function ()
2022-05-21 16:24:43 +00:00
-- update deltas
_dt__compute_all()
-------------
-- REACTOR --
-------------
2022-05-18 17:28:43 +00:00
-- check PLC status
2022-05-11 16:31:19 +00:00
self.db.annunciator.PLCOnline = (self.plc_s ~= nil) and (self.plc_s.open)
2022-05-18 17:28:43 +00:00
if self.plc_s ~= nil then
local plc_db = self.plc_s.get_db()
-- update annunciator
self.db.annunciator.ReactorTrip = plc_db.rps_tripped
self.db.annunciator.ManualReactorTrip = plc_db.rps_trip_cause == types.rps_status_t.manual
self.db.annunciator.RCPTrip = plc_db.rps_tripped and (plc_db.rps_status.ex_hcool or plc_db.rps_status.no_cool)
self.db.annunciator.RCSFlowLow = plc_db.mek_status.ccool_fill < 0.75 or plc_db.mek_status.hcool_fill > 0.25
self.db.annunciator.ReactorTempHigh = plc_db.mek_status.temp > 1000
2022-05-21 16:24:43 +00:00
self.db.annunciator.ReactorHighDeltaT = _get_dt(DT_KEYS.ReactorTemp) > 100
self.db.annunciator.FuelInputRateLow = _get_dt(DT_KEYS.ReactorFuel) < 0.0 or plc_db.mek_status.fuel_fill <= 0.01
self.db.annunciator.WasteLineOcclusion = _get_dt(DT_KEYS.ReactorWaste) > 0.0 or plc_db.mek_status.waste_fill >= 0.99
2022-05-18 17:28:43 +00:00
-- @todo this is dependent on setup, i.e. how much coolant is buffered and the turbine setup
self.db.annunciator.HighStartupRate = not plc_db.control_state and plc_db.mek_status.burn_rate > 40
end
-------------
-- BOILERS --
-------------
-- check boiler online status
local connected_boilers = #self.boilers
if connected_boilers == 0 and self.num_boilers > 0 then
self.db.annunciator.BoilerOnline = TRI_FAIL.FULL
elseif connected_boilers > 0 and connected_boilers ~= self.num_boilers then
self.db.annunciator.BoilerOnline = TRI_FAIL.PARTIAL
else
self.db.annunciator.BoilerOnline = TRI_FAIL.OK
end
2022-05-21 16:24:43 +00:00
-- compute aggregated statistics
2022-05-18 17:28:43 +00:00
local total_boil_rate = 0.0
2022-05-21 16:24:43 +00:00
local boiler_steam_dt_sum = 0.0
local boiler_water_dt_sum = 0.0
2022-05-18 17:28:43 +00:00
for i = 1, #self.boilers do
local boiler = self.boilers[i].get_db() ---@type boiler_session_db
2022-05-21 16:24:43 +00:00
total_boil_rate = total_boil_rate + boiler.state.boil_rate
boiler_steam_dt_sum = _get_dt(DT_KEYS.BoilerSteam .. self.boilers[i].get_device_idx())
boiler_water_dt_sum = _get_dt(DT_KEYS.BoilerWater .. self.boilers[i].get_device_idx())
2022-05-18 17:28:43 +00:00
end
2022-05-21 16:24:43 +00:00
-- check heating rate low
2022-05-18 17:28:43 +00:00
if self.plc_s ~= nil then
2022-05-21 16:24:43 +00:00
-- check for inactive boilers while reactor is active
for i = 1, #self.boilers do
local boiler = self.boilers[i] ---@type unit_session
local idx = boiler.get_device_idx()
local db = boiler.get_db() ---@type boiler_session_db
if self.plc_s.get_db().mek_status.status then
self.db.annunciator.HeatingRateLow[idx] = db.state.boil_rate == 0
else
self.db.annunciator.HeatingRateLow[idx] = false
end
end
-- check for rate mismatch
2022-05-18 17:28:43 +00:00
local expected_boil_rate = self.plc_s.get_db().mek_status.heating_rate / 10.0
self.db.annunciator.BoilRateMismatch = math.abs(expected_boil_rate - total_boil_rate) > 25.0
end
2022-05-21 16:24:43 +00:00
-- check coolant feed mismatch
local cfmismatch = false
for i = 1, #self.boilers do
local boiler = self.boilers[i] ---@type unit_session
local idx = boiler.get_device_idx()
local db = boiler.get_db() ---@type boiler_session_db
-- gaining heated coolant
cfmismatch = cfmismatch or _get_dt(DT_KEYS.BoilerHCool .. idx) > 0 or db.tanks.hcool_fill == 1
-- losing cooled coolant
cfmismatch = cfmismatch or _get_dt(DT_KEYS.BoilerCCool .. idx) < 0 or db.tanks.ccool_fill == 0
end
self.db.annunciator.CoolantFeedMismatch = cfmismatch
2022-05-18 17:28:43 +00:00
--------------
-- TURBINES --
--------------
-- check turbine online status
local connected_turbines = #self.turbines
if connected_turbines == 0 and self.num_turbines > 0 then
self.db.annunciator.TurbineOnline = TRI_FAIL.FULL
elseif connected_turbines > 0 and connected_turbines ~= self.num_turbines then
self.db.annunciator.TurbineOnline = TRI_FAIL.PARTIAL
else
self.db.annunciator.TurbineOnline = TRI_FAIL.OK
end
2022-05-21 16:24:43 +00:00
-- compute aggregated statistics
local total_flow_rate = 0
local total_input_rate = 0
local max_water_return_rate = 0
for i = 1, #self.turbines do
local turbine = self.turbines[i].get_db() ---@type turbine_session_db
total_flow_rate = total_flow_rate + turbine.state.flow_rate
total_input_rate = total_input_rate + turbine.state.steam_input_rate
max_water_return_rate = max_water_return_rate + turbine.build.max_water_output
end
-- check for steam feed mismatch and max return rate
local sfmismatch = math.abs(total_flow_rate - total_input_rate) > 10
sfmismatch = sfmismatch or boiler_steam_dt_sum > 0 or boiler_water_dt_sum < 0
self.db.annunciator.SteamFeedMismatch = sfmismatch
self.db.annunciator.MaxWaterReturnFeed = max_water_return_rate == total_flow_rate
-- check if steam dumps are open
for i = 1, #self.turbines do
local turbine = self.turbines[i] ---@type unit_session
local db = turbine.get_db() ---@type turbine_session_db
local idx = turbine.get_device_idx()
if db.state.dumping_mode == DUMPING_MODE.IDLE then
self.db.annunciator.SteamDumpOpen[idx] = TRI_FAIL.OK
elseif db.state.dumping_mode == DUMPING_MODE.DUMPING_EXCESS then
self.db.annunciator.SteamDumpOpen[idx] = TRI_FAIL.PARTIAL
else
self.db.annunciator.SteamDumpOpen[idx] = TRI_FAIL.FULL
end
end
-- check if turbines are at max speed but not keeping up
for i = 1, #self.turbines do
local turbine = self.turbines[i] ---@type unit_session
local db = turbine.get_db() ---@type turbine_session_db
local idx = turbine.get_device_idx()
self.db.annunciator.TurbineOverSpeed[idx] = (db.state.flow_rate == db.build.max_flow_rate) and (_get_dt(DT_KEYS.TurbineSteam .. idx) > 0)
end
2022-05-18 17:28:43 +00:00
--[[
Turbine Trip
2022-05-21 16:24:43 +00:00
a turbine trip is when the turbine stops, which means we are no longer receiving water and lose the ability to cool.
2022-05-18 17:28:43 +00:00
this can be identified by these conditions:
- the current flow rate is 0 mB/t and it should not be
- can initially catch this by detecting a 0 flow rate with a non-zero input rate, but eventually the steam will fill up
- can later identified by presence of steam in tank with a 0 flow rate
]]--
2022-05-21 16:24:43 +00:00
for i = 1, #self.turbines do
local turbine = self.turbines[i] ---@type unit_session
local db = turbine.get_db() ---@type turbine_session_db
local has_steam = db.state.steam_input_rate > 0 or db.tanks.steam_fill > 0.01
self.db.annunciator.TurbineTrip[turbine.get_device_idx()] = has_steam and db.state.flow_rate == 0
end
2022-05-18 17:28:43 +00:00
end
-- unlink disconnected units
---@param sessions table
local _unlink_disconnected_units = function (sessions)
util.filter_table(sessions, function (u) return u.is_connected() end)
2022-05-11 16:31:19 +00:00
end
-- PUBLIC FUNCTIONS --
-- link the PLC
---@param plc_session plc_session_struct
public.link_plc_session = function (plc_session)
self.plc_s = plc_session
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)
end
2022-05-18 17:28:43 +00:00
-- link a turbine RTU session
---@param turbine unit_session
public.add_turbine = function (turbine)
2022-05-21 16:24:43 +00:00
if #self.turbines < self.num_turbines and turbine.get_device_idx() <= self.num_turbines then
table.insert(self.turbines, turbine)
-- reset deltas
_reset_dt(DT_KEYS.TurbineSteam .. turbine.get_device_idx())
_reset_dt(DT_KEYS.TurbinePower .. turbine.get_device_idx())
return true
else
return false
end
end
2022-05-18 17:28:43 +00:00
-- link a boiler RTU session
---@param boiler unit_session
2022-05-10 16:01:56 +00:00
public.add_boiler = function (boiler)
2022-05-21 16:24:43 +00:00
if #self.boilers < self.num_boilers and boiler.get_device_idx() <= self.num_boilers then
table.insert(self.boilers, boiler)
-- reset deltas
_reset_dt(DT_KEYS.BoilerWater .. boiler.get_device_idx())
_reset_dt(DT_KEYS.BoilerSteam .. boiler.get_device_idx())
_reset_dt(DT_KEYS.BoilerCCool .. boiler.get_device_idx())
_reset_dt(DT_KEYS.BoilerHCool .. boiler.get_device_idx())
return true
else
return false
end
end
2022-05-11 16:31:19 +00:00
-- link a redstone RTU capability
public.add_redstone = function (field, accessor)
-- ensure field exists
2022-05-10 16:01:56 +00:00
if self.redstone[field] == nil then
self.redstone[field] = {}
end
-- insert into list
2022-05-10 16:01:56 +00:00
table.insert(self.redstone[field], accessor)
end
2022-05-21 16:24:43 +00:00
-- update (iterate) this unit
public.update = function ()
-- unlink PLC if session was closed
if not self.plc_s.open then
self.plc_s = nil
end
2022-05-18 17:28:43 +00:00
-- unlink RTU unit sessions if they are closed
_unlink_disconnected_units(self.boilers)
_unlink_disconnected_units(self.turbines)
-- update annunciator logic
_update_annunciator()
end
2022-05-21 16:24:43 +00:00
-- get build properties of all machines
public.get_build = function ()
local build = {}
if self.plc_s ~= nil then
build.reactor = self.plc_s.get_struct()
end
build.boilers = {}
for i = 1, #self.boilers do
table.insert(build.boilers, self.boilers[i].get_db().build)
end
build.turbines = {}
for i = 1, #self.turbines do
table.insert(build.turbines, self.turbines[i].get_db().build)
end
return build
end
-- get reactor status
public.get_reactor_status = function ()
local status = {}
if self.plc_s ~= nil then
local reactor = self.plc_s
status.mek = reactor.get_status()
status.rps = reactor.get_rps()
status.general = reactor.get_general_status()
end
return status
end
-- get RTU statuses
public.get_rtu_statuses = function ()
local status = {}
-- status of boilers (including tanks)
status.boilers = {}
for i = 1, #self.boilers do
table.insert(status.boilers, {
state = self.boilers[i].get_db().state,
tanks = self.boilers[i].get_db().tanks,
})
end
-- status of turbines (including tanks)
status.turbines = {}
for i = 1, #self.turbines do
table.insert(status.turbines, {
state = self.turbines[i].get_db().state,
tanks = self.turbines[i].get_db().tanks,
})
end
return status
end
2022-05-11 16:31:19 +00:00
-- get the annunciator status
public.get_annunciator = function () return self.db.annunciator end
return public
end
return unit