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

Fix coils alignment #18528

Merged
merged 11 commits into from
Jul 6, 2020
22 changes: 20 additions & 2 deletions Marlin/src/feature/tmc_util.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -480,6 +480,10 @@
TMC_GLOBAL_SCALER,
TMC_CS_ACTUAL,
TMC_PWM_SCALE,
TMC_PWM_SCALE_SUM,
TMC_PWM_SCALE_AUTO,
TMC_PWM_OFS_AUTO,
TMC_PWM_GRAD_AUTO,
TMC_VSENSE,
TMC_STEALTHCHOP,
TMC_MICROSTEPS,
Expand All @@ -492,7 +496,8 @@
TMC_TBL,
TMC_HEND,
TMC_HSTRT,
TMC_SGT
TMC_SGT,
TMC_MSCNT
};
enum TMC_drv_status_enum : char {
TMC_DRV_CODES,
Expand Down Expand Up @@ -591,7 +596,10 @@
#if HAS_TMC220x
static void _tmc_status(TMC2208Stepper &st, const TMC_debug_enum i) {
switch (i) {
case TMC_PWM_SCALE: SERIAL_PRINT(st.pwm_scale_sum(), DEC); break;
case TMC_PWM_SCALE_SUM: SERIAL_PRINT(st.pwm_scale_sum(), DEC); break;
case TMC_PWM_SCALE_AUTO: SERIAL_PRINT(st.pwm_scale_auto(), DEC); break;
case TMC_PWM_OFS_AUTO: SERIAL_PRINT(st.pwm_ofs_auto(), DEC); break;
case TMC_PWM_GRAD_AUTO: SERIAL_PRINT(st.pwm_grad_auto(), DEC); break;
case TMC_STEALTHCHOP: serialprint_truefalse(st.stealth()); break;
case TMC_S2VSA: if (st.s2vsa()) SERIAL_CHAR('*'); break;
case TMC_S2VSB: if (st.s2vsb()) SERIAL_CHAR('*'); break;
Expand Down Expand Up @@ -680,6 +688,7 @@
case TMC_TBL: SERIAL_PRINT(st.blank_time(), DEC); break;
case TMC_HEND: SERIAL_PRINT(st.hysteresis_end(), DEC); break;
case TMC_HSTRT: SERIAL_PRINT(st.hysteresis_start(), DEC); break;
case TMC_MSCNT: SERIAL_PRINT(st.get_microstep_counter(), DEC); break;
default: _tmc_status(st, i); break;
}
}
Expand Down Expand Up @@ -900,11 +909,20 @@
#if ENABLED(MONITOR_DRIVER_STATUS)
TMC_REPORT("triggered\n OTP\t", TMC_OTPW_TRIGGERED);
#endif

#if HAS_TMC220x
TMC_REPORT("pwm scale sum", TMC_PWM_SCALE_SUM);
TMC_REPORT("pwm scale auto", TMC_PWM_SCALE_AUTO);
TMC_REPORT("pwm offset auto", TMC_PWM_OFS_AUTO);
TMC_REPORT("pwm grad auto", TMC_PWM_GRAD_AUTO);
#endif

TMC_REPORT("off time", TMC_TOFF);
TMC_REPORT("blank time", TMC_TBL);
TMC_REPORT("hysteresis\n -end\t", TMC_HEND);
TMC_REPORT(" -start\t", TMC_HSTRT);
TMC_REPORT("Stallguard thrs", TMC_SGT);
TMC_REPORT("uStep count", TMC_MSCNT);
DRV_REPORT("DRVSTATUS", TMC_DRV_CODES);
#if HAS_TMCX1X0 || HAS_TMC220x
DRV_REPORT("sg_result", TMC_SG_RESULT);
Expand Down
65 changes: 34 additions & 31 deletions Marlin/src/module/motion.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1443,63 +1443,67 @@ void set_axis_not_trusted(const AxisEnum axis) {
TERN_(I2C_POSITION_ENCODERS, I2CPEM.unhomed(axis));
}

/**
* Move the axis back to its home_phase if set and driver is capable (TMC)
*
* Improves homing repeatability by homing to stepper coil's nearest absolute
* phase position. Trinamic drivers use a stepper phase table with 1024 values
* spanning 4 full steps with 256 positions each (ergo, 1024 positions).
*/
void backout_to_tmc_homing_phase(const AxisEnum axis) {
#ifdef TMC_HOME_PHASE
const abc_long_t home_phase = TMC_HOME_PHASE;
#ifdef TMC_HOME_PHASE
/**
* Move the axis back to its home_phase if set and driver is capable (TMC)
*
* Improves homing repeatability by homing to stepper coil's nearest absolute
* phase position. Trinamic drivers use a stepper phase table with 1024 values
* spanning 4 full steps with 256 positions each (ergo, 1024 positions).
*/
void backout_to_tmc_homing_phase(const AxisEnum axis) {
const xyz_long_t home_phase = TMC_HOME_PHASE;

// check if home phase is disabled for this axis.
if (home_phase[axis] < 0) return;

int16_t axisMicrostepSize;
int16_t phaseCurrent;
bool invertDir;
int16_t phasePerUStep, // tmc µsteps(phase) per marlin µsteps
phaseCurrent, // the tmc µsteps(phase) count of the current position
effectorBackoutDir, // direction in which the effector mm coordinates move away from endstop.
stepperBackoutDir; // direction in which the tmc µstep count(phase) move away from endstop.

switch (axis) {
#ifdef X_MICROSTEPS
case X_AXIS:
axisMicrostepSize = 256 / (X_MICROSTEPS);
phasePerUStep = 256 / (X_MICROSTEPS);
phaseCurrent = stepperX.get_microstep_counter();
invertDir = INVERT_X_DIR;
effectorBackoutDir = -X_HOME_DIR;
stepperBackoutDir = INVERT_X_DIR ? effectorBackoutDir : -effectorBackoutDir;
break;
#endif
#ifdef Y_MICROSTEPS
case Y_AXIS:
axisMicrostepSize = 256 / (Y_MICROSTEPS);
phasePerUStep = 256 / (Y_MICROSTEPS);
phaseCurrent = stepperY.get_microstep_counter();
invertDir = INVERT_Y_DIR;
effectorBackoutDir = -Y_HOME_DIR;
stepperBackoutDir = INVERT_Y_DIR ? effectorBackoutDir : -effectorBackoutDir;
break;
#endif
#ifdef Z_MICROSTEPS
case Z_AXIS:
axisMicrostepSize = 256 / (Z_MICROSTEPS);
phasePerUStep = 256 / (Z_MICROSTEPS);
phaseCurrent = stepperZ.get_microstep_counter();
invertDir = INVERT_Z_DIR;
effectorBackoutDir = -Z_HOME_DIR;
stepperBackoutDir = INVERT_Z_DIR ? effectorBackoutDir : -effectorBackoutDir;
GMagician marked this conversation as resolved.
Show resolved Hide resolved
break;
#endif
default: return;
}

// Depending on invert dir measure the distance to nearest home phase.
int16_t phaseDelta = (invertDir ? -1 : 1) * (home_phase[axis] - phaseCurrent);
// Phase distance to nearest home phase position when moving in the backout direction from endstop(may be negative).
int16_t phaseDelta = (home_phase[axis] - phaseCurrent) * stepperBackoutDir;

// Check if home distance within endstop assumed repeatability noise of .05mm and warn.
if (ABS(phaseDelta) * planner.steps_to_mm[axis] / axisMicrostepSize < 0.05f)
if (ABS(phaseDelta) * planner.steps_to_mm[axis] / phasePerUStep < 0.05f)
DEBUG_ECHOLNPAIR("Selected home phase ", home_phase[axis],
" too close to endstop trigger phase ", phaseCurrent,
". Pick a different phase for ", axis_codes[axis]);

// Skip to next if target position is behind current. So it only moves away from endstop.
if (phaseDelta < 0) phaseDelta += 1024;

// Get the integer µsteps to target. Unreachable phase? Consistently stop at the µstep before / after based on invertDir.
const float mmDelta = -(int16_t(phaseDelta / axisMicrostepSize) * planner.steps_to_mm[axis] * (Z_HOME_DIR));
// Convert tmc µsteps(phase) to whole Marlin µsteps to effector backout direction to mm
const float mmDelta = int16_t(phaseDelta / phasePerUStep) * effectorBackoutDir * planner.steps_to_mm[axis];
GMagician marked this conversation as resolved.
Show resolved Hide resolved

// optional debug messages.
if (DEBUGGING(LEVELING)) {
Expand All @@ -1513,11 +1517,8 @@ void backout_to_tmc_homing_phase(const AxisEnum axis) {
// retrace by the amount computed in mmDelta.
do_homing_move(axis, mmDelta, get_homing_bump_feedrate(axis));
}
#else
UNUSED(axis);
#endif
}

}
#endif

/**
* Home an individual "raw axis" to its endstop.
Expand Down Expand Up @@ -1748,8 +1749,10 @@ void homeaxis(const AxisEnum axis) {
}
#endif

// move back to homing phase if configured and capable
backout_to_tmc_homing_phase(axis);
#ifdef TMC_HOME_PHASE
// move back to homing phase if configured and capable
backout_to_tmc_homing_phase(axis);
#endif

#if IS_SCARA

Expand Down