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

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