@@ -1489,6 +1489,14 @@ void unified_bed_leveling::smart_fill_mesh() {
14891489 xy_float_t points[3 ];
14901490 probe.get_three_points (points);
14911491
1492+ #if ENABLED(UBL_TILT_ON_MESH_POINTS_3POINT)
1493+ mesh_index_pair cpos[3 ];
1494+ LOOP_L_N (ix, 3 ) { // Convert points to coordinates of mesh points
1495+ cpos[ix] = find_closest_mesh_point_of_type (REAL, points[ix], true );
1496+ points[ix] = cpos[ix].meshpos ();
1497+ }
1498+ #endif
1499+
14921500 #if ENABLED(VALIDATE_MESH_TILT)
14931501 float gotz[3 ]; // Used for algorithm validation below
14941502 #endif
@@ -1500,7 +1508,7 @@ void unified_bed_leveling::smart_fill_mesh() {
15001508 measured_z = probe.probe_at_point (points[i], i < 2 ? PROBE_PT_RAISE : PROBE_PT_LAST_STOW, param.V_verbosity );
15011509 if ((abort_flag = isnan (measured_z))) break ;
15021510
1503- measured_z -= get_z_correction (points[i]);
1511+ measured_z -= TERN (UBL_TILT_ON_MESH_POINTS_3POINT, z_values[cpos[i]. pos . x ][cpos[i]. pos . y ], get_z_correction (points[i]) );
15041512 TERN_ (VALIDATE_MESH_TILT, gotz[i] = measured_z);
15051513
15061514 if (param.V_verbosity > 3 ) { serial_spaces (16 ); SERIAL_ECHOLNPGM (" Corrected_Z=" , measured_z); }
@@ -1518,16 +1526,14 @@ void unified_bed_leveling::smart_fill_mesh() {
15181526 }
15191527 else { // !do_3_pt_leveling
15201528
1521- #ifdef G29J_MESH_TILT_MARGIN
1522- const float x_min = _MAX (probe.min_x () + (G29J_MESH_TILT_MARGIN), X_MIN_POS),
1523- x_max = _MIN (probe.max_x () - (G29J_MESH_TILT_MARGIN), X_MAX_POS),
1524- y_min = _MAX (probe.min_y () + (G29J_MESH_TILT_MARGIN), Y_MIN_POS),
1525- y_max = _MIN (probe.max_y () - (G29J_MESH_TILT_MARGIN), Y_MAX_POS);
1526- #else
1527- const float x_min = probe.min_x (), x_max = probe.max_x (),
1528- y_min = probe.min_y (), y_max = probe.max_y ();
1529+ #ifndef G29J_MESH_TILT_MARGIN
1530+ #define G29J_MESH_TILT_MARGIN 0
15291531 #endif
1530- const float dx = (x_max - x_min) / (param.J_grid_size - 1 ),
1532+ const float x_min = _MAX ((X_MIN_POS) + (G29J_MESH_TILT_MARGIN), MESH_MIN_X, probe.min_x ()),
1533+ x_max = _MIN ((X_MAX_POS) - (G29J_MESH_TILT_MARGIN), MESH_MAX_X, probe.max_x ()),
1534+ y_min = _MAX ((Y_MIN_POS) + (G29J_MESH_TILT_MARGIN), MESH_MIN_Y, probe.min_y ()),
1535+ y_max = _MIN ((Y_MAX_POS) - (G29J_MESH_TILT_MARGIN), MESH_MAX_Y, probe.max_y ()),
1536+ dx = (x_max - x_min) / (param.J_grid_size - 1 ),
15311537 dy = (y_max - y_min) / (param.J_grid_size - 1 );
15321538
15331539 bool zig_zag = false ;
@@ -1541,18 +1547,35 @@ void unified_bed_leveling::smart_fill_mesh() {
15411547 LOOP_L_N (iy, param.J_grid_size ) {
15421548 rpos.y = y_min + dy * (zig_zag ? param.J_grid_size - 1 - iy : iy);
15431549
1550+ #if ENABLED(UBL_TILT_ON_MESH_POINTS)
1551+ #if ENABLED(DEBUG_LEVELING_FEATURE)
1552+ xy_pos_t oldRpos;
1553+ if (DEBUGGING (LEVELING)) oldRpos = rpos;
1554+ #endif
1555+ mesh_index_pair cpos;
1556+ rpos -= probe.offset ;
1557+ cpos = find_closest_mesh_point_of_type (REAL, rpos, true );
1558+ rpos = cpos.meshpos ();
1559+ #endif
1560+
15441561 SERIAL_ECHOLNPGM (" Tilting mesh point " , point_num, " /" , total_points, " \n " );
15451562 TERN_ (HAS_STATUS_MESSAGE, ui.status_printf (0 , F (S_FMT " %i/%i" ), GET_TEXT (MSG_LCD_TILTING_MESH), point_num, total_points));
15461563
15471564 measured_z = probe.probe_at_point (rpos, parser.seen_test (' E' ) ? PROBE_PT_STOW : PROBE_PT_RAISE, param.V_verbosity ); // TODO: Needs error handling
15481565
15491566 if ((abort_flag = isnan (measured_z))) break ;
15501567
1551- const float zcorr = get_z_correction (rpos);
1568+ const float zcorr = TERN (UBL_TILT_ON_MESH_POINTS, z_values[cpos. pos . x ][cpos. pos . y ], get_z_correction (rpos) );
15521569
15531570 #if ENABLED(DEBUG_LEVELING_FEATURE)
15541571 if (DEBUGGING (LEVELING)) {
15551572 const xy_pos_t lpos = rpos.asLogical ();
1573+ #if ENABLED(UBL_TILT_ON_MESH_POINTS)
1574+ const xy_pos_t oldLpos = oldRpos.asLogical ();
1575+ DEBUG_ECHOPGM (" Calculated point: (" ); DEBUG_ECHO_F (oldRpos.x , 7 ); DEBUG_CHAR (' ,' ); DEBUG_ECHO_F (oldRpos.y , 7 );
1576+ DEBUG_ECHOPAIR_F (" ) logical: (" , oldLpos.x , 7 ); DEBUG_CHAR (' ,' ); DEBUG_ECHO_F (oldLpos.y , 7 );
1577+ DEBUG_ECHOPGM (" )\n Selected mesh point: " );
1578+ #endif
15561579 DEBUG_CHAR (' (' ); DEBUG_ECHO_F (rpos.x , 7 ); DEBUG_CHAR (' ,' ); DEBUG_ECHO_F (rpos.y , 7 );
15571580 DEBUG_ECHOPAIR_F (" ) logical: (" , lpos.x , 7 ); DEBUG_CHAR (' ,' ); DEBUG_ECHO_F (lpos.y , 7 );
15581581 DEBUG_ECHOPAIR_F (" ) measured: " , measured_z, 7 );
0 commit comments