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.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  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. #include "../inc/MarlinConfig.h"
  24. //#define MIXER_NORMALIZER_DEBUG
  25. #ifndef __AVR__ // || DUAL_MIXING_EXTRUDER
  26. // Use 16-bit (or fastest) data for the integer mix factors
  27. typedef uint_fast16_t mixer_comp_t;
  28. typedef uint_fast16_t mixer_accu_t;
  29. #define COLOR_A_MASK 0x8000
  30. #define COLOR_MASK 0x7FFF
  31. #else
  32. // Use 8-bit data for the integer mix factors
  33. // Exactness is sacrificed for speed
  34. #define MIXER_ACCU_SIGNED
  35. typedef uint8_t mixer_comp_t;
  36. typedef int8_t mixer_accu_t;
  37. #define COLOR_A_MASK 0x80
  38. #define COLOR_MASK 0x7F
  39. #endif
  40. typedef int8_t mixer_perc_t;
  41. #ifndef MIXING_VIRTUAL_TOOLS
  42. #define MIXING_VIRTUAL_TOOLS 1
  43. #endif
  44. enum MixTool {
  45. FIRST_USER_VIRTUAL_TOOL = 0,
  46. LAST_USER_VIRTUAL_TOOL = MIXING_VIRTUAL_TOOLS - 1,
  47. NR_USER_VIRTUAL_TOOLS,
  48. MIXER_DIRECT_SET_TOOL = NR_USER_VIRTUAL_TOOLS,
  49. #if ENABLED(RETRACT_SYNC_MIXING)
  50. MIXER_AUTORETRACT_TOOL,
  51. #endif
  52. NR_MIXING_VIRTUAL_TOOLS
  53. };
  54. #if ENABLED(RETRACT_SYNC_MIXING)
  55. #define MAX_VTOOLS 254
  56. #else
  57. #define MAX_VTOOLS 255
  58. #endif
  59. static_assert(NR_MIXING_VIRTUAL_TOOLS <= MAX_VTOOLS, "MIXING_VIRTUAL_TOOLS must be <= " STRINGIFY(MAX_VTOOLS) "!");
  60. #define MIXER_STEPPER_LOOP(VAR) \
  61. for (uint_fast8_t VAR = 0; VAR < MIXING_STEPPERS; VAR++)
  62. #define MIXER_BLOCK_FIELD mixer_comp_t b_color[MIXING_STEPPERS]
  63. #define MIXER_POPULATE_BLOCK() mixer.populate_block(block->b_color)
  64. #define MIXER_STEPPER_SETUP() mixer.stepper_setup(current_block->b_color)
  65. #if ENABLED(GRADIENT_MIX)
  66. typedef struct {
  67. bool enabled; // This gradient is enabled
  68. mixer_comp_t color[MIXING_STEPPERS]; // The current gradient color
  69. float start_z, end_z; // Region for gradient
  70. int8_t start_vtool, end_vtool; // Start and end virtual tools
  71. mixer_perc_t start_mix[MIXING_STEPPERS], // Start and end mixes from those tools
  72. end_mix[MIXING_STEPPERS];
  73. #if ENABLED(GRADIENT_VTOOL)
  74. int8_t vtool_index; // Use this virtual tool number as index
  75. #endif
  76. } gradient_t;
  77. #endif
  78. /**
  79. * @brief Mixer class
  80. * @details Contains data and behaviors for a Mixing Extruder
  81. */
  82. class Mixer {
  83. public:
  84. static float collector[MIXING_STEPPERS]; // M163 components, also editable from LCD
  85. static void init(); // Populate colors at boot time
  86. static void reset_vtools();
  87. static void refresh_collector(const float proportion=1.0, const uint8_t t=selected_vtool, float (&c)[MIXING_STEPPERS]=collector);
  88. // Used up to Planner level
  89. FORCE_INLINE static void set_collector(const uint8_t c, const float f) { collector[c] = _MAX(f, 0.0f); }
  90. static void normalize(const uint8_t tool_index);
  91. FORCE_INLINE static void normalize() { normalize(selected_vtool); }
  92. FORCE_INLINE static uint8_t get_current_vtool() { return selected_vtool; }
  93. FORCE_INLINE static void T(const uint_fast8_t c) {
  94. selected_vtool = c;
  95. #if ENABLED(GRADIENT_VTOOL)
  96. refresh_gradient();
  97. #endif
  98. #if DUAL_MIXING_EXTRUDER
  99. update_mix_from_vtool();
  100. #endif
  101. }
  102. // Used when dealing with blocks
  103. FORCE_INLINE static void populate_block(mixer_comp_t b_color[MIXING_STEPPERS]) {
  104. #if ENABLED(GRADIENT_MIX)
  105. if (gradient.enabled) {
  106. MIXER_STEPPER_LOOP(i) b_color[i] = gradient.color[i];
  107. return;
  108. }
  109. #endif
  110. MIXER_STEPPER_LOOP(i) b_color[i] = color[selected_vtool][i];
  111. }
  112. FORCE_INLINE static void stepper_setup(mixer_comp_t b_color[MIXING_STEPPERS]) {
  113. MIXER_STEPPER_LOOP(i) s_color[i] = b_color[i];
  114. }
  115. #if DUAL_MIXING_EXTRUDER || ENABLED(GRADIENT_MIX)
  116. static mixer_perc_t mix[MIXING_STEPPERS]; // Scratch array for the Mix in proportion to 100
  117. static inline void copy_mix_to_color(mixer_comp_t (&tcolor)[MIXING_STEPPERS]) {
  118. // Scale each component to the largest one in terms of COLOR_A_MASK
  119. // So the largest component will be COLOR_A_MASK and the other will be in proportion to it
  120. const float scale = (COLOR_A_MASK) * RECIPROCAL(_MAX(
  121. LIST_N(MIXING_STEPPERS, mix[0], mix[1], mix[2], mix[3], mix[4], mix[5])
  122. ));
  123. // Scale all values so their maximum is COLOR_A_MASK
  124. MIXER_STEPPER_LOOP(i) tcolor[i] = mix[i] * scale;
  125. #ifdef MIXER_NORMALIZER_DEBUG
  126. SERIAL_ECHOPGM("Mix [ ");
  127. SERIAL_ECHOLIST_N(MIXING_STEPPERS, int(mix[0]), int(mix[1]), int(mix[2]), int(mix[3]), int(mix[4]), int(mix[5]));
  128. SERIAL_ECHOPGM(" ] to Color [ ");
  129. SERIAL_ECHOLIST_N(MIXING_STEPPERS, int(tcolor[0]), int(tcolor[1]), int(tcolor[2]), int(tcolor[3]), int(tcolor[4]), int(tcolor[5]));
  130. SERIAL_ECHOLNPGM(" ]");
  131. #endif
  132. }
  133. static inline void update_mix_from_vtool(const uint8_t j=selected_vtool) {
  134. float ctot = 0;
  135. MIXER_STEPPER_LOOP(i) ctot += color[j][i];
  136. //MIXER_STEPPER_LOOP(i) mix[i] = 100.0f * color[j][i] / ctot;
  137. MIXER_STEPPER_LOOP(i) mix[i] = mixer_perc_t(100.0f * color[j][i] / ctot);
  138. #ifdef MIXER_NORMALIZER_DEBUG
  139. SERIAL_ECHOPAIR("V-tool ", int(j), " [ ");
  140. SERIAL_ECHOLIST_N(MIXING_STEPPERS, int(color[j][0]), int(color[j][1]), int(color[j][2]), int(color[j][3]), int(color[j][4]), int(color[j][5]));
  141. SERIAL_ECHOPGM(" ] to Mix [ ");
  142. SERIAL_ECHOLIST_N(MIXING_STEPPERS, int(mix[0]), int(mix[1]), int(mix[2]), int(mix[3]), int(mix[4]), int(mix[5]));
  143. SERIAL_ECHOLNPGM(" ]");
  144. #endif
  145. }
  146. #endif // DUAL_MIXING_EXTRUDER || GRADIENT_MIX
  147. #if DUAL_MIXING_EXTRUDER
  148. // Update the virtual tool from an edited mix
  149. static inline void update_vtool_from_mix() {
  150. copy_mix_to_color(color[selected_vtool]);
  151. #if ENABLED(GRADIENT_MIX)
  152. refresh_gradient();
  153. #endif
  154. // MIXER_STEPPER_LOOP(i) collector[i] = mix[i];
  155. // normalize();
  156. }
  157. #endif // DUAL_MIXING_EXTRUDER
  158. #if ENABLED(GRADIENT_MIX)
  159. static gradient_t gradient;
  160. static float prev_z;
  161. // Update the current mix from the gradient for a given Z
  162. static void update_gradient_for_z(const float z);
  163. static void update_gradient_for_planner_z();
  164. static inline void gradient_control(const float z) {
  165. if (gradient.enabled) {
  166. if (z >= gradient.end_z)
  167. T(gradient.end_vtool);
  168. else
  169. update_gradient_for_z(z);
  170. }
  171. }
  172. static inline void update_mix_from_gradient() {
  173. float ctot = 0;
  174. MIXER_STEPPER_LOOP(i) ctot += gradient.color[i];
  175. MIXER_STEPPER_LOOP(i) mix[i] = (mixer_perc_t)CEIL(100.0f * gradient.color[i] / ctot);
  176. #ifdef MIXER_NORMALIZER_DEBUG
  177. SERIAL_ECHOPGM("Gradient [ ");
  178. SERIAL_ECHOLIST_N(MIXING_STEPPERS, int(gradient.color[0]), int(gradient.color[1]), int(gradient.color[2]), int(gradient.color[3]), int(gradient.color[4]), int(gradient.color[5]));
  179. SERIAL_ECHOPGM(" ] to Mix [ ");
  180. SERIAL_ECHOLIST_N(MIXING_STEPPERS, int(mix[0]), int(mix[1]), int(mix[2]), int(mix[3]), int(mix[4]), int(mix[5]));
  181. SERIAL_ECHOLNPGM(" ]");
  182. #endif
  183. }
  184. // Refresh the gradient after a change
  185. static void refresh_gradient() {
  186. #if ENABLED(GRADIENT_VTOOL)
  187. const bool is_grd = (gradient.vtool_index == -1 || selected_vtool == (uint8_t)gradient.vtool_index);
  188. #else
  189. constexpr bool is_grd = true;
  190. #endif
  191. gradient.enabled = is_grd && gradient.start_vtool != gradient.end_vtool && gradient.start_z < gradient.end_z;
  192. if (gradient.enabled) {
  193. mixer_perc_t mix_bak[MIXING_STEPPERS];
  194. COPY(mix_bak, mix);
  195. update_mix_from_vtool(gradient.start_vtool);
  196. COPY(gradient.start_mix, mix);
  197. update_mix_from_vtool(gradient.end_vtool);
  198. COPY(gradient.end_mix, mix);
  199. update_gradient_for_planner_z();
  200. COPY(mix, mix_bak);
  201. prev_z = -1;
  202. }
  203. }
  204. #endif // GRADIENT_MIX
  205. // Used in Stepper
  206. FORCE_INLINE static uint8_t get_stepper() { return runner; }
  207. FORCE_INLINE static uint8_t get_next_stepper() {
  208. for (;;) {
  209. if (--runner < 0) runner = MIXING_STEPPERS - 1;
  210. accu[runner] += s_color[runner];
  211. if (
  212. #ifdef MIXER_ACCU_SIGNED
  213. accu[runner] < 0
  214. #else
  215. accu[runner] & COLOR_A_MASK
  216. #endif
  217. ) {
  218. accu[runner] &= COLOR_MASK;
  219. return runner;
  220. }
  221. }
  222. }
  223. private:
  224. // Used up to Planner level
  225. static uint_fast8_t selected_vtool;
  226. static mixer_comp_t color[NR_MIXING_VIRTUAL_TOOLS][MIXING_STEPPERS];
  227. // Used in Stepper
  228. static int_fast8_t runner;
  229. static mixer_comp_t s_color[MIXING_STEPPERS];
  230. static mixer_accu_t accu[MIXING_STEPPERS];
  231. };
  232. extern Mixer mixer;