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.6KB

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