2017-09-06 06:28:31 -05:00
|
|
|
/**
|
|
|
|
* Marlin 3D Printer Firmware
|
2019-06-27 23:57:50 -05:00
|
|
|
* Copyright (c) 2019 MarlinFirmware [https://github.com/MarlinFirmware/Marlin]
|
2017-09-06 06:28:31 -05:00
|
|
|
*
|
|
|
|
* Based on Sprinter and grbl.
|
2019-06-27 23:57:50 -05:00
|
|
|
* Copyright (c) 2011 Camiel Gubbels / Erik van der Zalm
|
2017-09-06 06:28:31 -05: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/>.
|
|
|
|
*
|
|
|
|
*/
|
2018-10-16 03:38:57 -05:00
|
|
|
#pragma once
|
2017-09-18 00:06:16 -05:00
|
|
|
|
|
|
|
#include "../inc/MarlinConfig.h"
|
|
|
|
|
2019-02-10 04:54:23 -06:00
|
|
|
//#define MIXER_NORMALIZER_DEBUG
|
|
|
|
|
2019-06-13 03:45:46 -05:00
|
|
|
#ifndef __AVR__ // || DUAL_MIXING_EXTRUDER
|
2019-02-10 04:54:23 -06:00
|
|
|
// Use 16-bit (or fastest) data for the integer mix factors
|
|
|
|
typedef uint_fast16_t mixer_comp_t;
|
|
|
|
typedef uint_fast16_t mixer_accu_t;
|
|
|
|
#define COLOR_A_MASK 0x8000
|
|
|
|
#define COLOR_MASK 0x7FFF
|
|
|
|
#else
|
|
|
|
// Use 8-bit data for the integer mix factors
|
|
|
|
// Exactness is sacrificed for speed
|
2018-10-16 03:38:57 -05:00
|
|
|
#define MIXER_ACCU_SIGNED
|
2019-02-02 23:30:26 -06:00
|
|
|
typedef uint8_t mixer_comp_t;
|
2018-10-16 03:38:57 -05:00
|
|
|
typedef int8_t mixer_accu_t;
|
2018-11-01 14:38:45 -05:00
|
|
|
#define COLOR_A_MASK 0x80
|
|
|
|
#define COLOR_MASK 0x7F
|
2017-09-18 00:06:16 -05:00
|
|
|
#endif
|
|
|
|
|
2019-02-10 04:54:23 -06:00
|
|
|
typedef int8_t mixer_perc_t;
|
|
|
|
|
2018-10-16 03:38:57 -05:00
|
|
|
#ifndef MIXING_VIRTUAL_TOOLS
|
|
|
|
#define MIXING_VIRTUAL_TOOLS 1
|
|
|
|
#endif
|
2017-09-18 00:06:16 -05:00
|
|
|
|
2019-02-10 04:54:23 -06:00
|
|
|
enum MixTool {
|
|
|
|
FIRST_USER_VIRTUAL_TOOL = 0,
|
|
|
|
LAST_USER_VIRTUAL_TOOL = MIXING_VIRTUAL_TOOLS - 1,
|
|
|
|
NR_USER_VIRTUAL_TOOLS,
|
2019-05-01 21:55:58 -05:00
|
|
|
MIXER_DIRECT_SET_TOOL = NR_USER_VIRTUAL_TOOLS,
|
|
|
|
#if ENABLED(RETRACT_SYNC_MIXING)
|
|
|
|
MIXER_AUTORETRACT_TOOL,
|
2018-11-01 14:38:45 -05:00
|
|
|
#endif
|
2019-05-01 21:55:58 -05:00
|
|
|
NR_MIXING_VIRTUAL_TOOLS
|
2019-02-10 04:54:23 -06:00
|
|
|
};
|
|
|
|
|
2019-05-01 21:55:58 -05:00
|
|
|
#if ENABLED(RETRACT_SYNC_MIXING)
|
|
|
|
#define MAX_VTOOLS 254
|
2018-10-16 03:38:57 -05:00
|
|
|
#else
|
2019-05-01 21:55:58 -05:00
|
|
|
#define MAX_VTOOLS 255
|
2017-09-18 00:06:16 -05:00
|
|
|
#endif
|
2019-05-01 21:55:58 -05:00
|
|
|
static_assert(NR_MIXING_VIRTUAL_TOOLS <= MAX_VTOOLS, "MIXING_VIRTUAL_TOOLS must be <= " STRINGIFY(MAX_VTOOLS) "!");
|
2017-09-18 00:06:16 -05:00
|
|
|
|
2018-10-16 03:38:57 -05:00
|
|
|
#define MIXER_STEPPER_LOOP(VAR) \
|
|
|
|
for (uint_fast8_t VAR = 0; VAR < MIXING_STEPPERS; VAR++)
|
|
|
|
|
2019-02-10 04:54:23 -06:00
|
|
|
#define MIXER_BLOCK_FIELD mixer_comp_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)
|
|
|
|
|
|
|
|
#if ENABLED(GRADIENT_MIX)
|
|
|
|
|
|
|
|
typedef struct {
|
|
|
|
bool enabled; // This gradient is enabled
|
|
|
|
mixer_comp_t color[MIXING_STEPPERS]; // The current gradient color
|
|
|
|
float start_z, end_z; // Region for gradient
|
|
|
|
int8_t start_vtool, end_vtool; // Start and end virtual tools
|
|
|
|
mixer_perc_t start_mix[MIXING_STEPPERS], // Start and end mixes from those tools
|
|
|
|
end_mix[MIXING_STEPPERS];
|
|
|
|
#if ENABLED(GRADIENT_VTOOL)
|
|
|
|
int8_t vtool_index; // Use this virtual tool number as index
|
|
|
|
#endif
|
|
|
|
} gradient_t;
|
|
|
|
|
|
|
|
#endif
|
2018-10-16 03:38:57 -05:00
|
|
|
|
2019-02-02 23:30:26 -06:00
|
|
|
/**
|
|
|
|
* @brief Mixer class
|
|
|
|
* @details Contains data and behaviors for a Mixing Extruder
|
|
|
|
*/
|
2018-10-16 03:38:57 -05:00
|
|
|
class Mixer {
|
|
|
|
public:
|
|
|
|
|
2019-02-10 04:54:23 -06:00
|
|
|
static float collector[MIXING_STEPPERS]; // M163 components, also editable from LCD
|
|
|
|
|
|
|
|
static void init(); // Populate colors at boot time
|
2019-02-02 23:30:26 -06:00
|
|
|
|
|
|
|
static void reset_vtools();
|
2019-05-01 21:55:58 -05:00
|
|
|
static void refresh_collector(const float proportion=1.0, const uint8_t t=selected_vtool, float (&c)[MIXING_STEPPERS]=collector);
|
2018-10-16 03:38:57 -05:00
|
|
|
|
|
|
|
// Used up to Planner level
|
2019-07-05 18:01:21 -05:00
|
|
|
FORCE_INLINE static void set_collector(const uint8_t c, const float f) { collector[c] = _MAX(f, 0.0f); }
|
2019-02-10 04:54:23 -06:00
|
|
|
|
2018-10-16 03:38:57 -05:00
|
|
|
static void normalize(const uint8_t tool_index);
|
2019-02-10 04:54:23 -06:00
|
|
|
FORCE_INLINE static void normalize() { normalize(selected_vtool); }
|
|
|
|
|
2019-02-02 23:30:26 -06:00
|
|
|
FORCE_INLINE static uint8_t get_current_vtool() { return selected_vtool; }
|
2019-02-10 04:54:23 -06:00
|
|
|
|
|
|
|
FORCE_INLINE static void T(const uint_fast8_t c) {
|
|
|
|
selected_vtool = c;
|
|
|
|
#if ENABLED(GRADIENT_VTOOL)
|
|
|
|
refresh_gradient();
|
|
|
|
#endif
|
|
|
|
#if DUAL_MIXING_EXTRUDER
|
|
|
|
update_mix_from_vtool();
|
|
|
|
#endif
|
|
|
|
}
|
2018-10-16 03:38:57 -05:00
|
|
|
|
|
|
|
// Used when dealing with blocks
|
2019-02-10 04:54:23 -06:00
|
|
|
FORCE_INLINE static void populate_block(mixer_comp_t b_color[MIXING_STEPPERS]) {
|
|
|
|
#if ENABLED(GRADIENT_MIX)
|
|
|
|
if (gradient.enabled) {
|
|
|
|
MIXER_STEPPER_LOOP(i) b_color[i] = gradient.color[i];
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
MIXER_STEPPER_LOOP(i) b_color[i] = color[selected_vtool][i];
|
2018-10-16 03:38:57 -05:00
|
|
|
}
|
2019-02-10 04:54:23 -06:00
|
|
|
|
|
|
|
FORCE_INLINE static void stepper_setup(mixer_comp_t b_color[MIXING_STEPPERS]) {
|
|
|
|
MIXER_STEPPER_LOOP(i) s_color[i] = b_color[i];
|
|
|
|
}
|
|
|
|
|
|
|
|
#if DUAL_MIXING_EXTRUDER || ENABLED(GRADIENT_MIX)
|
|
|
|
|
|
|
|
static mixer_perc_t mix[MIXING_STEPPERS]; // Scratch array for the Mix in proportion to 100
|
|
|
|
|
|
|
|
static inline void copy_mix_to_color(mixer_comp_t (&tcolor)[MIXING_STEPPERS]) {
|
|
|
|
// Scale each component to the largest one in terms of COLOR_A_MASK
|
|
|
|
// So the largest component will be COLOR_A_MASK and the other will be in proportion to it
|
2019-07-05 18:01:21 -05:00
|
|
|
const float scale = (COLOR_A_MASK) * RECIPROCAL(float(_MAX(mix[0], mix[1])));
|
2019-02-10 04:54:23 -06:00
|
|
|
|
|
|
|
// Scale all values so their maximum is COLOR_A_MASK
|
|
|
|
MIXER_STEPPER_LOOP(i) tcolor[i] = mix[i] * scale;
|
|
|
|
|
|
|
|
#ifdef MIXER_NORMALIZER_DEBUG
|
2019-03-05 06:46:19 -06:00
|
|
|
SERIAL_ECHOLNPAIR("Mix [", int(mix[0]), ", ", int(mix[1]), "] to Color [", int(tcolor[0]), ", ", int(tcolor[1]), "]");
|
2019-02-10 04:54:23 -06:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline void update_mix_from_vtool(const uint8_t j=selected_vtool) {
|
|
|
|
float ctot = 0;
|
|
|
|
MIXER_STEPPER_LOOP(i) ctot += color[j][i];
|
|
|
|
//MIXER_STEPPER_LOOP(i) mix[i] = 100.0f * color[j][i] / ctot;
|
|
|
|
mix[0] = mixer_perc_t(100.0f * color[j][0] / ctot);
|
|
|
|
mix[1] = 100 - mix[0];
|
|
|
|
#ifdef MIXER_NORMALIZER_DEBUG
|
2019-03-05 06:46:19 -06:00
|
|
|
SERIAL_ECHOLNPAIR("V-tool ", int(j), " [", int(color[j][0]), ", ", int(color[j][1]), "] to Mix [", int(mix[0]), ", ", int(mix[1]), "]");
|
2019-02-10 04:54:23 -06:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif // DUAL_MIXING_EXTRUDER || GRADIENT_MIX
|
|
|
|
|
|
|
|
#if DUAL_MIXING_EXTRUDER
|
|
|
|
|
|
|
|
// Update the virtual tool from an edited mix
|
|
|
|
static inline void update_vtool_from_mix() {
|
|
|
|
copy_mix_to_color(color[selected_vtool]);
|
|
|
|
#if ENABLED(GRADIENT_MIX)
|
|
|
|
refresh_gradient();
|
|
|
|
#endif
|
|
|
|
// MIXER_STEPPER_LOOP(i) collector[i] = mix[i];
|
|
|
|
// normalize();
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif // DUAL_MIXING_EXTRUDER
|
|
|
|
|
|
|
|
#if ENABLED(GRADIENT_MIX)
|
|
|
|
|
|
|
|
static gradient_t gradient;
|
|
|
|
static float prev_z;
|
|
|
|
|
|
|
|
// Update the current mix from the gradient for a given Z
|
|
|
|
static void update_gradient_for_z(const float z);
|
|
|
|
static void update_gradient_for_planner_z();
|
|
|
|
static inline void gradient_control(const float z) {
|
|
|
|
if (gradient.enabled) {
|
|
|
|
if (z >= gradient.end_z)
|
|
|
|
T(gradient.end_vtool);
|
|
|
|
else
|
|
|
|
update_gradient_for_z(z);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline void update_mix_from_gradient() {
|
|
|
|
float ctot = 0;
|
|
|
|
MIXER_STEPPER_LOOP(i) ctot += gradient.color[i];
|
|
|
|
mix[0] = (mixer_perc_t)CEIL(100.0f * gradient.color[0] / ctot);
|
|
|
|
mix[1] = 100 - mix[0];
|
|
|
|
#ifdef MIXER_NORMALIZER_DEBUG
|
2019-03-05 06:46:19 -06:00
|
|
|
SERIAL_ECHOLNPAIR("Gradient [", int(gradient.color[0]), ", ", int(gradient.color[1]), "] to Mix [", int(mix[0]), ", ", int(mix[1]), "]");
|
2019-02-10 04:54:23 -06:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
// Refresh the gradient after a change
|
|
|
|
static void refresh_gradient() {
|
|
|
|
#if ENABLED(GRADIENT_VTOOL)
|
2019-03-24 20:02:28 -05:00
|
|
|
const bool is_grd = (gradient.vtool_index == -1 || selected_vtool == (uint8_t)gradient.vtool_index);
|
2019-02-10 04:54:23 -06:00
|
|
|
#else
|
|
|
|
constexpr bool is_grd = true;
|
|
|
|
#endif
|
|
|
|
gradient.enabled = is_grd && gradient.start_vtool != gradient.end_vtool && gradient.start_z < gradient.end_z;
|
|
|
|
if (gradient.enabled) {
|
|
|
|
mixer_perc_t mix_bak[MIXING_STEPPERS];
|
|
|
|
COPY(mix_bak, mix);
|
|
|
|
update_mix_from_vtool(gradient.start_vtool);
|
|
|
|
COPY(gradient.start_mix, mix);
|
|
|
|
update_mix_from_vtool(gradient.end_vtool);
|
|
|
|
COPY(gradient.end_mix, mix);
|
|
|
|
update_gradient_for_planner_z();
|
|
|
|
COPY(mix, mix_bak);
|
|
|
|
prev_z = -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif // GRADIENT_MIX
|
2018-10-16 03:38:57 -05:00
|
|
|
|
|
|
|
// Used in Stepper
|
2019-02-02 23:30:26 -06:00
|
|
|
FORCE_INLINE static uint8_t get_stepper() { return runner; }
|
|
|
|
FORCE_INLINE static uint8_t get_next_stepper() {
|
2019-02-10 04:54:23 -06:00
|
|
|
for (;;) {
|
2018-10-16 03:38:57 -05:00
|
|
|
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;
|
|
|
|
}
|
2019-02-10 04:54:23 -06:00
|
|
|
}
|
2018-10-16 03:38:57 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
|
|
|
// Used up to Planner level
|
2019-02-02 23:30:26 -06:00
|
|
|
static uint_fast8_t selected_vtool;
|
|
|
|
static mixer_comp_t color[NR_MIXING_VIRTUAL_TOOLS][MIXING_STEPPERS];
|
2018-10-16 03:38:57 -05:00
|
|
|
|
|
|
|
// Used in Stepper
|
2019-02-02 23:30:26 -06:00
|
|
|
static int_fast8_t runner;
|
|
|
|
static mixer_comp_t s_color[MIXING_STEPPERS];
|
|
|
|
static mixer_accu_t accu[MIXING_STEPPERS];
|
2018-10-16 03:38:57 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
extern Mixer mixer;
|