diff --git a/ptd_client_server/lib/server.py b/ptd_client_server/lib/server.py index e5c994ec..e6fca2a1 100644 --- a/ptd_client_server/lib/server.py +++ b/ptd_client_server/lib/server.py @@ -132,11 +132,13 @@ def is_finished(self) -> bool: return self._cur_number >= len(self.words) - 1 -def max_volts_amps( +def max_volts_amps_avg_watts( log_fname: str, mark: str, start_channel: int, amount_of_channels: int -) -> Tuple[str, str]: +) -> Tuple[str, str, str]: maxVolts = Decimal("-1") maxAmps = Decimal("-1") + avgWatts = Decimal("-1") + watts = [] with open(log_fname, "r") as f: for line in f: m = RE_PTD_LOG.match(line.rstrip("\r\n")) @@ -145,7 +147,12 @@ def max_volts_amps( parser.lit("Time") parser.skip() parser.lit("Watts") - parser.skip() + if amount_of_channels == 0: + watts_raw = parser.decimal() + if watts_raw > 0: + watts.append(watts_raw) + else: + parser.skip() parser.lit("Volts") volts = parser.decimal() parser.lit("Amps") @@ -167,7 +174,7 @@ def max_volts_amps( channel_range.pop(0) parser.skip() parser.lit("Watts") - parser.skip() + watts_raw = parser.decimal() parser.lit("Volts") volts = parser.decimal() parser.lit("Amps") @@ -177,15 +184,19 @@ def max_volts_amps( if is_sutable_channel: maxVolts = max(maxVolts, volts) maxAmps = max(maxAmps, amps) + if watts_raw > 0: + watts.append(watts_raw) if len(channel_range) == 0: break if len(channel_range): raise ExtraChannelError( f"There are extra ptd channels in configuration" ) - if maxVolts <= 0 or maxAmps <= 0: - raise MaxVoltsAmpsNegativeValuesError(f"Could not find values for {mark!r}") - return str(maxVolts), str(maxAmps) + if len(watts) >= 1: + avgWatts = Decimal(sum(watts) / len(watts)) + else: + avgWatts = Decimal(-1) + return str(maxVolts), str(maxAmps), str("%.6f" % avgWatts) def read_log(log_fname: str, mark: str) -> str: @@ -769,6 +780,8 @@ def __init__(self, server: Server, label: str) -> None: self._state = SessionState.INITIAL self._maxAmps: Optional[str] = None self._maxVolts: Optional[str] = None + self._avgWatts: Optional[str] = None + self._desirableCurrentRange: Optional[str] = None def start(self, mode: Mode) -> bool: if mode == Mode.RANGING and self._state == SessionState.RANGING: @@ -812,7 +825,7 @@ def start(self, mode: Mode) -> bool: self._server._summary.phase("testing", 0) self._ptd.start() self._ptd.cmd(f"SR,V,{self._maxVolts}") - self._ptd.cmd(f"SR,A,{self._maxAmps}") + self._ptd.cmd(f"SR,A,{self._desirableCurrentRange}") with common.sig: time.sleep(ANALYZER_SLEEP_SECONDS) logging.info("Starting testing mode") @@ -868,13 +881,29 @@ def stop(self, mode: Mode) -> bool: if len(self._server._config.ptd_channel) == 2: channels_amount = self._server._config.ptd_channel[1] - self._maxVolts, self._maxAmps = max_volts_amps( + ( + self._maxVolts, + self._maxAmps, + self._avgWatts, + ) = max_volts_amps_avg_watts( self._server._config.ptd_logfile, self._id + "_ranging", start_channel, channels_amount, ) + # we will query average power consumed and depending on that, we will add fix to crest factor + # default is crest factor 3 (pek current is 3x rms current) + # PSUs under 75W don't have mandatory Power Factor Correction, so they can be arbitrarily dirty + # Tektronix' app note on power supplies claims that power supplies typically exhibit crest factor between 4 and 10 + # https://assets.testequity.com/te1/Documents/pdf/power-measurements_AC-DC-an.pdf + # in order to achieve same peak detection, range should be 3.3 higher than max measured RMS (since crest factor of meter is 3 and 3*3.3 is almost 10 :) ) + + if float(self._avgWatts) < 75: + self._desirableCurrentRange = str(float(self._maxAmps) * 3.3) + else: + self._desirableCurrentRange = str(float(self._maxAmps) * 1.1) + except MaxVoltsAmpsNegativeValuesError as e: if test_duration < 1: raise MeasurementEndedTooFastError( diff --git a/ptd_client_server/tests/unit/test_server.py b/ptd_client_server/tests/unit/test_server.py index d7234294..f624bd10 100644 --- a/ptd_client_server/tests/unit/test_server.py +++ b/ptd_client_server/tests/unit/test_server.py @@ -49,7 +49,7 @@ def test_tcp_port_is_occupied() -> None: assert server.tcp_port_is_occupied(9999) is False -def test_max_volts_amps(tmp_path: Path) -> None: +def test_max_volts_amps_avg_watts(tmp_path: Path) -> None: with open(tmp_path / "logs_tmp", "wb") as f: f.write( b"Time,11-07-2020 17:49:06.145,NOTICE,Analyzer identity response of 32 bytes: YOKOGAWA,WT310,C2PH13047V,F1.03\n" @@ -65,30 +65,47 @@ def test_max_volts_amps(tmp_path: Path) -> None: b"Time,11-13-2020 22:39:00.239,Watts,275.630000,Volts,-1.000000,Amps,-1.000000,PF,-1.000000,Mark,notset1,Ch1,Watts1,91.960000,Volts,120.910000,Amps,0.810300,PF,0.938600,Ch2,Watts,91.870000,Volts,120.830000,Amps,0.810400,PF,0.938500,Ch3,Watts,91.800000,Volts,120.730000,Amps,0.810200,PF,0.938400\n" ) - assert server.max_volts_amps(str(tmp_path / "logs_tmp"), "notset", 3, 1) == ( + assert server.max_volts_amps_avg_watts( + str(tmp_path / "logs_tmp"), "notset", 3, 1 + ) == ( "120.750000", "0.810300", + "91.500000", ) - assert server.max_volts_amps(str(tmp_path / "logs_tmp"), "notset", 2, 2) == ( + assert server.max_volts_amps_avg_watts( + str(tmp_path / "logs_tmp"), "notset", 2, 2 + ) == ( "120.850000", "0.810400", + "91.535000", ) with pytest.raises(server.ExtraChannelError) as excinfo: - server.max_volts_amps(str(tmp_path / "logs_tmp"), "notset", 2, 4) + server.max_volts_amps_avg_watts(str(tmp_path / "logs_tmp"), "notset", 2, 4) assert "There are extra ptd channels in configuration" in str(excinfo.value) - assert server.max_volts_amps(str(tmp_path / "logs_tmp"), "notset", 1, 3) == ( + assert server.max_volts_amps_avg_watts( + str(tmp_path / "logs_tmp"), "notset", 1, 3 + ) == ( "120.950000", "0.832100", + "91.576667", ) - assert server.max_volts_amps( + assert server.max_volts_amps_avg_watts( str(tmp_path / "logs_tmp"), "2021-01-22_15-05-02_loadgen_ranging", 0, 0 - ) == ("227.370000", "0.225410") + ) == ( + "227.370000", + "0.225410", + "24.980000", + ) - assert server.max_volts_amps( + assert server.max_volts_amps_avg_watts( str(tmp_path / "logs_tmp"), "2021-01-22_15-05-02_loadgen_ranging", 1, 0 - ) == ("227.370000", "0.225410") + ) == ( + "227.370000", + "0.225410", + "24.980000", + ) with pytest.raises(server.LitNotFoundError) as excinfo: - server.max_volts_amps(str(tmp_path / "logs_tmp"), "notset1", 1, 3) + server.max_volts_amps_avg_watts(str(tmp_path / "logs_tmp"), "notset1", 1, 3) assert "Expected 'Watts', got 'Watts1'" in str(excinfo.value)