Skip to content

Commit

Permalink
🐛 Fix babystep corrupting DIR
Browse files Browse the repository at this point in the history
Followup to 2538a7c
  • Loading branch information
thinkyhead committed May 8, 2023
1 parent f80e706 commit c6ea99f
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 122 deletions.
2 changes: 1 addition & 1 deletion Marlin/Configuration_adv.h
Original file line number Diff line number Diff line change
Expand Up @@ -2188,7 +2188,7 @@
//#define BABYSTEP_WITHOUT_HOMING
//#define BABYSTEP_ALWAYS_AVAILABLE // Allow babystepping at all times (not just during movement).
//#define BABYSTEP_XY // Also enable X/Y Babystepping. Not supported on DELTA!
#define BABYSTEP_INVERT_Z false // Change if Z babysteps should go the other way
//#define BABYSTEP_INVERT_Z // Enable if Z babysteps should go the other way
//#define BABYSTEP_MILLIMETER_UNITS // Specify BABYSTEP_MULTIPLICATOR_(XY|Z) in mm instead of micro-steps
#define BABYSTEP_MULTIPLICATOR_Z 1 // (steps or mm) Steps or millimeter distance for each Z babystep
#define BABYSTEP_MULTIPLICATOR_XY 1 // (steps or mm) Steps or millimeter distance for each XY babystep
Expand Down
121 changes: 35 additions & 86 deletions Marlin/src/module/stepper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3609,31 +3609,31 @@ void Stepper::report_positions() {

#if DISABLED(DELTA)

#define BABYSTEP_AXIS(AXIS, DIR, INV) do{ \
const uint8_t old_dir = _READ_DIR(AXIS); \
_ENABLE_AXIS(AXIS); \
DIR_WAIT_BEFORE(); \
_APPLY_DIR(AXIS, (DIR)^(INV)); \
DIR_WAIT_AFTER(); \
_SAVE_START(); \
_APPLY_STEP(AXIS, _STEP_STATE(AXIS), true); \
_PULSE_WAIT(); \
_APPLY_STEP(AXIS, !_STEP_STATE(AXIS), true); \
EXTRA_DIR_WAIT_BEFORE(); \
_APPLY_DIR(AXIS, old_dir); \
EXTRA_DIR_WAIT_AFTER(); \
#define BABYSTEP_AXIS(AXIS, FWD, INV) do{ \
const bool old_fwd = _READ_DIR(AXIS); \
_ENABLE_AXIS(AXIS); \
DIR_WAIT_BEFORE(); \
_APPLY_DIR(AXIS, (FWD)^(INV)); \
DIR_WAIT_AFTER(); \
_SAVE_START(); \
_APPLY_STEP(AXIS, _STEP_STATE(AXIS), true); \
_PULSE_WAIT(); \
_APPLY_STEP(AXIS, !_STEP_STATE(AXIS), true); \
EXTRA_DIR_WAIT_BEFORE(); \
_APPLY_DIR(AXIS, old_fwd); \
EXTRA_DIR_WAIT_AFTER(); \
}while(0)

#endif

#if IS_CORE

#define BABYSTEP_CORE(A, B, DIR, INV, ALT) do{ \
const xy_byte_t old_dir = { _READ_DIR(A), _READ_DIR(B) }; \
#define BABYSTEP_CORE(A, B, FWD, INV, ALT) do{ \
const xy_byte_t old_fwd = { _READ_DIR(A), _READ_DIR(B) }; \
_ENABLE_AXIS(A); _ENABLE_AXIS(B); \
DIR_WAIT_BEFORE(); \
_APPLY_DIR(A, (DIR)^(INV)); \
_APPLY_DIR(B, (DIR)^(INV)^(ALT)); \
_APPLY_DIR(A, (FWD)^(INV)); \
_APPLY_DIR(B, (FWD)^(INV)^(ALT)); \
DIR_WAIT_AFTER(); \
_SAVE_START(); \
_APPLY_STEP(A, _STEP_STATE(A), true); \
Expand All @@ -3642,7 +3642,7 @@ void Stepper::report_positions() {
_APPLY_STEP(A, !_STEP_STATE(A), true); \
_APPLY_STEP(B, !_STEP_STATE(B), true); \
EXTRA_DIR_WAIT_BEFORE(); \
_APPLY_DIR(A, old_dir.a); _APPLY_DIR(B, old_dir.b); \
_APPLY_DIR(A, old_fwd.a); _APPLY_DIR(B, old_fwd.b); \
EXTRA_DIR_WAIT_AFTER(); \
}while(0)

