1
0
mirror of https://github.com/MarlinFirmware/Marlin.git synced 2024-12-11 21:14:34 +00:00
MarlinFirmware/Marlin/ubl.cpp

192 lines
6.2 KiB
C++
Raw Normal View History

2017-03-18 15:14:31 +00: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)
#include "ubl.h"
2017-03-18 15:14:31 +00:00
#include "hex_print_routines.h"
#include "temperature.h"
#include "planner.h"
/**
* 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-05-02 06:06:25 +00:00
uint8_t ubl_cnt = 0;
2017-05-16 07:34:36 +00:00
void unified_bed_leveling::echo_name() { SERIAL_PROTOCOLPGM("Unified Bed Leveling"); }
void unified_bed_leveling::report_state() {
echo_name();
SERIAL_PROTOCOLPGM(" System v" UBL_VERSION " ");
if (!planner.leveling_active) SERIAL_PROTOCOLPGM("in");
2017-05-16 07:34:36 +00:00
SERIAL_PROTOCOLLNPGM("active.");
safe_delay(50);
}
static void serial_echo_xy(const int16_t x, const int16_t y) {
2017-03-29 00:45:54 +00:00
SERIAL_CHAR('(');
SERIAL_ECHO(x);
SERIAL_CHAR(',');
SERIAL_ECHO(y);
SERIAL_CHAR(')');
safe_delay(10);
}
2017-10-14 02:58:45 +00:00
int8_t unified_bed_leveling::storage_slot;
2017-03-18 15:14:31 +00:00
float unified_bed_leveling::z_values[GRID_MAX_POINTS_X][GRID_MAX_POINTS_Y];
// 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];
bool unified_bed_leveling::g26_debug_flag = false,
unified_bed_leveling::has_control_of_lcd_panel = false;
2017-03-18 15:14:31 +00:00
volatile int unified_bed_leveling::encoder_diff;
unified_bed_leveling::unified_bed_leveling() {
ubl_cnt++; // Debug counter to insure we only have one UBL object present in memory. We can eliminate this (and all references to ubl_cnt) very soon.
2017-03-18 15:14:31 +00:00
reset();
}
2017-03-20 06:42:41 +00:00
void unified_bed_leveling::reset() {
set_bed_leveling_enabled(false);
2017-10-14 02:58:45 +00:00
storage_slot = -1;
#if ENABLED(ENABLE_LEVELING_FADE_HEIGHT)
planner.set_z_fade_height(10.0);
#endif
2017-03-18 15:14:31 +00:00
ZERO(z_values);
}
2017-03-20 06:42:41 +00:00
void unified_bed_leveling::invalidate() {
set_bed_leveling_enabled(false);
set_all_mesh_points_to_value(NAN);
}
2017-10-13 00:26:06 +00:00
void unified_bed_leveling::set_all_mesh_points_to_value(const float value) {
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 15:14:31 +00: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
void unified_bed_leveling::display_map(const int map_type) {
constexpr uint8_t spaces = 8 * (GRID_MAX_POINTS_X - 2);
2017-03-18 15:14:31 +00:00
2017-06-16 18:01:52 +00:00
SERIAL_PROTOCOLPGM("\nBed Topography Report");
if (map_type == 0) {
2017-06-16 18:01:52 +00:00
SERIAL_PROTOCOLPGM(":\n\n");
serial_echo_xy(0, GRID_MAX_POINTS_Y - 1);
2017-05-08 00:37:57 +00:00
SERIAL_ECHO_SP(spaces + 3);
serial_echo_xy(GRID_MAX_POINTS_X - 1, GRID_MAX_POINTS_Y - 1);
2017-06-09 15:51:23 +00:00
SERIAL_EOL();
2017-10-19 06:40:29 +00:00
serial_echo_xy(MESH_MIN_X, MESH_MAX_Y);
SERIAL_ECHO_SP(spaces);
2017-10-19 06:40:29 +00:00
serial_echo_xy(MESH_MAX_X, MESH_MAX_Y);
2017-06-09 15:51:23 +00:00
SERIAL_EOL();
}
2017-06-16 18:01:52 +00:00
else {
SERIAL_PROTOCOLPGM(" for ");
serialprintPGM(map_type == 1 ? PSTR("CSV:\n\n") : PSTR("LCD:\n\n"));
}
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-29 00:45:54 +00: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 00:45:54 +00:00
const bool is_current = i == current_xi && j == current_yi;
2017-03-18 15:14:31 +00:00
// is the nozzle here? then mark the number
if (map_type == 0) SERIAL_CHAR(is_current ? '[' : ' ');
2017-03-29 00:45:54 +00:00
const float f = z_values[i][j];
if (isnan(f)) {
2017-06-16 18:01:52 +00:00
serialprintPGM(map_type == 0 ? PSTR(" . ") : PSTR("NAN"));
2017-03-29 00:45:54 +00:00
}
2017-06-16 18:01:52 +00:00
else if (map_type <= 1) {
2017-03-18 15:14:31 +00:00
// if we don't do this, the columns won't line up nicely
2017-06-16 18:01:52 +00:00
if (map_type == 0 && f >= 0.0) SERIAL_CHAR(' ');
SERIAL_PROTOCOL_F(f, 3);
2017-03-18 15:14:31 +00:00
}
2017-06-16 18:01:52 +00:00
idle();
if (map_type == 1 && i < GRID_MAX_POINTS_X - 1) SERIAL_CHAR(',');
2017-03-29 00:45:54 +00:00
#if TX_BUFFER_SIZE > 0
MYSERIAL.flushTX();
#endif
safe_delay(15);
if (map_type == 0) {
2017-03-29 00:45:54 +00:00
SERIAL_CHAR(is_current ? ']' : ' ');
SERIAL_CHAR(' ');
}
2017-03-18 15:14:31 +00:00
}
2017-06-09 15:51:23 +00:00
SERIAL_EOL();
2017-06-16 18:01:52 +00:00
if (j && map_type == 0) { // we want the (0,0) up tight against the block of numbers
2017-03-18 15:14:31 +00:00
SERIAL_CHAR(' ');
2017-06-09 15:51:23 +00:00
SERIAL_EOL();
2017-03-18 15:14:31 +00:00
}
}
if (map_type == 0) {
2017-10-19 06:40:29 +00:00
serial_echo_xy(MESH_MIN_X, MESH_MIN_Y);
SERIAL_ECHO_SP(spaces + 4);
2017-10-19 06:40:29 +00:00
serial_echo_xy(MESH_MAX_X, MESH_MIN_Y);
2017-06-09 15:51:23 +00:00
SERIAL_EOL();
2017-03-29 00:45:54 +00:00
serial_echo_xy(0, 0);
2017-05-08 00:37:57 +00:00
SERIAL_ECHO_SP(spaces + 5);
serial_echo_xy(GRID_MAX_POINTS_X - 1, 0);
2017-06-09 15:51:23 +00:00
SERIAL_EOL();
}
2017-03-18 15:14:31 +00:00
}
2017-03-20 06:42:41 +00:00
bool unified_bed_leveling::sanity_check() {
2017-03-18 15:14:31 +00:00
uint8_t error_flag = 0;
2017-06-16 18:01:52 +00:00
if (settings.calc_num_meshes() < 1) {
SERIAL_PROTOCOLLNPGM("?Insufficient EEPROM storage for a mesh of this size.");
2017-03-18 15:14:31 +00:00
error_flag++;
}
return !!error_flag;
}
#endif // AUTO_BED_LEVELING_UBL