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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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; // = 0
  30. #define SPINDLE_LASER_PWM_OFF ((SPINDLE_LASER_PWM_INVERT) ? 255 : 0)
  31. void SpindleLaser::init() {
  32. OUT_WRITE(SPINDLE_LASER_ENA_PIN, !SPINDLE_LASER_ACTIVE_HIGH); // Init spindle to off
  33. #if ENABLED(SPINDLE_CHANGE_DIR)
  34. OUT_WRITE(SPINDLE_DIR_PIN, SPINDLE_INVERT_DIR ? 255 : 0); // Init rotation to clockwise (M3)
  35. #endif
  36. #if ENABLED(SPINDLE_LASER_PWM)
  37. SET_PWM(SPINDLE_LASER_PWM_PIN);
  38. analogWrite(pin_t(SPINDLE_LASER_PWM_PIN), SPINDLE_LASER_PWM_OFF); // set to lowest speed
  39. #endif
  40. }
  41. #if ENABLED(SPINDLE_LASER_PWM)
  42. /**
  43. * ocr_val_mode() is used for debugging and to get the points needed to compute the RPM vs ocr_val line
  44. *
  45. * it accepts inputs of 0-255
  46. */
  47. void SpindleLaser::set_ocr(const uint8_t ocr) {
  48. WRITE(SPINDLE_LASER_ENA_PIN, SPINDLE_LASER_ACTIVE_HIGH); // turn spindle on (active low)
  49. analogWrite(pin_t(SPINDLE_LASER_PWM_PIN), ocr ^ SPINDLE_LASER_PWM_OFF);
  50. }
  51. #endif
  52. void SpindleLaser::apply_power(const cutter_power_t inpow) {
  53. static cutter_power_t last_power_applied = 0;
  54. if (inpow == last_power_applied) return;
  55. last_power_applied = inpow;
  56. #if ENABLED(SPINDLE_LASER_PWM)
  57. if (enabled()) {
  58. #define _scaled(F) ((F - (SPEED_POWER_INTERCEPT)) * inv_slope)
  59. constexpr float inv_slope = RECIPROCAL(SPEED_POWER_SLOPE),
  60. min_ocr = _scaled(SPEED_POWER_MIN),
  61. max_ocr = _scaled(SPEED_POWER_MAX);
  62. int16_t ocr_val;
  63. if (inpow <= SPEED_POWER_MIN) ocr_val = min_ocr; // Use minimum if set below
  64. else if (inpow >= SPEED_POWER_MAX) ocr_val = max_ocr; // Use maximum if set above
  65. else ocr_val = _scaled(inpow); // Use calculated OCR value
  66. set_ocr(ocr_val & 0xFF); // ...limited to Atmel PWM max
  67. }
  68. else {
  69. WRITE(SPINDLE_LASER_ENA_PIN, !SPINDLE_LASER_ACTIVE_HIGH); // Turn spindle off (active low)
  70. analogWrite(pin_t(SPINDLE_LASER_PWM_PIN), SPINDLE_LASER_PWM_OFF); // Only write low byte
  71. }
  72. #else
  73. WRITE(SPINDLE_LASER_ENA_PIN, (SPINDLE_LASER_ACTIVE_HIGH) ? enabled() : !enabled());
  74. #endif
  75. }
  76. #if ENABLED(SPINDLE_CHANGE_DIR)
  77. void SpindleLaser::set_direction(const bool reverse) {
  78. const bool dir_state = (reverse == SPINDLE_INVERT_DIR); // Forward (M3) HIGH when not inverted
  79. #if ENABLED(SPINDLE_STOP_ON_DIR_CHANGE)
  80. if (enabled() && READ(SPINDLE_DIR_PIN) != dir_state) disable();
  81. #endif
  82. WRITE(SPINDLE_DIR_PIN, dir_state);
  83. }
  84. #endif
  85. #endif // HAS_CUTTER