My Marlin configs for Fabrikator Mini and CTC i3 Pro B
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

probe_temp_comp.cpp 7.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. /**
  2. * Marlin 3D Printer Firmware
  3. * Copyright (c) 2020 MarlinFirmware [https://github.com/MarlinFirmware/Marlin]
  4. *
  5. * Based on Sprinter and grbl.
  6. * Copyright (c) 2011 Camiel Gubbels / Erik van der Zalm
  7. *
  8. * This program is free software: you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation, either version 3 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  20. *
  21. */
  22. #include "../inc/MarlinConfigPre.h"
  23. #if ENABLED(PROBE_TEMP_COMPENSATION)
  24. #include "probe_temp_comp.h"
  25. #include <math.h>
  26. ProbeTempComp temp_comp;
  27. int16_t ProbeTempComp::z_offsets_probe[ProbeTempComp::cali_info_init[TSI_PROBE].measurements], // = {0}
  28. ProbeTempComp::z_offsets_bed[ProbeTempComp::cali_info_init[TSI_BED].measurements]; // = {0}
  29. #if ENABLED(USE_TEMP_EXT_COMPENSATION)
  30. int16_t ProbeTempComp::z_offsets_ext[ProbeTempComp::cali_info_init[TSI_EXT].measurements]; // = {0}
  31. #endif
  32. int16_t *ProbeTempComp::sensor_z_offsets[TSI_COUNT] = {
  33. ProbeTempComp::z_offsets_probe, ProbeTempComp::z_offsets_bed
  34. #if ENABLED(USE_TEMP_EXT_COMPENSATION)
  35. , ProbeTempComp::z_offsets_ext
  36. #endif
  37. };
  38. const temp_calib_t ProbeTempComp::cali_info[TSI_COUNT] = {
  39. ProbeTempComp::cali_info_init[TSI_PROBE], ProbeTempComp::cali_info_init[TSI_BED]
  40. #if ENABLED(USE_TEMP_EXT_COMPENSATION)
  41. , ProbeTempComp::cali_info_init[TSI_EXT]
  42. #endif
  43. };
  44. uint8_t ProbeTempComp::calib_idx; // = 0
  45. float ProbeTempComp::init_measurement; // = 0.0
  46. void ProbeTempComp::clear_offsets(const TempSensorID tsi) {
  47. LOOP_L_N(i, cali_info[tsi].measurements)
  48. sensor_z_offsets[tsi][i] = 0;
  49. calib_idx = 0;
  50. }
  51. bool ProbeTempComp::set_offset(const TempSensorID tsi, const uint8_t idx, const int16_t offset) {
  52. if (idx >= cali_info[tsi].measurements) return false;
  53. sensor_z_offsets[tsi][idx] = offset;
  54. return true;
  55. }
  56. void ProbeTempComp::print_offsets() {
  57. LOOP_L_N(s, TSI_COUNT) {
  58. float temp = cali_info[s].start_temp;
  59. for (int16_t i = -1; i < cali_info[s].measurements; ++i) {
  60. serialprintPGM(s == TSI_BED ? PSTR("Bed") :
  61. #if ENABLED(USE_TEMP_EXT_COMPENSATION)
  62. s == TSI_EXT ? PSTR("Extruder") :
  63. #endif
  64. PSTR("Probe")
  65. );
  66. SERIAL_ECHOLNPAIR(
  67. " temp: ", temp,
  68. "C; Offset: ", i < 0 ? 0.0f : sensor_z_offsets[s][i], " um"
  69. );
  70. temp += cali_info[s].temp_res;
  71. }
  72. }
  73. }
  74. void ProbeTempComp::prepare_new_calibration(const float &init_meas_z) {
  75. calib_idx = 0;
  76. init_measurement = init_meas_z;
  77. }
  78. void ProbeTempComp::push_back_new_measurement(const TempSensorID tsi, const float &meas_z) {
  79. switch (tsi) {
  80. case TSI_PROBE:
  81. case TSI_BED:
  82. //case TSI_EXT:
  83. if (calib_idx >= cali_info[tsi].measurements) return;
  84. sensor_z_offsets[tsi][calib_idx++] = static_cast<int16_t>(meas_z * 1000.0f - init_measurement * 1000.0f);
  85. default: break;
  86. }
  87. }
  88. bool ProbeTempComp::finish_calibration(const TempSensorID tsi) {
  89. if (tsi != TSI_PROBE && tsi != TSI_BED) return false;
  90. if (calib_idx < 3) {
  91. SERIAL_ECHOLNPGM("!Insufficient measurements (min. 3).");
  92. clear_offsets(tsi);
  93. return false;
  94. }
  95. const uint8_t measurements = cali_info[tsi].measurements;
  96. const float start_temp = cali_info[tsi].start_temp,
  97. res_temp = cali_info[tsi].temp_res;
  98. int16_t * const data = sensor_z_offsets[tsi];
  99. // Extrapolate
  100. float k, d;
  101. if (calib_idx < measurements) {
  102. SERIAL_ECHOLNPAIR("Got ", calib_idx, " measurements. ");
  103. if (linear_regression(tsi, k, d)) {
  104. SERIAL_ECHOPGM("Applying linear extrapolation");
  105. calib_idx--;
  106. for (; calib_idx < measurements; ++calib_idx) {
  107. const float temp = start_temp + float(calib_idx) * res_temp;
  108. data[calib_idx] = static_cast<int16_t>(k * temp + d);
  109. }
  110. }
  111. else {
  112. // Simply use the last measured value for higher temperatures
  113. SERIAL_ECHOPGM("Failed to extrapolate");
  114. const int16_t last_val = data[calib_idx];
  115. for (; calib_idx < measurements; ++calib_idx)
  116. data[calib_idx] = last_val;
  117. }
  118. SERIAL_ECHOLNPGM(" for higher temperatures.");
  119. }
  120. // Sanity check
  121. for (calib_idx = 0; calib_idx < measurements; ++calib_idx) {
  122. // Restrict the max. offset
  123. if (abs(data[calib_idx]) > 2000) {
  124. SERIAL_ECHOLNPGM("!Invalid Z-offset detected (0-2).");
  125. clear_offsets(tsi);
  126. return false;
  127. }
  128. // Restrict the max. offset difference between two probings
  129. if (calib_idx > 0 && abs(data[calib_idx - 1] - data[calib_idx]) > 800) {
  130. SERIAL_ECHOLNPGM("!Invalid Z-offset between two probings detected (0-0.8).");
  131. clear_offsets(TSI_PROBE);
  132. return false;
  133. }
  134. }
  135. return true;
  136. }
  137. void ProbeTempComp::compensate_measurement(const TempSensorID tsi, const float &temp, float &meas_z) {
  138. if (WITHIN(temp, cali_info[tsi].start_temp, cali_info[tsi].end_temp))
  139. meas_z -= get_offset_for_temperature(tsi, temp);
  140. }
  141. float ProbeTempComp::get_offset_for_temperature(const TempSensorID tsi, const float &temp) {
  142. const uint8_t measurements = cali_info[tsi].measurements;
  143. const float start_temp = cali_info[tsi].start_temp,
  144. end_temp = cali_info[tsi].end_temp,
  145. res_temp = cali_info[tsi].temp_res;
  146. const int16_t * const data = sensor_z_offsets[tsi];
  147. if (temp <= start_temp) return 0.0f;
  148. if (temp >= end_temp) return static_cast<float>(data[measurements - 1]) / 1000.0f;
  149. // Linear interpolation
  150. int16_t val1 = 0, val2 = data[0];
  151. uint8_t idx = 0;
  152. float meas_temp = start_temp + res_temp;
  153. while (meas_temp < temp) {
  154. if (++idx >= measurements) return static_cast<float>(val2) / 1000.0f;
  155. meas_temp += res_temp;
  156. val1 = val2;
  157. val2 = data[idx];
  158. }
  159. const float factor = (meas_temp - temp) / static_cast<float>(res_temp);
  160. return (static_cast<float>(val2) - static_cast<float>(val2 - val1) * factor) / 1000.0f;
  161. }
  162. bool ProbeTempComp::linear_regression(const TempSensorID tsi, float &k, float &d) {
  163. if (tsi != TSI_PROBE && tsi != TSI_BED) return false;
  164. if (!WITHIN(calib_idx, 2, cali_info[tsi].measurements)) return false;
  165. const float start_temp = cali_info[tsi].start_temp,
  166. res_temp = cali_info[tsi].temp_res;
  167. const int16_t * const data = sensor_z_offsets[tsi];
  168. float sum_x = start_temp,
  169. sum_x2 = sq(start_temp),
  170. sum_xy = 0, sum_y = 0;
  171. LOOP_L_N(i, calib_idx) {
  172. const float xi = start_temp + (i + 1) * res_temp,
  173. yi = static_cast<float>(data[i]);
  174. sum_x += xi;
  175. sum_x2 += sq(xi);
  176. sum_xy += xi * yi;
  177. sum_y += yi;
  178. }
  179. const float denom = static_cast<float>(calib_idx + 1) * sum_x2 - sq(sum_x);
  180. if (fabs(denom) <= 10e-5) {
  181. // Singularity - unable to solve
  182. k = d = 0.0;
  183. return false;
  184. }
  185. k = (static_cast<float>(calib_idx + 1) * sum_xy - sum_x * sum_y) / denom;
  186. d = (sum_y - k * sum_x) / static_cast<float>(calib_idx + 1);
  187. return true;
  188. }
  189. #endif // PROBE_TEMP_COMPENSATION