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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  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 <https://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 bedlevel;
  26. #include "../../../MarlinCore.h"
  27. #include "../../../gcode/gcode.h"
  28. #include "../../../module/settings.h"
  29. #include "../../../module/planner.h"
  30. #include "../../../module/motion.h"
  31. #include "../../../module/probe.h"
  32. #include "../../../module/temperature.h"
  33. #if ENABLED(EXTENSIBLE_UI)
  34. #include "../../../lcd/extui/ui_api.h"
  35. #endif
  36. #include "math.h"
  37. void unified_bed_leveling::echo_name() { SERIAL_ECHOPGM("Unified Bed Leveling"); }
  38. void unified_bed_leveling::report_current_mesh() {
  39. if (!leveling_is_valid()) return;
  40. SERIAL_ECHO_MSG(" G29 I999");
  41. GRID_LOOP(x, y)
  42. if (!isnan(z_values[x][y])) {
  43. SERIAL_ECHO_START();
  44. SERIAL_ECHOPGM(" M421 I", x, " J", y);
  45. SERIAL_ECHOLNPAIR_F_P(SP_Z_STR, z_values[x][y], 4);
  46. serial_delay(75); // Prevent Printrun from exploding
  47. }
  48. }
  49. void unified_bed_leveling::report_state() {
  50. echo_name();
  51. SERIAL_ECHO_TERNARY(planner.leveling_active, " System v" UBL_VERSION " ", "", "in", "active\n");
  52. serial_delay(50);
  53. }
  54. int8_t unified_bed_leveling::storage_slot;
  55. float unified_bed_leveling::z_values[GRID_MAX_POINTS_X][GRID_MAX_POINTS_Y];
  56. #define _GRIDPOS(A,N) (MESH_MIN_##A + N * (MESH_##A##_DIST))
  57. const float
  58. unified_bed_leveling::_mesh_index_to_xpos[GRID_MAX_POINTS_X] PROGMEM = ARRAY_N(GRID_MAX_POINTS_X,
  59. _GRIDPOS(X, 0), _GRIDPOS(X, 1), _GRIDPOS(X, 2), _GRIDPOS(X, 3),
  60. _GRIDPOS(X, 4), _GRIDPOS(X, 5), _GRIDPOS(X, 6), _GRIDPOS(X, 7),
  61. _GRIDPOS(X, 8), _GRIDPOS(X, 9), _GRIDPOS(X, 10), _GRIDPOS(X, 11),
  62. _GRIDPOS(X, 12), _GRIDPOS(X, 13), _GRIDPOS(X, 14), _GRIDPOS(X, 15)
  63. ),
  64. unified_bed_leveling::_mesh_index_to_ypos[GRID_MAX_POINTS_Y] PROGMEM = ARRAY_N(GRID_MAX_POINTS_Y,
  65. _GRIDPOS(Y, 0), _GRIDPOS(Y, 1), _GRIDPOS(Y, 2), _GRIDPOS(Y, 3),
  66. _GRIDPOS(Y, 4), _GRIDPOS(Y, 5), _GRIDPOS(Y, 6), _GRIDPOS(Y, 7),
  67. _GRIDPOS(Y, 8), _GRIDPOS(Y, 9), _GRIDPOS(Y, 10), _GRIDPOS(Y, 11),
  68. _GRIDPOS(Y, 12), _GRIDPOS(Y, 13), _GRIDPOS(Y, 14), _GRIDPOS(Y, 15)
  69. );
  70. volatile int16_t unified_bed_leveling::encoder_diff;
  71. unified_bed_leveling::unified_bed_leveling() { reset(); }
  72. void unified_bed_leveling::reset() {
  73. const bool was_enabled = planner.leveling_active;
  74. set_bed_leveling_enabled(false);
  75. storage_slot = -1;
  76. ZERO(z_values);
  77. #if ENABLED(EXTENSIBLE_UI)
  78. GRID_LOOP(x, y) ExtUI::onMeshUpdate(x, y, 0);
  79. #endif
  80. if (was_enabled) report_current_position();
  81. }
  82. void unified_bed_leveling::invalidate() {
  83. set_bed_leveling_enabled(false);
  84. set_all_mesh_points_to_value(NAN);
  85. }
  86. void unified_bed_leveling::set_all_mesh_points_to_value(const_float_t value) {
  87. GRID_LOOP(x, y) {
  88. z_values[x][y] = value;
  89. TERN_(EXTENSIBLE_UI, ExtUI::onMeshUpdate(x, y, value));
  90. }
  91. }
  92. #if ENABLED(OPTIMIZED_MESH_STORAGE)
  93. constexpr float mesh_store_scaling = 1000;
  94. constexpr int16_t Z_STEPS_NAN = INT16_MAX;
  95. void unified_bed_leveling::set_store_from_mesh(const bed_mesh_t &in_values, mesh_store_t &stored_values) {
  96. auto z_to_store = [](const_float_t z) {
  97. if (isnan(z)) return Z_STEPS_NAN;
  98. const int32_t z_scaled = TRUNC(z * mesh_store_scaling);
  99. if (z_scaled == Z_STEPS_NAN || !WITHIN(z_scaled, INT16_MIN, INT16_MAX))
  100. return Z_STEPS_NAN; // If Z is out of range, return our custom 'NaN'
  101. return int16_t(z_scaled);
  102. };
  103. GRID_LOOP(x, y) stored_values[x][y] = z_to_store(in_values[x][y]);
  104. }
  105. void unified_bed_leveling::set_mesh_from_store(const mesh_store_t &stored_values, bed_mesh_t &out_values) {
  106. auto store_to_z = [](const int16_t z_scaled) {
  107. return z_scaled == Z_STEPS_NAN ? NAN : z_scaled / mesh_store_scaling;
  108. };
  109. GRID_LOOP(x, y) out_values[x][y] = store_to_z(stored_values[x][y]);
  110. }
  111. #endif // OPTIMIZED_MESH_STORAGE
  112. static void serial_echo_xy(const uint8_t sp, const int16_t x, const int16_t y) {
  113. SERIAL_ECHO_SP(sp);
  114. SERIAL_CHAR('(');
  115. if (x < 100) { SERIAL_CHAR(' '); if (x < 10) SERIAL_CHAR(' '); }
  116. SERIAL_ECHO(x);
  117. SERIAL_CHAR(',');
  118. if (y < 100) { SERIAL_CHAR(' '); if (y < 10) SERIAL_CHAR(' '); }
  119. SERIAL_ECHO(y);
  120. SERIAL_CHAR(')');
  121. serial_delay(5);
  122. }
  123. static void serial_echo_column_labels(const uint8_t sp) {
  124. SERIAL_ECHO_SP(7);
  125. LOOP_L_N(i, GRID_MAX_POINTS_X) {
  126. if (i < 10) SERIAL_CHAR(' ');
  127. SERIAL_ECHO(i);
  128. SERIAL_ECHO_SP(sp);
  129. }
  130. serial_delay(10);
  131. }
  132. /**
  133. * Produce one of these mesh maps:
  134. * 0: Human-readable
  135. * 1: CSV format for spreadsheet import
  136. * 2: TODO: Display on Graphical LCD
  137. * 4: Compact Human-Readable
  138. */
  139. void unified_bed_leveling::display_map(const uint8_t map_type) {
  140. const bool was = gcode.set_autoreport_paused(true);
  141. constexpr uint8_t eachsp = 1 + 6 + 1, // [-3.567]
  142. twixt = eachsp * (GRID_MAX_POINTS_X) - 9 * 2; // Leading 4sp, Coordinates 9sp each
  143. const bool human = !(map_type & 0x3), csv = map_type == 1, lcd = map_type == 2, comp = map_type & 0x4;
  144. SERIAL_ECHOPGM("\nBed Topography Report");
  145. if (human) {
  146. SERIAL_ECHOLNPGM(":\n");
  147. serial_echo_xy(4, MESH_MIN_X, MESH_MAX_Y);
  148. serial_echo_xy(twixt, MESH_MAX_X, MESH_MAX_Y);
  149. SERIAL_EOL();
  150. serial_echo_column_labels(eachsp - 2);
  151. }
  152. else
  153. SERIAL_ECHOPGM(" for ", csv ? F("CSV:\n") : F("LCD:\n"));
  154. // Add XY probe offset from extruder because probe.probe_at_point() subtracts them when
  155. // moving to the XY position to be measured. This ensures better agreement between
  156. // the current Z position after G28 and the mesh values.
  157. const xy_int8_t curr = closest_indexes(xy_pos_t(current_position) + probe.offset_xy);
  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. LOOP_L_N(i, GRID_MAX_POINTS_X) {
  168. // Opening Brace or Space
  169. const bool is_current = i == curr.x && j == curr.y;
  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. SERIAL_ECHOF(human ? F(" . ") : F("NAN"));
  178. else if (human || csv) {
  179. if (human && f >= 0) SERIAL_CHAR(f > 0 ? '+' : ' '); // Display sign also for positive numbers (' ' for 0)
  180. SERIAL_DECIMAL(f); // 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_no_sleep();
  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. gcode.set_autoreport_paused(was);
  201. }
  202. bool unified_bed_leveling::sanity_check() {
  203. uint8_t error_flag = 0;
  204. if (settings.calc_num_meshes() < 1) {
  205. SERIAL_ECHOLNPGM("?Mesh too big for EEPROM.");
  206. error_flag++;
  207. }
  208. return !!error_flag;
  209. }
  210. #if ENABLED(UBL_MESH_WIZARD)
  211. /**
  212. * M1004: UBL Mesh Wizard - One-click mesh creation with or without a probe
  213. */
  214. void GcodeSuite::M1004() {
  215. #define ALIGN_GCODE TERN(Z_STEPPER_AUTO_ALIGN, "G34\n", "")
  216. #define PROBE_GCODE TERN(HAS_BED_PROBE, "G29P1\nG29P3", "G29P4R")
  217. #if HAS_HOTEND
  218. if (parser.seenval('H')) { // Handle H# parameter to set Hotend temp
  219. const celsius_t hotend_temp = parser.value_int(); // Marlin never sends itself F or K, always C
  220. thermalManager.setTargetHotend(hotend_temp, 0);
  221. thermalManager.wait_for_hotend(false);
  222. }
  223. #endif
  224. #if HAS_HEATED_BED
  225. if (parser.seenval('B')) { // Handle B# parameter to set Bed temp
  226. const celsius_t bed_temp = parser.value_int(); // Marlin never sends itself F or K, always C
  227. thermalManager.setTargetBed(bed_temp);
  228. thermalManager.wait_for_bed(false);
  229. }
  230. #endif
  231. process_subcommands_now(FPSTR(G28_STR)); // Home
  232. process_subcommands_now(F(ALIGN_GCODE // Align multi z axis if available
  233. PROBE_GCODE "\n" // Build mesh with available hardware
  234. "G29P3\nG29P3")); // Ensure mesh is complete by running smart fill twice
  235. if (parser.seenval('S')) {
  236. char umw_gcode[32];
  237. sprintf_P(umw_gcode, PSTR("G29S%i"), parser.value_int());
  238. queue.inject(umw_gcode);
  239. }
  240. process_subcommands_now(F("G29A\nG29F10\n" // Set UBL Active & Fade 10
  241. "M140S0\nM104S0\n" // Turn off heaters
  242. "M500")); // Store settings
  243. }
  244. #endif // UBL_MESH_WIZARD
  245. #endif // AUTO_BED_LEVELING_UBL