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.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  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 ENABLED(EXTENSIBLE_UI)
  108. for (uint8_t x = 0; x < GRID_MAX_POINTS_X; x++)
  109. for (uint8_t y = 0; y < GRID_MAX_POINTS_Y; y++)
  110. ExtUI::onMeshUpdate(x, y, 0);
  111. #endif
  112. if (was_enabled) report_current_position();
  113. }
  114. void unified_bed_leveling::invalidate() {
  115. set_bed_leveling_enabled(false);
  116. set_all_mesh_points_to_value(NAN);
  117. }
  118. void unified_bed_leveling::set_all_mesh_points_to_value(const float value) {
  119. for (uint8_t x = 0; x < GRID_MAX_POINTS_X; x++) {
  120. for (uint8_t y = 0; y < GRID_MAX_POINTS_Y; y++) {
  121. z_values[x][y] = value;
  122. #if ENABLED(EXTENSIBLE_UI)
  123. ExtUI::onMeshUpdate(x, y, value);
  124. #endif
  125. }
  126. }
  127. }
  128. static void serial_echo_xy(const uint8_t sp, const int16_t x, const int16_t y) {
  129. SERIAL_ECHO_SP(sp);
  130. SERIAL_CHAR('(');
  131. if (x < 100) { SERIAL_CHAR(' '); if (x < 10) SERIAL_CHAR(' '); }
  132. SERIAL_ECHO(x);
  133. SERIAL_CHAR(',');
  134. if (y < 100) { SERIAL_CHAR(' '); if (y < 10) SERIAL_CHAR(' '); }
  135. SERIAL_ECHO(y);
  136. SERIAL_CHAR(')');
  137. serial_delay(5);
  138. }
  139. static void serial_echo_column_labels(const uint8_t sp) {
  140. SERIAL_ECHO_SP(7);
  141. for (int8_t i = 0; i < GRID_MAX_POINTS_X; i++) {
  142. if (i < 10) SERIAL_CHAR(' ');
  143. SERIAL_ECHO(i);
  144. SERIAL_ECHO_SP(sp);
  145. }
  146. serial_delay(10);
  147. }
  148. /**
  149. * Produce one of these mesh maps:
  150. * 0: Human-readable
  151. * 1: CSV format for spreadsheet import
  152. * 2: TODO: Display on Graphical LCD
  153. * 4: Compact Human-Readable
  154. */
  155. void unified_bed_leveling::display_map(const int map_type) {
  156. #if HAS_AUTO_REPORTING || ENABLED(HOST_KEEPALIVE_FEATURE)
  157. suspend_auto_report = true;
  158. #endif
  159. constexpr uint8_t eachsp = 1 + 6 + 1, // [-3.567]
  160. twixt = eachsp * (GRID_MAX_POINTS_X) - 9 * 2; // Leading 4sp, Coordinates 9sp each
  161. const bool human = !(map_type & 0x3), csv = map_type == 1, lcd = map_type == 2, comp = map_type & 0x4;
  162. SERIAL_ECHOPGM("\nBed Topography Report");
  163. if (human) {
  164. SERIAL_ECHOLNPGM(":\n");
  165. serial_echo_xy(4, MESH_MIN_X, MESH_MAX_Y);
  166. serial_echo_xy(twixt, MESH_MAX_X, MESH_MAX_Y);
  167. SERIAL_EOL();
  168. serial_echo_column_labels(eachsp - 2);
  169. }
  170. else {
  171. SERIAL_ECHOPGM(" for ");
  172. serialprintPGM(csv ? PSTR("CSV:\n") : PSTR("LCD:\n"));
  173. }
  174. // Add XY_PROBE_OFFSET_FROM_EXTRUDER because probe_pt() subtracts these when
  175. // moving to the xy position to be measured. This ensures better agreement between
  176. // the current Z position after G28 and the mesh values.
  177. const float current_xi = find_closest_x_index(current_position[X_AXIS] + X_PROBE_OFFSET_FROM_EXTRUDER),
  178. current_yi = find_closest_y_index(current_position[Y_AXIS] + Y_PROBE_OFFSET_FROM_EXTRUDER);
  179. if (!lcd) SERIAL_EOL();
  180. for (int8_t j = GRID_MAX_POINTS_Y - 1; j >= 0; j--) {
  181. // Row Label (J index)
  182. if (human) {
  183. if (j < 10) SERIAL_CHAR(' ');
  184. SERIAL_ECHO(j);
  185. SERIAL_ECHOPGM(" |");
  186. }
  187. // Row Values (I indexes)
  188. for (uint8_t i = 0; i < GRID_MAX_POINTS_X; i++) {
  189. // Opening Brace or Space
  190. const bool is_current = i == current_xi && j == current_yi;
  191. if (human) SERIAL_CHAR(is_current ? '[' : ' ');
  192. // Z Value at current I, J
  193. const float f = z_values[i][j];
  194. if (lcd) {
  195. // TODO: Display on Graphical LCD
  196. }
  197. else if (isnan(f))
  198. serialprintPGM(human ? PSTR(" . ") : PSTR("NAN"));
  199. else if (human || csv) {
  200. if (human && f >= 0.0) SERIAL_CHAR(f > 0 ? '+' : ' '); // Space for positive ('-' for negative)
  201. SERIAL_ECHO_F(f, 3); // Positive: 5 digits, Negative: 6 digits
  202. }
  203. if (csv && i < GRID_MAX_POINTS_X - 1) SERIAL_CHAR('\t');
  204. // Closing Brace or Space
  205. if (human) SERIAL_CHAR(is_current ? ']' : ' ');
  206. SERIAL_FLUSHTX();
  207. idle();
  208. }
  209. if (!lcd) SERIAL_EOL();
  210. // A blank line between rows (unless compact)
  211. if (j && human && !comp) SERIAL_ECHOLNPGM(" |");
  212. }
  213. if (human) {
  214. serial_echo_column_labels(eachsp - 2);
  215. SERIAL_EOL();
  216. serial_echo_xy(4, MESH_MIN_X, MESH_MIN_Y);
  217. serial_echo_xy(twixt, MESH_MAX_X, MESH_MIN_Y);
  218. SERIAL_EOL();
  219. SERIAL_EOL();
  220. }
  221. #if HAS_AUTO_REPORTING || ENABLED(HOST_KEEPALIVE_FEATURE)
  222. suspend_auto_report = false;
  223. #endif
  224. }
  225. bool unified_bed_leveling::sanity_check() {
  226. uint8_t error_flag = 0;
  227. if (settings.calc_num_meshes() < 1) {
  228. SERIAL_ECHOLNPGM("?Mesh too big for EEPROM.");
  229. error_flag++;
  230. }
  231. return !!error_flag;
  232. }
  233. #endif // AUTO_BED_LEVELING_UBL