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.

G35.cpp 5.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  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/MarlinConfig.h"
  23. #if ENABLED(ASSISTED_TRAMMING)
  24. #include "../gcode.h"
  25. #include "../../module/planner.h"
  26. #include "../../module/probe.h"
  27. #include "../../feature/bedlevel/bedlevel.h"
  28. #if HAS_MULTI_HOTEND
  29. #include "../../module/tool_change.h"
  30. #endif
  31. #define DEBUG_OUT ENABLED(DEBUG_LEVELING_FEATURE)
  32. #include "../../core/debug_out.h"
  33. constexpr xy_pos_t screws_tilt_adjust_pos[] = TRAMMING_POINT_XY;
  34. static PGMSTR(point_name_1, TRAMMING_POINT_NAME_1);
  35. static PGMSTR(point_name_2, TRAMMING_POINT_NAME_2);
  36. static PGMSTR(point_name_3, TRAMMING_POINT_NAME_3);
  37. #ifdef TRAMMING_POINT_NAME_4
  38. static PGMSTR(point_name_4, TRAMMING_POINT_NAME_4);
  39. #ifdef TRAMMING_POINT_NAME_5
  40. static PGMSTR(point_name_5, TRAMMING_POINT_NAME_5);
  41. #endif
  42. #endif
  43. static PGM_P const tramming_point_name[] PROGMEM = {
  44. point_name_1, point_name_2, point_name_3
  45. #ifdef TRAMMING_POINT_NAME_4
  46. , point_name_4
  47. #ifdef TRAMMING_POINT_NAME_5
  48. , point_name_5
  49. #endif
  50. #endif
  51. };
  52. #define G35_PROBE_COUNT COUNT(screws_tilt_adjust_pos)
  53. #if !WITHIN(TRAMMING_SCREW_THREAD, 30, 51) || TRAMMING_SCREW_THREAD % 10 > 1
  54. #error "TRAMMING_SCREW_THREAD must be equal to 30, 31, 40, 41, 50, or 51."
  55. #endif
  56. static_assert(G35_PROBE_COUNT > 2, "TRAMMING_POINT_XY requires at least 3 XY positions.");
  57. /**
  58. * G35: Read bed corners to help adjust bed screws
  59. *
  60. * S<screw_thread>
  61. *
  62. * Screw thread: 30 - Clockwise M3
  63. * 31 - Counter-Clockwise M3
  64. * 40 - Clockwise M4
  65. * 41 - Counter-Clockwise M4
  66. * 50 - Clockwise M5
  67. * 51 - Counter-Clockwise M5
  68. **/
  69. void GcodeSuite::G35() {
  70. DEBUG_SECTION(log_G35, "G35", DEBUGGING(LEVELING));
  71. if (DEBUGGING(LEVELING)) log_machine_info();
  72. float z_measured[G35_PROBE_COUNT] = { 0 };
  73. const uint8_t screw_thread = parser.byteval('S', TRAMMING_SCREW_THREAD);
  74. if (!WITHIN(screw_thread, 30, 51) || screw_thread % 10 > 1) {
  75. SERIAL_ECHOLNPGM("?(S)crew thread must be 30, 31, 40, 41, 50, or 51.");
  76. return;
  77. }
  78. // Wait for planner moves to finish!
  79. planner.synchronize();
  80. // Disable the leveling matrix before auto-aligning
  81. #if HAS_LEVELING
  82. TERN_(RESTORE_LEVELING_AFTER_G35, const bool leveling_was_active = planner.leveling_active);
  83. set_bed_leveling_enabled(false);
  84. #endif
  85. #if ENABLED(CNC_WORKSPACE_PLANES)
  86. workspace_plane = PLANE_XY;
  87. #endif
  88. // Always home with tool 0 active
  89. #if HAS_MULTI_HOTEND
  90. const uint8_t old_tool_index = active_extruder;
  91. tool_change(0, true);
  92. #endif
  93. #if HAS_DUPLICATION_MODE
  94. extruder_duplication_enabled = false;
  95. #endif
  96. // Home all before this procedure
  97. home_all_axes();
  98. bool err_break = false;
  99. // Probe all positions
  100. LOOP_L_N(i, G35_PROBE_COUNT) {
  101. // In BLTOUCH HS mode, the probe travels in a deployed state.
  102. // Users of G35 might have a badly misaligned bed, so raise Z by the
  103. // length of the deployed pin (BLTOUCH stroke < 7mm)
  104. current_position.z = (Z_CLEARANCE_BETWEEN_PROBES) + (7 * ENABLED(BLTOUCH_HS_MODE));
  105. const float z_probed_height = probe.probe_at_point(screws_tilt_adjust_pos[i], PROBE_PT_RAISE, 0, true);
  106. if (isnan(z_probed_height)) {
  107. SERIAL_ECHOPAIR("G35 failed at point ", int(i), " (", tramming_point_name[i], ")");
  108. SERIAL_ECHOLNPAIR_P(SP_X_STR, screws_tilt_adjust_pos[i].x, SP_Y_STR, screws_tilt_adjust_pos[i].y);
  109. err_break = true;
  110. break;
  111. }
  112. if (DEBUGGING(LEVELING)) {
  113. DEBUG_ECHOPAIR("Probing point ", int(i), " (", tramming_point_name[i], ")");
  114. SERIAL_ECHOLNPAIR_P(SP_X_STR, screws_tilt_adjust_pos[i].x, SP_Y_STR, screws_tilt_adjust_pos[i].y, SP_Z_STR, z_probed_height);
  115. }
  116. z_measured[i] = z_probed_height;
  117. }
  118. if (!err_break) {
  119. const float threads_factor[] = { 0.5, 0.7, 0.8 };
  120. // Calculate adjusts
  121. LOOP_S_L_N(i, 1, G35_PROBE_COUNT) {
  122. const float diff = z_measured[0] - z_measured[i],
  123. adjust = abs(diff) < 0.001f ? 0 : diff / threads_factor[(screw_thread - 30) / 10];
  124. const int full_turns = trunc(adjust);
  125. const float decimal_part = adjust - float(full_turns);
  126. const int minutes = trunc(decimal_part * 60.0f);
  127. SERIAL_ECHOPAIR("Turn ", tramming_point_name[i],
  128. " ", (screw_thread & 1) == (adjust > 0) ? "CCW" : "CW",
  129. " by ", abs(full_turns), " turns");
  130. if (minutes) SERIAL_ECHOPAIR(" and ", abs(minutes), " minutes");
  131. if (ENABLED(REPORT_TRAMMING_MM)) SERIAL_ECHOPAIR(" (", -diff, "mm)");
  132. SERIAL_EOL();
  133. }
  134. }
  135. else
  136. SERIAL_ECHOLNPGM("G35 aborted.");
  137. // Restore the active tool after homing
  138. #if HAS_MULTI_HOTEND
  139. tool_change(old_tool_index, DISABLED(PARKING_EXTRUDER)); // Fetch previous toolhead if not PARKING_EXTRUDER
  140. #endif
  141. #if BOTH(HAS_LEVELING, RESTORE_LEVELING_AFTER_G35)
  142. set_bed_leveling_enabled(leveling_was_active);
  143. #endif
  144. // Stow the probe, as the last call to probe.probe_at_point(...) left
  145. // the probe deployed if it was successful.
  146. probe.stow();
  147. // After this operation the Z position needs correction
  148. set_axis_never_homed(Z_AXIS);
  149. // Home Z after the alignment procedure
  150. process_subcommands_now_P(PSTR("G28Z"));
  151. }
  152. #endif // ASSISTED_TRAMMING