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 9.1KB

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