threaded RTU/PLC bugfixes

This commit is contained in:
Mikayla Fischler 2022-04-27 15:52:34 -04:00
parent 14377e7348
commit 67a93016c0
4 changed files with 39 additions and 14 deletions

View File

@ -12,7 +12,7 @@ os.loadAPI("config.lua")
os.loadAPI("plc.lua")
os.loadAPI("threads.lua")
local R_PLC_VERSION = "alpha-v0.4.0"
local R_PLC_VERSION = "alpha-v0.4.1"
local print = util.print
local println = util.println
@ -58,7 +58,7 @@ local __shared_memory = {
-- message queues
q = {
mq_iss = mqueue.new(),
mq_comms = mqeueu.new()
mq_comms = mqueue.new()
}
}

View File

@ -26,6 +26,8 @@ local MQ__COMM_CMD = {
function thread__main(smem, init)
-- execute thread
local exec = function ()
log._debug("main thread init, clock inactive")
-- send status updates at 2Hz (every 10 server ticks) (every loop tick)
-- send link requests at 0.5Hz (every 40 server ticks) (every 4 loop ticks)
local LINK_TICKS = 4
@ -168,7 +170,7 @@ function thread__main(smem, init)
elseif event == "clock_start" then
-- start loop clock
loop_clock = os.startTimer(MAIN_CLOCK)
log._debug("main thread started")
log._debug("main thread clock started")
end
-- check for termination request
@ -188,6 +190,8 @@ end
function thread__iss(smem)
-- execute thread
local exec = function ()
log._debug("iss thread start")
-- load in from shared memory
local networked = smem.networked
local plc_state = smem.plc_state
@ -233,8 +237,8 @@ function thread__iss(smem)
end
-- check for messages in the message queue
while comms_queue.ready() do
local msg = comms_queue.pop()
while iss_queue.ready() do
local msg = iss_queue.pop()
if msg.qtype == mqueue.TYPE.COMMAND then
-- received a command
@ -265,7 +269,7 @@ function thread__iss(smem)
-- received a packet
end
-- quick yield
-- quick yield if we are looping right back
if iss_queue.ready() then util.nop() end
end
@ -296,8 +300,11 @@ function thread__iss(smem)
-- delay before next check
local sleep_for = ISS_CLOCK - (util.time() - last_update)
if sleep_for > 0.05 then
last_update = util.time()
if sleep_for > 0 then
sleep(sleep_for)
else
sleep(0.05)
end
end
end
@ -309,6 +316,8 @@ end
function thread__comms(smem)
-- execute thread
local exec = function ()
log._debug("comms thread start")
-- load in from shared memory
local plc_state = smem.plc_state
local plc_comms = smem.plc_sys.plc_comms
@ -338,7 +347,7 @@ function thread__comms(smem)
plc_comms.handle_packet(msg.message, plc_state)
end
-- quick yield
-- quick yield if we are looping right back
if comms_queue.ready() then util.nop() end
end
@ -350,9 +359,14 @@ function thread__comms(smem)
-- delay before next check
local sleep_for = COMMS_CLOCK - (util.time() - last_update)
if sleep_for > 0.05 then
last_update = util.time()
if sleep_for > 0 then
sleep(sleep_for)
else
sleep(0.05)
end
end
end
return { exec = exec }
end

View File

@ -19,7 +19,7 @@ os.loadAPI("dev/boiler_rtu.lua")
os.loadAPI("dev/imatrix_rtu.lua")
os.loadAPI("dev/turbine_rtu.lua")
local RTU_VERSION = "alpha-v0.4.0"
local RTU_VERSION = "alpha-v0.4.1"
local print = util.print
local println = util.println
@ -58,11 +58,12 @@ local __shared_memory = {
-- message queues
q = {
mq_comms = mqeueu.new()
mq_comms = mqueue.new()
}
}
local smem_dev = __shared_memory.rtu_dev
local smem_sys = __shared_memory.rtu_sys
-- get modem
if smem_dev.modem == nil then
@ -71,7 +72,7 @@ if smem_dev.modem == nil then
return
end
local rtu_comms = rtu.rtu_comms(modem, config.LISTEN_PORT, config.SERVER_PORT)
smem_sys.rtu_comms = rtu.rtu_comms(smem_dev.modem, config.LISTEN_PORT, config.SERVER_PORT)
----------------------------------------
-- interpret config and init units

View File

@ -15,6 +15,8 @@ local COMMS_CLOCK = 0.25 -- (4Hz, 5 ticks)
function thread__main(smem)
-- execute thread
local exec = function ()
log._debug("main thread start")
-- advertisement/heartbeat clock
local loop_clock = os.startTimer(MAIN_CLOCK)
@ -22,6 +24,7 @@ function thread__main(smem)
local rtu_state = smem.rtu_state
local rtu_dev = smem.rtu_dev
local rtu_comms = smem.rtu_sys.rtu_comms
local units = smem.rtu_sys.units
-- event loop
while true do
@ -102,6 +105,8 @@ end
function thread__comms(smem)
-- execute thread
local exec = function ()
log._debug("comms thread start")
-- load in from shared memory
local rtu_state = smem.rtu_state
local rtu_comms = smem.rtu_sys.rtu_comms
@ -127,7 +132,7 @@ function thread__comms(smem)
rtu_comms.handle_packet(msg.message, units, rtu_state)
end
-- quick yield
-- quick yield if we are looping right back
if comms_queue.ready() then util.nop() end
end
@ -139,9 +144,14 @@ function thread__comms(smem)
-- delay before next check
local sleep_for = COMMS_CLOCK - (util.time() - last_update)
if sleep_for > 0.05 then
last_update = util.time()
if sleep_for > 0 then
sleep(sleep_for)
else
sleep(0.05)
end
end
end
return { exec = exec }
end