forked from LoopKit/Loop
-
Notifications
You must be signed in to change notification settings - Fork 1
Loop's Temp Basal Logic
Ryan Packer edited this page Jun 3, 2019
·
2 revisions
The purpose of this page is to describe my understanding of the code used to set a temp basal.
- Loop initiates a request to determine what if any temp basal to set by making a call to the
recommendedTempBasalfunction in Loop/Managers/DoseMath.swift. -
recommendedTempBasalimmediately calls the 'insulinCorrection' function which is used to determine what correction would be required for the given glucose predictions and the other inputs. This function is used to calculate the insulin required for both basal and bolus situations. - The
insulinCorrectionfunction starts by examining each glucose prediction to determine 4 simple things:- each prediction is checked to make sure it's within a valid date range which is effectively the time between when the correction would be made and when the insulin would no longer have effect - the
effectDurationfor the given insulin model. - If any prediction is below the suspend threshold, the function returns a
.suspendInsulinCorrection -
minGlucoseis the prediction with the lowest glucose value. -
eventualGlucoseis the last prediction that's still in the valid date range.
- each prediction is checked to make sure it's within a valid date range which is effectively the time between when the correction would be made and when the insulin would no longer have effect - the
- The function then calculates how much insulin would be required (+/-) to correct each prediction to a specific target. This target is not the correction range, but rather is a line defined by the function
targetGlucoseValue. Effectively this function returns the suspend threshold for predictions that are in the first half of the effectDuration time, and returns points along a line from the suspend threshold to the middle of the correction range at the time of that prediction.-
model.percentEffectRemainingis called to determine what percentage of an insulin correction would still have remaining impact at the time of the prediction.1 - model.percentEffectRemainingtells us what percentage of a correction would impact the prediction. - This
percentEffectedis then multiplied by the ISF to get the effective ISF for that prediction. If ISF tells you how much 1 unit of insulin will impact blood glucose eventually, this effective ISF tells you how much one unit of insulin should effect blood glucose at the time of the given prediction. - This information is given to the
insulinCorrectionUnitsfunction, which is the function that ultimately does the diabetes math -(fromValue - toValue)/(effective ISF) - We keep track of the smallest correction to bring a given prediction to the target line described earlier. Another way to think of this - we're calculating the largest correction we could give without taking any of the predictions below the target line.
- We keep track of both the amount of the minimum correction and which prediction requires the minimum correction.
-
- Having determined this information, Loop can now decide what type of correction to return:
- If the minimum predicted BG is < the bottom of the correction range (at the time of the minimum BG) AND the eventual BG is < the bottom of the correction range (at the time of the eventual BG) ... calculate how much (negative) insulin would be required to bring the minimum BG up to the middle of the correction range at the time of the minimum BG.