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

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