2017-09-18 05:06:16 +00:00
|
|
|
/**
|
|
|
|
* Marlin 3D Printer Firmware
|
2020-02-03 14:00:57 +00:00
|
|
|
* Copyright (c) 2020 MarlinFirmware [https://github.com/MarlinFirmware/Marlin]
|
2017-09-18 05:06:16 +00:00
|
|
|
*
|
|
|
|
* Based on Sprinter and grbl.
|
2019-06-28 04:57:50 +00:00
|
|
|
* Copyright (c) 2011 Camiel Gubbels / Erik van der Zalm
|
2017-09-18 05:06:16 +00:00
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "../inc/MarlinConfig.h"
|
|
|
|
|
|
|
|
#if ENABLED(MIXING_EXTRUDER)
|
|
|
|
|
2018-10-16 08:38:57 +00:00
|
|
|
//#define MIXER_NORMALIZER_DEBUG
|
2017-09-30 21:06:43 +00:00
|
|
|
|
2018-10-16 08:38:57 +00:00
|
|
|
#include "mixing.h"
|
2017-09-18 05:06:16 +00:00
|
|
|
|
2018-10-16 08:38:57 +00:00
|
|
|
Mixer mixer;
|
2017-09-18 05:06:16 +00:00
|
|
|
|
2019-02-03 05:30:26 +00:00
|
|
|
#ifdef MIXER_NORMALIZER_DEBUG
|
|
|
|
#include "../core/serial.h"
|
|
|
|
#endif
|
|
|
|
|
2018-10-16 08:38:57 +00:00
|
|
|
// Used up to Planner level
|
2019-02-10 10:54:23 +00:00
|
|
|
uint_fast8_t Mixer::selected_vtool = 0;
|
|
|
|
float Mixer::collector[MIXING_STEPPERS]; // mix proportion. 0.0 = off, otherwise <= COLOR_A_MASK.
|
|
|
|
mixer_comp_t Mixer::color[NR_MIXING_VIRTUAL_TOOLS][MIXING_STEPPERS];
|
2017-09-18 05:06:16 +00:00
|
|
|
|
2018-10-16 08:38:57 +00:00
|
|
|
// Used in Stepper
|
2019-02-10 10:54:23 +00:00
|
|
|
int_fast8_t Mixer::runner = 0;
|
|
|
|
mixer_comp_t Mixer::s_color[MIXING_STEPPERS];
|
|
|
|
mixer_accu_t Mixer::accu[MIXING_STEPPERS] = { 0 };
|
|
|
|
|
2020-04-22 21:35:03 +00:00
|
|
|
#if EITHER(HAS_DUAL_MIXING, GRADIENT_MIX)
|
2019-02-10 10:54:23 +00:00
|
|
|
mixer_perc_t Mixer::mix[MIXING_STEPPERS];
|
|
|
|
#endif
|
2017-11-25 00:02:09 +00:00
|
|
|
|
2018-10-16 08:38:57 +00:00
|
|
|
void Mixer::normalize(const uint8_t tool_index) {
|
|
|
|
float cmax = 0;
|
|
|
|
#ifdef MIXER_NORMALIZER_DEBUG
|
|
|
|
float csum = 0;
|
|
|
|
#endif
|
|
|
|
MIXER_STEPPER_LOOP(i) {
|
2019-02-03 05:30:26 +00:00
|
|
|
const float v = collector[i];
|
|
|
|
NOLESS(cmax, v);
|
2018-10-16 08:38:57 +00:00
|
|
|
#ifdef MIXER_NORMALIZER_DEBUG
|
2019-02-03 05:30:26 +00:00
|
|
|
csum += v;
|
2017-11-25 00:02:09 +00:00
|
|
|
#endif
|
2017-09-18 05:06:16 +00:00
|
|
|
}
|
2018-10-16 08:38:57 +00:00
|
|
|
#ifdef MIXER_NORMALIZER_DEBUG
|
2019-02-03 05:30:26 +00:00
|
|
|
SERIAL_ECHOPGM("Mixer: Old relation : [ ");
|
2018-10-16 08:38:57 +00:00
|
|
|
MIXER_STEPPER_LOOP(i) {
|
2019-02-03 05:30:26 +00:00
|
|
|
SERIAL_ECHO_F(collector[i] / csum, 3);
|
2018-10-16 08:38:57 +00:00
|
|
|
SERIAL_CHAR(' ');
|
|
|
|
}
|
2019-02-03 05:32:48 +00:00
|
|
|
SERIAL_ECHOLNPGM("]");
|
2018-10-16 08:38:57 +00:00
|
|
|
#endif
|
2017-09-18 05:06:16 +00:00
|
|
|
|
2018-10-16 08:38:57 +00:00
|
|
|
// Scale all values so their maximum is COLOR_A_MASK
|
2019-02-10 10:54:23 +00:00
|
|
|
const float scale = float(COLOR_A_MASK) / cmax;
|
|
|
|
MIXER_STEPPER_LOOP(i) color[tool_index][i] = collector[i] * scale;
|
2017-09-18 05:06:16 +00:00
|
|
|
|
2018-10-16 08:38:57 +00:00
|
|
|
#ifdef MIXER_NORMALIZER_DEBUG
|
|
|
|
csum = 0;
|
2019-02-03 05:30:26 +00:00
|
|
|
SERIAL_ECHOPGM("Mixer: Normalize to : [ ");
|
2018-10-16 08:38:57 +00:00
|
|
|
MIXER_STEPPER_LOOP(i) {
|
|
|
|
SERIAL_ECHO(uint16_t(color[tool_index][i]));
|
|
|
|
SERIAL_CHAR(' ');
|
|
|
|
csum += color[tool_index][i];
|
2017-09-18 05:06:16 +00:00
|
|
|
}
|
2018-10-16 08:38:57 +00:00
|
|
|
SERIAL_ECHOLNPGM("]");
|
2019-02-03 05:30:26 +00:00
|
|
|
SERIAL_ECHOPGM("Mixer: New relation : [ ");
|
2018-10-16 08:38:57 +00:00
|
|
|
MIXER_STEPPER_LOOP(i) {
|
|
|
|
SERIAL_ECHO_F(uint16_t(color[tool_index][i]) / csum, 3);
|
|
|
|
SERIAL_CHAR(' ');
|
2017-09-18 05:06:16 +00:00
|
|
|
}
|
2018-10-16 08:38:57 +00:00
|
|
|
SERIAL_ECHOLNPGM("]");
|
|
|
|
#endif
|
2019-02-10 10:54:23 +00:00
|
|
|
|
2020-04-22 21:35:03 +00:00
|
|
|
TERN_(GRADIENT_MIX, refresh_gradient());
|
2018-10-16 08:38:57 +00:00
|
|
|
}
|
|
|
|
|
2019-02-03 05:30:26 +00:00
|
|
|
void Mixer::reset_vtools() {
|
2018-10-16 08:38:57 +00:00
|
|
|
// Virtual Tools 0, 1, 2, 3 = Filament 1, 2, 3, 4, etc.
|
|
|
|
// Every virtual tool gets a pure filament
|
2020-04-21 17:49:22 +00:00
|
|
|
LOOP_L_N(t, _MIN(MIXING_VIRTUAL_TOOLS, MIXING_STEPPERS))
|
2018-10-16 08:38:57 +00:00
|
|
|
MIXER_STEPPER_LOOP(i)
|
|
|
|
color[t][i] = (t == i) ? COLOR_A_MASK : 0;
|
|
|
|
|
|
|
|
// Remaining virtual tools are 100% filament 1
|
2019-02-03 05:30:26 +00:00
|
|
|
#if MIXING_VIRTUAL_TOOLS > MIXING_STEPPERS
|
2020-03-14 04:18:16 +00:00
|
|
|
LOOP_S_L_N(t, MIXING_STEPPERS, MIXING_VIRTUAL_TOOLS)
|
2018-10-16 08:38:57 +00:00
|
|
|
MIXER_STEPPER_LOOP(i)
|
|
|
|
color[t][i] = (i == 0) ? COLOR_A_MASK : 0;
|
|
|
|
#endif
|
2019-02-03 05:30:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// called at boot
|
|
|
|
void Mixer::init() {
|
|
|
|
|
|
|
|
reset_vtools();
|
2018-10-16 08:38:57 +00:00
|
|
|
|
2020-04-19 07:01:30 +00:00
|
|
|
#if HAS_MIXER_SYNC_CHANNEL
|
2018-10-16 08:38:57 +00:00
|
|
|
// AUTORETRACT_TOOL gets the same amount of all filaments
|
|
|
|
MIXER_STEPPER_LOOP(i)
|
|
|
|
color[MIXER_AUTORETRACT_TOOL][i] = COLOR_A_MASK;
|
|
|
|
#endif
|
|
|
|
|
2019-02-03 05:30:26 +00:00
|
|
|
ZERO(collector);
|
2019-02-10 10:54:23 +00:00
|
|
|
|
2020-04-22 21:35:03 +00:00
|
|
|
#if EITHER(HAS_DUAL_MIXING, GRADIENT_MIX)
|
2019-02-10 10:54:23 +00:00
|
|
|
update_mix_from_vtool();
|
|
|
|
#endif
|
|
|
|
|
2020-04-22 21:35:03 +00:00
|
|
|
TERN_(GRADIENT_MIX, update_gradient_for_planner_z());
|
2018-10-16 08:38:57 +00:00
|
|
|
}
|
2017-09-18 05:06:16 +00:00
|
|
|
|
2019-05-02 02:55:58 +00:00
|
|
|
void Mixer::refresh_collector(const float proportion/*=1.0*/, const uint8_t t/*=selected_vtool*/, float (&c)[MIXING_STEPPERS]/*=collector*/) {
|
2019-02-10 10:54:23 +00:00
|
|
|
float csum = 0, cmax = 0;
|
|
|
|
MIXER_STEPPER_LOOP(i) {
|
|
|
|
const float v = color[t][i];
|
2019-07-05 23:01:21 +00:00
|
|
|
cmax = _MAX(cmax, v);
|
2019-02-10 10:54:23 +00:00
|
|
|
csum += v;
|
|
|
|
}
|
2019-03-05 12:46:19 +00:00
|
|
|
//SERIAL_ECHOPAIR("Mixer::refresh_collector(", proportion, ", ", int(t), ") cmax=", cmax, " csum=", csum, " color");
|
2019-02-10 10:54:23 +00:00
|
|
|
const float inv_prop = proportion / csum;
|
|
|
|
MIXER_STEPPER_LOOP(i) {
|
2019-05-02 02:55:58 +00:00
|
|
|
c[i] = color[t][i] * inv_prop;
|
|
|
|
//SERIAL_ECHOPAIR(" [", int(t), "][", int(i), "] = ", int(color[t][i]), " (", c[i], ") ");
|
2019-02-10 10:54:23 +00:00
|
|
|
}
|
|
|
|
//SERIAL_EOL();
|
|
|
|
}
|
|
|
|
|
|
|
|
#if ENABLED(GRADIENT_MIX)
|
|
|
|
|
|
|
|
#include "../module/motion.h"
|
|
|
|
#include "../module/planner.h"
|
|
|
|
|
|
|
|
gradient_t Mixer::gradient = {
|
|
|
|
false, // enabled
|
|
|
|
{0}, // color (array)
|
|
|
|
0, 0, // start_z, end_z
|
|
|
|
0, 1, // start_vtool, end_vtool
|
|
|
|
{0}, {0} // start_mix[], end_mix[]
|
|
|
|
#if ENABLED(GRADIENT_VTOOL)
|
|
|
|
, -1 // vtool_index
|
|
|
|
#endif
|
|
|
|
};
|
|
|
|
|
|
|
|
float Mixer::prev_z; // = 0
|
|
|
|
|
|
|
|
void Mixer::update_gradient_for_z(const float z) {
|
|
|
|
if (z == prev_z) return;
|
|
|
|
prev_z = z;
|
|
|
|
|
|
|
|
const float slice = gradient.end_z - gradient.start_z;
|
|
|
|
|
|
|
|
float pct = (z - gradient.start_z) / slice;
|
|
|
|
NOLESS(pct, 0.0f); NOMORE(pct, 1.0f);
|
|
|
|
|
|
|
|
MIXER_STEPPER_LOOP(i) {
|
|
|
|
const mixer_perc_t sm = gradient.start_mix[i];
|
|
|
|
mix[i] = sm + (gradient.end_mix[i] - sm) * pct;
|
|
|
|
}
|
|
|
|
|
|
|
|
copy_mix_to_color(gradient.color);
|
|
|
|
}
|
|
|
|
|
|
|
|
void Mixer::update_gradient_for_planner_z() {
|
|
|
|
update_gradient_for_z(planner.get_axis_position_mm(Z_AXIS));
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif // GRADIENT_MIX
|
|
|
|
|
2017-09-18 05:06:16 +00:00
|
|
|
#endif // MIXING_EXTRUDER
|