Newer
Older
ibsystem / ibmanager / logic / scripts / utils / Hysteresis.lua

-- module defines Hystersis class, that handles below functionality
--
--                             threshold
--                                 |                                ^
--                                 V                                |
--                              +--*--+-<-->-- <- valueAfter        |
--                              |     |                             |
--                              |     |                             |
--                              v     ^                             |
--                              |     |                             | outputValue
--                              |     |                             |
--                              |     |                             |
-- valueBefore -> ---<-->-------+--*--+                             |
--                              ^     ^                             |
--                              |     |                             |
--                              |width|                             |
--
-- ----------------------------------------------------------------->
--                                inputValue


-- class definition
Hysteresis = {};

Hysteresis.__index = Hysteresis;

-- constructor
function Hysteresis.create(threshold, valueBefore, valueAfter, width, outputValue)

  local hyst = {};                  -- our new object
  setmetatable(hyst, Hysteresis);    -- make Hysteresis handle lookup

  -- initialize our object
  hyst.threshold = threshold;
  hyst.valueBefore = valueBefore;
  hyst.valueAfter = valueAfter;
  hyst.width = width;
  hyst.outputValue = outputValue;

  return hyst;

  end


-- function updates parameters
-- should be called if below parameters can change
function Hysteresis:updateParameters(threshold, valueBefore, valueAfter, width)

  self.threshold = threshold;
  self.valueBefore = valueBefore;
  self.valueAfter = valueAfter;
  self.width = width;

end

--returns output values on the basis of hysteresis parameters and input value
function Hysteresis:getOutputValue(inputValue)

  if inputValue >= (self.threshold + (self.width / 2)) then
    self.outputValue = self.valueAfter;
  elseif inputValue <= (self.threshold - (self.width / 2)) then
    self.outputValue = self.valueBefore;
  end

  return self.outputValue;

end