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 4.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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 (Continuous power)
  31. *
  32. * Spindle:
  33. * M3 - Spindle ON (Clockwise)
  34. * M4 - Spindle ON (Counter-clockwise)
  35. *
  36. * Parameters:
  37. * S<power> - Set power. S0 will turn the spindle/laser off, except in relative mode.
  38. * O<ocr> - Set power and OCR (oscillator count register)
  39. *
  40. * If no PWM pin is defined then M3/M4 just turns it on.
  41. *
  42. * At least 12.8kHz (50Hz * 256) is needed for Spindle PWM.
  43. * Hardware PWM is required on AVR. ISRs are too slow.
  44. *
  45. * NOTE: WGM for timers 3, 4, and 5 must be either Mode 1 or Mode 5.
  46. * No other settings give a PWM signal that goes from 0 to 5 volts.
  47. *
  48. * The system automatically sets WGM to Mode 1, so no special
  49. * initialization is needed.
  50. *
  51. * WGM bits for timer 2 are automatically set by the system to
  52. * Mode 1. This produces an acceptable 0 to 5 volt signal.
  53. * No special initialization is needed.
  54. *
  55. * NOTE: A minimum PWM frequency of 50 Hz is needed. All prescaler
  56. * factors for timers 2, 3, 4, and 5 are acceptable.
  57. *
  58. * SPINDLE_LASER_ENA_PIN needs an external pullup or it may power on
  59. * the spindle/laser during power-up or when connecting to the host
  60. * (usually goes through a reset which sets all I/O pins to tri-state)
  61. *
  62. * PWM duty cycle goes from 0 (off) to 255 (always on).
  63. */
  64. void GcodeSuite::M3_M4(const bool is_M4) {
  65. #if EITHER(SPINDLE_LASER_USE_PWM, SPINDLE_SERVO)
  66. auto get_s_power = [] {
  67. if (parser.seenval('S')) {
  68. const float spwr = parser.value_float();
  69. #if ENABLED(SPINDLE_SERVO)
  70. cutter.unitPower = spwr;
  71. #else
  72. cutter.unitPower = TERN(SPINDLE_LASER_USE_PWM,
  73. cutter.power_to_range(cutter_power_t(round(spwr))),
  74. spwr > 0 ? 255 : 0);
  75. #endif
  76. }
  77. else
  78. cutter.unitPower = cutter.cpwr_to_upwr(SPEED_POWER_STARTUP);
  79. return cutter.unitPower;
  80. };
  81. #endif
  82. #if ENABLED(LASER_POWER_INLINE)
  83. if (parser.seen('I') == DISABLED(LASER_POWER_INLINE_INVERT)) {
  84. // Laser power in inline mode
  85. cutter.inline_direction(is_M4); // Should always be unused
  86. #if ENABLED(SPINDLE_LASER_USE_PWM)
  87. if (parser.seenval('O')) {
  88. cutter.unitPower = cutter.power_to_range(parser.value_byte(), 0);
  89. cutter.inline_ocr_power(cutter.unitPower); // The OCR is a value from 0 to 255 (uint8_t)
  90. }
  91. else
  92. cutter.inline_power(cutter.upower_to_ocr(get_s_power()));
  93. #else
  94. cutter.set_inline_enabled(true);
  95. #endif
  96. return;
  97. }
  98. // Non-inline, standard case
  99. cutter.inline_disable(); // Prevent future blocks re-setting the power
  100. #endif
  101. planner.synchronize(); // Wait for previous movement commands (G0/G0/G2/G3) to complete before changing power
  102. cutter.set_reverse(is_M4);
  103. #if ENABLED(SPINDLE_LASER_USE_PWM)
  104. if (parser.seenval('O')) {
  105. cutter.unitPower = cutter.power_to_range(parser.value_byte(), 0);
  106. cutter.ocr_set_power(cutter.unitPower); // The OCR is a value from 0 to 255 (uint8_t)
  107. }
  108. else
  109. cutter.set_power(cutter.upower_to_ocr(get_s_power()));
  110. #elif ENABLED(SPINDLE_SERVO)
  111. cutter.set_power(get_s_power());
  112. #else
  113. cutter.set_enabled(true);
  114. #endif
  115. cutter.menuPower = cutter.unitPower;
  116. }
  117. /**
  118. * M5 - Cutter OFF (when moves are complete)
  119. */
  120. void GcodeSuite::M5() {
  121. #if ENABLED(LASER_POWER_INLINE)
  122. if (parser.seen('I') == DISABLED(LASER_POWER_INLINE_INVERT)) {
  123. cutter.set_inline_enabled(false); // Laser power in inline mode
  124. return;
  125. }
  126. // Non-inline, standard case
  127. cutter.inline_disable(); // Prevent future blocks re-setting the power
  128. #endif
  129. planner.synchronize();
  130. cutter.set_enabled(false);
  131. cutter.menuPower = cutter.unitPower;
  132. }
  133. #endif // HAS_CUTTER