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_M192_M871.cpp 13KB

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