From 6f768ef6b3aea88defdd8c1944e7c419a8c0d5d0 Mon Sep 17 00:00:00 2001 From: Mikayla Fischler Date: Sun, 28 Apr 2024 01:26:44 -0400 Subject: [PATCH] #469 made ETA tolerant to induction matrix capacity changes --- scada-common/util.lua | 10 +++++++--- supervisor/facility.lua | 15 ++++++++++----- 2 files changed, 17 insertions(+), 8 deletions(-) diff --git a/scada-common/util.lua b/scada-common/util.lua index 3b876e3..d29a85e 100644 --- a/scada-common/util.lua +++ b/scada-common/util.lua @@ -189,11 +189,15 @@ function util.mov_avg(length) ---@class moving_average local public = {} - -- reset all to a given value - ---@param x number value + -- reset all to a given value, or clear all data if no value is given + ---@param x number? value function public.reset(x) + index = 1 data = {} - for _ = 1, length do t_insert(data, x) end + + if x then + for _ = 1, length do t_insert(data, x) end + end end -- record a new value diff --git a/supervisor/facility.lua b/supervisor/facility.lua index 0fd0ed9..51283d1 100644 --- a/supervisor/facility.lua +++ b/supervisor/facility.lua @@ -133,6 +133,7 @@ function facility.new(config, cooling_conf) avg_outflow = util.mov_avg(6), -- 3 seconds -- induction matrix charge delta stats avg_net = util.mov_avg(60), -- 60 seconds + last_capacity = 0, charge_last = 0, charge_last_t = 0 } @@ -326,7 +327,13 @@ function facility.new(config, cooling_conf) self.charge_last = energy self.charge_last_t = charge_update - self.avg_net.record(delta, charge_update) + -- if the capacity changed, toss out existing data + if db.build.max_energy ~= self.last_capacity then + self.last_capacity = db.build.max_energy + self.avg_net.reset() + else + self.avg_net.record(delta, charge_update) + end end else self.im_stat_init = true @@ -641,8 +648,7 @@ function facility.new(config, cooling_conf) local astatus = self.ascram_status if self.induction[1] ~= nil then - local matrix = self.induction[1] ---@type unit_session - local db = matrix.get_db() ---@type imatrix_session_db + local db = self.induction[1].get_db() ---@type imatrix_session_db -- clear matrix disconnected if astatus.matrix_dc then @@ -798,8 +804,7 @@ function facility.new(config, cooling_conf) -- update induction matrix related outputs if self.induction[1] ~= nil then - local matrix = self.induction[1] ---@type unit_session - local db = matrix.get_db() ---@type imatrix_session_db + local db = self.induction[1].get_db() ---@type imatrix_session_db self.io_ctl.digital_write(IO.F_MATRIX_LOW, db.tanks.energy_fill < const.RS_THRESHOLDS.IMATRIX_CHARGE_LOW) self.io_ctl.digital_write(IO.F_MATRIX_HIGH, db.tanks.energy_fill > const.RS_THRESHOLDS.IMATRIX_CHARGE_HIGH)