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

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. /**
  2. * Marlin 3D Printer Firmware
  3. * Copyright (C) 2016 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. #if ENABLED(DIRECT_MIXING_IN_G1)
  25. #include "../gcode/parser.h"
  26. #endif
  27. float mixing_factor[MIXING_STEPPERS]; // Reciprocal of mix proportion. 0.0 = off, otherwise >= 1.0.
  28. #if MIXING_VIRTUAL_TOOLS > 1
  29. float mixing_virtual_tool_mix[MIXING_VIRTUAL_TOOLS][MIXING_STEPPERS];
  30. void mixing_tools_init() {
  31. // Virtual Tools 0, 1, 2, 3 = Filament 1, 2, 3, 4, etc.
  32. for (uint8_t t = 0; t < MIXING_VIRTUAL_TOOLS && t < MIXING_STEPPERS; t++)
  33. for (uint8_t i = 0; i < MIXING_STEPPERS; i++)
  34. mixing_virtual_tool_mix[t][i] = (t == i) ? 1.0 : 0.0;
  35. // Remaining virtual tools are 100% filament 1
  36. #if MIXING_STEPPERS < MIXING_VIRTUAL_TOOLS
  37. for (uint8_t t = MIXING_STEPPERS; t < MIXING_VIRTUAL_TOOLS; t++)
  38. for (uint8_t i = 0; i < MIXING_STEPPERS; i++)
  39. mixing_virtual_tool_mix[t][i] = (i == 0) ? 1.0 : 0.0;
  40. #endif
  41. // Initialize mixing to tool 0 color
  42. for (uint8_t i = 0; i < MIXING_STEPPERS; i++)
  43. mixing_factor[i] = mixing_virtual_tool_mix[0][i];
  44. }
  45. #endif // MIXING_VIRTUAL_TOOLS > 1
  46. void normalize_mix() {
  47. float mix_total = 0.0;
  48. for (uint8_t i = 0; i < MIXING_STEPPERS; i++) mix_total += RECIPROCAL(mixing_factor[i]);
  49. // Scale all values if they don't add up to ~1.0
  50. if (!NEAR(mix_total, 1.0)) {
  51. SERIAL_PROTOCOLLNPGM("Warning: Mix factors must add up to 1.0. Scaling.");
  52. for (uint8_t i = 0; i < MIXING_STEPPERS; i++) mixing_factor[i] *= mix_total;
  53. }
  54. }
  55. #if ENABLED(DIRECT_MIXING_IN_G1)
  56. // Get mixing parameters from the GCode
  57. // The total "must" be 1.0 (but it will be normalized)
  58. // If no mix factors are given, the old mix is preserved
  59. void gcode_get_mix() {
  60. const char mixing_codes[] = { 'A', 'B', 'C', 'D', 'H', 'I' };
  61. byte mix_bits = 0;
  62. for (uint8_t i = 0; i < MIXING_STEPPERS; i++) {
  63. if (parser.seenval(mixing_codes[i])) {
  64. SBI(mix_bits, i);
  65. float v = parser.value_float();
  66. NOLESS(v, 0.0);
  67. mixing_factor[i] = RECIPROCAL(v);
  68. }
  69. }
  70. // If any mixing factors were included, clear the rest
  71. // If none were included, preserve the last mix
  72. if (mix_bits) {
  73. for (uint8_t i = 0; i < MIXING_STEPPERS; i++)
  74. if (!TEST(mix_bits, i)) mixing_factor[i] = 0.0;
  75. normalize_mix();
  76. }
  77. }
  78. #endif
  79. #endif // MIXING_EXTRUDER