2017-03-18 10:14:31 -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"
|
2017-03-18 10:14:31 -05:00
|
|
|
|
|
|
|
#if ENABLED(AUTO_BED_LEVELING_UBL)
|
2017-03-24 00:53:37 -05:00
|
|
|
|
2017-04-05 19:25:36 -05:00
|
|
|
#include "ubl.h"
|
2017-09-06 06:28:32 -05:00
|
|
|
unified_bed_leveling ubl;
|
2017-03-18 10:14:31 -05:00
|
|
|
|
2017-09-08 15:35:25 -05:00
|
|
|
#include "../../../module/configuration_store.h"
|
|
|
|
#include "../../../module/planner.h"
|
|
|
|
#include "../../../module/motion.h"
|
|
|
|
#include "../../bedlevel/bedlevel.h"
|
2017-09-06 06:28:32 -05:00
|
|
|
|
|
|
|
#include "math.h"
|
2017-05-21 22:09:51 -05:00
|
|
|
|
2017-05-02 01:06:25 -05:00
|
|
|
uint8_t ubl_cnt = 0;
|
2017-04-25 20:50:17 -05:00
|
|
|
|
2017-11-05 08:49:38 -06:00
|
|
|
void unified_bed_leveling::echo_name(
|
|
|
|
#if NUM_SERIAL > 1
|
|
|
|
const int8_t port/*= -1*/
|
|
|
|
#endif
|
|
|
|
) {
|
|
|
|
SERIAL_PROTOCOLPGM_P(port, "Unified Bed Leveling");
|
|
|
|
}
|
2017-05-16 02:34:36 -05:00
|
|
|
|
2018-02-25 08:07:48 -06:00
|
|
|
void unified_bed_leveling::report_current_mesh(
|
|
|
|
#if NUM_SERIAL > 1
|
|
|
|
const int8_t port/*= -1*/
|
|
|
|
#endif
|
|
|
|
) {
|
|
|
|
if (!leveling_is_valid()) return;
|
|
|
|
SERIAL_ECHO_START_P(port);
|
|
|
|
SERIAL_ECHOLNPGM_P(port, " G29 I 999");
|
|
|
|
for (uint8_t x = 0; x < GRID_MAX_POINTS_X; x++)
|
|
|
|
for (uint8_t y = 0; y < GRID_MAX_POINTS_Y; y++)
|
|
|
|
if (!isnan(z_values[x][y])) {
|
|
|
|
SERIAL_ECHO_START_P(port);
|
|
|
|
SERIAL_ECHOPAIR_P(port, " M421 I", x);
|
|
|
|
SERIAL_ECHOPAIR_P(port, " J", y);
|
|
|
|
SERIAL_ECHOPGM_P(port, " Z");
|
|
|
|
SERIAL_ECHO_F_P(port, z_values[x][y], 6);
|
|
|
|
SERIAL_ECHOPAIR_P(port, " ; X", LOGICAL_X_POSITION(mesh_index_to_xpos(x)));
|
|
|
|
SERIAL_ECHOPAIR_P(port, ", Y", LOGICAL_Y_POSITION(mesh_index_to_ypos(y)));
|
|
|
|
SERIAL_EOL_P(port);
|
2018-02-25 21:59:19 -06:00
|
|
|
safe_delay(75); // Prevent Printrun from exploding
|
2018-02-25 08:07:48 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-11-05 08:49:38 -06:00
|
|
|
void unified_bed_leveling::report_state(
|
|
|
|
#if NUM_SERIAL > 1
|
|
|
|
const int8_t port/*= -1*/
|
|
|
|
#endif
|
|
|
|
) {
|
|
|
|
echo_name(
|
|
|
|
#if NUM_SERIAL > 1
|
|
|
|
port
|
|
|
|
#endif
|
|
|
|
);
|
|
|
|
SERIAL_PROTOCOLPGM_P(port, " System v" UBL_VERSION " ");
|
|
|
|
if (!planner.leveling_active) SERIAL_PROTOCOLPGM_P(port, "in");
|
|
|
|
SERIAL_PROTOCOLLNPGM_P(port, "active.");
|
2017-05-16 02:34:36 -05:00
|
|
|
safe_delay(50);
|
|
|
|
}
|
|
|
|
|
2017-05-11 22:33:47 -05:00
|
|
|
static void serial_echo_xy(const int16_t x, const int16_t y) {
|
2017-03-28 19:45:54 -05:00
|
|
|
SERIAL_CHAR('(');
|
|
|
|
SERIAL_ECHO(x);
|
|
|
|
SERIAL_CHAR(',');
|
|
|
|
SERIAL_ECHO(y);
|
|
|
|
SERIAL_CHAR(')');
|
|
|
|
safe_delay(10);
|
|
|
|
}
|
|
|
|
|
2017-12-09 07:14:06 -06:00
|
|
|
#if ENABLED(UBL_DEVEL_DEBUGGING)
|
|
|
|
|
|
|
|
static void debug_echo_axis(const AxisEnum axis) {
|
|
|
|
if (current_position[axis] == destination[axis])
|
|
|
|
SERIAL_ECHOPGM("-------------");
|
|
|
|
else
|
|
|
|
SERIAL_ECHO_F(destination[X_AXIS], 6);
|
|
|
|
}
|
|
|
|
|
|
|
|
void debug_current_and_destination(const char *title) {
|
|
|
|
|
|
|
|
// if the title message starts with a '!' it is so important, we are going to
|
|
|
|
// ignore the status of the g26_debug_flag
|
|
|
|
if (*title != '!' && !g26_debug_flag) return;
|
|
|
|
|
|
|
|
const float de = destination[E_AXIS] - current_position[E_AXIS];
|
|
|
|
|
|
|
|
if (de == 0.0) return; // Printing moves only
|
|
|
|
|
|
|
|
const float dx = destination[X_AXIS] - current_position[X_AXIS],
|
|
|
|
dy = destination[Y_AXIS] - current_position[Y_AXIS],
|
|
|
|
xy_dist = HYPOT(dx, dy);
|
|
|
|
|
|
|
|
if (xy_dist == 0.0) return;
|
|
|
|
|
|
|
|
SERIAL_ECHOPGM(" fpmm=");
|
|
|
|
const float fpmm = de / xy_dist;
|
|
|
|
SERIAL_ECHO_F(fpmm, 6);
|
|
|
|
|
|
|
|
SERIAL_ECHOPGM(" current=( ");
|
|
|
|
SERIAL_ECHO_F(current_position[X_AXIS], 6);
|
|
|
|
SERIAL_ECHOPGM(", ");
|
|
|
|
SERIAL_ECHO_F(current_position[Y_AXIS], 6);
|
|
|
|
SERIAL_ECHOPGM(", ");
|
|
|
|
SERIAL_ECHO_F(current_position[Z_AXIS], 6);
|
|
|
|
SERIAL_ECHOPGM(", ");
|
|
|
|
SERIAL_ECHO_F(current_position[E_AXIS], 6);
|
|
|
|
SERIAL_ECHOPGM(" ) destination=( ");
|
|
|
|
debug_echo_axis(X_AXIS);
|
|
|
|
SERIAL_ECHOPGM(", ");
|
|
|
|
debug_echo_axis(Y_AXIS);
|
|
|
|
SERIAL_ECHOPGM(", ");
|
|
|
|
debug_echo_axis(Z_AXIS);
|
|
|
|
SERIAL_ECHOPGM(", ");
|
|
|
|
debug_echo_axis(E_AXIS);
|
|
|
|
SERIAL_ECHOPGM(" ) ");
|
|
|
|
SERIAL_ECHO(title);
|
|
|
|
SERIAL_EOL();
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif // UBL_DEVEL_DEBUGGING
|
|
|
|
|
2017-10-13 21:56:27 -05:00
|
|
|
int8_t unified_bed_leveling::storage_slot;
|
2017-03-18 10:14:31 -05:00
|
|
|
|
2017-10-13 21:56:27 -05:00
|
|
|
float unified_bed_leveling::z_values[GRID_MAX_POINTS_X][GRID_MAX_POINTS_Y];
|
2017-04-22 18:01:39 -05:00
|
|
|
|
|
|
|
// 15 is the maximum nubmer of grid points supported + 1 safety margin for now,
|
|
|
|
// until determinism prevails
|
2017-05-22 14:41:09 -05:00
|
|
|
constexpr float unified_bed_leveling::_mesh_index_to_xpos[16],
|
|
|
|
unified_bed_leveling::_mesh_index_to_ypos[16];
|
2017-03-31 00:15:32 -05:00
|
|
|
|
2017-12-06 16:56:30 -06:00
|
|
|
#if ENABLED(ULTIPANEL)
|
2017-09-08 15:35:25 -05:00
|
|
|
bool unified_bed_leveling::lcd_map_control = false;
|
|
|
|
#endif
|
|
|
|
|
2017-03-31 00:15:32 -05:00
|
|
|
volatile int unified_bed_leveling::encoder_diff;
|
|
|
|
|
|
|
|
unified_bed_leveling::unified_bed_leveling() {
|
2017-12-10 21:17:07 -06:00
|
|
|
ubl_cnt++; // Debug counter to ensure we only have one UBL object present in memory. We can eliminate this (and all references to ubl_cnt) very soon.
|
2017-03-18 10:14:31 -05:00
|
|
|
reset();
|
|
|
|
}
|
|
|
|
|
2017-03-20 01:42:41 -05:00
|
|
|
void unified_bed_leveling::reset() {
|
2017-12-10 21:17:07 -06:00
|
|
|
const bool was_enabled = planner.leveling_active;
|
2017-06-03 17:11:43 -05:00
|
|
|
set_bed_leveling_enabled(false);
|
2017-10-13 21:56:27 -05:00
|
|
|
storage_slot = -1;
|
2017-05-21 22:09:51 -05:00
|
|
|
#if ENABLED(ENABLE_LEVELING_FADE_HEIGHT)
|
2017-10-13 17:21:25 -05:00
|
|
|
planner.set_z_fade_height(10.0);
|
2017-05-21 22:09:51 -05:00
|
|
|
#endif
|
2017-03-18 10:14:31 -05:00
|
|
|
ZERO(z_values);
|
2017-12-10 21:17:07 -06:00
|
|
|
if (was_enabled) report_current_position();
|
2017-03-18 10:14:31 -05:00
|
|
|
}
|
|
|
|
|
2017-03-20 01:42:41 -05:00
|
|
|
void unified_bed_leveling::invalidate() {
|
2017-06-03 17:11:43 -05:00
|
|
|
set_bed_leveling_enabled(false);
|
|
|
|
set_all_mesh_points_to_value(NAN);
|
|
|
|
}
|
|
|
|
|
2017-10-12 22:20:02 -05:00
|
|
|
void unified_bed_leveling::set_all_mesh_points_to_value(const float value) {
|
2017-06-03 17:11:43 -05:00
|
|
|
for (uint8_t x = 0; x < GRID_MAX_POINTS_X; x++) {
|
|
|
|
for (uint8_t y = 0; y < GRID_MAX_POINTS_Y; y++) {
|
|
|
|
z_values[x][y] = value;
|
|
|
|
}
|
|
|
|
}
|
2017-03-18 10:14:31 -05:00
|
|
|
}
|
|
|
|
|
2017-06-12 18:26:49 -05:00
|
|
|
// display_map() currently produces three different mesh map types
|
|
|
|
// 0 : suitable for PronterFace and Repetier's serial console
|
|
|
|
// 1 : .CSV file suitable for importation into various spread sheets
|
|
|
|
// 2 : disply of the map data on a RepRap Graphical LCD Panel
|
|
|
|
|
2017-03-24 00:53:37 -05:00
|
|
|
void unified_bed_leveling::display_map(const int map_type) {
|
2018-03-21 00:29:09 -05:00
|
|
|
#if HAS_AUTO_REPORTING || ENABLED(HOST_KEEPALIVE_FEATURE)
|
2018-03-07 02:00:19 -06:00
|
|
|
suspend_auto_report = true;
|
|
|
|
#endif
|
|
|
|
|
2017-05-21 22:09:51 -05:00
|
|
|
constexpr uint8_t spaces = 8 * (GRID_MAX_POINTS_X - 2);
|
2017-03-18 10:14:31 -05:00
|
|
|
|
2017-06-16 13:01:52 -05:00
|
|
|
SERIAL_PROTOCOLPGM("\nBed Topography Report");
|
2017-06-12 18:26:49 -05:00
|
|
|
if (map_type == 0) {
|
2017-06-16 13:01:52 -05:00
|
|
|
SERIAL_PROTOCOLPGM(":\n\n");
|
2017-04-05 22:29:44 -05:00
|
|
|
serial_echo_xy(0, GRID_MAX_POINTS_Y - 1);
|
2017-05-07 19:37:57 -05:00
|
|
|
SERIAL_ECHO_SP(spaces + 3);
|
2017-04-05 22:29:44 -05:00
|
|
|
serial_echo_xy(GRID_MAX_POINTS_X - 1, GRID_MAX_POINTS_Y - 1);
|
2017-06-09 10:51:23 -05:00
|
|
|
SERIAL_EOL();
|
2017-10-19 01:39:39 -05:00
|
|
|
serial_echo_xy(MESH_MIN_X, MESH_MAX_Y);
|
2017-05-08 17:23:17 -05:00
|
|
|
SERIAL_ECHO_SP(spaces);
|
2017-10-19 01:39:39 -05:00
|
|
|
serial_echo_xy(MESH_MAX_X, MESH_MAX_Y);
|
2017-06-09 10:51:23 -05:00
|
|
|
SERIAL_EOL();
|
2017-03-25 18:37:54 -05:00
|
|
|
}
|
2017-06-16 13:01:52 -05:00
|
|
|
else {
|
|
|
|
SERIAL_PROTOCOLPGM(" for ");
|
|
|
|
serialprintPGM(map_type == 1 ? PSTR("CSV:\n\n") : PSTR("LCD:\n\n"));
|
|
|
|
}
|
2017-06-12 18:26:49 -05:00
|
|
|
|
2017-05-22 14:41:09 -05:00
|
|
|
const float current_xi = get_cell_index_x(current_position[X_AXIS] + (MESH_X_DIST) / 2.0),
|
|
|
|
current_yi = get_cell_index_y(current_position[Y_AXIS] + (MESH_Y_DIST) / 2.0);
|
2017-03-28 19:45:54 -05:00
|
|
|
|
2017-04-05 22:29:44 -05:00
|
|
|
for (int8_t j = GRID_MAX_POINTS_Y - 1; j >= 0; j--) {
|
|
|
|
for (uint8_t i = 0; i < GRID_MAX_POINTS_X; i++) {
|
2017-03-28 19:45:54 -05:00
|
|
|
const bool is_current = i == current_xi && j == current_yi;
|
2017-03-18 10:14:31 -05:00
|
|
|
|
2017-03-30 21:32:50 -05:00
|
|
|
// is the nozzle here? then mark the number
|
2017-06-12 18:26:49 -05:00
|
|
|
if (map_type == 0) SERIAL_CHAR(is_current ? '[' : ' ');
|
2017-03-28 19:45:54 -05:00
|
|
|
|
|
|
|
const float f = z_values[i][j];
|
|
|
|
if (isnan(f)) {
|
2017-06-16 13:01:52 -05:00
|
|
|
serialprintPGM(map_type == 0 ? PSTR(" . ") : PSTR("NAN"));
|
2017-03-28 19:45:54 -05:00
|
|
|
}
|
2017-06-16 13:01:52 -05:00
|
|
|
else if (map_type <= 1) {
|
2017-03-18 10:14:31 -05:00
|
|
|
// if we don't do this, the columns won't line up nicely
|
2017-06-16 13:01:52 -05:00
|
|
|
if (map_type == 0 && f >= 0.0) SERIAL_CHAR(' ');
|
|
|
|
SERIAL_PROTOCOL_F(f, 3);
|
2017-03-18 10:14:31 -05:00
|
|
|
}
|
2017-06-16 13:01:52 -05:00
|
|
|
idle();
|
2017-06-12 18:26:49 -05:00
|
|
|
if (map_type == 1 && i < GRID_MAX_POINTS_X - 1) SERIAL_CHAR(',');
|
2017-11-05 08:49:38 -06:00
|
|
|
SERIAL_FLUSHTX();
|
2017-03-26 23:49:56 -05:00
|
|
|
safe_delay(15);
|
2017-06-12 18:26:49 -05:00
|
|
|
if (map_type == 0) {
|
2017-03-28 19:45:54 -05:00
|
|
|
SERIAL_CHAR(is_current ? ']' : ' ');
|
2017-03-25 18:37:54 -05:00
|
|
|
SERIAL_CHAR(' ');
|
|
|
|
}
|
2017-03-18 10:14:31 -05:00
|
|
|
}
|
2017-06-09 10:51:23 -05:00
|
|
|
SERIAL_EOL();
|
2017-06-16 13:01:52 -05:00
|
|
|
if (j && map_type == 0) { // we want the (0,0) up tight against the block of numbers
|
2017-03-18 10:14:31 -05:00
|
|
|
SERIAL_CHAR(' ');
|
2017-06-09 10:51:23 -05:00
|
|
|
SERIAL_EOL();
|
2017-03-18 10:14:31 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-06-12 18:26:49 -05:00
|
|
|
if (map_type == 0) {
|
2017-10-19 01:39:39 -05:00
|
|
|
serial_echo_xy(MESH_MIN_X, MESH_MIN_Y);
|
2017-05-08 17:23:17 -05:00
|
|
|
SERIAL_ECHO_SP(spaces + 4);
|
2017-10-19 01:39:39 -05:00
|
|
|
serial_echo_xy(MESH_MAX_X, MESH_MIN_Y);
|
2017-06-09 10:51:23 -05:00
|
|
|
SERIAL_EOL();
|
2017-03-28 19:45:54 -05:00
|
|
|
serial_echo_xy(0, 0);
|
2017-05-07 19:37:57 -05:00
|
|
|
SERIAL_ECHO_SP(spaces + 5);
|
2017-04-05 22:29:44 -05:00
|
|
|
serial_echo_xy(GRID_MAX_POINTS_X - 1, 0);
|
2017-06-09 10:51:23 -05:00
|
|
|
SERIAL_EOL();
|
2017-03-24 18:35:10 -05:00
|
|
|
}
|
2018-03-07 02:00:19 -06:00
|
|
|
|
2018-03-21 00:29:09 -05:00
|
|
|
#if HAS_AUTO_REPORTING || ENABLED(HOST_KEEPALIVE_FEATURE)
|
2018-03-07 02:00:19 -06:00
|
|
|
suspend_auto_report = false;
|
|
|
|
#endif
|
2017-03-18 10:14:31 -05:00
|
|
|
}
|
|
|
|
|
2017-03-20 01:42:41 -05:00
|
|
|
bool unified_bed_leveling::sanity_check() {
|
2017-03-18 10:14:31 -05:00
|
|
|
uint8_t error_flag = 0;
|
|
|
|
|
2017-06-16 13:01:52 -05:00
|
|
|
if (settings.calc_num_meshes() < 1) {
|
2017-12-09 07:14:06 -06:00
|
|
|
SERIAL_PROTOCOLLNPGM("?Mesh too big for EEPROM.");
|
2017-03-18 10:14:31 -05:00
|
|
|
error_flag++;
|
|
|
|
}
|
|
|
|
|
|
|
|
return !!error_flag;
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif // AUTO_BED_LEVELING_UBL
|