My Marlin configs for Fabrikator Mini and CTC i3 Pro B
Du kannst nicht mehr als 25 Themen auswählen Themen müssen mit entweder einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

probe_temp_comp.cpp 7.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  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 <https://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[cali_info_init[TSI_PROBE].measurements], // = {0}
  28. ProbeTempComp::z_offsets_bed[cali_info_init[TSI_BED].measurements]; // = {0}
  29. #if ENABLED(USE_TEMP_EXT_COMPENSATION)
  30. int16_t ProbeTempComp::z_offsets_ext[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. cali_info_init[TSI_PROBE], cali_info_init[TSI_BED]
  40. #if ENABLED(USE_TEMP_EXT_COMPENSATION)
  41. , cali_info_init[TSI_EXT]
  42. #endif
  43. };
  44. constexpr xyz_pos_t ProbeTempComp::park_point;
  45. constexpr xy_pos_t ProbeTempComp::measure_point;
  46. constexpr int ProbeTempComp::probe_calib_bed_temp;
  47. uint8_t ProbeTempComp::calib_idx; // = 0
  48. float ProbeTempComp::init_measurement; // = 0.0
  49. void ProbeTempComp::clear_offsets(const TempSensorID tsi) {
  50. LOOP_L_N(i, cali_info[tsi].measurements)
  51. sensor_z_offsets[tsi][i] = 0;
  52. calib_idx = 0;
  53. }
  54. bool ProbeTempComp::set_offset(const TempSensorID tsi, const uint8_t idx, const int16_t offset) {
  55. if (idx >= cali_info[tsi].measurements) return false;
  56. sensor_z_offsets[tsi][idx] = offset;
  57. return true;
  58. }
  59. void ProbeTempComp::print_offsets() {
  60. LOOP_L_N(s, TSI_COUNT) {
  61. float temp = cali_info[s].start_temp;
  62. for (int16_t i = -1; i < cali_info[s].measurements; ++i) {
  63. serialprintPGM(s == TSI_BED ? PSTR("Bed") :
  64. #if ENABLED(USE_TEMP_EXT_COMPENSATION)
  65. s == TSI_EXT ? PSTR("Extruder") :
  66. #endif
  67. PSTR("Probe")
  68. );
  69. SERIAL_ECHOLNPAIR(
  70. " temp: ", temp,
  71. "C; Offset: ", i < 0 ? 0.0f : sensor_z_offsets[s][i], " um"
  72. );
  73. temp += cali_info[s].temp_res;
  74. }
  75. }
  76. }
  77. void ProbeTempComp::prepare_new_calibration(const float &init_meas_z) {
  78. calib_idx = 0;
  79. init_measurement = init_meas_z;
  80. }
  81. void ProbeTempComp::push_back_new_measurement(const TempSensorID tsi, const float &meas_z) {
  82. switch (tsi) {
  83. case TSI_PROBE:
  84. case TSI_BED:
  85. //case TSI_EXT:
  86. if (calib_idx >= cali_info[tsi].measurements) return;
  87. sensor_z_offsets[tsi][calib_idx++] = static_cast<int16_t>(meas_z * 1000.0f - init_measurement * 1000.0f);
  88. default: break;
  89. }
  90. }
  91. bool ProbeTempComp::finish_calibration(const TempSensorID tsi) {
  92. if (tsi != TSI_PROBE && tsi != TSI_BED) return false;
  93. if (calib_idx < 3) {
  94. SERIAL_ECHOLNPGM("!Insufficient measurements (min. 3).");
  95. clear_offsets(tsi);
  96. return false;
  97. }
  98. const uint8_t measurements = cali_info[tsi].measurements;
  99. const float start_temp = cali_info[tsi].start_temp,
  100. res_temp = cali_info[tsi].temp_res;
  101. int16_t * const data = sensor_z_offsets[tsi];
  102. // Extrapolate
  103. float k, d;
  104. if (calib_idx < measurements) {
  105. SERIAL_ECHOLNPAIR("Got ", calib_idx, " measurements. ");
  106. if (linear_regression(tsi, k, d)) {
  107. SERIAL_ECHOPGM("Applying linear extrapolation");
  108. calib_idx--;
  109. for (; calib_idx < measurements; ++calib_idx) {
  110. const float temp = start_temp + float(calib_idx) * res_temp;
  111. data[calib_idx] = static_cast<int16_t>(k * temp + d);
  112. }
  113. }
  114. else {
  115. // Simply use the last measured value for higher temperatures
  116. SERIAL_ECHOPGM("Failed to extrapolate");
  117. const int16_t last_val = data[calib_idx];
  118. for (; calib_idx < measurements; ++calib_idx)
  119. data[calib_idx] = last_val;
  120. }
  121. SERIAL_ECHOLNPGM(" for higher temperatures.");
  122. }
  123. // Sanity check
  124. for (calib_idx = 0; calib_idx < measurements; ++calib_idx) {
  125. // Restrict the max. offset
  126. if (abs(data[calib_idx]) > 2000) {
  127. SERIAL_ECHOLNPGM("!Invalid Z-offset detected (0-2).");
  128. clear_offsets(tsi);
  129. return false;
  130. }
  131. // Restrict the max. offset difference between two probings
  132. if (calib_idx > 0 && abs(data[calib_idx - 1] - data[calib_idx]) > 800) {
  133. SERIAL_ECHOLNPGM("!Invalid Z-offset between two probings detected (0-0.8).");
  134. clear_offsets(TSI_PROBE);
  135. return false;
  136. }
  137. }
  138. return true;
  139. }
  140. void ProbeTempComp::compensate_measurement(const TempSensorID tsi, const float &temp, float &meas_z) {
  141. if (WITHIN(temp, cali_info[tsi].start_temp, cali_info[tsi].end_temp))
  142. meas_z -= get_offset_for_temperature(tsi, temp);
  143. }
  144. float ProbeTempComp::get_offset_for_temperature(const TempSensorID tsi, const float &temp) {
  145. const uint8_t measurements = cali_info[tsi].measurements;
  146. const float start_temp = cali_info[tsi].start_temp,
  147. res_temp = cali_info[tsi].temp_res;
  148. const int16_t * const data = sensor_z_offsets[tsi];
  149. auto point = [&](uint8_t i) {
  150. return xy_float_t({start_temp + i*res_temp, static_cast<float>(data[i])});
  151. };
  152. auto linear_interp = [](float x, xy_float_t p1, xy_float_t p2) {
  153. return (p2.y - p1.y) / (p2.x - p2.y) * (x - p1.x) + p1.y;
  154. };
  155. // Linear interpolation
  156. uint8_t idx = static_cast<uint8_t>((temp - start_temp) / res_temp);
  157. // offset in um
  158. float offset = 0.0f;
  159. #if !defined(PTC_LINEAR_EXTRAPOLATION) || PTC_LINEAR_EXTRAPOLATION <= 0
  160. if (idx < 0)
  161. offset = 0.0f;
  162. else if (idx > measurements - 2)
  163. offset = static_cast<float>(data[measurements - 1]);
  164. #else
  165. if (idx < 0)
  166. offset = linear_interp(temp, point(0), point(PTC_LINEAR_EXTRAPOLATION));
  167. else if (idx > measurements - 2)
  168. offset = linear_interp(temp, point(measurements - PTC_LINEAR_EXTRAPOLATION - 1), point(measurements - 1));
  169. #endif
  170. else
  171. offset = linear_interp(temp, point(idx), point(idx + 1));
  172. // return offset in mm
  173. return offset / 1000.0f;
  174. }
  175. bool ProbeTempComp::linear_regression(const TempSensorID tsi, float &k, float &d) {
  176. if (tsi != TSI_PROBE && tsi != TSI_BED) return false;
  177. if (!WITHIN(calib_idx, 2, cali_info[tsi].measurements)) return false;
  178. const float start_temp = cali_info[tsi].start_temp,
  179. res_temp = cali_info[tsi].temp_res;
  180. const int16_t * const data = sensor_z_offsets[tsi];
  181. float sum_x = start_temp,
  182. sum_x2 = sq(start_temp),
  183. sum_xy = 0, sum_y = 0;
  184. LOOP_L_N(i, calib_idx) {
  185. const float xi = start_temp + (i + 1) * res_temp,
  186. yi = static_cast<float>(data[i]);
  187. sum_x += xi;
  188. sum_x2 += sq(xi);
  189. sum_xy += xi * yi;
  190. sum_y += yi;
  191. }
  192. const float denom = static_cast<float>(calib_idx + 1) * sum_x2 - sq(sum_x);
  193. if (fabs(denom) <= 10e-5) {
  194. // Singularity - unable to solve
  195. k = d = 0.0;
  196. return false;
  197. }
  198. k = (static_cast<float>(calib_idx + 1) * sum_xy - sum_x * sum_y) / denom;
  199. d = (sum_y - k * sum_x) / static_cast<float>(calib_idx + 1);
  200. return true;
  201. }
  202. #endif // PROBE_TEMP_COMPENSATION