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
7 changes: 5 additions & 2 deletions Loop/Managers/DiagnosticLogger+LoopKit.swift
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ extension DiagnosticLogger {
addError(String(describing: message), fromSource: source)
}

func addLoopStatus(startDate: Date, endDate: Date, glucose: GlucoseValue, effects: [String: [GlucoseEffect]], error: Error?, prediction: [GlucoseValue], predictionWithRetrospectiveEffect: Double, recommendedTempBasal: LoopDataManager.TempBasalRecommendation?) {
func addLoopStatus(startDate: Date, endDate: Date, glucose: GlucoseValue, effects: [String: [GlucoseEffect]], error: Error?, prediction: [GlucoseValue], predictionWithRetrospectiveEffect: Double, eventualBG: Double, eventualBGWithRetrospectiveEffect: Double, eventualBGWithoutMomentum: Double, recommendedTempBasal: LoopDataManager.TempBasalRecommendation?) {

let dateFormatter = DateFormatter.ISO8601StrictDateFormatter()
let unit = HKUnit.milligramsPerDeciliterUnit()
Expand Down Expand Up @@ -57,7 +57,10 @@ extension DiagnosticLogger {
"unit": unit.unitString
]
},
"prediction_retrospect_delta": predictionWithRetrospectiveEffect
"prediction_retrospect_delta": predictionWithRetrospectiveEffect,
"eventualBG": eventualBG,
"eventualBGWithRetrospectiveEffect": eventualBGWithRetrospectiveEffect,
"eventualBGWithoutMomentum": eventualBGWithoutMomentum
]

if let error = error {
Expand Down
27 changes: 25 additions & 2 deletions Loop/Managers/LoopDataManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,7 @@ final class LoopDataManager {
}
}

if self.predictedGlucose == nil {
if (self.predictedGlucose == nil || self.predictedGlucoseWithoutMomentum == nil) {
do {
try self.updatePredictedGlucoseAndRecommendedBasal()
} catch let error {
Expand Down Expand Up @@ -305,6 +305,7 @@ final class LoopDataManager {
private var carbEffect: [GlucoseEffect]? {
didSet {
predictedGlucose = nil
predictedGlucoseWithoutMomentum = nil

// Carb data may be back-dated, so re-calculate the retrospective glucose.
retrospectivePredictedGlucose = nil
Expand All @@ -318,6 +319,7 @@ final class LoopDataManager {
}

predictedGlucose = nil
predictedGlucoseWithoutMomentum = nil
}
}
private var insulinOnBoard: InsulinValue?
Expand All @@ -336,6 +338,7 @@ final class LoopDataManager {
recommendedTempBasal = nil
}
}
private var predictedGlucoseWithoutMomentum: [GlucoseValue]?
private var retrospectivePredictedGlucose: [GlucoseValue]? {
didSet {
retrospectiveGlucoseEffect = []
Expand Down Expand Up @@ -509,6 +512,7 @@ final class LoopDataManager {

let prediction = LoopMath.predictGlucose(glucose, momentum: momentum, effects: carbEffect, insulinEffect)
let predictionWithRetrospectiveEffect = LoopMath.predictGlucose(glucose, momentum: momentum, effects: carbEffect, insulinEffect, retrospectiveGlucoseEffect)
let predictionWithoutMomentum = LoopMath.predictGlucose(glucose, effects: carbEffect, insulinEffect)

let predictDiff: Double

Expand All @@ -521,6 +525,10 @@ final class LoopDataManager {
predictDiff = 0
}

let eventualBG: Double = prediction.last?.quantity.doubleValue(for: unit) ?? 0
let eventualBGWithRetrospectiveEffect: Double = predictionWithRetrospectiveEffect.last?.quantity.doubleValue(for: unit) ?? 0
let eventualBGWithoutMomentum: Double = predictionWithoutMomentum.last?.quantity.doubleValue(for: unit) ?? 0

defer {
deviceDataManager.logger.addLoopStatus(
startDate: startDate,
Expand All @@ -535,11 +543,15 @@ final class LoopDataManager {
error: error,
prediction: prediction,
predictionWithRetrospectiveEffect: predictDiff,
eventualBG: eventualBG,
eventualBGWithRetrospectiveEffect: eventualBGWithRetrospectiveEffect,
eventualBGWithoutMomentum: eventualBGWithoutMomentum,
recommendedTempBasal: recommendedTempBasal
)
}

self.predictedGlucose = retrospectiveCorrectionEnabled ? predictionWithRetrospectiveEffect : prediction
self.predictedGlucoseWithoutMomentum = predictionWithoutMomentum

guard let
maxBasal = deviceDataManager.maximumBasalRatePerHour,
Expand Down Expand Up @@ -597,6 +609,7 @@ final class LoopDataManager {
private func recommendBolus() throws -> Double {
guard let
glucose = self.predictedGlucose,
let glucoseWithoutMomentum = self.predictedGlucoseWithoutMomentum,
let maxBolus = self.deviceDataManager.maximumBolus,
let glucoseTargetRange = self.deviceDataManager.glucoseTargetRangeSchedule,
let insulinSensitivity = self.deviceDataManager.insulinSensitivitySchedule,
Expand All @@ -617,13 +630,23 @@ final class LoopDataManager {

let pendingBolusAmount: Double = lastBolus?.units ?? 0

return max(0, DoseMath.recommendBolusFromPredictedGlucose(glucose,
let recommendedBolusWithMomentum = max(0, DoseMath.recommendBolusFromPredictedGlucose(glucose,
lastTempBasal: self.lastTempBasal,
maxBolus: maxBolus,
glucoseTargetRange: glucoseTargetRange,
insulinSensitivity: insulinSensitivity,
basalRateSchedule: basalRates
) - pendingBolusAmount)

let recommendedBolusWithoutMomentum = max(0, DoseMath.recommendBolusFromPredictedGlucose(glucoseWithoutMomentum,
lastTempBasal: self.lastTempBasal,
maxBolus: maxBolus,
glucoseTargetRange: glucoseTargetRange,
insulinSensitivity: insulinSensitivity,
basalRateSchedule: basalRates
) - pendingBolusAmount)

return min(recommendedBolusWithMomentum, recommendedBolusWithoutMomentum)
}

func getRecommendedBolus(_ resultsHandler: @escaping (_ units: Double?, _ error: Error?) -> Void) {
Expand Down