fixed up worker loop delay logic

This commit is contained in:
Mikayla Fischler 2022-04-27 17:59:25 -04:00
parent 46a27a3f3a
commit fe3b8e6f88
4 changed files with 27 additions and 39 deletions

View File

@ -12,7 +12,7 @@ os.loadAPI("config.lua")
os.loadAPI("plc.lua") os.loadAPI("plc.lua")
os.loadAPI("threads.lua") os.loadAPI("threads.lua")
local R_PLC_VERSION = "alpha-v0.4.4" local R_PLC_VERSION = "alpha-v0.4.5"
local print = util.print local print = util.print
local println = util.println local println = util.println

View File

@ -10,9 +10,9 @@ local println_ts = util.println_ts
local psleep = util.psleep local psleep = util.psleep
local MAIN_CLOCK = 1 -- (1Hz, 20 ticks) local MAIN_CLOCK = 1 -- (1Hz, 20 ticks)
local ISS_CLOCK = 0.5 -- (2Hz, 10 ticks) local ISS_SLEEP = 500 -- (500ms, 10 ticks)
local COMMS_CLOCK = 0.25 -- (4Hz, 5 ticks) local COMMS_SLEEP = 150 -- (150ms, 3 ticks)
local MQ__ISS_CMD = { local MQ__ISS_CMD = {
SCRAM = 1, SCRAM = 1,
@ -44,9 +44,6 @@ function thread__main(smem, init)
local plc_comms = smem.plc_sys.plc_comms local plc_comms = smem.plc_sys.plc_comms
local conn_watchdog = smem.plc_sys.conn_watchdog local conn_watchdog = smem.plc_sys.conn_watchdog
-- debug
local last_update = util.time()
-- 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()
@ -71,11 +68,6 @@ function thread__main(smem, init)
end end
end end
end end
-- debug
print(util.time() - last_update)
println("ms")
last_update = util.time()
end end
elseif event == "modem_message" and networked and not plc_state.no_modem then elseif event == "modem_message" and networked and not plc_state.no_modem then
-- got a packet -- got a packet
@ -271,8 +263,8 @@ function thread__iss(smem)
-- received a packet -- received a packet
end end
-- quick yield if we are looping right back -- quick yield
if iss_queue.ready() then util.nop() end util.nop()
end end
-- check for termination request -- check for termination request
@ -300,13 +292,11 @@ function thread__iss(smem)
-- println("ms") -- println("ms")
-- last_update = util.time() -- last_update = util.time()
-- delay before next check -- delay before next check, only if >50ms since we did already yield
local sleep_for = ISS_CLOCK - (util.time() - last_update) local sleep_for = ISS_SLEEP - (util.time() - last_update)
last_update = util.time() last_update = util.time()
if sleep_for > 0 then if sleep_for >= 50 then
psleep(sleep_for) psleep(sleep_for / 1000.0)
else
psleep(0.05)
end end
end end
end end
@ -349,8 +339,8 @@ function thread__comms(smem)
plc_comms.handle_packet(msg.message, plc_state) plc_comms.handle_packet(msg.message, plc_state)
end end
-- quick yield if we are looping right back -- quick yield
if comms_queue.ready() then util.nop() end util.nop()
end end
-- check for termination request -- check for termination request
@ -359,13 +349,12 @@ function thread__comms(smem)
break break
end end
-- delay before next check -- delay before next check, only if >50ms since we did already yield
local sleep_for = COMMS_CLOCK - (util.time() - last_update) local sleep_for = COMMS_SLEEP - (util.time() - last_update)
last_update = util.time() last_update = util.time()
if sleep_for > 0 then if sleep_for >= 50 then
psleep(sleep_for) println("sleep for " .. (sleep_for / 1000.0))
else psleep(sleep_for / 1000.0)
psleep(0.05)
end end
end end
end end

View File

@ -19,7 +19,7 @@ os.loadAPI("dev/boiler_rtu.lua")
os.loadAPI("dev/imatrix_rtu.lua") os.loadAPI("dev/imatrix_rtu.lua")
os.loadAPI("dev/turbine_rtu.lua") os.loadAPI("dev/turbine_rtu.lua")
local RTU_VERSION = "alpha-v0.4.4" local RTU_VERSION = "alpha-v0.4.5"
local print = util.print local print = util.print
local println = util.println local println = util.println

View File

@ -10,8 +10,8 @@ local println_ts = util.println_ts
local psleep = util.psleep local psleep = util.psleep
local MAIN_CLOCK = 2 -- (2Hz, 40 ticks) local MAIN_CLOCK = 2 -- (2Hz, 40 ticks)
local COMMS_CLOCK = 0.25 -- (4Hz, 5 ticks) local COMMS_SLEEP = 150 -- (150ms, 3 ticks)
-- main thread -- main thread
function thread__main(smem) function thread__main(smem)
@ -134,8 +134,8 @@ function thread__comms(smem)
rtu_comms.handle_packet(msg.message, units, rtu_state) rtu_comms.handle_packet(msg.message, units, rtu_state)
end end
-- quick yield if we are looping right back -- quick yield
if comms_queue.ready() then util.nop() end util.nop()
end end
-- check for termination request -- check for termination request
@ -144,13 +144,12 @@ function thread__comms(smem)
break break
end end
-- delay before next check -- delay before next check, only if >50ms since we did already yield
local sleep_for = COMMS_CLOCK - (util.time() - last_update) local sleep_for = COMMS_SLEEP - (util.time() - last_update)
last_update = util.time() last_update = util.time()
if sleep_for > 0 then if sleep_for >= 50 then
psleep(sleep_for) println("sleep for " .. (sleep_for / 1000.0))
else psleep(sleep_for / 1000.0)
psleep(0.05)
end end
end end
end end