2016-03-25 01:19:46 -05:00
|
|
|
/**
|
2016-03-24 13:01:20 -05:00
|
|
|
* Marlin 3D Printer Firmware
|
|
|
|
* Copyright (C) 2016 MarlinFirmware [https://github.com/MarlinFirmware/Marlin]
|
|
|
|
*
|
|
|
|
* Based on Sprinter and grbl.
|
|
|
|
* Copyright (C) 2011 Camiel Gubbels / Erik van der Zalm
|
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2017-09-08 15:35:25 -05:00
|
|
|
#include "../../../inc/MarlinConfig.h"
|
2015-03-15 04:43:26 -05:00
|
|
|
|
2015-07-31 00:21:18 -05:00
|
|
|
#if ENABLED(MESH_BED_LEVELING)
|
2015-03-15 04:43:26 -05:00
|
|
|
|
2017-09-06 06:28:32 -05:00
|
|
|
#include "mesh_bed_leveling.h"
|
|
|
|
|
2017-09-08 15:35:25 -05:00
|
|
|
#include "../../../module/motion.h"
|
|
|
|
#include "../../../feature/bedlevel/bedlevel.h"
|
2017-09-07 22:33:16 -05:00
|
|
|
|
2015-03-25 22:36:24 -05:00
|
|
|
mesh_bed_leveling mbl;
|
2015-03-15 04:43:26 -05:00
|
|
|
|
2017-10-13 17:21:25 -05:00
|
|
|
bool mesh_bed_leveling::has_mesh;
|
2017-03-31 01:28:07 -05:00
|
|
|
|
|
|
|
float mesh_bed_leveling::z_offset,
|
2017-04-12 17:24:05 -05:00
|
|
|
mesh_bed_leveling::z_values[GRID_MAX_POINTS_X][GRID_MAX_POINTS_Y],
|
2017-04-05 22:29:44 -05:00
|
|
|
mesh_bed_leveling::index_to_xpos[GRID_MAX_POINTS_X],
|
|
|
|
mesh_bed_leveling::index_to_ypos[GRID_MAX_POINTS_Y];
|
2017-03-31 01:28:07 -05:00
|
|
|
|
|
|
|
mesh_bed_leveling::mesh_bed_leveling() {
|
2017-04-05 22:29:44 -05:00
|
|
|
for (uint8_t i = 0; i < GRID_MAX_POINTS_X; ++i)
|
2017-03-31 01:28:07 -05:00
|
|
|
index_to_xpos[i] = MESH_MIN_X + i * (MESH_X_DIST);
|
2017-04-05 22:29:44 -05:00
|
|
|
for (uint8_t i = 0; i < GRID_MAX_POINTS_Y; ++i)
|
2017-03-31 01:28:07 -05:00
|
|
|
index_to_ypos[i] = MESH_MIN_Y + i * (MESH_Y_DIST);
|
|
|
|
reset();
|
|
|
|
}
|
2015-10-03 01:08:58 -05:00
|
|
|
|
2015-03-25 22:36:24 -05:00
|
|
|
void mesh_bed_leveling::reset() {
|
2017-10-13 17:21:25 -05:00
|
|
|
has_mesh = false;
|
2016-03-24 16:16:09 -05:00
|
|
|
z_offset = 0;
|
2016-10-22 10:07:18 -05:00
|
|
|
ZERO(z_values);
|
2015-03-25 22:36:24 -05:00
|
|
|
}
|
2015-03-15 17:18:11 -05:00
|
|
|
|
2017-09-07 22:33:16 -05:00
|
|
|
/**
|
|
|
|
* Prepare a mesh-leveled linear move in a Cartesian setup,
|
|
|
|
* splitting the move where it crosses mesh borders.
|
|
|
|
*/
|
|
|
|
void mesh_line_to_destination(const float fr_mm_s, uint8_t x_splits, uint8_t y_splits) {
|
|
|
|
int cx1 = mbl.cell_index_x(RAW_CURRENT_POSITION(X)),
|
|
|
|
cy1 = mbl.cell_index_y(RAW_CURRENT_POSITION(Y)),
|
|
|
|
cx2 = mbl.cell_index_x(RAW_X_POSITION(destination[X_AXIS])),
|
|
|
|
cy2 = mbl.cell_index_y(RAW_Y_POSITION(destination[Y_AXIS]));
|
|
|
|
NOMORE(cx1, GRID_MAX_POINTS_X - 2);
|
|
|
|
NOMORE(cy1, GRID_MAX_POINTS_Y - 2);
|
|
|
|
NOMORE(cx2, GRID_MAX_POINTS_X - 2);
|
|
|
|
NOMORE(cy2, GRID_MAX_POINTS_Y - 2);
|
|
|
|
|
|
|
|
if (cx1 == cx2 && cy1 == cy2) {
|
|
|
|
// Start and end on same mesh square
|
|
|
|
line_to_destination(fr_mm_s);
|
|
|
|
set_current_to_destination();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
#define MBL_SEGMENT_END(A) (current_position[A ##_AXIS] + (destination[A ##_AXIS] - current_position[A ##_AXIS]) * normalized_dist)
|
|
|
|
|
|
|
|
float normalized_dist, end[XYZE];
|
|
|
|
|
|
|
|
// Split at the left/front border of the right/top square
|
|
|
|
const int8_t gcx = max(cx1, cx2), gcy = max(cy1, cy2);
|
|
|
|
if (cx2 != cx1 && TEST(x_splits, gcx)) {
|
|
|
|
COPY(end, destination);
|
|
|
|
destination[X_AXIS] = LOGICAL_X_POSITION(mbl.index_to_xpos[gcx]);
|
|
|
|
normalized_dist = (destination[X_AXIS] - current_position[X_AXIS]) / (end[X_AXIS] - current_position[X_AXIS]);
|
|
|
|
destination[Y_AXIS] = MBL_SEGMENT_END(Y);
|
|
|
|
CBI(x_splits, gcx);
|
|
|
|
}
|
|
|
|
else if (cy2 != cy1 && TEST(y_splits, gcy)) {
|
|
|
|
COPY(end, destination);
|
|
|
|
destination[Y_AXIS] = LOGICAL_Y_POSITION(mbl.index_to_ypos[gcy]);
|
|
|
|
normalized_dist = (destination[Y_AXIS] - current_position[Y_AXIS]) / (end[Y_AXIS] - current_position[Y_AXIS]);
|
|
|
|
destination[X_AXIS] = MBL_SEGMENT_END(X);
|
|
|
|
CBI(y_splits, gcy);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
// Already split on a border
|
|
|
|
line_to_destination(fr_mm_s);
|
|
|
|
set_current_to_destination();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
destination[Z_AXIS] = MBL_SEGMENT_END(Z);
|
|
|
|
destination[E_AXIS] = MBL_SEGMENT_END(E);
|
|
|
|
|
|
|
|
// Do the split and look for more borders
|
|
|
|
mesh_line_to_destination(fr_mm_s, x_splits, y_splits);
|
|
|
|
|
|
|
|
// Restore destination from stack
|
|
|
|
COPY(destination, end);
|
|
|
|
mesh_line_to_destination(fr_mm_s, x_splits, y_splits);
|
|
|
|
}
|
|
|
|
|
2017-09-08 15:35:25 -05:00
|
|
|
void mbl_mesh_report() {
|
|
|
|
SERIAL_PROTOCOLLNPGM("Num X,Y: " STRINGIFY(GRID_MAX_POINTS_X) "," STRINGIFY(GRID_MAX_POINTS_Y));
|
|
|
|
SERIAL_PROTOCOLPGM("Z offset: "); SERIAL_PROTOCOL_F(mbl.z_offset, 5);
|
|
|
|
SERIAL_PROTOCOLLNPGM("\nMeasured points:");
|
|
|
|
print_2d_array(GRID_MAX_POINTS_X, GRID_MAX_POINTS_Y, 5,
|
|
|
|
[](const uint8_t ix, const uint8_t iy) { return mbl.z_values[ix][iy]; }
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2017-05-09 12:35:43 -05:00
|
|
|
#endif // MESH_BED_LEVELING
|