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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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. bool SpindleLaser::isReady; // Ready to apply power setting from the UI to OCR
  34. cutter_power_t SpindleLaser::menuPower, // Power set via LCD menu in PWM, PERCENT, or RPM
  35. SpindleLaser::unitPower; // LCD status power in PWM, PERCENT, or RPM
  36. #if ENABLED(MARLIN_DEV_MODE)
  37. cutter_frequency_t SpindleLaser::frequency; // PWM frequency setting; range: 2K - 50K
  38. #endif
  39. #define SPINDLE_LASER_PWM_OFF ((SPINDLE_LASER_PWM_INVERT) ? 255 : 0)
  40. //
  41. // Init the cutter to a safe OFF state
  42. //
  43. void SpindleLaser::init() {
  44. #if ENABLED(SPINDLE_SERVO)
  45. MOVE_SERVO(SPINDLE_SERVO_NR, SPINDLE_SERVO_MIN);
  46. #else
  47. OUT_WRITE(SPINDLE_LASER_ENA_PIN, !SPINDLE_LASER_ACTIVE_STATE); // Init spindle to off
  48. #endif
  49. #if ENABLED(SPINDLE_CHANGE_DIR)
  50. OUT_WRITE(SPINDLE_DIR_PIN, SPINDLE_INVERT_DIR ? 255 : 0); // Init rotation to clockwise (M3)
  51. #endif
  52. #if ENABLED(SPINDLE_LASER_PWM)
  53. SET_PWM(SPINDLE_LASER_PWM_PIN);
  54. analogWrite(pin_t(SPINDLE_LASER_PWM_PIN), SPINDLE_LASER_PWM_OFF); // Set to lowest speed
  55. #endif
  56. #if ENABLED(HAL_CAN_SET_PWM_FREQ) && defined(SPINDLE_LASER_FREQUENCY)
  57. set_pwm_frequency(pin_t(SPINDLE_LASER_PWM_PIN), SPINDLE_LASER_FREQUENCY);
  58. TERN_(MARLIN_DEV_MODE, frequency = SPINDLE_LASER_FREQUENCY);
  59. #endif
  60. }
  61. #if ENABLED(SPINDLE_LASER_PWM)
  62. /**
  63. * Set the cutter PWM directly to the given ocr value
  64. */
  65. void SpindleLaser::set_ocr(const uint8_t ocr) {
  66. WRITE(SPINDLE_LASER_ENA_PIN, SPINDLE_LASER_ACTIVE_STATE); // Turn spindle on
  67. analogWrite(pin_t(SPINDLE_LASER_PWM_PIN), ocr ^ SPINDLE_LASER_PWM_OFF);
  68. #if NEEDS_HARDWARE_PWM && SPINDLE_LASER_FREQUENCY
  69. set_pwm_duty(pin_t(SPINDLE_LASER_PWM_PIN), ocr ^ SPINDLE_LASER_PWM_OFF);
  70. #endif
  71. }
  72. void SpindleLaser::ocr_off() {
  73. WRITE(SPINDLE_LASER_ENA_PIN, !SPINDLE_LASER_ACTIVE_STATE); // Turn spindle off
  74. analogWrite(pin_t(SPINDLE_LASER_PWM_PIN), SPINDLE_LASER_PWM_OFF); // Only write low byte
  75. }
  76. #endif
  77. //
  78. // Set cutter ON/OFF state (and PWM) to the given cutter power value
  79. //
  80. void SpindleLaser::apply_power(const uint8_t opwr) {
  81. static uint8_t last_power_applied = 0;
  82. if (opwr == last_power_applied) return;
  83. last_power_applied = opwr;
  84. power = opwr;
  85. #if ENABLED(SPINDLE_LASER_PWM)
  86. if (cutter.unitPower == 0 && CUTTER_UNIT_IS(RPM)) {
  87. ocr_off();
  88. isReady = false;
  89. }
  90. else if (enabled() || ENABLED(CUTTER_POWER_RELATIVE)) {
  91. set_ocr(power);
  92. isReady = true;
  93. }
  94. else {
  95. ocr_off();
  96. isReady = false;
  97. }
  98. #elif ENABLED(SPINDLE_SERVO)
  99. MOVE_SERVO(SPINDLE_SERVO_NR, power);
  100. #else
  101. WRITE(SPINDLE_LASER_ENA_PIN, enabled() ? SPINDLE_LASER_ACTIVE_STATE : !SPINDLE_LASER_ACTIVE_STATE);
  102. isReady = true;
  103. #endif
  104. }
  105. #if ENABLED(SPINDLE_CHANGE_DIR)
  106. //
  107. // Set the spindle direction and apply immediately
  108. // Stop on direction change if SPINDLE_STOP_ON_DIR_CHANGE is enabled
  109. //
  110. void SpindleLaser::set_reverse(const bool reverse) {
  111. const bool dir_state = (reverse == SPINDLE_INVERT_DIR); // Forward (M3) HIGH when not inverted
  112. if (TERN0(SPINDLE_STOP_ON_DIR_CHANGE, enabled()) && READ(SPINDLE_DIR_PIN) != dir_state) disable();
  113. WRITE(SPINDLE_DIR_PIN, dir_state);
  114. }
  115. #endif
  116. #endif // HAS_CUTTER