My Marlin configs for Fabrikator Mini and CTC i3 Pro B
Du kannst nicht mehr als 25 Themen auswählen Themen müssen mit entweder einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

UBL_Bed_Leveling.cpp 10KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  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 "Marlin.h"
  23. #include "math.h"
  24. #if ENABLED(AUTO_BED_LEVELING_UBL)
  25. #include "UBL.h"
  26. #include "hex_print_routines.h"
  27. /**
  28. * These support functions allow the use of large bit arrays of flags that take very
  29. * little RAM. Currently they are limited to being 16x16 in size. Changing the declaration
  30. * to unsigned long will allow us to go to 32x32 if higher resolution Mesh's are needed
  31. * in the future.
  32. */
  33. void bit_clear(uint16_t bits[16], uint8_t x, uint8_t y) { CBI(bits[y], x); }
  34. void bit_set(uint16_t bits[16], uint8_t x, uint8_t y) { SBI(bits[y], x); }
  35. bool is_bit_set(uint16_t bits[16], uint8_t x, uint8_t y) { return TEST(bits[y], x); }
  36. static void serial_echo_xy(const uint16_t x, const uint16_t y) {
  37. SERIAL_CHAR('(');
  38. SERIAL_ECHO(x);
  39. SERIAL_CHAR(',');
  40. SERIAL_ECHO(y);
  41. SERIAL_CHAR(')');
  42. safe_delay(10);
  43. }
  44. static void serial_echo_10x_spaces() {
  45. for (uint8_t i = UBL_MESH_NUM_X_POINTS - 1; --i;) {
  46. SERIAL_ECHOPGM(" ");
  47. #if TX_BUFFER_SIZE > 0
  48. MYSERIAL.flushTX();
  49. #endif
  50. safe_delay(10);
  51. }
  52. }
  53. /**
  54. * These variables used to be declared inside the unified_bed_leveling class. We are going to
  55. * still declare them within the .cpp file for bed leveling. But there is only one instance of
  56. * the bed leveling object and we can get rid of a level of inderection by not making them
  57. * 'member data'. So, in the interest of speed, we do it this way. On a 32-bit CPU they can be
  58. * moved back inside the bed leveling class.
  59. */
  60. float last_specified_z,
  61. fade_scaling_factor_for_current_height,
  62. z_values[UBL_MESH_NUM_X_POINTS][UBL_MESH_NUM_Y_POINTS],
  63. mesh_index_to_x_location[UBL_MESH_NUM_X_POINTS + 1], // +1 just because of paranoia that we might end up on the
  64. mesh_index_to_y_location[UBL_MESH_NUM_Y_POINTS + 1]; // the last Mesh Line and that is the start of a whole new cell
  65. unified_bed_leveling::unified_bed_leveling() {
  66. for (uint8_t i = 0; i <= UBL_MESH_NUM_X_POINTS; i++) // We go one past what we expect to ever need for safety
  67. mesh_index_to_x_location[i] = double(UBL_MESH_MIN_X) + double(MESH_X_DIST) * double(i);
  68. for (uint8_t i = 0; i <= UBL_MESH_NUM_Y_POINTS; i++) // We go one past what we expect to ever need for safety
  69. mesh_index_to_y_location[i] = double(UBL_MESH_MIN_Y) + double(MESH_Y_DIST) * double(i);
  70. reset();
  71. }
  72. void unified_bed_leveling::store_state() {
  73. const uint16_t i = UBL_LAST_EEPROM_INDEX;
  74. eeprom_write_block((void *)&ubl.state, (void *)i, sizeof(state));
  75. }
  76. void unified_bed_leveling::load_state() {
  77. const uint16_t i = UBL_LAST_EEPROM_INDEX;
  78. eeprom_read_block((void *)&ubl.state, (void *)i, sizeof(state));
  79. if (sanity_check())
  80. SERIAL_PROTOCOLLNPGM("?In load_state() sanity_check() failed.\n");
  81. #if ENABLED(ENABLE_LEVELING_FADE_HEIGHT)
  82. /**
  83. * These lines can go away in a few weeks. They are just
  84. * to make sure people updating thier firmware won't be using
  85. * an incomplete Bed_Leveling.state structure. For speed
  86. * we now multiply by the inverse of the Fade Height instead of
  87. * dividing by it. Soon... all of the old structures will be
  88. * updated, but until then, we try to ease the transition
  89. * for our Beta testers.
  90. */
  91. if (ubl.state.g29_fade_height_multiplier != 1.0 / ubl.state.g29_correction_fade_height) {
  92. ubl.state.g29_fade_height_multiplier = 1.0 / ubl.state.g29_correction_fade_height;
  93. store_state();
  94. }
  95. #endif
  96. }
  97. void unified_bed_leveling::load_mesh(const int16_t m) {
  98. int16_t j = (UBL_LAST_EEPROM_INDEX - ubl_eeprom_start) / sizeof(z_values);
  99. if (m == -1) {
  100. SERIAL_PROTOCOLLNPGM("?No mesh saved in EEPROM. Zeroing mesh in memory.\n");
  101. reset();
  102. return;
  103. }
  104. if (m < 0 || m >= j || ubl_eeprom_start <= 0) {
  105. SERIAL_PROTOCOLLNPGM("?EEPROM storage not available to load mesh.\n");
  106. return;
  107. }
  108. j = UBL_LAST_EEPROM_INDEX - (m + 1) * sizeof(z_values);
  109. eeprom_read_block((void *)&z_values, (void *)j, sizeof(z_values));
  110. SERIAL_PROTOCOLPAIR("Mesh loaded from slot ", m);
  111. SERIAL_PROTOCOLLNPAIR(" at offset 0x", hex_word(j));
  112. }
  113. void unified_bed_leveling::store_mesh(const int16_t m) {
  114. int16_t j = (UBL_LAST_EEPROM_INDEX - ubl_eeprom_start) / sizeof(z_values);
  115. if (m < 0 || m >= j || ubl_eeprom_start <= 0) {
  116. SERIAL_PROTOCOLLNPGM("?EEPROM storage not available to load mesh.\n");
  117. SERIAL_PROTOCOL(m);
  118. SERIAL_PROTOCOLLNPGM(" mesh slots available.\n");
  119. SERIAL_PROTOCOLLNPAIR("E2END : ", E2END);
  120. SERIAL_PROTOCOLLNPAIR("k : ", (int)UBL_LAST_EEPROM_INDEX);
  121. SERIAL_PROTOCOLLNPAIR("j : ", j);
  122. SERIAL_PROTOCOLLNPAIR("m : ", m);
  123. SERIAL_EOL;
  124. return;
  125. }
  126. j = UBL_LAST_EEPROM_INDEX - (m + 1) * sizeof(z_values);
  127. eeprom_write_block((const void *)&z_values, (void *)j, sizeof(z_values));
  128. SERIAL_PROTOCOLPAIR("Mesh saved in slot ", m);
  129. SERIAL_PROTOCOLLNPAIR(" at offset 0x", hex_word(j));
  130. }
  131. void unified_bed_leveling::reset() {
  132. state.active = false;
  133. state.z_offset = 0;
  134. state.eeprom_storage_slot = -1;
  135. ZERO(z_values);
  136. last_specified_z = -999.9;
  137. fade_scaling_factor_for_current_height = 0.0;
  138. }
  139. void unified_bed_leveling::invalidate() {
  140. print_hex_word((uint16_t)this);
  141. SERIAL_EOL;
  142. state.active = false;
  143. state.z_offset = 0;
  144. for (int x = 0; x < UBL_MESH_NUM_X_POINTS; x++)
  145. for (int y = 0; y < UBL_MESH_NUM_Y_POINTS; y++)
  146. z_values[x][y] = NAN;
  147. }
  148. void unified_bed_leveling::display_map(const int map_type) {
  149. const bool map0 = map_type == 0;
  150. if (map0) {
  151. SERIAL_PROTOCOLLNPGM("\nBed Topography Report:\n");
  152. serial_echo_xy(0, UBL_MESH_NUM_Y_POINTS - 1);
  153. SERIAL_ECHOPGM(" ");
  154. }
  155. if (map0) {
  156. serial_echo_10x_spaces();
  157. serial_echo_xy(UBL_MESH_NUM_X_POINTS - 1, UBL_MESH_NUM_Y_POINTS - 1);
  158. SERIAL_EOL;
  159. serial_echo_xy(UBL_MESH_MIN_X, UBL_MESH_MIN_Y);
  160. serial_echo_10x_spaces();
  161. serial_echo_xy(UBL_MESH_MAX_X, UBL_MESH_MAX_Y);
  162. SERIAL_EOL;
  163. }
  164. const float current_xi = ubl.get_cell_index_x(current_position[X_AXIS] + (MESH_X_DIST) / 2.0),
  165. current_yi = ubl.get_cell_index_y(current_position[Y_AXIS] + (MESH_Y_DIST) / 2.0);
  166. for (uint8_t j = UBL_MESH_NUM_Y_POINTS - 1; j >= 0; j--) {
  167. for (uint8_t i = 0; i < UBL_MESH_NUM_X_POINTS; i++) {
  168. const bool is_current = i == current_xi && j == current_yi;
  169. // is the nozzle here? if so, mark the number
  170. if (map0)
  171. SERIAL_CHAR(is_current ? '[' : ' ');
  172. const float f = z_values[i][j];
  173. if (isnan(f)) {
  174. serialprintPGM(map0 ? PSTR(" . ") : PSTR("NAN"));
  175. }
  176. else {
  177. // if we don't do this, the columns won't line up nicely
  178. if (f >= 0.0 && map0) SERIAL_CHAR(' ');
  179. SERIAL_PROTOCOL_F(f, 3);
  180. idle();
  181. }
  182. if (!map0 && i < UBL_MESH_NUM_X_POINTS - 1)
  183. SERIAL_CHAR(',');
  184. #if TX_BUFFER_SIZE > 0
  185. MYSERIAL.flushTX();
  186. #endif
  187. safe_delay(15);
  188. if (map0) {
  189. SERIAL_CHAR(is_current ? ']' : ' ');
  190. SERIAL_CHAR(' ');
  191. }
  192. }
  193. SERIAL_EOL;
  194. if (j && map0) { // we want the (0,0) up tight against the block of numbers
  195. SERIAL_CHAR(' ');
  196. SERIAL_EOL;
  197. }
  198. }
  199. if (map0) {
  200. serial_echo_xy(UBL_MESH_MIN_X, UBL_MESH_MIN_Y);
  201. SERIAL_ECHOPGM(" ");
  202. serial_echo_10x_spaces();
  203. serial_echo_xy(UBL_MESH_MAX_X, UBL_MESH_MIN_Y);
  204. SERIAL_EOL;
  205. serial_echo_xy(0, 0);
  206. SERIAL_ECHOPGM(" ");
  207. serial_echo_10x_spaces();
  208. serial_echo_xy(UBL_MESH_NUM_X_POINTS - 1, 0);
  209. SERIAL_EOL;
  210. }
  211. }
  212. bool unified_bed_leveling::sanity_check() {
  213. uint8_t error_flag = 0;
  214. if (state.n_x != UBL_MESH_NUM_X_POINTS) {
  215. SERIAL_PROTOCOLLNPGM("?UBL_MESH_NUM_X_POINTS set wrong\n");
  216. error_flag++;
  217. }
  218. if (state.n_y != UBL_MESH_NUM_Y_POINTS) {
  219. SERIAL_PROTOCOLLNPGM("?UBL_MESH_NUM_Y_POINTS set wrong\n");
  220. error_flag++;
  221. }
  222. if (state.mesh_x_min != UBL_MESH_MIN_X) {
  223. SERIAL_PROTOCOLLNPGM("?UBL_MESH_MIN_X set wrong\n");
  224. error_flag++;
  225. }
  226. if (state.mesh_y_min != UBL_MESH_MIN_Y) {
  227. SERIAL_PROTOCOLLNPGM("?UBL_MESH_MIN_Y set wrong\n");
  228. error_flag++;
  229. }
  230. if (state.mesh_x_max != UBL_MESH_MAX_X) {
  231. SERIAL_PROTOCOLLNPGM("?UBL_MESH_MAX_X set wrong\n");
  232. error_flag++;
  233. }
  234. if (state.mesh_y_max != UBL_MESH_MAX_Y) {
  235. SERIAL_PROTOCOLLNPGM("?UBL_MESH_MAX_Y set wrong\n");
  236. error_flag++;
  237. }
  238. if (state.mesh_x_dist != MESH_X_DIST) {
  239. SERIAL_PROTOCOLLNPGM("?MESH_X_DIST set wrong\n");
  240. error_flag++;
  241. }
  242. if (state.mesh_y_dist != MESH_Y_DIST) {
  243. SERIAL_PROTOCOLLNPGM("?MESH_Y_DIST set wrong\n");
  244. error_flag++;
  245. }
  246. const int j = (UBL_LAST_EEPROM_INDEX - ubl_eeprom_start) / sizeof(z_values);
  247. if (j < 1) {
  248. SERIAL_PROTOCOLLNPGM("?No EEPROM storage available for a mesh of this size.\n");
  249. error_flag++;
  250. }
  251. // SERIAL_PROTOCOLPGM("?sanity_check() return value: ");
  252. // SERIAL_PROTOCOL(error_flag);
  253. // SERIAL_EOL;
  254. return !!error_flag;
  255. }
  256. #endif // AUTO_BED_LEVELING_UBL