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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  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. #pragma once
  23. /**
  24. * feature/spindle_laser.h
  25. * Support for Laser Power or Spindle Power & Direction
  26. */
  27. #include "../inc/MarlinConfig.h"
  28. #include "spindle_laser_types.h"
  29. #if ENABLED(LASER_POWER_INLINE)
  30. #include "../module/planner.h"
  31. #endif
  32. class SpindleLaser {
  33. public:
  34. static bool isOn; // state to determine when to apply setPower to power
  35. static cutter_power_t power;
  36. static cutter_setPower_t setPower; // spindle/laser menu set power; in PWM, Percentage or RPM
  37. #if ENABLED(MARLIN_DEV_MODE)
  38. static cutter_frequency_t frequency; // set PWM frequency; range: 2K-50K
  39. #endif
  40. static cutter_setPower_t interpret_power(const float pwr) { // convert speed/power to configured PWM, Percentage or RPM in relative or normal range
  41. #if CUTTER_DISPLAY_IS(PERCENT)
  42. return (pwr / SPEED_POWER_MAX) * 100; // to percent
  43. #elif CUTTER_DISPLAY_IS(RPM) // to RPM is unaltered
  44. return pwr;
  45. #else // to PWM
  46. #if ENABLED(CUTTER_POWER_RELATIVE)
  47. return (pwr - SPEED_POWER_MIN) / (SPEED_POWER_MAX - SPEED_POWER_MIN) * 255; // using rpm range as relative percentage
  48. #else
  49. return (pwr / SPEED_POWER_MAX) * 255;
  50. #endif
  51. #endif
  52. }
  53. /**
  54. * Translate speed/power --> percentage --> PWM value
  55. **/
  56. static cutter_power_t translate_power(const float pwr) {
  57. float pwrpc;
  58. #if CUTTER_DISPLAY_IS(PERCENT)
  59. pwrpc = pwr;
  60. #elif CUTTER_DISPLAY_IS(RPM) // RPM to percent
  61. #if ENABLED(CUTTER_POWER_RELATIVE)
  62. pwrpc = (pwr - SPEED_POWER_MIN) / (SPEED_POWER_MAX - SPEED_POWER_MIN) * 100;
  63. #else
  64. pwrpc = pwr / SPEED_POWER_MAX * 100;
  65. #endif
  66. #else
  67. return pwr; // PWM
  68. #endif
  69. #if ENABLED(SPINDLE_FEATURE)
  70. #if ENABLED(CUTTER_POWER_RELATIVE)
  71. constexpr float spmin = 0;
  72. #else
  73. constexpr float spmin = SPEED_POWER_MIN / SPEED_POWER_MAX * 100; // convert to percentage
  74. #endif
  75. constexpr float spmax = 100;
  76. #else
  77. constexpr float spmin = SPEED_POWER_MIN;
  78. constexpr float spmax = SPEED_POWER_MAX;
  79. #endif
  80. constexpr float inv_slope = RECIPROCAL(SPEED_POWER_SLOPE),
  81. min_ocr = (spmin - (SPEED_POWER_INTERCEPT)) * inv_slope, // Minimum allowed
  82. max_ocr = (spmax - (SPEED_POWER_INTERCEPT)) * inv_slope; // Maximum allowed
  83. float ocr_val;
  84. if (pwrpc < spmin) ocr_val = min_ocr; // Use minimum if set below
  85. else if (pwrpc > spmax) ocr_val = max_ocr; // Use maximum if set above
  86. else ocr_val = (pwrpc - (SPEED_POWER_INTERCEPT)) * inv_slope; // Use calculated OCR value
  87. return ocr_val; // ...limited to Atmel PWM max
  88. }
  89. static void init();
  90. // Modifying this function should update everywhere
  91. static inline bool enabled(const cutter_power_t pwr) { return pwr > 0; }
  92. static inline bool enabled() { return enabled(power); }
  93. #if ENABLED(MARLIN_DEV_MODE)
  94. static inline void refresh_frequency() { set_pwm_frequency(pin_t(SPINDLE_LASER_PWM_PIN), frequency); }
  95. #endif
  96. static void apply_power(const cutter_power_t inpow);
  97. FORCE_INLINE static void refresh() { apply_power(power); }
  98. FORCE_INLINE static void set_power(const cutter_power_t pwr) { power = pwr; refresh(); }
  99. static inline void set_enabled(const bool enable) { set_power(enable ? (power ?: interpret_power(SPEED_POWER_STARTUP)) : 0); }
  100. #if ENABLED(SPINDLE_LASER_PWM)
  101. static void set_ocr(const uint8_t ocr);
  102. static inline void set_ocr_power(const uint8_t pwr) { power = pwr; set_ocr(pwr); }
  103. // static uint8_t translate_power(const cutter_power_t pwr); // Used by update output for power->OCR translation
  104. #endif
  105. // Wait for spindle to spin up or spin down
  106. static inline void power_delay(const bool on) {
  107. #if DISABLED(LASER_POWER_INLINE)
  108. safe_delay(on ? SPINDLE_LASER_POWERUP_DELAY : SPINDLE_LASER_POWERDOWN_DELAY);
  109. #endif
  110. }
  111. #if ENABLED(SPINDLE_CHANGE_DIR)
  112. static void set_direction(const bool reverse);
  113. #else
  114. static inline void set_direction(const bool) {}
  115. #endif
  116. static inline void disable() { isOn = false; set_enabled(false); }
  117. #if HAS_LCD_MENU
  118. static inline void enable_forward() { isOn = true; setPower ? (power = setPower) : (setPower = interpret_power(SPEED_POWER_STARTUP)); set_direction(false); set_enabled(true); }
  119. static inline void enable_reverse() { isOn = true; setPower ? (power = setPower) : (setPower = interpret_power(SPEED_POWER_STARTUP)); set_direction(true); set_enabled(true); }
  120. #endif
  121. #if ENABLED(LASER_POWER_INLINE)
  122. // Force disengage planner power control
  123. static inline void inline_disable() { planner.settings.laser.status = 0; planner.settings.laser.power = 0; isOn = false;}
  124. // Inline modes of all other functions; all enable planner inline power control
  125. static inline void inline_enabled(const bool enable) { enable ? inline_power(SPEED_POWER_STARTUP) : inline_ocr_power(0); }
  126. static void inline_power(const cutter_power_t pwr) {
  127. #if ENABLED(SPINDLE_LASER_PWM)
  128. inline_ocr_power(translate_power(pwr));
  129. #else
  130. planner.settings.laser.status = enabled(pwr) ? 0x03 : 0x01;
  131. planner.settings.laser.power = pwr;
  132. #endif
  133. }
  134. static inline void inline_direction(const bool reverse) { UNUSED(reverse); } // TODO is this ever going to be needed
  135. #if ENABLED(SPINDLE_LASER_PWM)
  136. static inline void inline_ocr_power(const uint8_t pwr) {
  137. planner.settings.laser.status = pwr ? 0x03 : 0x01;
  138. planner.settings.laser.power = pwr;
  139. }
  140. #endif
  141. #endif
  142. static inline void kill() {
  143. TERN_(LASER_POWER_INLINE, inline_disable());
  144. disable();
  145. }
  146. };
  147. extern SpindleLaser cutter;