determine_basal.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  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. // Your current target, lower limit
  10. const currentMinTarget = profile.min_bg;
  11. var exerciseSetting = 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. // Calculate TDD --------------------------------------
  39. //Bolus:
  40. for (let i = 0; i < pumphistory.length; i++) {
  41. if (pumphistory[i]._type == "Bolus") {
  42. bolusInsulin += pumphistory[i].amount;
  43. }
  44. }
  45. // Temp basals:
  46. if (minimalDose != 0.05) {
  47. minimalDose = 0.1;
  48. }
  49. for (let j = 1; j < pumphistory.length; j++) {
  50. if (pumphistory[j]._type == "TempBasal" && pumphistory[j].rate > 0) {
  51. current = j;
  52. quota = pumphistory[j].rate;
  53. var duration = pumphistory[j-1]['duration (min)'] / 60;
  54. var origDur = duration;
  55. var pastTime = new Date(pumphistory[j-1].timestamp);
  56. // If temp basal hasn't yet ended, use now as end date for calculation
  57. do {
  58. j--;
  59. if (j <= 0) {
  60. var morePresentTime = new Date();
  61. break;
  62. } else if (pumphistory[j]._type == "TempBasal" || pumphistory[j]._type == "PumpSuspend") {
  63. var morePresentTime = new Date(pumphistory[j].timestamp);
  64. break;
  65. }
  66. }
  67. while (j >= 0);
  68. var diff = (morePresentTime - pastTime) / 36e5;
  69. if (diff < origDur) {
  70. duration = diff;
  71. }
  72. insulin = quota * duration;
  73. // Account for smallest possible pump dosage
  74. incrementsRaw = insulin / minimalDose;
  75. if (incrementsRaw >= 1) {
  76. incrementsRounded = Math.floor(incrementsRaw);
  77. insulin = incrementsRounded * minimalDose;
  78. tempInsulin += insulin;
  79. } else { insulin = 0}
  80. j = current;
  81. }
  82. }
  83. // Check and count for when basals are delivered with a scheduled basal rate or an Autotuned basal rate.
  84. // 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.
  85. // 2. Check for temp basals that completes. This is for when disconected from link/iphone, or when in open loop.
  86. // To do: need to check for more circumstances when scheduled basal rates are used.
  87. //
  88. for (let i = 0; i < pumphistory.length; i++) {
  89. // Check for 0 temp basals with 0 min duration.
  90. insulin = 0;
  91. if (pumphistory[i]['duration (min)'] == 0) {
  92. let time1 = new Date(pumphistory[i].timestamp);
  93. let time2 = time1;
  94. let j = i;
  95. do {
  96. j--;
  97. if (pumphistory[j]._type == "TempBasal" && j >= 0) {
  98. time2 = new Date(pumphistory[j].timestamp);
  99. break;
  100. }
  101. } while (j >= 0);
  102. // duration of current scheduled basal in h
  103. let basDuration = (time2 - time1) / 36e5;
  104. if (basDuration > 0) {
  105. let hour = time1.getHours();
  106. let minutes = time1.getMinutes();
  107. let seconds = "00";
  108. string = "" + hour + ":" + minutes + ":" + seconds;
  109. let basalScheduledRate = 0;
  110. for (let k = 0; k < profile.basalprofile.length; k++) {
  111. if (profile.basalprofile[k].start == string) {
  112. basalScheduledRate = profile.basalprofile[k].rate;
  113. insulin = basalScheduledRate * basDuration;
  114. break;
  115. } else if (k + 1 < profile.basalprofile.length) {
  116. if (profile.basalprofile[k].start < string && profile.basalprofile[k+1].start > string){
  117. basalScheduledRate = profile.basalprofile[k].rate;
  118. insulin = basalScheduledRate * basDuration;
  119. break;
  120. }
  121. }
  122. }
  123. // Account for smallest possible pump dosage
  124. incrementsRaw = insulin / minimalDose;
  125. if (incrementsRaw >= 1) {
  126. incrementsRounded = Math.floor(incrementsRaw);
  127. insulin = incrementsRounded * minimalDose;
  128. scheduledBasalInsulin += insulin;
  129. } else { insulin = 0}
  130. }
  131. }
  132. // Check for temp basals that completes
  133. if (pumphistory[i]._type == "TempBasal") {
  134. let time1 = new Date(pumphistory[i].timestamp);
  135. let time2 = time1;
  136. for (let m = i; m < pumphistory.length; m--) {
  137. if (pumphistory[m]._type == "TempBasal") {
  138. let time2 = new Date(pumphistory[m].timestamp);
  139. break;
  140. }
  141. }
  142. let basDuration = (time2 - time1) / 36e5;
  143. if ((pumphistory[i-1]['duration (min)'] / 60 ) < basDuration) {
  144. let timeOrig = new Date(pumphistory[i-1].timestamp);
  145. for (let l = i-1; l < pumphistory.length; l++) {
  146. if (pumphistory[l]._type == "TempBasal") {
  147. let timeNext = new Date(timeOrig = pumphistory[l].timestamp);
  148. break;
  149. }
  150. }
  151. let durationOfSheduledBasal = (timeNext - timeOrig) / 36e5;
  152. let hour = time1.getHours();
  153. let minutes = time1.getMinutes();
  154. let seconds = "00";
  155. let string = "" + hour + ":" + minutes + ":" + seconds;
  156. let basalScheduledRate = 0;
  157. for (let k = 0; k < profile.basalprofile.length; k++) {
  158. if (profile.basalprofile[k].start == string) {
  159. basalScheduledRate = profile.basalprofile[k].rate;
  160. // This is the scheduled insulin amount delivered after a fully completed temp basal
  161. insulin = basalScheduledRate * basDuration;
  162. break;
  163. } else if (k + 1 < profile.basalprofile.length) {
  164. if (profile.basalprofile[k].start < string && profile.basalprofile[k+1].start > string){
  165. basalScheduledRate = profile.basalprofile[k].rate;
  166. // This is the scheduled insulin amount delivered after a fully completed temp basal
  167. insulin = basalScheduledRate * basDuration;
  168. break;
  169. }
  170. }
  171. }
  172. // Account for smallest possible pump dosage
  173. incrementsRaw = insulin / minimalDose;
  174. if (incrementsRaw >= 1) {
  175. incrementsRounded = Math.floor(incrementsRaw);
  176. scheduledBasalInsulin += incrementsRounded * minimalDose;
  177. } else { insulin = 0}
  178. }
  179. }
  180. }
  181. TDD = bolusInsulin + tempInsulin + scheduledBasalInsulin;
  182. logBolus = ". Bolus insulin: " + bolusInsulin.toPrecision(5) + " U";
  183. logTempBasal = ". Temporary basal insulin: " + tempInsulin.toPrecision(5) + " U";
  184. logBasal = ". Delivered scheduled basal rate insulin: " + scheduledBasalInsulin.toPrecision(5) + " U";
  185. logTDD = ". TDD past 24h is: " + TDD.toPrecision(5) + " U";
  186. // ----------------------------------------------------
  187. // Chris' formula with added adjustmentFactor for tuning:
  188. if (chrisFormula == true && TDD > 0) {
  189. var newRatio = profile.sens / (277700 / (adjustmentFactor * TDD * BG));
  190. 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)";
  191. // Respect autosens.max and autosens.min limits
  192. if (newRatio > maxLimitChris) {
  193. newRatio = maxLimitChris;
  194. 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)";
  195. } else if (newRatio < minLimitChris) {
  196. newRatio = minLimitChris;
  197. 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)";
  198. }
  199. // Set the new ratio
  200. autosens.ratio = newRatio;
  201. // Print to logs
  202. return log + logTDD + logBolus + logTempBasal + logBasal;
  203. } else { return "Chris' formula is off." }
  204. }