determine_basal.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. function middleware(iob, currenttemp, glucose, profile, autosens, meal, reservoir, clock, pumphistory) {
  2. // This middleware only works if you have added pumphistory to middleware in FreeAPS X code (my pumphistory branch).
  3. const BG = glucose[0].glucose;
  4. // Change to false to turn off Chris Wilson's formula
  5. var chrisFormula = true;
  6. const minLimitChris = profile.autosens_min;
  7. const maxLimitChris = profile.autosens_max;
  8. const adjustmentFactor = 1;
  9. const currentMinTarget = profile.min_bg;
  10. var exerciseSetting = false;
  11. var log = "";
  12. var logTDD = "";
  13. var logBasal = "";
  14. var logBolus = "";
  15. var logTempBasal = "";
  16. var current = 0;
  17. // If you have not set this to 0.05 in FAX settings (Omnipod), this will be set to 0.1 in code.
  18. var minimalDose = profile.bolus_increment;
  19. var TDD = 0;
  20. var insulin = 0;
  21. var tempInsulin = 0;
  22. var bolusInsulin = 0;
  23. var scheduledBasalInsulin = 0;
  24. var incrementsRaw = 0;
  25. var incrementsRounded = 0;
  26. var quota = 0;
  27. if (profile.high_temptarget_raises_sensitivity == true || profile.exercise_mode == true) {
  28. exerciseSetting = true;
  29. }
  30. // Turn off Chris' formula (and AutoISF) when using a temp target >= 118 (6.5 mol/l) and if an exercise setting is enabled.
  31. // If using AutoISF uncomment the profile.use_autoisf = false
  32. if (currentMinTarget >= 118 && exerciseSetting == true) {
  33. // profile.use_autoisf = false;
  34. chrisFormula = false;
  35. log = "Chris' formula is off due to a high temp target/exercising. Current min target: " + currentMinTarget;
  36. }
  37. // Calculate TDD --------------------------------------
  38. //Bolus:
  39. for (let i = 0; i < pumphistory.length; i++) {
  40. if (pumphistory[i]._type == "Bolus") {
  41. bolusInsulin += pumphistory[i].amount;
  42. }
  43. }
  44. // Temp basals:
  45. if (minimalDose != 0.05) {
  46. minimalDose = 0.1;
  47. }
  48. for (let j = 1; j < pumphistory.length; j++) {
  49. if (pumphistory[j]._type == "TempBasal" && pumphistory[j].rate > 0) {
  50. current = j;
  51. quota = pumphistory[j].rate;
  52. var duration = pumphistory[j-1]['duration (min)'] / 60;
  53. var origDur = duration;
  54. var pastTime = new Date(pumphistory[j-1].timestamp);
  55. // If temp basal hasn't yet ended, use now as end date for calculation
  56. do {
  57. j--;
  58. if (j <= 0) {
  59. morePresentTime = new Date();
  60. break;
  61. } else if (pumphistory[j]._type == "TempBasal" || pumphistory[j]._type == "PumpSuspend") {
  62. morePresentTime = new Date(pumphistory[j].timestamp);
  63. break;
  64. }
  65. }
  66. while (j >= 0);
  67. var diff = (morePresentTime - pastTime) / 36e5;
  68. if (diff < origDur) {
  69. duration = diff;
  70. }
  71. insulin = quota * duration;
  72. // Account for smallest possible pump dosage
  73. incrementsRaw = insulin / minimalDose;
  74. if (incrementsRaw >= 1) {
  75. incrementsRounded = Math.floor(incrementsRaw);
  76. insulin = incrementsRounded * minimalDose;
  77. tempInsulin += insulin;
  78. } else { insulin = 0}
  79. j = current;
  80. }
  81. }
  82. // Check and count for when basals are delivered with a scheduled basal rate or an Autotuned basal rate.
  83. // 1. Check for 0 temp basals with 0 min duration. This is for when ending a manual temp basal and (perhaps) continuing in open loop for a while.
  84. // 2. Check for temp basals that completes. This is for when disconected from link/iphone, or when in open loop.
  85. // To do: need to check for more circumstances when scheduled basal rates are used.
  86. //
  87. for (let i = 0; i < pumphistory.length; i++) {
  88. // Check for 0 temp basals with 0 min duration.
  89. insulin = 0;
  90. if (pumphistory[i]['duration (min)'] == 0) {
  91. let time1 = new Date(pumphistory[i].timestamp);
  92. let time2 = time1;
  93. let j = i;
  94. do {
  95. --j;
  96. if (pumphistory[j]._type == "TempBasal" && j >= 0) {
  97. time2 = new Date(pumphistory[j].timestamp);
  98. break;
  99. }
  100. } while (j >= 0);
  101. // duration of current scheduled basal in h
  102. let basDuration = (time2 - time1) / 36e5;
  103. if (basDuration > 0) {
  104. let hour = time1.getHours();
  105. let minutes = time1.getMinutes();
  106. let seconds = "00";
  107. let string = "" + hour + ":" + minutes + ":" + seconds;
  108. let baseTime = new Date(string);
  109. let basalScheduledRate = 0;
  110. for (let k = 0; k < profile.basalprofile.length; k++) {
  111. if (profile.basalprofile[k].start == baseTime) {
  112. basalScheduledRate = profile.basalprofile[k].rate;
  113. insulin = basalScheduledRate * basDuration;
  114. break;
  115. }
  116. else if (k + 1 < profile.basalprofile.length) {
  117. if (profile.basalprofile[k].start < baseTime && profile.basalprofile[k+1].start > baseTime){
  118. basalScheduledRate = profile.basalprofile[k].rate;
  119. insulin = basalScheduledRate * basDuration;
  120. break;
  121. }
  122. }
  123. }
  124. // Account for smallest possible pump dosage
  125. incrementsRaw = insulin / minimalDose;
  126. if (incrementsRaw >= 1) {
  127. incrementsRounded = Math.floor(incrementsRaw);
  128. insulin = incrementsRounded * minimalDose;
  129. scheduledBasalInsulin += insulin;
  130. } else { insulin = 0}
  131. }
  132. }
  133. }
  134. // Check for temp basals that completes
  135. for (let i = 2; i < pumphistory.length; i++) {
  136. if (pumphistory[i]._type == "TempBasalDuration" && pumphistory[i]['duration (min)'] > 0) {
  137. let time2Duration = pumphistory[i]['duration (min)'] / 60;
  138. let time2 = new Date(pumphistory[i].timestamp);
  139. let time1 = time2;
  140. let m = i;
  141. do {
  142. --m;
  143. if (pumphistory[m]._type == "TempBasal" && m >= 0) {
  144. // next (newer) temp basal
  145. let time1 = new Date(pumphistory[m].timestamp);
  146. break;
  147. }
  148. } while (m >= 0);
  149. let tempBasalTimeDifference = (time2 - time1) / 36e5;
  150. if (time2Duration < tempBasalTimeDifference) {
  151. let timeOfbasal = tempBasalTimeDifference - time2Duration; //
  152. let hour = time2.getHours();
  153. let minutes = time2.getMinutes();
  154. let seconds = "00";
  155. let string = "" + hour + ":" + minutes + ":" + seconds;
  156. let baseTime = new Date(string);
  157. // Default if correct basal schedule rate not found
  158. let basalScheduledRate = profile.basalprofile[0].rate;
  159. for (let k = 0; k < profile.basalprofile.length; ++k) {
  160. if (profile.basalprofile[k].start == baseTime || profile.basalprofile[k+1].start > baseTime) {
  161. basalScheduledRate = profile.basalprofile[k].rate;
  162. insulin = basalScheduledRate * basDuration;
  163. break;
  164. }
  165. else if (profile.basalprofile[k].start < baseTime && profile.basalprofile[k+1].start > baseTime) {
  166. basalScheduledRate = profile.basalprofile[k].rate;
  167. insulin = basalScheduledRate * basDuration;
  168. break;
  169. }
  170. else if (k = profile.basalprofile.length) {
  171. basalScheduledRate = profile.basalprofile[k-1].rate;
  172. insulin = basalScheduledRate * basDuration;
  173. break;
  174. }
  175. }
  176. // Account for smallest possible pump dosage
  177. incrementsRaw = insulin / minimalDose;
  178. if (incrementsRaw >= 1) {
  179. incrementsRounded = Math.floor(incrementsRaw);
  180. scheduledBasalInsulin += incrementsRounded * minimalDose;
  181. } else { insulin = 0}
  182. }
  183. }
  184. }
  185. TDD = bolusInsulin + tempInsulin + scheduledBasalInsulin;
  186. logBolus = ". Bolus insulin: " + bolusInsulin.toPrecision(5) + " U";
  187. logTempBasal = ". Temporary basal insulin: " + tempInsulin.toPrecision(5) + " U";
  188. logBasal = ". Delivered scheduled basal rate insulin: " + scheduledBasalInsulin.toPrecision(5) + " U";
  189. logTDD = ". TDD past 24h is: " + TDD.toPrecision(5) + " U";
  190. // ----------------------------------------------------
  191. // Chris' formula with added adjustmentFactor for tuning:
  192. if (chrisFormula == true && TDD > 0) {
  193. var newRatio = profile.sens / (277700 / (adjustmentFactor * TDD * BG));
  194. log = "New ratio using Chris' formula is " + newRatio.toPrecision(3) + " with ISF: " + (profile.sens / newRatio).toPrecision(3) + " (" + ((profile.sens / newRatio) * 0.0555).toPrecision(3) + " mmol/l/U)";
  195. // Respect autosens.max and autosens.min limits
  196. if (newRatio > maxLimitChris) {
  197. newRatio = maxLimitChris;
  198. log = "Chris' formula hit limit by autosens_max setting: " + maxLimitChris + ". ISF: " + (profile.sens / newRatio).toPrecision(3) + " (" + ((profile.sens / newRatio) * 0.0555).toPrecision(3) + " mmol/l/U)";
  199. } else if (newRatio < minLimitChris) {
  200. newRatio = minLimitChris;
  201. log = "Chris' formula hit limit by autosens_min setting: " + minLimitChris + ". ISF: " + (profile.sens / newRatio).toPrecision(3) + " (" + ((profile.sens / newRatio) * 0.0555).toPrecision(3) + " mmol/l/U)";
  202. }
  203. // Set the new ratio
  204. autosens.ratio = newRatio;
  205. // Print to log
  206. return log + logTDD + logBolus + logTempBasal + logBasal;
  207. } else { return "Chris' formula is off." }
  208. }