Skip to content

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 recommendedTempBasal function in Loop/Managers/DoseMath.swift.
  • recommendedTempBasal immediately 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 insulinCorrection function 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 effectDuration for the given insulin model.
    • If any prediction is below the suspend threshold, the function returns a .suspend InsulinCorrection
    • minGlucose is the prediction with the lowest glucose value.
    • eventualGlucose is the last prediction that's still in the valid date range.
  • 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.percentEffectRemaining is called to determine what percentage of an insulin correction would still have remaining impact at the time of the prediction. 1 - model.percentEffectRemaining tells us what percentage of a correction would impact the prediction.
    • This percentEffected is 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 insulinCorrectionUnits function, 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.

Clone this wiki locally