Expand Down Expand Up @@ -3683,104 +3683,53 @@ void Stepper::report_positions() {
case Z_AXIS: {

#if CORE_IS_XZ
BABYSTEP_CORE(X, Z, direction, BABYSTEP_INVERT_Z, (CORESIGN(1)>0));
BABYSTEP_CORE(X, Z, direction, ENABLED(BABYSTEP_INVERT_Z), (CORESIGN(1)>0));
#elif CORE_IS_YZ
BABYSTEP_CORE(Y, Z, direction, BABYSTEP_INVERT_Z, (CORESIGN(1)<0));
BABYSTEP_CORE(Y, Z, direction, ENABLED(BABYSTEP_INVERT_Z), (CORESIGN(1)<0));
#elif DISABLED(DELTA)
BABYSTEP_AXIS(Z, direction, BABYSTEP_INVERT_Z);
BABYSTEP_AXIS(Z, direction, ENABLED(BABYSTEP_INVERT_Z));

#else // DELTA

const bool z_direction = TERN_(BABYSTEP_INVERT_Z, !) direction;

NUM_AXIS_CODE(
enable_axis(X_AXIS), enable_axis(Y_AXIS), enable_axis(Z_AXIS),
enable_axis(I_AXIS), enable_axis(J_AXIS), enable_axis(K_AXIS),
enable_axis(U_AXIS), enable_axis(V_AXIS), enable_axis(W_AXIS)
);
enable_axis(A_AXIS); enable_axis(B_AXIS); enable_axis(C_AXIS);

DIR_WAIT_BEFORE();

const xyz_byte_t old_dir = NUM_AXIS_ARRAY(
X_DIR_READ(), Y_DIR_READ(), Z_DIR_READ(),
I_DIR_READ(), J_DIR_READ(), K_DIR_READ(),
U_DIR_READ(), V_DIR_READ(), W_DIR_READ()
);
const bool old_fwd[3] = { X_DIR_READ(), Y_DIR_READ(), Z_DIR_READ() };

#ifdef X_DIR_WRITE
X_DIR_WRITE(z_direction);
#endif
#ifdef Y_DIR_WRITE
Y_DIR_WRITE(z_direction);
#endif
#ifdef Z_DIR_WRITE
Z_DIR_WRITE(z_direction);
#endif
X_DIR_WRITE(z_direction);
Y_DIR_WRITE(z_direction);
Z_DIR_WRITE(z_direction);

DIR_WAIT_AFTER();

_SAVE_START();

#ifdef X_STEP_WRITE
X_STEP_WRITE(STEP_STATE_X);
#endif
#ifdef Y_STEP_WRITE
Y_STEP_WRITE(STEP_STATE_Y);
#endif
#ifdef Z_STEP_WRITE
Z_STEP_WRITE(STEP_STATE_Z);
#endif
X_STEP_WRITE(STEP_STATE_X);
Y_STEP_WRITE(STEP_STATE_Y);
Z_STEP_WRITE(STEP_STATE_Z);

_PULSE_WAIT();

#ifdef X_STEP_WRITE
X_STEP_WRITE(!STEP_STATE_X);
#endif
#ifdef Y_STEP_WRITE
Y_STEP_WRITE(!STEP_STATE_Y);
#endif
#ifdef Z_STEP_WRITE
Z_STEP_WRITE(!STEP_STATE_Z);
#endif
X_STEP_WRITE(!STEP_STATE_X);
Y_STEP_WRITE(!STEP_STATE_Y);
Z_STEP_WRITE(!STEP_STATE_Z);

// Restore direction bits
EXTRA_DIR_WAIT_BEFORE();

#ifdef X_DIR_WRITE
X_DIR_WRITE(old_dir.x);
#endif
#ifdef Y_DIR_WRITE
Y_DIR_WRITE(old_dir.y);
#endif
#ifdef Z_DIR_WRITE
Z_DIR_WRITE(old_dir.z);
#endif
X_DIR_WRITE(old_fwd[A_AXIS]);
Y_DIR_WRITE(old_fwd[B_AXIS]);
Z_DIR_WRITE(old_fwd[C_AXIS]);

EXTRA_DIR_WAIT_AFTER();

#endif

} break;

#if HAS_I_AXIS
case I_AXIS: BABYSTEP_AXIS(I, direction, 0); break;
#endif
#if HAS_J_AXIS
case J_AXIS: BABYSTEP_AXIS(J, direction, 0); break;
#endif
#if HAS_K_AXIS
case K_AXIS: BABYSTEP_AXIS(K, direction, 0); break;
#endif
#if HAS_U_AXIS
case U_AXIS: BABYSTEP_AXIS(U, direction, 0); break;
#endif
#if HAS_V_AXIS
case V_AXIS: BABYSTEP_AXIS(V, direction, 0); break;
#endif
#if HAS_W_AXIS
case W_AXIS: BABYSTEP_AXIS(W, direction, 0); break;
#endif

default: break;
}

