2023-06-03 21:40:57 +00:00
|
|
|
local log = require("scada-common.log")
|
|
|
|
local mqueue = require("scada-common.mqueue")
|
|
|
|
local ppm = require("scada-common.ppm")
|
|
|
|
local tcd = require("scada-common.tcd")
|
|
|
|
local util = require("scada-common.util")
|
2022-05-04 17:37:01 +00:00
|
|
|
|
2023-06-03 21:40:57 +00:00
|
|
|
local databus = require("reactor-plc.databus")
|
|
|
|
local renderer = require("reactor-plc.renderer")
|
2023-04-09 01:35:44 +00:00
|
|
|
|
2023-06-03 21:40:57 +00:00
|
|
|
local core = require("graphics.core")
|
2022-04-25 15:40:53 +00:00
|
|
|
|
2023-04-08 20:49:54 +00:00
|
|
|
local threads = {}
|
2022-04-25 15:40:53 +00:00
|
|
|
|
2022-12-07 17:59:21 +00:00
|
|
|
local MAIN_CLOCK = 0.5 -- (2Hz, 10 ticks)
|
2022-05-05 17:14:14 +00:00
|
|
|
local RPS_SLEEP = 250 -- (250ms, 5 ticks)
|
2022-04-30 02:27:54 +00:00
|
|
|
local COMMS_SLEEP = 150 -- (150ms, 3 ticks)
|
|
|
|
local SP_CTRL_SLEEP = 250 -- (250ms, 5 ticks)
|
|
|
|
|
|
|
|
local BURN_RATE_RAMP_mB_s = 5.0
|
2022-04-25 15:40:53 +00:00
|
|
|
|
2022-05-05 15:55:04 +00:00
|
|
|
local MQ__RPS_CMD = {
|
2022-04-25 15:40:53 +00:00
|
|
|
SCRAM = 1,
|
|
|
|
DEGRADED_SCRAM = 2,
|
|
|
|
TRIP_TIMEOUT = 3
|
|
|
|
}
|
|
|
|
|
2022-04-27 16:21:10 +00:00
|
|
|
local MQ__COMM_CMD = {
|
|
|
|
SEND_STATUS = 1
|
|
|
|
}
|
|
|
|
|
2022-04-25 15:40:53 +00:00
|
|
|
-- main thread
|
2023-02-21 21:57:33 +00:00
|
|
|
---@nodiscard
|
2022-05-11 01:49:14 +00:00
|
|
|
---@param smem plc_shared_memory
|
|
|
|
---@param init function
|
2022-05-31 18:14:17 +00:00
|
|
|
function threads.thread__main(smem, init)
|
2023-04-08 20:49:54 +00:00
|
|
|
-- print a log message to the terminal as long as the UI isn't running
|
|
|
|
local function println_ts(message) if not smem.plc_state.fp_ok then util.println_ts(message) end end
|
|
|
|
|
2023-02-21 17:27:16 +00:00
|
|
|
---@class parallel_thread
|
|
|
|
local public = {}
|
2022-05-21 17:56:14 +00:00
|
|
|
|
2022-04-25 15:40:53 +00:00
|
|
|
-- execute thread
|
2022-05-31 18:14:17 +00:00
|
|
|
function public.exec()
|
2023-04-08 20:49:54 +00:00
|
|
|
databus.tx_rt_status("main", true)
|
2022-05-04 17:37:01 +00:00
|
|
|
log.debug("main thread init, clock inactive")
|
2022-04-27 19:52:34 +00:00
|
|
|
|
2022-12-07 17:59:21 +00:00
|
|
|
-- send status updates at 2Hz (every 10 server ticks) (every loop tick)
|
|
|
|
-- send link requests at 0.5Hz (every 40 server ticks) (every 8 loop ticks)
|
|
|
|
local LINK_TICKS = 8
|
2022-04-26 01:00:50 +00:00
|
|
|
local ticks_to_update = 0
|
2022-05-10 17:06:13 +00:00
|
|
|
local loop_clock = util.new_clock(MAIN_CLOCK)
|
2022-04-25 15:40:53 +00:00
|
|
|
|
|
|
|
-- load in from shared memory
|
2023-02-21 21:57:33 +00:00
|
|
|
local networked = smem.networked
|
|
|
|
local plc_state = smem.plc_state
|
|
|
|
local plc_dev = smem.plc_dev
|
2022-04-25 15:40:53 +00:00
|
|
|
|
|
|
|
-- event loop
|
|
|
|
while true do
|
2022-05-22 21:57:24 +00:00
|
|
|
-- get plc_sys fields (may have been set late due to degraded boot)
|
|
|
|
local rps = smem.plc_sys.rps
|
2023-06-23 17:52:24 +00:00
|
|
|
local nic = smem.plc_sys.nic
|
2022-05-22 21:57:24 +00:00
|
|
|
local plc_comms = smem.plc_sys.plc_comms
|
|
|
|
local conn_watchdog = smem.plc_sys.conn_watchdog
|
|
|
|
|
2022-07-19 19:18:11 +00:00
|
|
|
local event, param1, param2, param3, param4, param5 = util.pull_event()
|
2022-04-25 15:40:53 +00:00
|
|
|
|
|
|
|
-- handle event
|
2022-05-10 17:06:13 +00:00
|
|
|
if event == "timer" and loop_clock.is_clock(param1) then
|
2023-06-23 17:52:24 +00:00
|
|
|
-- note: loop clock is only running if init_ok = true
|
2023-04-08 20:49:54 +00:00
|
|
|
-- blink heartbeat indicator
|
|
|
|
databus.heartbeat()
|
|
|
|
|
2022-04-25 15:40:53 +00:00
|
|
|
-- core clock tick
|
|
|
|
if networked then
|
|
|
|
-- start next clock timer
|
2022-05-10 17:06:13 +00:00
|
|
|
loop_clock.start()
|
2022-04-25 15:40:53 +00:00
|
|
|
|
|
|
|
-- send updated data
|
2023-07-11 22:22:09 +00:00
|
|
|
if nic.is_connected() then
|
2022-04-25 15:40:53 +00:00
|
|
|
if plc_comms.is_linked() then
|
2022-04-29 02:36:45 +00:00
|
|
|
smem.q.mq_comms_tx.push_command(MQ__COMM_CMD.SEND_STATUS)
|
2022-04-25 15:40:53 +00:00
|
|
|
else
|
2022-04-26 01:00:50 +00:00
|
|
|
if ticks_to_update == 0 then
|
2022-04-25 15:40:53 +00:00
|
|
|
plc_comms.send_link_req()
|
|
|
|
ticks_to_update = LINK_TICKS
|
2022-04-26 01:00:50 +00:00
|
|
|
else
|
|
|
|
ticks_to_update = ticks_to_update - 1
|
2022-04-25 15:40:53 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2022-10-25 17:29:57 +00:00
|
|
|
|
|
|
|
-- are we now formed after waiting to be formed?
|
2022-11-05 16:44:40 +00:00
|
|
|
if (not plc_state.reactor_formed) and rps.is_formed() then
|
2022-10-25 17:29:57 +00:00
|
|
|
-- push a connect event and unmount it from the PPM
|
|
|
|
local iface = ppm.get_iface(plc_dev.reactor)
|
|
|
|
if iface then
|
2022-10-26 03:40:36 +00:00
|
|
|
log.info("unmounting and remounting unformed reactor")
|
2022-10-25 17:29:57 +00:00
|
|
|
ppm.unmount(plc_dev.reactor)
|
|
|
|
|
|
|
|
local type, device = ppm.mount(iface)
|
|
|
|
|
2022-11-05 16:44:40 +00:00
|
|
|
if type == "fissionReactorLogicAdapter" and device ~= nil then
|
2022-10-25 17:29:57 +00:00
|
|
|
-- reconnect reactor
|
|
|
|
plc_dev.reactor = device
|
|
|
|
|
2022-11-05 16:44:40 +00:00
|
|
|
-- we need to assume formed here as we cannot check in this main loop
|
|
|
|
-- RPS will identify if it isn't and this will get set false later
|
|
|
|
plc_state.reactor_formed = true
|
2022-10-26 03:40:36 +00:00
|
|
|
|
2022-11-05 16:44:40 +00:00
|
|
|
println_ts("reactor reconnected.")
|
|
|
|
log.info("reactor reconnected")
|
|
|
|
|
|
|
|
-- SCRAM newly connected reactor
|
|
|
|
smem.q.mq_rps.push_command(MQ__RPS_CMD.SCRAM)
|
2022-10-25 17:29:57 +00:00
|
|
|
|
|
|
|
-- determine if we are still in a degraded state
|
2023-07-11 22:22:09 +00:00
|
|
|
if (not networked) or nic.is_connected() then
|
2022-10-25 17:29:57 +00:00
|
|
|
plc_state.degraded = false
|
|
|
|
end
|
2022-10-26 03:40:36 +00:00
|
|
|
|
|
|
|
rps.reconnect_reactor(plc_dev.reactor)
|
|
|
|
if networked then
|
|
|
|
plc_comms.reconnect_reactor(plc_dev.reactor)
|
|
|
|
end
|
2022-11-05 16:44:40 +00:00
|
|
|
|
2024-03-05 22:12:12 +00:00
|
|
|
-- partial reset of RPS, specific to becoming formed
|
|
|
|
rps.reset_formed()
|
2022-10-25 17:29:57 +00:00
|
|
|
else
|
|
|
|
-- fully lost the reactor now :(
|
|
|
|
println_ts("reactor lost (failed reconnect)!")
|
2022-10-26 03:40:36 +00:00
|
|
|
log.error("reactor lost (failed reconnect)")
|
2022-10-25 17:29:57 +00:00
|
|
|
|
|
|
|
plc_state.no_reactor = true
|
|
|
|
plc_state.degraded = true
|
|
|
|
end
|
|
|
|
else
|
|
|
|
log.error("failed to get interface of previously connected reactor", true)
|
|
|
|
end
|
|
|
|
elseif not rps.is_formed() then
|
|
|
|
-- reactor no longer formed
|
|
|
|
plc_state.reactor_formed = false
|
|
|
|
end
|
2023-04-08 04:38:46 +00:00
|
|
|
|
|
|
|
-- update indicators
|
2023-04-08 20:49:54 +00:00
|
|
|
databus.tx_hw_status(plc_state)
|
2023-07-11 22:22:09 +00:00
|
|
|
elseif event == "modem_message" and networked and plc_state.init_ok and nic.is_connected() then
|
2022-04-25 15:40:53 +00:00
|
|
|
-- got a packet
|
2022-04-27 16:21:10 +00:00
|
|
|
local packet = plc_comms.parse_packet(param1, param2, param3, param4, param5)
|
|
|
|
if packet ~= nil then
|
2022-04-29 02:36:45 +00:00
|
|
|
-- pass the packet onto the comms message queue
|
|
|
|
smem.q.mq_comms_rx.push_packet(packet)
|
2022-04-27 16:21:10 +00:00
|
|
|
end
|
2022-05-22 21:57:24 +00:00
|
|
|
elseif event == "timer" and networked and plc_state.init_ok and conn_watchdog.is_timer(param1) then
|
2023-08-26 01:42:35 +00:00
|
|
|
-- haven't heard from server recently? close connection and shutdown reactor
|
|
|
|
plc_comms.close()
|
2022-05-05 15:55:04 +00:00
|
|
|
smem.q.mq_rps.push_command(MQ__RPS_CMD.TRIP_TIMEOUT)
|
2023-04-08 20:49:54 +00:00
|
|
|
elseif event == "timer" then
|
|
|
|
-- notify timer callback dispatcher if no other timer case claimed this event
|
2023-06-03 21:40:57 +00:00
|
|
|
tcd.handle(param1)
|
2022-04-25 15:40:53 +00:00
|
|
|
elseif event == "peripheral_detach" then
|
|
|
|
-- peripheral disconnect
|
2022-05-10 21:08:38 +00:00
|
|
|
local type, device = ppm.handle_unmount(param1)
|
2022-04-25 15:40:53 +00:00
|
|
|
|
2022-05-10 21:08:38 +00:00
|
|
|
if type ~= nil and device ~= nil then
|
2023-08-11 14:20:13 +00:00
|
|
|
if device == plc_dev.reactor then
|
2022-05-10 21:08:38 +00:00
|
|
|
println_ts("reactor disconnected!")
|
2022-10-26 03:40:36 +00:00
|
|
|
log.error("reactor logic adapter disconnected")
|
2022-10-25 17:29:57 +00:00
|
|
|
|
2022-05-10 21:08:38 +00:00
|
|
|
plc_state.no_reactor = true
|
2022-04-25 15:40:53 +00:00
|
|
|
plc_state.degraded = true
|
2022-05-10 21:08:38 +00:00
|
|
|
elseif networked and type == "modem" then
|
|
|
|
-- we only care if this is our wireless modem
|
2023-12-18 01:28:26 +00:00
|
|
|
-- note, check init_ok first since nic will be nil if it is false
|
|
|
|
if plc_state.init_ok and nic.is_modem(device) then
|
2023-06-23 17:52:24 +00:00
|
|
|
nic.disconnect()
|
|
|
|
|
2022-10-25 17:29:57 +00:00
|
|
|
println_ts("comms modem disconnected!")
|
2023-07-11 22:22:09 +00:00
|
|
|
log.warning("comms modem disconnected")
|
2022-10-25 17:29:57 +00:00
|
|
|
|
2023-07-11 22:22:09 +00:00
|
|
|
local other_modem = ppm.get_wireless_modem()
|
|
|
|
if other_modem then
|
|
|
|
log.info("found another wireless modem, using it for comms")
|
|
|
|
nic.connect(other_modem)
|
|
|
|
else
|
|
|
|
plc_state.no_modem = true
|
|
|
|
plc_state.degraded = true
|
2022-05-10 21:08:38 +00:00
|
|
|
|
2023-07-11 22:22:09 +00:00
|
|
|
if plc_state.init_ok then
|
|
|
|
-- try to scram reactor if it is still connected
|
|
|
|
smem.q.mq_rps.push_command(MQ__RPS_CMD.DEGRADED_SCRAM)
|
|
|
|
end
|
2022-05-10 21:08:38 +00:00
|
|
|
end
|
|
|
|
else
|
2023-12-18 01:28:26 +00:00
|
|
|
log.warning("a modem was disconnected")
|
2022-05-10 21:08:38 +00:00
|
|
|
end
|
2022-04-25 15:40:53 +00:00
|
|
|
end
|
|
|
|
end
|
2023-04-08 04:38:46 +00:00
|
|
|
|
|
|
|
-- update indicators
|
2023-04-08 20:49:54 +00:00
|
|
|
databus.tx_hw_status(plc_state)
|
2022-04-25 15:40:53 +00:00
|
|
|
elseif event == "peripheral" then
|
|
|
|
-- peripheral connect
|
|
|
|
local type, device = ppm.mount(param1)
|
|
|
|
|
2022-05-10 21:08:38 +00:00
|
|
|
if type ~= nil and device ~= nil then
|
2023-08-11 14:20:13 +00:00
|
|
|
if plc_state.no_reactor and (type == "fissionReactorLogicAdapter") then
|
2022-05-10 21:08:38 +00:00
|
|
|
-- reconnected reactor
|
|
|
|
plc_dev.reactor = device
|
2023-06-23 17:52:24 +00:00
|
|
|
plc_state.no_reactor = false
|
2022-04-25 15:40:53 +00:00
|
|
|
|
2022-05-10 21:08:38 +00:00
|
|
|
println_ts("reactor reconnected.")
|
2022-05-22 21:57:24 +00:00
|
|
|
log.info("reactor reconnected")
|
2022-10-25 17:29:57 +00:00
|
|
|
|
2022-11-05 16:44:40 +00:00
|
|
|
-- we need to assume formed here as we cannot check in this main loop
|
|
|
|
-- RPS will identify if it isn't and this will get set false later
|
|
|
|
plc_state.reactor_formed = true
|
2022-04-25 15:40:53 +00:00
|
|
|
|
2022-10-26 03:40:36 +00:00
|
|
|
-- determine if we are still in a degraded state
|
|
|
|
if (not networked or not plc_state.no_modem) and plc_state.reactor_formed then
|
|
|
|
plc_state.degraded = false
|
|
|
|
end
|
|
|
|
|
2022-04-25 15:40:53 +00:00
|
|
|
if plc_state.init_ok then
|
2022-11-05 16:44:40 +00:00
|
|
|
smem.q.mq_rps.push_command(MQ__RPS_CMD.SCRAM)
|
2022-10-26 03:40:36 +00:00
|
|
|
|
2022-05-10 21:08:38 +00:00
|
|
|
rps.reconnect_reactor(plc_dev.reactor)
|
|
|
|
if networked then
|
|
|
|
plc_comms.reconnect_reactor(plc_dev.reactor)
|
|
|
|
end
|
2022-11-05 16:44:40 +00:00
|
|
|
|
2024-03-05 22:12:12 +00:00
|
|
|
-- partial reset of RPS, specific to becoming formed
|
|
|
|
rps.reset_formed()
|
2022-04-25 15:40:53 +00:00
|
|
|
end
|
2022-05-10 21:08:38 +00:00
|
|
|
elseif networked and type == "modem" then
|
2023-12-18 01:28:26 +00:00
|
|
|
-- note, check init_ok first since nic will be nil if it is false
|
|
|
|
if device.isWireless() and not (plc_state.init_ok and nic.is_connected()) then
|
2022-05-10 21:08:38 +00:00
|
|
|
-- reconnected modem
|
|
|
|
plc_dev.modem = device
|
2023-06-23 17:52:24 +00:00
|
|
|
plc_state.no_modem = false
|
2022-05-10 21:08:38 +00:00
|
|
|
|
2023-06-23 17:52:24 +00:00
|
|
|
if plc_state.init_ok then nic.connect(device) end
|
2022-05-10 21:08:38 +00:00
|
|
|
|
|
|
|
println_ts("wireless modem reconnected.")
|
2022-05-22 21:57:24 +00:00
|
|
|
log.info("comms modem reconnected")
|
2022-05-10 21:08:38 +00:00
|
|
|
|
|
|
|
-- determine if we are still in a degraded state
|
2022-05-22 21:57:24 +00:00
|
|
|
if not plc_state.no_reactor then
|
2022-05-10 21:08:38 +00:00
|
|
|
plc_state.degraded = false
|
|
|
|
end
|
2023-07-12 01:06:47 +00:00
|
|
|
elseif device.isWireless() then
|
|
|
|
log.info("unused wireless modem reconnected")
|
2022-05-10 21:08:38 +00:00
|
|
|
else
|
2022-05-22 21:57:24 +00:00
|
|
|
log.info("wired modem reconnected")
|
2022-05-10 21:08:38 +00:00
|
|
|
end
|
2022-04-25 15:40:53 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-05-22 21:57:24 +00:00
|
|
|
-- if not init'd and no longer degraded, proceed to init
|
2022-04-25 15:40:53 +00:00
|
|
|
if not plc_state.init_ok and not plc_state.degraded then
|
|
|
|
plc_state.init_ok = true
|
|
|
|
init()
|
|
|
|
end
|
2023-04-08 04:38:46 +00:00
|
|
|
|
|
|
|
-- update indicators
|
2023-04-08 20:49:54 +00:00
|
|
|
databus.tx_hw_status(plc_state)
|
2023-10-04 02:52:13 +00:00
|
|
|
elseif event == "mouse_click" or event == "mouse_up" or event == "mouse_drag" or event == "mouse_scroll" or
|
2023-09-23 16:58:09 +00:00
|
|
|
event == "double_click" then
|
2023-05-10 19:21:54 +00:00
|
|
|
-- handle a mouse event
|
|
|
|
renderer.handle_mouse(core.events.new_mouse_event(event, param1, param2, param3))
|
2022-04-25 15:40:53 +00:00
|
|
|
elseif event == "clock_start" then
|
|
|
|
-- start loop clock
|
2022-05-10 17:06:13 +00:00
|
|
|
loop_clock.start()
|
2022-05-04 17:37:01 +00:00
|
|
|
log.debug("main thread clock started")
|
2022-04-25 15:40:53 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
-- check for termination request
|
|
|
|
if event == "terminate" or ppm.should_terminate() then
|
2022-05-04 17:37:01 +00:00
|
|
|
log.info("terminate requested, main thread exiting")
|
2022-05-05 15:55:04 +00:00
|
|
|
-- rps handles reactor shutdown
|
2022-04-27 16:21:10 +00:00
|
|
|
plc_state.shutdown = true
|
2022-04-25 15:40:53 +00:00
|
|
|
break
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-05-21 17:56:14 +00:00
|
|
|
-- execute the thread in a protected mode, retrying it on return if not shutting down
|
2022-05-31 18:14:17 +00:00
|
|
|
function public.p_exec()
|
2022-05-21 17:56:14 +00:00
|
|
|
local plc_state = smem.plc_state
|
|
|
|
|
|
|
|
while not plc_state.shutdown do
|
|
|
|
local status, result = pcall(public.exec)
|
|
|
|
if status == false then
|
2022-09-21 19:53:51 +00:00
|
|
|
log.fatal(util.strval(result))
|
2022-05-21 17:56:14 +00:00
|
|
|
end
|
|
|
|
|
2023-04-08 20:49:54 +00:00
|
|
|
databus.tx_rt_status("main", false)
|
|
|
|
|
2022-05-21 17:56:14 +00:00
|
|
|
-- if status is true, then we are probably exiting, so this won't matter
|
|
|
|
-- if not, we need to restart the clock
|
|
|
|
-- this thread cannot be slept because it will miss events (namely "terminate" otherwise)
|
|
|
|
if not plc_state.shutdown then
|
|
|
|
log.info("main thread restarting now...")
|
2022-10-25 17:29:57 +00:00
|
|
|
util.push_event("clock_start")
|
2022-05-21 17:56:14 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
return public
|
2022-04-25 15:40:53 +00:00
|
|
|
end
|
|
|
|
|
2022-05-05 15:55:04 +00:00
|
|
|
-- RPS operation thread
|
2023-02-21 21:57:33 +00:00
|
|
|
---@nodiscard
|
2022-05-11 01:49:14 +00:00
|
|
|
---@param smem plc_shared_memory
|
2022-05-31 18:14:17 +00:00
|
|
|
function threads.thread__rps(smem)
|
2023-04-08 20:49:54 +00:00
|
|
|
-- print a log message to the terminal as long as the UI isn't running
|
|
|
|
local function println_ts(message) if not smem.plc_state.fp_ok then util.println_ts(message) end end
|
|
|
|
|
2023-02-21 17:27:16 +00:00
|
|
|
---@class parallel_thread
|
|
|
|
local public = {}
|
2022-05-21 17:56:14 +00:00
|
|
|
|
2022-04-25 15:40:53 +00:00
|
|
|
-- execute thread
|
2022-05-31 18:14:17 +00:00
|
|
|
function public.exec()
|
2023-04-08 20:49:54 +00:00
|
|
|
databus.tx_rt_status("rps", true)
|
2022-05-05 15:55:04 +00:00
|
|
|
log.debug("rps thread start")
|
2022-04-27 19:52:34 +00:00
|
|
|
|
2022-04-25 15:40:53 +00:00
|
|
|
-- load in from shared memory
|
2022-04-27 16:21:10 +00:00
|
|
|
local networked = smem.networked
|
|
|
|
local plc_state = smem.plc_state
|
|
|
|
local plc_dev = smem.plc_dev
|
2022-04-25 15:40:53 +00:00
|
|
|
|
2022-05-05 15:55:04 +00:00
|
|
|
local rps_queue = smem.q.mq_rps
|
2022-04-25 15:40:53 +00:00
|
|
|
|
2022-05-02 21:40:00 +00:00
|
|
|
local was_linked = false
|
2022-04-27 16:21:10 +00:00
|
|
|
local last_update = util.time()
|
2022-04-25 15:40:53 +00:00
|
|
|
|
2022-04-27 16:21:10 +00:00
|
|
|
-- thread loop
|
2022-04-25 15:40:53 +00:00
|
|
|
while true do
|
2022-05-22 21:57:24 +00:00
|
|
|
-- get plc_sys fields (may have been set late due to degraded boot)
|
2023-02-21 21:57:33 +00:00
|
|
|
local rps = smem.plc_sys.rps
|
|
|
|
local plc_comms = smem.plc_sys.plc_comms
|
2022-05-22 21:57:24 +00:00
|
|
|
-- get reactor, may have changed do to disconnect/reconnect
|
2023-02-21 21:57:33 +00:00
|
|
|
local reactor = plc_dev.reactor
|
2022-05-10 21:08:38 +00:00
|
|
|
|
2022-05-05 15:55:04 +00:00
|
|
|
-- RPS checks
|
2022-04-27 16:21:10 +00:00
|
|
|
if plc_state.init_ok then
|
2022-05-02 16:06:04 +00:00
|
|
|
-- SCRAM if no open connection
|
2022-05-02 21:40:00 +00:00
|
|
|
if networked and not plc_comms.is_linked() then
|
|
|
|
if was_linked then
|
|
|
|
was_linked = false
|
2022-05-05 15:55:04 +00:00
|
|
|
rps.trip_timeout()
|
2022-05-02 16:06:04 +00:00
|
|
|
end
|
|
|
|
else
|
2022-05-02 21:40:00 +00:00
|
|
|
was_linked = true
|
2022-05-02 16:06:04 +00:00
|
|
|
end
|
|
|
|
|
2023-04-08 20:49:54 +00:00
|
|
|
if (not plc_state.no_reactor) and rps.is_formed() then
|
|
|
|
-- check reactor status
|
2022-05-11 01:49:14 +00:00
|
|
|
---@diagnostic disable-next-line: need-check-nil
|
2023-04-08 20:49:54 +00:00
|
|
|
local reactor_status = reactor.getStatus()
|
|
|
|
databus.tx_reactor_state(reactor_status)
|
|
|
|
|
|
|
|
-- if we tried to SCRAM but failed, keep trying
|
|
|
|
-- in that case, SCRAM won't be called until it reconnects (this is the expected use of this check)
|
|
|
|
if rps.is_tripped() and reactor_status then
|
|
|
|
rps.scram()
|
|
|
|
end
|
2022-04-27 16:21:10 +00:00
|
|
|
end
|
2022-04-25 15:40:53 +00:00
|
|
|
|
2024-04-07 22:04:11 +00:00
|
|
|
-- if we are in standalone mode and the front panel isn't working, continuously reset RPS
|
2022-05-05 15:55:04 +00:00
|
|
|
-- RPS will trip again if there are faults, but if it isn't cleared, the user can't re-enable
|
2024-04-07 22:04:11 +00:00
|
|
|
if not (networked or smem.plc_state.fp_ok) then rps.reset(true) end
|
2022-04-25 15:40:53 +00:00
|
|
|
|
2022-04-27 16:21:10 +00:00
|
|
|
-- check safety (SCRAM occurs if tripped)
|
2022-05-02 16:06:04 +00:00
|
|
|
if not plc_state.no_reactor then
|
2022-05-05 15:55:04 +00:00
|
|
|
local rps_tripped, rps_status_string, rps_first = rps.check()
|
2022-04-27 16:21:10 +00:00
|
|
|
|
2022-05-22 21:57:24 +00:00
|
|
|
if rps_tripped and rps_first then
|
2022-05-05 15:55:04 +00:00
|
|
|
println_ts("[RPS] SCRAM! safety trip: " .. rps_status_string)
|
2022-05-02 16:06:04 +00:00
|
|
|
if networked and not plc_state.no_modem then
|
2022-05-05 15:55:04 +00:00
|
|
|
plc_comms.send_rps_alarm(rps_status_string)
|
2022-04-25 15:40:53 +00:00
|
|
|
end
|
2022-04-27 16:21:10 +00:00
|
|
|
end
|
2022-04-25 15:40:53 +00:00
|
|
|
end
|
2022-04-27 16:21:10 +00:00
|
|
|
end
|
2022-05-10 21:08:38 +00:00
|
|
|
|
2022-04-27 16:21:10 +00:00
|
|
|
-- check for messages in the message queue
|
2022-05-05 15:55:04 +00:00
|
|
|
while rps_queue.ready() and not plc_state.shutdown do
|
|
|
|
local msg = rps_queue.pop()
|
2022-04-27 16:21:10 +00:00
|
|
|
|
2022-05-11 01:49:14 +00:00
|
|
|
if msg ~= nil then
|
|
|
|
if msg.qtype == mqueue.TYPE.COMMAND then
|
|
|
|
-- received a command
|
|
|
|
if plc_state.init_ok then
|
|
|
|
if msg.message == MQ__RPS_CMD.SCRAM then
|
|
|
|
-- SCRAM
|
|
|
|
rps.scram()
|
|
|
|
elseif msg.message == MQ__RPS_CMD.DEGRADED_SCRAM then
|
|
|
|
-- lost peripheral(s)
|
|
|
|
rps.trip_fault()
|
|
|
|
elseif msg.message == MQ__RPS_CMD.TRIP_TIMEOUT then
|
|
|
|
-- watchdog tripped
|
|
|
|
rps.trip_timeout()
|
|
|
|
println_ts("server timeout")
|
|
|
|
log.warning("server timeout")
|
|
|
|
end
|
2022-04-25 15:40:53 +00:00
|
|
|
end
|
2022-05-11 01:49:14 +00:00
|
|
|
elseif msg.qtype == mqueue.TYPE.DATA then
|
|
|
|
-- received data
|
|
|
|
elseif msg.qtype == mqueue.TYPE.PACKET then
|
|
|
|
-- received a packet
|
2022-04-27 16:21:10 +00:00
|
|
|
end
|
2022-04-25 15:40:53 +00:00
|
|
|
end
|
2022-04-27 16:21:10 +00:00
|
|
|
|
2022-04-27 21:59:25 +00:00
|
|
|
-- quick yield
|
|
|
|
util.nop()
|
2022-04-25 15:40:53 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
-- check for termination request
|
2022-04-27 16:21:10 +00:00
|
|
|
if plc_state.shutdown then
|
2022-04-25 15:40:53 +00:00
|
|
|
-- safe exit
|
2022-05-05 15:55:04 +00:00
|
|
|
log.info("rps thread shutdown initiated")
|
2022-04-25 15:40:53 +00:00
|
|
|
if plc_state.init_ok then
|
2022-05-05 17:14:14 +00:00
|
|
|
if rps.scram() then
|
2022-04-25 15:40:53 +00:00
|
|
|
println_ts("reactor disabled")
|
2022-05-05 15:55:04 +00:00
|
|
|
log.info("rps thread reactor SCRAM OK")
|
2022-04-25 15:40:53 +00:00
|
|
|
else
|
|
|
|
println_ts("exiting, reactor failed to disable")
|
2022-05-05 15:55:04 +00:00
|
|
|
log.error("rps thread failed to SCRAM reactor on exit")
|
2022-04-25 15:40:53 +00:00
|
|
|
end
|
|
|
|
end
|
2022-05-05 15:55:04 +00:00
|
|
|
log.info("rps thread exiting")
|
2022-04-27 16:43:32 +00:00
|
|
|
break
|
2022-04-27 16:21:10 +00:00
|
|
|
end
|
|
|
|
|
2022-04-27 23:06:01 +00:00
|
|
|
-- delay before next check
|
2022-05-05 15:55:04 +00:00
|
|
|
last_update = util.adaptive_delay(RPS_SLEEP, last_update)
|
2022-04-25 15:40:53 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-05-21 17:56:14 +00:00
|
|
|
-- execute the thread in a protected mode, retrying it on return if not shutting down
|
2022-05-31 18:14:17 +00:00
|
|
|
function public.p_exec()
|
2022-05-21 17:56:14 +00:00
|
|
|
local plc_state = smem.plc_state
|
|
|
|
|
|
|
|
while not plc_state.shutdown do
|
|
|
|
local status, result = pcall(public.exec)
|
|
|
|
if status == false then
|
2022-09-21 19:53:51 +00:00
|
|
|
log.fatal(util.strval(result))
|
2022-05-21 17:56:14 +00:00
|
|
|
end
|
|
|
|
|
2023-04-08 20:49:54 +00:00
|
|
|
databus.tx_rt_status("rps", false)
|
|
|
|
|
2022-05-21 17:56:14 +00:00
|
|
|
if not plc_state.shutdown then
|
2022-05-22 21:57:24 +00:00
|
|
|
if plc_state.init_ok then smem.plc_sys.rps.scram() end
|
2022-05-21 17:56:14 +00:00
|
|
|
log.info("rps thread restarting in 5 seconds...")
|
|
|
|
util.psleep(5)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
return public
|
2022-04-25 15:40:53 +00:00
|
|
|
end
|
2022-04-27 16:21:10 +00:00
|
|
|
|
2022-04-30 02:27:54 +00:00
|
|
|
-- communications sender thread
|
2023-02-21 21:57:33 +00:00
|
|
|
---@nodiscard
|
2022-05-11 01:49:14 +00:00
|
|
|
---@param smem plc_shared_memory
|
2022-05-31 18:14:17 +00:00
|
|
|
function threads.thread__comms_tx(smem)
|
2023-02-21 17:27:16 +00:00
|
|
|
---@class parallel_thread
|
|
|
|
local public = {}
|
2022-05-21 17:56:14 +00:00
|
|
|
|
2022-04-27 16:21:10 +00:00
|
|
|
-- execute thread
|
2022-05-31 18:14:17 +00:00
|
|
|
function public.exec()
|
2023-04-08 20:49:54 +00:00
|
|
|
databus.tx_rt_status("comms_tx", true)
|
2022-05-04 17:37:01 +00:00
|
|
|
log.debug("comms tx thread start")
|
2022-04-27 19:52:34 +00:00
|
|
|
|
2022-04-27 16:21:10 +00:00
|
|
|
-- load in from shared memory
|
|
|
|
local plc_state = smem.plc_state
|
2022-04-29 02:36:45 +00:00
|
|
|
local comms_queue = smem.q.mq_comms_tx
|
2022-04-27 16:21:10 +00:00
|
|
|
|
2022-04-27 16:43:32 +00:00
|
|
|
local last_update = util.time()
|
|
|
|
|
2022-04-27 16:21:10 +00:00
|
|
|
-- thread loop
|
|
|
|
while true do
|
2022-05-22 21:57:24 +00:00
|
|
|
-- get plc_sys fields (may have been set late due to degraded boot)
|
|
|
|
local plc_comms = smem.plc_sys.plc_comms
|
|
|
|
|
2022-04-27 16:21:10 +00:00
|
|
|
-- check for messages in the message queue
|
2022-04-27 20:38:41 +00:00
|
|
|
while comms_queue.ready() and not plc_state.shutdown do
|
2022-04-27 16:21:10 +00:00
|
|
|
local msg = comms_queue.pop()
|
|
|
|
|
2022-05-22 21:57:24 +00:00
|
|
|
if msg ~= nil and plc_state.init_ok then
|
2022-05-11 01:49:14 +00:00
|
|
|
if msg.qtype == mqueue.TYPE.COMMAND then
|
|
|
|
-- received a command
|
|
|
|
if msg.message == MQ__COMM_CMD.SEND_STATUS then
|
|
|
|
-- send PLC/RPS status
|
2022-10-25 17:29:57 +00:00
|
|
|
plc_comms.send_status(plc_state.no_reactor, plc_state.reactor_formed)
|
2022-05-11 01:49:14 +00:00
|
|
|
plc_comms.send_rps_status()
|
|
|
|
end
|
|
|
|
elseif msg.qtype == mqueue.TYPE.DATA then
|
|
|
|
-- received data
|
|
|
|
elseif msg.qtype == mqueue.TYPE.PACKET then
|
|
|
|
-- received a packet
|
2022-04-27 16:21:10 +00:00
|
|
|
end
|
2022-04-29 02:36:45 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
-- quick yield
|
|
|
|
util.nop()
|
|
|
|
end
|
|
|
|
|
|
|
|
-- check for termination request
|
|
|
|
if plc_state.shutdown then
|
2022-05-04 17:37:01 +00:00
|
|
|
log.info("comms tx thread exiting")
|
2022-04-29 02:36:45 +00:00
|
|
|
break
|
|
|
|
end
|
|
|
|
|
|
|
|
-- delay before next check
|
|
|
|
last_update = util.adaptive_delay(COMMS_SLEEP, last_update)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-05-21 17:56:14 +00:00
|
|
|
-- execute the thread in a protected mode, retrying it on return if not shutting down
|
2022-05-31 18:14:17 +00:00
|
|
|
function public.p_exec()
|
2022-05-21 17:56:14 +00:00
|
|
|
local plc_state = smem.plc_state
|
|
|
|
|
|
|
|
while not plc_state.shutdown do
|
|
|
|
local status, result = pcall(public.exec)
|
|
|
|
if status == false then
|
2022-09-21 19:53:51 +00:00
|
|
|
log.fatal(util.strval(result))
|
2022-05-21 17:56:14 +00:00
|
|
|
end
|
|
|
|
|
2023-04-08 20:49:54 +00:00
|
|
|
databus.tx_rt_status("comms_tx", false)
|
|
|
|
|
2022-05-21 17:56:14 +00:00
|
|
|
if not plc_state.shutdown then
|
|
|
|
log.info("comms tx thread restarting in 5 seconds...")
|
|
|
|
util.psleep(5)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
return public
|
2022-04-29 02:36:45 +00:00
|
|
|
end
|
|
|
|
|
2022-04-30 02:27:54 +00:00
|
|
|
-- communications handler thread
|
2023-02-21 21:57:33 +00:00
|
|
|
---@nodiscard
|
2022-05-11 01:49:14 +00:00
|
|
|
---@param smem plc_shared_memory
|
2022-05-31 18:14:17 +00:00
|
|
|
function threads.thread__comms_rx(smem)
|
2023-02-21 17:27:16 +00:00
|
|
|
---@class parallel_thread
|
|
|
|
local public = {}
|
2022-05-21 17:56:14 +00:00
|
|
|
|
2022-04-29 02:36:45 +00:00
|
|
|
-- execute thread
|
2022-05-31 18:14:17 +00:00
|
|
|
function public.exec()
|
2023-04-08 20:49:54 +00:00
|
|
|
databus.tx_rt_status("comms_rx", true)
|
2022-05-04 17:37:01 +00:00
|
|
|
log.debug("comms rx thread start")
|
2022-04-29 02:36:45 +00:00
|
|
|
|
|
|
|
-- load in from shared memory
|
2022-05-11 01:49:14 +00:00
|
|
|
local plc_state = smem.plc_state
|
|
|
|
local setpoints = smem.setpoints
|
2022-04-29 02:36:45 +00:00
|
|
|
|
2022-05-11 01:49:14 +00:00
|
|
|
local comms_queue = smem.q.mq_comms_rx
|
2022-04-29 02:36:45 +00:00
|
|
|
|
2022-05-11 01:49:14 +00:00
|
|
|
local last_update = util.time()
|
2022-04-29 02:36:45 +00:00
|
|
|
|
|
|
|
-- thread loop
|
|
|
|
while true do
|
2022-05-22 21:57:24 +00:00
|
|
|
-- get plc_sys fields (may have been set late due to degraded boot)
|
|
|
|
local plc_comms = smem.plc_sys.plc_comms
|
|
|
|
|
2022-04-29 02:36:45 +00:00
|
|
|
-- check for messages in the message queue
|
|
|
|
while comms_queue.ready() and not plc_state.shutdown do
|
|
|
|
local msg = comms_queue.pop()
|
|
|
|
|
2022-05-22 21:57:24 +00:00
|
|
|
if msg ~= nil and plc_state.init_ok then
|
2022-05-11 01:49:14 +00:00
|
|
|
if msg.qtype == mqueue.TYPE.COMMAND then
|
|
|
|
-- received a command
|
|
|
|
elseif msg.qtype == mqueue.TYPE.DATA then
|
|
|
|
-- received data
|
|
|
|
elseif msg.qtype == mqueue.TYPE.PACKET then
|
|
|
|
-- received a packet
|
|
|
|
-- handle the packet (setpoints passed to update burn rate setpoint)
|
|
|
|
-- (plc_state passed to check if degraded)
|
2022-09-21 19:53:51 +00:00
|
|
|
plc_comms.handle_packet(msg.message, plc_state, setpoints)
|
2022-05-11 01:49:14 +00:00
|
|
|
end
|
2022-04-27 16:21:10 +00:00
|
|
|
end
|
|
|
|
|
2022-04-27 21:59:25 +00:00
|
|
|
-- quick yield
|
|
|
|
util.nop()
|
2022-04-27 16:21:10 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
-- check for termination request
|
|
|
|
if plc_state.shutdown then
|
2022-05-04 17:37:01 +00:00
|
|
|
log.info("comms rx thread exiting")
|
2022-04-27 16:43:32 +00:00
|
|
|
break
|
2022-04-27 16:21:10 +00:00
|
|
|
end
|
|
|
|
|
2022-04-27 23:06:01 +00:00
|
|
|
-- delay before next check
|
|
|
|
last_update = util.adaptive_delay(COMMS_SLEEP, last_update)
|
2022-04-27 16:21:10 +00:00
|
|
|
end
|
|
|
|
end
|
2022-04-27 19:52:34 +00:00
|
|
|
|
2022-05-21 17:56:14 +00:00
|
|
|
-- execute the thread in a protected mode, retrying it on return if not shutting down
|
2022-05-31 18:14:17 +00:00
|
|
|
function public.p_exec()
|
2022-05-21 17:56:14 +00:00
|
|
|
local plc_state = smem.plc_state
|
|
|
|
|
|
|
|
while not plc_state.shutdown do
|
|
|
|
local status, result = pcall(public.exec)
|
|
|
|
if status == false then
|
2022-09-21 19:53:51 +00:00
|
|
|
log.fatal(util.strval(result))
|
2022-05-21 17:56:14 +00:00
|
|
|
end
|
|
|
|
|
2023-04-08 20:49:54 +00:00
|
|
|
databus.tx_rt_status("comms_rx", false)
|
|
|
|
|
2022-05-21 17:56:14 +00:00
|
|
|
if not plc_state.shutdown then
|
|
|
|
log.info("comms rx thread restarting in 5 seconds...")
|
|
|
|
util.psleep(5)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
return public
|
2022-04-27 16:21:10 +00:00
|
|
|
end
|
2022-04-30 02:27:54 +00:00
|
|
|
|
2023-02-21 21:57:33 +00:00
|
|
|
-- ramp control outputs to desired setpoints
|
|
|
|
---@nodiscard
|
2022-05-11 01:49:14 +00:00
|
|
|
---@param smem plc_shared_memory
|
2022-05-31 18:14:17 +00:00
|
|
|
function threads.thread__setpoint_control(smem)
|
2023-02-21 17:27:16 +00:00
|
|
|
---@class parallel_thread
|
|
|
|
local public = {}
|
2022-05-21 17:56:14 +00:00
|
|
|
|
2022-04-30 02:27:54 +00:00
|
|
|
-- execute thread
|
2022-05-31 18:14:17 +00:00
|
|
|
function public.exec()
|
2023-04-08 20:49:54 +00:00
|
|
|
databus.tx_rt_status("spctl", true)
|
2022-05-04 17:37:01 +00:00
|
|
|
log.debug("setpoint control thread start")
|
2022-04-30 02:27:54 +00:00
|
|
|
|
|
|
|
-- load in from shared memory
|
2022-05-11 01:49:14 +00:00
|
|
|
local plc_state = smem.plc_state
|
|
|
|
local setpoints = smem.setpoints
|
|
|
|
local plc_dev = smem.plc_dev
|
2022-04-30 02:27:54 +00:00
|
|
|
|
2022-05-11 01:49:14 +00:00
|
|
|
local last_update = util.time()
|
|
|
|
local running = false
|
2022-04-30 02:27:54 +00:00
|
|
|
|
2023-02-03 03:51:21 +00:00
|
|
|
local last_burn_sp = 0.0
|
|
|
|
|
2022-05-11 01:49:14 +00:00
|
|
|
-- do not use the actual elapsed time, it could spike
|
|
|
|
-- we do not want to have big jumps as that is what we are trying to avoid in the first place
|
|
|
|
local min_elapsed_s = SP_CTRL_SLEEP / 1000.0
|
2022-04-30 02:27:54 +00:00
|
|
|
|
|
|
|
-- thread loop
|
|
|
|
while true do
|
2022-05-22 21:57:24 +00:00
|
|
|
-- get plc_sys fields (may have been set late due to degraded boot)
|
|
|
|
local rps = smem.plc_sys.rps
|
|
|
|
-- get reactor, may have changed do to disconnect/reconnect
|
2022-04-30 02:27:54 +00:00
|
|
|
local reactor = plc_dev.reactor
|
|
|
|
|
2023-02-03 03:04:26 +00:00
|
|
|
if plc_state.init_ok and (not plc_state.no_reactor) then
|
2022-05-11 01:49:14 +00:00
|
|
|
-- check if we should start ramping
|
2023-02-03 03:51:21 +00:00
|
|
|
if setpoints.burn_rate_en and (setpoints.burn_rate ~= last_burn_sp) then
|
2022-05-11 01:49:14 +00:00
|
|
|
---@diagnostic disable-next-line: need-check-nil
|
2023-02-03 03:04:26 +00:00
|
|
|
local cur_burn_rate = reactor.getBurnRate()
|
|
|
|
|
2023-02-03 03:51:21 +00:00
|
|
|
if (type(cur_burn_rate) == "number") and (setpoints.burn_rate ~= cur_burn_rate) and rps.is_active() then
|
|
|
|
last_burn_sp = setpoints.burn_rate
|
|
|
|
|
2023-02-03 03:04:26 +00:00
|
|
|
-- update without ramp if <= 2.5 mB/t change
|
|
|
|
running = math.abs(setpoints.burn_rate - cur_burn_rate) > 2.5
|
|
|
|
|
|
|
|
if running then
|
2023-02-03 03:51:21 +00:00
|
|
|
log.debug(util.c("SPCTL: starting burn rate ramp from ", cur_burn_rate, " mB/t to ", setpoints.burn_rate, " mB/t"))
|
2022-05-11 01:49:14 +00:00
|
|
|
else
|
2023-02-03 03:51:21 +00:00
|
|
|
log.debug(util.c("SPCTL: setting burn rate directly to ", setpoints.burn_rate, " mB/t"))
|
2023-02-03 03:04:26 +00:00
|
|
|
---@diagnostic disable-next-line: need-check-nil
|
|
|
|
reactor.setBurnRate(setpoints.burn_rate)
|
2022-05-11 01:49:14 +00:00
|
|
|
end
|
2022-04-30 07:03:34 +00:00
|
|
|
end
|
|
|
|
end
|
2022-04-30 02:27:54 +00:00
|
|
|
|
2022-05-11 01:49:14 +00:00
|
|
|
-- only check I/O if active to save on processing time
|
|
|
|
if running then
|
|
|
|
-- clear so we can later evaluate if we should keep running
|
|
|
|
running = false
|
|
|
|
|
|
|
|
-- adjust burn rate (setpoints.burn_rate)
|
|
|
|
if setpoints.burn_rate_en then
|
|
|
|
if rps.is_active() then
|
|
|
|
---@diagnostic disable-next-line: need-check-nil
|
|
|
|
local current_burn_rate = reactor.getBurnRate()
|
|
|
|
|
|
|
|
-- we yielded, check enable again
|
2023-02-03 01:17:23 +00:00
|
|
|
if setpoints.burn_rate_en and (type(current_burn_rate) == "number") and (current_burn_rate ~= setpoints.burn_rate) then
|
2022-05-11 01:49:14 +00:00
|
|
|
-- calculate new burn rate
|
2023-04-12 20:02:29 +00:00
|
|
|
local new_burn_rate ---@type number
|
2022-05-11 01:49:14 +00:00
|
|
|
|
|
|
|
if setpoints.burn_rate > current_burn_rate then
|
|
|
|
-- need to ramp up
|
2023-02-03 01:17:23 +00:00
|
|
|
new_burn_rate = current_burn_rate + (BURN_RATE_RAMP_mB_s * min_elapsed_s)
|
2023-02-03 03:04:26 +00:00
|
|
|
if new_burn_rate > setpoints.burn_rate then new_burn_rate = setpoints.burn_rate end
|
2022-05-11 01:49:14 +00:00
|
|
|
else
|
|
|
|
-- need to ramp down
|
2023-02-03 01:17:23 +00:00
|
|
|
new_burn_rate = current_burn_rate - (BURN_RATE_RAMP_mB_s * min_elapsed_s)
|
2023-02-03 03:04:26 +00:00
|
|
|
if new_burn_rate < setpoints.burn_rate then new_burn_rate = setpoints.burn_rate end
|
2022-05-05 20:00:49 +00:00
|
|
|
end
|
2022-04-30 02:27:54 +00:00
|
|
|
|
2023-02-03 03:04:26 +00:00
|
|
|
running = running or (new_burn_rate ~= setpoints.burn_rate)
|
|
|
|
|
2022-05-11 01:49:14 +00:00
|
|
|
-- set the burn rate
|
|
|
|
---@diagnostic disable-next-line: need-check-nil
|
|
|
|
reactor.setBurnRate(new_burn_rate)
|
|
|
|
end
|
2023-02-03 03:51:21 +00:00
|
|
|
else
|
|
|
|
log.debug("SPCTL: ramping aborted (reactor inactive)")
|
|
|
|
setpoints.burn_rate_en = false
|
2022-05-05 20:00:49 +00:00
|
|
|
end
|
2022-04-30 02:27:54 +00:00
|
|
|
end
|
2023-02-03 03:51:21 +00:00
|
|
|
elseif setpoints.burn_rate_en then
|
|
|
|
log.debug(util.c("SPCTL: ramping completed (setpoint of ", setpoints.burn_rate, " mB/t)"))
|
|
|
|
setpoints.burn_rate_en = false
|
|
|
|
end
|
|
|
|
|
|
|
|
-- if ramping completed or was aborted, reset last burn setpoint so that if it is requested again it will be re-attempted
|
2023-06-23 17:52:24 +00:00
|
|
|
if not setpoints.burn_rate_en then last_burn_sp = 0 end
|
2022-04-30 02:27:54 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
-- check for termination request
|
|
|
|
if plc_state.shutdown then
|
2022-05-04 17:37:01 +00:00
|
|
|
log.info("setpoint control thread exiting")
|
2022-04-30 02:27:54 +00:00
|
|
|
break
|
|
|
|
end
|
|
|
|
|
|
|
|
-- delay before next check
|
2022-04-30 17:44:28 +00:00
|
|
|
last_update = util.adaptive_delay(SP_CTRL_SLEEP, last_update)
|
2022-04-30 02:27:54 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-05-21 17:56:14 +00:00
|
|
|
-- execute the thread in a protected mode, retrying it on return if not shutting down
|
2022-05-31 18:14:17 +00:00
|
|
|
function public.p_exec()
|
2022-05-21 17:56:14 +00:00
|
|
|
local plc_state = smem.plc_state
|
|
|
|
|
|
|
|
while not plc_state.shutdown do
|
|
|
|
local status, result = pcall(public.exec)
|
|
|
|
if status == false then
|
2022-09-21 19:53:51 +00:00
|
|
|
log.fatal(util.strval(result))
|
2022-05-21 17:56:14 +00:00
|
|
|
end
|
|
|
|
|
2023-04-08 20:49:54 +00:00
|
|
|
databus.tx_rt_status("spctl", false)
|
|
|
|
|
2022-05-21 17:56:14 +00:00
|
|
|
if not plc_state.shutdown then
|
|
|
|
log.info("setpoint control thread restarting in 5 seconds...")
|
|
|
|
util.psleep(5)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
return public
|
2022-04-30 02:27:54 +00:00
|
|
|
end
|
2022-05-04 17:37:01 +00:00
|
|
|
|
|
|
|
return threads
|