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.cpp 6.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  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. #include "../inc/MarlinConfig.h"
  23. #if ENABLED(MIXING_EXTRUDER)
  24. //#define MIXER_NORMALIZER_DEBUG
  25. #include "mixing.h"
  26. Mixer mixer;
  27. #ifdef MIXER_NORMALIZER_DEBUG
  28. #include "../core/serial.h"
  29. #endif
  30. // Used up to Planner level
  31. uint_fast8_t Mixer::selected_vtool = 0;
  32. float Mixer::collector[MIXING_STEPPERS]; // mix proportion. 0.0 = off, otherwise <= COLOR_A_MASK.
  33. mixer_comp_t Mixer::color[NR_MIXING_VIRTUAL_TOOLS][MIXING_STEPPERS];
  34. // Used in Stepper
  35. int_fast8_t Mixer::runner = 0;
  36. mixer_comp_t Mixer::s_color[MIXING_STEPPERS];
  37. mixer_accu_t Mixer::accu[MIXING_STEPPERS] = { 0 };
  38. #if EITHER(HAS_DUAL_MIXING, GRADIENT_MIX)
  39. mixer_perc_t Mixer::mix[MIXING_STEPPERS];
  40. #endif
  41. void Mixer::normalize(const uint8_t tool_index) {
  42. float cmax = 0;
  43. #ifdef MIXER_NORMALIZER_DEBUG
  44. float csum = 0;
  45. #endif
  46. MIXER_STEPPER_LOOP(i) {
  47. const float v = collector[i];
  48. NOLESS(cmax, v);
  49. #ifdef MIXER_NORMALIZER_DEBUG
  50. csum += v;
  51. #endif
  52. }
  53. #ifdef MIXER_NORMALIZER_DEBUG
  54. SERIAL_ECHOPGM("Mixer: Old relation : [ ");
  55. MIXER_STEPPER_LOOP(i) {
  56. SERIAL_DECIMAL(collector[i] / csum);
  57. SERIAL_CHAR(' ');
  58. }
  59. SERIAL_ECHOLNPGM("]");
  60. #endif
  61. // Scale all values so their maximum is COLOR_A_MASK
  62. const float scale = float(COLOR_A_MASK) / cmax;
  63. MIXER_STEPPER_LOOP(i) color[tool_index][i] = collector[i] * scale;
  64. #ifdef MIXER_NORMALIZER_DEBUG
  65. csum = 0;
  66. SERIAL_ECHOPGM("Mixer: Normalize to : [ ");
  67. MIXER_STEPPER_LOOP(i) {
  68. SERIAL_ECHO(uint16_t(color[tool_index][i]));
  69. SERIAL_CHAR(' ');
  70. csum += color[tool_index][i];
  71. }
  72. SERIAL_ECHOLNPGM("]");
  73. SERIAL_ECHOPGM("Mixer: New relation : [ ");
  74. MIXER_STEPPER_LOOP(i) {
  75. SERIAL_ECHO_F(uint16_t(color[tool_index][i]) / csum, 3);
  76. SERIAL_CHAR(' ');
  77. }
  78. SERIAL_ECHOLNPGM("]");
  79. #endif
  80. TERN_(GRADIENT_MIX, refresh_gradient());
  81. }
  82. void Mixer::reset_vtools() {
  83. // Virtual Tools 0, 1, 2, 3 = Filament 1, 2, 3, 4, etc.
  84. // Every virtual tool gets a pure filament
  85. LOOP_L_N(t, _MIN(MIXING_VIRTUAL_TOOLS, MIXING_STEPPERS))
  86. MIXER_STEPPER_LOOP(i)
  87. color[t][i] = (t == i) ? COLOR_A_MASK : 0;
  88. // Remaining virtual tools are 100% filament 1
  89. #if MIXING_VIRTUAL_TOOLS > MIXING_STEPPERS
  90. LOOP_S_L_N(t, MIXING_STEPPERS, MIXING_VIRTUAL_TOOLS)
  91. MIXER_STEPPER_LOOP(i)
  92. color[t][i] = (i == 0) ? COLOR_A_MASK : 0;
  93. #endif
  94. // MIXING_PRESETS: Set a variety of obvious mixes as presets
  95. #if ENABLED(MIXING_PRESETS) && WITHIN(MIXING_STEPPERS, 2, 3)
  96. #if MIXING_STEPPERS == 2
  97. if (MIXING_VIRTUAL_TOOLS > 2) { collector[0] = 1; collector[1] = 1; mixer.normalize(2); } // 1:1
  98. if (MIXING_VIRTUAL_TOOLS > 3) { collector[0] = 3; mixer.normalize(3); } // 3:1
  99. if (MIXING_VIRTUAL_TOOLS > 4) { collector[0] = 1; collector[1] = 3; mixer.normalize(4); } // 1:3
  100. if (MIXING_VIRTUAL_TOOLS > 5) { collector[1] = 2; mixer.normalize(5); } // 1:2
  101. if (MIXING_VIRTUAL_TOOLS > 6) { collector[0] = 2; collector[1] = 1; mixer.normalize(6); } // 2:1
  102. if (MIXING_VIRTUAL_TOOLS > 7) { collector[0] = 3; collector[1] = 2; mixer.normalize(7); } // 3:2
  103. #else
  104. if (MIXING_VIRTUAL_TOOLS > 3) { collector[0] = 1; collector[1] = 1; collector[2] = 1; mixer.normalize(3); } // 1:1:1
  105. if (MIXING_VIRTUAL_TOOLS > 4) { collector[1] = 3; collector[2] = 0; mixer.normalize(4); } // 1:3:0
  106. if (MIXING_VIRTUAL_TOOLS > 5) { collector[0] = 0; collector[2] = 1; mixer.normalize(5); } // 0:3:1
  107. if (MIXING_VIRTUAL_TOOLS > 6) { collector[1] = 1; mixer.normalize(6); } // 0:1:1
  108. if (MIXING_VIRTUAL_TOOLS > 7) { collector[0] = 1; collector[2] = 0; mixer.normalize(7); } // 1:1:0
  109. #endif
  110. ZERO(collector);
  111. #endif
  112. }
  113. // called at boot
  114. void Mixer::init() {
  115. ZERO(collector);
  116. reset_vtools();
  117. #if HAS_MIXER_SYNC_CHANNEL
  118. // AUTORETRACT_TOOL gets the same amount of all filaments
  119. MIXER_STEPPER_LOOP(i)
  120. color[MIXER_AUTORETRACT_TOOL][i] = COLOR_A_MASK;
  121. #endif
  122. #if EITHER(HAS_DUAL_MIXING, GRADIENT_MIX)
  123. update_mix_from_vtool();
  124. #endif
  125. TERN_(GRADIENT_MIX, update_gradient_for_planner_z());
  126. }
  127. void Mixer::refresh_collector(const float proportion/*=1.0*/, const uint8_t t/*=selected_vtool*/, float (&c)[MIXING_STEPPERS]/*=collector*/) {
  128. float csum = 0, cmax = 0;
  129. MIXER_STEPPER_LOOP(i) {
  130. const float v = color[t][i];
  131. cmax = _MAX(cmax, v);
  132. csum += v;
  133. }
  134. //SERIAL_ECHOPGM("Mixer::refresh_collector(", proportion, ", ", t, ") cmax=", cmax, " csum=", csum, " color");
  135. const float inv_prop = proportion / csum;
  136. MIXER_STEPPER_LOOP(i) {
  137. c[i] = color[t][i] * inv_prop;
  138. //SERIAL_ECHOPGM(" [", t, "][", i, "] = ", color[t][i], " (", c[i], ") ");
  139. }
  140. //SERIAL_EOL();
  141. }
  142. #if ENABLED(GRADIENT_MIX)
  143. #include "../module/motion.h"
  144. #include "../module/planner.h"
  145. gradient_t Mixer::gradient = {
  146. false, // enabled
  147. {0}, // color (array)
  148. 0, 0, // start_z, end_z
  149. 0, 1, // start_vtool, end_vtool
  150. {0}, {0} // start_mix[], end_mix[]
  151. OPTARG(GRADIENT_VTOOL, -1) // vtool_index
  152. };
  153. float Mixer::prev_z; // = 0
  154. void Mixer::update_gradient_for_z(const_float_t z) {
  155. if (z == prev_z) return;
  156. prev_z = z;
  157. const float slice = gradient.end_z - gradient.start_z;
  158. float pct = (z - gradient.start_z) / slice;
  159. NOLESS(pct, 0.0f); NOMORE(pct, 1.0f);
  160. MIXER_STEPPER_LOOP(i) {
  161. const mixer_perc_t sm = gradient.start_mix[i];
  162. mix[i] = sm + (gradient.end_mix[i] - sm) * pct;
  163. }
  164. copy_mix_to_color(gradient.color);
  165. }
  166. void Mixer::update_gradient_for_planner_z() {
  167. #if ENABLED(DELTA)
  168. get_cartesian_from_steppers();
  169. update_gradient_for_z(cartes.z);
  170. #else
  171. update_gradient_for_z(planner.get_axis_position_mm(Z_AXIS));
  172. #endif
  173. }
  174. #endif // GRADIENT_MIX
  175. #endif // MIXING_EXTRUDER