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.

G76_M871.cpp 13KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339
  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. /**
  23. * G76_M871.cpp - Temperature calibration/compensation for z-probing
  24. */
  25. #include "../../inc/MarlinConfig.h"
  26. #if HAS_PTC
  27. #include "../gcode.h"
  28. #include "../../module/motion.h"
  29. #include "../../module/planner.h"
  30. #include "../../module/probe.h"
  31. #include "../../feature/bedlevel/bedlevel.h"
  32. #include "../../module/temperature.h"
  33. #include "../../module/probe.h"
  34. #include "../../feature/probe_temp_comp.h"
  35. #include "../../lcd/marlinui.h"
  36. /**
  37. * G76: calibrate probe and/or bed temperature offsets
  38. * Notes:
  39. * - When calibrating probe, bed temperature is held constant.
  40. * Compensation values are deltas to first probe measurement at probe temp. = 30°C.
  41. * - When calibrating bed, probe temperature is held constant.
  42. * Compensation values are deltas to first probe measurement at bed temp. = 60°C.
  43. * - The hotend will not be heated at any time.
  44. * - On my Průša MK3S clone I put a piece of paper between the probe and the hotend
  45. * so the hotend fan would not cool my probe constantly. Alternatively you could just
  46. * make sure the fan is not running while running the calibration process.
  47. *
  48. * Probe calibration:
  49. * - Moves probe to cooldown point.
  50. * - Heats up bed to 100°C.
  51. * - Moves probe to probing point (1mm above heatbed).
  52. * - Waits until probe reaches target temperature (30°C).
  53. * - Does a z-probing (=base value) and increases target temperature by 5°C.
  54. * - Waits until probe reaches increased target temperature.
  55. * - Does a z-probing (delta to base value will be a compensation value) and increases target temperature by 5°C.
  56. * - Repeats last two steps until max. temperature reached or timeout (i.e. probe does not heat up any further).
  57. * - Compensation values of higher temperatures will be extrapolated (using linear regression first).
  58. * While this is not exact by any means it is still better than simply using the last compensation value.
  59. *
  60. * Bed calibration:
  61. * - Moves probe to cooldown point.
  62. * - Heats up bed to 60°C.
  63. * - Moves probe to probing point (1mm above heatbed).
  64. * - Waits until probe reaches target temperature (30°C).
  65. * - Does a z-probing (=base value) and increases bed temperature by 5°C.
  66. * - Moves probe to cooldown point.
  67. * - Waits until probe is below 30°C and bed has reached target temperature.
  68. * - Moves probe to probing point and waits until it reaches target temperature (30°C).
  69. * - Does a z-probing (delta to base value will be a compensation value) and increases bed temperature by 5°C.
  70. * - Repeats last four points until max. bed temperature reached (110°C) or timeout.
  71. * - Compensation values of higher temperatures will be extrapolated (using linear regression first).
  72. * While this is not exact by any means it is still better than simply using the last compensation value.
  73. *
  74. * G76 [B | P]
  75. * - no flag - Both calibration procedures will be run.
  76. * - `B` - Run bed temperature calibration.
  77. * - `P` - Run probe temperature calibration.
  78. */
  79. #if BOTH(PTC_PROBE, PTC_BED)
  80. static void say_waiting_for() { SERIAL_ECHOPGM("Waiting for "); }
  81. static void say_waiting_for_probe_heating() { say_waiting_for(); SERIAL_ECHOLNPGM("probe heating."); }
  82. static void say_successfully_calibrated() { SERIAL_ECHOPGM("Successfully calibrated"); }
  83. static void say_failed_to_calibrate() { SERIAL_ECHOPGM("!Failed to calibrate"); }
  84. void GcodeSuite::G76() {
  85. auto report_temps = [](millis_t &ntr, millis_t timeout=0) {
  86. idle_no_sleep();
  87. const millis_t ms = millis();
  88. if (ELAPSED(ms, ntr)) {
  89. ntr = ms + 1000;
  90. thermalManager.print_heater_states(active_extruder);
  91. }
  92. return (timeout && ELAPSED(ms, timeout));
  93. };
  94. auto wait_for_temps = [&](const celsius_t tb, const celsius_t tp, millis_t &ntr, const millis_t timeout=0) {
  95. say_waiting_for(); SERIAL_ECHOLNPGM("bed and probe temperature.");
  96. while (thermalManager.wholeDegBed() != tb || thermalManager.wholeDegProbe() > tp)
  97. if (report_temps(ntr, timeout)) return true;
  98. return false;
  99. };
  100. auto g76_probe = [](const TempSensorID sid, celsius_t &targ, const xy_pos_t &nozpos) {
  101. do_z_clearance(5.0); // Raise nozzle before probing
  102. ptc.set_enabled(false);
  103. const float measured_z = probe.probe_at_point(nozpos, PROBE_PT_STOW, 0, false); // verbose=0, probe_relative=false
  104. ptc.set_enabled(true);
  105. if (isnan(measured_z))
  106. SERIAL_ECHOLNPGM("!Received NAN. Aborting.");
  107. else {
  108. SERIAL_ECHOLNPAIR_F("Measured: ", measured_z);
  109. if (targ == ProbeTempComp::cali_info[sid].start_temp)
  110. ptc.prepare_new_calibration(measured_z);
  111. else
  112. ptc.push_back_new_measurement(sid, measured_z);
  113. targ += ProbeTempComp::cali_info[sid].temp_resolution;
  114. }
  115. return measured_z;
  116. };
  117. #if ENABLED(BLTOUCH)
  118. // Make sure any BLTouch error condition is cleared
  119. bltouch_command(BLTOUCH_RESET, BLTOUCH_RESET_DELAY);
  120. set_bltouch_deployed(false);
  121. #endif
  122. bool do_bed_cal = parser.boolval('B'), do_probe_cal = parser.boolval('P');
  123. if (!do_bed_cal && !do_probe_cal) do_bed_cal = do_probe_cal = true;
  124. // Synchronize with planner
  125. planner.synchronize();
  126. #ifndef PTC_PROBE_HEATING_OFFSET
  127. #define PTC_PROBE_HEATING_OFFSET 0
  128. #endif
  129. const xyz_pos_t parkpos = PTC_PARK_POS,
  130. probe_pos_xyz = xyz_pos_t(PTC_PROBE_POS) + xyz_pos_t({ 0.0f, 0.0f, PTC_PROBE_HEATING_OFFSET }),
  131. noz_pos_xyz = probe_pos_xyz - probe.offset_xy; // Nozzle position based on probe position
  132. if (do_bed_cal || do_probe_cal) {
  133. // Ensure park position is reachable
  134. bool reachable = position_is_reachable(parkpos) || WITHIN(parkpos.z, Z_MIN_POS - fslop, Z_MAX_POS + fslop);
  135. if (!reachable)
  136. SERIAL_ECHOLNPGM("!Park");
  137. else {
  138. // Ensure probe position is reachable
  139. reachable = probe.can_reach(probe_pos_xyz);
  140. if (!reachable) SERIAL_ECHOLNPGM("!Probe");
  141. }
  142. if (!reachable) {
  143. SERIAL_ECHOLNPGM(" position unreachable - aborting.");
  144. return;
  145. }
  146. process_subcommands_now(FPSTR(G28_STR));
  147. }
  148. remember_feedrate_scaling_off();
  149. /******************************************
  150. * Calibrate bed temperature offsets
  151. ******************************************/
  152. // Report temperatures every second and handle heating timeouts
  153. millis_t next_temp_report = millis() + 1000;
  154. auto report_targets = [&](const celsius_t tb, const celsius_t tp) {
  155. SERIAL_ECHOLNPGM("Target Bed:", tb, " Probe:", tp);
  156. };
  157. if (do_bed_cal) {
  158. celsius_t target_bed = PTC_BED_START,
  159. target_probe = PTC_PROBE_TEMP;
  160. say_waiting_for(); SERIAL_ECHOLNPGM(" cooling.");
  161. while (thermalManager.wholeDegBed() > target_bed || thermalManager.wholeDegProbe() > target_probe)
  162. report_temps(next_temp_report);
  163. // Disable leveling so it won't mess with us
  164. TERN_(HAS_LEVELING, set_bed_leveling_enabled(false));
  165. for (uint8_t idx = 0; idx <= PTC_BED_COUNT; idx++) {
  166. thermalManager.setTargetBed(target_bed);
  167. report_targets(target_bed, target_probe);
  168. // Park nozzle
  169. do_blocking_move_to(parkpos);
  170. // Wait for heatbed to reach target temp and probe to cool below target temp
  171. if (wait_for_temps(target_bed, target_probe, next_temp_report, millis() + MIN_TO_MS(15))) {
  172. SERIAL_ECHOLNPGM("!Bed heating timeout.");
  173. break;
  174. }
  175. // Move the nozzle to the probing point and wait for the probe to reach target temp
  176. do_blocking_move_to(noz_pos_xyz);
  177. say_waiting_for_probe_heating();
  178. SERIAL_EOL();
  179. while (thermalManager.wholeDegProbe() < target_probe)
  180. report_temps(next_temp_report);
  181. const float measured_z = g76_probe(TSI_BED, target_bed, noz_pos_xyz);
  182. if (isnan(measured_z) || target_bed > (BED_MAX_TARGET)) break;
  183. }
  184. SERIAL_ECHOLNPGM("Retrieved measurements: ", ptc.get_index());
  185. if (ptc.finish_calibration(TSI_BED)) {
  186. say_successfully_calibrated();
  187. SERIAL_ECHOLNPGM(" bed.");
  188. }
  189. else {
  190. say_failed_to_calibrate();
  191. SERIAL_ECHOLNPGM(" bed. Values reset.");
  192. }
  193. // Cleanup
  194. thermalManager.setTargetBed(0);
  195. TERN_(HAS_LEVELING, set_bed_leveling_enabled(true));
  196. } // do_bed_cal
  197. /********************************************
  198. * Calibrate probe temperature offsets
  199. ********************************************/
  200. if (do_probe_cal) {
  201. // Park nozzle
  202. do_blocking_move_to(parkpos);
  203. // Initialize temperatures
  204. const celsius_t target_bed = BED_MAX_TARGET;
  205. thermalManager.setTargetBed(target_bed);
  206. celsius_t target_probe = PTC_PROBE_START;
  207. report_targets(target_bed, target_probe);
  208. // Wait for heatbed to reach target temp and probe to cool below target temp
  209. wait_for_temps(target_bed, target_probe, next_temp_report);
  210. // Disable leveling so it won't mess with us
  211. TERN_(HAS_LEVELING, set_bed_leveling_enabled(false));
  212. bool timeout = false;
  213. for (uint8_t idx = 0; idx <= PTC_PROBE_COUNT; idx++) {
  214. // Move probe to probing point and wait for it to reach target temperature
  215. do_blocking_move_to(noz_pos_xyz);
  216. say_waiting_for_probe_heating();
  217. SERIAL_ECHOLNPGM(" Bed:", target_bed, " Probe:", target_probe);
  218. const millis_t probe_timeout_ms = millis() + SEC_TO_MS(900UL);
  219. while (thermalManager.degProbe() < target_probe) {
  220. if (report_temps(next_temp_report, probe_timeout_ms)) {
  221. SERIAL_ECHOLNPGM("!Probe heating timed out.");
  222. timeout = true;
  223. break;
  224. }
  225. }
  226. if (timeout) break;
  227. const float measured_z = g76_probe(TSI_PROBE, target_probe, noz_pos_xyz);
  228. if (isnan(measured_z)) break;
  229. }
  230. SERIAL_ECHOLNPGM("Retrieved measurements: ", ptc.get_index());
  231. if (ptc.finish_calibration(TSI_PROBE))
  232. say_successfully_calibrated();
  233. else
  234. say_failed_to_calibrate();
  235. SERIAL_ECHOLNPGM(" probe.");
  236. // Cleanup
  237. thermalManager.setTargetBed(0);
  238. TERN_(HAS_LEVELING, set_bed_leveling_enabled(true));
  239. SERIAL_ECHOLNPGM("Final compensation values:");
  240. ptc.print_offsets();
  241. } // do_probe_cal
  242. restore_feedrate_and_scaling();
  243. }
  244. #endif // PTC_PROBE && PTC_BED
  245. /**
  246. * M871: Report / reset temperature compensation offsets.
  247. * Note: This does not affect values in EEPROM until M500.
  248. *
  249. * M871 [ R | B | P | E ]
  250. *
  251. * No Parameters - Print current offset values.
  252. *
  253. * Select only one of these flags:
  254. * R - Reset all offsets to zero (i.e., disable compensation).
  255. * B - Manually set offset for bed
  256. * P - Manually set offset for probe
  257. * E - Manually set offset for extruder
  258. *
  259. * With B, P, or E:
  260. * I[index] - Index in the array
  261. * V[value] - Adjustment in µm
  262. */
  263. void GcodeSuite::M871() {
  264. if (parser.seen('R')) {
  265. // Reset z-probe offsets to factory defaults
  266. ptc.clear_all_offsets();
  267. SERIAL_ECHOLNPGM("Offsets reset to default.");
  268. }
  269. else if (parser.seen("BPE")) {
  270. if (!parser.seenval('V')) return;
  271. const int16_t offset_val = parser.value_int();
  272. if (!parser.seenval('I')) return;
  273. const int16_t idx = parser.value_int();
  274. const TempSensorID mod = TERN_(PTC_BED, parser.seen_test('B') ? TSI_BED :)
  275. TERN_(PTC_HOTEND, parser.seen_test('E') ? TSI_EXT :)
  276. TERN_(PTC_PROBE, parser.seen_test('P') ? TSI_PROBE :) TSI_COUNT;
  277. if (mod == TSI_COUNT)
  278. SERIAL_ECHOLNPGM("!Invalid sensor.");
  279. else if (idx > 0 && ptc.set_offset(mod, idx - 1, offset_val))
  280. SERIAL_ECHOLNPGM("Set value: ", offset_val);
  281. else
  282. SERIAL_ECHOLNPGM("!Invalid index. Failed to set value (note: value at index 0 is constant).");
  283. }
  284. else // Print current Z-probe adjustments. Note: Values in EEPROM might differ.
  285. ptc.print_offsets();
  286. }
  287. #endif // HAS_PTC