My Marlin configs for Fabrikator Mini and CTC i3 Pro B
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ubl.cpp 8.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  1. /**
  2. * Marlin 3D Printer Firmware
  3. * Copyright (c) 2019 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 "../../../inc/MarlinConfig.h"
  23. #if ENABLED(AUTO_BED_LEVELING_UBL)
  24. #include "../bedlevel.h"
  25. unified_bed_leveling ubl;
  26. #include "../../../module/configuration_store.h"
  27. #include "../../../module/planner.h"
  28. #include "../../../module/motion.h"
  29. #include "../../../module/probe.h"
  30. #if ENABLED(EXTENSIBLE_UI)
  31. #include "../../../lcd/extensible_ui/ui_api.h"
  32. #endif
  33. #include "math.h"
  34. void unified_bed_leveling::echo_name() {
  35. SERIAL_ECHOPGM("Unified Bed Leveling");
  36. }
  37. void unified_bed_leveling::report_current_mesh() {
  38. if (!leveling_is_valid()) return;
  39. SERIAL_ECHO_MSG(" G29 I99");
  40. for (uint8_t x = 0; x < GRID_MAX_POINTS_X; x++)
  41. for (uint8_t y = 0; y < GRID_MAX_POINTS_Y; y++)
  42. if (!isnan(z_values[x][y])) {
  43. SERIAL_ECHO_START();
  44. SERIAL_ECHOPAIR(" M421 I", x, " J", y);
  45. SERIAL_ECHOPAIR_F(" Z", z_values[x][y], 4);
  46. SERIAL_EOL();
  47. serial_delay(75); // Prevent Printrun from exploding
  48. }
  49. }
  50. void unified_bed_leveling::report_state() {
  51. echo_name();
  52. SERIAL_ECHO_TERNARY(planner.leveling_active, " System v" UBL_VERSION " ", "", "in", "active\n");
  53. serial_delay(50);
  54. }
  55. int8_t unified_bed_leveling::storage_slot;
  56. float unified_bed_leveling::z_values[GRID_MAX_POINTS_X][GRID_MAX_POINTS_Y];
  57. #define _GRIDPOS(A,N) (MESH_MIN_##A + N * (MESH_##A##_DIST))
  58. const float
  59. unified_bed_leveling::_mesh_index_to_xpos[GRID_MAX_POINTS_X] PROGMEM = ARRAY_N(GRID_MAX_POINTS_X,
  60. _GRIDPOS(X, 0), _GRIDPOS(X, 1), _GRIDPOS(X, 2), _GRIDPOS(X, 3),
  61. _GRIDPOS(X, 4), _GRIDPOS(X, 5), _GRIDPOS(X, 6), _GRIDPOS(X, 7),
  62. _GRIDPOS(X, 8), _GRIDPOS(X, 9), _GRIDPOS(X, 10), _GRIDPOS(X, 11),
  63. _GRIDPOS(X, 12), _GRIDPOS(X, 13), _GRIDPOS(X, 14), _GRIDPOS(X, 15)
  64. ),
  65. unified_bed_leveling::_mesh_index_to_ypos[GRID_MAX_POINTS_Y] PROGMEM = ARRAY_N(GRID_MAX_POINTS_Y,
  66. _GRIDPOS(Y, 0), _GRIDPOS(Y, 1), _GRIDPOS(Y, 2), _GRIDPOS(Y, 3),
  67. _GRIDPOS(Y, 4), _GRIDPOS(Y, 5), _GRIDPOS(Y, 6), _GRIDPOS(Y, 7),
  68. _GRIDPOS(Y, 8), _GRIDPOS(Y, 9), _GRIDPOS(Y, 10), _GRIDPOS(Y, 11),
  69. _GRIDPOS(Y, 12), _GRIDPOS(Y, 13), _GRIDPOS(Y, 14), _GRIDPOS(Y, 15)
  70. );
  71. #if HAS_LCD_MENU
  72. bool unified_bed_leveling::lcd_map_control = false;
  73. #endif
  74. volatile int unified_bed_leveling::encoder_diff;
  75. unified_bed_leveling::unified_bed_leveling() {
  76. reset();
  77. }
  78. void unified_bed_leveling::reset() {
  79. const bool was_enabled = planner.leveling_active;
  80. set_bed_leveling_enabled(false);
  81. storage_slot = -1;
  82. #if ENABLED(ENABLE_LEVELING_FADE_HEIGHT)
  83. planner.set_z_fade_height(10.0);
  84. #endif
  85. ZERO(z_values);
  86. #if ENABLED(EXTENSIBLE_UI)
  87. for (uint8_t x = 0; x < GRID_MAX_POINTS_X; x++)
  88. for (uint8_t y = 0; y < GRID_MAX_POINTS_Y; y++)
  89. ExtUI::onMeshUpdate(x, y, 0);
  90. #endif
  91. if (was_enabled) report_current_position();
  92. }
  93. void unified_bed_leveling::invalidate() {
  94. set_bed_leveling_enabled(false);
  95. set_all_mesh_points_to_value(NAN);
  96. }
  97. void unified_bed_leveling::set_all_mesh_points_to_value(const float value) {
  98. for (uint8_t x = 0; x < GRID_MAX_POINTS_X; x++) {
  99. for (uint8_t y = 0; y < GRID_MAX_POINTS_Y; y++) {
  100. z_values[x][y] = value;
  101. #if ENABLED(EXTENSIBLE_UI)
  102. ExtUI::onMeshUpdate(x, y, value);
  103. #endif
  104. }
  105. }
  106. }
  107. static void serial_echo_xy(const uint8_t sp, const int16_t x, const int16_t y) {
  108. SERIAL_ECHO_SP(sp);
  109. SERIAL_CHAR('(');
  110. if (x < 100) { SERIAL_CHAR(' '); if (x < 10) SERIAL_CHAR(' '); }
  111. SERIAL_ECHO(x);
  112. SERIAL_CHAR(',');
  113. if (y < 100) { SERIAL_CHAR(' '); if (y < 10) SERIAL_CHAR(' '); }
  114. SERIAL_ECHO(y);
  115. SERIAL_CHAR(')');
  116. serial_delay(5);
  117. }
  118. static void serial_echo_column_labels(const uint8_t sp) {
  119. SERIAL_ECHO_SP(7);
  120. for (int8_t i = 0; i < GRID_MAX_POINTS_X; i++) {
  121. if (i < 10) SERIAL_CHAR(' ');
  122. SERIAL_ECHO(i);
  123. SERIAL_ECHO_SP(sp);
  124. }
  125. serial_delay(10);
  126. }
  127. /**
  128. * Produce one of these mesh maps:
  129. * 0: Human-readable
  130. * 1: CSV format for spreadsheet import
  131. * 2: TODO: Display on Graphical LCD
  132. * 4: Compact Human-Readable
  133. */
  134. void unified_bed_leveling::display_map(const int map_type) {
  135. #if HAS_AUTO_REPORTING || ENABLED(HOST_KEEPALIVE_FEATURE)
  136. suspend_auto_report = true;
  137. #endif
  138. constexpr uint8_t eachsp = 1 + 6 + 1, // [-3.567]
  139. twixt = eachsp * (GRID_MAX_POINTS_X) - 9 * 2; // Leading 4sp, Coordinates 9sp each
  140. const bool human = !(map_type & 0x3), csv = map_type == 1, lcd = map_type == 2, comp = map_type & 0x4;
  141. SERIAL_ECHOPGM("\nBed Topography Report");
  142. if (human) {
  143. SERIAL_ECHOLNPGM(":\n");
  144. serial_echo_xy(4, MESH_MIN_X, MESH_MAX_Y);
  145. serial_echo_xy(twixt, MESH_MAX_X, MESH_MAX_Y);
  146. SERIAL_EOL();
  147. serial_echo_column_labels(eachsp - 2);
  148. }
  149. else {
  150. SERIAL_ECHOPGM(" for ");
  151. serialprintPGM(csv ? PSTR("CSV:\n") : PSTR("LCD:\n"));
  152. }
  153. // Add XY_PROBE_OFFSET_FROM_EXTRUDER because probe_pt() subtracts these when
  154. // moving to the xy position to be measured. This ensures better agreement between
  155. // the current Z position after G28 and the mesh values.
  156. const float current_xi = find_closest_x_index(current_position[X_AXIS] + X_PROBE_OFFSET_FROM_EXTRUDER),
  157. current_yi = find_closest_y_index(current_position[Y_AXIS] + Y_PROBE_OFFSET_FROM_EXTRUDER);
  158. if (!lcd) SERIAL_EOL();
  159. for (int8_t j = GRID_MAX_POINTS_Y - 1; j >= 0; j--) {
  160. // Row Label (J index)
  161. if (human) {
  162. if (j < 10) SERIAL_CHAR(' ');
  163. SERIAL_ECHO(j);
  164. SERIAL_ECHOPGM(" |");
  165. }
  166. // Row Values (I indexes)
  167. for (uint8_t i = 0; i < GRID_MAX_POINTS_X; i++) {
  168. // Opening Brace or Space
  169. const bool is_current = i == current_xi && j == current_yi;
  170. if (human) SERIAL_CHAR(is_current ? '[' : ' ');
  171. // Z Value at current I, J
  172. const float f = z_values[i][j];
  173. if (lcd) {
  174. // TODO: Display on Graphical LCD
  175. }
  176. else if (isnan(f))
  177. serialprintPGM(human ? PSTR(" . ") : PSTR("NAN"));
  178. else if (human || csv) {
  179. if (human && f >= 0.0) SERIAL_CHAR(f > 0 ? '+' : ' '); // Space for positive ('-' for negative)
  180. SERIAL_ECHO_F(f, 3); // Positive: 5 digits, Negative: 6 digits
  181. }
  182. if (csv && i < GRID_MAX_POINTS_X - 1) SERIAL_CHAR('\t');
  183. // Closing Brace or Space
  184. if (human) SERIAL_CHAR(is_current ? ']' : ' ');
  185. SERIAL_FLUSHTX();
  186. idle();
  187. }
  188. if (!lcd) SERIAL_EOL();
  189. // A blank line between rows (unless compact)
  190. if (j && human && !comp) SERIAL_ECHOLNPGM(" |");
  191. }
  192. if (human) {
  193. serial_echo_column_labels(eachsp - 2);
  194. SERIAL_EOL();
  195. serial_echo_xy(4, MESH_MIN_X, MESH_MIN_Y);
  196. serial_echo_xy(twixt, MESH_MAX_X, MESH_MIN_Y);
  197. SERIAL_EOL();
  198. SERIAL_EOL();
  199. }
  200. #if HAS_AUTO_REPORTING || ENABLED(HOST_KEEPALIVE_FEATURE)
  201. suspend_auto_report = false;
  202. #endif
  203. }
  204. bool unified_bed_leveling::sanity_check() {
  205. uint8_t error_flag = 0;
  206. if (settings.calc_num_meshes() < 1) {
  207. SERIAL_ECHOLNPGM("?Mesh too big for EEPROM.");
  208. error_flag++;
  209. }
  210. return !!error_flag;
  211. }
  212. #endif // AUTO_BED_LEVELING_UBL