Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/whole-amp-phase-current.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"ftw": patch
---

Read the site meter's phase current from its phase power as well. Many P1 meters report phase current in whole amperes, so a phase could carry up to 1 A more than the meter showed, and the car's fuse limit let it stay there. Each phase now counts as at least its power over its measured voltage, when the meter reports both and the phase powers add up to its total. Meters that report no phase voltage read as before.
18 changes: 17 additions & 1 deletion go/internal/control/fuse_heat.go
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,9 @@ func (s *State) stepFuseHeat(meter *telemetry.DerReading, totalW float64) {
// the phases' powers add up to the reading's total. Otherwise its current
// counts as import, the safe side: a phase near 0 W, a reactive phase, a
// meter without phase power, or a driver whose phase power has the wrong
// sign. totalW is the same reading's site total. present is false when the
// sign. A phase's current is at least its power over its measured voltage
// (|P| ≤ V·I): many P1 meters report whole amperes and hide up to 1 A.
// totalW is the same reading's site total. present is false when the
// meter reports no phase currents; valid is false when they cannot be used.
func phaseCurrentsA(meter *telemetry.DerReading, state *State, totalW float64) (amps []float64, present, valid bool) {
// DER data uses snake case; separate fields retain absent-vs-zero.
Expand All @@ -165,6 +167,9 @@ func phaseCurrentsA(meter *telemetry.DerReading, state *State, totalW float64) (
L1W *float64 `json:"l1_w"`
L2W *float64 `json:"l2_w"`
L3W *float64 `json:"l3_w"`
L1V *float64 `json:"l1_v"`
L2V *float64 `json:"l2_v"`
L3V *float64 `json:"l3_v"`
}
if json.Unmarshal(meter.Data, &data) != nil || (data.L1 == nil && data.L2 == nil && data.L3 == nil) {
return nil, false, false
Expand All @@ -174,11 +179,15 @@ func phaseCurrentsA(meter *telemetry.DerReading, state *State, totalW float64) (
}
powers := []*float64{data.L1W, data.L2W, data.L3W}[:state.SiteFusePhases]
signs := phasePowersAddUp(powers, totalW)
volts := []*float64{data.L1V, data.L2V, data.L3V}
for i, a := range []*float64{data.L1, data.L2, data.L3}[:state.SiteFusePhases] {
if a == nil || !finiteFuseValue(*a) {
return nil, true, false
}
signed := math.Abs(*a)
if v := volts[i]; signs && v != nil && plausiblePhaseVoltage(*v, state.SiteFuseVoltage) {
signed = math.Max(signed, math.Abs(*powers[i]) / *v)
}
if w := powers[i]; signs && *w < -math.Max(exportSignMinW, signed*state.SiteFuseVoltage/4) {
signed = -signed
}
Expand All @@ -187,6 +196,13 @@ func phaseCurrentsA(meter *telemetry.DerReading, state *State, totalW float64) (
return amps, true, true
}

// plausiblePhaseVoltage reports whether a measured phase voltage is within
// a fifth of the fuse's nominal voltage. Grids stay within a tenth; a value
// outside the band is a driver or scale fault and bounds nothing.
func plausiblePhaseVoltage(v, nominal float64) bool {
return finiteFuseValue(v) && v >= 0.8*nominal && v <= 1.2*nominal
}

// exportSignMinW is the least export a phase's power must show before its
// current counts as export.
const exportSignMinW = 25.0
Expand Down
70 changes: 70 additions & 0 deletions go/internal/control/fuse_heat_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -635,3 +635,73 @@ func lastingFuseHeat(store *telemetry.Store, state *State) {
// stepFuseHeat0 folds a reading with phase currents but no phase power, so
// its total does not matter.
func (s *State) stepFuseHeat0(meter *telemetry.DerReading) { s.stepFuseHeat(meter, 0) }

// Many P1 meters report phase current in whole amperes and hide up to 1 A.
// The same reading's phase power over its measured voltage bounds the
// current from below (|P| ≤ V·I). The measured voltage, not the nominal,
// keeps a high grid from reading as extra current.
func TestPhaseCurrentsReadWholeAmpMetersFromPhasePower(t *testing.T) {
_, state := fuseHeatState()
for _, tc := range []struct {
name string
data map[string]float64
totalW float64
want []float64
}{
{"truncated 16.6 A", map[string]float64{"l1_a": 16, "l2_a": 1, "l3_a": 1, "l1_w": 3818, "l2_w": 230, "l3_w": 230, "l1_v": 230, "l2_v": 230, "l3_v": 230}, 4278, []float64{16.6, 1, 1}},
{"exporting 16.6 A", map[string]float64{"l1_a": 16, "l2_a": 1, "l3_a": 1, "l1_w": -3818, "l2_w": 230, "l3_w": 230, "l1_v": 230, "l2_v": 230, "l3_v": 230}, -3358, []float64{-16.6, 1, 1}},
{"16 A on a 240 V grid", map[string]float64{"l1_a": 16, "l2_a": 1, "l3_a": 1, "l1_w": 3840, "l2_w": 240, "l3_w": 240, "l1_v": 240, "l2_v": 240, "l3_v": 240}, 4320, []float64{16, 1, 1}},
{"no phase voltage", map[string]float64{"l1_a": 16, "l2_a": 1, "l3_a": 1, "l1_w": 3818, "l2_w": 230, "l3_w": 230}, 4278, []float64{16, 1, 1}},
{"voltage off scale", map[string]float64{"l1_a": 16, "l2_a": 1, "l3_a": 1, "l1_w": 3818, "l2_w": 230, "l3_w": 230, "l1_v": 23, "l2_v": 23, "l3_v": 23}, 4278, []float64{16, 1, 1}},
{"phase power against the total", map[string]float64{"l1_a": 16, "l2_a": 1, "l3_a": 1, "l1_w": 3818, "l2_w": 230, "l3_w": 230, "l1_v": 230, "l2_v": 230, "l3_v": 230}, 1000, []float64{16, 1, 1}},
{"reactive load", map[string]float64{"l1_a": 16.6, "l2_a": 1, "l3_a": 1, "l1_w": 3000, "l2_w": 230, "l3_w": 230, "l1_v": 230, "l2_v": 230, "l3_v": 230}, 3460, []float64{16.6, 1, 1}},
} {
raw, _ := json.Marshal(tc.data)
amps, present, valid := phaseCurrentsA(&telemetry.DerReading{Data: raw}, state, tc.totalW)
ok := present && valid && len(amps) == 3
for i := 0; ok && i < 3; i++ {
ok = math.Abs(amps[i]-tc.want[i]) < 1e-9
}
if !ok {
t.Errorf("%s: %v (present %v, valid %v), want %v", tc.name, amps, present, valid, tc.want)
}
}
}

// A whole-ampere meter reads 26 A on L1 while it carries 26.9 A. The car's
// fuse cut is the one a meter showing 26.9 A gets. On a 240 V grid a phase
// at 26.0 A cuts as 26.0 A, not as its power over 230 V.
func TestEVHardLimitCutsWhatAWholeAmpMeterHides(t *testing.T) {
capFor := func(data map[string]float64, totalW float64) float64 {
t.Helper()
store, state, points, configs := evMeterBudgetFixture(t, 0, nil, 10350, "3p")
raw, _ := json.Marshal(data)
store.Update("meter", telemetry.DerMeter, totalW, nil, raw)
// The hard limit cuts an overage that has lasted, not one reading.
lastingFuseHeat(store, state)
got, known := EVFuseBudget(store, state, points, configs, 17250)
if !known {
t.Fatalf("%v: cap unknown", data)
}
return got
}
for _, tc := range []struct {
name string
volts, trueA float64
}{
{"26.9 A at 230 V", 230, 26.9},
{"26.0 A at 240 V", 240, 26},
} {
totalW := (tc.trueA + 40) * tc.volts
whole := capFor(map[string]float64{"l1_a": math.Trunc(tc.trueA), "l2_a": 20, "l3_a": 20,
"l1_w": tc.trueA * tc.volts, "l2_w": 20 * tc.volts, "l3_w": 20 * tc.volts,
"l1_v": tc.volts, "l2_v": tc.volts, "l3_v": tc.volts}, totalW)
exact := capFor(map[string]float64{"l1_a": tc.trueA, "l2_a": 20, "l3_a": 20}, totalW)
if exact >= 10350 {
t.Fatalf("%s: setup: %.1f A on L1 does not cut (cap %.0f W)", tc.name, tc.trueA, exact)
}
if math.Abs(whole-exact) > .01 {
t.Errorf("%s: whole-ampere meter cap %.1f W, exact meter %.1f W", tc.name, whole, exact)
}
}
}
Loading