determine_basal.js 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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. var TDD = 0.0;
  7. const minLimitChris = profile.autosens_min;
  8. const maxLimitChris = profile.autosens_max;
  9. const adjustmentFactor = 1;
  10. // Your current target, lower limit
  11. const currentMinTarget = profile.min_bg;
  12. var exerciseSetting = false;
  13. var log = "";
  14. var logTDD = "";
  15. var current = 0;
  16. // If you have not set this to 0.05 in FAX settings (Omnipod), this will be set to 0.1 in code.
  17. var minimalDose = profile.bolus_increment;
  18. var insulin = 0.00;
  19. var incrementsRaw = 0.00;
  20. var incrementsRounded = 0.00;
  21. var quota = 0;
  22. if (profile.high_temptarget_raises_sensitivity == true || profile.exercise_mode == true) {
  23. exerciseSetting = true;
  24. }
  25. // Turn off Chris' formula (and AutoISF) when using a temp target >= 118 (6.5 mol/l) and if an exercise setting is enabled.
  26. // If using AutoISF uncomment the profile.use_autoisf = false
  27. if (currentMinTarget >= 118 && exerciseSetting == true) {
  28. // profile.use_autoisf = false;
  29. chrisFormula = false;
  30. log = "Chris' formula off due to a high temp target/exercising. Current min target: " + currentMinTarget;
  31. }
  32. // Calculate TDD --------------------------------------
  33. //Bolus:
  34. for (let i = 0; i < pumphistory.length; i++) {
  35. if (pumphistory[i]._type == "Bolus") {
  36. // Bolus delivered
  37. TDD += pumphistory[i].amount;
  38. }
  39. }
  40. // Temp basals:
  41. for (let j = 1; j < pumphistory.length; j++) {
  42. if (pumphistory[j]._type == "TempBasal" && pumphistory[j].rate > 0) {
  43. current = j;
  44. quota = pumphistory[j].rate;
  45. var duration = pumphistory[j-1]['duration (min)'] / 60;
  46. var origDur = duration;
  47. var pastTime = new Date(pumphistory[j-1].timestamp);
  48. // If temp basal hasn't yet ended, use now as end date for calculation
  49. do {
  50. j--;
  51. if (j <= 0) {
  52. var morePresentTime = new Date();
  53. break;
  54. } else if (pumphistory[j]._type == "TempBasal" || pumphistory[j]._type == "PumpSuspend") {
  55. var morePresentTime = new Date(pumphistory[j].timestamp);
  56. break;
  57. }
  58. }
  59. while (j >= 0);
  60. var diff = (morePresentTime - pastTime) / 36e5;
  61. if (diff < origDur) {
  62. duration = diff;
  63. }
  64. insulin = quota * duration;
  65. // Account for smallest possible pump dosage
  66. if (minimalDose != 0.05) {
  67. minimalDose = 0.1;
  68. }
  69. incrementsRaw = insulin / minimalDose;
  70. if (incrementsRaw >= 1) {
  71. incrementsRounded = Math.floor(incrementsRaw);
  72. insulin = incrementsRounded * minimalDose;
  73. } else { insulin = 0}
  74. // Add temp basal delivered to TDD
  75. TDD += insulin;
  76. j = current;
  77. }
  78. }
  79. logTDD = ". TDD past 24h is: " + TDD.toPrecision(3) + " U";
  80. // ----------------------------------------------------
  81. // Chris' formula:
  82. if (chrisFormula == true && TDD > 0) {
  83. var newRatio = profile.sens / (277700 / (adjustmentFactor * TDD * BG));
  84. 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)";
  85. // Respect autosens.max and autosens.min limits
  86. if (newRatio > maxLimitChris) {
  87. newRatio = maxLimitChris;
  88. 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)";
  89. } else if (newRatio < minLimitChris) {
  90. newRatio = minLimitChris;
  91. 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)";
  92. }
  93. // Set the new ratio
  94. autosens.ratio = newRatio;
  95. return log + logTDD;
  96. } else { return "Chris' formula is disabled." }
  97. }