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

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