My Marlin configs for Fabrikator Mini and CTC i3 Pro B
Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  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. #if ENABLED(I2C_AMMETER)
  32. #include "../feature/ammeter.h"
  33. #endif
  34. SpindleLaser cutter;
  35. bool SpindleLaser::enable_state; // Virtual enable state, controls enable pin if present and or apply power if > 0
  36. uint8_t SpindleLaser::power, // Actual power output 0-255 ocr or "0 = off" > 0 = "on"
  37. SpindleLaser::last_power_applied; // = 0 // Basic power state tracking
  38. #if ENABLED(LASER_FEATURE)
  39. cutter_test_pulse_t SpindleLaser::testPulse = 50; // (ms) Test fire pulse default duration
  40. uint8_t SpindleLaser::last_block_power; // = 0 // Track power changes for dynamic inline power
  41. feedRate_t SpindleLaser::feedrate_mm_m = 1500,
  42. SpindleLaser::last_feedrate_mm_m; // = 0 // (mm/min) Track feedrate changes for dynamic power
  43. #endif
  44. bool SpindleLaser::isReadyForUI = false; // Ready to apply power setting from the UI to OCR
  45. CutterMode SpindleLaser::cutter_mode = CUTTER_MODE_STANDARD; // Default is standard mode
  46. constexpr cutter_cpower_t SpindleLaser::power_floor;
  47. cutter_power_t SpindleLaser::menuPower = 0, // Power value via LCD menu in PWM, PERCENT, or RPM based on configured format set by CUTTER_POWER_UNIT.
  48. SpindleLaser::unitPower = 0; // Unit power is in PWM, PERCENT, or RPM based on CUTTER_POWER_UNIT.
  49. cutter_frequency_t SpindleLaser::frequency; // PWM frequency setting; range: 2K - 50K
  50. #define SPINDLE_LASER_PWM_OFF TERN(SPINDLE_LASER_PWM_INVERT, 255, 0)
  51. /**
  52. * Init the cutter to a safe OFF state
  53. */
  54. void SpindleLaser::init() {
  55. #if ENABLED(SPINDLE_SERVO)
  56. servo[SPINDLE_SERVO_NR].move(SPINDLE_SERVO_MIN);
  57. #else
  58. OUT_WRITE(SPINDLE_LASER_ENA_PIN, !SPINDLE_LASER_ACTIVE_STATE); // Init spindle to off
  59. #endif
  60. #if ENABLED(SPINDLE_CHANGE_DIR)
  61. OUT_WRITE(SPINDLE_DIR_PIN, SPINDLE_INVERT_DIR); // Init rotation to clockwise (M3)
  62. #endif
  63. #if ENABLED(HAL_CAN_SET_PWM_FREQ) && SPINDLE_LASER_FREQUENCY
  64. frequency = SPINDLE_LASER_FREQUENCY;
  65. hal.set_pwm_frequency(pin_t(SPINDLE_LASER_PWM_PIN), SPINDLE_LASER_FREQUENCY);
  66. #endif
  67. #if ENABLED(SPINDLE_LASER_USE_PWM)
  68. SET_PWM(SPINDLE_LASER_PWM_PIN);
  69. hal.set_pwm_duty(pin_t(SPINDLE_LASER_PWM_PIN), SPINDLE_LASER_PWM_OFF); // Set to lowest speed
  70. #endif
  71. #if ENABLED(AIR_EVACUATION)
  72. OUT_WRITE(AIR_EVACUATION_PIN, !AIR_EVACUATION_ACTIVE); // Init Vacuum/Blower OFF
  73. #endif
  74. #if ENABLED(AIR_ASSIST)
  75. OUT_WRITE(AIR_ASSIST_PIN, !AIR_ASSIST_ACTIVE); // Init Air Assist OFF
  76. #endif
  77. TERN_(I2C_AMMETER, ammeter.init()); // Init I2C Ammeter
  78. }
  79. #if ENABLED(SPINDLE_LASER_USE_PWM)
  80. /**
  81. * Set the cutter PWM directly to the given ocr value
  82. *
  83. * @param ocr Power value
  84. */
  85. void SpindleLaser::_set_ocr(const uint8_t ocr) {
  86. #if ENABLED(HAL_CAN_SET_PWM_FREQ) && SPINDLE_LASER_FREQUENCY
  87. hal.set_pwm_frequency(pin_t(SPINDLE_LASER_PWM_PIN), frequency);
  88. #endif
  89. hal.set_pwm_duty(pin_t(SPINDLE_LASER_PWM_PIN), ocr ^ SPINDLE_LASER_PWM_OFF);
  90. }
  91. void SpindleLaser::set_ocr(const uint8_t ocr) {
  92. WRITE(SPINDLE_LASER_ENA_PIN, SPINDLE_LASER_ACTIVE_STATE); // Cutter ON
  93. _set_ocr(ocr);
  94. }
  95. void SpindleLaser::ocr_off() {
  96. WRITE(SPINDLE_LASER_ENA_PIN, !SPINDLE_LASER_ACTIVE_STATE); // Cutter OFF
  97. _set_ocr(0);
  98. }
  99. #endif // SPINDLE_LASER_USE_PWM
  100. /**
  101. * Apply power for Laser or Spindle
  102. *
  103. * Apply cutter power value for PWM, Servo, and on/off pin.
  104. *
  105. * @param opwr Power value. Range 0 to MAX.
  106. */
  107. void SpindleLaser::apply_power(const uint8_t opwr) {
  108. if (enabled() || opwr == 0) { // 0 check allows us to disable where no ENA pin exists
  109. // Test and set the last power used to improve performance
  110. if (opwr == last_power_applied) return;
  111. last_power_applied = opwr;
  112. // Handle PWM driven or just simple on/off
  113. #if ENABLED(SPINDLE_LASER_USE_PWM)
  114. if (CUTTER_UNIT_IS(RPM) && unitPower == 0)
  115. ocr_off();
  116. else if (ENABLED(CUTTER_POWER_RELATIVE) || enabled() || opwr == 0) {
  117. set_ocr(opwr);
  118. isReadyForUI = true;
  119. }
  120. else
  121. ocr_off();
  122. #elif ENABLED(SPINDLE_SERVO)
  123. MOVE_SERVO(SPINDLE_SERVO_NR, power);
  124. #else
  125. WRITE(SPINDLE_LASER_ENA_PIN, enabled() ? SPINDLE_LASER_ACTIVE_STATE : !SPINDLE_LASER_ACTIVE_STATE);
  126. isReadyForUI = true;
  127. #endif
  128. }
  129. else {
  130. #if PIN_EXISTS(SPINDLE_LASER_ENA)
  131. WRITE(SPINDLE_LASER_ENA_PIN, !SPINDLE_LASER_ACTIVE_STATE);
  132. #endif
  133. isReadyForUI = false; // Only used for UI display updates.
  134. TERN_(SPINDLE_LASER_USE_PWM, ocr_off());
  135. }
  136. }
  137. #if ENABLED(SPINDLE_CHANGE_DIR)
  138. /**
  139. * Set the spindle direction and apply immediately
  140. * Stop on direction change if SPINDLE_STOP_ON_DIR_CHANGE is enabled
  141. */
  142. void SpindleLaser::set_reverse(const bool reverse) {
  143. const bool dir_state = (reverse == SPINDLE_INVERT_DIR); // Forward (M3) HIGH when not inverted
  144. if (TERN0(SPINDLE_STOP_ON_DIR_CHANGE, enabled()) && READ(SPINDLE_DIR_PIN) != dir_state) disable();
  145. WRITE(SPINDLE_DIR_PIN, dir_state);
  146. }
  147. #endif
  148. #if ENABLED(AIR_EVACUATION)
  149. // Enable / disable Cutter Vacuum or Laser Blower motor
  150. void SpindleLaser::air_evac_enable() { WRITE(AIR_EVACUATION_PIN, AIR_EVACUATION_ACTIVE); } // Turn ON
  151. void SpindleLaser::air_evac_disable() { WRITE(AIR_EVACUATION_PIN, !AIR_EVACUATION_ACTIVE); } // Turn OFF
  152. void SpindleLaser::air_evac_toggle() { TOGGLE(AIR_EVACUATION_PIN); } // Toggle state
  153. #endif
  154. #if ENABLED(AIR_ASSIST)
  155. // Enable / disable air assist
  156. void SpindleLaser::air_assist_enable() { WRITE(AIR_ASSIST_PIN, AIR_ASSIST_ACTIVE); } // Turn ON
  157. void SpindleLaser::air_assist_disable() { WRITE(AIR_ASSIST_PIN, !AIR_ASSIST_ACTIVE); } // Turn OFF
  158. void SpindleLaser::air_assist_toggle() { TOGGLE(AIR_ASSIST_PIN); } // Toggle state
  159. #endif
  160. #endif // HAS_CUTTER