from ScEpTIC.emulator.energy import energy_utils from ScEpTIC.emulator.energy.system_energy_model import SystemEnergyModel from ScEpTIC.exceptions import ConfigurationException class ChangePointDetectorLogic: """ Base model of the FBTC Changepoint Detector. """ def __init__(self, R1, R2, system_model): """ :param R1: R1 of the changepoint detector's voltage divider :param R2: R2 of the changepoint detector's voltage divider :param system_model: the SystemEnergyModel """ if not isinstance(system_model, SystemEnergyModel): raise ConfigurationException(f"{system_model.__class__.__name__} is not a valid SystemEnergyModel for {self.__class__.__name__}") self.system_model = system_model self.R1 = energy_utils.str_to_float(R1) self.R2 = energy_utils.str_to_float(R2) self.partition = self.R2 / (self.R1 + self.R2) def output(self, vreg_voltage): """ Outputs the logical value of the Changepoint Detector :param vreg_voltage: voltage of the voltage regulator :return: a changepoint is reached """ raise Exception(f"{self.__class__.__name__} must implement output()") def _get_output_charge_detector(self, vreg_voltage): """ Outputs the logical value of the Changepoint Detector configured to detect charge :param vreg_voltage: voltage of the voltage regulator :return: a charge changepoint is reached """ v_buff = self.system_model.energy_buffer.get_voltage() * self.partition #print(f"Charge state: v_buff = {v_buff}V; v_sup = {vreg_voltage}V; {v_buff > vreg_voltage}") return v_buff > vreg_voltage def _get_output_discharge_detector(self, vreg_voltage): """ Outputs the logical value of the Changepoint Detector configured to detect discharge :param vreg_voltage: voltage of the voltage regulator :return: a discharge changepoint is reached """ v_buff = self.system_model.energy_buffer.get_voltage() * self.partition #print(f"Discharge state: v_buff = {v_buff}V; v_sup = {vreg_voltage}V; {v_buff < vreg_voltage}") return v_buff < vreg_voltage class ChangepointChargeDetectorLogic(ChangePointDetectorLogic): """ Model of the FBTC Changepoint Charge Detector. It consists in an OpAmp that outputs if the voltage of the energy buffer is higher than a certain threshold w.r.t. the voltage of the voltage regulator. """ def output(self, vreg_voltage): """ Outputs the logical value of the Changepoint Charge Detector :param vreg_voltage: voltage of the voltage regulator :return: a charge changepoint is reached """ return self._get_output_charge_detector(vreg_voltage) class ChangepointDischargeDetectorLogic(ChangePointDetectorLogic): """ Model of the FBTC Changepoint Discharge Detector. It consists in an OpAmp that outputs if the voltage of the energy buffer is lower than a certain threshold w.r.t. the voltage of the voltage regulator. """ def output(self, vreg_voltage): """ Outputs the logical value of the Changepoint Discharge Detector :param vreg_voltage: voltage of the voltage regulator :return: a discharge changepoint is reached """ return self._get_output_discharge_detector(vreg_voltage)