My Marlin configs for Fabrikator Mini and CTC i3 Pro B
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

spindle_laser.cpp 3.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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 <http://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. cutter_power_t SpindleLaser::power;
  30. bool SpindleLaser::isOn; // state to determine when to apply setPower to power
  31. cutter_setPower_t SpindleLaser::setPower = interpret_power(SPEED_POWER_MIN); // spindle/laser speed/power control in PWM, Percentage or RPM
  32. #if ENABLED(MARLIN_DEV_MODE)
  33. cutter_frequency_t SpindleLaser::frequency; // setting PWM frequency; range: 2K - 50K
  34. #endif
  35. #define SPINDLE_LASER_PWM_OFF ((SPINDLE_LASER_PWM_INVERT) ? 255 : 0)
  36. //
  37. // Init the cutter to a safe OFF state
  38. //
  39. void SpindleLaser::init() {
  40. OUT_WRITE(SPINDLE_LASER_ENA_PIN, !SPINDLE_LASER_ACTIVE_HIGH); // Init spindle to off
  41. #if ENABLED(SPINDLE_CHANGE_DIR)
  42. OUT_WRITE(SPINDLE_DIR_PIN, SPINDLE_INVERT_DIR ? 255 : 0); // Init rotation to clockwise (M3)
  43. #endif
  44. #if ENABLED(SPINDLE_LASER_PWM)
  45. SET_PWM(SPINDLE_LASER_PWM_PIN);
  46. analogWrite(pin_t(SPINDLE_LASER_PWM_PIN), SPINDLE_LASER_PWM_OFF); // set to lowest speed
  47. #endif
  48. #if ENABLED(HAL_CAN_SET_PWM_FREQ) && defined(SPINDLE_LASER_FREQUENCY)
  49. set_pwm_frequency(pin_t(SPINDLE_LASER_PWM_PIN), SPINDLE_LASER_FREQUENCY);
  50. TERN_(MARLIN_DEV_MODE, frequency = SPINDLE_LASER_FREQUENCY);
  51. #endif
  52. }
  53. #if ENABLED(SPINDLE_LASER_PWM)
  54. /**
  55. * Set the cutter PWM directly to the given ocr value
  56. **/
  57. void SpindleLaser::set_ocr(const uint8_t ocr) {
  58. WRITE(SPINDLE_LASER_ENA_PIN, SPINDLE_LASER_ACTIVE_HIGH); // turn spindle on
  59. analogWrite(pin_t(SPINDLE_LASER_PWM_PIN), ocr ^ SPINDLE_LASER_PWM_OFF);
  60. }
  61. #endif
  62. //
  63. // Set cutter ON state (and PWM) to the given cutter power value
  64. //
  65. void SpindleLaser::apply_power(const cutter_power_t inpow) {
  66. static cutter_power_t last_power_applied = 0;
  67. if (inpow == last_power_applied) return;
  68. last_power_applied = inpow;
  69. #if ENABLED(SPINDLE_LASER_PWM)
  70. if (enabled())
  71. set_ocr(translate_power(inpow));
  72. else {
  73. WRITE(SPINDLE_LASER_ENA_PIN, !SPINDLE_LASER_ACTIVE_HIGH); // Turn spindle off
  74. analogWrite(pin_t(SPINDLE_LASER_PWM_PIN), SPINDLE_LASER_PWM_OFF); // Only write low byte
  75. }
  76. #else
  77. WRITE(SPINDLE_LASER_ENA_PIN, (SPINDLE_LASER_ACTIVE_HIGH) ? enabled() : !enabled());
  78. #endif
  79. }
  80. #if ENABLED(SPINDLE_CHANGE_DIR)
  81. //
  82. // Set the spindle direction and apply immediately
  83. // Stop on direction change if SPINDLE_STOP_ON_DIR_CHANGE is enabled
  84. //
  85. void SpindleLaser::set_direction(const bool reverse) {
  86. const bool dir_state = (reverse == SPINDLE_INVERT_DIR); // Forward (M3) HIGH when not inverted
  87. if (TERN0(SPINDLE_STOP_ON_DIR_CHANGE, enabled()) && READ(SPINDLE_DIR_PIN) != dir_state) disable();
  88. WRITE(SPINDLE_DIR_PIN, dir_state);
  89. }
  90. #endif
  91. #endif // HAS_CUTTER