determine_basal.js 11 KB

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