PLC state code cleanup and bugfixes

This commit is contained in:
Mikayla Fischler 2022-04-05 09:41:06 -04:00
parent 02763c9cb3
commit 13b0fcf65f
2 changed files with 48 additions and 43 deletions

View File

@ -495,9 +495,8 @@ function comms_init(id, modem, local_port, server_port, reactor, iss)
end end
-- send live status information -- send live status information
-- control_state : acknowledged control state from supervisor -- overridden : if ISS force disabled reactor
-- overridden : if ISS force disabled reactor local send_status = function (overridden)
local send_status = function (control_state, overridden)
local mek_data = nil local mek_data = nil
if _update_status_cache() then if _update_status_cache() then
@ -508,7 +507,7 @@ function comms_init(id, modem, local_port, server_port, reactor, iss)
id = self.id, id = self.id,
type = RPLC_TYPES.STATUS, type = RPLC_TYPES.STATUS,
timestamp = os.time(), timestamp = os.time(),
control_state = control_state, control_state = ~self.scrammed,
overridden = overridden, overridden = overridden,
heating_rate = self.reactor.getHeatingRate(), heating_rate = self.reactor.getHeatingRate(),
mek_data = mek_data mek_data = mek_data

View File

@ -22,28 +22,38 @@ ppm.mount_all()
local reactor = ppm.get_device("fissionReactor") local reactor = ppm.get_device("fissionReactor")
local modem = ppm.get_device("modem") local modem = ppm.get_device("modem")
local init_ok = true local plc_state = {
local control_degraded = { degraded = false, no_reactor = false, no_modem = false } init_ok = true,
scram = true, -- treated as latching e-stop, all conditions must be OK to set false
degraded = false,
no_reactor = false,
no_modem = false
}
-- we need a reactor and a modem -- we need a reactor and a modem
if reactor == nil then if reactor == nil then
print_ts("Fission reactor not found. Running in a degraded state...\n"); print_ts("Fission reactor not found. Running in a degraded state...\n");
log._warning("no reactor on startup") log._warning("no reactor on startup")
init_ok = false plc_state.init_ok = false
control_degraded.degraded = true plc_state.degraded = true
control_degraded.no_reactor = true plc_state.no_reactor = true
end end
if modem == nil then if modem == nil then
print_ts("No modem found. Disabling reactor and running in a degraded state...\n") if reactor ~= nil then
print_ts("No modem found. Disabling reactor and running in a degraded state...\n")
reactor.scram()
else
print_ts("No modem found. Running in a degraded state...\n")
end
log._warning("no modem on startup") log._warning("no modem on startup")
reactor.scram() plc_state.init_ok = false
init_ok = false plc_state.degraded = true
control_degraded.degraded = true plc_state.no_modem = true
control_degraded.no_modem = true
end end
::init:: ::init::
if init_ok then if plc_state.init_ok then
-- just booting up, no fission allowed (neutrons stay put thanks) -- just booting up, no fission allowed (neutrons stay put thanks)
reactor.scram() reactor.scram()
@ -74,20 +84,16 @@ local LINK_TICKS = 20
-- start by linking -- start by linking
local ticks_to_update = LINK_TICKS local ticks_to_update = LINK_TICKS
-- runtime variables
local control_state = false
local reactor_scram = true -- treated as latching e-stop
-- event loop -- event loop
while true do while true do
local event, param1, param2, param3, param4, param5 = os.pullEventRaw() local event, param1, param2, param3, param4, param5 = os.pullEventRaw()
if init_ok then if plc_state.init_ok then
-- if we tried to SCRAM but failed, keep trying -- if we tried to SCRAM but failed, keep trying
-- if it disconnected, isPowered will return nil (and error logs will get spammed at 10Hz, so disable reporting) -- if it disconnected, isPowered will return nil (and error logs will get spammed at 10Hz, so disable reporting)
-- in that case, SCRAM won't be called until it reconnects (this is the expected use of this check) -- in that case, SCRAM won't be called until it reconnects (this is the expected use of this check)
ppm.disable_reporting() ppm.disable_reporting()
if reactor_scram and reactor.isPowered() then if plc_state.scram and reactor.isPowered() then
reactor.scram() reactor.scram()
end end
ppm.enable_reporting() ppm.enable_reporting()
@ -100,16 +106,16 @@ while true do
if device.type == "fissionReactor" then if device.type == "fissionReactor" then
print_ts("reactor disconnected!\n") print_ts("reactor disconnected!\n")
log._error("reactor disconnected!") log._error("reactor disconnected!")
control_degraded.no_reactor = true plc_state.no_reactor = true
-- send an alarm: plc_comms.send_alarm(ALARMS.PLC_PERI_DC) ? -- send an alarm: plc_comms.send_alarm(ALARMS.PLC_PERI_DC) ?
elseif device.type == "modem" then elseif device.type == "modem" then
print_ts("modem disconnected!\n") print_ts("modem disconnected!\n")
log._error("modem disconnected!") log._error("modem disconnected!")
control_degraded.no_modem = true plc_state.no_modem = true
if init_ok then if plc_state.init_ok then
-- try to scram reactor if it is still connected -- try to scram reactor if it is still connected
reactor_scram = true plc_state.scram = true
if reactor.scram() then if reactor.scram() then
print_ts("successful reactor SCRAM\n") print_ts("successful reactor SCRAM\n")
else else
@ -117,55 +123,55 @@ while true do
end end
end end
control_degraded.degraded = true plc_state.degraded = true
end end
elseif event == "peripheral" then elseif event == "peripheral" then
local device = ppm.mount(param1) local device = ppm.mount(param1)
if device.type == "fissionReactor" then if device.type == "fissionReactor" then
-- reconnected reactor -- reconnected reactor
reactor_scram = true plc_state.scram = true
device.scram() device.scram()
print_ts("reactor reconnected.\n") print_ts("reactor reconnected.\n")
log._info("reactor reconnected.") log._info("reactor reconnected.")
control_degraded.no_reactor = false plc_state.no_reactor = false
if init_ok then if plc_state.init_ok then
iss.reconnect_reactor(device) iss.reconnect_reactor(device)
plc_comms.reconnect_reactor(device) plc_comms.reconnect_reactor(device)
end end
-- determine if we are still in a degraded state -- determine if we are still in a degraded state
if get_device("modem") not nil then if get_device("modem") not nil then
control_degraded.degraded = false plc_state.degraded = false
end end
elseif device.type == "modem" then elseif device.type == "modem" then
-- reconnected modem -- reconnected modem
if init_ok then if plc_state.init_ok then
plc_comms.reconnect_modem(device) plc_comms.reconnect_modem(device)
end end
print_ts("modem reconnected.\n") print_ts("modem reconnected.\n")
log._info("modem reconnected.") log._info("modem reconnected.")
control_degraded.no_modem = false plc_state.no_modem = false
-- determine if we are still in a degraded state -- determine if we are still in a degraded state
if ppm.get_device("fissionReactor") not nil then if ppm.get_device("fissionReactor") not nil then
control_degraded.degraded = false plc_state.degraded = false
end end
end end
if not init_ok and not control_degraded.degraded then if not plc_state.init_ok and not plc_state.degraded then
init_ok = false plc_state.init_ok = false
goto init goto init
end end
end end
-- check safety (SCRAM occurs if tripped) -- check safety (SCRAM occurs if tripped)
if not control_degraded.degraded then if not plc_state.degraded then
local iss_tripped, iss_status, iss_first = iss.check() local iss_tripped, iss_status, iss_first = iss.check()
reactor_scram = reactor_scram or iss_tripped plc_state.scram = plc_state.scram or iss_tripped
if iss_first then if iss_first then
plc_comms.send_iss_alarm(iss_status) plc_comms.send_iss_alarm(iss_status)
end end
@ -173,14 +179,14 @@ while true do
-- handle event -- handle event
if event == "timer" and param1 == loop_tick then if event == "timer" and param1 == loop_tick then
if not control_degraded.no_modem then if not plc_state.no_modem then
-- basic event tick, send updated data if it is time (~3.33Hz) -- basic event tick, send updated data if it is time (~3.33Hz)
-- iss was already checked (main reason for this tick rate) -- iss was already checked (main reason for this tick rate)
ticks_to_update = ticks_to_update - 1 ticks_to_update = ticks_to_update - 1
if plc_comms.is_linked() then if plc_comms.is_linked() then
if ticks_to_update <= 0 then if ticks_to_update <= 0 then
plc_comms.send_status(control_state, iss_tripped) plc_comms.send_status(iss_tripped)
ticks_to_update = UPDATE_TICKS ticks_to_update = UPDATE_TICKS
end end
else else
@ -197,17 +203,17 @@ while true do
local packet = plc_comms.parse_packet(p1, p2, p3, p4, p5) local packet = plc_comms.parse_packet(p1, p2, p3, p4, p5)
plc_comms.handle_packet(packet) plc_comms.handle_packet(packet)
reactor_scram = reactor_scram or plc_comms.is_scrammed() plc_state.scram = plc_state.scram or plc_comms.is_scrammed()
elseif event == "timer" and param1 == conn_watchdog.get_timer() then elseif event == "timer" and param1 == conn_watchdog.get_timer() then
-- haven't heard from server recently? shutdown reactor -- haven't heard from server recently? shutdown reactor
reactor_scram = true plc_state.scram = true
plc_comms.unlink() plc_comms.unlink()
iss.trip_timeout() iss.trip_timeout()
print_ts("[alert] server timeout, reactor disabled\n") print_ts("[alert] server timeout, reactor disabled\n")
elseif event == "terminate" then elseif event == "terminate" then
-- safe exit -- safe exit
if init_ok then if plc_state.init_ok then
reactor_scram = true plc_state.scram = true
if reactor.scram() then if reactor.scram() then
print_ts("[alert] exiting, reactor disabled\n") print_ts("[alert] exiting, reactor disabled\n")
else else