New Continuous Filament Mixer (#12098)

This commit is contained in:
AnHardt
2018-10-16 10:38:57 +02:00
committed by Scott Lahteine
parent 2d2cd628c6
commit f56968ba0b
14 changed files with 296 additions and 236 deletions

View File

@ -142,11 +142,8 @@ void FWRetract::retract(const bool retracting
set_destination_from_current();
#if ENABLED(RETRACT_SYNC_MIXING)
float old_mixing_factor[MIXING_STEPPERS];
for (uint8_t i = 0; i < MIXING_STEPPERS; i++) {
old_mixing_factor[i] = mixing_factor[i];
mixing_factor[i] = RECIPROCAL(MIXING_STEPPERS);
}
uint8_t old_mixing_tool = mixer.get_current_v_tool();
mixer.T(MIXER_AUTORETRACT_TOOL);
#endif
if (retracting) {
@ -196,7 +193,7 @@ void FWRetract::retract(const bool retracting
}
#if ENABLED(RETRACT_SYNC_MIXING)
COPY(mixing_factor, old_mixing_factor); // Restore original mixing factor
mixer.T(old_mixing_tool); // Restore original mixing tool
#endif
feedrate_mm_s = old_feedrate_mm_s; // Restore original feedrate

View File

@ -24,81 +24,90 @@
#if ENABLED(MIXING_EXTRUDER)
#if ENABLED(DIRECT_MIXING_IN_G1)
#include "../gcode/parser.h"
//#define MIXER_NORMALIZER_DEBUG
#ifdef MIXER_NORMALIZER_DEBUG
#include "../core/serial.h"
#endif
float mixing_factor[MIXING_STEPPERS]; // Reciprocal of mix proportion. 0.0 = off, otherwise <= 1.0. (Array must sum to 1.0.)
#include "mixing.h"
#if MIXING_VIRTUAL_TOOLS > 1
Mixer mixer;
float mixing_virtual_tool_mix[MIXING_VIRTUAL_TOOLS][MIXING_STEPPERS];
// 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];
void mixing_tools_init() {
// Virtual Tools 0, 1, 2, 3 = Filament 1, 2, 3, 4, etc.
for (uint8_t t = 0; t < MIXING_VIRTUAL_TOOLS && t < MIXING_STEPPERS; t++)
for (uint8_t i = 0; i < MIXING_STEPPERS; i++)
mixing_virtual_tool_mix[t][i] = (t == i) ? 1.0 : 0.0;
// 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 };
// Remaining virtual tools are 100% filament 1
#if MIXING_STEPPERS < MIXING_VIRTUAL_TOOLS
for (uint8_t t = MIXING_STEPPERS; t < MIXING_VIRTUAL_TOOLS; t++)
for (uint8_t i = 0; i < MIXING_STEPPERS; i++)
mixing_virtual_tool_mix[t][i] = (i == 0) ? 1.0 : 0.0;
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];
#endif
// Initialize mixing to tool 0 color
for (uint8_t i = 0; i < MIXING_STEPPERS; i++)
mixing_factor[i] = mixing_virtual_tool_mix[0][i];
}
#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
#endif // MIXING_VIRTUAL_TOOLS > 1
// 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;
void normalize_mix() {
float mix_total = 0.0;
for (uint8_t i = 0; i < MIXING_STEPPERS; i++) mix_total += mixing_factor[i];
// Scale all values if they don't add up to ~1.0
if (!NEAR(mix_total, 1.0)) {
SERIAL_PROTOCOLLNPGM("Warning: Mix factors must add up to 1.0. Scaling.");
const float inverse_sum = RECIPROCAL(mix_total);
for (uint8_t i = 0; i < MIXING_STEPPERS; i++) mixing_factor[i] *= inverse_sum;
}
#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];
}
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(' ');
}
SERIAL_ECHOLNPGM("]");
#endif
}
#if ENABLED(DIRECT_MIXING_IN_G1)
// Get mixing parameters from the GCode
// The total "must" be 1.0 (but it will be normalized)
// If no mix factors are given, the old mix is preserved
void gcode_get_mix() {
const char mixing_codes[] = { 'A', 'B'
#if MIXING_STEPPERS > 2
, 'C'
#if MIXING_STEPPERS > 3
, 'D'
#if MIXING_STEPPERS > 4
, 'H'
#if MIXING_STEPPERS > 5
, 'I'
#endif // MIXING_STEPPERS > 5
#endif // MIXING_STEPPERS > 4
#endif // MIXING_STEPPERS > 3
#endif // MIXING_STEPPERS > 2
};
byte mix_bits = 0;
for (uint8_t i = 0; i < MIXING_STEPPERS; i++) {
if (parser.seenval(mixing_codes[i])) {
SBI(mix_bits, i);
mixing_factor[i] = MAX(parser.value_float(), 0.0);
}
}
// If any mixing factors were included, clear the rest
// If none were included, preserve the last mix
if (mix_bits) {
for (uint8_t i = 0; i < MIXING_STEPPERS; i++)
if (!TEST(mix_bits, i)) mixing_factor[i] = 0.0;
normalize_mix();
}
}
#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);
}
#endif // MIXING_EXTRUDER

