|
| 1 | +package com.evsuite.hardware.telemetry |
| 2 | + |
| 3 | +import kotlin.math.abs |
| 4 | +import kotlin.math.sqrt |
| 5 | + |
| 6 | +/** |
| 7 | + * How much state of charge a kilometre costs. |
| 8 | + * |
| 9 | + * The obvious route to an arrival forecast runs through the energy model: kWh/100 km, times a |
| 10 | + * distance, divided by a pack capacity. On this vehicle it is a dead end — the model trains on |
| 11 | + * energy integrated from battery power, and `EV_INSTANTANEOUS_CHARGE_RATE` publishes nothing. |
| 12 | + * |
| 13 | + * State of charge per kilometre needs no energy unit at all. Both of its inputs read on a car |
| 14 | + * where battery power does not, which is why the forecast is built on it instead. |
| 15 | + */ |
| 16 | +data class SocRate( |
| 17 | + /** Percentage points of charge per kilometre. Always positive for a car that is driving. */ |
| 18 | + val percentPerKm: Double, |
| 19 | + /** Half-width of the band on [percentPerKm], in the same unit. */ |
| 20 | + val uncertaintyPercentPerKm: Double, |
| 21 | + val source: Source, |
| 22 | + /** Trips behind the figure, or 0 when it came from the vehicle's own range. */ |
| 23 | + val sampleCount: Int = 0, |
| 24 | +) { |
| 25 | + enum class Source { |
| 26 | + /** |
| 27 | + * State of charge divided by the vehicle's own remaining range. Available from the |
| 28 | + * first reading, and only as good as the car's own estimate. |
| 29 | + */ |
| 30 | + VEHICLE_RANGE, |
| 31 | + |
| 32 | + /** Measured across recorded trips: what this driver actually spends per kilometre. */ |
| 33 | + TRIP_HISTORY, |
| 34 | + } |
| 35 | +} |
| 36 | + |
| 37 | +/** Builds a [SocRate] from whichever evidence exists, preferring what the driver has driven. */ |
| 38 | +object SocRateEstimator { |
| 39 | + |
| 40 | + /** |
| 41 | + * How wrong the vehicle's own range estimate may be. |
| 42 | + * |
| 43 | + * The car publishes a range and no error bar. It is a model like any other — it reacts to |
| 44 | + * recent driving, temperature and its own assumptions — so a fifth is a stated assumption |
| 45 | + * rather than a measurement, and it is deliberately wide. Trip history replaces it with a |
| 46 | + * band that was actually observed. |
| 47 | + */ |
| 48 | + const val VEHICLE_RANGE_RELATIVE_UNCERTAINTY = 0.20 |
| 49 | + |
| 50 | + /** Below this a trip's own state-of-charge resolution dominates whatever it measured. */ |
| 51 | + const val MIN_TRIP_DISTANCE_KM = 1.0 |
| 52 | + |
| 53 | + /** Fewer than this and the spread across trips is not a band, it is noise. */ |
| 54 | + const val MIN_TRIPS = 3 |
| 55 | + |
| 56 | + /** A floor on the observed spread, so a run of similar trips cannot imply false precision. */ |
| 57 | + const val MIN_TRIP_RELATIVE_UNCERTAINTY = 0.10 |
| 58 | + |
| 59 | + /** |
| 60 | + * The rate implied by the vehicle's own range reading. |
| 61 | + * |
| 62 | + * @param socPercent the vehicle's state of charge, 0..100. |
| 63 | + * @param rangeKm the vehicle's own remaining range. |
| 64 | + */ |
| 65 | + fun fromVehicleRange(socPercent: Double?, rangeKm: Double?): SocRate? { |
| 66 | + if (socPercent == null || rangeKm == null) return null |
| 67 | + if (!socPercent.isFinite() || !rangeKm.isFinite()) return null |
| 68 | + if (socPercent !in 0.0..100.0 || rangeKm <= 0.0) return null |
| 69 | + val rate = socPercent / rangeKm |
| 70 | + if (!rate.isFinite() || rate <= 0.0) return null |
| 71 | + return SocRate( |
| 72 | + percentPerKm = rate, |
| 73 | + uncertaintyPercentPerKm = rate * VEHICLE_RANGE_RELATIVE_UNCERTAINTY, |
| 74 | + source = SocRate.Source.VEHICLE_RANGE, |
| 75 | + ) |
| 76 | + } |
| 77 | + |
| 78 | + /** |
| 79 | + * The rate this driver has actually spent, across trips whose distance can be vouched for. |
| 80 | + * |
| 81 | + * Only trips carrying [EnergyTripSummary.modellableDistanceKm] count: a distance recorded |
| 82 | + * under the old speed conversion is 3.6 times too long, and averaging it in would make the |
| 83 | + * car look three and a half times more efficient than it is. |
| 84 | + * |
| 85 | + * Charging stops are excluded by construction — a trip whose charge went up is not a trip |
| 86 | + * whose consumption means anything. |
| 87 | + */ |
| 88 | + fun fromTrips( |
| 89 | + trips: List<EnergyTripSummary>, |
| 90 | + generation: com.evsuite.hardware.FirmwareInfo.Gen = |
| 91 | + com.evsuite.hardware.FirmwareInfo.getGeneration(), |
| 92 | + ): SocRate? { |
| 93 | + val rates = trips.mapNotNull { trip -> |
| 94 | + val km = trip.modellableDistanceKm(generation)?.takeIf { it >= MIN_TRIP_DISTANCE_KM } |
| 95 | + ?: return@mapNotNull null |
| 96 | + val start = trip.startSocPercent ?: return@mapNotNull null |
| 97 | + val end = trip.endSocPercent ?: return@mapNotNull null |
| 98 | + val spent = (start - end).toDouble() |
| 99 | + if (spent <= 0.0) return@mapNotNull null |
| 100 | + (spent / km).takeIf { it.isFinite() && it > 0.0 } |
| 101 | + } |
| 102 | + if (rates.size < MIN_TRIPS) return null |
| 103 | + val mean = rates.average() |
| 104 | + if (!mean.isFinite() || mean <= 0.0) return null |
| 105 | + val variance = rates.sumOf { (it - mean) * (it - mean) } / (rates.size - 1) |
| 106 | + val observed = sqrt(variance) |
| 107 | + return SocRate( |
| 108 | + percentPerKm = mean, |
| 109 | + uncertaintyPercentPerKm = maxOf(observed, abs(mean) * MIN_TRIP_RELATIVE_UNCERTAINTY), |
| 110 | + source = SocRate.Source.TRIP_HISTORY, |
| 111 | + sampleCount = rates.size, |
| 112 | + ) |
| 113 | + } |
| 114 | + |
| 115 | + /** Trip history when there is enough of it, the vehicle's own range otherwise. */ |
| 116 | + fun best( |
| 117 | + socPercent: Double?, |
| 118 | + rangeKm: Double?, |
| 119 | + trips: List<EnergyTripSummary>, |
| 120 | + generation: com.evsuite.hardware.FirmwareInfo.Gen = |
| 121 | + com.evsuite.hardware.FirmwareInfo.getGeneration(), |
| 122 | + ): SocRate? = fromTrips(trips, generation) ?: fromVehicleRange(socPercent, rangeKm) |
| 123 | +} |
0 commit comments