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.

spindle_laser.cpp 5.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  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. /**
  23. * feature/spindle_laser.cpp
  24. */
  25. #include "../inc/MarlinConfig.h"
  26. #if HAS_CUTTER
  27. #include "spindle_laser.h"
  28. #if ENABLED(SPINDLE_SERVO)
  29. #include "../module/servo.h"
  30. #endif
  31. SpindleLaser cutter;
  32. uint8_t SpindleLaser::power;
  33. #if ENABLED(LASER_FEATURE)
  34. cutter_test_pulse_t SpindleLaser::testPulse = 50; // Test fire Pulse time ms value.
  35. #endif
  36. bool SpindleLaser::isReady; // Ready to apply power setting from the UI to OCR
  37. cutter_power_t SpindleLaser::menuPower, // Power set via LCD menu in PWM, PERCENT, or RPM
  38. SpindleLaser::unitPower; // LCD status power in PWM, PERCENT, or RPM
  39. #if ENABLED(MARLIN_DEV_MODE)
  40. cutter_frequency_t SpindleLaser::frequency; // PWM frequency setting; range: 2K - 50K
  41. #endif
  42. #define SPINDLE_LASER_PWM_OFF TERN(SPINDLE_LASER_PWM_INVERT, 255, 0)
  43. //
  44. // Init the cutter to a safe OFF state
  45. //
  46. void SpindleLaser::init() {
  47. #if ENABLED(SPINDLE_SERVO)
  48. MOVE_SERVO(SPINDLE_SERVO_NR, SPINDLE_SERVO_MIN);
  49. #else
  50. OUT_WRITE(SPINDLE_LASER_ENA_PIN, !SPINDLE_LASER_ACTIVE_STATE); // Init spindle to off
  51. #endif
  52. #if ENABLED(SPINDLE_CHANGE_DIR)
  53. OUT_WRITE(SPINDLE_DIR_PIN, SPINDLE_INVERT_DIR ? 255 : 0); // Init rotation to clockwise (M3)
  54. #endif
  55. #if ENABLED(SPINDLE_LASER_PWM)
  56. SET_PWM(SPINDLE_LASER_PWM_PIN);
  57. analogWrite(pin_t(SPINDLE_LASER_PWM_PIN), SPINDLE_LASER_PWM_OFF); // Set to lowest speed
  58. #endif
  59. #if ENABLED(HAL_CAN_SET_PWM_FREQ) && defined(SPINDLE_LASER_FREQUENCY)
  60. set_pwm_frequency(pin_t(SPINDLE_LASER_PWM_PIN), SPINDLE_LASER_FREQUENCY);
  61. TERN_(MARLIN_DEV_MODE, frequency = SPINDLE_LASER_FREQUENCY);
  62. #endif
  63. #if ENABLED(AIR_EVACUATION)
  64. OUT_WRITE(AIR_EVACUATION_PIN, !AIR_EVACUATION_ACTIVE); // Init Vacuum/Blower OFF
  65. #endif
  66. }
  67. #if ENABLED(SPINDLE_LASER_PWM)
  68. /**
  69. * Set the cutter PWM directly to the given ocr value
  70. */
  71. void SpindleLaser::_set_ocr(const uint8_t ocr) {
  72. #if NEEDS_HARDWARE_PWM && SPINDLE_LASER_FREQUENCY
  73. set_pwm_frequency(pin_t(SPINDLE_LASER_PWM_PIN), TERN(MARLIN_DEV_MODE, frequency, SPINDLE_LASER_FREQUENCY));
  74. set_pwm_duty(pin_t(SPINDLE_LASER_PWM_PIN), ocr ^ SPINDLE_LASER_PWM_OFF);
  75. #else
  76. analogWrite(pin_t(SPINDLE_LASER_PWM_PIN), ocr ^ SPINDLE_LASER_PWM_OFF);
  77. #endif
  78. }
  79. void SpindleLaser::set_ocr(const uint8_t ocr) {
  80. WRITE(SPINDLE_LASER_ENA_PIN, SPINDLE_LASER_ACTIVE_STATE); // Cutter ON
  81. _set_ocr(ocr);
  82. }
  83. void SpindleLaser::ocr_off() {
  84. WRITE(SPINDLE_LASER_ENA_PIN, !SPINDLE_LASER_ACTIVE_STATE); // Cutter OFF
  85. _set_ocr(0);
  86. }
  87. #endif
  88. //
  89. // Set cutter ON/OFF state (and PWM) to the given cutter power value
  90. //
  91. void SpindleLaser::apply_power(const uint8_t opwr) {
  92. static uint8_t last_power_applied = 0;
  93. if (opwr == last_power_applied) return;
  94. last_power_applied = opwr;
  95. power = opwr;
  96. #if ENABLED(SPINDLE_LASER_PWM)
  97. if (cutter.unitPower == 0 && CUTTER_UNIT_IS(RPM)) {
  98. ocr_off();
  99. isReady = false;
  100. }
  101. else if (ENABLED(CUTTER_POWER_RELATIVE) || enabled()) {
  102. set_ocr(power);
  103. isReady = true;
  104. }
  105. else {
  106. ocr_off();
  107. isReady = false;
  108. }
  109. #elif ENABLED(SPINDLE_SERVO)
  110. MOVE_SERVO(SPINDLE_SERVO_NR, power);
  111. #else
  112. WRITE(SPINDLE_LASER_ENA_PIN, enabled() ? SPINDLE_LASER_ACTIVE_STATE : !SPINDLE_LASER_ACTIVE_STATE);
  113. isReady = true;
  114. #endif
  115. }
  116. #if ENABLED(SPINDLE_CHANGE_DIR)
  117. //
  118. // Set the spindle direction and apply immediately
  119. // Stop on direction change if SPINDLE_STOP_ON_DIR_CHANGE is enabled
  120. //
  121. void SpindleLaser::set_reverse(const bool reverse) {
  122. const bool dir_state = (reverse == SPINDLE_INVERT_DIR); // Forward (M3) HIGH when not inverted
  123. if (TERN0(SPINDLE_STOP_ON_DIR_CHANGE, enabled()) && READ(SPINDLE_DIR_PIN) != dir_state) disable();
  124. WRITE(SPINDLE_DIR_PIN, dir_state);
  125. }
  126. #endif
  127. #if ENABLED(AIR_EVACUATION)
  128. // Enable / disable Cutter Vacuum or Laser Blower motor
  129. void SpindleLaser::air_evac_enable() { WRITE(AIR_EVACUATION_PIN, AIR_EVACUATION_ACTIVE); } // Turn ON
  130. void SpindleLaser::air_evac_disable() { WRITE(AIR_EVACUATION_PIN, !AIR_EVACUATION_ACTIVE); } // Turn OFF
  131. void SpindleLaser::air_evac_toggle() { TOGGLE(AIR_EVACUATION_PIN); } // Toggle state
  132. #endif
  133. #endif // HAS_CUTTER