#134 #104 redstone RTU integration with supervisor unit, waste routing implemented, changed how redstone I/O works (again, should be good now), modbus fixes

This commit is contained in:
Mikayla Fischler
2022-11-30 23:31:14 -05:00
parent 9c27ac7ae6
commit e1d7c7b1c0
17 changed files with 527 additions and 346 deletions

View File

@@ -31,17 +31,17 @@ config.RTU_REDSTONE = {
-- for_reactor = 1,
-- io = {
-- {
-- channel = rsio.IO.WASTE_PO,
-- port = rsio.IO.WASTE_PO,
-- side = "top",
-- bundled_color = colors.blue
-- },
-- {
-- channel = rsio.IO.WASTE_PU,
-- port = rsio.IO.WASTE_PU,
-- side = "top",
-- bundled_color = colors.cyan
-- },
-- {
-- channel = rsio.IO.WASTE_AM,
-- port = rsio.IO.WASTE_AM,
-- side = "top",
-- bundled_color = colors.purple
-- }

View File

@@ -4,9 +4,10 @@ local rsio = require("scada-common.rsio")
local redstone_rtu = {}
local IO_LVL = rsio.IO_LVL
local digital_read = rsio.digital_read
local digital_write = rsio.digital_write
local digital_is_active = rsio.digital_is_active
-- create new redstone device
function redstone_rtu.new()
@@ -47,10 +48,9 @@ function redstone_rtu.new()
end
-- link digital output
---@param channel RS_IO
---@param side string
---@param color integer
function public.link_do(channel, side, color)
function public.link_do(side, color)
local f_read = nil
local f_write = nil
@@ -60,15 +60,17 @@ function redstone_rtu.new()
end
f_write = function (level)
local output = rs.getBundledOutput(side)
if level ~= IO_LVL.FLOATING and level ~= IO_LVL.DISCONNECT then
local output = rs.getBundledOutput(side)
if digital_write(channel, level) then
output = colors.combine(output, color)
else
output = colors.subtract(output, color)
if digital_write(level) then
output = colors.combine(output, color)
else
output = colors.subtract(output, color)
end
rs.setBundledOutput(side, output)
end
rs.setBundledOutput(side, output)
end
else
f_read = function ()
@@ -76,7 +78,9 @@ function redstone_rtu.new()
end
f_write = function (level)
rs.setOutput(side, digital_is_active(channel, level))
if level ~= IO_LVL.FLOATING and level ~= IO_LVL.DISCONNECT then
rs.setOutput(side, digital_write(level))
end
end
end

View File

@@ -366,7 +366,7 @@ function modbus.new(rtu_dev, use_parallel_read)
local return_code = true
local response = nil
if packet.length == 2 then
if packet.length >= 2 then
-- handle by function code
if packet.func_code == MODBUS_FCODE.READ_COILS then
return_code, response = _1_read_coils(packet.data[1], packet.data[2])
@@ -381,9 +381,9 @@ function modbus.new(rtu_dev, use_parallel_read)
elseif packet.func_code == MODBUS_FCODE.WRITE_SINGLE_HOLD_REG then
return_code, response = _6_write_single_holding_register(packet.data[1], packet.data[2])
elseif packet.func_code == MODBUS_FCODE.WRITE_MUL_COILS then
return_code, response = _15_write_multiple_coils(packet.data[1], packet.data[2])
return_code, response = _15_write_multiple_coils(packet.data[1], { table.unpack(packet.data, 2, packet.length) })
elseif packet.func_code == MODBUS_FCODE.WRITE_MUL_HOLD_REGS then
return_code, response = _16_write_multiple_holding_registers(packet.data[1], packet.data[2])
return_code, response = _16_write_multiple_holding_registers(packet.data[1], { table.unpack(packet.data, 2, packet.length) })
else
-- unknown function
return_code = false
@@ -392,6 +392,7 @@ function modbus.new(rtu_dev, use_parallel_read)
else
-- invalid length
return_code = false
response = MODBUS_EXCODE.NEG_ACKNOWLEDGE
end
-- default is to echo back

View File

@@ -25,7 +25,7 @@ local sna_rtu = require("rtu.dev.sna_rtu")
local sps_rtu = require("rtu.dev.sps_rtu")
local turbinev_rtu = require("rtu.dev.turbinev_rtu")
local RTU_VERSION = "beta-v0.9.7"
local RTU_VERSION = "beta-v0.9.8"
local rtu_t = types.rtu_t
@@ -166,7 +166,7 @@ local function main()
local conf = io_table[i]
-- verify configuration
if rsio.is_valid_channel(conf.channel) and rsio.is_valid_side(conf.side) then
if rsio.is_valid_port(conf.port) and rsio.is_valid_side(conf.side) then
if conf.bundled_color then
valid = rsio.is_color(conf.bundled_color)
else
@@ -182,22 +182,22 @@ local function main()
return false
else
-- link redstone in RTU
local mode = rsio.get_io_mode(conf.channel)
local mode = rsio.get_io_mode(conf.port)
if mode == rsio.IO_MODE.DIGITAL_IN then
-- can't have duplicate inputs
if util.table_contains(capabilities, conf.channel) then
local message = util.c("configure> skipping duplicate input for channel ", rsio.to_string(conf.channel), " on side ", conf.side)
if util.table_contains(capabilities, conf.port) then
local message = util.c("configure> skipping duplicate input for port ", rsio.to_string(conf.port), " on side ", conf.side)
println(message)
log.warning(message)
else
rs_rtu.link_di(conf.side, conf.bundled_color)
end
elseif mode == rsio.IO_MODE.DIGITAL_OUT then
rs_rtu.link_do(conf.channel, conf.side, conf.bundled_color)
rs_rtu.link_do(conf.side, conf.bundled_color)
elseif mode == rsio.IO_MODE.ANALOG_IN then
-- can't have duplicate inputs
if util.table_contains(capabilities, conf.channel) then
local message = util.c("configure> skipping duplicate input for channel ", rsio.to_string(conf.channel), " on side ", conf.side)
if util.table_contains(capabilities, conf.port) then
local message = util.c("configure> skipping duplicate input for port ", rsio.to_string(conf.port), " on side ", conf.side)
println(message)
log.warning(message)
else
@@ -206,15 +206,15 @@ local function main()
elseif mode == rsio.IO_MODE.ANALOG_OUT then
rs_rtu.link_ao(conf.side)
else
-- should be unreachable code, we already validated channels
-- should be unreachable code, we already validated ports
log.error("configure> fell through if chain attempting to identify IO mode", true)
println("configure> encountered a software error, check logs")
return false
end
table.insert(capabilities, conf.channel)
table.insert(capabilities, conf.port)
log.debug(util.c("configure> linked redstone ", #capabilities, ": ", rsio.to_string(conf.channel),
log.debug(util.c("configure> linked redstone ", #capabilities, ": ", rsio.to_string(conf.port),
" (", conf.side, ") for reactor ", io_reactor))
end
end
@@ -225,7 +225,7 @@ local function main()
type = rtu_t.redstone,
index = entry_idx,
reactor = io_reactor,
device = capabilities, -- use device field for redstone channels
device = capabilities, -- use device field for redstone ports
formed = nil, ---@type boolean|nil
rtu = rs_rtu, ---@type rtu_device|rtu_rs_device
modbus_io = modbus.new(rs_rtu, false),