My Marlin configs for Fabrikator Mini and CTC i3 Pro B
Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

ubl.cpp 7.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  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. #include "temperature.h"
  28. /**
  29. * These support functions allow the use of large bit arrays of flags that take very
  30. * little RAM. Currently they are limited to being 16x16 in size. Changing the declaration
  31. * to unsigned long will allow us to go to 32x32 if higher resolution Mesh's are needed
  32. * in the future.
  33. */
  34. void bit_clear(uint16_t bits[16], uint8_t x, uint8_t y) { CBI(bits[y], x); }
  35. void bit_set(uint16_t bits[16], uint8_t x, uint8_t y) { SBI(bits[y], x); }
  36. bool is_bit_set(uint16_t bits[16], uint8_t x, uint8_t y) { return TEST(bits[y], x); }
  37. uint8_t ubl_cnt = 0;
  38. static void serial_echo_xy(const uint16_t x, const uint16_t y) {
  39. SERIAL_CHAR('(');
  40. SERIAL_ECHO(x);
  41. SERIAL_CHAR(',');
  42. SERIAL_ECHO(y);
  43. SERIAL_CHAR(')');
  44. safe_delay(10);
  45. }
  46. static void serial_echo_mspaces(const uint8_t cnt) {
  47. for (uint8_t i = GRID_MAX_POINTS_X - 1; --i;) {
  48. SERIAL_ECHO_SP((uint8_t)cnt);
  49. safe_delay(10);
  50. }
  51. }
  52. ubl_state unified_bed_leveling::state;
  53. float unified_bed_leveling::z_values[GRID_MAX_POINTS_X][GRID_MAX_POINTS_Y],
  54. unified_bed_leveling::last_specified_z;
  55. // 15 is the maximum nubmer of grid points supported + 1 safety margin for now,
  56. // until determinism prevails
  57. constexpr float unified_bed_leveling::mesh_index_to_xpos[16],
  58. unified_bed_leveling::mesh_index_to_ypos[16];
  59. bool unified_bed_leveling::g26_debug_flag = false,
  60. unified_bed_leveling::has_control_of_lcd_panel = false;
  61. int16_t unified_bed_leveling::eeprom_start = -1; // Please stop changing this to 8 bits in size
  62. // It needs to hold values bigger than this.
  63. volatile int unified_bed_leveling::encoder_diff;
  64. unified_bed_leveling::unified_bed_leveling() {
  65. ubl_cnt++; // Debug counter to insure we only have one UBL object present in memory.
  66. reset();
  67. }
  68. void unified_bed_leveling::load_mesh(const int16_t slot) {
  69. int16_t j = (UBL_LAST_EEPROM_INDEX - eeprom_start) / sizeof(z_values);
  70. if (slot == -1) {
  71. SERIAL_PROTOCOLLNPGM("?No mesh saved in EEPROM. Zeroing mesh in memory.\n");
  72. reset();
  73. return;
  74. }
  75. if (!WITHIN(slot, 0, j - 1) || eeprom_start <= 0) {
  76. SERIAL_PROTOCOLLNPGM("?EEPROM storage not available to load mesh.\n");
  77. return;
  78. }
  79. j = UBL_LAST_EEPROM_INDEX - (slot + 1) * sizeof(z_values);
  80. eeprom_read_block((void *)&z_values, (void *)j, sizeof(z_values));
  81. SERIAL_PROTOCOLPAIR("Mesh loaded from slot ", slot);
  82. SERIAL_PROTOCOLLNPAIR(" at offset ", hex_address((void*)j));
  83. }
  84. void unified_bed_leveling::store_mesh(const int16_t slot) {
  85. int16_t j = (UBL_LAST_EEPROM_INDEX - eeprom_start) / sizeof(z_values);
  86. if (!WITHIN(slot, 0, j - 1) || eeprom_start <= 0) {
  87. SERIAL_PROTOCOLLNPGM("?EEPROM storage not available to load mesh.\n");
  88. SERIAL_PROTOCOL(slot);
  89. SERIAL_PROTOCOLLNPGM(" mesh slots available.\n");
  90. SERIAL_PROTOCOLLNPAIR("E2END : ", E2END);
  91. SERIAL_PROTOCOLLNPAIR("k : ", (int)UBL_LAST_EEPROM_INDEX);
  92. SERIAL_PROTOCOLLNPAIR("j : ", j);
  93. SERIAL_PROTOCOLLNPAIR("m : ", slot);
  94. SERIAL_EOL;
  95. return;
  96. }
  97. j = UBL_LAST_EEPROM_INDEX - (slot + 1) * sizeof(z_values);
  98. eeprom_write_block((const void *)&z_values, (void *)j, sizeof(z_values));
  99. SERIAL_PROTOCOLPAIR("Mesh saved in slot ", slot);
  100. SERIAL_PROTOCOLLNPAIR(" at offset ", hex_address((void*)j));
  101. }
  102. void unified_bed_leveling::reset() {
  103. state.active = false;
  104. state.z_offset = 0;
  105. state.eeprom_storage_slot = -1;
  106. ZERO(z_values);
  107. last_specified_z = -999.9;
  108. }
  109. void unified_bed_leveling::invalidate() {
  110. state.active = false;
  111. state.z_offset = 0;
  112. for (int x = 0; x < GRID_MAX_POINTS_X; x++)
  113. for (int y = 0; y < GRID_MAX_POINTS_Y; y++)
  114. z_values[x][y] = NAN;
  115. }
  116. void unified_bed_leveling::display_map(const int map_type) {
  117. const bool map0 = map_type == 0;
  118. const uint8_t spaces = 9;
  119. if (map0) {
  120. SERIAL_PROTOCOLLNPGM("\nBed Topography Report:\n");
  121. serial_echo_xy(0, GRID_MAX_POINTS_Y - 1);
  122. SERIAL_ECHO_SP(3);
  123. serial_echo_mspaces(spaces);
  124. serial_echo_xy(GRID_MAX_POINTS_X - 1, GRID_MAX_POINTS_Y - 1);
  125. SERIAL_EOL;
  126. serial_echo_xy(UBL_MESH_MIN_X, UBL_MESH_MAX_Y);
  127. serial_echo_mspaces(spaces);
  128. serial_echo_xy(UBL_MESH_MAX_X, UBL_MESH_MAX_Y);
  129. SERIAL_EOL;
  130. }
  131. const float current_xi = ubl.get_cell_index_x(current_position[X_AXIS] + (MESH_X_DIST) / 2.0),
  132. current_yi = ubl.get_cell_index_y(current_position[Y_AXIS] + (MESH_Y_DIST) / 2.0);
  133. for (int8_t j = GRID_MAX_POINTS_Y - 1; j >= 0; j--) {
  134. for (uint8_t i = 0; i < GRID_MAX_POINTS_X; i++) {
  135. const bool is_current = i == current_xi && j == current_yi;
  136. // is the nozzle here? then mark the number
  137. if (map0) SERIAL_CHAR(is_current ? '[' : ' ');
  138. const float f = z_values[i][j];
  139. if (isnan(f)) {
  140. serialprintPGM(map0 ? PSTR(" . ") : PSTR("NAN"));
  141. }
  142. else {
  143. // if we don't do this, the columns won't line up nicely
  144. if (map0 && f >= 0.0) SERIAL_CHAR(' ');
  145. SERIAL_PROTOCOL_F(f, 3);
  146. idle();
  147. }
  148. if (!map0 && i < GRID_MAX_POINTS_X - 1) SERIAL_CHAR(',');
  149. #if TX_BUFFER_SIZE > 0
  150. MYSERIAL.flushTX();
  151. #endif
  152. safe_delay(15);
  153. if (map0) {
  154. SERIAL_CHAR(is_current ? ']' : ' ');
  155. SERIAL_CHAR(' ');
  156. }
  157. }
  158. SERIAL_EOL;
  159. if (j && map0) { // we want the (0,0) up tight against the block of numbers
  160. SERIAL_CHAR(' ');
  161. SERIAL_EOL;
  162. }
  163. }
  164. if (map0) {
  165. serial_echo_xy(UBL_MESH_MIN_X, UBL_MESH_MIN_Y);
  166. SERIAL_ECHO_SP(4);
  167. serial_echo_mspaces(spaces);
  168. serial_echo_xy(UBL_MESH_MAX_X, UBL_MESH_MIN_Y);
  169. SERIAL_EOL;
  170. serial_echo_xy(0, 0);
  171. SERIAL_ECHO_SP(5);
  172. serial_echo_mspaces(spaces);
  173. serial_echo_xy(GRID_MAX_POINTS_X - 1, 0);
  174. SERIAL_EOL;
  175. }
  176. }
  177. bool unified_bed_leveling::sanity_check() {
  178. uint8_t error_flag = 0;
  179. const int j = (UBL_LAST_EEPROM_INDEX - eeprom_start) / sizeof(z_values);
  180. if (j < 1) {
  181. SERIAL_PROTOCOLLNPGM("?No EEPROM storage available for a mesh of this size.\n");
  182. error_flag++;
  183. }
  184. return !!error_flag;
  185. }
  186. #endif // AUTO_BED_LEVELING_UBL