Skip to content

Commit 42425ad

Browse files
ps2Kdisimone
authored andcommitted
From DM61's work on IRC, fixes a case where RC velocity can have an a… (#778)
* From DM61's work on IRC, fixes a case where RC velocity can have an artificially large magnitude from short glucose change intervals. * Typo
1 parent 1a44645 commit 42425ad

File tree

1 file changed

+4
-1
lines changed

1 file changed

+4
-1
lines changed

Loop/Managers/LoopDataManager.swift

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -841,7 +841,10 @@ extension LoopDataManager {
841841
let velocityUnit = glucoseUnit.unitDivided(by: HKUnit.second())
842842

843843
let discrepancy = change.end.quantity.doubleValue(for: glucoseUnit) - lastGlucose.quantity.doubleValue(for: glucoseUnit) // mg/dL
844-
let velocity = HKQuantity(unit: velocityUnit, doubleValue: discrepancy / change.end.endDate.timeIntervalSince(change.start.endDate))
844+
845+
// Determine the interval of discrepancy, requiring a minimum of the configured interval to avoid magnifying effects from short intervals
846+
let discrepancyTime = max(change.end.endDate.timeIntervalSince(change.start.endDate), settings.retrospectiveCorrectionInterval)
847+
let velocity = HKQuantity(unit: velocityUnit, doubleValue: discrepancy / discrepancyTime)
845848
let type = HKQuantityType.quantityType(forIdentifier: HKQuantityTypeIdentifier.bloodGlucose)!
846849
let glucose = HKQuantitySample(type: type, quantity: change.end.quantity, start: change.end.startDate, end: change.end.endDate)
847850

0 commit comments

Comments
 (0)