My Marlin configs for Fabrikator Mini and CTC i3 Pro B
選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

UBL_Bed_Leveling.cpp 9.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  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. ubl_state unified_bed_leveling::state, unified_bed_leveling::pre_initialized;
  54. float unified_bed_leveling::z_values[UBL_MESH_NUM_X_POINTS][UBL_MESH_NUM_Y_POINTS],
  55. unified_bed_leveling::last_specified_z,
  56. unified_bed_leveling::fade_scaling_factor_for_current_height,
  57. unified_bed_leveling::mesh_index_to_xpos[UBL_MESH_NUM_X_POINTS + 1], // +1 safety margin for now, until determinism prevails
  58. unified_bed_leveling::mesh_index_to_ypos[UBL_MESH_NUM_Y_POINTS + 1];
  59. bool unified_bed_leveling::g26_debug_flag = false,
  60. unified_bed_leveling::has_control_of_lcd_panel = false;
  61. int8_t unified_bed_leveling::eeprom_start = -1;
  62. volatile int unified_bed_leveling::encoder_diff;
  63. unified_bed_leveling::unified_bed_leveling() {
  64. for (uint8_t i = 0; i < COUNT(mesh_index_to_xpos); i++)
  65. mesh_index_to_xpos[i] = UBL_MESH_MIN_X + i * (MESH_X_DIST);
  66. for (uint8_t i = 0; i < COUNT(mesh_index_to_ypos); i++)
  67. mesh_index_to_ypos[i] = UBL_MESH_MIN_Y + i * (MESH_Y_DIST);
  68. reset();
  69. }
  70. void unified_bed_leveling::store_state() {
  71. const uint16_t i = UBL_LAST_EEPROM_INDEX;
  72. eeprom_write_block((void *)&ubl.state, (void *)i, sizeof(state));
  73. }
  74. void unified_bed_leveling::load_state() {
  75. const uint16_t i = UBL_LAST_EEPROM_INDEX;
  76. eeprom_read_block((void *)&ubl.state, (void *)i, sizeof(state));
  77. if (sanity_check())
  78. SERIAL_PROTOCOLLNPGM("?In load_state() sanity_check() failed.\n");
  79. #if ENABLED(ENABLE_LEVELING_FADE_HEIGHT)
  80. /**
  81. * These lines can go away in a few weeks. They are just
  82. * to make sure people updating their firmware won't be using
  83. * an incomplete Bed_Leveling.state structure. For speed
  84. * we now multiply by the inverse of the Fade Height instead of
  85. * dividing by it. Soon... all of the old structures will be
  86. * updated, but until then, we try to ease the transition
  87. * for our Beta testers.
  88. */
  89. if (ubl.state.g29_fade_height_multiplier != 1.0 / ubl.state.g29_correction_fade_height) {
  90. ubl.state.g29_fade_height_multiplier = 1.0 / ubl.state.g29_correction_fade_height;
  91. store_state();
  92. }
  93. #endif
  94. }
  95. void unified_bed_leveling::load_mesh(const int16_t m) {
  96. int16_t j = (UBL_LAST_EEPROM_INDEX - eeprom_start) / sizeof(z_values);
  97. if (m == -1) {
  98. SERIAL_PROTOCOLLNPGM("?No mesh saved in EEPROM. Zeroing mesh in memory.\n");
  99. reset();
  100. return;
  101. }
  102. if (!WITHIN(m, 0, j - 1) || eeprom_start <= 0) {
  103. SERIAL_PROTOCOLLNPGM("?EEPROM storage not available to load mesh.\n");
  104. return;
  105. }
  106. j = UBL_LAST_EEPROM_INDEX - (m + 1) * sizeof(z_values);
  107. eeprom_read_block((void *)&z_values, (void *)j, sizeof(z_values));
  108. SERIAL_PROTOCOLPAIR("Mesh loaded from slot ", m);
  109. SERIAL_PROTOCOLLNPAIR(" at offset 0x", hex_word(j));
  110. }
  111. void unified_bed_leveling::store_mesh(const int16_t m) {
  112. int16_t j = (UBL_LAST_EEPROM_INDEX - eeprom_start) / sizeof(z_values);
  113. if (!WITHIN(m, 0, j - 1) || eeprom_start <= 0) {
  114. SERIAL_PROTOCOLLNPGM("?EEPROM storage not available to load mesh.\n");
  115. SERIAL_PROTOCOL(m);
  116. SERIAL_PROTOCOLLNPGM(" mesh slots available.\n");
  117. SERIAL_PROTOCOLLNPAIR("E2END : ", E2END);
  118. SERIAL_PROTOCOLLNPAIR("k : ", (int)UBL_LAST_EEPROM_INDEX);
  119. SERIAL_PROTOCOLLNPAIR("j : ", j);
  120. SERIAL_PROTOCOLLNPAIR("m : ", m);
  121. SERIAL_EOL;
  122. return;
  123. }
  124. j = UBL_LAST_EEPROM_INDEX - (m + 1) * sizeof(z_values);
  125. eeprom_write_block((const void *)&z_values, (void *)j, sizeof(z_values));
  126. SERIAL_PROTOCOLPAIR("Mesh saved in slot ", m);
  127. SERIAL_PROTOCOLLNPAIR(" at offset 0x", hex_word(j));
  128. }
  129. void unified_bed_leveling::reset() {
  130. state.active = false;
  131. state.z_offset = 0;
  132. state.eeprom_storage_slot = -1;
  133. ZERO(z_values);
  134. last_specified_z = -999.9;
  135. fade_scaling_factor_for_current_height = 0.0;
  136. }
  137. void unified_bed_leveling::invalidate() {
  138. state.active = false;
  139. state.z_offset = 0;
  140. for (int x = 0; x < UBL_MESH_NUM_X_POINTS; x++)
  141. for (int y = 0; y < UBL_MESH_NUM_Y_POINTS; y++)
  142. z_values[x][y] = NAN;
  143. }
  144. void unified_bed_leveling::display_map(const int map_type) {
  145. const bool map0 = map_type == 0;
  146. if (map0) {
  147. SERIAL_PROTOCOLLNPGM("\nBed Topography Report:\n");
  148. serial_echo_xy(0, UBL_MESH_NUM_Y_POINTS - 1);
  149. SERIAL_ECHOPGM(" ");
  150. }
  151. if (map0) {
  152. serial_echo_10x_spaces();
  153. serial_echo_xy(UBL_MESH_NUM_X_POINTS - 1, UBL_MESH_NUM_Y_POINTS - 1);
  154. SERIAL_EOL;
  155. serial_echo_xy(UBL_MESH_MIN_X, UBL_MESH_MIN_Y);
  156. serial_echo_10x_spaces();
  157. serial_echo_xy(UBL_MESH_MAX_X, UBL_MESH_MAX_Y);
  158. SERIAL_EOL;
  159. }
  160. const float current_xi = ubl.get_cell_index_x(current_position[X_AXIS] + (MESH_X_DIST) / 2.0),
  161. current_yi = ubl.get_cell_index_y(current_position[Y_AXIS] + (MESH_Y_DIST) / 2.0);
  162. for (uint8_t j = UBL_MESH_NUM_Y_POINTS - 1; j >= 0; j--) {
  163. for (uint8_t i = 0; i < UBL_MESH_NUM_X_POINTS; i++) {
  164. const bool is_current = i == current_xi && j == current_yi;
  165. // is the nozzle here? then mark the number
  166. if (map0) SERIAL_CHAR(is_current ? '[' : ' ');
  167. const float f = z_values[i][j];
  168. if (isnan(f)) {
  169. serialprintPGM(map0 ? PSTR(" . ") : PSTR("NAN"));
  170. }
  171. else {
  172. // if we don't do this, the columns won't line up nicely
  173. if (map0 && f >= 0.0) SERIAL_CHAR(' ');
  174. SERIAL_PROTOCOL_F(f, 3);
  175. idle();
  176. }
  177. if (!map0 && i < UBL_MESH_NUM_X_POINTS - 1) SERIAL_CHAR(',');
  178. #if TX_BUFFER_SIZE > 0
  179. MYSERIAL.flushTX();
  180. #endif
  181. safe_delay(15);
  182. if (map0) {
  183. SERIAL_CHAR(is_current ? ']' : ' ');
  184. SERIAL_CHAR(' ');
  185. }
  186. }
  187. SERIAL_EOL;
  188. if (j && map0) { // we want the (0,0) up tight against the block of numbers
  189. SERIAL_CHAR(' ');
  190. SERIAL_EOL;
  191. }
  192. }
  193. if (map0) {
  194. serial_echo_xy(UBL_MESH_MIN_X, UBL_MESH_MIN_Y);
  195. SERIAL_ECHOPGM(" ");
  196. serial_echo_10x_spaces();
  197. serial_echo_xy(UBL_MESH_MAX_X, UBL_MESH_MIN_Y);
  198. SERIAL_EOL;
  199. serial_echo_xy(0, 0);
  200. SERIAL_ECHOPGM(" ");
  201. serial_echo_10x_spaces();
  202. serial_echo_xy(UBL_MESH_NUM_X_POINTS - 1, 0);
  203. SERIAL_EOL;
  204. }
  205. }
  206. bool unified_bed_leveling::sanity_check() {
  207. uint8_t error_flag = 0;
  208. if (state.n_x != UBL_MESH_NUM_X_POINTS) {
  209. SERIAL_PROTOCOLLNPGM("?UBL_MESH_NUM_X_POINTS set wrong\n");
  210. error_flag++;
  211. }
  212. if (state.n_y != UBL_MESH_NUM_Y_POINTS) {
  213. SERIAL_PROTOCOLLNPGM("?UBL_MESH_NUM_Y_POINTS set wrong\n");
  214. error_flag++;
  215. }
  216. if (state.mesh_x_min != UBL_MESH_MIN_X) {
  217. SERIAL_PROTOCOLLNPGM("?UBL_MESH_MIN_X set wrong\n");
  218. error_flag++;
  219. }
  220. if (state.mesh_y_min != UBL_MESH_MIN_Y) {
  221. SERIAL_PROTOCOLLNPGM("?UBL_MESH_MIN_Y set wrong\n");
  222. error_flag++;
  223. }
  224. if (state.mesh_x_max != UBL_MESH_MAX_X) {
  225. SERIAL_PROTOCOLLNPGM("?UBL_MESH_MAX_X set wrong\n");
  226. error_flag++;
  227. }
  228. if (state.mesh_y_max != UBL_MESH_MAX_Y) {
  229. SERIAL_PROTOCOLLNPGM("?UBL_MESH_MAX_Y set wrong\n");
  230. error_flag++;
  231. }
  232. if (state.mesh_x_dist != MESH_X_DIST) {
  233. SERIAL_PROTOCOLLNPGM("?MESH_X_DIST set wrong\n");
  234. error_flag++;
  235. }
  236. if (state.mesh_y_dist != MESH_Y_DIST) {
  237. SERIAL_PROTOCOLLNPGM("?MESH_Y_DIST set wrong\n");
  238. error_flag++;
  239. }
  240. const int j = (UBL_LAST_EEPROM_INDEX - eeprom_start) / sizeof(z_values);
  241. if (j < 1) {
  242. SERIAL_PROTOCOLLNPGM("?No EEPROM storage available for a mesh of this size.\n");
  243. error_flag++;
  244. }
  245. // SERIAL_PROTOCOLPGM("?sanity_check() return value: ");
  246. // SERIAL_PROTOCOL(error_flag);
  247. // SERIAL_EOL;
  248. return !!error_flag;
  249. }
  250. #endif // AUTO_BED_LEVELING_UBL