determine_basal.js 12 KB

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