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.

UBL_Bed_Leveling.cpp 9.6KB

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