determine_basal.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  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. morePresentTime = new Date();
  61. break;
  62. } else if (pumphistory[j]._type == "TempBasal" || pumphistory[j]._type == "PumpSuspend") {
  63. 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. let string = "" + hour + ":" + minutes + ":" + seconds;
  109. let baseRate = new Date(string);
  110. let basalScheduledRate = 0;
  111. for (let k = 0; k < profile.basalprofile.length; k++) {
  112. if (profile.basalprofile[k].start == baseRate) {
  113. basalScheduledRate = profile.basalprofile[k].rate;
  114. insulin = basalScheduledRate * basDuration;
  115. break;
  116. } else if (k + 1 < profile.basalprofile.length) {
  117. if (profile.basalprofile[k].start < baseRate && profile.basalprofile[k+1].start > baseRate){
  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 == "TempBasal") {
  137. let time1 = new Date(pumphistory[i].timestamp);
  138. let time2 = time1;
  139. let m = i;
  140. do {
  141. --m;
  142. if (pumphistory[m]._type == "TempBasal" && m >= 0) {
  143. // next (newer) temp basal
  144. let time2 = new Date(pumphistory[m].timestamp);
  145. break;
  146. }
  147. } while (m >= 0);
  148. let basDuration = (time2 - time1) / 36e5;
  149. if ((pumphistory[i-1]['duration (min)'] / 60 ) < basDuration) {
  150. let timeOfbasal = basDuration - (pumphistory[i-1]['duration (min)'] / 60); //
  151. console.log('timeOfBasal: ' + timeOfbasal);
  152. let hour = time1.getHours();
  153. let minutes = time1.getMinutes();
  154. let seconds = "00";
  155. let string = "" + hour + ":" + minutes + ":" + seconds;
  156. let baseTime = new Date(string);
  157. console.log('baseTime: ' + baseTime + ' and time dirrence:' + (time2 - time1) / 36e5);
  158. for (let k = 0; k < profile.basalprofile.length; k++) {
  159. if (profile.basalprofile[k].start == baseTime) {
  160. let basalScheduledRate = profile.basalprofile[k].rate;
  161. insulin = basalScheduledRate * timeOfbasal;
  162. break;
  163. }
  164. else if (k+1 <= (profile.basalprofile.length - 1)) {
  165. if (profile.basalprofile[k].start < baseTime && profile.basalprofile[k+1].start > baseTime) {
  166. basalScheduledRate = profile.basalprofile[k].rate;
  167. insulin = basalScheduledRate * timeOfbasal;
  168. break;
  169. }
  170. else if (k == (profile.basalprofile.length - 1) && baseTime >= profile.basalprofile[k].start) {
  171. basalScheduledRate = profile.basalprofile[k].rate;
  172. insulin = basalScheduledRate * timeOfbasal;
  173. break;
  174. }
  175. }
  176. }
  177. // Account for smallest possible pump dosage
  178. incrementsRaw = insulin / minimalDose;
  179. if (incrementsRaw >= 1) {
  180. incrementsRounded = Math.floor(incrementsRaw);
  181. scheduledBasalInsulin += incrementsRounded * minimalDose;
  182. } else { insulin = 0}
  183. }
  184. }
  185. }
  186. TDD = bolusInsulin + tempInsulin + scheduledBasalInsulin;
  187. logBolus = ". Bolus insulin: " + bolusInsulin.toPrecision(5) + " U";
  188. logTempBasal = ". Temporary basal insulin: " + tempInsulin.toPrecision(5) + " U";
  189. logBasal = ". Delivered scheduled basal rate insulin: " + scheduledBasalInsulin.toPrecision(5) + " U";
  190. logTDD = ". TDD past 24h is: " + TDD.toPrecision(5) + " U";
  191. // ----------------------------------------------------
  192. // Chris' formula with added adjustmentFactor for tuning:
  193. if (chrisFormula == true && TDD > 0) {
  194. var newRatio = profile.sens / (277700 / (adjustmentFactor * TDD * BG));
  195. 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)";
  196. // Respect autosens.max and autosens.min limits
  197. if (newRatio > maxLimitChris) {
  198. newRatio = maxLimitChris;
  199. 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)";
  200. } else if (newRatio < minLimitChris) {
  201. newRatio = minLimitChris;
  202. 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)";
  203. }
  204. // Set the new ratio
  205. autosens.ratio = newRatio;
  206. // Print to log
  207. return log + logTDD + logBolus + logTempBasal + logBasal;
  208. } else { return "Chris' formula is off." }
  209. }