Skip to content

Commit bf1e003

Browse files
committed
mitigate irc and momentum stacking
1 parent f04d40f commit bf1e003

File tree

1 file changed

+4
-1
lines changed

1 file changed

+4
-1
lines changed

Loop/Models/RetrospectiveCorrection/IntegralRetrospectiveCorrection.swift

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,10 @@ class IntegralRetrospectiveCorrection: RetrospectiveCorrection {
179179

180180
// Overall glucose effect calculated as a sum of propotional, integral and differential effects
181181
proportionalCorrection = IntegralRetrospectiveCorrection.proportionalGain * currentDiscrepancyValue
182-
differentialCorrection = IntegralRetrospectiveCorrection.differentialGain * differentialDiscrepancy
182+
// Differential effect added only when negative, to avoid upward stacking with momentum, while still mitigating slugeshness of retrospective correction when discrepancies start decreasing
183+
if differentialDiscrepancy < 0 {
184+
differentialCorrection = IntegralRetrospectiveCorrection.differentialGain * differentialDiscrepancy
185+
}
183186
let totalCorrection = proportionalCorrection + integralCorrection + differentialCorrection
184187
totalGlucoseCorrectionEffect = HKQuantity(unit: unit, doubleValue: totalCorrection)
185188
integralCorrectionEffectDuration = TimeInterval(minutes: integralCorrectionEffectMinutes)

0 commit comments

Comments
 (0)