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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  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_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. 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. }
  95. // called at boot
  96. void Mixer::init() {
  97. reset_vtools();
  98. #if HAS_MIXER_SYNC_CHANNEL
  99. // AUTORETRACT_TOOL gets the same amount of all filaments
  100. MIXER_STEPPER_LOOP(i)
  101. color[MIXER_AUTORETRACT_TOOL][i] = COLOR_A_MASK;
  102. #endif
  103. ZERO(collector);
  104. #if EITHER(HAS_DUAL_MIXING, GRADIENT_MIX)
  105. update_mix_from_vtool();
  106. #endif
  107. TERN_(GRADIENT_MIX, update_gradient_for_planner_z());
  108. }
  109. void Mixer::refresh_collector(const float proportion/*=1.0*/, const uint8_t t/*=selected_vtool*/, float (&c)[MIXING_STEPPERS]/*=collector*/) {
  110. float csum = 0, cmax = 0;
  111. MIXER_STEPPER_LOOP(i) {
  112. const float v = color[t][i];
  113. cmax = _MAX(cmax, v);
  114. csum += v;
  115. }
  116. //SERIAL_ECHOPAIR("Mixer::refresh_collector(", proportion, ", ", int(t), ") cmax=", cmax, " csum=", csum, " color");
  117. const float inv_prop = proportion / csum;
  118. MIXER_STEPPER_LOOP(i) {
  119. c[i] = color[t][i] * inv_prop;
  120. //SERIAL_ECHOPAIR(" [", int(t), "][", int(i), "] = ", int(color[t][i]), " (", c[i], ") ");
  121. }
  122. //SERIAL_EOL();
  123. }
  124. #if ENABLED(GRADIENT_MIX)
  125. #include "../module/motion.h"
  126. #include "../module/planner.h"
  127. gradient_t Mixer::gradient = {
  128. false, // enabled
  129. {0}, // color (array)
  130. 0, 0, // start_z, end_z
  131. 0, 1, // start_vtool, end_vtool
  132. {0}, {0} // start_mix[], end_mix[]
  133. #if ENABLED(GRADIENT_VTOOL)
  134. , -1 // vtool_index
  135. #endif
  136. };
  137. float Mixer::prev_z; // = 0
  138. void Mixer::update_gradient_for_z(const float z) {
  139. if (z == prev_z) return;
  140. prev_z = z;
  141. const float slice = gradient.end_z - gradient.start_z;
  142. float pct = (z - gradient.start_z) / slice;
  143. NOLESS(pct, 0.0f); NOMORE(pct, 1.0f);
  144. MIXER_STEPPER_LOOP(i) {
  145. const mixer_perc_t sm = gradient.start_mix[i];
  146. mix[i] = sm + (gradient.end_mix[i] - sm) * pct;
  147. }
  148. copy_mix_to_color(gradient.color);
  149. }
  150. void Mixer::update_gradient_for_planner_z() {
  151. #if ENABLED(DELTA)
  152. get_cartesian_from_steppers();
  153. update_gradient_for_z(cartes.z);
  154. #else
  155. update_gradient_for_z(planner.get_axis_position_mm(Z_AXIS));
  156. #endif
  157. }
  158. #endif // GRADIENT_MIX
  159. #endif // MIXING_EXTRUDER