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.

M3-M5.cpp 5.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  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 HAS_CUTTER
  24. #include "../gcode.h"
  25. #include "../../feature/spindle_laser.h"
  26. #include "../../module/stepper.h"
  27. /**
  28. * Laser:
  29. * M3 - Laser ON/Power (Ramped power)
  30. * M4 - Laser ON/Power (Ramped power)
  31. * M5 - Set power output to 0 (leaving inline mode unchanged).
  32. *
  33. * M3I - Enable continuous inline power to be processed by the planner, with power
  34. * calculated and set in the planner blocks, processed inline during stepping.
  35. * Within inline mode M3 S-Values will set the power for the next moves e.g. G1 X10 Y10 powers on with the last S-Value.
  36. * M3I must be set before using planner-synced M3 inline S-Values (LASER_POWER_SYNC).
  37. *
  38. * M4I - Set dynamic mode which calculates laser power OCR based on the current feedrate.
  39. *
  40. * M5I - Clear inline mode and set power to 0.
  41. *
  42. * Spindle:
  43. * M3 - Spindle ON (Clockwise)
  44. * M4 - Spindle ON (Counter-clockwise)
  45. * M5 - Spindle OFF
  46. *
  47. * Parameters:
  48. * S<power> - Set power. S0 will turn the spindle/laser off.
  49. *
  50. * If no PWM pin is defined then M3/M4 just turns it on or off.
  51. *
  52. * At least 12.8kHz (50Hz * 256) is needed for Spindle PWM.
  53. * Hardware PWM is required on AVR. ISRs are too slow.
  54. *
  55. * NOTE: WGM for timers 3, 4, and 5 must be either Mode 1 or Mode 5.
  56. * No other settings give a PWM signal that goes from 0 to 5 volts.
  57. *
  58. * The system automatically sets WGM to Mode 1, so no special
  59. * initialization is needed.
  60. *
  61. * WGM bits for timer 2 are automatically set by the system to
  62. * Mode 1. This produces an acceptable 0 to 5 volt signal.
  63. * No special initialization is needed.
  64. *
  65. * NOTE: A minimum PWM frequency of 50 Hz is needed. All prescaler
  66. * factors for timers 2, 3, 4, and 5 are acceptable.
  67. *
  68. * SPINDLE_LASER_ENA_PIN needs an external pullup or it may power on
  69. * the spindle/laser during power-up or when connecting to the host
  70. * (usually goes through a reset which sets all I/O pins to tri-state)
  71. *
  72. * PWM duty cycle goes from 0 (off) to 255 (always on).
  73. */
  74. void GcodeSuite::M3_M4(const bool is_M4) {
  75. #if LASER_SAFETY_TIMEOUT_MS > 0
  76. reset_stepper_timeout(); // Reset timeout to allow subsequent G-code to power the laser (imm.)
  77. #endif
  78. if (cutter.cutter_mode == CUTTER_MODE_STANDARD)
  79. planner.synchronize(); // Wait for previous movement commands (G0/G1/G2/G3) to complete before changing power
  80. #if ENABLED(LASER_FEATURE)
  81. if (parser.seen_test('I')) {
  82. cutter.cutter_mode = is_M4 ? CUTTER_MODE_DYNAMIC : CUTTER_MODE_CONTINUOUS;
  83. cutter.inline_power(0);
  84. cutter.set_enabled(true);
  85. }
  86. #endif
  87. auto get_s_power = [] {
  88. float u;
  89. if (parser.seenval('S')) {
  90. const float v = parser.value_float();
  91. u = TERN(LASER_POWER_TRAP, v, cutter.power_to_range(v));
  92. }
  93. else if (cutter.cutter_mode == CUTTER_MODE_STANDARD)
  94. u = cutter.cpwr_to_upwr(SPEED_POWER_STARTUP);
  95. cutter.menuPower = cutter.unitPower = u;
  96. // PWM not implied, power converted to OCR from unit definition and on/off if not PWM.
  97. cutter.power = TERN(SPINDLE_LASER_USE_PWM, cutter.upower_to_ocr(u), u > 0 ? 255 : 0);
  98. return u;
  99. };
  100. if (cutter.cutter_mode == CUTTER_MODE_CONTINUOUS || cutter.cutter_mode == CUTTER_MODE_DYNAMIC) { // Laser power in inline mode
  101. #if ENABLED(LASER_FEATURE)
  102. planner.laser_inline.status.isPowered = true; // M3 or M4 is powered either way
  103. get_s_power(); // Update cutter.power if seen
  104. #if ENABLED(LASER_POWER_SYNC)
  105. // With power sync we only set power so it does not effect queued inline power sets
  106. planner.buffer_sync_block(BLOCK_BIT_LASER_PWR); // Send the flag, queueing inline power
  107. #else
  108. planner.synchronize();
  109. cutter.inline_power(cutter.power);
  110. #endif
  111. #endif
  112. }
  113. else {
  114. cutter.set_enabled(true);
  115. get_s_power();
  116. cutter.apply_power(
  117. #if ENABLED(SPINDLE_SERVO)
  118. cutter.unitPower
  119. #elif ENABLED(SPINDLE_LASER_USE_PWM)
  120. cutter.upower_to_ocr(cutter.unitPower)
  121. #else
  122. cutter.unitPower > 0 ? 255 : 0
  123. #endif
  124. );
  125. TERN_(SPINDLE_CHANGE_DIR, cutter.set_reverse(is_M4));
  126. }
  127. }
  128. /**
  129. * M5 - Cutter OFF (when moves are complete)
  130. */
  131. void GcodeSuite::M5() {
  132. planner.synchronize();
  133. cutter.power = 0;
  134. cutter.apply_power(0); // M5 just kills power, leaving inline mode unchanged
  135. if (cutter.cutter_mode != CUTTER_MODE_STANDARD) {
  136. if (parser.seen_test('I')) {
  137. TERN_(LASER_FEATURE, cutter.inline_power(cutter.power));
  138. cutter.set_enabled(false); // Needs to happen while we are in inline mode to clear inline power.
  139. cutter.cutter_mode = CUTTER_MODE_STANDARD; // Switch from inline to standard mode.
  140. }
  141. }
  142. cutter.set_enabled(false); // Disable enable output setting
  143. }
  144. #endif // HAS_CUTTER