determine_basal.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  1. function middleware(iob, currenttemp, glucose, profile, autosens, meal, reservoir, clock, pumphistory, preferences) {
  2. // This middleware only works if you have added pumphistory and preferences to middleware in FreeAPS X code (my mw_preferences 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 k = 0; k < pumphistory.length; k++) {
  104. // Check for 0 temp basals with 0 min duration.
  105. insulin = 0;
  106. if (pumphistory[k]['duration (min)'] == 0) {
  107. let time1 = new Date(pumphistory[k].timestamp);
  108. let time2 = time1;
  109. let l = k;
  110. do {
  111. --l;
  112. if (pumphistory[l]._type == "TempBasal" && l >= 0) {
  113. time2 = new Date(pumphistory[l].timestamp);
  114. break;
  115. }
  116. } while (l > 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 m = 0; m < profile.basalprofile.length; m++) {
  127. if (profile.basalprofile[m].start == baseTime) {
  128. basalScheduledRate = profile.basalprofile[m].rate;
  129. insulin = basalScheduledRate * basDuration;
  130. break;
  131. }
  132. else if (m + 1 < profile.basalprofile.length) {
  133. if (profile.basalprofile[m].start < baseTime && profile.basalprofile[m+1].start > baseTime) {
  134. basalScheduledRate = profile.basalprofile[m].rate;
  135. insulin = basalScheduledRate * basDuration;
  136. break;
  137. }
  138. }
  139. else if (m == profile.basalprofile.length - 1) {
  140. basalScheduledRate = profile.basalprofile[m].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. }
  156. // Check for temp basals that completes
  157. for (let n = pumphistory.length -1; n > 0; n--) {
  158. if (pumphistory[n]._type == "TempBasalDuration") {
  159. // duration in hours
  160. let oldBasalDuration = pumphistory[n]['duration (min)'] / 60;
  161. // time of old temp basal
  162. let oldTime = new Date(pumphistory[n].timestamp);
  163. let newTime = oldTime;
  164. let o = n;
  165. do {
  166. --o;
  167. if (o >= 0) {
  168. if (pumphistory[o]._type == "TempBasal") {
  169. // time of next (new) temp basal
  170. newTime = new Date(pumphistory[o].timestamp);
  171. break;
  172. }
  173. }
  174. } while (o > 0);
  175. // Time difference in hours, new - old
  176. let tempBasalTimeDifference = (newTime - oldTime) / 36e5;
  177. let timeOfbasal = tempBasalTimeDifference - oldBasalDuration;
  178. // if duration of scheduled basal is more than 0
  179. if (timeOfbasal > 0) {
  180. let hour = oldTime.getHours();
  181. let minutes = oldTime.getMinutes();
  182. let seconds = "00";
  183. // "hour:minutes:00"
  184. let timeString = "" + hour + ":" + minutes + ":" + seconds;
  185. let baseTime = new Date(timeString);
  186. // Default if correct basal schedule rate not found
  187. let basalScheduledRate = profile.basalprofile[0].rate;
  188. for (let p = 0; p < profile.basalprofile.length; ++p) {
  189. let basalRateTime = new Date(profile.basalprofile[p].start);
  190. if (basalRateTime == baseTime) {
  191. basalScheduledRate = profile.basalprofile[p].rate;
  192. break;
  193. }
  194. else if (p+1 < profile.basalprofile.length) {
  195. let nextBasalRateTime = new Date(profile.basalprofile[p+1].start);
  196. if (basalRateTime < baseTime && nextBasalRateTime > baseTime) {
  197. basalScheduledRate = profile.basalprofile[p].rate;
  198. break;
  199. }
  200. }
  201. else if (p == (profile.basalprofile.length - 1)) {
  202. basalScheduledRate = profile.basalprofile[p].rate;
  203. break;
  204. }
  205. }
  206. insulin = basalScheduledRate * timeOfbasal;
  207. // Account for smallest possible pump dosage
  208. incrementsRaw = insulin / minimalDose;
  209. if (incrementsRaw >= 1) {
  210. incrementsRounded = Math.floor(incrementsRaw);
  211. scheduledBasalInsulin += incrementsRounded * minimalDose;
  212. } else { insulin = 0}
  213. }
  214. }
  215. }
  216. TDD = bolusInsulin + tempInsulin + scheduledBasalInsulin;
  217. logBolus = ". Bolus insulin: " + bolusInsulin.toPrecision(5) + " U";
  218. logTempBasal = ". Temporary basal insulin: " + tempInsulin.toPrecision(5) + " U";
  219. logBasal = ". Delivered scheduled basal rate insulin: " + scheduledBasalInsulin.toPrecision(5) + " U";
  220. logTDD = ". TDD past 24h is: " + TDD.toPrecision(5) + " U";
  221. // ----------------------------------------------------
  222. // Chris' formula with added adjustmentFactor for tuning:
  223. if (chrisFormula == true && TDD > 0) {
  224. var newRatio = profile.sens / (277700 / (adjustmentFactor * TDD * BG));
  225. 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)";
  226. // Respect autosens.max and autosens.min limits
  227. if (newRatio > maxLimitChris) {
  228. newRatio = maxLimitChris;
  229. log = "Chris' formula hit limit by autosens_max setting: " + maxLimitChris + ". ISF: " + (profile.sens / maxLimitChrisv).toPrecision(3) + " (" + ((profile.sens / maxLimitChris) * 0.0555).toPrecision(3) + " mmol/l/U)";
  230. } else if (newRatio < minLimitChris) {
  231. newRatio = minLimitChris;
  232. log = "Chris' formula hit limit by autosens_min setting: " + minLimitChris + ". ISF: " + (profile.sens / minLimitChris).toPrecision(3) + " (" + ((profile.sens / minLimitChris) * 0.0555).toPrecision(3) + " mmol/l/U)";
  233. }
  234. // Set the new ratio
  235. autosens.ratio = newRatio;
  236. // Print to log
  237. return log + logTDD + logBolus + logTempBasal + logBasal;
  238. } else { return "Chris' formula is off." }
  239. }