|
| 1 | +package com.evsuite.abrp; |
| 2 | + |
| 3 | +import static org.junit.Assert.assertEquals; |
| 4 | +import static org.junit.Assert.assertFalse; |
| 5 | +import static org.junit.Assert.assertNull; |
| 6 | +import static org.junit.Assert.assertTrue; |
| 7 | + |
| 8 | +import org.junit.Test; |
| 9 | + |
| 10 | +/** |
| 11 | + * Charge detection. The reported regression was a charging car being read as |
| 12 | + * "parked and not charging", which hands it to {@link UploadCadence#PARKED_INTERVAL_MS} |
| 13 | + * and costs ABRP the SOC curve it needs to size a charging stop. |
| 14 | + */ |
| 15 | +public class ChargingSignalTest { |
| 16 | + |
| 17 | + private static final long T0 = 1_700_000_000_000L; |
| 18 | + |
| 19 | + private final ChargingSignal signal = new ChargingSignal(); |
| 20 | + |
| 21 | + // ---- Charge port property, when the VHAL answers it ---- |
| 22 | + |
| 23 | + @Test |
| 24 | + public void aConnectedPortIsCharging() { |
| 25 | + assertTrue(signal.evaluate(T0, true, null, true, 50)); |
| 26 | + } |
| 27 | + |
| 28 | + @Test |
| 29 | + public void anUnpluggedParkedCarIsNotCharging() { |
| 30 | + assertFalse(signal.evaluate(T0, false, null, true, 50)); |
| 31 | + } |
| 32 | + |
| 33 | + @Test |
| 34 | + public void anUnreadablePortStaysUnknown() { |
| 35 | + // Unknown must not collapse to "not charging": UploadCadence treats null as active. |
| 36 | + assertNull(signal.evaluate(T0, null, null, true, 50)); |
| 37 | + } |
| 38 | + |
| 39 | + // ---- Fallback 1: charge rate ---- |
| 40 | + |
| 41 | + @Test |
| 42 | + public void aPositiveChargeRateOverridesAPortThatReadsUnplugged() { |
| 43 | + assertTrue(signal.evaluate(T0, false, 7.4f, true, 50)); |
| 44 | + } |
| 45 | + |
| 46 | + @Test |
| 47 | + public void aNegativeChargeRateIsDriving() { |
| 48 | + assertFalse(signal.evaluate(T0, false, -14.5f, false, 50)); |
| 49 | + } |
| 50 | + |
| 51 | + @Test |
| 52 | + public void noiseAroundZeroIsNotCharging() { |
| 53 | + assertFalse(signal.evaluate(T0, false, 0.1f, true, 50)); |
| 54 | + } |
| 55 | + |
| 56 | + // ---- Fallback 2: rising SOC on a parked car ---- |
| 57 | + |
| 58 | + @Test |
| 59 | + public void aRisingSocOnAParkedCarIsCharging() { |
| 60 | + // Neither charge property readable — the only evidence left is the battery filling. |
| 61 | + assertNull(signal.evaluate(T0, null, null, true, 50)); |
| 62 | + assertTrue(signal.evaluate(T0 + 300_000, null, null, true, 51)); |
| 63 | + } |
| 64 | + |
| 65 | + @Test |
| 66 | + public void theSocRiseIsLatchedAcrossFlatTicks() { |
| 67 | + // SOC moves a whole percent every few minutes; without the latch is_charging |
| 68 | + // would flap between every pair of ticks. |
| 69 | + signal.evaluate(T0, null, null, true, 50); |
| 70 | + signal.evaluate(T0 + 300_000, null, null, true, 51); |
| 71 | + assertTrue(signal.evaluate(T0 + 315_000, null, null, true, 51)); |
| 72 | + assertTrue(signal.evaluate(T0 + 1_500_000, null, null, true, 51)); |
| 73 | + } |
| 74 | + |
| 75 | + @Test |
| 76 | + public void theLatchExpiresSoAParkedCarGoesBackToIdle() { |
| 77 | + signal.evaluate(T0, null, null, true, 50); |
| 78 | + signal.evaluate(T0 + 300_000, null, null, true, 51); |
| 79 | + assertNull(signal.evaluate(T0 + 300_000 + ChargingSignal.SOC_RISE_LATCH_MS, |
| 80 | + null, null, true, 51)); |
| 81 | + } |
| 82 | + |
| 83 | + @Test |
| 84 | + public void aFallingSocDropsTheLatchImmediately() { |
| 85 | + signal.evaluate(T0, null, null, true, 50); |
| 86 | + assertTrue(signal.evaluate(T0 + 300_000, null, null, true, 51)); |
| 87 | + assertNull(signal.evaluate(T0 + 600_000, null, null, true, 50)); |
| 88 | + } |
| 89 | + |
| 90 | + @Test |
| 91 | + public void regenWhileDrivingDoesNotLatch() { |
| 92 | + // SOC climbing on a downhill run, then the car parks: not a charging session. |
| 93 | + signal.evaluate(T0, false, null, false, 50); |
| 94 | + signal.evaluate(T0 + 60_000, false, null, false, 52); |
| 95 | + assertFalse(signal.evaluate(T0 + 120_000, false, null, true, 52)); |
| 96 | + } |
| 97 | + |
| 98 | + @Test |
| 99 | + public void anUnreadableSocKeepsThePreviousReadingToCompareAgainst() { |
| 100 | + signal.evaluate(T0, null, null, true, 50); |
| 101 | + signal.evaluate(T0 + 15_000, null, null, true, null); // one failed read |
| 102 | + assertTrue(signal.evaluate(T0 + 30_000, null, null, true, 51)); |
| 103 | + } |
| 104 | + |
| 105 | + // ---- The regression itself ---- |
| 106 | + |
| 107 | + @Test |
| 108 | + public void aChargingCarIsNeverHandedToTheParkedCadence() { |
| 109 | + // Charging at home with a VHAL that does not implement the charge port property: |
| 110 | + // before the fix this was parked + FALSE, i.e. one upload every 15 minutes. |
| 111 | + ChargingSignal s = new ChargingSignal(); |
| 112 | + UploadSettings settings = UploadSettings.defaults(); |
| 113 | + |
| 114 | + Boolean charging = s.evaluate(T0, false, null, true, 50); |
| 115 | + assertEquals(UploadCadence.PARKED_INTERVAL_MS, |
| 116 | + UploadCadence.intervalFor(true, charging, 50, settings)); |
| 117 | + |
| 118 | + charging = s.evaluate(T0 + 300_000, false, null, true, 51); |
| 119 | + assertEquals(settings.intervalMs(), |
| 120 | + UploadCadence.intervalFor(true, charging, 51, settings)); |
| 121 | + } |
| 122 | +} |
0 commit comments