Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

FT_MOTION : update preprocessor directives #27359

Merged
merged 12 commits into from
Aug 24, 2024
10 changes: 9 additions & 1 deletion Marlin/src/inc/SanityCheck.h
Original file line number Diff line number Diff line change
Expand Up @@ -4380,8 +4380,16 @@ static_assert(_PLUS_TEST(3), "DEFAULT_MAX_ACCELERATION values must be positive."
#elif DISABLED(FTM_UNIFIED_BWS)
#error "FT_MOTION requires FTM_UNIFIED_BWS to be enabled because FBS is not yet implemented."
#endif
#if !HAS_X_AXIS && !HAS_Y_AXIS
#warning "FT_MOTION shaping needs an X or an Y axis."
#else
#define FTM_SHAPING
#endif
thinkyhead marked this conversation as resolved.
Show resolved Hide resolved
#if !HAS_X_AXIS
static_assert(FTM_DEFAULT_SHAPER_X != ftMotionShaper_NONE, "Without any linear axes FTM_DEFAULT_SHAPER_X must be ftMotionShaper_NONE.");
static_assert(FTM_DEFAULT_SHAPER_X != ftMotionShaper_NONE, "Without X axis FTM_DEFAULT_SHAPER_X must be ftMotionShaper_NONE.");
#endif
#if !HAS_Y_AXIS
static_assert(FTM_DEFAULT_SHAPER_Y != ftMotionShaper_NONE, "Without Y axis FTM_DEFAULT_SHAPER_Y must be ftMotionShaper_NONE.");
thinkyhead marked this conversation as resolved.
Show resolved Hide resolved
#endif
#if HAS_DYNAMIC_FREQ_MM
static_assert(FTM_DEFAULT_DYNFREQ_MODE != dynFreqMode_Z_BASED, "dynFreqMode_Z_BASED requires a Z axis.");
Expand Down
20 changes: 10 additions & 10 deletions Marlin/src/lcd/menu/menu_motion.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -323,7 +323,6 @@ void menu_move() {
#if ENABLED(FT_MOTION_MENU)

#include "../../module/ft_motion.h"
#include "../../gcode/gcode.h"

FSTR_P get_shaper_name(const AxisEnum axis=X_AXIS) {
switch (ftMotion.cfg.shaper[axis]) {
Expand Down Expand Up @@ -417,10 +416,9 @@ void menu_move() {
void menu_ft_motion() {
// Define stuff ahead of the menu loop
MString<20> shaper_name[NUM_AXES_SHAPED] {};
#if HAS_X_AXIS
for (uint_fast8_t a = X_AXIS; a < NUM_AXES_SHAPED; ++a)
shaper_name[a] = get_shaper_name(AxisEnum(a));
#endif
TERN0(HAS_X_AXIS,shaper_name[X_AXIS] = get_shaper_name(X_AXIS);)
TERN0(HAS_Y_AXIS,shaper_name[Y_AXIS] = get_shaper_name(Y_AXIS);)

thinkyhead marked this conversation as resolved.
Show resolved Hide resolved
#if HAS_DYNAMIC_FREQ
MString<20> dmode = get_dyn_freq_mode_name();
#endif
Expand Down Expand Up @@ -484,14 +482,15 @@ void menu_move() {
void menu_tune_ft_motion() {
// Define stuff ahead of the menu loop
MString<20> shaper_name[NUM_AXES_SHAPED] {};
#if HAS_X_AXIS
for (uint_fast8_t a = X_AXIS; a < NUM_AXES_SHAPED; ++a)
shaper_name[a] = get_shaper_name(AxisEnum(a));
#endif
TERN0(HAS_X_AXIS, shaper_name[X_AXIS] = get_shaper_name(X_AXIS);)
TERN0(HAS_Y_AXIS, shaper_name[Y_AXIS] = get_shaper_name(Y_AXIS);)

#if HAS_DYNAMIC_FREQ
MString<20> dmode = get_dyn_freq_mode_name();
#endif

ft_config_t &c = ftMotion.cfg;

START_MENU();

#if HAS_X_AXIS
Expand All @@ -508,7 +507,8 @@ void menu_move() {
MENU_ITEM_ADDON_START_RJ(dmode.length()); lcd_put_u8str(dmode); MENU_ITEM_ADDON_END();
#endif
#if HAS_EXTRUDERS
EDIT_ITEM(bool, MSG_LINEAR_ADVANCE, &ftMotion.cfg.linearAdvEna);
EDIT_ITEM(bool, MSG_LINEAR_ADVANCE, &c.linearAdvEna);
if (c.linearAdvEna) EDIT_ITEM(float42_52, MSG_ADVANCE_K, &c.linearAdvK, 0, 10);
thinkyhead marked this conversation as resolved.
Show resolved Hide resolved
#endif

END_MENU();
Expand Down
7 changes: 6 additions & 1 deletion Marlin/src/lcd/menu/menu_tune.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,10 @@
#include "../../module/temperature.h"
#include "../../MarlinCore.h"

#if ENABLED(FT_MOTION)
#include "../../module/ft_motion.h"
#endif

#if HAS_LEVELING
#include "../../feature/bedlevel/bedlevel.h"
#endif
Expand Down Expand Up @@ -195,7 +199,8 @@ void menu_tune() {
//
#if ENABLED(FT_MOTION_MENU)
void menu_tune_ft_motion();
SUBMENU(MSG_FIXED_TIME_MOTION, menu_tune_ft_motion);
if(ftMotion.cfg.active)
thinkyhead marked this conversation as resolved.
Show resolved Hide resolved
SUBMENU(MSG_FIXED_TIME_MOTION, menu_tune_ft_motion);
#endif

//
Expand Down
34 changes: 19 additions & 15 deletions Marlin/src/module/ft_motion.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -88,10 +88,12 @@ xyze_long_t FTMotion::steps = { 0 }; // Step count accumulator.
uint32_t FTMotion::interpIdx = 0; // Index of current data point being interpolated.

// Shaping variables.
#if HAS_X_AXIS
#if ENABLED(FTM_SHAPING)
FTMotion::shaping_t FTMotion::shaping = {
0,
x:{ false, { 0.0f }, { 0.0f }, { 0 }, { 0 } }, // ena, d_zi, Ai, Ni, max_i
#if HAS_X_AXIS
x:{ false, { 0.0f }, { 0.0f }, { 0 }, { 0 } }, // ena, d_zi, Ai, Ni, max_i
#endif
#if HAS_Y_AXIS
y:{ false, { 0.0f }, { 0.0f }, { 0 }, { 0 } } // ena, d_zi, Ai, Ni, max_i
#endif
Expand Down Expand Up @@ -211,7 +213,7 @@ void FTMotion::loop() {

}

#if HAS_X_AXIS
#if ENABLED(FTM_SHAPING)

// Refresh the gains used by shaping functions.
void FTMotion::AxisShaping::set_axis_shaping_A(const ftMotionShaper_t shaper, const_float_t zeta, const_float_t vtol) {
Expand Down Expand Up @@ -362,7 +364,7 @@ void FTMotion::loop() {
#endif
}

#endif // HAS_X_AXIS
#endif // FTM_SHAPING

// Reset all trajectory processing variables.
void FTMotion::reset() {
Expand All @@ -383,8 +385,8 @@ void FTMotion::reset() {

stepper.axis_did_move.reset();

#if HAS_X_AXIS
ZERO(shaping.x.d_zi);
#if ENABLED(FTM_SHAPING)
TERN_(HAS_X_AXIS, ZERO(shaping.x.d_zi));
TERN_(HAS_Y_AXIS, ZERO(shaping.y.d_zi));
shaping.zi_idx = 0;
#endif
Expand Down Expand Up @@ -626,15 +628,17 @@ void FTMotion::makeVector() {
}

// Apply shaping if active on each axis
#if HAS_X_AXIS
if (shaping.x.ena) {
shaping.x.d_zi[shaping.zi_idx] = traj.x[makeVector_batchIdx];
traj.x[makeVector_batchIdx] *= shaping.x.Ai[0];
for (uint32_t i = 1U; i <= shaping.x.max_i; i++) {
const uint32_t udiffx = shaping.zi_idx - shaping.x.Ni[i];
traj.x[makeVector_batchIdx] += shaping.x.Ai[i] * shaping.x.d_zi[shaping.x.Ni[i] > shaping.zi_idx ? (FTM_ZMAX) + udiffx : udiffx];
#if ENABLED(FTM_SHAPING)
#if HAS_X_AXIS
if (shaping.x.ena) {
shaping.x.d_zi[shaping.zi_idx] = traj.x[makeVector_batchIdx];
traj.x[makeVector_batchIdx] *= shaping.x.Ai[0];
for (uint32_t i = 1U; i <= shaping.x.max_i; i++) {
const uint32_t udiffx = shaping.zi_idx - shaping.x.Ni[i];
traj.x[makeVector_batchIdx] += shaping.x.Ai[i] * shaping.x.d_zi[shaping.x.Ni[i] > shaping.zi_idx ? (FTM_ZMAX) + udiffx : udiffx];
}
}
}
#endif

#if HAS_Y_AXIS
if (shaping.y.ena) {
Expand All @@ -647,7 +651,7 @@ void FTMotion::makeVector() {
}
#endif
if (++shaping.zi_idx == (FTM_ZMAX)) shaping.zi_idx = 0;
#endif // HAS_X_AXIS
#endif // FTM_SHAPING

// Filled up the queue with regular and shaped steps
if (++makeVector_batchIdx == FTM_WINDOW_SIZE) {
Expand Down
90 changes: 45 additions & 45 deletions Marlin/src/module/ft_motion.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,27 +39,27 @@
typedef struct FTConfig {
bool active = ENABLED(FTM_IS_DEFAULT_MOTION); // Active (else standard motion)

#if HAS_X_AXIS
ft_shaped_shaper_t shaper = // Shaper type
{ SHAPED_ELEM(FTM_DEFAULT_SHAPER_X, FTM_DEFAULT_SHAPER_Y) };
ft_shaped_float_t baseFreq = // Base frequency. [Hz]
{ SHAPED_ELEM(FTM_SHAPING_DEFAULT_X_FREQ, FTM_SHAPING_DEFAULT_Y_FREQ) };
ft_shaped_float_t zeta = // Damping factor
{ SHAPED_ELEM(FTM_SHAPING_ZETA_X, FTM_SHAPING_ZETA_Y) };
ft_shaped_float_t vtol = // Vibration Level
{ SHAPED_ELEM(FTM_SHAPING_V_TOL_X, FTM_SHAPING_V_TOL_Y) };
#endif

#if HAS_DYNAMIC_FREQ
dynFreqMode_t dynFreqMode = FTM_DEFAULT_DYNFREQ_MODE; // Dynamic frequency mode configuration.
ft_shaped_float_t dynFreqK = { 0.0f }; // Scaling / gain for dynamic frequency. [Hz/mm] or [Hz/g]
#else
static constexpr dynFreqMode_t dynFreqMode = dynFreqMode_DISABLED;
#endif
#if ENABLED(FTM_SHAPING)
ft_shaped_shaper_t shaper = // Shaper type
{ TERN(HAS_X_AXIS, FTM_DEFAULT_SHAPER_X, ftMotionShaper_NONE), TERN(HAS_Y_AXIS, FTM_DEFAULT_SHAPER_Y, ftMotionShaper_NONE) };
ft_shaped_float_t baseFreq = // Base frequency. [Hz]
{ FTM_SHAPING_DEFAULT_X_FREQ, FTM_SHAPING_DEFAULT_Y_FREQ };
ft_shaped_float_t zeta = // Damping factor
{ FTM_SHAPING_ZETA_X, FTM_SHAPING_ZETA_Y };
ft_shaped_float_t vtol = // Vibration Level
{ FTM_SHAPING_V_TOL_X, FTM_SHAPING_V_TOL_Y };

#if HAS_DYNAMIC_FREQ
dynFreqMode_t dynFreqMode = FTM_DEFAULT_DYNFREQ_MODE; // Dynamic frequency mode configuration.
ft_shaped_float_t dynFreqK = { 0.0f }; // Scaling / gain for dynamic frequency. [Hz/mm] or [Hz/g]
#else
static constexpr dynFreqMode_t dynFreqMode = dynFreqMode_DISABLED;
#endif

#if HAS_EXTRUDERS
bool linearAdvEna = FTM_LINEAR_ADV_DEFAULT_ENA; // Linear advance enable configuration.
float linearAdvK = FTM_LINEAR_ADV_DEFAULT_K; // Linear advance gain.
#if HAS_EXTRUDERS
bool linearAdvEna = FTM_LINEAR_ADV_DEFAULT_ENA; // Linear advance enable configuration.
float linearAdvK = FTM_LINEAR_ADV_DEFAULT_K; // Linear advance gain.
#endif
#endif
} ft_config_t;

Expand All @@ -74,33 +74,34 @@ class FTMotion {
static void set_defaults() {
cfg.active = ENABLED(FTM_IS_DEFAULT_MOTION);

#if HAS_X_AXIS
cfg.shaper.x = FTM_DEFAULT_SHAPER_X;
cfg.baseFreq.x = FTM_SHAPING_DEFAULT_X_FREQ;
cfg.zeta.x = FTM_SHAPING_ZETA_X;
cfg.vtol.x = FTM_SHAPING_V_TOL_X;
#endif
#if ENABLED(FTM_SHAPING)
#if HAS_X_AXIS
cfg.shaper.x = FTM_DEFAULT_SHAPER_X;
cfg.baseFreq.x = FTM_SHAPING_DEFAULT_X_FREQ;
cfg.zeta.x = FTM_SHAPING_ZETA_X;
cfg.vtol.x = FTM_SHAPING_V_TOL_X;
#endif

#if HAS_Y_AXIS
cfg.shaper.y = FTM_DEFAULT_SHAPER_Y;
cfg.baseFreq.y = FTM_SHAPING_DEFAULT_Y_FREQ;
cfg.zeta.y = FTM_SHAPING_ZETA_Y;
cfg.vtol.y = FTM_SHAPING_V_TOL_Y;
#endif
#if HAS_Y_AXIS
cfg.shaper.y = FTM_DEFAULT_SHAPER_Y;
cfg.baseFreq.y = FTM_SHAPING_DEFAULT_Y_FREQ;
cfg.zeta.y = FTM_SHAPING_ZETA_Y;
cfg.vtol.y = FTM_SHAPING_V_TOL_Y;
#endif

#if HAS_DYNAMIC_FREQ
cfg.dynFreqMode = FTM_DEFAULT_DYNFREQ_MODE;
cfg.dynFreqK[X_AXIS] = TERN_(HAS_Y_AXIS, cfg.dynFreqK[Y_AXIS]) = 0.0f;
#endif

#if HAS_DYNAMIC_FREQ
cfg.dynFreqMode = FTM_DEFAULT_DYNFREQ_MODE;
TERN_(HAS_X_AXIS, cfg.dynFreqK.x = 0.0f);
TERN_(HAS_Y_AXIS, cfg.dynFreqK.y = 0.0f);
update_shaping_params();
#endif

#if HAS_EXTRUDERS
cfg.linearAdvEna = FTM_LINEAR_ADV_DEFAULT_ENA;
cfg.linearAdvK = FTM_LINEAR_ADV_DEFAULT_K;
#endif

TERN_(HAS_X_AXIS, update_shaping_params());

reset();
}

Expand All @@ -114,7 +115,7 @@ class FTMotion {
static void init();
static void loop(); // Controller main, to be invoked from non-isr task.

#if HAS_X_AXIS
#if ENABLED(FTM_SHAPING)
// Refresh gains and indices used by shaping functions.
static void update_shaping_params(void);
#endif
Expand Down Expand Up @@ -155,7 +156,7 @@ class FTMotion {
static xyze_long_t steps;

// Shaping variables.
#if HAS_X_AXIS
#if ENABLED(FTM_SHAPING)

typedef struct AxisShaping {
bool ena = false; // Enabled indication.
Expand All @@ -171,16 +172,15 @@ class FTMotion {

typedef struct Shaping {
uint32_t zi_idx; // Index of storage in the data point delay vectors.
axis_shaping_t x;
#if HAS_Y_AXIS
axis_shaping_t y;
#endif

TERN0(HAS_X_AXIS, axis_shaping_t x;)
TERN0(HAS_Y_AXIS, axis_shaping_t y;)
thinkyhead marked this conversation as resolved.
Show resolved Hide resolved

} shaping_t;

static shaping_t shaping; // Shaping data

#endif // HAS_X_AXIS
#endif // FTM_SHAPING

// Linear advance variables.
#if HAS_EXTRUDERS
Expand Down
10 changes: 6 additions & 4 deletions Marlin/src/module/ft_types.h
Original file line number Diff line number Diff line change
Expand Up @@ -59,14 +59,16 @@ enum {
FT_BIT_COUNT
};

#define NUM_AXES_SHAPED TERN(HAS_Y_AXIS, 2, 1)
#define SHAPED_ELEM(A, B) A OPTARG(HAS_Y_AXIS, B)
#if ENABLED(FTM_SHAPING)
#define NUM_AXES_SHAPED 2
#endif
//#define SHAPED_ELEM(A, B) A OPTARG(HAS_Y_AXIS, B)

template<typename T>
struct FTShapedAxes {
union {
struct { T SHAPED_ELEM(X, Y); };
struct { T SHAPED_ELEM(x, y); };
struct { T X, Y; };
struct { T x, y; };
T val[NUM_AXES_SHAPED];
thinkyhead marked this conversation as resolved.
Show resolved Hide resolved
};
T& operator[](int i) { return val[i]; }
Expand Down