My Marlin configs for Fabrikator Mini and CTC i3 Pro B
Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

UBL_Bed_Leveling.cpp 11KB

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