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

[1.1] Skew Correction #8623

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ script:
- opt_enable PIDTEMPBED FIX_MOUNTED_PROBE Z_SAFE_HOMING ARC_P_CIRCLES CNC_WORKSPACE_PLANES CNC_COORDINATE_SYSTEMS
- opt_enable REPRAP_DISCOUNT_SMART_CONTROLLER SDSUPPORT EEPROM_SETTINGS
- opt_enable BLINKM PCA9632 RGB_LED NEOPIXEL_LED
- opt_enable AUTO_BED_LEVELING_LINEAR Z_MIN_PROBE_REPEATABILITY_TEST DEBUG_LEVELING_FEATURE
- opt_enable AUTO_BED_LEVELING_LINEAR Z_MIN_PROBE_REPEATABILITY_TEST DEBUG_LEVELING_FEATURE SKEW_CORRECTION SKEW_CORRECTION_FOR_Z SKEW_CORRECTION_GCODE
- opt_enable_adv FWRETRACT MAX7219_DEBUG LED_CONTROL_MENU
- opt_set ABL_GRID_POINTS_X 16
- opt_set ABL_GRID_POINTS_Y 16
Expand Down
45 changes: 44 additions & 1 deletion Marlin/Conditionals_post.h
Original file line number Diff line number Diff line change
Expand Up @@ -844,6 +844,49 @@
#define Z_PROBE_OFFSET_FROM_EXTRUDER 0
#endif

/**
* XYZ Bed Skew Correction
*/
#if ENABLED(SKEW_CORRECTION)
#define SKEW_FACTOR_MIN -1
#define SKEW_FACTOR_MAX 1

#define _GET_SIDE(a,b,c) (SQRT(2*sq(a)+2*sq(b)-4*sq(c))*0.5)
#define _SKEW_SIDE(a,b,c) tan(M_PI*0.5-acos((sq(a)-sq(b)-sq(c))/(2*c*b)))
#define _SKEW_FACTOR(a,b,c) _SKEW_SIDE(a,_GET_SIDE(a,b,c),c)

#ifndef XY_SKEW_FACTOR
constexpr float XY_SKEW_FACTOR = (
#if defined(XY_DIAG_AC) && defined(XY_DIAG_BD) && defined(XY_SIDE_AD)
_SKEW_FACTOR(XY_DIAG_AC, XY_DIAG_BD, XY_SIDE_AD)
#else
0.0
#endif
);
#endif
#ifndef XZ_SKEW_FACTOR
#if defined(XY_SIDE_AD) && !defined(XZ_SIDE_AD)
#define XZ_SIDE_AD XY_SIDE_AD
#endif
constexpr float XZ_SKEW_FACTOR = (
#if defined(XZ_DIAG_AC) && defined(XZ_DIAG_BD) && defined(XZ_SIDE_AD)
_SKEW_FACTOR(XZ_DIAG_AC, XZ_DIAG_BD, XZ_SIDE_AD)
#else
0.0
#endif
);
#endif
#ifndef YZ_SKEW_FACTOR
constexpr float YZ_SKEW_FACTOR = (
#if defined(YZ_DIAG_AC) && defined(YZ_DIAG_BD) && defined(YZ_SIDE_AD)
_SKEW_FACTOR(YZ_DIAG_AC, YZ_DIAG_BD, YZ_SIDE_AD)
#else
0.0
#endif
);
#endif
#endif // SKEW_CORRECTION

/**
* Heater & Fan Pausing
*/
Expand Down Expand Up @@ -898,7 +941,7 @@
#define HAS_LEVELING (HAS_ABL || ENABLED(MESH_BED_LEVELING))
#define HAS_AUTOLEVEL (HAS_ABL && DISABLED(PROBE_MANUALLY))
#define HAS_MESH (ENABLED(AUTO_BED_LEVELING_BILINEAR) || ENABLED(AUTO_BED_LEVELING_UBL) || ENABLED(MESH_BED_LEVELING))
#define PLANNER_LEVELING (OLDSCHOOL_ABL || ENABLED(MESH_BED_LEVELING) || UBL_DELTA)
#define PLANNER_LEVELING (OLDSCHOOL_ABL || ENABLED(MESH_BED_LEVELING) || UBL_DELTA || ENABLED(SKEW_CORRECTION))
#define HAS_PROBING_PROCEDURE (HAS_ABL || ENABLED(Z_MIN_PROBE_REPEATABILITY_TEST))
#if HAS_PROBING_PROCEDURE
#define PROBE_BED_WIDTH abs(RIGHT_PROBE_BED_POSITION - (LEFT_PROBE_BED_POSITION))
Expand Down
59 changes: 58 additions & 1 deletion Marlin/Configuration.h
Original file line number Diff line number Diff line change
Expand Up @@ -832,7 +832,7 @@
//===========================================================================
//=============================== Bed Leveling ==============================
//===========================================================================
// @section bedlevel
// @section calibrate

