1
0
mirror of https://github.com/MarlinFirmware/Marlin.git synced 2024-11-29 06:46:42 +00:00
MarlinFirmware/Marlin/src/feature/mixing.cpp

114 lines
3.4 KiB
C++
Raw Normal View History

2017-09-18 05:06:16 +00:00
/**
* Marlin 3D Printer Firmware
* Copyright (C) 2016 MarlinFirmware [https://github.com/MarlinFirmware/Marlin]
*
* Based on Sprinter and grbl.
* Copyright (C) 2011 Camiel Gubbels / Erik van der Zalm
*
* 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
#ifdef MIXER_NORMALIZER_DEBUG
#include "../core/serial.h"
2017-09-30 21:06:43 +00:00
#endif
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
2018-10-16 08:38:57 +00:00
// Used up to Planner level
uint_fast8_t Mixer::selected_v_tool = 0;
float Mixer::M163_collector[MIXING_STEPPERS]; // mix proportion. 0.0 = off, otherwise <= COLOR_A_MASK.
mixer_color_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
int_fast8_t Mixer::runner = 0;
mixer_color_t Mixer::s_color[MIXING_STEPPERS];
mixer_accu_t Mixer::accu[MIXING_STEPPERS] = { 0 };
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) {
cmax = max(cmax, M163_collector[i]);
#ifdef MIXER_NORMALIZER_DEBUG
csum += M163_collector[i];
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
SERIAL_ECHOPGM("Mixer: Relation before normalizing: [ ");
MIXER_STEPPER_LOOP(i) {
SERIAL_ECHO_F(M163_collector[i] / csum, 3);
SERIAL_CHAR(' ');
}
SERIAL_ECHOPGM("]\n");
#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
const float inverse_max = RECIPROCAL(cmax);
MIXER_STEPPER_LOOP(i)
color[tool_index][i] = M163_collector[i] * COLOR_A_MASK * inverse_max;
2017-09-18 05:06:16 +00:00
2018-10-16 08:38:57 +00:00
#ifdef MIXER_NORMALIZER_DEBUG
csum = 0;
SERIAL_ECHOPGM("Mixer: Normalizing to : [ ");
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("]");
SERIAL_ECHOPGM("Mixer: Relation after normalizing: [ ");
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
}
// called at boot
void Mixer::init( void ) {
// Virtual Tools 0, 1, 2, 3 = Filament 1, 2, 3, 4, etc.
// Every virtual tool gets a pure filament
for (uint8_t t = 0; t < MIXING_VIRTUAL_TOOLS && t < MIXING_STEPPERS; t++)
MIXER_STEPPER_LOOP(i)
color[t][i] = (t == i) ? COLOR_A_MASK : 0;
// Remaining virtual tools are 100% filament 1
#if MIXING_STEPPERS < MIXING_VIRTUAL_TOOLS
for (uint8_t t = MIXING_STEPPERS; t < MIXING_VIRTUAL_TOOLS; t++)
MIXER_STEPPER_LOOP(i)
color[t][i] = (i == 0) ? COLOR_A_MASK : 0;
#endif
#if ENABLED(RETRACT_SYNC_MIXING)
// AUTORETRACT_TOOL gets the same amount of all filaments
MIXER_STEPPER_LOOP(i)
color[MIXER_AUTORETRACT_TOOL][i] = COLOR_A_MASK;
#endif
ZERO(M163_collector);
}
2017-09-18 05:06:16 +00:00
#endif // MIXING_EXTRUDER