View File

@ -19,23 +19,88 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
#ifndef __MIXING_H__
#define __MIXING_H__
#pragma once
#include "../inc/MarlinConfig.h"
extern float mixing_factor[MIXING_STEPPERS]; // Reciprocal of mix proportion. 0.0 = off, otherwise >= 1.0.
#if MIXING_VIRTUAL_TOOLS > 1
extern float mixing_virtual_tool_mix[MIXING_VIRTUAL_TOOLS][MIXING_STEPPERS];
void mixing_tools_init();
#ifdef __AVR__
#define MIXER_ACCU_SIGNED
typedef uint8_t mixer_color_t;
typedef int8_t mixer_accu_t;
#else
typedef uint_fast16_t mixer_color_t;
typedef uint_fast16_t mixer_accu_t;
#endif
void normalize_mix();
#define COLOR_A_MASK _BV(sizeof(mixer_color_t) * 8 - 1) // 0x80 or 0x8000
#define COLOR_MASK (COLOR_A_MASK - 1) // 0x7F or 0x7FFF
#if ENABLED(DIRECT_MIXING_IN_G1)
void gcode_get_mix();
#ifndef MIXING_VIRTUAL_TOOLS
#define MIXING_VIRTUAL_TOOLS 1
#endif
#endif // __MIXING_H__
#ifdef RETRACT_SYNC_MIXING
#define NR_MIXING_VIRTUAL_TOOLS (MIXING_VIRTUAL_TOOLS + 1)
#define MIXER_AUTORETRACT_TOOL MIXING_VIRTUAL_TOOLS
#else
#define NR_MIXING_VIRTUAL_TOOLS (MIXING_VIRTUAL_TOOLS)
#endif
#define MIXER_STEPPER_LOOP(VAR) \
for (uint_fast8_t VAR = 0; VAR < MIXING_STEPPERS; VAR++)
#define MIXER_BLOCK_DEFINITION mixer_color_t b_color[MIXING_STEPPERS]
#define MIXER_POPULATE_BLOCK() mixer.populate_block(block->b_color)
#define MIXER_STEPPER_SETUP() mixer.stepper_setup(current_block->b_color)
class Mixer {
public:
static void init(void); // Populate colors at boot time
// Used up to Planner level
static void normalize(const uint8_t tool_index);
FORCE_INLINE static uint8_t get_current_v_tool(void) { return selected_v_tool; }
FORCE_INLINE static void T(const uint_fast8_t c) { selected_v_tool = c; }
FORCE_INLINE static void set_M163_collector(const uint8_t c, const float f) { M163_collector[c] = f; }
// Used when dealing with blocks
FORCE_INLINE static void populate_block(mixer_color_t b_color[]) {
uint_fast8_t j = get_current_v_tool();
MIXER_STEPPER_LOOP(i) b_color[i] = color[j][i];
}
FORCE_INLINE static void stepper_setup(mixer_color_t b_color[]) { MIXER_STEPPER_LOOP(i) s_color[i] = b_color[i]; }
// Used in Stepper
FORCE_INLINE static uint8_t get_stepper(void) { return runner; }
FORCE_INLINE static uint8_t get_next_stepper(void) {
do {
if (--runner < 0) runner = MIXING_STEPPERS - 1;
accu[runner] += s_color[runner];
if (
#ifdef MIXER_ACCU_SIGNED
accu[runner] < 0
#else
accu[runner] & COLOR_A_MASK
#endif
) {
accu[runner] &= COLOR_MASK;
return runner;
}
} while( true );
}
private:
// Used up to Planner level
static uint_fast8_t selected_v_tool;
static float M163_collector[MIXING_STEPPERS];
static mixer_color_t color[NR_MIXING_VIRTUAL_TOOLS][MIXING_STEPPERS];
// Used in Stepper
static int_fast8_t runner;
static mixer_color_t s_color[MIXING_STEPPERS];
static mixer_accu_t accu[MIXING_STEPPERS];
};
extern Mixer mixer;