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.

mixing.h 8.7KB

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