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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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. auto get_s_power = [] {
  66. if (parser.seenval('S')) {
  67. const float spwr = parser.value_float();
  68. #if ENABLED(SPINDLE_SERVO)
  69. cutter.unitPower = spwr;
  70. #else
  71. cutter.unitPower = TERN(SPINDLE_LASER_PWM,
  72. cutter.power_to_range(cutter_power_t(round(spwr))),
  73. spwr > 0 ? 255 : 0);
  74. #endif
  75. }
  76. else
  77. cutter.unitPower = cutter.cpwr_to_upwr(SPEED_POWER_STARTUP);
  78. return cutter.unitPower;
  79. };
  80. #if ENABLED(LASER_POWER_INLINE)
  81. if (parser.seen('I') == DISABLED(LASER_POWER_INLINE_INVERT)) {
  82. // Laser power in inline mode
  83. cutter.inline_direction(is_M4); // Should always be unused
  84. #if ENABLED(SPINDLE_LASER_PWM)
  85. if (parser.seen('O')) {
  86. cutter.unitPower = cutter.power_to_range(parser.value_byte(), 0);
  87. cutter.inline_ocr_power(cutter.unitPower); // The OCR is a value from 0 to 255 (uint8_t)
  88. }
  89. else
  90. cutter.inline_power(cutter.upower_to_ocr(get_s_power()));
  91. #else
  92. cutter.set_inline_enabled(true);
  93. #endif
  94. return;
  95. }
  96. // Non-inline, standard case
  97. cutter.inline_disable(); // Prevent future blocks re-setting the power
  98. #endif
  99. planner.synchronize(); // Wait for previous movement commands (G0/G0/G2/G3) to complete before changing power
  100. cutter.set_reverse(is_M4);
  101. #if ENABLED(SPINDLE_LASER_PWM)
  102. if (parser.seenval('O')) {
  103. cutter.unitPower = cutter.power_to_range(parser.value_byte(), 0);
  104. cutter.ocr_set_power(cutter.unitPower); // The OCR is a value from 0 to 255 (uint8_t)
  105. }
  106. else
  107. cutter.set_power(cutter.upower_to_ocr(get_s_power()));
  108. #elif ENABLED(SPINDLE_SERVO)
  109. cutter.set_power(get_s_power());
  110. #else
  111. cutter.set_enabled(true);
  112. #endif
  113. cutter.menuPower = cutter.unitPower;
  114. }
  115. /**
  116. * M5 - Cutter OFF (when moves are complete)
  117. */
  118. void GcodeSuite::M5() {
  119. #if ENABLED(LASER_POWER_INLINE)
  120. if (parser.seen('I') == DISABLED(LASER_POWER_INLINE_INVERT)) {
  121. cutter.set_inline_enabled(false); // Laser power in inline mode
  122. return;
  123. }
  124. // Non-inline, standard case
  125. cutter.inline_disable(); // Prevent future blocks re-setting the power
  126. #endif
  127. planner.synchronize();
  128. cutter.set_enabled(false);
  129. cutter.menuPower = cutter.unitPower;
  130. }
  131. #endif // HAS_CUTTER