work on PLC comms

This commit is contained in:
Mikayla Fischler
2022-01-22 14:26:25 -05:00
parent c6722c4cbe
commit ffca88845b
3 changed files with 277 additions and 121 deletions

View File

@@ -1,8 +1,8 @@
function scada_link(comms)
function scada_link(plc_comms)
local linked = false
local link_timeout = os.startTimer(5)
comms.send_link_req()
plc_comms.send_link_req()
print_ts("sent link request")
repeat
@@ -12,31 +12,40 @@ function scada_link(comms)
if event == "timer" and param1 == link_timeout then
-- no response yet
print("...no response");
comms.send_link_req()
print_ts("sent link request")
link_timeout = os.startTimer(5)
elseif event == "modem_message" then
-- server response? cancel timeout
if link_timeout ~= nil then
os.cancelTimer(link_timeout)
end
local packet = comms.make_packet(p1, p2, p3, p4, p5)
-- handle response
local response = comms.handle_link(packet)
if response == nil then
print_ts("invalid link response, bad channel?\n")
return
elseif response == true then
print_ts("...linked!\n")
linked = true
else
print_ts("...denied, exiting\n")
return
local s_packet = comms.scada_packet()
s_packet.receive(p1, p2, p3, p4, p5)
local packet = s_packet.as_rplc()
if packet then
-- handle response
local response = plc_comms.handle_link(packet)
if response == nil then
print_ts("invalid link response, bad channel?\n")
break
elseif response == comms.RPLC_LINKING.COLLISION then
print_ts("...reactor PLC ID collision (check config), exiting...\n")
break
elseif response == comms.RPLC_LINKING.ALLOW then
print_ts("...linked!\n")
linked = true
plc_comms.send_rs_io_conns()
plc_comms.send_struct()
plc_comms.send_status()
print_ts("sent initial data\n")
else
print_ts("...denied, exiting...\n")
break
end
end
end
until linked
return linked
end
-- Internal Safety System
@@ -44,13 +53,15 @@ end
-- autonomous from main control
function iss_init(reactor)
local self = {
_reactor = reactor,
_tripped = false,
_trip_cause = ""
reactor = reactor,
timed_out = false,
tripped = false,
trip_cause = ""
}
local check = function ()
local status = "ok"
local was_tripped = self.tripped
-- check system states in order of severity
if self.damage_critical() then
@@ -63,59 +74,100 @@ function iss_init(reactor)
status = "full_waste"
elseif self.insufficient_fuel() then
status = "no_fuel"
elseif self._tripped then
status = self._trip_cause
elseif self.tripped then
status = self.trip_cause
else
self._tripped = false
self.tripped = false
end
if status ~= "ok" then
self._tripped = true
self._trip_cause = status
self._reactor.scram()
self.tripped = true
self.trip_cause = status
self.reactor.scram()
end
local first_trip = ~was_tripped and self.tripped
return self._tripped, status
return self.tripped, status, first_trip
end
local trip_timeout = function ()
self.tripped = false
self.trip_cause = "timeout"
self.timed_out = true
self.reactor.scram()
end
local reset = function ()
self._tripped = false
self._trip_cause = ""
self.timed_out = false
self.tripped = false
self.trip_cause = ""
end
local status = function (named)
if named then
return {
damage_critical = damage_critical(),
excess_heated_coolant = excess_heated_coolant(),
excess_waste = excess_waste(),
high_temp = high_temp(),
insufficient_fuel = insufficient_fuel(),
no_coolant = no_coolant(),
timed_out = timed_out()
}
else
return {
damage_critical(),
excess_heated_coolant(),
excess_waste(),
high_temp(),
insufficient_fuel(),
no_coolant(),
timed_out()
}
end
end
local damage_critical = function ()
return self._reactor.getDamagePercent() >= 100
return self.reactor.getDamagePercent() >= 100
end
local excess_heated_coolant = function ()
return self._reactor.getHeatedCoolantNeeded() == 0
return self.reactor.getHeatedCoolantNeeded() == 0
end
local excess_waste = function ()
return self._reactor.getWasteNeeded() == 0
return self.reactor.getWasteNeeded() == 0
end
local high_temp = function ()
-- mekanism: MAX_DAMAGE_TEMPERATURE = 1_200
return self._reactor.getTemperature() >= 1200
return self.reactor.getTemperature() >= 1200
end
local insufficient_fuel = function ()
return self._reactor.getFuel() == 0
return self.reactor.getFuel() == 0
end
local no_coolant = function()
return self._reactor.getCoolantFilledPercentage() < 2
local no_coolant = function ()
return self.reactor.getCoolantFilledPercentage() < 2
end
local timed_out = function ()
return self.timed_out
end
return {
check = check,
trip_timeout = trip_timeout,
reset = reset,
status = status,
damage_critical = damage_critical,
excess_heated_coolant = excess_heated_coolant,
excess_waste = excess_waste,
high_temp = high_temp,
insufficient_fuel = insufficient_fuel,
no_coolant = no_coolant
no_coolant = no_coolant,
timed_out = timed_out
}
end

View File

@@ -37,11 +37,13 @@ if not modem.isOpen(config.LISTEN_PORT) then
modem.open(config.LISTEN_PORT)
end
local comms = comms.rplc_comms(config.REACTOR_ID, modem, config.LISTEN_PORT, config.SERVER_PORT, reactor)
local plc_comms = comms.rplc_comms(config.REACTOR_ID, modem, config.LISTEN_PORT, config.SERVER_PORT, reactor)
-- attempt server connection
-- exits application if connection is denied
plc.scada_link(comms)
-- exit application if connection is denied
if ~plc.scada_link(plc_comms) then
return
end
-- comms watchdog, 3 second timeout
local conn_watchdog = watchdog.new_watchdog(3)
@@ -51,28 +53,47 @@ local conn_watchdog = watchdog.new_watchdog(3)
local loop_tick = os.startTimer(0.05)
local ticks_to_update = 5
-- runtime variables
local control_state = false
-- event loop
while true do
local event, param1, param2, param3, param4, param5 = os.pullEvent()
if event == "peripheral_detach" then
print_ts("[fatal] lost a peripheral, stopping...\n")
-- todo: determine which disconnected and what is left
-- hopefully it wasn't the reactor
reactor.scram()
-- send an alarm: plc_comms.send_alarm(ALARMS.PLC_DC) ?
return
end
-- check safety (SCRAM occurs if tripped)
local iss_status, iss_tripped = iss.check()
local iss_status, iss_tripped, iss_first = iss.check()
if iss_first then
plc_comms.send_iss_alarm(iss_status)
end
-- handle event
if event == "timer" and param1 == loop_tick then
-- basic event tick, send updated data if it is time
-- basic event tick, send updated data if it is time (4Hz)
ticks_to_update = ticks_to_update - 1
if ticks_to_update == 0 then
plc_comms.send_status(control_state, iss_tripped)
ticks_to_update = 5
end
elseif event == "modem_message" then
-- got a packet
-- feed the watchdog first so it doesn't eat our packets
-- feed the watchdog first so it doesn't uhh,,,eat our packets
conn_watchdog.feed()
local packet = comms.make_packet(p1, p2, p3, p4, p5)
plc_comms.handle_packet(packet)
elseif event == "timer" and param1 == conn_watchdog.get_timer() then
-- haven't heard from server recently? shutdown
reactor.scram()
iss.trip_timeout()
print_ts("[alert] server timeout, reactor disabled\n")
end
end