Skip to content

Commit df4e022

Browse files
🚸 Fix, extend X Axis Twist Compensation (MarlinFirmware#23745)
Co-authored-by: Scott Lahteine <[email protected]>
1 parent b123fa7 commit df4e022

File tree

14 files changed

+142
-34
lines changed

14 files changed

+142
-34
lines changed

Marlin/src/core/serial.cpp

+8
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,14 @@ void serial_error_start() { static PGMSTR(errormagic, "Error:"); serial_print_P(
7878

7979
void serial_spaces(uint8_t count) { count *= (PROPORTIONAL_FONT_RATIO); while (count--) SERIAL_CHAR(' '); }
8080

81+
void serial_offset(const_float_t v, const uint8_t sp/*=0*/) {
82+
if (v == 0 && sp == 1)
83+
SERIAL_CHAR(' ');
84+
else if (v > 0 || (v == 0 && sp == 2))
85+
SERIAL_CHAR('+');
86+
SERIAL_DECIMAL(v);
87+
}
88+
8189
void serial_ternary(const bool onoff, FSTR_P const pre, FSTR_P const on, FSTR_P const off, FSTR_P const post/*=nullptr*/) {
8290
if (pre) serial_print(pre);
8391
serial_print(onoff ? on : off);

Marlin/src/core/serial.h

+1
Original file line numberDiff line numberDiff line change
@@ -345,6 +345,7 @@ void serialprint_onoff(const bool onoff);
345345
void serialprintln_onoff(const bool onoff);
346346
void serialprint_truefalse(const bool tf);
347347
void serial_spaces(uint8_t count);
348+
void serial_offset(const_float_t v, const uint8_t sp=0); // For v==0 draw space (sp==1) or plus (sp==2)
348349

349350
void print_bin(const uint16_t val);
350351
void print_pos(LINEAR_AXIS_ARGS(const_float_t), FSTR_P const prefix=nullptr, FSTR_P const suffix=nullptr);

Marlin/src/core/utility.cpp

+1-3
Original file line numberDiff line numberDiff line change
@@ -126,10 +126,8 @@ void safe_delay(millis_t ms) {
126126
#if ABL_PLANAR
127127
SERIAL_ECHOPGM("ABL Adjustment");
128128
LOOP_LINEAR_AXES(a) {
129-
const float v = planner.get_axis_position_mm(AxisEnum(a)) - current_position[a];
130129
SERIAL_CHAR(' ', AXIS_CHAR(a));
131-
if (v > 0) SERIAL_CHAR('+');
132-
SERIAL_DECIMAL(v);
130+
serial_offset(planner.get_axis_position_mm(AxisEnum(a)) - current_position[a]);
133131
}
134132
#else
135133
#if ENABLED(AUTO_BED_LEVELING_UBL)

Marlin/src/feature/bedlevel/ubl/ubl.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -213,8 +213,8 @@ void unified_bed_leveling::display_map(const uint8_t map_type) {
213213
else if (isnan(f))
214214
SERIAL_ECHOF(human ? F(" . ") : F("NAN"));
215215
else if (human || csv) {
216-
if (human && f >= 0.0) SERIAL_CHAR(f > 0 ? '+' : ' '); // Display sign also for positive numbers (' ' for 0)
217-
SERIAL_ECHO_F(f, 3); // Positive: 5 digits, Negative: 6 digits
216+
if (human && f >= 0) SERIAL_CHAR(f > 0 ? '+' : ' '); // Display sign also for positive numbers (' ' for 0)
217+
SERIAL_DECIMAL(f); // Positive: 5 digits, Negative: 6 digits
218218
}
219219
if (csv && i < (GRID_MAX_POINTS_X) - 1) SERIAL_CHAR('\t');
220220

Marlin/src/feature/mixing.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ void Mixer::normalize(const uint8_t tool_index) {
6363
#ifdef MIXER_NORMALIZER_DEBUG
6464
SERIAL_ECHOPGM("Mixer: Old relation : [ ");
6565
MIXER_STEPPER_LOOP(i) {
66-
SERIAL_ECHO_F(collector[i] / csum, 3);
66+
SERIAL_DECIMAL(collector[i] / csum);
6767
SERIAL_CHAR(' ');
6868
}
6969
SERIAL_ECHOLNPGM("]");

Marlin/src/feature/x_twist.cpp

+7-12
Original file line numberDiff line numberDiff line change
@@ -36,23 +36,19 @@ void XATC::reset() {
3636
constexpr float xzo[] = XATC_Z_OFFSETS;
3737
static_assert(COUNT(xzo) == XATC_MAX_POINTS, "XATC_Z_OFFSETS is the wrong size.");
3838
COPY(z_offset, xzo);
39-
xatc.spacing = (probe.max_x() - probe.min_x()) / (XATC_MAX_POINTS - 1);
40-
xatc.start = probe.min_x();
39+
start = probe.min_x();
40+
spacing = (probe.max_x() - start) / (XATC_MAX_POINTS - 1);
4141
enabled = true;
4242
}
4343

4444
void XATC::print_points() {
4545
SERIAL_ECHOLNPGM(" X-Twist Correction:");
4646
LOOP_L_N(x, XATC_MAX_POINTS) {
4747
SERIAL_CHAR(' ');
48-
if (!isnan(z_offset[x])) {
49-
if (z_offset[x] >= 0) SERIAL_CHAR('+');
50-
SERIAL_ECHO_F(z_offset[x], 3);
51-
}
52-
else {
53-
LOOP_L_N(i, 6)
54-
SERIAL_CHAR(i ? '=' : ' ');
55-
}
48+
if (!isnan(z_offset[x]))
49+
serial_offset(z_offset[x]);
50+
else
51+
LOOP_L_N(i, 6) SERIAL_CHAR(i ? '=' : ' ');
5652
}
5753
SERIAL_EOL();
5854
}
@@ -63,8 +59,7 @@ float XATC::compensation(const xy_pos_t &raw) {
6359
if (!enabled) return 0;
6460
if (NEAR_ZERO(spacing)) return 0;
6561
float t = (raw.x - start) / spacing;
66-
int i = FLOOR(t);
67-
LIMIT(i, 0, XATC_MAX_POINTS - 2);
62+
const int i = constrain(FLOOR(t), 0, XATC_MAX_POINTS - 2);
6863
t -= i;
6964
return lerp(t, z_offset[i], z_offset[i + 1]);
7065
}

Marlin/src/gcode/calibrate/G33.cpp

+1-2
Original file line numberDiff line numberDiff line change
@@ -98,8 +98,7 @@ void ac_cleanup(TERN_(HAS_MULTI_HOTEND, const uint8_t old_tool_index)) {
9898
void print_signed_float(FSTR_P const prefix, const_float_t f) {
9999
SERIAL_ECHOPGM(" ");
100100
SERIAL_ECHOF(prefix, AS_CHAR(':'));
101-
if (f >= 0) SERIAL_CHAR('+');
102-
SERIAL_ECHO_F(f, 2);
101+
serial_offset(f);
103102
}
104103

105104
/**

Marlin/src/gcode/gcode.h

+5
Original file line numberDiff line numberDiff line change
@@ -1195,6 +1195,11 @@ class GcodeSuite {
11951195
static void M1000();
11961196
#endif
11971197

1198+
#if ENABLED(X_AXIS_TWIST_COMPENSATION)
1199+
static void M423();
1200+
static void M423_report(const bool forReplay=true);
1201+
#endif
1202+
11981203
#if ENABLED(SDSUPPORT)
11991204
static void M1001();
12001205
#endif

Marlin/src/gcode/probe/M423.cpp

+99
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
/**
2+
* Marlin 3D Printer Firmware
3+
* Copyright (c) 2022 MarlinFirmware [https://github.com/MarlinFirmware/Marlin]
4+
*
5+
* Based on Sprinter and grbl.
6+
* Copyright (c) 2011 Camiel Gubbels / Erik van der Zalm
7+
*
8+
* This program is free software: you can redistribute it and/or modify
9+
* it under the terms of the GNU General Public License as published by
10+
* the Free Software Foundation, either version 3 of the License, or
11+
* (at your option) any later version.
12+
*
13+
* This program is distributed in the hope that it will be useful,
14+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
15+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16+
* GNU General Public License for more details.
17+
*
18+
* You should have received a copy of the GNU General Public License
19+
* along with this program. If not, see <https://www.gnu.org/licenses/>.
20+
*
21+
*/
22+
23+
/**
24+
* M423.cpp - X-Axis Twist Compensation
25+
*/
26+
27+
#include "../../inc/MarlinConfig.h"
28+
29+
#if ENABLED(X_AXIS_TWIST_COMPENSATION)
30+
31+
#include "../gcode.h"
32+
#include "../../feature/x_twist.h"
33+
#include "../../module/probe.h"
34+
35+
/**
36+
* M423: Set a Z offset for X-Twist (added to the mesh on future G29).
37+
* M423 [R] [A<startx>] [I<interval>] [X<index> Z<offset>]
38+
*
39+
* R - Reset the twist compensation data
40+
* A<linear> - Set the X twist starting X position
41+
* E<linear> - Set the X twist ending X position
42+
* I<linear> - Set the X twist X-spacing directly
43+
* X<index> - Index of a Z value in the list
44+
* Z<linear> - A Z value to set
45+
*/
46+
void GcodeSuite::M423() {
47+
48+
bool do_report = true;
49+
float new_spacing = 0;
50+
51+
if (parser.seen_test('R')) {
52+
do_report = false;
53+
xatc.reset();
54+
}
55+
if (parser.seenval('A')) {
56+
do_report = false;
57+
xatc.start = parser.value_float();
58+
new_spacing = (probe.max_x() - xatc.start) / (XATC_MAX_POINTS - 1);
59+
}
60+
if (parser.seenval('E')) {
61+
do_report = false;
62+
new_spacing = (parser.value_float() - xatc.start) / (XATC_MAX_POINTS - 1);
63+
}
64+
else if (parser.seenval('I')) {
65+
do_report = false;
66+
new_spacing = parser.value_float();
67+
}
68+
69+
if (new_spacing) xatc.spacing = new_spacing;
70+
71+
if (parser.seenval('X')) {
72+
do_report = false;
73+
const int8_t x = parser.value_int();
74+
if (!WITHIN(x, 0, XATC_MAX_POINTS - 1))
75+
SERIAL_ECHOLNPGM("?(X) out of range (0..", XATC_MAX_POINTS - 1, ").");
76+
else {
77+
if (parser.seenval('Z'))
78+
xatc.z_offset[x] = parser.value_linear_units();
79+
else
80+
SERIAL_ECHOLNPGM("?(Z) required.");
81+
}
82+
}
83+
84+
if (do_report) M423_report();
85+
86+
}
87+
88+
void GcodeSuite::M423_report(const bool forReplay/*=true*/) {
89+
report_heading(forReplay, F("X-Twist Correction"));
90+
SERIAL_ECHOLNPGM(" M423 A", xatc.start, " I", xatc.spacing);
91+
LOOP_L_N(x, XATC_MAX_POINTS) {
92+
const float z = xatc.z_offset[x];
93+
SERIAL_ECHOPGM(" M423 X", x, " Z");
94+
serial_offset(isnan(z) ? 0 : z);
95+
SERIAL_EOL();
96+
}
97+
}
98+
99+
#endif // X_AXIS_TWIST_COMPENSATION

Marlin/src/lcd/menu/menu_x_twist.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,7 @@ void xatc_wizard_goto_next_point() {
153153
measured_z = probe.probe_at_point(x, XATC_Y_POSITION, PROBE_PT_STOW);
154154
xatc.set_enabled(true);
155155
current_position += probe.offset_xy;
156-
current_position.z = XATC_START_Z - probe.offset.z + measured_z;
156+
current_position.z = (XATC_START_Z) - probe.offset.z + measured_z;
157157
line_to_current_position(MMM_TO_MMS(XY_PROBE_FEEDRATE));
158158
ui.wait_for_move = false;
159159
}

Marlin/src/libs/vector_3.cpp

+1-2
Original file line numberDiff line numberDiff line change
@@ -141,8 +141,7 @@ void matrix_3x3::debug(FSTR_P const title) {
141141
if (title) SERIAL_ECHOLNF(title);
142142
LOOP_L_N(i, 3) {
143143
LOOP_L_N(j, 3) {
144-
if (vectors[i][j] >= 0.0) SERIAL_CHAR('+');
145-
SERIAL_ECHO_F(vectors[i][j], 6);
144+
serial_offset(vectors[i][j], 2);
146145
SERIAL_CHAR(' ');
147146
}
148147
SERIAL_EOL();

Marlin/src/module/settings.cpp

+13-9
Original file line numberDiff line numberDiff line change
@@ -277,7 +277,7 @@ typedef struct SettingsDataStruct {
277277
// X_AXIS_TWIST_COMPENSATION
278278
//
279279
#if ENABLED(X_AXIS_TWIST_COMPENSATION)
280-
XATC xatc; // TBD
280+
XATC xatc; // M423 X Z
281281
#endif
282282

283283
//
@@ -901,7 +901,9 @@ void MarlinSettings::postprocess() {
901901
//
902902
#if ENABLED(X_AXIS_TWIST_COMPENSATION)
903903
_FIELD_TEST(xatc);
904-
EEPROM_WRITE(xatc);
904+
EEPROM_WRITE(xatc.spacing);
905+
EEPROM_WRITE(xatc.start);
906+
EEPROM_WRITE(xatc.z_offset);
905907
#endif
906908

907909
//
@@ -1809,7 +1811,10 @@ void MarlinSettings::postprocess() {
18091811
// X Axis Twist Compensation
18101812
//
18111813
#if ENABLED(X_AXIS_TWIST_COMPENSATION)
1812-
EEPROM_READ(xatc);
1814+
_FIELD_TEST(xatc);
1815+
EEPROM_READ(xatc.spacing);
1816+
EEPROM_READ(xatc.start);
1817+
EEPROM_READ(xatc.z_offset);
18131818
#endif
18141819

18151820
//
@@ -3337,14 +3342,13 @@ void MarlinSettings::reset() {
33373342

33383343
#endif
33393344

3340-
// TODO: Create G-code for settings
3341-
//#if ENABLED(X_AXIS_TWIST_COMPENSATION)
3342-
// CONFIG_ECHO_START();
3343-
// xatc.print_points();
3344-
//#endif
3345-
33463345
#endif // HAS_LEVELING
33473346

3347+
//
3348+
// X Axis Twist Compensation
3349+
//
3350+
TERN_(X_AXIS_TWIST_COMPENSATION, gcode.M423_report(forReplay));
3351+
33483352
//
33493353
// Editable Servo Angles
33503354
//

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/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> +<src/gcode/probe/M423.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
@@ -150,7 +150,7 @@ default_src_filter = +<src/*> -<src/config> -<src/HAL> +<src/HAL/shared>
150150
-<src/feature/tmc_util.cpp> -<src/module/stepper/trinamic.cpp>
151151
-<src/feature/tramming.cpp>
152152
-<src/feature/twibus.cpp>
153-
-<src/feature/x_twist.cpp>
153+
-<src/feature/x_twist.cpp> -<src/gcode/probe/M423.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)