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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  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. #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 DUAL_MIXING_EXTRUDER || ENABLED(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_ECHO_F(collector[i] / csum, 3);
  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. #if ENABLED(GRADIENT_MIX)
  81. refresh_gradient();
  82. #endif
  83. }
  84. void Mixer::reset_vtools() {
  85. // Virtual Tools 0, 1, 2, 3 = Filament 1, 2, 3, 4, etc.
  86. // Every virtual tool gets a pure filament
  87. LOOP_L_N(t, MIXING_VIRTUAL_TOOLS && t < MIXING_STEPPERS)
  88. MIXER_STEPPER_LOOP(i)
  89. color[t][i] = (t == i) ? COLOR_A_MASK : 0;
  90. // Remaining virtual tools are 100% filament 1
  91. #if MIXING_VIRTUAL_TOOLS > MIXING_STEPPERS
  92. LOOP_S_L_N(t, MIXING_STEPPERS, MIXING_VIRTUAL_TOOLS)
  93. MIXER_STEPPER_LOOP(i)
  94. color[t][i] = (i == 0) ? COLOR_A_MASK : 0;
  95. #endif
  96. }
  97. // called at boot
  98. void Mixer::init() {
  99. reset_vtools();
  100. #if ENABLED(RETRACT_SYNC_MIXING)
  101. // AUTORETRACT_TOOL gets the same amount of all filaments
  102. MIXER_STEPPER_LOOP(i)
  103. color[MIXER_AUTORETRACT_TOOL][i] = COLOR_A_MASK;
  104. #endif
  105. ZERO(collector);
  106. #if DUAL_MIXING_EXTRUDER || ENABLED(GRADIENT_MIX)
  107. update_mix_from_vtool();
  108. #endif
  109. #if ENABLED(GRADIENT_MIX)
  110. update_gradient_for_planner_z();
  111. #endif
  112. }
  113. void Mixer::refresh_collector(const float proportion/*=1.0*/, const uint8_t t/*=selected_vtool*/, float (&c)[MIXING_STEPPERS]/*=collector*/) {
  114. float csum = 0, cmax = 0;
  115. MIXER_STEPPER_LOOP(i) {
  116. const float v = color[t][i];
  117. cmax = _MAX(cmax, v);
  118. csum += v;
  119. }
  120. //SERIAL_ECHOPAIR("Mixer::refresh_collector(", proportion, ", ", int(t), ") cmax=", cmax, " csum=", csum, " color");
  121. const float inv_prop = proportion / csum;
  122. MIXER_STEPPER_LOOP(i) {
  123. c[i] = color[t][i] * inv_prop;
  124. //SERIAL_ECHOPAIR(" [", int(t), "][", int(i), "] = ", int(color[t][i]), " (", c[i], ") ");
  125. }
  126. //SERIAL_EOL();
  127. }
  128. #if ENABLED(GRADIENT_MIX)
  129. #include "../module/motion.h"
  130. #include "../module/planner.h"
  131. gradient_t Mixer::gradient = {
  132. false, // enabled
  133. {0}, // color (array)
  134. 0, 0, // start_z, end_z
  135. 0, 1, // start_vtool, end_vtool
  136. {0}, {0} // start_mix[], end_mix[]
  137. #if ENABLED(GRADIENT_VTOOL)
  138. , -1 // vtool_index
  139. #endif
  140. };
  141. float Mixer::prev_z; // = 0
  142. void Mixer::update_gradient_for_z(const float z) {
  143. if (z == prev_z) return;
  144. prev_z = z;
  145. const float slice = gradient.end_z - gradient.start_z;
  146. float pct = (z - gradient.start_z) / slice;
  147. NOLESS(pct, 0.0f); NOMORE(pct, 1.0f);
  148. MIXER_STEPPER_LOOP(i) {
  149. const mixer_perc_t sm = gradient.start_mix[i];
  150. mix[i] = sm + (gradient.end_mix[i] - sm) * pct;
  151. }
  152. copy_mix_to_color(gradient.color);
  153. }
  154. void Mixer::update_gradient_for_planner_z() {
  155. update_gradient_for_z(planner.get_axis_position_mm(Z_AXIS));
  156. }
  157. #endif // GRADIENT_MIX
  158. #endif // MIXING_EXTRUDER