ubl.cpp 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  1. /**
  2. * Marlin 3D Printer Firmware
  3. * Copyright (C) 2016 MarlinFirmware [https://github.com/MarlinFirmware/Marlin]
  4. *
  5. * Based on Sprinter and grbl.
  6. * Copyright (C) 2011 Camiel Gubbels / Erik van der Zalm
  7. *
  8. * This program is free software: you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation, either version 3 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  20. *
  21. */
  22. #include "MarlinConfig.h"
  23. #if ENABLED(AUTO_BED_LEVELING_UBL)
  24. #include "Marlin.h"
  25. #include "ubl.h"
  26. #include "hex_print_routines.h"
  27. #include "temperature.h"
  28. #include "planner.h"
  29. #include "math.h"
  30. unified_bed_leveling ubl;
  31. uint8_t ubl_cnt = 0;
  32. void unified_bed_leveling::echo_name() { SERIAL_PROTOCOLPGM("Unified Bed Leveling"); }
  33. void unified_bed_leveling::report_current_mesh() {
  34. if (!leveling_is_valid()) return;
  35. SERIAL_ECHO_START();
  36. SERIAL_ECHOLNPGM(" G29 I999");
  37. for (uint8_t x = 0; x < GRID_MAX_POINTS_X; x++)
  38. for (uint8_t y = 0; y < GRID_MAX_POINTS_Y; y++)
  39. if (!isnan(z_values[x][y])) {
  40. SERIAL_ECHO_START();
  41. SERIAL_ECHOPAIR(" M421 I", x);
  42. SERIAL_ECHOPAIR(" J", y);
  43. SERIAL_ECHOPGM(" Z");
  44. SERIAL_ECHO_F(z_values[x][y], 2);
  45. SERIAL_EOL();
  46. }
  47. }
  48. void unified_bed_leveling::report_state() {
  49. echo_name();
  50. SERIAL_PROTOCOLPGM(" System v" UBL_VERSION " ");
  51. if (!planner.leveling_active) SERIAL_PROTOCOLPGM("in");
  52. SERIAL_PROTOCOLLNPGM("active.");
  53. safe_delay(50);
  54. }
  55. #if ENABLED(UBL_DEVEL_DEBUGGING)
  56. static void debug_echo_axis(const AxisEnum axis) {
  57. if (current_position[axis] == destination[axis])
  58. SERIAL_ECHOPGM("-------------");
  59. else
  60. SERIAL_ECHO_F(destination[X_AXIS], 6);
  61. }
  62. void debug_current_and_destination(const char *title) {
  63. // if the title message starts with a '!' it is so important, we are going to
  64. // ignore the status of the g26_debug_flag
  65. if (*title != '!' && !g26_debug_flag) return;
  66. const float de = destination[E_CART] - current_position[E_CART];
  67. if (de == 0.0) return; // Printing moves only
  68. const float dx = destination[X_AXIS] - current_position[X_AXIS],
  69. dy = destination[Y_AXIS] - current_position[Y_AXIS],
  70. xy_dist = HYPOT(dx, dy);
  71. if (xy_dist == 0.0) return;
  72. SERIAL_ECHOPGM(" fpmm=");
  73. const float fpmm = de / xy_dist;
  74. SERIAL_ECHO_F(fpmm, 6);
  75. SERIAL_ECHOPGM(" current=( ");
  76. SERIAL_ECHO_F(current_position[X_AXIS], 6);
  77. SERIAL_ECHOPGM(", ");
  78. SERIAL_ECHO_F(current_position[Y_AXIS], 6);
  79. SERIAL_ECHOPGM(", ");
  80. SERIAL_ECHO_F(current_position[Z_AXIS], 6);
  81. SERIAL_ECHOPGM(", ");
  82. SERIAL_ECHO_F(current_position[E_CART], 6);
  83. SERIAL_ECHOPGM(" ) destination=( ");
  84. debug_echo_axis(X_AXIS);
  85. SERIAL_ECHOPGM(", ");
  86. debug_echo_axis(Y_AXIS);
  87. SERIAL_ECHOPGM(", ");
  88. debug_echo_axis(Z_AXIS);
  89. SERIAL_ECHOPGM(", ");
  90. debug_echo_axis(E_AXIS);
  91. SERIAL_ECHOPGM(" ) ");
  92. SERIAL_ECHO(title);
  93. SERIAL_EOL();
  94. }
  95. #endif // UBL_DEVEL_DEBUGGING
  96. int8_t unified_bed_leveling::storage_slot;
  97. float unified_bed_leveling::z_values[GRID_MAX_POINTS_X][GRID_MAX_POINTS_Y];
  98. // 15 is the maximum nubmer of grid points supported + 1 safety margin for now,
  99. // until determinism prevails
  100. constexpr float unified_bed_leveling::_mesh_index_to_xpos[16],
  101. unified_bed_leveling::_mesh_index_to_ypos[16];
  102. #if ENABLED(ULTIPANEL)
  103. bool unified_bed_leveling::lcd_map_control = false;
  104. #endif
  105. volatile int unified_bed_leveling::encoder_diff;
  106. unified_bed_leveling::unified_bed_leveling() {
  107. 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.
  108. reset();
  109. }
  110. void unified_bed_leveling::reset() {
  111. const bool was_enabled = planner.leveling_active;
  112. set_bed_leveling_enabled(false);
  113. storage_slot = -1;
  114. #if ENABLED(ENABLE_LEVELING_FADE_HEIGHT)
  115. planner.set_z_fade_height(10.0);
  116. #endif
  117. ZERO(z_values);
  118. if (was_enabled) report_current_position();
  119. }
  120. void unified_bed_leveling::invalidate() {
  121. set_bed_leveling_enabled(false);
  122. set_all_mesh_points_to_value(NAN);
  123. }
  124. void unified_bed_leveling::set_all_mesh_points_to_value(const float value) {
  125. for (uint8_t x = 0; x < GRID_MAX_POINTS_X; x++) {
  126. for (uint8_t y = 0; y < GRID_MAX_POINTS_Y; y++) {
  127. z_values[x][y] = value;
  128. }
  129. }
  130. }
  131. static void serial_echo_xy(const uint8_t sp, const int16_t x, const int16_t y) {
  132. SERIAL_ECHO_SP(sp);
  133. SERIAL_CHAR('(');
  134. if (x < 100) { SERIAL_CHAR(' '); if (x < 10) SERIAL_CHAR(' '); }
  135. SERIAL_ECHO(x);
  136. SERIAL_CHAR(',');
  137. if (y < 100) { SERIAL_CHAR(' '); if (y < 10) SERIAL_CHAR(' '); }
  138. SERIAL_ECHO(y);
  139. SERIAL_CHAR(')');
  140. safe_delay(5);
  141. }
  142. static void serial_echo_column_labels(const uint8_t sp) {
  143. SERIAL_ECHO_SP(7);
  144. for (int8_t i = 0; i < GRID_MAX_POINTS_X; i++) {
  145. if (i < 10) SERIAL_CHAR(' ');
  146. SERIAL_ECHO(i);
  147. SERIAL_ECHO_SP(sp);
  148. }
  149. safe_delay(10);
  150. }
  151. /**
  152. * Produce one of these mesh maps:
  153. * 0: Human-readable
  154. * 1: CSV format for spreadsheet import
  155. * 2: TODO: Display on Graphical LCD
  156. * 4: Compact Human-Readable
  157. */
  158. void unified_bed_leveling::display_map(const int map_type) {
  159. #if HAS_AUTO_REPORTING || ENABLED(HOST_KEEPALIVE_FEATURE)
  160. suspend_auto_report = true;
  161. #endif
  162. constexpr uint8_t eachsp = 1 + 6 + 1, // [-3.567]
  163. twixt = eachsp * (GRID_MAX_POINTS_X) - 9 * 2; // Leading 4sp, Coordinates 9sp each
  164. const bool human = !(map_type & 0x3), csv = map_type == 1, lcd = map_type == 2, comp = map_type & 0x4;
  165. SERIAL_ECHOPGM("\nBed Topography Report");
  166. if (human) {
  167. SERIAL_ECHOPGM(":\n\n");
  168. serial_echo_xy(4, MESH_MIN_X, MESH_MAX_Y);
  169. serial_echo_xy(twixt, MESH_MAX_X, MESH_MAX_Y);
  170. SERIAL_EOL();
  171. serial_echo_column_labels(eachsp - 2);
  172. }
  173. else {
  174. SERIAL_ECHOPGM(" for ");
  175. serialprintPGM(csv ? PSTR("CSV:\n") : PSTR("LCD:\n"));
  176. }
  177. const float current_xi = get_cell_index_x(current_position[X_AXIS] + (MESH_X_DIST) / 2.0),
  178. current_yi = get_cell_index_y(current_position[Y_AXIS] + (MESH_Y_DIST) / 2.0);
  179. if (!lcd) SERIAL_EOL();
  180. for (int8_t j = GRID_MAX_POINTS_Y - 1; j >= 0; j--) {
  181. // Row Label (J index)
  182. if (human) {
  183. if (j < 10) SERIAL_CHAR(' ');
  184. SERIAL_ECHO(j);
  185. SERIAL_ECHOPGM(" |");
  186. }
  187. // Row Values (I indexes)
  188. for (uint8_t i = 0; i < GRID_MAX_POINTS_X; i++) {
  189. // Opening Brace or Space
  190. const bool is_current = i == current_xi && j == current_yi;
  191. if (human) SERIAL_CHAR(is_current ? '[' : ' ');
  192. // Z Value at current I, J
  193. const float f = z_values[i][j];
  194. if (lcd) {
  195. // TODO: Display on Graphical LCD
  196. }
  197. else if (isnan(f))
  198. serialprintPGM(human ? PSTR(" . ") : PSTR("NAN"));
  199. else if (human || csv) {
  200. if (human && f >= 0.0) SERIAL_CHAR(f > 0 ? '+' : ' '); // Space for positive ('-' for negative)
  201. SERIAL_ECHO_F(f, 3); // Positive: 5 digits, Negative: 6 digits
  202. }
  203. idle();
  204. if (csv && i < GRID_MAX_POINTS_X - 1) SERIAL_CHAR('\t');
  205. // Closing Brace or Space
  206. if (human) SERIAL_CHAR(is_current ? ']' : ' ');
  207. #if TX_BUFFER_SIZE > 0
  208. SERIAL_FLUSHTX();
  209. #endif
  210. safe_delay(5);
  211. }
  212. if (!lcd) SERIAL_EOL();
  213. // A blank line between rows (unless compact)
  214. if (j && human && !comp) SERIAL_ECHOLNPGM(" |");
  215. }
  216. if (human) {
  217. serial_echo_column_labels(eachsp - 2);
  218. SERIAL_EOL();
  219. serial_echo_xy(4, MESH_MIN_X, MESH_MIN_Y);
  220. serial_echo_xy(twixt, MESH_MAX_X, MESH_MIN_Y);
  221. SERIAL_EOL();
  222. SERIAL_EOL();
  223. }
  224. #if HAS_AUTO_REPORTING || ENABLED(HOST_KEEPALIVE_FEATURE)
  225. suspend_auto_report = false;
  226. #endif
  227. }
  228. bool unified_bed_leveling::sanity_check() {
  229. uint8_t error_flag = 0;
  230. if (settings.calc_num_meshes() < 1) {
  231. SERIAL_PROTOCOLLNPGM("?Mesh too big for EEPROM.");
  232. error_flag++;
  233. }
  234. return !!error_flag;
  235. }
  236. #endif // AUTO_BED_LEVELING_UBL