Skip to content
Merged
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
37 changes: 27 additions & 10 deletions lib/determine-basal/determine-basal.js
Original file line number Diff line number Diff line change
Expand Up @@ -367,6 +367,9 @@ var determine_basal = function determine_basal(glucose_status, currenttemp, iob_
var minCOBPredBG = 999;
var minUAMPredBG = 999;
var minGuardBG = 999;
var minCOBGuardBG = 999;
var minUAMGuardBG = 999;
var minIOBGuardBG = 999;
var minPredBG;
var avgPredBG;
var IOBpredBG = eventualBG;
Expand Down Expand Up @@ -426,14 +429,10 @@ var determine_basal = function determine_basal(glucose_status, currenttemp, iob_
if ( COBpredBGs.length < 48) { COBpredBGs.push(COBpredBG); }
if ( aCOBpredBGs.length < 48) { aCOBpredBGs.push(aCOBpredBG); }
if ( UAMpredBGs.length < 48) { UAMpredBGs.push(UAMpredBG); }
// calculate minGuardBG without a wait from COB if available, or UAM, or IOB predBGs
if ( (cid || remainingCIpeak > 0) && fractionCOBAbsorbed < 0.75 ) {
if ( COBpredBG < minGuardBG ) { minGuardBG = round(COBpredBG); }
} else if ( enableUAM ) {
if ( UAMpredBG < minGuardBG ) { minGuardBG = round(UAMpredBG); }
} else {
if ( IOBpredBG < minGuardBG ) { minGuardBG = round(IOBpredBG); }
}
// calculate minGuardBGs without a wait from COB, UAM, IOB predBGs
if ( COBpredBG < minCOBGuardBG ) { minCOBGuardBG = round(COBpredBG); }
if ( UAMpredBG < minUAMGuardBG ) { minUAMGuardBG = round(UAMpredBG); }
if ( IOBpredBG < minIOBGuardBG ) { minIOBGuardBG = round(IOBpredBG); }

// set minPredBGs starting when currently-dosed insulin activity will peak
var insulinPeakTime = 75;
Expand Down Expand Up @@ -520,6 +519,7 @@ var determine_basal = function determine_basal(glucose_status, currenttemp, iob_

console.error("UAM Impact:",uci,"mg/dL per 5m; UAM Duration:",UAMduration,"hours");


minIOBPredBG = Math.max(39,minIOBPredBG);
minCOBPredBG = Math.max(39,minCOBPredBG);
minUAMPredBG = Math.max(39,minUAMPredBG);
Expand All @@ -540,27 +540,44 @@ var determine_basal = function determine_basal(glucose_status, currenttemp, iob_
avgPredBG = round( IOBpredBG );
}

// if we have both minCOBGuardBG and minUAMGuardBG, blend according to fractionCarbsLeft
if ( (cid || remainingCIpeak > 0) && enableUAM ) {
if ( enableUAM ) {
minGuardBG = fractionCarbsLeft*minCOBGuardBG + (1-fractionCarbsLeft)*minUAMGuardBG;
} else {
minGuardBG = minCOBGuardBG;
}
} else if ( enableUAM ) {
minGuardBG = minUAMGuardBG;
} else {
minGuardBG = minIOBGuardBG;
}
minGuardBG = round(minGuardBG);
//console.error(minCOBGuardBG, minUAMGuardBG, minIOBGuardBG, minGuardBG);

// if any carbs have been entered recently
if (meal_data.carbs) {
// average the minIOBPredBG and minUAMPredBG if available
/*
if ( minUAMPredBG < 999 ) {
avgMinPredBG = round( (minIOBPredBG+minUAMPredBG)/2 );
} else {
avgMinPredBG = minIOBPredBG;
}
*/

// if UAM is disabled, use max of minIOBPredBG, minCOBPredBG
if ( ! enableUAM && minCOBPredBG < 999 ) {
minPredBG = round(Math.max(minIOBPredBG, minCOBPredBG));
// if we have COB, use minCOBPredBG, or blendedMinPredBG if it's higher
} else if ( minCOBPredBG < 999 ) {
// calculate blendedMinPredBG based on how many carbs remain as COB
blendedMinPredBG = fractionCarbsLeft*minCOBPredBG + (1-fractionCarbsLeft)*avgMinPredBG;
blendedMinPredBG = fractionCarbsLeft*minCOBPredBG + (1-fractionCarbsLeft)*minUAMPredBG;
// if blendedMinPredBG > minCOBPredBG, use that instead
minPredBG = round(Math.max(minIOBPredBG, minCOBPredBG, blendedMinPredBG));
// if carbs have been entered, but have expired, use avg of minIOBPredBG and minUAMPredBG
} else {
minPredBG = avgMinPredBG;
minPredBG = minUAMPredBG;
}
// in pure UAM mode, use the higher of minIOBPredBG,minUAMPredBG
} else if ( enableUAM ) {
Expand Down