My Marlin configs for Fabrikator Mini and CTC i3 Pro B
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

ubl.cpp 7.8KB

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