2017-03-18 16:14:31 +01: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/>.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "Marlin.h"
|
|
|
|
#include "math.h"
|
|
|
|
|
|
|
|
#if ENABLED(AUTO_BED_LEVELING_UBL)
|
2017-03-24 06:53:37 +01:00
|
|
|
|
2017-04-06 02:25:36 +02:00
|
|
|
#include "ubl.h"
|
2017-03-18 16:14:31 +01:00
|
|
|
#include "hex_print_routines.h"
|
2017-04-26 03:50:17 +02:00
|
|
|
#include "temperature.h"
|
2017-03-18 16:14:31 +01:00
|
|
|
|
2017-03-24 06:53:37 +01:00
|
|
|
/**
|
|
|
|
* These support functions allow the use of large bit arrays of flags that take very
|
|
|
|
* little RAM. Currently they are limited to being 16x16 in size. Changing the declaration
|
|
|
|
* to unsigned long will allow us to go to 32x32 if higher resolution Mesh's are needed
|
|
|
|
* in the future.
|
|
|
|
*/
|
|
|
|
void bit_clear(uint16_t bits[16], uint8_t x, uint8_t y) { CBI(bits[y], x); }
|
|
|
|
void bit_set(uint16_t bits[16], uint8_t x, uint8_t y) { SBI(bits[y], x); }
|
|
|
|
bool is_bit_set(uint16_t bits[16], uint8_t x, uint8_t y) { return TEST(bits[y], x); }
|
|
|
|
|
2017-04-26 03:50:17 +02:00
|
|
|
int ubl_cnt=0;
|
|
|
|
|
2017-03-29 02:45:54 +02:00
|
|
|
static void serial_echo_xy(const uint16_t x, const uint16_t y) {
|
|
|
|
SERIAL_CHAR('(');
|
|
|
|
SERIAL_ECHO(x);
|
|
|
|
SERIAL_CHAR(',');
|
|
|
|
SERIAL_ECHO(y);
|
|
|
|
SERIAL_CHAR(')');
|
|
|
|
safe_delay(10);
|
|
|
|
}
|
|
|
|
|
2017-05-02 00:19:57 +02:00
|
|
|
static void serial_echo_mspaces(const uint8_t cnt) {
|
2017-04-06 05:29:44 +02:00
|
|
|
for (uint8_t i = GRID_MAX_POINTS_X - 1; --i;) {
|
2017-05-02 00:19:57 +02:00
|
|
|
SERIAL_ECHO_SP((uint8_t)cnt);
|
2017-03-29 02:45:54 +02:00
|
|
|
safe_delay(10);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-22 23:04:28 +02:00
|
|
|
ubl_state unified_bed_leveling::state;
|
2017-03-18 16:14:31 +01:00
|
|
|
|
2017-04-06 05:29:44 +02:00
|
|
|
float unified_bed_leveling::z_values[GRID_MAX_POINTS_X][GRID_MAX_POINTS_Y],
|
2017-04-23 01:01:39 +02:00
|
|
|
unified_bed_leveling::last_specified_z;
|
|
|
|
|
|
|
|
// 15 is the maximum nubmer of grid points supported + 1 safety margin for now,
|
|
|
|
// until determinism prevails
|
|
|
|
constexpr float unified_bed_leveling::mesh_index_to_xpos[16],
|
|
|
|
unified_bed_leveling::mesh_index_to_ypos[16];
|
2017-03-31 07:15:32 +02:00
|
|
|
|
|
|
|
bool unified_bed_leveling::g26_debug_flag = false,
|
|
|
|
unified_bed_leveling::has_control_of_lcd_panel = false;
|
2017-03-18 16:14:31 +01:00
|
|
|
|
2017-04-26 03:50:17 +02:00
|
|
|
int16_t unified_bed_leveling::eeprom_start = -1; // Please stop changing this to 8 bits in size
|
|
|
|
// It needs to hold values bigger than this.
|
2017-03-18 16:14:31 +01:00
|
|
|
|
2017-03-31 07:15:32 +02:00
|
|
|
volatile int unified_bed_leveling::encoder_diff;
|
|
|
|
|
|
|
|
unified_bed_leveling::unified_bed_leveling() {
|
2017-04-26 03:50:17 +02:00
|
|
|
ubl_cnt++; // Debug counter to insure we only have one UBL object present in memory.
|
2017-03-18 16:14:31 +01:00
|
|
|
reset();
|
|
|
|
}
|
|
|
|
|
2017-04-22 23:04:28 +02:00
|
|
|
void unified_bed_leveling::load_mesh(const int16_t slot) {
|
2017-03-31 04:32:50 +02:00
|
|
|
int16_t j = (UBL_LAST_EEPROM_INDEX - eeprom_start) / sizeof(z_values);
|
2017-03-18 16:14:31 +01:00
|
|
|
|
2017-04-22 23:04:28 +02:00
|
|
|
if (slot == -1) {
|
2017-03-18 16:14:31 +01:00
|
|
|
SERIAL_PROTOCOLLNPGM("?No mesh saved in EEPROM. Zeroing mesh in memory.\n");
|
|
|
|
reset();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-04-22 23:04:28 +02:00
|
|
|
if (!WITHIN(slot, 0, j - 1) || eeprom_start <= 0) {
|
2017-03-18 16:14:31 +01:00
|
|
|
SERIAL_PROTOCOLLNPGM("?EEPROM storage not available to load mesh.\n");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-04-22 23:04:28 +02:00
|
|
|
j = UBL_LAST_EEPROM_INDEX - (slot + 1) * sizeof(z_values);
|
2017-03-29 02:45:54 +02:00
|
|
|
eeprom_read_block((void *)&z_values, (void *)j, sizeof(z_values));
|
2017-03-18 16:14:31 +01:00
|
|
|
|
2017-04-22 23:04:28 +02:00
|
|
|
SERIAL_PROTOCOLPAIR("Mesh loaded from slot ", slot);
|
2017-04-13 02:39:26 +02:00
|
|
|
SERIAL_PROTOCOLLNPAIR(" at offset ", hex_address((void*)j));
|
2017-03-18 16:14:31 +01:00
|
|
|
}
|
|
|
|
|
2017-04-22 23:04:28 +02:00
|
|
|
void unified_bed_leveling::store_mesh(const int16_t slot) {
|
2017-03-31 04:32:50 +02:00
|
|
|
int16_t j = (UBL_LAST_EEPROM_INDEX - eeprom_start) / sizeof(z_values);
|
2017-03-18 16:14:31 +01:00
|
|
|
|
2017-04-22 23:04:28 +02:00
|
|
|
if (!WITHIN(slot, 0, j - 1) || eeprom_start <= 0) {
|
2017-03-18 16:14:31 +01:00
|
|
|
SERIAL_PROTOCOLLNPGM("?EEPROM storage not available to load mesh.\n");
|
2017-04-22 23:04:28 +02:00
|
|
|
SERIAL_PROTOCOL(slot);
|
2017-03-18 16:14:31 +01:00
|
|
|
SERIAL_PROTOCOLLNPGM(" mesh slots available.\n");
|
|
|
|
SERIAL_PROTOCOLLNPAIR("E2END : ", E2END);
|
2017-03-24 06:53:37 +01:00
|
|
|
SERIAL_PROTOCOLLNPAIR("k : ", (int)UBL_LAST_EEPROM_INDEX);
|
2017-03-18 16:14:31 +01:00
|
|
|
SERIAL_PROTOCOLLNPAIR("j : ", j);
|
2017-04-22 23:04:28 +02:00
|
|
|
SERIAL_PROTOCOLLNPAIR("m : ", slot);
|
2017-03-18 16:14:31 +01:00
|
|
|
SERIAL_EOL;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-04-22 23:04:28 +02:00
|
|
|
j = UBL_LAST_EEPROM_INDEX - (slot + 1) * sizeof(z_values);
|
2017-03-18 16:14:31 +01:00
|
|
|
eeprom_write_block((const void *)&z_values, (void *)j, sizeof(z_values));
|
|
|
|
|
2017-04-22 23:04:28 +02:00
|
|
|
SERIAL_PROTOCOLPAIR("Mesh saved in slot ", slot);
|
2017-04-13 02:39:26 +02:00
|
|
|
SERIAL_PROTOCOLLNPAIR(" at offset ", hex_address((void*)j));
|
2017-03-18 16:14:31 +01:00
|
|
|
}
|
|
|
|
|
2017-03-20 07:42:41 +01:00
|
|
|
void unified_bed_leveling::reset() {
|
2017-03-18 16:14:31 +01:00
|
|
|
state.active = false;
|
|
|
|
state.z_offset = 0;
|
2017-03-20 07:42:41 +01:00
|
|
|
state.eeprom_storage_slot = -1;
|
2017-03-18 16:14:31 +01:00
|
|
|
|
|
|
|
ZERO(z_values);
|
|
|
|
|
2017-03-20 07:42:41 +01:00
|
|
|
last_specified_z = -999.9;
|
2017-03-18 16:14:31 +01:00
|
|
|
}
|
|
|
|
|
2017-03-20 07:42:41 +01:00
|
|
|
void unified_bed_leveling::invalidate() {
|
2017-03-18 16:14:31 +01:00
|
|
|
state.active = false;
|
|
|
|
state.z_offset = 0;
|
2017-04-06 05:29:44 +02:00
|
|
|
for (int x = 0; x < GRID_MAX_POINTS_X; x++)
|
|
|
|
for (int y = 0; y < GRID_MAX_POINTS_Y; y++)
|
2017-03-18 16:14:31 +01:00
|
|
|
z_values[x][y] = NAN;
|
|
|
|
}
|
|
|
|
|
2017-03-24 06:53:37 +01:00
|
|
|
void unified_bed_leveling::display_map(const int map_type) {
|
2017-03-29 02:45:54 +02:00
|
|
|
const bool map0 = map_type == 0;
|
2017-05-02 00:19:57 +02:00
|
|
|
const uint8_t spaces = 9;
|
2017-03-18 16:14:31 +01:00
|
|
|
|
2017-03-29 02:45:54 +02:00
|
|
|
if (map0) {
|
|
|
|
SERIAL_PROTOCOLLNPGM("\nBed Topography Report:\n");
|
2017-04-06 05:29:44 +02:00
|
|
|
serial_echo_xy(0, GRID_MAX_POINTS_Y - 1);
|
2017-05-02 00:19:57 +02:00
|
|
|
SERIAL_ECHO_SP(3);
|
|
|
|
serial_echo_mspaces(spaces);
|
2017-04-06 05:29:44 +02:00
|
|
|
serial_echo_xy(GRID_MAX_POINTS_X - 1, GRID_MAX_POINTS_Y - 1);
|
2017-03-29 02:45:54 +02:00
|
|
|
SERIAL_EOL;
|
2017-05-02 00:19:57 +02:00
|
|
|
serial_echo_xy(UBL_MESH_MIN_X, UBL_MESH_MAX_Y);
|
|
|
|
serial_echo_mspaces(spaces);
|
2017-03-29 02:45:54 +02:00
|
|
|
serial_echo_xy(UBL_MESH_MAX_X, UBL_MESH_MAX_Y);
|
|
|
|
SERIAL_EOL;
|
2017-03-26 00:37:54 +01:00
|
|
|
}
|
2017-03-18 16:14:31 +01:00
|
|
|
|
2017-03-29 02:45:54 +02:00
|
|
|
const float current_xi = ubl.get_cell_index_x(current_position[X_AXIS] + (MESH_X_DIST) / 2.0),
|
|
|
|
current_yi = ubl.get_cell_index_y(current_position[Y_AXIS] + (MESH_Y_DIST) / 2.0);
|
|
|
|
|
2017-04-06 05:29:44 +02: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-29 02:45:54 +02:00
|
|
|
const bool is_current = i == current_xi && j == current_yi;
|
2017-03-18 16:14:31 +01:00
|
|
|
|
2017-03-31 04:32:50 +02:00
|
|
|
// is the nozzle here? then mark the number
|
|
|
|
if (map0) SERIAL_CHAR(is_current ? '[' : ' ');
|
2017-03-29 02:45:54 +02:00
|
|
|
|
|
|
|
const float f = z_values[i][j];
|
|
|
|
if (isnan(f)) {
|
2017-03-31 14:57:41 +02:00
|
|
|
serialprintPGM(map0 ? PSTR(" . ") : PSTR("NAN"));
|
2017-03-29 02:45:54 +02:00
|
|
|
}
|
2017-03-18 16:14:31 +01:00
|
|
|
else {
|
|
|
|
// if we don't do this, the columns won't line up nicely
|
2017-03-31 04:32:50 +02:00
|
|
|
if (map0 && f >= 0.0) SERIAL_CHAR(' ');
|
2017-03-25 00:35:10 +01:00
|
|
|
SERIAL_PROTOCOL_F(f, 3);
|
2017-03-18 16:14:31 +01:00
|
|
|
idle();
|
|
|
|
}
|
2017-04-06 05:29:44 +02:00
|
|
|
if (!map0 && i < GRID_MAX_POINTS_X - 1) SERIAL_CHAR(',');
|
2017-03-26 00:37:54 +01:00
|
|
|
|
2017-03-29 02:45:54 +02:00
|
|
|
#if TX_BUFFER_SIZE > 0
|
2017-03-25 00:35:10 +01:00
|
|
|
MYSERIAL.flushTX();
|
|
|
|
#endif
|
2017-03-27 06:49:56 +02:00
|
|
|
safe_delay(15);
|
2017-03-29 02:45:54 +02:00
|
|
|
if (map0) {
|
|
|
|
SERIAL_CHAR(is_current ? ']' : ' ');
|
2017-03-26 00:37:54 +01:00
|
|
|
SERIAL_CHAR(' ');
|
|
|
|
}
|
2017-03-18 16:14:31 +01:00
|
|
|
}
|
|
|
|
SERIAL_EOL;
|
2017-03-29 02:45:54 +02:00
|
|
|
if (j && map0) { // we want the (0,0) up tight against the block of numbers
|
2017-03-18 16:14:31 +01:00
|
|
|
SERIAL_CHAR(' ');
|
|
|
|
SERIAL_EOL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-29 02:45:54 +02:00
|
|
|
if (map0) {
|
|
|
|
serial_echo_xy(UBL_MESH_MIN_X, UBL_MESH_MIN_Y);
|
2017-05-01 01:36:40 +02:00
|
|
|
SERIAL_ECHO_SP(4);
|
2017-05-02 00:19:57 +02:00
|
|
|
serial_echo_mspaces(spaces);
|
2017-03-29 02:45:54 +02:00
|
|
|
serial_echo_xy(UBL_MESH_MAX_X, UBL_MESH_MIN_Y);
|
|
|
|
SERIAL_EOL;
|
|
|
|
serial_echo_xy(0, 0);
|
2017-05-02 00:19:57 +02:00
|
|
|
SERIAL_ECHO_SP(5);
|
|
|
|
serial_echo_mspaces(spaces);
|
2017-04-06 05:29:44 +02:00
|
|
|
serial_echo_xy(GRID_MAX_POINTS_X - 1, 0);
|
2017-03-26 00:37:54 +01:00
|
|
|
SERIAL_EOL;
|
2017-03-25 00:35:10 +01:00
|
|
|
}
|
2017-03-18 16:14:31 +01:00
|
|
|
}
|
|
|
|
|
2017-03-20 07:42:41 +01:00
|
|
|
bool unified_bed_leveling::sanity_check() {
|
2017-03-18 16:14:31 +01:00
|
|
|
uint8_t error_flag = 0;
|
|
|
|
|
2017-03-31 04:32:50 +02:00
|
|
|
const int j = (UBL_LAST_EEPROM_INDEX - eeprom_start) / sizeof(z_values);
|
2017-03-18 16:14:31 +01:00
|
|
|
if (j < 1) {
|
|
|
|
SERIAL_PROTOCOLLNPGM("?No EEPROM storage available for a mesh of this size.\n");
|
|
|
|
error_flag++;
|
|
|
|
}
|
|
|
|
|
|
|
|
return !!error_flag;
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif // AUTO_BED_LEVELING_UBL
|