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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  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. #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. #define PCT_TO_PWM(X) ((X) * 255 / 100)
  33. #ifndef SPEED_POWER_INTERCEPT
  34. #define SPEED_POWER_INTERCEPT 0
  35. #endif
  36. #define SPEED_POWER_FLOOR TERN(CUTTER_POWER_RELATIVE, SPEED_POWER_MIN, 0)
  37. // #define _MAP(N,S1,S2,D1,D2) ((N)*_MAX((D2)-(D1),0)/_MAX((S2)-(S1),1)+(D1))
  38. class SpindleLaser {
  39. public:
  40. static constexpr float
  41. min_pct = round(TERN(CUTTER_POWER_RELATIVE, 0, (100 * float(SPEED_POWER_MIN) / TERN(SPINDLE_FEATURE, float(SPEED_POWER_MAX), 100)))),
  42. max_pct = round(TERN(SPINDLE_FEATURE, 100, float(SPEED_POWER_MAX)));
  43. static const inline uint8_t pct_to_ocr(const float pct) { return uint8_t(PCT_TO_PWM(pct)); }
  44. // cpower = configured values (ie SPEED_POWER_MAX)
  45. static const inline uint8_t cpwr_to_pct(const cutter_cpower_t cpwr) { // configured value to pct
  46. return unitPower ? round(100 * (cpwr - SPEED_POWER_FLOOR) / (SPEED_POWER_MAX - SPEED_POWER_FLOOR)) : 0;
  47. }
  48. // Convert a configured value (cpower)(ie SPEED_POWER_STARTUP) to unit power (upwr, upower),
  49. // which can be PWM, Percent, or RPM (rel/abs).
  50. static const inline cutter_power_t cpwr_to_upwr(const cutter_cpower_t cpwr) { // STARTUP power to Unit power
  51. const cutter_power_t upwr = (
  52. #if ENABLED(SPINDLE_FEATURE)
  53. // Spindle configured values are in RPM
  54. #if CUTTER_UNIT_IS(RPM)
  55. cpwr // to RPM
  56. #elif CUTTER_UNIT_IS(PERCENT) // to PCT
  57. cpwr_to_pct(cpwr)
  58. #else // to PWM
  59. PCT_TO_PWM(cpwr_to_pct(cpwr))
  60. #endif
  61. #else
  62. // Laser configured values are in PCT
  63. #if CUTTER_UNIT_IS(PWM255)
  64. PCT_TO_PWM(cpwr)
  65. #else
  66. cpwr // to RPM/PCT
  67. #endif
  68. #endif
  69. );
  70. return upwr;
  71. }
  72. static const cutter_power_t mpower_min() { return cpwr_to_upwr(SPEED_POWER_MIN); }
  73. static const cutter_power_t mpower_max() { return cpwr_to_upwr(SPEED_POWER_MAX); }
  74. static bool isReady; // Ready to apply power setting from the UI to OCR
  75. static uint8_t power;
  76. #if ENABLED(MARLIN_DEV_MODE)
  77. static cutter_frequency_t frequency; // Set PWM frequency; range: 2K-50K
  78. #endif
  79. static cutter_power_t menuPower, // Power as set via LCD menu in PWM, Percentage or RPM
  80. unitPower; // Power as displayed status in PWM, Percentage or RPM
  81. static void init();
  82. #if ENABLED(MARLIN_DEV_MODE)
  83. static inline void refresh_frequency() { set_pwm_frequency(pin_t(SPINDLE_LASER_PWM_PIN), frequency); }
  84. #endif
  85. // Modifying this function should update everywhere
  86. static inline bool enabled(const cutter_power_t opwr) { return opwr > 0; }
  87. static inline bool enabled() { return enabled(power); }
  88. static void apply_power(const uint8_t inpow);
  89. FORCE_INLINE static void refresh() { apply_power(power); }
  90. FORCE_INLINE static void set_power(const uint8_t upwr) { power = upwr; refresh(); }
  91. #if ENABLED(SPINDLE_LASER_PWM)
  92. static void set_ocr(const uint8_t ocr);
  93. static inline void set_ocr_power(const uint8_t ocr) { power = ocr; set_ocr(ocr); }
  94. static void ocr_off();
  95. // Used to update output for power->OCR translation
  96. static inline uint8_t upower_to_ocr(const cutter_power_t upwr) {
  97. return (
  98. #if CUTTER_UNIT_IS(PWM255)
  99. uint8_t(upwr)
  100. #elif CUTTER_UNIT_IS(PERCENT)
  101. pct_to_ocr(upwr)
  102. #else
  103. uint8_t(pct_to_ocr(cpwr_to_pct(upwr)))
  104. #endif
  105. );
  106. }
  107. // Correct power to configured range
  108. static inline cutter_power_t power_to_range(const cutter_power_t pwr) {
  109. return power_to_range(pwr, (
  110. #if CUTTER_UNIT_IS(PWM255)
  111. 0
  112. #elif CUTTER_UNIT_IS(PERCENT)
  113. 1
  114. #elif CUTTER_UNIT_IS(RPM)
  115. 2
  116. #else
  117. #error "???"
  118. #endif
  119. ));
  120. }
  121. static inline cutter_power_t power_to_range(const cutter_power_t pwr, const uint8_t pwrUnit) {
  122. if (pwr <= 0) return 0;
  123. cutter_power_t upwr;
  124. switch (pwrUnit) {
  125. case 0: // PWM
  126. upwr = cutter_power_t(
  127. (pwr < pct_to_ocr(min_pct)) ? pct_to_ocr(min_pct) // Use minimum if set below
  128. : (pwr > pct_to_ocr(max_pct)) ? pct_to_ocr(max_pct) // Use maximum if set above
  129. : pwr
  130. );
  131. break;
  132. case 1: // PERCENT
  133. upwr = cutter_power_t(
  134. (pwr < min_pct) ? min_pct // Use minimum if set below
  135. : (pwr > max_pct) ? max_pct // Use maximum if set above
  136. : pwr // PCT
  137. );
  138. break;
  139. case 2: // RPM
  140. upwr = cutter_power_t(
  141. (pwr < SPEED_POWER_MIN) ? SPEED_POWER_MIN // Use minimum if set below
  142. : (pwr > SPEED_POWER_MAX) ? SPEED_POWER_MAX // Use maximum if set above
  143. : pwr // Calculate OCR value
  144. );
  145. break;
  146. default: break;
  147. }
  148. return upwr;
  149. }
  150. #endif // SPINDLE_LASER_PWM
  151. static inline void set_enabled(const bool enable) {
  152. set_power(enable ? TERN(SPINDLE_LASER_PWM, (power ?: (unitPower ? upower_to_ocr(cpwr_to_upwr(SPEED_POWER_STARTUP)) : 0)), 255) : 0);
  153. }
  154. // Wait for spindle to spin up or spin down
  155. static inline void power_delay(const bool on) {
  156. #if DISABLED(LASER_POWER_INLINE)
  157. safe_delay(on ? SPINDLE_LASER_POWERUP_DELAY : SPINDLE_LASER_POWERDOWN_DELAY);
  158. #endif
  159. }
  160. #if ENABLED(SPINDLE_CHANGE_DIR)
  161. static void set_direction(const bool reverse);
  162. #else
  163. static inline void set_direction(const bool) {}
  164. #endif
  165. static inline void disable() { isReady = false; set_enabled(false); }
  166. #if HAS_LCD_MENU
  167. static inline void enable_with_dir(const bool reverse) {
  168. isReady = true;
  169. const uint8_t ocr = TERN(SPINDLE_LASER_PWM, upower_to_ocr(menuPower), 255);
  170. if (menuPower)
  171. power = ocr;
  172. else
  173. menuPower = cpwr_to_upwr(SPEED_POWER_STARTUP);
  174. unitPower = menuPower;
  175. set_direction(reverse);
  176. set_enabled(true);
  177. }
  178. FORCE_INLINE static void enable_forward() { enable_with_dir(false); }
  179. FORCE_INLINE static void enable_reverse() { enable_with_dir(true); }
  180. #if ENABLED(SPINDLE_LASER_PWM)
  181. static inline void update_from_mpower() {
  182. if (isReady) power = upower_to_ocr(menuPower);
  183. unitPower = menuPower;
  184. }
  185. #endif
  186. #endif
  187. #if ENABLED(LASER_POWER_INLINE)
  188. /**
  189. * Inline power adds extra fields to the planner block
  190. * to handle laser power and scale to movement speed.
  191. */
  192. // Force disengage planner power control
  193. static inline void inline_disable() {
  194. isReady = false;
  195. unitPower = 0;
  196. planner.laser_inline.status.isPlanned = false;
  197. planner.laser_inline.status.isEnabled = false;
  198. planner.laser_inline.power = 0;
  199. }
  200. // Inline modes of all other functions; all enable planner inline power control
  201. static inline void set_inline_enabled(const bool enable) {
  202. if (enable)
  203. inline_power(cpwr_to_upwr(SPEED_POWER_STARTUP));
  204. else {
  205. isReady = false;
  206. unitPower = menuPower = 0;
  207. planner.laser_inline.status.isPlanned = false;
  208. TERN(SPINDLE_LASER_PWM, inline_ocr_power, inline_power)(0);
  209. }
  210. }
  211. // Set the power for subsequent movement blocks
  212. static void inline_power(const cutter_power_t upwr) {
  213. unitPower = menuPower = upwr;
  214. #if ENABLED(SPINDLE_LASER_PWM)
  215. #if ENABLED(SPEED_POWER_RELATIVE) && !CUTTER_UNIT_IS(RPM) // relative mode does not turn laser off at 0, except for RPM
  216. planner.laser_inline.status.isEnabled = true;
  217. planner.laser_inline.power = upower_to_ocr(upwr);
  218. isReady = true;
  219. #else
  220. inline_ocr_power(upower_to_ocr(upwr));
  221. #endif
  222. #else
  223. planner.laser_inline.status.isEnabled = enabled(upwr);
  224. planner.laser_inline.power = upwr;
  225. isReady = enabled(upwr);
  226. #endif
  227. }
  228. static inline void inline_direction(const bool) { /* never */ }
  229. #if ENABLED(SPINDLE_LASER_PWM)
  230. static inline void inline_ocr_power(const uint8_t ocrpwr) {
  231. isReady = ocrpwr > 0;
  232. planner.laser_inline.status.isEnabled = ocrpwr > 0;
  233. planner.laser_inline.power = ocrpwr;
  234. }
  235. #endif
  236. #endif // LASER_POWER_INLINE
  237. static inline void kill() {
  238. TERN_(LASER_POWER_INLINE, inline_disable());
  239. disable();
  240. }
  241. };
  242. extern SpindleLaser cutter;