Skip to content

Commit 2e39bc3

Browse files
authored
🚸 Universal X_AXIS_TWIST_COMPENSATION (MarlinFirmware#23828)
1 parent b07a34e commit 2e39bc3

File tree

10 files changed

+67
-31
lines changed

10 files changed

+67
-31
lines changed

Marlin/Configuration_adv.h

+2-1
Original file line numberDiff line numberDiff line change
@@ -1299,7 +1299,7 @@
12991299

13001300
#if HAS_MARLINUI_MENU
13011301

1302-
#if BOTH(HAS_BED_PROBE, AUTO_BED_LEVELING_BILINEAR)
1302+
#if HAS_BED_PROBE
13031303
// Add calibration in the Probe Offsets menu to compensate for X-axis twist.
13041304
//#define X_AXIS_TWIST_COMPENSATION
13051305
#if ENABLED(X_AXIS_TWIST_COMPENSATION)
@@ -1311,6 +1311,7 @@
13111311
#define XATC_START_Z 0.0
13121312
#define XATC_MAX_POINTS 3 // Number of points to probe in the wizard
13131313
#define XATC_Y_POSITION Y_CENTER // (mm) Y position to probe
1314+
#define XATC_Z_OFFSETS { 0, 0, 0 } // Z offsets for X axis sample points
13141315
#endif
13151316
#endif
13161317

Marlin/src/feature/bedlevel/bedlevel.h

-3
Original file line numberDiff line numberDiff line change
@@ -63,9 +63,6 @@ class TemporaryBedLevelingState {
6363

6464
#if ENABLED(AUTO_BED_LEVELING_BILINEAR)
6565
#include "abl/abl.h"
66-
#if ENABLED(X_AXIS_TWIST_COMPENSATION)
67-
#include "abl/x_twist.h"
68-
#endif
6966
#elif ENABLED(AUTO_BED_LEVELING_UBL)
7067
#include "ubl/ubl.h"
7168
#elif ENABLED(MESH_BED_LEVELING)

Marlin/src/feature/bedlevel/abl/x_twist.cpp Marlin/src/feature/x_twist.cpp

+12-3
Original file line numberDiff line numberDiff line change
@@ -19,16 +19,24 @@
1919
* along with this program. If not, see <https://www.gnu.org/licenses/>.
2020
*
2121
*/
22-
#include "../../../inc/MarlinConfig.h"
22+
#include "../inc/MarlinConfig.h"
2323

2424
#if ENABLED(X_AXIS_TWIST_COMPENSATION)
2525

26-
#include "../bedlevel.h"
26+
#include "x_twist.h"
2727

2828
XATC xatc;
2929

30+
bool XATC::enabled = true;
3031
float XATC::spacing, XATC::start;
31-
xatc_array_t XATC::z_offset;
32+
xatc_array_t XATC::z_offset; // Initialized by settings.load()
33+
34+
void XATC::reset() {
35+
constexpr float xzo[] = XATC_Z_OFFSETS;
36+
static_assert(COUNT(xzo) == XATC_MAX_POINTS, "XATC_Z_OFFSETS is the wrong size.");
37+
enabled = false;
38+
COPY(z_offset, xzo);
39+
}
3240

3341
void XATC::print_points() {
3442
SERIAL_ECHOLNPGM(" X-Twist Correction:");
@@ -49,6 +57,7 @@ void XATC::print_points() {
4957
float lerp(const_float_t t, const_float_t a, const_float_t b) { return a + t * (b - a); }
5058

5159
float XATC::compensation(const xy_pos_t &raw) {
60+
if (!enabled) return 0;
5261
if (NEAR_ZERO(spacing)) return 0;
5362
float t = (raw.x - start) / spacing;
5463
int i = FLOOR(t);

Marlin/src/feature/bedlevel/abl/x_twist.h Marlin/src/feature/x_twist.h

+4-1
Original file line numberDiff line numberDiff line change
@@ -21,15 +21,18 @@
2121
*/
2222
#pragma once
2323

24-
#include "../../../inc/MarlinConfigPre.h"
24+
#include "../inc/MarlinConfigPre.h"
2525

2626
typedef float xatc_array_t[XATC_MAX_POINTS];
2727

2828
class XATC {
29+
static bool enabled;
2930
public:
3031
static float spacing, start;
3132
static xatc_array_t z_offset;
3233

34+
static void reset();
35+
static void set_enabled(const bool ena) { enabled = ena; }
3336
static float compensation(const xy_pos_t &raw);
3437
static void print_points();
3538
};

Marlin/src/gcode/bedlevel/abl/G29.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -681,7 +681,7 @@ G29_TYPE GcodeSuite::G29() {
681681
#elif ENABLED(AUTO_BED_LEVELING_BILINEAR)
682682

683683
const float z = abl.measured_z + abl.Z_offset;
684-
z_values[abl.meshCount.x][abl.meshCount.y] = z PLUS_TERN0(X_AXIS_TWIST_COMPENSATION, xatc.compensation(abl.probePos));
684+
z_values[abl.meshCount.x][abl.meshCount.y] = z;
685685
TERN_(EXTENSIBLE_UI, ExtUI::onMeshUpdate(abl.meshCount, z));
686686

687687
#endif

Marlin/src/lcd/menu/menu_x_twist.cpp

+3
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
#include "menu_addon.h"
2828
#include "../../module/planner.h"
2929
#include "../../feature/bedlevel/bedlevel.h"
30+
#include "../../feature/x_twist.h"
3031
#include "../../module/motion.h"
3132
#include "../../gcode/queue.h"
3233
#include "../../module/probe.h"
@@ -148,7 +149,9 @@ void xatc_wizard_goto_next_point() {
148149
// Deploy certain probes before starting probing
149150
TERN_(BLTOUCH, do_z_clearance(Z_CLEARANCE_DEPLOY_PROBE));
150151

152+
xatc.set_enabled(false);
151153
measured_z = probe.probe_at_point(x, XATC_Y_POSITION, PROBE_PT_STOW);
154+
xatc.set_enabled(true);
152155
current_position += probe.offset_xy;
153156
current_position.z = XATC_START_Z - probe.offset.z + measured_z;
154157
line_to_current_position(MMM_TO_MMS(XY_PROBE_FEEDRATE));

Marlin/src/module/probe.cpp

+5
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,10 @@
8181
#include "../feature/probe_temp_comp.h"
8282
#endif
8383

84+
#if ENABLED(X_AXIS_TWIST_COMPENSATION)
85+
#include "../feature/x_twist.h"
86+
#endif
87+
8488
#if ENABLED(EXTENSIBLE_UI)
8589
#include "../lcd/extui/ui_api.h"
8690
#elif ENABLED(DWIN_CREALITY_LCD_ENHANCED)
@@ -808,6 +812,7 @@ float Probe::probe_at_point(const_float_t rx, const_float_t ry, const ProbePtRai
808812
if (!deploy()) {
809813
measured_z = run_z_probe(sanity_check) + offset.z;
810814
TERN_(HAS_PTC, ptc.apply_compensation(measured_z));
815+
TERN_(X_AXIS_TWIST_COMPENSATION, measured_z += xatc.compensation(npos + offset_xy));
811816
}
812817
if (!isnan(measured_z)) {
813818
const bool big_raise = raise_after == PROBE_PT_BIG_RAISE;

Marlin/src/module/settings.cpp

+38-20
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@
6464
#if HAS_LEVELING
6565
#include "../feature/bedlevel/bedlevel.h"
6666
#if ENABLED(X_AXIS_TWIST_COMPENSATION)
67-
#include "../feature/bedlevel/abl/x_twist.h"
67+
#include "../feature/x_twist.h"
6868
#endif
6969
#endif
7070

@@ -269,13 +269,17 @@ typedef struct SettingsDataStruct {
269269
xy_pos_t bilinear_grid_spacing, bilinear_start; // G29 L F
270270
#if ENABLED(AUTO_BED_LEVELING_BILINEAR)
271271
bed_mesh_t z_values; // G29
272-
#if ENABLED(X_AXIS_TWIST_COMPENSATION)
273-
XATC xatc; // TBD
274-
#endif
275272
#else
276273
float z_values[3][3];
277274
#endif
278275

276+
//
277+
// X_AXIS_TWIST_COMPENSATION
278+
//
279+
#if ENABLED(X_AXIS_TWIST_COMPENSATION)
280+
XATC xatc; // TBD
281+
#endif
282+
279283
//
280284
// AUTO_BED_LEVELING_UBL
281285
//
@@ -298,7 +302,7 @@ typedef struct SettingsDataStruct {
298302
int16_t z_offsets_bed[COUNT(ptc.z_offsets_bed)]; // M871 B I V
299303
#endif
300304
#if ENABLED(PTC_HOTEND)
301-
int16_t z_offsets_hotend[COUNT(ptc.z_offsets_hotend)]; // M871 E I V
305+
int16_t z_offsets_hotend[COUNT(ptc.z_offsets_hotend)]; // M871 E I V
302306
#endif
303307
#endif
304308

@@ -873,9 +877,6 @@ void MarlinSettings::postprocess() {
873877
sizeof(z_values) == (GRID_MAX_POINTS) * sizeof(z_values[0][0]),
874878
"Bilinear Z array is the wrong size."
875879
);
876-
#if ENABLED(X_AXIS_TWIST_COMPENSATION)
877-
static_assert(COUNT(xatc.z_offset) == XATC_MAX_POINTS, "XATC Z-offset mesh is the wrong size.");
878-
#endif
879880
#else
880881
const xy_pos_t bilinear_start{0}, bilinear_grid_spacing{0};
881882
#endif
@@ -889,15 +890,20 @@ void MarlinSettings::postprocess() {
889890

890891
#if ENABLED(AUTO_BED_LEVELING_BILINEAR)
891892
EEPROM_WRITE(z_values); // 9-256 floats
892-
#if ENABLED(X_AXIS_TWIST_COMPENSATION)
893-
EEPROM_WRITE(xatc);
894-
#endif
895893
#else
896894
dummyf = 0;
897895
for (uint16_t q = grid_max_x * grid_max_y; q--;) EEPROM_WRITE(dummyf);
898896
#endif
899897
}
900898

899+
//
900+
// X Axis Twist Compensation
901+
//
902+
#if ENABLED(X_AXIS_TWIST_COMPENSATION)
903+
_FIELD_TEST(xatc);
904+
EEPROM_WRITE(xatc);
905+
#endif
906+
901907
//
902908
// Unified Bed Leveling
903909
//
@@ -1785,9 +1791,6 @@ void MarlinSettings::postprocess() {
17851791
EEPROM_READ(bilinear_grid_spacing); // 2 ints
17861792
EEPROM_READ(bilinear_start); // 2 ints
17871793
EEPROM_READ(z_values); // 9 to 256 floats
1788-
#if ENABLED(X_AXIS_TWIST_COMPENSATION)
1789-
EEPROM_READ(xatc);
1790-
#endif
17911794
}
17921795
else // EEPROM data is stale
17931796
#endif // AUTO_BED_LEVELING_BILINEAR
@@ -1800,6 +1803,13 @@ void MarlinSettings::postprocess() {
18001803
}
18011804
}
18021805

1806+
//
1807+
// X Axis Twist Compensation
1808+
//
1809+
#if ENABLED(X_AXIS_TWIST_COMPENSATION)
1810+
EEPROM_READ(xatc);
1811+
#endif
1812+
18031813
//
18041814
// Unified Bed Leveling active state
18051815
//
@@ -2849,6 +2859,14 @@ void MarlinSettings::reset() {
28492859
TERN_(ENABLE_LEVELING_FADE_HEIGHT, new_z_fade_height = (DEFAULT_LEVELING_FADE_HEIGHT));
28502860
TERN_(HAS_LEVELING, reset_bed_level());
28512861

2862+
//
2863+
// X Axis Twist Compensation
2864+
//
2865+
TERN_(X_AXIS_TWIST_COMPENSATION, xatc.reset());
2866+
2867+
//
2868+
// Nozzle-to-probe Offset
2869+
//
28522870
#if HAS_BED_PROBE
28532871
constexpr float dpo[] = NOZZLE_TO_PROBE_OFFSET;
28542872
static_assert(COUNT(dpo) == LINEAR_AXES, "NOZZLE_TO_PROBE_OFFSET must contain offsets for each linear axis X, Y, Z....");
@@ -3313,14 +3331,14 @@ void MarlinSettings::reset() {
33133331
}
33143332
}
33153333

3316-
// TODO: Create G-code for settings
3317-
//#if ENABLED(X_AXIS_TWIST_COMPENSATION)
3318-
// CONFIG_ECHO_START();
3319-
// xatc.print_points();
3320-
//#endif
3321-
33223334
#endif
33233335

3336+
// TODO: Create G-code for settings
3337+
//#if ENABLED(X_AXIS_TWIST_COMPENSATION)
3338+
// CONFIG_ECHO_START();
3339+
// xatc.print_points();
3340+
//#endif
3341+
33243342
#endif // HAS_LEVELING
33253343

33263344
//

ini/features.ini

+1-1
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ USB_FLASH_DRIVE_SUPPORT = src_filter=+<src/sd/usb_flashdrive/Sd2C
9898
HAS_MCP3426_ADC = src_filter=+<src/feature/adc> +<src/gcode/feature/adc>
9999
AUTO_BED_LEVELING_BILINEAR = src_filter=+<src/feature/bedlevel/abl>
100100
AUTO_BED_LEVELING_(3POINT|(BI)?LINEAR) = src_filter=+<src/gcode/bedlevel/abl>
101-
X_AXIS_TWIST_COMPENSATION = src_filter=+<src/feature/bedlevel/abl/x_twist.cpp> +<src/lcd/menu/menu_x_twist.cpp>
101+
X_AXIS_TWIST_COMPENSATION = src_filter=+<src/feature/x_twist.cpp> +<src/lcd/menu/menu_x_twist.cpp>
102102
MESH_BED_LEVELING = src_filter=+<src/feature/bedlevel/mbl> +<src/gcode/bedlevel/mbl>
103103
AUTO_BED_LEVELING_UBL = src_filter=+<src/feature/bedlevel/ubl> +<src/gcode/bedlevel/ubl>
104104
UBL_HILBERT_CURVE = src_filter=+<src/feature/bedlevel/hilbert_curve.cpp>

platformio.ini

+1-1
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,6 @@ default_src_filter = +<src/*> -<src/config> -<src/HAL> +<src/HAL/shared>
100100
-<src/feature/backlash.cpp>
101101
-<src/feature/baricuda.cpp> -<src/gcode/feature/baricuda>
102102
-<src/feature/bedlevel/abl> -<src/gcode/bedlevel/abl>
103-
-<src/feature/bedlevel/abl/x_twist.cpp>
104103
-<src/feature/bedlevel/mbl> -<src/gcode/bedlevel/mbl>
105104
-<src/feature/bedlevel/ubl> -<src/gcode/bedlevel/ubl>
106105
-<src/feature/bedlevel/hilbert_curve.cpp>
@@ -151,6 +150,7 @@ default_src_filter = +<src/*> -<src/config> -<src/HAL> +<src/HAL/shared>
151150
-<src/feature/tmc_util.cpp> -<src/module/stepper/trinamic.cpp>
152151
-<src/feature/tramming.cpp>
153152
-<src/feature/twibus.cpp>
153+
-<src/feature/x_twist.cpp>
154154
-<src/feature/z_stepper_align.cpp>
155155
-<src/gcode/bedlevel/G26.cpp>
156156
-<src/gcode/bedlevel/G35.cpp>

0 commit comments

Comments
 (0)