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.

M17_M18_M84.cpp 9.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  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 "../gcode.h"
  23. #include "../../MarlinCore.h" // for stepper_inactive_time, disable_e_steppers
  24. #include "../../lcd/marlinui.h"
  25. #include "../../module/stepper.h"
  26. #if ENABLED(AUTO_BED_LEVELING_UBL)
  27. #include "../../feature/bedlevel/bedlevel.h"
  28. #endif
  29. #define DEBUG_OUT ENABLED(MARLIN_DEV_MODE)
  30. #include "../../core/debug_out.h"
  31. #include "../../libs/hex_print.h"
  32. inline stepper_flags_t selected_axis_bits() {
  33. stepper_flags_t selected{0};
  34. #if HAS_EXTRUDERS
  35. if (parser.seen('E')) {
  36. if (E_TERN0(parser.has_value())) {
  37. const uint8_t e = parser.value_int();
  38. if (e < EXTRUDERS)
  39. selected.bits = _BV(INDEX_OF_AXIS(E_AXIS, e));
  40. }
  41. else
  42. selected.bits = selected.e_bits();
  43. }
  44. #endif
  45. selected.bits |= NUM_AXIS_GANG(
  46. (parser.seen_test('X') << X_AXIS),
  47. | (parser.seen_test('Y') << Y_AXIS),
  48. | (parser.seen_test('Z') << Z_AXIS),
  49. | (parser.seen_test(AXIS4_NAME) << I_AXIS),
  50. | (parser.seen_test(AXIS5_NAME) << J_AXIS),
  51. | (parser.seen_test(AXIS6_NAME) << K_AXIS),
  52. | (parser.seen_test(AXIS7_NAME) << U_AXIS),
  53. | (parser.seen_test(AXIS8_NAME) << V_AXIS),
  54. | (parser.seen_test(AXIS9_NAME) << W_AXIS)
  55. );
  56. return selected;
  57. }
  58. // Enable specified axes and warn about other affected axes
  59. void do_enable(const stepper_flags_t to_enable) {
  60. const ena_mask_t was_enabled = stepper.axis_enabled.bits,
  61. shall_enable = to_enable.bits & ~was_enabled;
  62. DEBUG_ECHOLNPGM("Now Enabled: ", hex_word(stepper.axis_enabled.bits), " Enabling: ", hex_word(to_enable.bits), " | ", shall_enable);
  63. if (!shall_enable) return; // All specified axes already enabled?
  64. ena_mask_t also_enabled = 0; // Track steppers enabled due to overlap
  65. // Enable all flagged axes
  66. LOOP_NUM_AXES(a) {
  67. if (TEST(shall_enable, a)) {
  68. stepper.enable_axis(AxisEnum(a)); // Mark and enable the requested axis
  69. DEBUG_ECHOLNPGM("Enabled ", axis_codes[a], " (", a, ") with overlap ", hex_word(enable_overlap[a]), " ... Enabled: ", hex_word(stepper.axis_enabled.bits));
  70. also_enabled |= enable_overlap[a];
  71. }
  72. }
  73. #if HAS_EXTRUDERS
  74. EXTRUDER_LOOP() {
  75. const uint8_t a = INDEX_OF_AXIS(E_AXIS, e);
  76. if (TEST(shall_enable, a)) {
  77. stepper.ENABLE_EXTRUDER(e);
  78. DEBUG_ECHOLNPGM("Enabled E", AS_DIGIT(e), " (", a, ") with overlap ", hex_word(enable_overlap[a]), " ... ", hex_word(stepper.axis_enabled.bits));
  79. also_enabled |= enable_overlap[a];
  80. }
  81. }
  82. #endif
  83. if ((also_enabled &= ~(shall_enable | was_enabled))) {
  84. SERIAL_CHAR('(');
  85. LOOP_NUM_AXES(a) if (TEST(also_enabled, a)) SERIAL_CHAR(AXIS_CHAR(a), ' ');
  86. #if HAS_EXTRUDERS
  87. #define _EN_ALSO(N) if (TEST(also_enabled, INDEX_OF_AXIS(E_AXIS, N))) SERIAL_CHAR('E', '0' + N, ' ');
  88. REPEAT(EXTRUDERS, _EN_ALSO)
  89. #endif
  90. SERIAL_ECHOLNPGM("also enabled)");
  91. }
  92. DEBUG_ECHOLNPGM("Enabled Now: ", hex_word(stepper.axis_enabled.bits));
  93. }
  94. /**
  95. * M17: Enable stepper motor power for one or more axes.
  96. * Print warnings for axes that share an ENABLE_PIN.
  97. *
  98. * Examples:
  99. *
  100. * M17 XZ ; Enable X and Z axes
  101. * M17 E ; Enable all E steppers
  102. * M17 E1 ; Enable just the E1 stepper
  103. */
  104. void GcodeSuite::M17() {
  105. if (parser.seen_axis()) {
  106. if (any_enable_overlap())
  107. do_enable(selected_axis_bits());
  108. else {
  109. #if HAS_EXTRUDERS
  110. if (parser.seen('E')) {
  111. if (parser.has_value()) {
  112. const uint8_t e = parser.value_int();
  113. if (e < EXTRUDERS) stepper.ENABLE_EXTRUDER(e);
  114. }
  115. else
  116. stepper.enable_e_steppers();
  117. }
  118. #endif
  119. NUM_AXIS_CODE(
  120. if (parser.seen_test('X')) stepper.enable_axis(X_AXIS),
  121. if (parser.seen_test('Y')) stepper.enable_axis(Y_AXIS),
  122. if (parser.seen_test('Z')) stepper.enable_axis(Z_AXIS),
  123. if (parser.seen_test(AXIS4_NAME)) stepper.enable_axis(I_AXIS),
  124. if (parser.seen_test(AXIS5_NAME)) stepper.enable_axis(J_AXIS),
  125. if (parser.seen_test(AXIS6_NAME)) stepper.enable_axis(K_AXIS),
  126. if (parser.seen_test(AXIS7_NAME)) stepper.enable_axis(U_AXIS),
  127. if (parser.seen_test(AXIS8_NAME)) stepper.enable_axis(V_AXIS),
  128. if (parser.seen_test(AXIS9_NAME)) stepper.enable_axis(W_AXIS)
  129. );
  130. }
  131. }
  132. else {
  133. LCD_MESSAGE(MSG_NO_MOVE);
  134. stepper.enable_all_steppers();
  135. }
  136. }
  137. void try_to_disable(const stepper_flags_t to_disable) {
  138. ena_mask_t still_enabled = to_disable.bits & stepper.axis_enabled.bits;
  139. DEBUG_ECHOLNPGM("Enabled: ", hex_word(stepper.axis_enabled.bits), " To Disable: ", hex_word(to_disable.bits), " | ", hex_word(still_enabled));
  140. if (!still_enabled) return;
  141. // Attempt to disable all flagged axes
  142. LOOP_NUM_AXES(a)
  143. if (TEST(to_disable.bits, a)) {
  144. DEBUG_ECHOPGM("Try to disable ", axis_codes[a], " (", a, ") with overlap ", hex_word(enable_overlap[a]), " ... ");
  145. if (stepper.disable_axis(AxisEnum(a))) { // Mark the requested axis and request to disable
  146. DEBUG_ECHOPGM("OK");
  147. still_enabled &= ~(_BV(a) | enable_overlap[a]); // If actually disabled, clear one or more tracked bits
  148. }
  149. else
  150. DEBUG_ECHOPGM("OVERLAP");
  151. DEBUG_ECHOLNPGM(" ... still_enabled=", hex_word(still_enabled));
  152. }
  153. #if HAS_EXTRUDERS
  154. EXTRUDER_LOOP() {
  155. const uint8_t a = INDEX_OF_AXIS(E_AXIS, e);
  156. if (TEST(to_disable.bits, a)) {
  157. DEBUG_ECHOPGM("Try to disable E", AS_DIGIT(e), " (", a, ") with overlap ", hex_word(enable_overlap[a]), " ... ");
  158. if (stepper.DISABLE_EXTRUDER(e)) {
  159. DEBUG_ECHOPGM("OK");
  160. still_enabled &= ~(_BV(a) | enable_overlap[a]);
  161. }
  162. else
  163. DEBUG_ECHOPGM("OVERLAP");
  164. DEBUG_ECHOLNPGM(" ... still_enabled=", hex_word(still_enabled));
  165. }
  166. }
  167. #endif
  168. auto overlap_warning = [](const ena_mask_t axis_bits) {
  169. SERIAL_ECHOPGM(" not disabled. Shared with");
  170. LOOP_NUM_AXES(a) if (TEST(axis_bits, a)) SERIAL_CHAR(' ', axis_codes[a]);
  171. #if HAS_EXTRUDERS
  172. #define _EN_STILLON(N) if (TEST(axis_bits, INDEX_OF_AXIS(E_AXIS, N))) SERIAL_CHAR(' ', 'E', '0' + N);
  173. REPEAT(EXTRUDERS, _EN_STILLON)
  174. #endif
  175. SERIAL_ECHOLNPGM(".");
  176. };
  177. // If any of the requested axes are still enabled, give a warning
  178. LOOP_NUM_AXES(a) {
  179. if (TEST(still_enabled, a)) {
  180. SERIAL_CHAR(axis_codes[a]);
  181. overlap_warning(stepper.axis_enabled.bits & enable_overlap[a]);
  182. }
  183. }
  184. #if HAS_EXTRUDERS
  185. EXTRUDER_LOOP() {
  186. const uint8_t a = INDEX_OF_AXIS(E_AXIS, e);
  187. if (TEST(still_enabled, a)) {
  188. SERIAL_CHAR('E', '0' + e);
  189. overlap_warning(stepper.axis_enabled.bits & enable_overlap[a]);
  190. }
  191. }
  192. #endif
  193. DEBUG_ECHOLNPGM("Enabled Now: ", hex_word(stepper.axis_enabled.bits));
  194. }
  195. /**
  196. * M18, M84: Disable stepper motor power for one or more axes.
  197. * Print warnings for axes that share an ENABLE_PIN.
  198. */
  199. void GcodeSuite::M18_M84() {
  200. if (parser.seenval('S')) {
  201. reset_stepper_timeout();
  202. stepper_inactive_time = parser.value_millis_from_seconds();
  203. }
  204. else {
  205. if (parser.seen_axis()) {
  206. planner.synchronize();
  207. if (any_enable_overlap())
  208. try_to_disable(selected_axis_bits());
  209. else {
  210. #if HAS_EXTRUDERS
  211. if (parser.seen('E')) {
  212. if (E_TERN0(parser.has_value()))
  213. stepper.DISABLE_EXTRUDER(parser.value_int());
  214. else
  215. stepper.disable_e_steppers();
  216. }
  217. #endif
  218. NUM_AXIS_CODE(
  219. if (parser.seen_test('X')) stepper.disable_axis(X_AXIS),
  220. if (parser.seen_test('Y')) stepper.disable_axis(Y_AXIS),
  221. if (parser.seen_test('Z')) stepper.disable_axis(Z_AXIS),
  222. if (parser.seen_test(AXIS4_NAME)) stepper.disable_axis(I_AXIS),
  223. if (parser.seen_test(AXIS5_NAME)) stepper.disable_axis(J_AXIS),
  224. if (parser.seen_test(AXIS6_NAME)) stepper.disable_axis(K_AXIS),
  225. if (parser.seen_test(AXIS7_NAME)) stepper.disable_axis(U_AXIS),
  226. if (parser.seen_test(AXIS8_NAME)) stepper.disable_axis(V_AXIS),
  227. if (parser.seen_test(AXIS9_NAME)) stepper.disable_axis(W_AXIS)
  228. );
  229. }
  230. }
  231. else
  232. planner.finish_and_disable();
  233. TERN_(AUTO_BED_LEVELING_UBL, ubl.steppers_were_disabled());
  234. }
  235. }