Repository navigation
fix(control): slew no longer reopens discharge on a blocked battery #1876
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| --- | ||
| "ftw": patch | ||
| --- | ||
|
|
||
| Stop asking a home battery to discharge when its charge is below 5 % or it reports that it cannot discharge. The slew limit used to ramp such a battery back toward the discharge it was already doing. If stopping it at once would put the site or one phase over the fuse or import limit, other batteries take that load first, and the blocked battery keeps only the discharge still needed. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,189 @@ | ||
| package control | ||
|
|
||
| import ( | ||
| "encoding/json" | ||
| "math" | ||
| "testing" | ||
| "time" | ||
|
|
||
| "github.com/srcfl/ftw/go/internal/telemetry" | ||
| ) | ||
|
|
||
| // Slew anchors on measured SmoothedW. It must not walk a pack back into the | ||
| // discharge the distributor closed (SoC below the 5 % floor, or | ||
| // discharge_capable=false). | ||
| func TestSlewDoesNotReopenBlockedDischarge(t *testing.T) { | ||
| cases := []struct { | ||
| name string | ||
| soc float64 | ||
| capability string | ||
| }{ | ||
| {"soc_below_floor", 0.04, ""}, | ||
| {"discharge_capable_false", 0.50, `{"discharge_capable":false}`}, | ||
| } | ||
| for _, tc := range cases { | ||
| t.Run(tc.name, func(t *testing.T) { | ||
| st := NewState(0, 42, "meter") | ||
| st.MinDispatchIntervalS = 0 | ||
| st.SlewRateW = 500 | ||
| store := seedDeadbandSite(2000, []deadbandBattery{{"bat", -3000, tc.soc, tc.capability}}) | ||
| out := ComputeDispatch(store, st, caps(map[string]float64{"bat": 10000}), 0) | ||
| if got := targetsByDriver(out)["bat"].TargetW; got < -1 { | ||
| t.Errorf("commanded %.0f W discharge to a pack that must not discharge", got) | ||
| } | ||
| }) | ||
| } | ||
| } | ||
|
|
||
| // When stopping the blocked pack at once would put the site over its fuse, | ||
| // the pack keeps only the discharge the fuse needs: 10000 + 3000 - 11040 W. | ||
| // That is less than the slewed ramp (-2500 W) and never more discharge. | ||
| func TestBlockedDischargeKeepsFuseOverage(t *testing.T) { | ||
| st := NewState(0, 42, "meter") | ||
| st.MinDispatchIntervalS = 0 | ||
| st.SlewRateW = 500 | ||
| store := seedDeadbandSite(10000, []deadbandBattery{{"bat", -3000, 0.04, ""}}) | ||
| out := ComputeDispatch(store, st, caps(map[string]float64{"bat": 10000}), 11040) | ||
| if got := targetsByDriver(out)["bat"].TargetW; math.Abs(got+1960) > 1 { | ||
| t.Errorf("target %.0f W, want -1960 W", got) | ||
| } | ||
| } | ||
|
|
||
| // A stop 60 W over the fuse keeps 60 W of discharge, not the whole ramp. | ||
| func TestBlockedDischargeKeepsOnlyTheOverage(t *testing.T) { | ||
| st := NewState(0, 42, "meter") | ||
| st.MinDispatchIntervalS = 0 | ||
| st.SlewRateW = 500 | ||
| store := seedDeadbandSite(8100, []deadbandBattery{{"blk", -3000, 0.04, ""}}) | ||
| out := ComputeDispatch(store, st, caps(map[string]float64{"blk": 10000}), 11040) | ||
| if got := targetsByDriver(out)["blk"].TargetW; math.Abs(got+60) > 1 { | ||
| t.Errorf("target %.0f W, want -60 W", got) | ||
| } | ||
| } | ||
|
|
||
| // A capable sibling takes the fuse relief, so the blocked pack stops. | ||
| func TestBlockedDischargeLeavesReliefToSibling(t *testing.T) { | ||
| st := NewState(0, 42, "meter") | ||
| st.MinDispatchIntervalS = 0 | ||
| st.SlewRateW = 500 | ||
| store := seedDeadbandSite(9000, []deadbandBattery{ | ||
| {"blk", -3000, 0.04, ""}, | ||
| {"ok", 0, 0.60, ""}, | ||
| }) | ||
| out := ComputeDispatch(store, st, caps(map[string]float64{"blk": 10000, "ok": 10000}), 11040) | ||
| m := targetsByDriver(out) | ||
| if m["blk"].TargetW < -1 { | ||
| t.Errorf("blocked pack at %.0f W while the sibling had room", m["blk"].TargetW) | ||
| } | ||
| // 9000 W meter + 3000 W stop + the sibling's target must fit 11040 W. | ||
| if grid := 12000 + m["ok"].TargetW; grid > 11041 { | ||
| t.Errorf("projected grid %.0f W over the 11040 W fuse (sibling %.0f W)", grid, m["ok"].TargetW) | ||
| } | ||
| } | ||
|
|
||
| // Two blocked packs share the discharge the fuse needs. Together they keep | ||
| // 6000 + 6000 - 11040 = 960 W, not two ramps of 2500 W. | ||
| func TestBlockedDischargeSharesOverage(t *testing.T) { | ||
| st := NewState(0, 42, "meter") | ||
| st.MinDispatchIntervalS = 0 | ||
| st.SlewRateW = 500 | ||
| store := seedDeadbandSite(6000, []deadbandBattery{ | ||
| {"a", -3000, 0.04, ""}, | ||
| {"b", -3000, 0.50, `{"discharge_capable":false}`}, | ||
| }) | ||
| out := ComputeDispatch(store, st, caps(map[string]float64{"a": 10000, "b": 10000}), 11040) | ||
| m := targetsByDriver(out) | ||
| if sum := m["a"].TargetW + m["b"].TargetW; math.Abs(sum+960) > 1 { | ||
| t.Errorf("blocked packs keep %.0f W + %.0f W, want -960 W in total", m["a"].TargetW, m["b"].TargetW) | ||
| } | ||
| } | ||
|
|
||
| // An unbalanced site: the total stays far under the fuse, but stopping a | ||
| // 3 kW discharge adds 4.35 A to each phase and L1 is at 15 A of 16 A. The | ||
| // pack keeps the discharge that holds L1 at 16 A. | ||
| func TestBlockedDischargeStopRespectsPhaseFuse(t *testing.T) { | ||
| st := NewState(0, 42, "meter") | ||
| st.MinDispatchIntervalS = 0 | ||
| st.SlewRateW = 500 | ||
| st.SiteFuseAmps = 16 | ||
| st.SiteFuseVoltage = 230 | ||
| st.SiteFusePhases = 3 | ||
| store := seedDeadbandSite(6000, []deadbandBattery{{"blk", -3000, 0.04, ""}}) | ||
| data, _ := json.Marshal(map[string]any{"l1_a": 15.0, "l2_a": 6.0, "l3_a": 5.1}) | ||
| store.Update("meter", telemetry.DerMeter, 6000, nil, data) | ||
| out := ComputeDispatch(store, st, caps(map[string]float64{"blk": 10000}), 11040) | ||
| got := targetsByDriver(out)["blk"].TargetW | ||
| if l1 := 15 + (got+3000)/(230*3); l1 > 16.01 { | ||
| t.Errorf("target %.0f W puts L1 at %.2f A on a 16 A fuse", got, l1) | ||
| } | ||
| if got < -2311 { | ||
| t.Errorf("target %.0f W keeps more discharge than L1 needs (-2310 W)", got) | ||
| } | ||
| } | ||
|
|
||
| // The stop holds while the pack's measured discharge decays. | ||
| func TestBlockedDischargeStaysStopped(t *testing.T) { | ||
| st := NewState(0, 42, "meter") | ||
| st.MinDispatchIntervalS = 0 | ||
| st.SlewRateW = 500 | ||
| live := -3000.0 | ||
| for tick := 0; tick < 6; tick++ { | ||
| store := seedDeadbandSite(-1000-live, []deadbandBattery{{"bat", live, 0.04, ""}}) | ||
| out := ComputeDispatch(store, st, caps(map[string]float64{"bat": 10000}), 0) | ||
| if got := targetsByDriver(out)["bat"].TargetW; got < -1 { | ||
| t.Fatalf("tick %d: %.0f W", tick, got) | ||
| } | ||
| live /= 2 | ||
| } | ||
| } | ||
|
|
||
| // A manual self-tune request goes through the same floor. | ||
| func TestSelfTuneDoesNotDischargeBlockedPack(t *testing.T) { | ||
| st := NewState(0, 42, "meter") | ||
| store := seedDeadbandSite(2000, []deadbandBattery{{"blk", -3000, 0.04, ""}}) | ||
| out := SelfTuneDispatch(store, st, caps(map[string]float64{"blk": 10000}), 11040, "blk", -2000) | ||
| if got := targetsByDriver(out)["blk"].TargetW; got < -1 { | ||
| t.Errorf("self-tune sent %.0f W", got) | ||
| } | ||
| } | ||
|
|
||
| // The phase relief uses only live readings from the same sample as the rest | ||
| // of the decision. A battery or meter that went offline, or a meter that | ||
| // moved on to a newer reading, gives no phase relief. | ||
| func TestStopPhaseReliefNeedsLiveReadings(t *testing.T) { | ||
| st := NewState(0, 42, "meter") | ||
| st.SiteFuseAmps = 16 | ||
| st.SiteFuseVoltage = 230 | ||
| st.SiteFusePhases = 3 | ||
| data, _ := json.Marshal(map[string]any{"l1_a": 15.0, "l2_a": 6.0, "l3_a": 5.1}) | ||
| seed := func() (*telemetry.Store, map[string]*telemetry.DerReading, *telemetry.DerReading) { | ||
| store := seedDeadbandSite(6000, []deadbandBattery{{"blk", -3000, 0.04, ""}}) | ||
| store.Update("meter", telemetry.DerMeter, 6000, nil, data) | ||
| readings := map[string]*telemetry.DerReading{"blk": store.Get("blk", telemetry.DerBattery)} | ||
| return store, readings, store.Get("meter", telemetry.DerMeter) | ||
| } | ||
| stop := []DispatchTarget{{Driver: "blk", TargetW: 0}} | ||
|
|
||
| store, readings, meter := seed() | ||
| // Stopping 3000 W puts L1 at 15 + 4.35 A; 3.35 A over 16 A is 2310 W. | ||
| if got := stopPhaseReliefW(store, stop, readings, meter, st); math.Abs(got-2310) > 1 { | ||
| t.Fatalf("live readings: relief %.0f W, want 2310 W", got) | ||
| } | ||
| store.DriverHealthMut("blk").SetOffline() | ||
| if got := stopPhaseReliefW(store, stop, readings, meter, st); got != 0 { | ||
| t.Errorf("offline battery: relief %.0f W, want 0", got) | ||
| } | ||
|
|
||
| store, readings, meter = seed() | ||
| store.DriverHealthMut("meter").SetOffline() | ||
| if got := stopPhaseReliefW(store, stop, readings, meter, st); got != 0 { | ||
| t.Errorf("offline meter: relief %.0f W, want 0", got) | ||
| } | ||
|
|
||
| store, readings, meter = seed() | ||
| time.Sleep(time.Millisecond) | ||
| store.Update("meter", telemetry.DerMeter, 6000, nil, data) | ||
| if got := stopPhaseReliefW(store, stop, readings, meter, st); got != 0 { | ||
| t.Errorf("newer meter reading: relief %.0f W, want 0", got) | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
On an unbalanced three-phase site, this check can approve an immediate stop that overloads one phase.
fuseReliefFromSnapshotchecks projected aggregate import, but its phase check only detects an overage already present in the meter sample; it does not predict the phase-current increase from stopping discharge. For example, with a 16 A/230 V three-phase fuse, 6 kW aggregate import, L1 at 15 A, and a blocked battery discharging at 3 kW, stopping the battery leaves aggregate import at a seemingly safe 9 kW but raises L1 to approximately 19.35 A. The previous 500 W slew step would leave L1 at approximately 15.72 A. The subsequentforceFuseDischargeuses the same check and cannot correct this. Compare the floored targets against the phase-aware import ceiling, or explicitly project phase currents, before bypassing the ramp.Useful? React with 👍 / 👎.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed in 97fc2b7.
stopPhaseReliefWprojects the stop onto each phase, and the blocked pack keeps the discharge that holds the worst phase at its limit. Your example (16 A, L1 at 15 A, 6 kW import, 3 kW blocked discharge) now gives -2310 W, so L1 lands at 16 A. Test:TestBlockedDischargeStopRespectsPhaseFuse.