#102 #21 auto control loop with induction matrix and unit alarm checks and handling

This commit is contained in:
Mikayla Fischler
2023-02-07 00:32:50 -05:00
parent 1100051585
commit 1d3a1672c8
15 changed files with 327 additions and 133 deletions

View File

@@ -12,13 +12,14 @@ local PROCESS = types.PROCESS
-- 2856 FE per blade per 1 mB, 285.6 FE per blade per 0.1 mB (minimum)
local POWER_PER_BLADE = util.joules_to_fe(7140)
local MAX_CHARGE = 0.99
local HIGH_CHARGE = 1.0
local RE_ENABLE_CHARGE = 0.95
local AUTO_SCRAM = {
NONE = 0,
MATRIX_DC = 1,
MATRIX_FILL = 2
MATRIX_FILL = 2,
CRIT_ALARM = 3
}
local charge_Kp = 1.0
@@ -46,6 +47,7 @@ function facility.new(num_reactors, cooling_conf)
units_ready = false,
mode = PROCESS.INACTIVE,
last_mode = PROCESS.INACTIVE,
return_mode = PROCESS.INACTIVE,
mode_set = PROCESS.SIMPLE,
max_burn_combined = 0.0, -- maximum burn rate to clamp at
burn_target = 0.1, -- burn rate target for aggregate burn mode
@@ -53,6 +55,7 @@ function facility.new(num_reactors, cooling_conf)
gen_rate_target = 0, -- FE/t charge rate target
group_map = { 0, 0, 0, 0 }, -- units -> group IDs
prio_defs = { {}, {}, {}, {} }, -- priority definitions (each level is a table of units)
at_max_burn = false,
ascram = false,
ascram_reason = AUTO_SCRAM.NONE,
-- closed loop control
@@ -102,6 +105,7 @@ function facility.new(num_reactors, cooling_conf)
-- 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
---@return integer unallocated
local function _allocate_burn_rate(burn_rate, ramp)
local unallocated = math.floor(burn_rate * 10)
@@ -117,32 +121,38 @@ function facility.new(num_reactors, cooling_conf)
splits[#units] = splits[#units] + (unallocated % #units)
-- go through all reactor units in this group
for u = 1, #units do
local ctl = units[u].get_control_inf() ---@type unit_control
for id = 1, #units do
local u = units[id] ---@type reactor_unit
local ctl = u.get_control_inf()
local lim_br10 = u.a_get_effective_limit()
local last = ctl.br10
if splits[u] <= ctl.lim_br10 then
ctl.br10 = splits[u]
if splits[id] <= lim_br10 then
ctl.br10 = splits[id]
else
ctl.br10 = ctl.lim_br10
ctl.br10 = lim_br10
if u < #units then
local remaining = #units - u
if id < #units then
local remaining = #units - id
split = math.floor(unallocated / remaining)
for x = (u + 1), #units do splits[x] = split end
for x = (id + 1), #units do splits[x] = split end
splits[#units] = splits[#units] + (unallocated % remaining)
end
end
unallocated = unallocated - ctl.br10
unallocated = math.max(0, unallocated - ctl.br10)
if last ~= ctl.br10 then units[u].a_commit_br10(ramp) end
if last ~= ctl.br10 then
log.debug("unit " .. id .. ": set to " .. ctl.br10 .. " (was " .. last .. ")")
u.a_commit_br10(ramp)
end
end
end
-- stop if fully allocated
if unallocated <= 0 then break end
end
return unallocated
end
-- PUBLIC FUNCTIONS --
@@ -215,10 +225,18 @@ function facility.new(num_reactors, cooling_conf)
local now = util.time_s()
local state_changed = self.mode ~= self.last_mode
local next_mode = self.mode
-- once auto control is started, sort the priority sublists by limits
if state_changed then
self.saturated = false
log.debug("FAC: state changed from " .. self.last_mode .. " to " .. self.mode)
if self.last_mode == PROCESS.INACTIVE then
---@todo change this to be a reset button
if self.mode ~= PROCESS.MATRIX_FAULT_IDLE then self.ascram = false end
local blade_count = 0
self.max_burn_combined = 0.0
@@ -259,27 +277,31 @@ function facility.new(num_reactors, cooling_conf)
self.initial_ramp = false
end
if self.mode == PROCESS.INACTIVE then
-- check if we are ready to start when that time comes
self.units_ready = true
for i = 1, #self.prio_defs do
for _, u in pairs(self.prio_defs[i]) do
self.units_ready = self.units_ready and u.get_control_inf().ready
end
-- update unit ready state
self.units_ready = true
for i = 1, #self.prio_defs do
for _, u in pairs(self.prio_defs[i]) do
self.units_ready = self.units_ready and u.get_control_inf().ready
end
end
-- perform mode-specific operations
if self.mode == PROCESS.INACTIVE then
if self.units_ready then
self.status_text = { "IDLE", "control disengaged" }
else
self.status_text = { "NOT READY", "assigned units not ready" }
end
elseif self.mode == PROCESS.SIMPLE then
-- run units at their last configured set point
-- run units at their limits
if state_changed then
self.time_start = now
---@todo will still need to ramp?
self.saturated = true
self.status_text = { "MONITORED MODE", "running reactors at limit" }
log.debug(util.c("FAC: SIMPLE mode first call completed"))
end
_allocate_burn_rate(self.max_burn_combined, true)
elseif self.mode == PROCESS.BURN_RATE then
-- a total aggregate burn rate
if state_changed then
@@ -294,7 +316,8 @@ function facility.new(num_reactors, cooling_conf)
end
if not self.waiting_on_ramp then
_allocate_burn_rate(self.burn_target, self.initial_ramp)
local unallocated = _allocate_burn_rate(self.burn_target, true)
self.saturated = self.burn_target == self.max_burn_combined or unallocated > 0
if self.initial_ramp then
self.status_text = { "BURN RATE MODE", "ramping reactors" }
@@ -397,16 +420,27 @@ function facility.new(num_reactors, cooling_conf)
_allocate_burn_rate(sp_c, false)
end
elseif self.mode == PROCESS.MATRIX_FAULT_IDLE then
-- exceeded charge, wait until condition clears
if self.ascram_reason == AUTO_SCRAM.NONE then
next_mode = self.return_mode
log.info("FAC: exiting matrix fault idle state due to fault resolution")
elseif self.ascram_reason == AUTO_SCRAM.CRIT_ALARM then
next_mode = PROCESS.INACTIVE
log.info("FAC: exiting matrix fault idle state due to critical unit alarm")
end
elseif self.mode == PROCESS.UNIT_ALARM_IDLE then
-- do nothing, wait for user to confirm (stop and reset)
elseif self.mode ~= PROCESS.INACTIVE then
log.error(util.c("FAC: unsupported process mode ", self.mode, ", switching to inactive"))
self.mode = PROCESS.INACTIVE
next_mode = PROCESS.INACTIVE
end
------------------------------
-- Evaluate Automatic SCRAM --
------------------------------
if self.mode ~= PROCESS.INACTIVE then
if (self.mode ~= PROCESS.INACTIVE) and (self.mode ~= PROCESS.UNIT_ALARM_IDLE) then
local scram = false
if self.induction[1] ~= nil then
@@ -415,37 +449,93 @@ function facility.new(num_reactors, cooling_conf)
if self.ascram_reason == AUTO_SCRAM.MATRIX_DC then
self.ascram_reason = AUTO_SCRAM.NONE
log.info("FAC: cleared automatic SCRAM trip due to prior induction matrix disconnect")
end
if (db.tanks.energy_fill > MAX_CHARGE) or
if (db.tanks.energy_fill >= HIGH_CHARGE) or
(self.ascram_reason == AUTO_SCRAM.MATRIX_FILL and db.tanks.energy_fill > RE_ENABLE_CHARGE) then
scram = true
if self.mode ~= PROCESS.MATRIX_FAULT_IDLE then
self.return_mode = self.mode
next_mode = PROCESS.MATRIX_FAULT_IDLE
end
if self.ascram_reason == AUTO_SCRAM.NONE then
self.ascram_reason = AUTO_SCRAM.MATRIX_FILL
end
elseif self.ascram_reason == AUTO_SCRAM.MATRIX_FILL then
log.info("FAC: charge state of induction matrix entered acceptable range <= " .. (RE_ENABLE_CHARGE * 100) .. "%")
self.ascram_reason = AUTO_SCRAM.NONE
end
for i = 1, #self.units do
local u = self.units[i] ---@type reactor_unit
if u.has_critical_alarm() then
scram = true
if self.ascram_reason == AUTO_SCRAM.NONE then
self.ascram_reason = AUTO_SCRAM.CRIT_ALARM
end
next_mode = PROCESS.UNIT_ALARM_IDLE
log.info("FAC: emergency exit of process control due to critical unit alarm")
break
end
end
else
scram = true
if self.mode ~= PROCESS.MATRIX_FAULT_IDLE then
self.return_mode = self.mode
next_mode = PROCESS.MATRIX_FAULT_IDLE
end
if self.ascram_reason == AUTO_SCRAM.NONE then
self.ascram_reason = AUTO_SCRAM.MATRIX_DC
end
end
-- SCRAM all units
if not self.ascram and scram then
if (not self.ascram) and scram then
for i = 1, #self.prio_defs do
for _, u in pairs(self.prio_defs[i]) do
u.a_scram()
end
end
self.ascram = true
if self.ascram_reason == AUTO_SCRAM.MATRIX_DC then
log.info("FAC: automatic SCRAM due to induction matrix disconnection")
self.status_text = { "AUTOMATIC SCRAM", "induction matrix disconnected" }
elseif self.ascram_reason == AUTO_SCRAM.MATRIX_FILL then
log.info("FAC: automatic SCRAM due to induction matrix high charge")
self.status_text = { "AUTOMATIC SCRAM", "induction matrix fill high" }
elseif self.ascram_reason == AUTO_SCRAM.CRIT_ALARM then
log.info("FAC: automatic SCRAM due to critical unit alarm")
self.status_text = { "AUTOMATIC SCRAM", "critical unit alarm tripped" }
else
log.error(util.c("FAC: automatic SCRAM reason (", self.ascram_reason, ") not set to a known value"))
end
end
self.ascram = scram
-- clear PLC SCRAM if we should
if not self.ascram then
self.ascram_reason = AUTO_SCRAM.NONE
for i = 1, #self.units do
local u = self.units[i] ---@type reactor_unit
u.a_cond_rps_reset()
end
end
end
-- update last mode
-- update last mode and set next mode
self.last_mode = self.mode
self.mode = next_mode
end
-- call the update function of all units in the facility
@@ -580,6 +670,7 @@ function facility.new(num_reactors, cooling_conf)
self.units_ready,
self.mode,
self.waiting_on_ramp,
self.at_max_burn or self.saturated,
self.ascram,
self.status_text[1],
self.status_text[2],

View File

@@ -28,7 +28,8 @@ local PLC_S_CMDS = {
SCRAM = 1,
ASCRAM = 2,
ENABLE = 3,
RPS_RESET = 4
RPS_RESET = 4,
RPS_AUTO_RESET = 5
}
local PLC_S_DATA = {
@@ -445,18 +446,29 @@ function plc.new_session(id, for_reactor, in_queue, out_queue)
cmd = UNIT_COMMANDS.RESET_RPS,
ack = ack
})
elseif pkt.type == RPLC_TYPES.RPS_AUTO_RESET then
-- RPS auto control reset acknowledgement
local ack = _get_ack(pkt)
if ack then
self.auto_scram = false
else
log.debug(log_header .. "RPS auto reset failed")
end
elseif pkt.type == RPLC_TYPES.AUTO_BURN_RATE then
if pkt.length == 1 then
local ack = pkt.data[1]
self.acks.burn_rate = ack ~= PLC_AUTO_ACK.FAIL
---@todo implement error handling here
if ack == PLC_AUTO_ACK.FAIL then
elseif ack == PLC_AUTO_ACK.DIRECT_SET_OK then
elseif ack == PLC_AUTO_ACK.RAMP_SET_OK then
elseif ack == PLC_AUTO_ACK.ZERO_DIS_OK then
self.acks.burn_rate = false
log.debug(log_header .. "RPLC automatic burn rate set fail")
elseif ack == PLC_AUTO_ACK.DIRECT_SET_OK or ack == PLC_AUTO_ACK.RAMP_SET_OK or ack == PLC_AUTO_ACK.ZERO_DIS_OK then
self.acks.burn_rate = true
elseif ack == PLC_AUTO_ACK.ZERO_DIS_WAIT then
self.acks.burn_rate = false
log.debug(log_header .. "RPLC automatic burn rate too soon to disable at 0 mB/t")
else
self.acks.burn_rate = false
log.debug(log_header .. "RPLC automatic burn rate ack unknown")
end
else
log.debug(log_header .. "RPLC automatic burn rate ack packet length mismatch")
@@ -614,6 +626,10 @@ function plc.new_session(id, for_reactor, in_queue, out_queue)
self.acks.rps_reset = false
self.retry_times.rps_reset_req = util.time() + INITIAL_WAIT
_send(RPLC_TYPES.RPS_RESET, {})
elseif cmd == PLC_S_CMDS.RPS_AUTO_RESET then
if self.auto_scram or self.sDB.rps_status.timeout then
_send(RPLC_TYPES.RPS_AUTO_RESET, {})
end
else
log.warning(log_header .. "unsupported command received in in_queue (this is a bug)")
end

View File

@@ -375,55 +375,6 @@ function unit.new(for_reactor, num_boilers, num_turbines)
--#endregion
-- AUTO CONTROL --
--#region
-- engage automatic control
function public.a_engage()
self.db.annunciator.AutoControl = true
if self.plc_i ~= nil then
self.plc_i.auto_lock(true)
end
end
-- disengage automatic control
function public.a_disengage()
self.db.annunciator.AutoControl = false
if self.plc_i ~= nil then
self.plc_i.auto_lock(false)
self.db.control.br10 = 0
end
end
-- set the automatic burn rate based on the last set br10
---@param ramp boolean true to ramp to rate, false to set right away
function public.a_commit_br10(ramp)
if self.db.annunciator.AutoControl then
if self.plc_i ~= nil then
self.plc_i.auto_set_burn(self.db.control.br10 / 10, ramp)
if ramp then self.ramp_target_br10 = self.db.control.br10 end
end
end
end
-- check if ramping is complete (burn rate is same as target)
---@return boolean complete
function public.a_ramp_complete()
if self.plc_i ~= nil then
return self.plc_i.is_ramp_complete() or (self.plc_i.get_status().act_burn_rate == 0 and self.db.control.br10 == 0)
else return true end
end
-- perform an automatic SCRAM
function public.a_scram()
if self.plc_s ~= nil then
self.plc_s.in_queue.push_command(PLC_S_CMDS.ASCRAM)
end
end
--#endregion
-- UPDATE SESSION --
-- update (iterate) this unit
@@ -444,6 +395,32 @@ function unit.new(for_reactor, num_boilers, num_turbines)
-- 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()
@@ -457,7 +434,82 @@ function unit.new(for_reactor, num_boilers, num_turbines)
logic.update_status_text(self)
end
-- AUTO CONTROL OPERATIONS --
--#region
-- engage automatic control
function public.a_engage()
self.db.annunciator.AutoControl = true
if self.plc_i ~= nil then
self.plc_i.auto_lock(true)
end
end
-- disengage automatic control
function public.a_disengage()
self.db.annunciator.AutoControl = false
if self.plc_i ~= nil then
self.plc_i.auto_lock(false)
self.db.control.br10 = 0
end
end
-- get the actual limit of this unit
--
-- if it is degraded or not ready, the limit will be 0
---@return integer lim_br10
function public.a_get_effective_limit()
if not self.db.control.ready or self.db.control.degraded or self.plc_cache.rps_trip then
self.db.control.br10 = 0
return 0
else
return self.db.control.lim_br10
end
end
-- set the automatic burn rate based on the last set br10
---@param ramp boolean true to ramp to rate, false to set right away
function public.a_commit_br10(ramp)
if self.db.annunciator.AutoControl then
if self.plc_i ~= nil then
self.plc_i.auto_set_burn(self.db.control.br10 / 10, ramp)
if ramp then self.ramp_target_br10 = self.db.control.br10 end
end
end
end
-- check if ramping is complete (burn rate is same as target)
---@return boolean complete
function public.a_ramp_complete()
if self.plc_i ~= nil then
return self.plc_i.is_ramp_complete() or
(self.plc_i.get_status().act_burn_rate == 0 and self.db.control.br10 == 0) or
public.a_get_effective_limit() == 0
else return true end
end
-- perform an automatic SCRAM
function public.a_scram()
if self.plc_s ~= nil then
self.plc_s.in_queue.push_command(PLC_S_CMDS.ASCRAM)
end
end
-- queue a command to clear timeout/auto-scram if set
function public.a_cond_rps_reset()
if self.plc_s ~= nil and self.plc_i ~= nil then
local rps = self.plc_i.get_rps()
if rps.timeout or rps.automatic then
self.plc_s.in_queue.push_command(PLC_S_CMDS.RPS_AUTO_RESET)
end
end
end
--#endregion
-- OPERATIONS --
--#region
-- queue a command to SCRAM the reactor
function public.scram()
@@ -537,7 +589,21 @@ function unit.new(for_reactor, num_boilers, num_turbines)
end
end
--#endregion
-- READ STATES/PROPERTIES --
--#region
-- check if a critical alarm is tripped
function public.has_critical_alarm()
for _, data in pairs(self.alarms) do
if data.tier == PRIO.CRITICAL and (data.state == AISTATE.TRIPPED or data.state == AISTATE.ACKED) then
return true
end
end
return false
end
-- get build properties of all machines
function public.get_build()
@@ -622,6 +688,8 @@ function unit.new(for_reactor, num_boilers, num_turbines)
-- get the reactor ID
function public.get_id() return self.r_id end
--#endregion
return public
end

View File

@@ -516,11 +516,7 @@ function logic.update_status_text(self)
elseif self.db.annunciator.WasteLineOcclusion then
self.status_text[2] = "insufficient waste output rate"
elseif (util.time_ms() - self.last_rate_change_ms) <= self.defs.FLOW_STABILITY_DELAY_MS then
if self.num_turbines > 1 then
self.status_text[2] = "turbines spinning up"
else
self.status_text[2] = "turbine spinning up"
end
self.status_text[2] = "awaiting flow stability"
else
self.status_text[2] = "system nominal"
end

View File

@@ -14,7 +14,7 @@ local svsessions = require("supervisor.session.svsessions")
local config = require("supervisor.config")
local supervisor = require("supervisor.supervisor")
local SUPERVISOR_VERSION = "beta-v0.9.13"
local SUPERVISOR_VERSION = "beta-v0.9.14"
local print = util.print
local println = util.println