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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369
  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, 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. static void say_waiting_for() { SERIAL_ECHOPGM("Waiting for "); }
  87. static void say_waiting_for_probe_heating() { say_waiting_for(); SERIAL_ECHOLNPGM("probe heating."); }
  88. static void say_successfully_calibrated() { SERIAL_ECHOPGM("Successfully calibrated"); }
  89. static void say_failed_to_calibrate() { SERIAL_ECHOPGM("!Failed to calibrate"); }
  90. void GcodeSuite::G76() {
  91. // Check if heated bed is available and z-homing is done with probe
  92. #if TEMP_SENSOR_BED == 0 || !(HOMING_Z_WITH_PROBE)
  93. return;
  94. #endif
  95. auto report_temps = [](millis_t &ntr, millis_t timeout=0) {
  96. idle_no_sleep();
  97. const millis_t ms = millis();
  98. if (ELAPSED(ms, ntr)) {
  99. ntr = ms + 1000;
  100. thermalManager.print_heater_states(active_extruder);
  101. }
  102. return (timeout && ELAPSED(ms, timeout));
  103. };
  104. auto wait_for_temps = [&](const float tb, const float tp, millis_t &ntr, const millis_t timeout=0) {
  105. say_waiting_for(); SERIAL_ECHOLNPGM("bed and probe temperature.");
  106. while (fabs(thermalManager.degBed() - tb) > 0.1f || thermalManager.degProbe() > tp)
  107. if (report_temps(ntr, timeout)) return true;
  108. return false;
  109. };
  110. auto g76_probe = [](const TempSensorID sid, uint16_t &targ, const xy_pos_t &nozpos) {
  111. do_z_clearance(5.0); // Raise nozzle before probing
  112. const float measured_z = probe.probe_at_point(nozpos, PROBE_PT_STOW, 0, false); // verbose=0, probe_relative=false
  113. if (isnan(measured_z))
  114. SERIAL_ECHOLNPGM("!Received NAN. Aborting.");
  115. else {
  116. SERIAL_ECHOLNPAIR_F("Measured: ", measured_z);
  117. if (targ == cali_info_init[sid].start_temp)
  118. temp_comp.prepare_new_calibration(measured_z);
  119. else
  120. temp_comp.push_back_new_measurement(sid, measured_z);
  121. targ += cali_info_init[sid].temp_res;
  122. }
  123. return measured_z;
  124. };
  125. #if ENABLED(BLTOUCH)
  126. // Make sure any BLTouch error condition is cleared
  127. bltouch_command(BLTOUCH_RESET, BLTOUCH_RESET_DELAY);
  128. set_bltouch_deployed(false);
  129. #endif
  130. bool do_bed_cal = parser.boolval('B'), do_probe_cal = parser.boolval('P');
  131. if (!do_bed_cal && !do_probe_cal) do_bed_cal = do_probe_cal = true;
  132. // Synchronize with planner
  133. planner.synchronize();
  134. const xyz_pos_t parkpos = temp_comp.park_point,
  135. probe_pos_xyz = xyz_pos_t(temp_comp.measure_point) + xyz_pos_t({ 0.0f, 0.0f, PTC_PROBE_HEATING_OFFSET }),
  136. noz_pos_xyz = probe_pos_xyz - probe.offset_xy; // Nozzle position based on probe position
  137. if (do_bed_cal || do_probe_cal) {
  138. // Ensure park position is reachable
  139. bool reachable = position_is_reachable(parkpos) || WITHIN(parkpos.z, Z_MIN_POS - fslop, Z_MAX_POS + fslop);
  140. if (!reachable)
  141. SERIAL_ECHOLNPGM("!Park");
  142. else {
  143. // Ensure probe position is reachable
  144. reachable = probe.can_reach(probe_pos_xyz);
  145. if (!reachable) SERIAL_ECHOLNPGM("!Probe");
  146. }
  147. if (!reachable) {
  148. SERIAL_ECHOLNPGM(" position unreachable - aborting.");
  149. return;
  150. }
  151. process_subcommands_now_P(G28_STR);
  152. }
  153. remember_feedrate_scaling_off();
  154. /******************************************
  155. * Calibrate bed temperature offsets
  156. ******************************************/
  157. // Report temperatures every second and handle heating timeouts
  158. millis_t next_temp_report = millis() + 1000;
  159. auto report_targets = [&](const uint16_t tb, const uint16_t tp) {
  160. SERIAL_ECHOLNPAIR("Target Bed:", tb, " Probe:", tp);
  161. };
  162. if (do_bed_cal) {
  163. uint16_t target_bed = cali_info_init[TSI_BED].start_temp,
  164. target_probe = temp_comp.bed_calib_probe_temp;
  165. say_waiting_for(); SERIAL_ECHOLNPGM(" cooling.");
  166. while (thermalManager.degBed() > target_bed || thermalManager.degProbe() > target_probe)
  167. report_temps(next_temp_report);
  168. // Disable leveling so it won't mess with us
  169. TERN_(HAS_LEVELING, set_bed_leveling_enabled(false));
  170. for (;;) {
  171. thermalManager.setTargetBed(target_bed);
  172. report_targets(target_bed, target_probe);
  173. // Park nozzle
  174. do_blocking_move_to(parkpos);
  175. // Wait for heatbed to reach target temp and probe to cool below target temp
  176. if (wait_for_temps(target_bed, target_probe, next_temp_report, millis() + MIN_TO_MS(15))) {
  177. SERIAL_ECHOLNPGM("!Bed heating timeout.");
  178. break;
  179. }
  180. // Move the nozzle to the probing point and wait for the probe to reach target temp
  181. do_blocking_move_to(noz_pos_xyz);
  182. say_waiting_for_probe_heating();
  183. SERIAL_EOL();
  184. while (thermalManager.degProbe() < target_probe)
  185. report_temps(next_temp_report);
  186. const float measured_z = g76_probe(TSI_BED, target_bed, noz_pos_xyz);
  187. if (isnan(measured_z) || target_bed > BED_MAX_TARGET) break;
  188. }
  189. SERIAL_ECHOLNPAIR("Retrieved measurements: ", temp_comp.get_index());
  190. if (temp_comp.finish_calibration(TSI_BED)) {
  191. say_successfully_calibrated();
  192. SERIAL_ECHOLNPGM(" bed.");
  193. }
  194. else {
  195. say_failed_to_calibrate();
  196. SERIAL_ECHOLNPGM(" bed. Values reset.");
  197. }
  198. // Cleanup
  199. thermalManager.setTargetBed(0);
  200. TERN_(HAS_LEVELING, set_bed_leveling_enabled(true));
  201. } // do_bed_cal
  202. /********************************************
  203. * Calibrate probe temperature offsets
  204. ********************************************/
  205. if (do_probe_cal) {
  206. // Park nozzle
  207. do_blocking_move_to(parkpos);
  208. // Initialize temperatures
  209. const uint16_t target_bed = temp_comp.probe_calib_bed_temp;
  210. thermalManager.setTargetBed(target_bed);
  211. uint16_t target_probe = cali_info_init[TSI_PROBE].start_temp;
  212. report_targets(target_bed, target_probe);
  213. // Wait for heatbed to reach target temp and probe to cool below target temp
  214. wait_for_temps(target_bed, target_probe, next_temp_report);
  215. // Disable leveling so it won't mess with us
  216. TERN_(HAS_LEVELING, set_bed_leveling_enabled(false));
  217. bool timeout = false;
  218. for (;;) {
  219. // Move probe to probing point and wait for it to reach target temperature
  220. do_blocking_move_to(noz_pos_xyz);
  221. say_waiting_for_probe_heating();
  222. SERIAL_ECHOLNPAIR(" Bed:", target_bed, " Probe:", target_probe);
  223. const millis_t probe_timeout_ms = millis() + 900UL * 1000UL;
  224. while (thermalManager.degProbe() < target_probe) {
  225. if (report_temps(next_temp_report, probe_timeout_ms)) {
  226. SERIAL_ECHOLNPGM("!Probe heating timed out.");
  227. timeout = true;
  228. break;
  229. }
  230. }
  231. if (timeout) break;
  232. const float measured_z = g76_probe(TSI_PROBE, target_probe, noz_pos_xyz);
  233. if (isnan(measured_z) || target_probe > cali_info_init[TSI_PROBE].end_temp) break;
  234. }
  235. SERIAL_ECHOLNPAIR("Retrieved measurements: ", temp_comp.get_index());
  236. if (temp_comp.finish_calibration(TSI_PROBE))
  237. say_successfully_calibrated();
  238. else
  239. say_failed_to_calibrate();
  240. SERIAL_ECHOLNPGM(" probe.");
  241. // Cleanup
  242. thermalManager.setTargetBed(0);
  243. TERN_(HAS_LEVELING, set_bed_leveling_enabled(true));
  244. SERIAL_ECHOLNPGM("Final compensation values:");
  245. temp_comp.print_offsets();
  246. } // do_probe_cal
  247. restore_feedrate_and_scaling();
  248. }
  249. /**
  250. * M871: Report / reset temperature compensation offsets.
  251. * Note: This does not affect values in EEPROM until M500.
  252. *
  253. * M871 [ R | B | P | E ]
  254. *
  255. * No Parameters - Print current offset values.
  256. *
  257. * Select only one of these flags:
  258. * R - Reset all offsets to zero (i.e., disable compensation).
  259. * B - Manually set offset for bed
  260. * P - Manually set offset for probe
  261. * E - Manually set offset for extruder
  262. *
  263. * With B, P, or E:
  264. * I[index] - Index in the array
  265. * V[value] - Adjustment in µm
  266. */
  267. void GcodeSuite::M871() {
  268. if (parser.seen('R')) {
  269. // Reset z-probe offsets to factory defaults
  270. temp_comp.clear_all_offsets();
  271. SERIAL_ECHOLNPGM("Offsets reset to default.");
  272. }
  273. else if (parser.seen("BPE")) {
  274. if (!parser.seenval('V')) return;
  275. const int16_t offset_val = parser.value_int();
  276. if (!parser.seenval('I')) return;
  277. const int16_t idx = parser.value_int();
  278. const TempSensorID mod = (parser.seen('B') ? TSI_BED :
  279. #if ENABLED(USE_TEMP_EXT_COMPENSATION)
  280. parser.seen('E') ? TSI_EXT :
  281. #endif
  282. TSI_PROBE
  283. );
  284. if (idx > 0 && temp_comp.set_offset(mod, idx - 1, offset_val))
  285. SERIAL_ECHOLNPAIR("Set value: ", offset_val);
  286. else
  287. SERIAL_ECHOLNPGM("!Invalid index. Failed to set value (note: value at index 0 is constant).");
  288. }
  289. else // Print current Z-probe adjustments. Note: Values in EEPROM might differ.
  290. temp_comp.print_offsets();
  291. }
  292. /**
  293. * M192: Wait for probe temperature sensor to reach a target
  294. *
  295. * Select only one of these flags:
  296. * R - Wait for heating or cooling
  297. * S - Wait only for heating
  298. */
  299. void GcodeSuite::M192() {
  300. if (DEBUGGING(DRYRUN)) return;
  301. const bool no_wait_for_cooling = parser.seenval('S');
  302. if (!no_wait_for_cooling && ! parser.seenval('R')) {
  303. SERIAL_ERROR_MSG("No target temperature set.");
  304. return;
  305. }
  306. const float target_temp = parser.value_celsius();
  307. ui.set_status_P(thermalManager.isProbeBelowTemp(target_temp) ? GET_TEXT(MSG_PROBE_HEATING) : GET_TEXT(MSG_PROBE_COOLING));
  308. thermalManager.wait_for_probe(target_temp, no_wait_for_cooling);
  309. }
  310. #endif // PROBE_TEMP_COMPENSATION