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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  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 I 999");
  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], 6);
  55. SERIAL_ECHOPAIR_P(port, " ; X", LOGICAL_X_POSITION(mesh_index_to_xpos(x)));
  56. SERIAL_ECHOPAIR_P(port, ", Y", LOGICAL_Y_POSITION(mesh_index_to_ypos(y)));
  57. SERIAL_EOL_P(port);
  58. safe_delay(75); // Prevent Printrun from exploding
  59. }
  60. }
  61. void unified_bed_leveling::report_state(
  62. #if NUM_SERIAL > 1
  63. const int8_t port/*= -1*/
  64. #endif
  65. ) {
  66. echo_name(
  67. #if NUM_SERIAL > 1
  68. port
  69. #endif
  70. );
  71. SERIAL_PROTOCOLPGM_P(port, " System v" UBL_VERSION " ");
  72. if (!planner.leveling_active) SERIAL_PROTOCOLPGM_P(port, "in");
  73. SERIAL_PROTOCOLLNPGM_P(port, "active.");
  74. safe_delay(50);
  75. }
  76. static void serial_echo_xy(const int16_t x, const int16_t y) {
  77. SERIAL_CHAR('(');
  78. SERIAL_ECHO(x);
  79. SERIAL_CHAR(',');
  80. SERIAL_ECHO(y);
  81. SERIAL_CHAR(')');
  82. safe_delay(10);
  83. }
  84. #if ENABLED(UBL_DEVEL_DEBUGGING)
  85. static void debug_echo_axis(const AxisEnum axis) {
  86. if (current_position[axis] == destination[axis])
  87. SERIAL_ECHOPGM("-------------");
  88. else
  89. SERIAL_ECHO_F(destination[X_AXIS], 6);
  90. }
  91. void debug_current_and_destination(const char *title) {
  92. // if the title message starts with a '!' it is so important, we are going to
  93. // ignore the status of the g26_debug_flag
  94. if (*title != '!' && !g26_debug_flag) return;
  95. const float de = destination[E_AXIS] - current_position[E_AXIS];
  96. if (de == 0.0) return; // Printing moves only
  97. const float dx = destination[X_AXIS] - current_position[X_AXIS],
  98. dy = destination[Y_AXIS] - current_position[Y_AXIS],
  99. xy_dist = HYPOT(dx, dy);
  100. if (xy_dist == 0.0) return;
  101. SERIAL_ECHOPGM(" fpmm=");
  102. const float fpmm = de / xy_dist;
  103. SERIAL_ECHO_F(fpmm, 6);
  104. SERIAL_ECHOPGM(" current=( ");
  105. SERIAL_ECHO_F(current_position[X_AXIS], 6);
  106. SERIAL_ECHOPGM(", ");
  107. SERIAL_ECHO_F(current_position[Y_AXIS], 6);
  108. SERIAL_ECHOPGM(", ");
  109. SERIAL_ECHO_F(current_position[Z_AXIS], 6);
  110. SERIAL_ECHOPGM(", ");
  111. SERIAL_ECHO_F(current_position[E_AXIS], 6);
  112. SERIAL_ECHOPGM(" ) destination=( ");
  113. debug_echo_axis(X_AXIS);
  114. SERIAL_ECHOPGM(", ");
  115. debug_echo_axis(Y_AXIS);
  116. SERIAL_ECHOPGM(", ");
  117. debug_echo_axis(Z_AXIS);
  118. SERIAL_ECHOPGM(", ");
  119. debug_echo_axis(E_AXIS);
  120. SERIAL_ECHOPGM(" ) ");
  121. SERIAL_ECHO(title);
  122. SERIAL_EOL();
  123. }
  124. #endif // UBL_DEVEL_DEBUGGING
  125. int8_t unified_bed_leveling::storage_slot;
  126. float unified_bed_leveling::z_values[GRID_MAX_POINTS_X][GRID_MAX_POINTS_Y];
  127. // 15 is the maximum nubmer of grid points supported + 1 safety margin for now,
  128. // until determinism prevails
  129. constexpr float unified_bed_leveling::_mesh_index_to_xpos[16],
  130. unified_bed_leveling::_mesh_index_to_ypos[16];
  131. #if ENABLED(ULTIPANEL)
  132. bool unified_bed_leveling::lcd_map_control = false;
  133. #endif
  134. volatile int unified_bed_leveling::encoder_diff;
  135. unified_bed_leveling::unified_bed_leveling() {
  136. 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.
  137. reset();
  138. }
  139. void unified_bed_leveling::reset() {
  140. const bool was_enabled = planner.leveling_active;
  141. set_bed_leveling_enabled(false);
  142. storage_slot = -1;
  143. #if ENABLED(ENABLE_LEVELING_FADE_HEIGHT)
  144. planner.set_z_fade_height(10.0);
  145. #endif
  146. ZERO(z_values);
  147. if (was_enabled) report_current_position();
  148. }
  149. void unified_bed_leveling::invalidate() {
  150. set_bed_leveling_enabled(false);
  151. set_all_mesh_points_to_value(NAN);
  152. }
  153. void unified_bed_leveling::set_all_mesh_points_to_value(const float value) {
  154. for (uint8_t x = 0; x < GRID_MAX_POINTS_X; x++) {
  155. for (uint8_t y = 0; y < GRID_MAX_POINTS_Y; y++) {
  156. z_values[x][y] = value;
  157. }
  158. }
  159. }
  160. // display_map() currently produces three different mesh map types
  161. // 0 : suitable for PronterFace and Repetier's serial console
  162. // 1 : .CSV file suitable for importation into various spread sheets
  163. // 2 : disply of the map data on a RepRap Graphical LCD Panel
  164. void unified_bed_leveling::display_map(const int map_type) {
  165. #if HAS_AUTO_REPORTING || ENABLED(HOST_KEEPALIVE_FEATURE)
  166. suspend_auto_report = true;
  167. #endif
  168. constexpr uint8_t spaces = 8 * (GRID_MAX_POINTS_X - 2);
  169. SERIAL_PROTOCOLPGM("\nBed Topography Report");
  170. if (map_type == 0) {
  171. SERIAL_PROTOCOLPGM(":\n\n");
  172. serial_echo_xy(0, GRID_MAX_POINTS_Y - 1);
  173. SERIAL_ECHO_SP(spaces + 3);
  174. serial_echo_xy(GRID_MAX_POINTS_X - 1, GRID_MAX_POINTS_Y - 1);
  175. SERIAL_EOL();
  176. serial_echo_xy(MESH_MIN_X, MESH_MAX_Y);
  177. SERIAL_ECHO_SP(spaces);
  178. serial_echo_xy(MESH_MAX_X, MESH_MAX_Y);
  179. SERIAL_EOL();
  180. }
  181. else {
  182. SERIAL_PROTOCOLPGM(" for ");
  183. serialprintPGM(map_type == 1 ? PSTR("CSV:\n\n") : PSTR("LCD:\n\n"));
  184. }
  185. const float current_xi = get_cell_index_x(current_position[X_AXIS] + (MESH_X_DIST) / 2.0),
  186. current_yi = get_cell_index_y(current_position[Y_AXIS] + (MESH_Y_DIST) / 2.0);
  187. for (int8_t j = GRID_MAX_POINTS_Y - 1; j >= 0; j--) {
  188. for (uint8_t i = 0; i < GRID_MAX_POINTS_X; i++) {
  189. const bool is_current = i == current_xi && j == current_yi;
  190. // is the nozzle here? then mark the number
  191. if (map_type == 0) SERIAL_CHAR(is_current ? '[' : ' ');
  192. const float f = z_values[i][j];
  193. if (isnan(f)) {
  194. serialprintPGM(map_type == 0 ? PSTR(" . ") : PSTR("NAN"));
  195. }
  196. else if (map_type <= 1) {
  197. // if we don't do this, the columns won't line up nicely
  198. if (map_type == 0 && f >= 0.0) SERIAL_CHAR(' ');
  199. SERIAL_PROTOCOL_F(f, 3);
  200. }
  201. idle();
  202. if (map_type == 1 && i < GRID_MAX_POINTS_X - 1) SERIAL_CHAR(',');
  203. SERIAL_FLUSHTX();
  204. safe_delay(15);
  205. if (map_type == 0) {
  206. SERIAL_CHAR(is_current ? ']' : ' ');
  207. SERIAL_CHAR(' ');
  208. }
  209. }
  210. SERIAL_EOL();
  211. if (j && map_type == 0) { // we want the (0,0) up tight against the block of numbers
  212. SERIAL_CHAR(' ');
  213. SERIAL_EOL();
  214. }
  215. }
  216. if (map_type == 0) {
  217. serial_echo_xy(MESH_MIN_X, MESH_MIN_Y);
  218. SERIAL_ECHO_SP(spaces + 4);
  219. serial_echo_xy(MESH_MAX_X, MESH_MIN_Y);
  220. SERIAL_EOL();
  221. serial_echo_xy(0, 0);
  222. SERIAL_ECHO_SP(spaces + 5);
  223. serial_echo_xy(GRID_MAX_POINTS_X - 1, 0);
  224. SERIAL_EOL();
  225. }
  226. #if HAS_AUTO_REPORTING || ENABLED(HOST_KEEPALIVE_FEATURE)
  227. suspend_auto_report = false;
  228. #endif
  229. }
  230. bool unified_bed_leveling::sanity_check() {
  231. uint8_t error_flag = 0;
  232. if (settings.calc_num_meshes() < 1) {
  233. SERIAL_PROTOCOLLNPGM("?Mesh too big for EEPROM.");
  234. error_flag++;
  235. }
  236. return !!error_flag;
  237. }
  238. #endif // AUTO_BED_LEVELING_UBL