#604 new redstone initialization logic
This commit is contained in:
parent
1dc3d82e59
commit
be462db50b
@ -44,8 +44,9 @@ function redstone_rtu.new(relay)
|
|||||||
---@param side string
|
---@param side string
|
||||||
---@param color integer
|
---@param color integer
|
||||||
---@param invert boolean|nil
|
---@param invert boolean|nil
|
||||||
|
---@return integer count count of digital inputs
|
||||||
function public.link_di(side, color, invert)
|
function public.link_di(side, color, invert)
|
||||||
local f_read ---@type function
|
local f_read ---@type function
|
||||||
|
|
||||||
if color then
|
if color then
|
||||||
if invert then
|
if invert then
|
||||||
@ -61,16 +62,17 @@ function redstone_rtu.new(relay)
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
unit.connect_di(f_read)
|
return unit.connect_di(f_read)
|
||||||
end
|
end
|
||||||
|
|
||||||
-- link digital output
|
-- link digital output
|
||||||
---@param side string
|
---@param side string
|
||||||
---@param color integer
|
---@param color integer
|
||||||
---@param invert boolean|nil
|
---@param invert boolean|nil
|
||||||
|
---@return integer count count of digital outputs
|
||||||
function public.link_do(side, color, invert)
|
function public.link_do(side, color, invert)
|
||||||
local f_read ---@type function
|
local f_read ---@type function
|
||||||
local f_write ---@type function
|
local f_write ---@type function
|
||||||
|
|
||||||
if color then
|
if color then
|
||||||
if invert then
|
if invert then
|
||||||
@ -123,19 +125,21 @@ function redstone_rtu.new(relay)
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
unit.connect_coil(f_read, f_write)
|
return unit.connect_coil(f_read, f_write)
|
||||||
end
|
end
|
||||||
|
|
||||||
-- link analog input
|
-- link analog input
|
||||||
---@param side string
|
---@param side string
|
||||||
|
---@return integer count count of analog inputs
|
||||||
function public.link_ai(side)
|
function public.link_ai(side)
|
||||||
unit.connect_input_reg(function () return phy.getAnalogInput(side) end)
|
return unit.connect_input_reg(function () return phy.getAnalogInput(side) end)
|
||||||
end
|
end
|
||||||
|
|
||||||
-- link analog output
|
-- link analog output
|
||||||
---@param side string
|
---@param side string
|
||||||
|
---@return integer count count of analog outputs
|
||||||
function public.link_ao(side)
|
function public.link_ao(side)
|
||||||
unit.connect_holding_reg(
|
return unit.connect_holding_reg(
|
||||||
function () return phy.getAnalogOutput(side) end,
|
function () return phy.getAnalogOutput(side) end,
|
||||||
function (value) phy.setAnalogOutput(side, value) end
|
function (value) phy.setAnalogOutput(side, value) end
|
||||||
)
|
)
|
||||||
|
|||||||
17
rtu/rtu.lua
17
rtu/rtu.lua
@ -338,13 +338,7 @@ function rtu.comms(version, nic, conn_watchdog)
|
|||||||
local unit = units[i]
|
local unit = units[i]
|
||||||
|
|
||||||
if unit.type ~= nil then
|
if unit.type ~= nil then
|
||||||
local advert = { unit.type, unit.index, unit.reactor }
|
insert(advertisement, { unit.type, unit.index, unit.reactor or -1, unit.rs_conns })
|
||||||
|
|
||||||
if unit.type == RTU_UNIT_TYPE.REDSTONE then
|
|
||||||
insert(advert, unit.device)
|
|
||||||
end
|
|
||||||
|
|
||||||
insert(advertisement, advert)
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
@ -479,12 +473,7 @@ function rtu.comms(version, nic, conn_watchdog)
|
|||||||
|
|
||||||
if unit.type == RTU_UNIT_TYPE.REDSTONE then
|
if unit.type == RTU_UNIT_TYPE.REDSTONE then
|
||||||
-- immediately execute redstone RTU requests
|
-- immediately execute redstone RTU requests
|
||||||
if not unit.device then
|
return_code, reply = unit.modbus_io.handle_packet(packet)
|
||||||
reply = modbus.reply__srv_device_fail(packet)
|
|
||||||
return_code = false
|
|
||||||
else
|
|
||||||
return_code, reply = unit.modbus_io.handle_packet(packet)
|
|
||||||
end
|
|
||||||
|
|
||||||
if not return_code then
|
if not return_code then
|
||||||
log.warning("requested MODBUS operation failed" .. unit_dbg_tag)
|
log.warning("requested MODBUS operation failed" .. unit_dbg_tag)
|
||||||
@ -502,7 +491,7 @@ function rtu.comms(version, nic, conn_watchdog)
|
|||||||
unit.pkt_queue.push_packet(packet)
|
unit.pkt_queue.push_packet(packet)
|
||||||
end
|
end
|
||||||
else
|
else
|
||||||
log.warning("cannot perform requested MODBUS operation" .. unit_dbg_tag)
|
log.warning("requested MODBUS operation failed" .. unit_dbg_tag)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
else
|
else
|
||||||
|
|||||||
112
rtu/startup.lua
112
rtu/startup.lua
@ -149,9 +149,9 @@ local function main()
|
|||||||
|
|
||||||
-- configure RTU gateway based on settings file definitions
|
-- configure RTU gateway based on settings file definitions
|
||||||
local function sys_config()
|
local function sys_config()
|
||||||
-- redstone interfaces
|
--#region Redstone Interfaces
|
||||||
local rs_rtus = {} ---@type { name: string, rtu: rtu_rs_device, phy: table|nil, banks: rtu_rs_definition[][] }[]
|
|
||||||
|
|
||||||
|
local rs_rtus = {} ---@type { name: string, rtu: rtu_rs_device, phy: table, banks: rtu_rs_definition[][] }[]
|
||||||
local all_conns = {}
|
local all_conns = {}
|
||||||
|
|
||||||
-- go through redstone definitions list
|
-- go through redstone definitions list
|
||||||
@ -161,6 +161,7 @@ local function main()
|
|||||||
local assignment
|
local assignment
|
||||||
local for_reactor = entry.unit
|
local for_reactor = entry.unit
|
||||||
local phy = entry.relay or 0
|
local phy = entry.relay or 0
|
||||||
|
local phy_name = entry.relay or "local"
|
||||||
local iface_name = entry_iface_name(entry)
|
local iface_name = entry_iface_name(entry)
|
||||||
|
|
||||||
if util.is_int(entry.unit) and entry.unit > 0 and entry.unit < 5 then
|
if util.is_int(entry.unit) and entry.unit > 0 and entry.unit < 5 then
|
||||||
@ -186,10 +187,12 @@ local function main()
|
|||||||
elseif not rs_rtus[entry.relay] then
|
elseif not rs_rtus[entry.relay] then
|
||||||
log.debug(util.c("sys_config> allocated relay redstone RTU on interface ", entry.relay))
|
log.debug(util.c("sys_config> allocated relay redstone RTU on interface ", entry.relay))
|
||||||
|
|
||||||
local relay = ppm.get_device(entry.relay)
|
local relay = ppm.get_device(entry.relay)
|
||||||
|
|
||||||
if not relay then
|
if not relay then
|
||||||
log.warning(util.c("sys_config> redstone relay ", entry.relay, " is not connected"))
|
log.warning(util.c("sys_config> redstone relay ", entry.relay, " is not connected"))
|
||||||
|
local _, v_device = ppm.mount_virtual()
|
||||||
|
relay = v_device
|
||||||
elseif ppm.get_type(entry.relay) ~= "redstone_relay" then
|
elseif ppm.get_type(entry.relay) ~= "redstone_relay" then
|
||||||
log.warning(util.c("sys_config> redstone relay ", entry.relay, " is not a redstone relay"))
|
log.warning(util.c("sys_config> redstone relay ", entry.relay, " is not a redstone relay"))
|
||||||
end
|
end
|
||||||
@ -208,8 +211,8 @@ local function main()
|
|||||||
end
|
end
|
||||||
|
|
||||||
-- local rs_rtu = rs_rtus[phy].rtu
|
-- local rs_rtu = rs_rtus[phy].rtu
|
||||||
local bank = rs_rtus[phy].banks[for_reactor]
|
local bank = rs_rtus[phy].banks[for_reactor]
|
||||||
local conns = all_conns[for_reactor]
|
local conns = all_conns[for_reactor]
|
||||||
|
|
||||||
if not valid then
|
if not valid then
|
||||||
local message = util.c("sys_config> invalid redstone definition at block index #", entry_idx)
|
local message = util.c("sys_config> invalid redstone definition at block index #", entry_idx)
|
||||||
@ -222,54 +225,50 @@ local function main()
|
|||||||
if mode == rsio.IO_MODE.DIGITAL_IN then
|
if mode == rsio.IO_MODE.DIGITAL_IN then
|
||||||
-- can't have duplicate inputs
|
-- can't have duplicate inputs
|
||||||
if util.table_contains(conns, entry.port) then
|
if util.table_contains(conns, entry.port) then
|
||||||
local message = util.c("sys_config> skipping duplicate input for port ", rsio.to_string(entry.port), " on side ", iface_name)
|
local message = util.c("sys_config> skipping duplicate input for port ", rsio.to_string(entry.port), " on side ", iface_name, " @ ", phy_name)
|
||||||
println(message)
|
println(message)
|
||||||
log.warning(message)
|
log.warning(message)
|
||||||
else
|
else
|
||||||
table.insert(bank, entry)
|
table.insert(bank, entry)
|
||||||
-- rs_rtu.link_di(entry.side, entry.color, entry.invert)
|
|
||||||
end
|
end
|
||||||
elseif mode == rsio.IO_MODE.DIGITAL_OUT then
|
|
||||||
table.insert(bank, entry)
|
|
||||||
-- rs_rtu.link_do(entry.side, entry.color, entry.invert)
|
|
||||||
elseif mode == rsio.IO_MODE.ANALOG_IN then
|
elseif mode == rsio.IO_MODE.ANALOG_IN then
|
||||||
-- can't have duplicate inputs
|
-- can't have duplicate inputs
|
||||||
if util.table_contains(conns, entry.port) then
|
if util.table_contains(conns, entry.port) then
|
||||||
local message = util.c("sys_config> skipping duplicate input for port ", rsio.to_string(entry.port), " on side ", iface_name)
|
local message = util.c("sys_config> skipping duplicate input for port ", rsio.to_string(entry.port), " on side ", iface_name, " @ ", phy_name)
|
||||||
println(message)
|
println(message)
|
||||||
log.warning(message)
|
log.warning(message)
|
||||||
else
|
else
|
||||||
table.insert(bank, entry)
|
table.insert(bank, entry)
|
||||||
-- rs_rtu.link_ai(entry.side)
|
|
||||||
end
|
end
|
||||||
elseif mode == rsio.IO_MODE.ANALOG_OUT then
|
elseif (mode == rsio.IO_MODE.DIGITAL_OUT) or (mode == rsio.IO_MODE.ANALOG_OUT) then
|
||||||
table.insert(bank, entry)
|
table.insert(bank, entry)
|
||||||
-- rs_rtu.link_ao(entry.side)
|
|
||||||
else
|
else
|
||||||
-- should be unreachable code, we already validated ports
|
-- should be unreachable code, we already validated ports
|
||||||
log.error("sys_config> failed to identify IO mode at block index #" .. entry_idx, true)
|
log.fatal("sys_config> failed to identify IO mode at block index #" .. entry_idx)
|
||||||
println("sys_config> encountered a software error, check logs")
|
println("sys_config> encountered a software error, check logs")
|
||||||
return false
|
return false
|
||||||
end
|
end
|
||||||
|
|
||||||
table.insert(conns, entry.port)
|
table.insert(conns, entry.port)
|
||||||
|
|
||||||
log.debug(util.c("sys_config> banked redstone ", #conns, ": ", rsio.to_string(entry.port), " (", iface_name, ") for ", assignment))
|
log.debug(util.c("sys_config> banked redstone ", #conns, ": ", rsio.to_string(entry.port), " (", iface_name, " @ ", phy_name, ") for ", assignment))
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
-- create unit entries for redstone RTUs
|
-- create unit entries for redstone RTUs
|
||||||
for _, def in pairs(rs_rtus) do
|
for _, def in pairs(rs_rtus) do
|
||||||
local rtu_conns = { [0] = {}, {}, {}, {}, {}}
|
local rtu_conns = { [0] = {}, {}, {}, {}, {} }
|
||||||
|
|
||||||
-- connect the IO banks
|
-- connect the IO banks
|
||||||
for for_reactor = 0, #def.banks do
|
for for_reactor = 0, #def.banks do
|
||||||
local bank = def.banks[for_reactor]
|
local bank = def.banks[for_reactor]
|
||||||
local conns = rtu_conns[for_reactor]
|
local conns = rtu_conns[for_reactor]
|
||||||
|
local assign = util.trinary(for_reactor > 0, "reactor unit " .. for_reactor, "the facility")
|
||||||
|
|
||||||
-- link redstone to the RTU
|
-- link redstone to the RTU
|
||||||
for i = 1, #bank do
|
for i = 1, #bank do
|
||||||
local conn = bank[i]
|
local conn = bank[i]
|
||||||
|
local phy_name = conn.relay or "local"
|
||||||
|
|
||||||
local mode = rsio.get_io_mode(conn.port)
|
local mode = rsio.get_io_mode(conn.port)
|
||||||
if mode == rsio.IO_MODE.DIGITAL_IN then
|
if mode == rsio.IO_MODE.DIGITAL_IN then
|
||||||
@ -280,44 +279,52 @@ local function main()
|
|||||||
def.rtu.link_ai(conn.side)
|
def.rtu.link_ai(conn.side)
|
||||||
elseif mode == rsio.IO_MODE.ANALOG_OUT then
|
elseif mode == rsio.IO_MODE.ANALOG_OUT then
|
||||||
def.rtu.link_ao(conn.side)
|
def.rtu.link_ao(conn.side)
|
||||||
|
else
|
||||||
|
log.fatal(util.c("sys_config> failed to identify IO mode of ", rsio.to_string(conn.port), " (", entry_iface_name(conn), " @ ", phy_name, ") for ", assign))
|
||||||
|
println("sys_config> encountered a software error, check logs")
|
||||||
|
return false
|
||||||
end
|
end
|
||||||
|
|
||||||
table.insert(conns, conn.port)
|
table.insert(conns, conn.port)
|
||||||
|
|
||||||
log.debug(util.c("sys_config> linked redstone ", for_reactor, ".", #conns, ": ", rsio.to_string(conn.port), " (", entry_iface_name(conn), ")"))
|
log.debug(util.c("sys_config> linked redstone ", for_reactor, ".", #conns, ": ", rsio.to_string(conn.port), " (", entry_iface_name(conn), ")", " @ ", phy_name, ") for ", assign))
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
local hw_state = util.trinary(def.phy, RTU_HW_STATE.OK, RTU_HW_STATE.OFFLINE)
|
local hw_state = util.trinary(def.phy, RTU_HW_STATE.OK, RTU_HW_STATE.OFFLINE)
|
||||||
|
|
||||||
---@class rtu_registry_entry
|
---@type rtu_registry_entry
|
||||||
local unit = {
|
local unit = {
|
||||||
uid = 0, ---@type integer
|
uid = 0,
|
||||||
name = def.name, ---@type string
|
name = def.name,
|
||||||
type = RTU_UNIT_TYPE.REDSTONE, ---@type RTU_UNIT_TYPE
|
type = RTU_UNIT_TYPE.REDSTONE,
|
||||||
index = false, ---@type integer|false
|
index = false,
|
||||||
reactor = nil, ---@type nil
|
reactor = nil,
|
||||||
device = def.phy, ---@type table|nil
|
device = def.phy,
|
||||||
rs_conns = rtu_conns, ---@type IO_PORT[][]|nil
|
rs_conns = rtu_conns,
|
||||||
is_multiblock = false, ---@type boolean
|
is_multiblock = false,
|
||||||
formed = nil, ---@type boolean|nil
|
formed = nil,
|
||||||
hw_state = hw_state, ---@type RTU_HW_STATE
|
hw_state = hw_state,
|
||||||
rtu = def.rtu, ---@type rtu_device|rtu_rs_device
|
rtu = def.rtu,
|
||||||
modbus_io = modbus.new(def.rtu, false),
|
modbus_io = modbus.new(def.rtu, false),
|
||||||
pkt_queue = nil, ---@type mqueue|nil
|
pkt_queue = nil,
|
||||||
thread = nil ---@type parallel_thread|nil
|
thread = nil
|
||||||
}
|
}
|
||||||
|
|
||||||
table.insert(units, unit)
|
table.insert(units, unit)
|
||||||
|
|
||||||
log.info(util.c("sys_config> initialized RTU unit #", #units, ": ", unit.name, " (redstone)"))
|
local type = util.trinary(def.phy == rs, "redstone", "redstone_relay")
|
||||||
|
|
||||||
|
log.info(util.c("sys_config> initialized RTU unit #", #units, ": ", unit.name, " (", type, ")"))
|
||||||
|
|
||||||
unit.uid = #units
|
unit.uid = #units
|
||||||
|
|
||||||
databus.tx_unit_hw_status(unit.uid, unit.hw_state)
|
databus.tx_unit_hw_status(unit.uid, unit.hw_state)
|
||||||
end
|
end
|
||||||
|
|
||||||
-- mounted peripherals
|
--#endregion
|
||||||
|
--#region Mounted Peripherals
|
||||||
|
|
||||||
for i = 1, #rtu_devices do
|
for i = 1, #rtu_devices do
|
||||||
local entry = rtu_devices[i] ---@type rtu_peri_definition
|
local entry = rtu_devices[i] ---@type rtu_peri_definition
|
||||||
local name = entry.name
|
local name = entry.name
|
||||||
@ -498,19 +505,20 @@ local function main()
|
|||||||
|
|
||||||
---@class rtu_registry_entry
|
---@class rtu_registry_entry
|
||||||
local rtu_unit = {
|
local rtu_unit = {
|
||||||
uid = 0, ---@type integer
|
uid = 0, ---@type integer RTU unit ID
|
||||||
name = name, ---@type string
|
name = name, ---@type string unit name
|
||||||
type = rtu_type, ---@type RTU_UNIT_TYPE
|
type = rtu_type, ---@type RTU_UNIT_TYPE unit type
|
||||||
index = index or false, ---@type integer|false
|
index = index or false, ---@type integer|false device index
|
||||||
reactor = for_reactor, ---@type integer
|
reactor = for_reactor, ---@type integer|nil unit/facility assignment
|
||||||
device = device, ---@type table peripheral reference
|
device = device, ---@type table peripheral reference
|
||||||
is_multiblock = is_multiblock, ---@type boolean
|
rs_conns = nil, ---@type IO_PORT[][]|nil available redstone connections
|
||||||
formed = formed, ---@type boolean|nil
|
is_multiblock = is_multiblock, ---@type boolean if this is for a multiblock peripheral
|
||||||
hw_state = RTU_HW_STATE.OFFLINE, ---@type RTU_HW_STATE
|
formed = formed, ---@type boolean|nil if this peripheral is currently formed
|
||||||
rtu = rtu_iface, ---@type rtu_device|rtu_rs_device
|
hw_state = RTU_HW_STATE.OFFLINE, ---@type RTU_HW_STATE hardware device status
|
||||||
modbus_io = modbus.new(rtu_iface, true),
|
rtu = rtu_iface, ---@type rtu_device|rtu_rs_device RTU hardware interface
|
||||||
pkt_queue = mqueue.new(), ---@type mqueue|nil
|
modbus_io = modbus.new(rtu_iface, true), ---@type modbus MODBUS interface
|
||||||
thread = nil ---@type parallel_thread|nil
|
pkt_queue = mqueue.new(), ---@type mqueue|nil packet queue
|
||||||
|
thread = nil ---@type parallel_thread|nil associated RTU thread
|
||||||
}
|
}
|
||||||
|
|
||||||
rtu_unit.thread = threads.thread__unit_comms(__shared_memory, rtu_unit)
|
rtu_unit.thread = threads.thread__unit_comms(__shared_memory, rtu_unit)
|
||||||
@ -544,6 +552,8 @@ local function main()
|
|||||||
databus.tx_unit_hw_status(rtu_unit.uid, rtu_unit.hw_state)
|
databus.tx_unit_hw_status(rtu_unit.uid, rtu_unit.hw_state)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
--#endregion
|
||||||
|
|
||||||
return true
|
return true
|
||||||
end
|
end
|
||||||
|
|
||||||
@ -612,7 +622,7 @@ local function main()
|
|||||||
-- run threads
|
-- run threads
|
||||||
parallel.waitForAll(table.unpack(_threads))
|
parallel.waitForAll(table.unpack(_threads))
|
||||||
else
|
else
|
||||||
println("configuration failed, exiting...")
|
println("system initialization failed, exiting...")
|
||||||
end
|
end
|
||||||
|
|
||||||
renderer.close_ui()
|
renderer.close_ui()
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user