/**
* Choose one of the options below to enable G29 Bed Leveling. The parameters
Expand Down Expand Up @@ -1044,6 +1044,63 @@
#define HOMING_FEEDRATE_XY (50*60)
#define HOMING_FEEDRATE_Z (4*60)

// @section calibrate

/**
* Bed Skew Compensation
*
* This feature corrects for misalignment in the XYZ axes.
*
* Take the following steps to get the bed skew in the XY plane:
* 1. Print a test square (e.g., https://www.thingiverse.com/thing:2563185)
* 2. For XY_DIAG_AC measure the diagonal A to C
* 3. For XY_DIAG_BD measure the diagonal B to D
* 4. For XY_SIDE_AD measure the edge A to D
*
* Marlin automatically computes skew factors from these measurements.
* Skew factors may also be computed and set manually:
*
* - Compute AB : SQRT(2*AC*AC+2*BD*BD-4*AD*AD)/2
* - XY_SKEW_FACTOR : TAN(PI/2-ACOS((AC*AC-AB*AB-AD*AD)/(2*AB*AD)))
*
* If desired, follow the same procedure for XZ and YZ.
* Use these diagrams for reference:
*
* Y Z Z
* ^ B-------C ^ B-------C ^ B-------C
* | / / | / / | / /
* | / / | / / | / /
* | A-------D | A-------D | A-------D
* +-------------->X +-------------->X +-------------->Y
* XY_SKEW_FACTOR XZ_SKEW_FACTOR YZ_SKEW_FACTOR
*/
//#define SKEW_CORRECTION

#if ENABLED(SKEW_CORRECTION)
// Input all length measurements here:
#define XY_DIAG_AC 282.8427124746
#define XY_DIAG_BD 282.8427124746
#define XY_SIDE_AD 200

// Or, set the default skew factors directly here
// to override the above measurements:
#define XY_SKEW_FACTOR 0.0

//#define SKEW_CORRECTION_FOR_Z
#if ENABLED(SKEW_CORRECTION_FOR_Z)
#define XZ_DIAG_AC 282.8427124746
#define XZ_DIAG_BD 282.8427124746
#define YZ_DIAG_AC 282.8427124746
#define YZ_DIAG_BD 282.8427124746
#define YZ_SIDE_AD 200
#define XZ_SKEW_FACTOR 0.0
#define YZ_SKEW_FACTOR 0.0
#endif

// Enable this option for M852 to set skew at runtime
//#define SKEW_CORRECTION_GCODE
#endif

