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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  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. #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. static inline void set_enabled(const bool enable) { set_power(enable ? (power ?: (unitPower ? upower_to_ocr(cpwr_to_upwr(SPEED_POWER_STARTUP)) : 0)) : 0); }
  92. #if ENABLED(SPINDLE_LASER_PWM)
  93. static void set_ocr(const uint8_t ocr);
  94. static inline void set_ocr_power(const uint8_t ocr) { power = ocr; set_ocr(ocr); }
  95. static void ocr_off();
  96. // Used to update output for power->OCR translation
  97. static inline uint8_t upower_to_ocr(const cutter_power_t upwr) {
  98. return (
  99. #if CUTTER_UNIT_IS(PWM255)
  100. uint8_t(upwr)
  101. #elif CUTTER_UNIT_IS(PERCENT)
  102. pct_to_ocr(upwr)
  103. #else
  104. uint8_t(pct_to_ocr(cpwr_to_pct(upwr)))
  105. #endif
  106. );
  107. }
  108. // Correct power to configured range
  109. static inline cutter_power_t power_to_range(const cutter_power_t pwr) {
  110. return power_to_range(pwr, (
  111. #if CUTTER_UNIT_IS(PWM255)
  112. 0
  113. #elif CUTTER_UNIT_IS(PERCENT)
  114. 1
  115. #elif CUTTER_UNIT_IS(RPM)
  116. 2
  117. #else
  118. #error "???"
  119. #endif
  120. ));
  121. }
  122. static inline cutter_power_t power_to_range(const cutter_power_t pwr, const uint8_t pwrUnit) {
  123. if (pwr <= 0) return 0;
  124. cutter_power_t upwr;
  125. switch (pwrUnit) {
  126. case 0: // PWM
  127. upwr = (
  128. (pwr < pct_to_ocr(min_pct)) ? pct_to_ocr(min_pct) // Use minimum if set below
  129. : (pwr > pct_to_ocr(max_pct)) ? pct_to_ocr(max_pct) // Use maximum if set above
  130. : pwr
  131. );
  132. break;
  133. case 1: // PERCENT
  134. upwr = (
  135. (pwr < min_pct) ? min_pct // Use minimum if set below
  136. : (pwr > max_pct) ? max_pct // Use maximum if set above
  137. : pwr // PCT
  138. );
  139. break;
  140. case 2: // RPM
  141. upwr = (
  142. (pwr < SPEED_POWER_MIN) ? SPEED_POWER_MIN // Use minimum if set below
  143. : (pwr > SPEED_POWER_MAX) ? SPEED_POWER_MAX // Use maximum if set above
  144. : pwr // Calculate OCR value
  145. );
  146. break;
  147. default: break;
  148. }
  149. return upwr;
  150. }
  151. #endif // SPINDLE_LASER_PWM
  152. // Wait for spindle to spin up or spin down
  153. static inline void power_delay(const bool on) {
  154. #if DISABLED(LASER_POWER_INLINE)
  155. safe_delay(on ? SPINDLE_LASER_POWERUP_DELAY : SPINDLE_LASER_POWERDOWN_DELAY);
  156. #endif
  157. }
  158. #if ENABLED(SPINDLE_CHANGE_DIR)
  159. static void set_direction(const bool reverse);
  160. #else
  161. static inline void set_direction(const bool) {}
  162. #endif
  163. static inline void disable() { isReady = false; set_enabled(false); }
  164. #if HAS_LCD_MENU
  165. static inline void enable_with_dir(const bool reverse) {
  166. isReady = true;
  167. const uint8_t ocr = upower_to_ocr(menuPower);
  168. if (menuPower)
  169. power = ocr;
  170. else
  171. menuPower = cpwr_to_upwr(SPEED_POWER_STARTUP);
  172. unitPower = menuPower;
  173. set_direction(reverse);
  174. set_enabled(true);
  175. }
  176. FORCE_INLINE static void enable_forward() { enable_with_dir(false); }
  177. FORCE_INLINE static void enable_reverse() { enable_with_dir(true); }
  178. #if ENABLED(SPINDLE_LASER_PWM)
  179. static inline void update_from_mpower() {
  180. if (isReady) power = upower_to_ocr(menuPower);
  181. unitPower = menuPower;
  182. }
  183. #endif
  184. #endif
  185. #if ENABLED(LASER_POWER_INLINE)
  186. /**
  187. * Inline power adds extra fields to the planner block
  188. * to handle laser power and scale to movement speed.
  189. */
  190. // Force disengage planner power control
  191. static inline void inline_disable() {
  192. isReady = false;
  193. unitPower = 0;
  194. planner.laser_inline.status.isPlanned = false;
  195. planner.laser_inline.status.isEnabled = false;
  196. planner.laser_inline.power = 0;
  197. }
  198. // Inline modes of all other functions; all enable planner inline power control
  199. static inline void set_inline_enabled(const bool enable) {
  200. if (enable)
  201. inline_power(cpwr_to_upwr(SPEED_POWER_STARTUP));
  202. else {
  203. isReady = false;
  204. unitPower = menuPower = 0;
  205. planner.laser_inline.status.isPlanned = false;
  206. TERN(SPINDLE_LASER_PWM, inline_ocr_power, inline_power)(0);
  207. }
  208. }
  209. // Set the power for subsequent movement blocks
  210. static void inline_power(const cutter_power_t upwr) {
  211. unitPower = menuPower = upwr;
  212. #if ENABLED(SPINDLE_LASER_PWM)
  213. #if ENABLED(SPEED_POWER_RELATIVE) && !CUTTER_UNIT_IS(RPM) // relative mode does not turn laser off at 0, except for RPM
  214. planner.laser_inline.status.isEnabled = true;
  215. planner.laser_inline.power = upower_to_ocr(upwr);
  216. isReady = true;
  217. #else
  218. inline_ocr_power(upower_to_ocr(upwr));
  219. #endif
  220. #else
  221. planner.laser_inline.status.isEnabled = enabled(upwr);
  222. planner.laser_inline.power = upwr;
  223. isReady = enabled(upwr);
  224. #endif
  225. }
  226. static inline void inline_direction(const bool) { /* never */ }
  227. #if ENABLED(SPINDLE_LASER_PWM)
  228. static inline void inline_ocr_power(const uint8_t ocrpwr) {
  229. isReady = ocrpwr > 0;
  230. planner.laser_inline.status.isEnabled = ocrpwr > 0;
  231. planner.laser_inline.power = ocrpwr;
  232. }
  233. #endif
  234. #endif // LASER_POWER_INLINE
  235. static inline void kill() {
  236. TERN_(LASER_POWER_INLINE, inline_disable());
  237. disable();
  238. }
  239. };
  240. extern SpindleLaser cutter;