2022-01-14 21:33:09 +00:00
|
|
|
--
|
|
|
|
-- Reactor Programmable Logic Controller
|
|
|
|
--
|
|
|
|
|
2022-03-10 19:23:14 +00:00
|
|
|
os.loadAPI("scada-common/log.lua")
|
2022-01-14 21:33:09 +00:00
|
|
|
os.loadAPI("scada-common/util.lua")
|
2022-03-14 18:19:14 +00:00
|
|
|
os.loadAPI("scada-common/ppm.lua")
|
2022-01-14 21:33:09 +00:00
|
|
|
os.loadAPI("scada-common/comms.lua")
|
2022-03-14 18:19:14 +00:00
|
|
|
|
2022-04-05 20:09:29 +00:00
|
|
|
os.loadAPI("config.lua")
|
|
|
|
os.loadAPI("plc.lua")
|
2022-01-14 21:33:09 +00:00
|
|
|
|
2022-04-05 21:29:27 +00:00
|
|
|
local R_PLC_VERSION = "alpha-v0.1.2"
|
2022-01-14 21:33:09 +00:00
|
|
|
|
2022-04-05 21:29:27 +00:00
|
|
|
local print = util.print
|
|
|
|
local println = util.println
|
2022-01-14 21:33:09 +00:00
|
|
|
local print_ts = util.print_ts
|
2022-04-05 21:29:27 +00:00
|
|
|
local println_ts = util.println_ts
|
2022-01-14 21:33:09 +00:00
|
|
|
|
2022-04-05 21:29:27 +00:00
|
|
|
log._info("========================================")
|
|
|
|
log._info("BOOTING reactor-plc.startup " .. R_PLC_VERSION)
|
|
|
|
log._info("========================================")
|
|
|
|
println(">> Reactor PLC " .. R_PLC_VERSION .. " <<")
|
2022-04-02 15:22:44 +00:00
|
|
|
|
|
|
|
-- mount connected devices
|
2022-03-10 19:23:14 +00:00
|
|
|
ppm.mount_all()
|
|
|
|
|
|
|
|
local reactor = ppm.get_device("fissionReactor")
|
|
|
|
local modem = ppm.get_device("modem")
|
2022-01-14 21:33:09 +00:00
|
|
|
|
2022-04-05 19:56:48 +00:00
|
|
|
local networked = config.NETWORKED
|
|
|
|
|
2022-04-05 13:41:06 +00:00
|
|
|
local plc_state = {
|
|
|
|
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
|
|
|
|
}
|
2022-04-03 16:08:22 +00:00
|
|
|
|
2022-01-14 21:33:09 +00:00
|
|
|
-- we need a reactor and a modem
|
|
|
|
if reactor == nil then
|
2022-04-05 21:29:27 +00:00
|
|
|
println("boot> fission reactor not found");
|
2022-04-03 16:08:22 +00:00
|
|
|
log._warning("no reactor on startup")
|
2022-04-05 21:29:27 +00:00
|
|
|
|
2022-04-05 13:41:06 +00:00
|
|
|
plc_state.init_ok = false
|
|
|
|
plc_state.degraded = true
|
|
|
|
plc_state.no_reactor = true
|
2022-04-03 16:08:22 +00:00
|
|
|
end
|
2022-04-05 19:56:48 +00:00
|
|
|
if networked and modem == nil then
|
2022-04-05 21:29:27 +00:00
|
|
|
println("boot> modem not found")
|
|
|
|
log._warning("no modem on startup")
|
|
|
|
|
2022-04-05 13:41:06 +00:00
|
|
|
if reactor ~= nil then
|
|
|
|
reactor.scram()
|
|
|
|
end
|
|
|
|
|
|
|
|
plc_state.init_ok = false
|
|
|
|
plc_state.degraded = true
|
|
|
|
plc_state.no_modem = true
|
2022-01-14 21:33:09 +00:00
|
|
|
end
|
|
|
|
|
2022-04-05 19:56:48 +00:00
|
|
|
local iss = nil
|
|
|
|
local plc_comms = nil
|
|
|
|
local conn_watchdog = nil
|
|
|
|
|
|
|
|
-- send status updates at ~3.33Hz (every 6 server ticks) (every 3 loop ticks)
|
|
|
|
-- send link requests at 0.5Hz (every 40 server ticks) (every 20 loop ticks)
|
|
|
|
local UPDATE_TICKS = 3
|
|
|
|
local LINK_TICKS = 20
|
|
|
|
|
|
|
|
local loop_tick = nil
|
|
|
|
local ticks_to_update = LINK_TICKS -- start by linking
|
|
|
|
|
2022-04-05 20:09:29 +00:00
|
|
|
function init()
|
|
|
|
if plc_state.init_ok then
|
|
|
|
-- just booting up, no fission allowed (neutrons stay put thanks)
|
|
|
|
reactor.scram()
|
|
|
|
|
|
|
|
-- init internal safety system
|
|
|
|
iss = plc.iss_init(reactor)
|
|
|
|
log._debug("iss init")
|
|
|
|
|
|
|
|
if networked then
|
|
|
|
-- start comms
|
|
|
|
plc_comms = plc.comms_init(config.REACTOR_ID, modem, config.LISTEN_PORT, config.SERVER_PORT, reactor, iss)
|
|
|
|
log._debug("comms init")
|
|
|
|
|
|
|
|
-- comms watchdog, 3 second timeout
|
2022-04-05 21:29:27 +00:00
|
|
|
conn_watchdog = util.new_watchdog(3)
|
2022-04-05 20:09:29 +00:00
|
|
|
log._debug("conn watchdog started")
|
|
|
|
else
|
|
|
|
log._debug("running without networking")
|
|
|
|
end
|
|
|
|
|
|
|
|
-- loop clock (10Hz, 2 ticks)
|
|
|
|
loop_tick = os.startTimer(0.05)
|
|
|
|
log._debug("loop clock started")
|
2022-04-05 21:29:27 +00:00
|
|
|
|
|
|
|
println("boot> completed");
|
2022-04-05 19:56:48 +00:00
|
|
|
else
|
2022-04-05 21:29:27 +00:00
|
|
|
println("boot> system in degraded state, awaiting devices...")
|
2022-04-05 20:09:29 +00:00
|
|
|
log._warning("booted in a degraded state, awaiting peripheral connections...")
|
2022-04-05 19:56:48 +00:00
|
|
|
end
|
2022-04-03 16:08:22 +00:00
|
|
|
end
|
2022-04-02 15:22:44 +00:00
|
|
|
|
2022-04-05 20:09:29 +00:00
|
|
|
-- initialize PLC
|
|
|
|
init()
|
|
|
|
|
2022-01-14 21:33:09 +00:00
|
|
|
-- event loop
|
|
|
|
while true do
|
2022-03-10 19:23:14 +00:00
|
|
|
local event, param1, param2, param3, param4, param5 = os.pullEventRaw()
|
2022-01-14 21:33:09 +00:00
|
|
|
|
2022-04-05 13:41:06 +00:00
|
|
|
if plc_state.init_ok then
|
2022-04-03 16:08:22 +00:00
|
|
|
-- 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)
|
|
|
|
-- in that case, SCRAM won't be called until it reconnects (this is the expected use of this check)
|
|
|
|
ppm.disable_reporting()
|
2022-04-05 21:29:27 +00:00
|
|
|
if plc_state.scram and reactor.getStatus() then
|
2022-04-03 16:08:22 +00:00
|
|
|
reactor.scram()
|
|
|
|
end
|
|
|
|
ppm.enable_reporting()
|
2022-04-02 15:46:14 +00:00
|
|
|
end
|
|
|
|
|
2022-04-03 16:08:22 +00:00
|
|
|
-- check for peripheral changes before ISS checks
|
2022-01-22 19:26:25 +00:00
|
|
|
if event == "peripheral_detach" then
|
2022-04-03 16:08:22 +00:00
|
|
|
local device = ppm.handle_unmount(param1)
|
|
|
|
|
|
|
|
if device.type == "fissionReactor" then
|
2022-04-05 21:29:27 +00:00
|
|
|
println_ts("reactor disconnected!")
|
2022-04-03 16:08:22 +00:00
|
|
|
log._error("reactor disconnected!")
|
2022-04-05 13:41:06 +00:00
|
|
|
plc_state.no_reactor = true
|
2022-04-05 21:29:27 +00:00
|
|
|
plc_state.degraded = true
|
2022-04-03 16:08:22 +00:00
|
|
|
-- send an alarm: plc_comms.send_alarm(ALARMS.PLC_PERI_DC) ?
|
2022-04-05 19:56:48 +00:00
|
|
|
elseif networked and device.type == "modem" then
|
2022-04-05 21:29:27 +00:00
|
|
|
println_ts("modem disconnected!")
|
2022-04-03 16:08:22 +00:00
|
|
|
log._error("modem disconnected!")
|
2022-04-05 13:41:06 +00:00
|
|
|
plc_state.no_modem = true
|
2022-04-03 16:08:22 +00:00
|
|
|
|
2022-04-05 13:41:06 +00:00
|
|
|
if plc_state.init_ok then
|
2022-04-03 16:08:22 +00:00
|
|
|
-- try to scram reactor if it is still connected
|
2022-04-05 13:41:06 +00:00
|
|
|
plc_state.scram = true
|
2022-04-03 16:08:22 +00:00
|
|
|
if reactor.scram() then
|
2022-04-05 21:29:27 +00:00
|
|
|
println_ts("successful reactor SCRAM")
|
2022-04-03 16:08:22 +00:00
|
|
|
else
|
2022-04-05 21:29:27 +00:00
|
|
|
println_ts("failed reactor SCRAM")
|
2022-04-03 16:08:22 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-04-05 13:41:06 +00:00
|
|
|
plc_state.degraded = true
|
2022-04-03 16:08:22 +00:00
|
|
|
end
|
|
|
|
elseif event == "peripheral" then
|
2022-04-05 21:29:27 +00:00
|
|
|
local type, device = ppm.mount(param1)
|
2022-04-03 16:08:22 +00:00
|
|
|
|
2022-04-05 21:29:27 +00:00
|
|
|
if type == "fissionReactor" then
|
2022-04-03 16:08:22 +00:00
|
|
|
-- reconnected reactor
|
2022-04-05 21:29:27 +00:00
|
|
|
reactor = device
|
|
|
|
|
2022-04-05 13:41:06 +00:00
|
|
|
plc_state.scram = true
|
2022-04-05 21:29:27 +00:00
|
|
|
reactor.scram()
|
2022-04-03 16:08:22 +00:00
|
|
|
|
2022-04-05 21:29:27 +00:00
|
|
|
println_ts("reactor reconnected.")
|
2022-04-03 16:08:22 +00:00
|
|
|
log._info("reactor reconnected.")
|
2022-04-05 13:41:06 +00:00
|
|
|
plc_state.no_reactor = false
|
2022-04-03 16:08:22 +00:00
|
|
|
|
2022-04-05 13:41:06 +00:00
|
|
|
if plc_state.init_ok then
|
2022-04-05 21:29:27 +00:00
|
|
|
iss.reconnect_reactor(reactor)
|
2022-04-05 19:56:48 +00:00
|
|
|
if networked then
|
2022-04-05 21:29:27 +00:00
|
|
|
plc_comms.reconnect_reactor(reactor)
|
2022-04-05 19:56:48 +00:00
|
|
|
end
|
2022-04-03 16:08:22 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
-- determine if we are still in a degraded state
|
2022-04-05 21:29:27 +00:00
|
|
|
if not networked or ppm.get_device("modem") ~= nil then
|
2022-04-05 13:41:06 +00:00
|
|
|
plc_state.degraded = false
|
2022-04-03 16:08:22 +00:00
|
|
|
end
|
2022-04-05 21:29:27 +00:00
|
|
|
elseif networked and type == "modem" then
|
2022-04-03 16:08:22 +00:00
|
|
|
-- reconnected modem
|
2022-04-05 21:29:27 +00:00
|
|
|
modem = device
|
|
|
|
|
2022-04-05 13:41:06 +00:00
|
|
|
if plc_state.init_ok then
|
2022-04-05 21:29:27 +00:00
|
|
|
plc_comms.reconnect_modem(modem)
|
2022-04-03 16:08:22 +00:00
|
|
|
end
|
|
|
|
|
2022-04-05 21:29:27 +00:00
|
|
|
println_ts("modem reconnected.")
|
2022-04-03 16:08:22 +00:00
|
|
|
log._info("modem reconnected.")
|
2022-04-05 13:41:06 +00:00
|
|
|
plc_state.no_modem = false
|
2022-04-03 16:08:22 +00:00
|
|
|
|
|
|
|
-- determine if we are still in a degraded state
|
2022-04-05 20:09:29 +00:00
|
|
|
if ppm.get_device("fissionReactor") ~= nil then
|
2022-04-05 13:41:06 +00:00
|
|
|
plc_state.degraded = false
|
2022-04-03 16:08:22 +00:00
|
|
|
end
|
2022-03-10 19:23:14 +00:00
|
|
|
end
|
|
|
|
|
2022-04-05 13:41:06 +00:00
|
|
|
if not plc_state.init_ok and not plc_state.degraded then
|
2022-04-05 21:29:27 +00:00
|
|
|
plc_state.init_ok = true
|
2022-04-05 20:09:29 +00:00
|
|
|
init()
|
2022-04-03 16:08:22 +00:00
|
|
|
end
|
2022-01-22 19:26:25 +00:00
|
|
|
end
|
|
|
|
|
2022-01-14 21:33:09 +00:00
|
|
|
-- check safety (SCRAM occurs if tripped)
|
2022-04-05 13:41:06 +00:00
|
|
|
if not plc_state.degraded then
|
2022-04-03 16:08:22 +00:00
|
|
|
local iss_tripped, iss_status, iss_first = iss.check()
|
2022-04-05 13:41:06 +00:00
|
|
|
plc_state.scram = plc_state.scram or iss_tripped
|
2022-04-05 19:56:48 +00:00
|
|
|
if networked and iss_first then
|
2022-04-03 16:08:22 +00:00
|
|
|
plc_comms.send_iss_alarm(iss_status)
|
|
|
|
end
|
2022-04-05 21:29:27 +00:00
|
|
|
elseif plc_state.init_ok then
|
|
|
|
reactor.scram()
|
2022-01-22 19:26:25 +00:00
|
|
|
end
|
2022-01-14 21:33:09 +00:00
|
|
|
|
|
|
|
-- handle event
|
2022-04-05 19:56:48 +00:00
|
|
|
if event == "timer" and param1 == loop_tick and networked and not plc_state.no_modem then
|
|
|
|
-- basic event tick, send updated data if it is time (~3.33Hz)
|
|
|
|
-- iss was already checked (that's the main reason for this tick rate)
|
|
|
|
ticks_to_update = ticks_to_update - 1
|
|
|
|
|
|
|
|
if plc_comms.is_linked() then
|
|
|
|
if ticks_to_update <= 0 then
|
|
|
|
plc_comms.send_status(iss_tripped)
|
|
|
|
ticks_to_update = UPDATE_TICKS
|
|
|
|
end
|
|
|
|
else
|
|
|
|
if ticks_to_update <= 0 then
|
|
|
|
plc_comms.send_link_req()
|
|
|
|
ticks_to_update = LINK_TICKS
|
2022-04-02 15:22:44 +00:00
|
|
|
end
|
2022-01-14 21:33:09 +00:00
|
|
|
end
|
2022-04-05 19:56:48 +00:00
|
|
|
elseif event == "modem_message" and networked and not plc_state.no_modem then
|
2022-01-14 21:33:09 +00:00
|
|
|
-- got a packet
|
2022-03-25 15:50:03 +00:00
|
|
|
-- feed the watchdog first so it doesn't uhh...eat our packets
|
2022-01-14 21:33:09 +00:00
|
|
|
conn_watchdog.feed()
|
|
|
|
|
2022-03-14 18:19:14 +00:00
|
|
|
local packet = plc_comms.parse_packet(p1, p2, p3, p4, p5)
|
2022-01-22 19:26:25 +00:00
|
|
|
plc_comms.handle_packet(packet)
|
2022-04-05 13:41:06 +00:00
|
|
|
plc_state.scram = plc_state.scram or plc_comms.is_scrammed()
|
2022-04-05 19:56:48 +00:00
|
|
|
elseif event == "timer" and param1 == conn_watchdog.get_timer() and networked then
|
2022-03-25 15:50:03 +00:00
|
|
|
-- haven't heard from server recently? shutdown reactor
|
2022-04-05 13:41:06 +00:00
|
|
|
plc_state.scram = true
|
2022-04-02 15:22:44 +00:00
|
|
|
plc_comms.unlink()
|
2022-01-22 19:26:25 +00:00
|
|
|
iss.trip_timeout()
|
2022-04-05 21:29:27 +00:00
|
|
|
println_ts("server timeout, reactor disabled")
|
|
|
|
log._warning("server timeout, reactor disabled")
|
2022-03-10 19:23:14 +00:00
|
|
|
elseif event == "terminate" then
|
|
|
|
-- safe exit
|
2022-04-05 13:41:06 +00:00
|
|
|
if plc_state.init_ok then
|
|
|
|
plc_state.scram = true
|
2022-04-03 16:08:22 +00:00
|
|
|
if reactor.scram() then
|
2022-04-05 21:29:27 +00:00
|
|
|
println_ts("reactor disabled")
|
2022-04-03 16:08:22 +00:00
|
|
|
else
|
|
|
|
-- send an alarm: plc_comms.send_alarm(ALARMS.PLC_LOST_CONTROL) ?
|
2022-04-05 21:29:27 +00:00
|
|
|
println_ts("exiting, reactor failed to disable")
|
2022-04-03 16:08:22 +00:00
|
|
|
end
|
2022-03-14 18:19:14 +00:00
|
|
|
end
|
2022-03-10 19:23:14 +00:00
|
|
|
-- send an alarm: plc_comms.send_alarm(ALARMS.PLC_SHUTDOWN) ?
|
2022-04-05 21:29:27 +00:00
|
|
|
println_ts("exited")
|
|
|
|
log._info("terminate requested, exiting")
|
2022-03-10 19:23:14 +00:00
|
|
|
return
|
2022-01-14 21:33:09 +00:00
|
|
|
end
|
2022-01-02 00:45:33 +00:00
|
|
|
end
|