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

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