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-04-27 16:21:10 +00:00
|
|
|
os.loadAPI("scada-common/mqueue.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-04-25 15:40:53 +00:00
|
|
|
os.loadAPI("threads.lua")
|
2022-01-14 21:33:09 +00:00
|
|
|
|
2022-04-27 16:21:10 +00:00
|
|
|
local R_PLC_VERSION = "alpha-v0.4.0"
|
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()
|
|
|
|
|
2022-04-25 15:40:53 +00:00
|
|
|
-- shared memory across threads
|
|
|
|
local __shared_memory = {
|
2022-04-27 16:21:10 +00:00
|
|
|
-- networked setting
|
2022-04-25 15:40:53 +00:00
|
|
|
networked = config.NETWORKED,
|
|
|
|
|
2022-04-27 16:21:10 +00:00
|
|
|
-- PLC system state flags
|
2022-04-25 15:40:53 +00:00
|
|
|
plc_state = {
|
|
|
|
init_ok = true,
|
2022-04-27 16:21:10 +00:00
|
|
|
shutdown = false,
|
2022-04-25 15:40:53 +00:00
|
|
|
scram = true,
|
|
|
|
degraded = false,
|
|
|
|
no_reactor = false,
|
|
|
|
no_modem = false
|
|
|
|
},
|
|
|
|
|
2022-04-27 16:21:10 +00:00
|
|
|
-- core PLC devices
|
|
|
|
plc_dev = {
|
2022-04-25 15:40:53 +00:00
|
|
|
reactor = ppm.get_fission_reactor(),
|
|
|
|
modem = ppm.get_wireless_modem()
|
|
|
|
},
|
|
|
|
|
2022-04-27 16:21:10 +00:00
|
|
|
-- system control objects
|
|
|
|
plc_sys = {
|
2022-04-25 15:40:53 +00:00
|
|
|
iss = nil,
|
|
|
|
plc_comms = nil,
|
|
|
|
conn_watchdog = nil
|
2022-04-27 16:21:10 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
-- message queues
|
|
|
|
q = {
|
|
|
|
mq_main = mqueue.new(),
|
|
|
|
mq_iss = mqueue.new(),
|
|
|
|
mq_comms = mqeuue.new()
|
2022-04-25 15:40:53 +00:00
|
|
|
}
|
|
|
|
}
|
2022-01-14 21:33:09 +00:00
|
|
|
|
2022-04-27 16:21:10 +00:00
|
|
|
local smem_dev = __shared_memory.plc_dev
|
|
|
|
local smem_sys = __shared_memory.plc_sys
|
2022-04-05 19:56:48 +00:00
|
|
|
|
2022-04-25 15:40:53 +00:00
|
|
|
local plc_state = __shared_memory.plc_state
|
2022-04-03 16:08:22 +00:00
|
|
|
|
2022-01-14 21:33:09 +00:00
|
|
|
-- we need a reactor and a modem
|
2022-04-25 15:40:53 +00:00
|
|
|
if smem_dev.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-25 15:40:53 +00:00
|
|
|
if networked and smem_dev.modem == nil then
|
2022-04-07 15:44:17 +00:00
|
|
|
println("boot> wireless modem not found")
|
|
|
|
log._warning("no wireless modem on startup")
|
2022-04-05 21:29:27 +00:00
|
|
|
|
2022-04-25 15:40:53 +00:00
|
|
|
if smem_dev.reactor ~= nil then
|
|
|
|
smem_dev.reactor.scram()
|
2022-04-05 13:41:06 +00:00
|
|
|
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 20:09:29 +00:00
|
|
|
function init()
|
|
|
|
if plc_state.init_ok then
|
|
|
|
-- just booting up, no fission allowed (neutrons stay put thanks)
|
2022-04-25 15:40:53 +00:00
|
|
|
smem_dev.reactor.scram()
|
2022-04-05 20:09:29 +00:00
|
|
|
|
|
|
|
-- init internal safety system
|
2022-04-25 15:40:53 +00:00
|
|
|
smem_sys.iss = plc.iss_init(smem_dev.reactor)
|
2022-04-05 20:09:29 +00:00
|
|
|
log._debug("iss init")
|
|
|
|
|
2022-04-25 15:40:53 +00:00
|
|
|
if __shared_memory.networked then
|
2022-04-05 20:09:29 +00:00
|
|
|
-- start comms
|
2022-04-25 15:40:53 +00:00
|
|
|
smem_sys.plc_comms = plc.comms_init(config.REACTOR_ID, smem_dev.modem, config.LISTEN_PORT, config.SERVER_PORT, smem_dev.reactor, smem_sys.iss)
|
2022-04-05 20:09:29 +00:00
|
|
|
log._debug("comms init")
|
|
|
|
|
|
|
|
-- comms watchdog, 3 second timeout
|
2022-04-25 15:40:53 +00:00
|
|
|
smem_sys.conn_watchdog = util.new_watchdog(3)
|
2022-04-05 20:09:29 +00:00
|
|
|
log._debug("conn watchdog started")
|
|
|
|
else
|
2022-04-21 16:40:21 +00:00
|
|
|
println("boot> starting in offline mode");
|
2022-04-05 20:09:29 +00:00
|
|
|
log._debug("running without networking")
|
|
|
|
end
|
|
|
|
|
2022-04-25 15:40:53 +00:00
|
|
|
os.queueEvent("clock_start")
|
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-04-25 15:40:53 +00:00
|
|
|
-- init threads
|
2022-04-27 16:21:10 +00:00
|
|
|
local main_thread = threads.thread__main(__shared_memory, init)
|
|
|
|
local iss_thread = threads.thread__iss(__shared_memory)
|
|
|
|
local comms_thread = threads.thread__comms(__shared_memory)
|
2022-04-03 16:08:22 +00:00
|
|
|
|
2022-04-25 15:40:53 +00:00
|
|
|
-- run threads
|
2022-04-27 16:21:10 +00:00
|
|
|
parallel.waitForAll(main_thread.exec, iss_thread.exec, comms_thread.exec)
|
2022-04-18 14:31:24 +00:00
|
|
|
|
|
|
|
-- send an alarm: plc_comms.send_alarm(ALARMS.PLC_SHUTDOWN) ?
|
|
|
|
println_ts("exited")
|
|
|
|
log._info("exited")
|