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.6KB

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