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 8.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  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 HAS_PTC
  24. //#define DEBUG_PTC // Print extra debug output with 'M871'
  25. #include "probe_temp_comp.h"
  26. #include <math.h>
  27. ProbeTempComp ptc;
  28. #if ENABLED(PTC_PROBE)
  29. constexpr int16_t z_offsets_probe_default[PTC_PROBE_COUNT] = PTC_PROBE_ZOFFS;
  30. int16_t ProbeTempComp::z_offsets_probe[PTC_PROBE_COUNT] = PTC_PROBE_ZOFFS;
  31. #endif
  32. #if ENABLED(PTC_BED)
  33. constexpr int16_t z_offsets_bed_default[PTC_BED_COUNT] = PTC_BED_ZOFFS;
  34. int16_t ProbeTempComp::z_offsets_bed[PTC_BED_COUNT] = PTC_BED_ZOFFS;
  35. #endif
  36. #if ENABLED(PTC_HOTEND)
  37. constexpr int16_t z_offsets_hotend_default[PTC_HOTEND_COUNT] = PTC_HOTEND_ZOFFS;
  38. int16_t ProbeTempComp::z_offsets_hotend[PTC_HOTEND_COUNT] = PTC_HOTEND_ZOFFS;
  39. #endif
  40. int16_t *ProbeTempComp::sensor_z_offsets[TSI_COUNT] = {
  41. #if ENABLED(PTC_PROBE)
  42. ProbeTempComp::z_offsets_probe,
  43. #endif
  44. #if ENABLED(PTC_BED)
  45. ProbeTempComp::z_offsets_bed,
  46. #endif
  47. #if ENABLED(PTC_HOTEND)
  48. ProbeTempComp::z_offsets_hotend,
  49. #endif
  50. };
  51. constexpr temp_calib_t ProbeTempComp::cali_info[TSI_COUNT];
  52. uint8_t ProbeTempComp::calib_idx; // = 0
  53. float ProbeTempComp::init_measurement; // = 0.0
  54. void ProbeTempComp::reset() {
  55. TERN_(PTC_PROBE, LOOP_L_N(i, PTC_PROBE_COUNT) z_offsets_probe[i] = z_offsets_probe_default[i]);
  56. TERN_(PTC_BED, LOOP_L_N(i, PTC_BED_COUNT) z_offsets_bed[i] = z_offsets_bed_default[i]);
  57. TERN_(PTC_HOTEND, LOOP_L_N(i, PTC_HOTEND_COUNT) z_offsets_hotend[i] = z_offsets_hotend_default[i]);
  58. }
  59. void ProbeTempComp::clear_offsets(const TempSensorID tsi) {
  60. LOOP_L_N(i, cali_info[tsi].measurements)
  61. sensor_z_offsets[tsi][i] = 0;
  62. calib_idx = 0;
  63. }
  64. bool ProbeTempComp::set_offset(const TempSensorID tsi, const uint8_t idx, const int16_t offset) {
  65. if (idx >= cali_info[tsi].measurements) return false;
  66. sensor_z_offsets[tsi][idx] = offset;
  67. return true;
  68. }
  69. void ProbeTempComp::print_offsets() {
  70. LOOP_L_N(s, TSI_COUNT) {
  71. celsius_t temp = cali_info[s].start_temp;
  72. for (int16_t i = -1; i < cali_info[s].measurements; ++i) {
  73. SERIAL_ECHOF(
  74. TERN_(PTC_BED, s == TSI_BED ? F("Bed") :)
  75. TERN_(PTC_HOTEND, s == TSI_EXT ? F("Extruder") :)
  76. F("Probe")
  77. );
  78. SERIAL_ECHOLNPGM(
  79. " temp: ", temp,
  80. "C; Offset: ", i < 0 ? 0.0f : sensor_z_offsets[s][i], " um"
  81. );
  82. temp += cali_info[s].temp_resolution;
  83. }
  84. }
  85. #if ENABLED(DEBUG_PTC)
  86. float meas[4] = { 0, 0, 0, 0 };
  87. compensate_measurement(TSI_PROBE, 27.5, meas[0]);
  88. compensate_measurement(TSI_PROBE, 32.5, meas[1]);
  89. compensate_measurement(TSI_PROBE, 77.5, meas[2]);
  90. compensate_measurement(TSI_PROBE, 82.5, meas[3]);
  91. SERIAL_ECHOLNPGM("DEBUG_PTC 27.5:", meas[0], " 32.5:", meas[1], " 77.5:", meas[2], " 82.5:", meas[3]);
  92. #endif
  93. }
  94. void ProbeTempComp::prepare_new_calibration(const_float_t init_meas_z) {
  95. calib_idx = 0;
  96. init_measurement = init_meas_z;
  97. }
  98. void ProbeTempComp::push_back_new_measurement(const TempSensorID tsi, const_float_t meas_z) {
  99. if (calib_idx >= cali_info[tsi].measurements) return;
  100. sensor_z_offsets[tsi][calib_idx++] = static_cast<int16_t>((meas_z - init_measurement) * 1000.0f);
  101. }
  102. bool ProbeTempComp::finish_calibration(const TempSensorID tsi) {
  103. if (!calib_idx) {
  104. SERIAL_ECHOLNPGM("!No measurements.");
  105. clear_offsets(tsi);
  106. return false;
  107. }
  108. const uint8_t measurements = cali_info[tsi].measurements;
  109. const celsius_t start_temp = cali_info[tsi].start_temp,
  110. res_temp = cali_info[tsi].temp_resolution;
  111. int16_t * const data = sensor_z_offsets[tsi];
  112. // Extrapolate
  113. float k, d;
  114. if (calib_idx < measurements) {
  115. SERIAL_ECHOLNPGM("Got ", calib_idx, " measurements. ");
  116. if (linear_regression(tsi, k, d)) {
  117. SERIAL_ECHOPGM("Applying linear extrapolation");
  118. for (; calib_idx < measurements; ++calib_idx) {
  119. const celsius_float_t temp = start_temp + float(calib_idx + 1) * res_temp;
  120. data[calib_idx] = static_cast<int16_t>(k * temp + d);
  121. }
  122. }
  123. else {
  124. // Simply use the last measured value for higher temperatures
  125. SERIAL_ECHOPGM("Failed to extrapolate");
  126. const int16_t last_val = data[calib_idx-1];
  127. for (; calib_idx < measurements; ++calib_idx)
  128. data[calib_idx] = last_val;
  129. }
  130. SERIAL_ECHOLNPGM(" for higher temperatures.");
  131. }
  132. // Sanity check
  133. for (calib_idx = 0; calib_idx < measurements; ++calib_idx) {
  134. // Restrict the max. offset
  135. if (ABS(data[calib_idx]) > 2000) {
  136. SERIAL_ECHOLNPGM("!Invalid Z-offset detected (0-2).");
  137. clear_offsets(tsi);
  138. return false;
  139. }
  140. // Restrict the max. offset difference between two probings
  141. if (calib_idx > 0 && ABS(data[calib_idx - 1] - data[calib_idx]) > 800) {
  142. SERIAL_ECHOLNPGM("!Invalid Z-offset between two probings detected (0-0.8).");
  143. clear_offsets(tsi);
  144. return false;
  145. }
  146. }
  147. return true;
  148. }
  149. void ProbeTempComp::compensate_measurement(const TempSensorID tsi, const celsius_t temp, float &meas_z) {
  150. const uint8_t measurements = cali_info[tsi].measurements;
  151. const celsius_t start_temp = cali_info[tsi].start_temp,
  152. res_temp = cali_info[tsi].temp_resolution,
  153. end_temp = start_temp + measurements * res_temp;
  154. const int16_t * const data = sensor_z_offsets[tsi];
  155. // Given a data index, return { celsius, zoffset } in the form { x, y }
  156. auto tpoint = [&](uint8_t i) -> xy_float_t {
  157. return xy_float_t({ static_cast<float>(start_temp) + i * res_temp, i ? static_cast<float>(data[i - 1]) : 0.0f });
  158. };
  159. // Interpolate Z based on a temperature being within a given range
  160. auto linear_interp = [](const_float_t x, xy_float_t p1, xy_float_t p2) {
  161. // zoffs1 + zoffset_per_toffset * toffset
  162. return p1.y + (p2.y - p1.y) / (p2.x - p1.x) * (x - p1.x);
  163. };
  164. // offset in µm
  165. float offset = 0.0f;
  166. #if PTC_LINEAR_EXTRAPOLATION
  167. if (temp < start_temp)
  168. offset = linear_interp(temp, tpoint(0), tpoint(PTC_LINEAR_EXTRAPOLATION));
  169. else if (temp >= end_temp)
  170. offset = linear_interp(temp, tpoint(measurements - PTC_LINEAR_EXTRAPOLATION), tpoint(measurements));
  171. #else
  172. if (temp < start_temp)
  173. offset = 0.0f;
  174. else if (temp >= end_temp)
  175. offset = static_cast<float>(data[measurements - 1]);
  176. #endif
  177. else {
  178. // Linear interpolation
  179. const int8_t idx = static_cast<int8_t>((temp - start_temp) / res_temp);
  180. offset = linear_interp(temp, tpoint(idx), tpoint(idx + 1));
  181. }
  182. // convert offset to mm and apply it
  183. meas_z -= offset / 1000.0f;
  184. }
  185. bool ProbeTempComp::linear_regression(const TempSensorID tsi, float &k, float &d) {
  186. if (!WITHIN(calib_idx, 1, cali_info[tsi].measurements)) return false;
  187. const celsius_t start_temp = cali_info[tsi].start_temp,
  188. res_temp = cali_info[tsi].temp_resolution;
  189. const int16_t * const data = sensor_z_offsets[tsi];
  190. float sum_x = start_temp,
  191. sum_x2 = sq(start_temp),
  192. sum_xy = 0, sum_y = 0;
  193. float xi = static_cast<float>(start_temp);
  194. LOOP_L_N(i, calib_idx) {
  195. const float yi = static_cast<float>(data[i]);
  196. xi += res_temp;
  197. sum_x += xi;
  198. sum_x2 += sq(xi);
  199. sum_xy += xi * yi;
  200. sum_y += yi;
  201. }
  202. const float denom = static_cast<float>(calib_idx + 1) * sum_x2 - sq(sum_x);
  203. if (fabs(denom) <= 10e-5) {
  204. // Singularity - unable to solve
  205. k = d = 0.0;
  206. return false;
  207. }
  208. k = (static_cast<float>(calib_idx + 1) * sum_xy - sum_x * sum_y) / denom;
  209. d = (sum_y - k * sum_x) / static_cast<float>(calib_idx + 1);
  210. return true;
  211. }
  212. #endif // HAS_PTC