from ScEpTIC.emulator.energy import energy_utils from ScEpTIC.emulator.energy.system_energy_model import SystemEnergyModel from ScEpTIC.exceptions import ConfigurationException class ChangePointDetector: """ 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): """ Outputs the logical value of the Changepoint Detector :return: a changepoint is reached """ raise Exception(f"{self.__class__.__name__} must implement output()") def _get_output_charge_detector(self): """ Outputs the logical value of the Changepoint Detector configured to detect charge :return: a charge changepoint is reached """ v_buff = self.system_model.energy_buffer.get_voltage() * self.partition v_sup = self.system_model.voltage_regulator.get_voltage() #print(f"Charge state: v_buff = {v_buff}V; v_sup = {v_sup}V; {v_buff > v_sup}") return v_buff > v_sup def _get_output_discharge_detector(self): """ Outputs the logical value of the Changepoint Detector configured to detect discharge :return: a discharge changepoint is reached """ v_buff = self.system_model.energy_buffer.get_voltage() * self.partition v_sup = self.system_model.voltage_regulator.get_voltage() #print(f"Discharge state: v_buff = {v_buff}V; v_sup = {v_sup}V; {v_buff < v_sup}") return v_buff < v_sup class ChangepointChargeDetector(ChangePointDetector): """ 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): """ Outputs the logical value of the Changepoint Charge Detector :return: a charge changepoint is reached """ return self._get_output_charge_detector() class ChangepointDischargeDetector(ChangePointDetector): """ 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): """ Outputs the logical value of the Changepoint Discharge Detector :return: a discharge changepoint is reached """ return self._get_output_discharge_detector()