Expand Down
44 changes: 22 additions & 22 deletions Marlin/src/module/stepper/indirection.h
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ void reset_stepper_drivers(); // Called by settings.load / settings.reset
#ifndef X_DIR_INIT
#define X_DIR_INIT() SET_OUTPUT(X_DIR_PIN)
#define X_DIR_WRITE(STATE) WRITE(X_DIR_PIN,INVERT_DIR(X, STATE))
#define X_DIR_READ() bool(READ(X_DIR_PIN))
#define X_DIR_READ() INVERT_DIR(X, bool(READ(X_DIR_PIN)))
#endif
#define X_STEP_INIT() SET_OUTPUT(X_STEP_PIN)
#ifndef X_STEP_WRITE
Expand All @@ -109,7 +109,7 @@ void reset_stepper_drivers(); // Called by settings.load / settings.reset
#ifndef Y_DIR_INIT
#define Y_DIR_INIT() SET_OUTPUT(Y_DIR_PIN)
#define Y_DIR_WRITE(STATE) WRITE(Y_DIR_PIN,INVERT_DIR(Y, STATE))
#define Y_DIR_READ() bool(READ(Y_DIR_PIN))
#define Y_DIR_READ() INVERT_DIR(Y, bool(READ(Y_DIR_PIN)))
#endif
#define Y_STEP_INIT() SET_OUTPUT(Y_STEP_PIN)
#ifndef Y_STEP_WRITE
Expand All @@ -128,7 +128,7 @@ void reset_stepper_drivers(); // Called by settings.load / settings.reset
#ifndef Z_DIR_INIT
#define Z_DIR_INIT() SET_OUTPUT(Z_DIR_PIN)
#define Z_DIR_WRITE(STATE) WRITE(Z_DIR_PIN,INVERT_DIR(Z, STATE))
#define Z_DIR_READ() bool(READ(Z_DIR_PIN))
#define Z_DIR_READ() INVERT_DIR(Z, bool(READ(Z_DIR_PIN)))
#endif
#define Z_STEP_INIT() SET_OUTPUT(Z_STEP_PIN)
#ifndef Z_STEP_WRITE
Expand All @@ -147,7 +147,7 @@ void reset_stepper_drivers(); // Called by settings.load / settings.reset
#ifndef X2_DIR_INIT
#define X2_DIR_INIT() SET_OUTPUT(X2_DIR_PIN)
#define X2_DIR_WRITE(STATE) WRITE(X2_DIR_PIN,INVERT_DIR(X2, STATE))
#define X2_DIR_READ() bool(READ(X2_DIR_PIN))
#define X2_DIR_READ() INVERT_DIR(X2, bool(READ(X2_DIR_PIN)))
#endif
#define X2_STEP_INIT() SET_OUTPUT(X2_STEP_PIN)
#ifndef X2_STEP_WRITE
Expand All @@ -166,7 +166,7 @@ void reset_stepper_drivers(); // Called by settings.load / settings.reset
#ifndef Y2_DIR_INIT
#define Y2_DIR_INIT() SET_OUTPUT(Y2_DIR_PIN)
#define Y2_DIR_WRITE(STATE) WRITE(Y2_DIR_PIN,INVERT_DIR(Y2, STATE))
#define Y2_DIR_READ() bool(READ(Y2_DIR_PIN))
#define Y2_DIR_READ() INVERT_DIR(Y2, bool(READ(Y2_DIR_PIN)))
#endif
#define Y2_STEP_INIT() SET_OUTPUT(Y2_STEP_PIN)
#ifndef Y2_STEP_WRITE
Expand All @@ -187,7 +187,7 @@ void reset_stepper_drivers(); // Called by settings.load / settings.reset
#ifndef Z2_DIR_INIT
#define Z2_DIR_INIT() SET_OUTPUT(Z2_DIR_PIN)
#define Z2_DIR_WRITE(STATE) WRITE(Z2_DIR_PIN,INVERT_DIR(Z2, STATE))
#define Z2_DIR_READ() bool(READ(Z2_DIR_PIN))
#define Z2_DIR_READ() INVERT_DIR(Z2, bool(READ(Z2_DIR_PIN)))
#endif
#define Z2_STEP_INIT() SET_OUTPUT(Z2_STEP_PIN)
#ifndef Z2_STEP_WRITE
Expand All @@ -208,7 +208,7 @@ void reset_stepper_drivers(); // Called by settings.load / settings.reset
#ifndef Z3_DIR_INIT
#define Z3_DIR_INIT() SET_OUTPUT(Z3_DIR_PIN)
#define Z3_DIR_WRITE(STATE) WRITE(Z3_DIR_PIN,INVERT_DIR(Z3, STATE))
#define Z3_DIR_READ() bool(READ(Z3_DIR_PIN))
#define Z3_DIR_READ() INVERT_DIR(Z3, bool(READ(Z3_DIR_PIN)))
#endif
#define Z3_STEP_INIT() SET_OUTPUT(Z3_STEP_PIN)
#ifndef Z3_STEP_WRITE
Expand All @@ -229,7 +229,7 @@ void reset_stepper_drivers(); // Called by settings.load / settings.reset
#ifndef Z4_DIR_INIT
#define Z4_DIR_INIT() SET_OUTPUT(Z4_DIR_PIN)
#define Z4_DIR_WRITE(STATE) WRITE(Z4_DIR_PIN,INVERT_DIR(Z4, STATE))
#define Z4_DIR_READ() bool(READ(Z4_DIR_PIN))
#define Z4_DIR_READ() INVERT_DIR(Z4, bool(READ(Z4_DIR_PIN)))
#endif
#define Z4_STEP_INIT() SET_OUTPUT(Z4_STEP_PIN)
#ifndef Z4_STEP_WRITE
Expand All @@ -250,7 +250,7 @@ void reset_stepper_drivers(); // Called by settings.load / settings.reset
#ifndef I_DIR_INIT
#define I_DIR_INIT() SET_OUTPUT(I_DIR_PIN)
#define I_DIR_WRITE(STATE) WRITE(I_DIR_PIN,INVERT_DIR(I, STATE))
#define I_DIR_READ() bool(READ(I_DIR_PIN))
#define I_DIR_READ() INVERT_DIR(I, bool(READ(I_DIR_PIN)))
#endif
#define I_STEP_INIT() SET_OUTPUT(I_STEP_PIN)
#ifndef I_STEP_WRITE
Expand All @@ -269,7 +269,7 @@ void reset_stepper_drivers(); // Called by settings.load / settings.reset
#ifndef J_DIR_INIT
#define J_DIR_INIT() SET_OUTPUT(J_DIR_PIN)
#define J_DIR_WRITE(STATE) WRITE(J_DIR_PIN,INVERT_DIR(J, STATE))
#define J_DIR_READ() bool(READ(J_DIR_PIN))
#define J_DIR_READ() INVERT_DIR(J, bool(READ(J_DIR_PIN)))
#endif
#define J_STEP_INIT() SET_OUTPUT(J_STEP_PIN)
#ifndef J_STEP_WRITE
Expand All @@ -288,7 +288,7 @@ void reset_stepper_drivers(); // Called by settings.load / settings.reset
#ifndef K_DIR_INIT
#define K_DIR_INIT() SET_OUTPUT(K_DIR_PIN)
#define K_DIR_WRITE(STATE) WRITE(K_DIR_PIN,INVERT_DIR(K, STATE))
#define K_DIR_READ() bool(READ(K_DIR_PIN))
#define K_DIR_READ() INVERT_DIR(K, bool(READ(K_DIR_PIN)))
#endif
#define K_STEP_INIT() SET_OUTPUT(K_STEP_PIN)
#ifndef K_STEP_WRITE
Expand All @@ -307,7 +307,7 @@ void reset_stepper_drivers(); // Called by settings.load / settings.reset
#ifndef U_DIR_INIT
#define U_DIR_INIT() SET_OUTPUT(U_DIR_PIN)
#define U_DIR_WRITE(STATE) WRITE(U_DIR_PIN,INVERT_DIR(U, STATE))
#define U_DIR_READ() bool(READ(U_DIR_PIN))
#define U_DIR_READ() INVERT_DIR(U, bool(READ(U_DIR_PIN)))
#endif
#define U_STEP_INIT() SET_OUTPUT(U_STEP_PIN)
#ifndef U_STEP_WRITE
Expand All @@ -326,7 +326,7 @@ void reset_stepper_drivers(); // Called by settings.load / settings.reset
#ifndef V_DIR_INIT
#define V_DIR_INIT() SET_OUTPUT(V_DIR_PIN)
#define V_DIR_WRITE(STATE) WRITE(V_DIR_PIN,INVERT_DIR(V, STATE))
#define V_DIR_READ() bool(READ(V_DIR_PIN))
#define V_DIR_READ() INVERT_DIR(V, bool(READ(V_DIR_PIN)))
#endif
#define V_STEP_INIT() SET_OUTPUT(V_STEP_PIN)
#ifndef V_STEP_WRITE
Expand All @@ -345,7 +345,7 @@ void reset_stepper_drivers(); // Called by settings.load / settings.reset
#ifndef W_DIR_INIT
#define W_DIR_INIT() SET_OUTPUT(W_DIR_PIN)
#define W_DIR_WRITE(STATE) WRITE(W_DIR_PIN,INVERT_DIR(W, STATE))
#define W_DIR_READ() bool(READ(W_DIR_PIN))
#define W_DIR_READ() INVERT_DIR(W, bool(READ(W_DIR_PIN)))
#endif
#define W_STEP_INIT() SET_OUTPUT(W_STEP_PIN)
#ifndef W_STEP_WRITE
Expand All @@ -363,7 +363,7 @@ void reset_stepper_drivers(); // Called by settings.load / settings.reset
#ifndef E0_DIR_INIT
#define E0_DIR_INIT() SET_OUTPUT(E0_DIR_PIN)
#define E0_DIR_WRITE(STATE) WRITE(E0_DIR_PIN,INVERT_DIR(E0, STATE))
#define E0_DIR_READ() bool(READ(E0_DIR_PIN))
#define E0_DIR_READ() INVERT_DIR(E0, bool(READ(E0_DIR_PIN)))
#endif
#define E0_STEP_INIT() SET_OUTPUT(E0_STEP_PIN)
#ifndef E0_STEP_WRITE
Expand All @@ -380,7 +380,7 @@ void reset_stepper_drivers(); // Called by settings.load / settings.reset
#ifndef E1_DIR_INIT
#define E1_DIR_INIT() SET_OUTPUT(E1_DIR_PIN)
#define E1_DIR_WRITE(STATE) WRITE(E1_DIR_PIN,INVERT_DIR(E1, STATE))
#define E1_DIR_READ() bool(READ(E1_DIR_PIN))
#define E1_DIR_READ() INVERT_DIR(E1, bool(READ(E1_DIR_PIN)))
#endif
#define E1_STEP_INIT() SET_OUTPUT(E1_STEP_PIN)
#ifndef E1_STEP_WRITE
Expand All @@ -397,7 +397,7 @@ void reset_stepper_drivers(); // Called by settings.load / settings.reset
#ifndef E2_DIR_INIT
#define E2_DIR_INIT() SET_OUTPUT(E2_DIR_PIN)
#define E2_DIR_WRITE(STATE) WRITE(E2_DIR_PIN,INVERT_DIR(E2, STATE))
#define E2_DIR_READ() bool(READ(E2_DIR_PIN))
#define E2_DIR_READ() INVERT_DIR(E2, bool(READ(E2_DIR_PIN)))
#endif
#define E2_STEP_INIT() SET_OUTPUT(E2_STEP_PIN)
#ifndef E2_STEP_WRITE
Expand All @@ -414,7 +414,7 @@ void reset_stepper_drivers(); // Called by settings.load / settings.reset
#ifndef E3_DIR_INIT
#define E3_DIR_INIT() SET_OUTPUT(E3_DIR_PIN)
#define E3_DIR_WRITE(STATE) WRITE(E3_DIR_PIN,INVERT_DIR(E3, STATE))
#define E3_DIR_READ() bool(READ(E3_DIR_PIN))
#define E3_DIR_READ() INVERT_DIR(E3, bool(READ(E3_DIR_PIN)))
#endif
#define E3_STEP_INIT() SET_OUTPUT(E3_STEP_PIN)
#ifndef E3_STEP_WRITE
Expand All @@ -431,7 +431,7 @@ void reset_stepper_drivers(); // Called by settings.load / settings.reset
#ifndef E4_DIR_INIT
#define E4_DIR_INIT() SET_OUTPUT(E4_DIR_PIN)
#define E4_DIR_WRITE(STATE) WRITE(E4_DIR_PIN,INVERT_DIR(E4, STATE))
#define E4_DIR_READ() bool(READ(E4_DIR_PIN))
#define E4_DIR_READ() INVERT_DIR(E4, bool(READ(E4_DIR_PIN)))
#endif
#define E4_STEP_INIT() SET_OUTPUT(E4_STEP_PIN)
#ifndef E4_STEP_WRITE
Expand All @@ -448,7 +448,7 @@ void reset_stepper_drivers(); // Called by settings.load / settings.reset
#ifndef E5_DIR_INIT
#define E5_DIR_INIT() SET_OUTPUT(E5_DIR_PIN)
#define E5_DIR_WRITE(STATE) WRITE(E5_DIR_PIN,INVERT_DIR(E5, STATE))
#define E5_DIR_READ() bool(READ(E5_DIR_PIN))
#define E5_DIR_READ() INVERT_DIR(E5, bool(READ(E5_DIR_PIN)))
#endif
#define E5_STEP_INIT() SET_OUTPUT(E5_STEP_PIN)
#ifndef E5_STEP_WRITE
Expand All @@ -465,7 +465,7 @@ void reset_stepper_drivers(); // Called by settings.load / settings.reset
#ifndef E6_DIR_INIT
#define E6_DIR_INIT() SET_OUTPUT(E6_DIR_PIN)
#define E6_DIR_WRITE(STATE) WRITE(E6_DIR_PIN,INVERT_DIR(E6, STATE))
#define E6_DIR_READ() bool(READ(E6_DIR_PIN))
#define E6_DIR_READ() INVERT_DIR(E6, bool(READ(E6_DIR_PIN)))
#endif
#define E6_STEP_INIT() SET_OUTPUT(E6_STEP_PIN)
#ifndef E6_STEP_WRITE
Expand All @@ -482,7 +482,7 @@ void reset_stepper_drivers(); // Called by settings.load / settings.reset
#ifndef E7_DIR_INIT
#define E7_DIR_INIT() SET_OUTPUT(E7_DIR_PIN)
#define E7_DIR_WRITE(STATE) WRITE(E7_DIR_PIN,INVERT_DIR(E7, STATE))
#define E7_DIR_READ() bool(READ(E7_DIR_PIN))
#define E7_DIR_READ() INVERT_DIR(E7, bool(READ(E7_DIR_PIN)))
#endif
#define E7_STEP_INIT() SET_OUTPUT(E7_STEP_PIN)
#ifndef E7_STEP_WRITE
Expand Down
Loading

0 comments on commit c6ea99f

Please sign in to comment.