//=============================================================================
//============================= Additional Features ===========================
//=============================================================================
Expand Down
70 changes: 70 additions & 0 deletions Marlin/Marlin_main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,7 @@
* M666 - Set delta endstop adjustment. (Requires DELTA)
* M605 - Set dual x-carriage movement mode: "M605 S<mode> [X<x_offset>] [R<temp_offset>]". (Requires DUAL_X_CARRIAGE)
* M851 - Set Z probe's Z offset in current units. (Negative = below the nozzle.)
* M852 - Set skew factors: "M852 [I<xy>] [J<xz>] [K<yz>]". (Requires SKEW_CORRECTION_GCODE, and SKEW_CORRECTION_FOR_Z for IJ)
* M860 - Report the position of position encoder modules.
* M861 - Report the status of position encoder modules.
* M862 - Perform an axis continuity test for position encoder modules.
Expand Down Expand Up @@ -9942,6 +9943,69 @@ inline void gcode_M502() {

#endif // HAS_BED_PROBE

#if ENABLED(SKEW_CORRECTION_GCODE)

/**
* M852: Get or set the machine skew factors. Reports current values with no arguments.
*
* S[xy_factor] - Alias for 'I'
* I[xy_factor] - New XY skew factor
* J[xz_factor] - New XZ skew factor
* K[yz_factor] - New YZ skew factor
*/
inline void gcode_M852() {
const bool ijk = parser.seen('I') || parser.seen('S')
#if ENABLED(SKEW_CORRECTION_FOR_Z)
|| parser.seen('J') || parser.seen('K')
#endif
;
bool badval = false;

if (parser.seen('I') || parser.seen('S')) {
const float value = parser.value_linear_units();
if (WITHIN(value, SKEW_FACTOR_MIN, SKEW_FACTOR_MAX))
planner.xy_skew_factor = value;
else
badval = true;
}

#if ENABLED(SKEW_CORRECTION_FOR_Z)

if (parser.seen('J')) {
const float value = parser.value_linear_units();
if (WITHIN(value, SKEW_FACTOR_MIN, SKEW_FACTOR_MAX))
planner.xz_skew_factor = value;
else
badval = true;
}

if (parser.seen('K')) {
const float value = parser.value_linear_units();
if (WITHIN(value, SKEW_FACTOR_MIN, SKEW_FACTOR_MAX))
planner.yz_skew_factor = value;
else
badval = true;
}

#endif

if (badval)
SERIAL_ECHOLNPGM(MSG_SKEW_MIN " " STRINGIFY(SKEW_FACTOR_MIN) " " MSG_SKEW_MAX " " STRINGIFY(SKEW_FACTOR_MAX));

if (!ijk) {
SERIAL_ECHO_START();
SERIAL_ECHOPAIR(MSG_SKEW_FACTOR " XY: ", planner.xy_skew_factor);
#if ENABLED(SKEW_CORRECTION_FOR_Z)
SERIAL_ECHOPAIR(" XZ: ", planner.xz_skew_factor);
SERIAL_ECHOLNPAIR(" YZ: ", planner.yz_skew_factor);
#else
SERIAL_EOL();
#endif
}
}

#endif // SKEW_CORRECTION_GCODE

#if ENABLED(ADVANCED_PAUSE_FEATURE)

/**
Expand Down Expand Up @@ -11787,6 +11851,12 @@ void process_parsed_command() {
break;
#endif // HAS_BED_PROBE

#if ENABLED(SKEW_CORRECTION_GCODE)
case 852: // M852: Set Skew factors
gcode_M852();
break;
#endif

#if ENABLED(ADVANCED_PAUSE_FEATURE)
case 600: // M600: Pause for filament change
gcode_M600();
Expand Down
14 changes: 14 additions & 0 deletions Marlin/SanityCheck.h
Original file line number Diff line number Diff line change
Expand Up @@ -1518,3 +1518,17 @@ static_assert(COUNT(sanity_arr_3) <= XYZE_N, "DEFAULT_MAX_ACCELERATION has too m
#if ENABLED(LED_CONTROL_MENU) && DISABLED(ULTIPANEL)
#error "LED_CONTROL_MENU requires an LCD controller."
#endif

#if ENABLED(SKEW_CORRECTION)
#if !defined(XY_SKEW_FACTOR) && !(defined(XY_DIAG_AC) && defined(XY_DIAG_BD) && defined(XY_SIDE_AD))
#error "SKEW_CORRECTION requires XY_SKEW_FACTOR or XY_DIAG_AC, XY_DIAG_BD, XY_SIDE_AD."
#endif
#if ENABLED(SKEW_CORRECTION_FOR_Z)
#if !defined(XZ_SKEW_FACTOR) && !(defined(XZ_DIAG_AC) && defined(XZ_DIAG_BD) && defined(XZ_SIDE_AD))
#error "SKEW_CORRECTION requires XZ_SKEW_FACTOR or XZ_DIAG_AC, XZ_DIAG_BD, XZ_SIDE_AD."
#endif
#if !defined(YZ_SKEW_FACTOR) && !(defined(YZ_DIAG_AC) && defined(YZ_DIAG_BD) && defined(YZ_SIDE_AD))
#error "SKEW_CORRECTION requires YZ_SKEW_FACTOR or YZ_DIAG_AC, YZ_DIAG_BD, YZ_SIDE_AD."
#endif
#endif
#endif
73 changes: 69 additions & 4 deletions Marlin/configuration_store.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,13 @@
*
*/

#define EEPROM_VERSION "V45"
#define EEPROM_VERSION "V46"

// Change EEPROM version if these are changed:
#define EEPROM_OFFSET 100

/**
* V45 EEPROM Layout:
* V46 EEPROM Layout:
*
* 100 Version (char x4)
* 104 EEPROM CRC16 (uint16_t)
Expand Down Expand Up @@ -166,8 +166,13 @@
* CNC_COORDINATE_SYSTEMS 108 bytes
* 602 G54-G59.3 coordinate_system (float x 27)
*
* 710 Minimum end-point
* 2239 (710 + 208 + 36 + 9 + 288 + 988) Maximum end-point
* SKEW_CORRECTION: 12 bytes
* 710 M852 I planner.xy_skew_factor (float)
* 714 M852 J planner.xz_skew_factor (float)
* 718 M852 K planner.yz_skew_factor (float)
*
* 722 Minimum end-point
* 2251 (722 + 208 + 36 + 9 + 288 + 988) Maximum end-point
*
* ========================================================================
* meshes_begin (between max and min end-point, directly above)
Expand Down Expand Up @@ -664,13 +669,30 @@ void MarlinSettings::postprocess() {
for (uint8_t q = 3; q--;) EEPROM_WRITE(dummyui32);
#endif

//
// CNC Coordinate Systems
//

#if ENABLED(CNC_COORDINATE_SYSTEMS)
EEPROM_WRITE(coordinate_system); // 27 floats
#else
dummy = 0.0f;
for (uint8_t q = 27; q--;) EEPROM_WRITE(dummy);
#endif

//
// Skew correction factors
//

#if ENABLED(SKEW_CORRECTION)
EEPROM_WRITE(planner.xy_skew_factor);
EEPROM_WRITE(planner.xz_skew_factor);
EEPROM_WRITE(planner.yz_skew_factor);
#else
dummy = 0.0f;
for (uint8_t q = 3; q--;) EEPROM_WRITE(dummy);
#endif

if (!eeprom_error) {
const int eeprom_size = eeprom_index;

Expand Down Expand Up @@ -1105,6 +1127,23 @@ void MarlinSettings::postprocess() {
for (uint8_t q = 27; q--;) EEPROM_READ(dummy);
#endif

//
// Skew correction factors
//

#if ENABLED(SKEW_CORRECTION_GCODE)
EEPROM_READ(planner.xy_skew_factor);
#if ENABLED(SKEW_CORRECTION_FOR_Z)
EEPROM_READ(planner.xz_skew_factor);
EEPROM_READ(planner.yz_skew_factor);
#else
EEPROM_READ(dummy);
EEPROM_READ(dummy);
#endif
#else
for (uint8_t q = 3; q--;) EEPROM_READ(dummy);
#endif

if (working_crc == stored_crc) {
postprocess();
#if ENABLED(EEPROM_CHITCHAT)
Expand Down Expand Up @@ -1483,6 +1522,14 @@ void MarlinSettings::reset() {
ubl.reset();
#endif

#if ENABLED(SKEW_CORRECTION_GCODE)
planner.xy_skew_factor = XY_SKEW_FACTOR;
#if ENABLED(SKEW_CORRECTION_FOR_Z)
planner.xz_skew_factor = XZ_SKEW_FACTOR;
planner.yz_skew_factor = YZ_SKEW_FACTOR;
#endif
#endif

postprocess();

#if ENABLED(EEPROM_CHITCHAT)
Expand Down Expand Up @@ -1912,6 +1959,24 @@ void MarlinSettings::reset() {
SERIAL_ECHOLNPAIR(" M851 Z", LINEAR_UNIT(zprobe_zoffset));
#endif

/**
* Bed Skew Correction
*/
#if ENABLED(SKEW_CORRECTION_GCODE)
if (!forReplay) {
CONFIG_ECHO_START;
SERIAL_ECHOLNPGM("Skew Factor: ");
}
CONFIG_ECHO_START;
#if ENABLED(SKEW_CORRECTION_FOR_Z)
SERIAL_ECHOPAIR(" M852 I", LINEAR_UNIT(planner.xy_skew_factor));
SERIAL_ECHOPAIR(" J", LINEAR_UNIT(planner.xz_skew_factor));
SERIAL_ECHOLNPAIR(" K", LINEAR_UNIT(planner.yz_skew_factor));
#else
SERIAL_ECHOLNPAIR(" M852 S", LINEAR_UNIT(planner.xy_skew_factor));
#endif
#endif

/**
* TMC2130 stepper driver current
*/
Expand Down
Loading