Skip to content

Commit 46d64d2

Browse files
authored
Merge pull request #2028 from dm61/irc-updates
mitigate irc and momentum stacking
2 parents f04d40f + 70edb11 commit 46d64d2

File tree

1 file changed

+6
-1
lines changed

1 file changed

+6
-1
lines changed

Loop/Models/RetrospectiveCorrection/IntegralRetrospectiveCorrection.swift

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,12 @@ 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 sluggishness of retrospective correction when discrepancies start decreasing
183+
if differentialDiscrepancy < 0.0 {
184+
differentialCorrection = IntegralRetrospectiveCorrection.differentialGain * differentialDiscrepancy
185+
} else {
186+
differentialCorrection = 0.0
187+
}
183188
let totalCorrection = proportionalCorrection + integralCorrection + differentialCorrection
184189
totalGlucoseCorrectionEffect = HKQuantity(unit: unit, doubleValue: totalCorrection)
185190
integralCorrectionEffectDuration = TimeInterval(minutes: integralCorrectionEffectMinutes)

0 commit comments

Comments
 (0)