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_tools.cpp 10KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  1. /**
  2. * Marlin 3D Printer Firmware
  3. * Copyright (c) 2022 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 <https://www.gnu.org/licenses/>.
  20. *
  21. */
  22. /**
  23. * UBL Tools and Mesh Viewer for Pro UI
  24. * Version: 1.0.0
  25. * Date: 2022/04/13
  26. *
  27. * Original Author: Henri-J-Norden
  28. * Original Source: https://github.com/Jyers/Marlin/pull/126
  29. */
  30. #include "../../../inc/MarlinConfigPre.h"
  31. #if BOTH(DWIN_LCD_PROUI, AUTO_BED_LEVELING_UBL)
  32. #include "ubl_tools.h"
  33. #include "../../marlinui.h"
  34. #include "../../../core/types.h"
  35. #include "dwin.h"
  36. #include "dwinui.h"
  37. #include "dwin_popup.h"
  38. #include "../../../feature/bedlevel/bedlevel.h"
  39. #include "../../../module/probe.h"
  40. #include "../../../gcode/gcode.h"
  41. #include "../../../module/planner.h"
  42. #include "../../../gcode/queue.h"
  43. #include "../../../libs/least_squares_fit.h"
  44. #include "../../../libs/vector_3.h"
  45. UBLMeshToolsClass ubl_tools;
  46. #if ENABLED(USE_UBL_VIEWER)
  47. bool UBLMeshToolsClass::viewer_asymmetric_range = false;
  48. bool UBLMeshToolsClass::viewer_print_value = false;
  49. #endif
  50. bool UBLMeshToolsClass::goto_mesh_value = false;
  51. uint8_t UBLMeshToolsClass::tilt_grid = 1;
  52. bool drawing_mesh = false;
  53. char cmd[MAX_CMD_SIZE+16], str_1[16], str_2[16], str_3[16];
  54. #if ENABLED(AUTO_BED_LEVELING_UBL)
  55. void UBLMeshToolsClass::manual_value_update(const uint8_t mesh_x, const uint8_t mesh_y, bool undefined/*=false*/) {
  56. sprintf_P(cmd, PSTR("M421 I%i J%i Z%s %s"), mesh_x, mesh_y, dtostrf(current_position.z, 1, 3, str_1), undefined ? "N" : "");
  57. gcode.process_subcommands_now(cmd);
  58. planner.synchronize();
  59. }
  60. bool UBLMeshToolsClass::create_plane_from_mesh() {
  61. struct linear_fit_data lsf_results;
  62. incremental_LSF_reset(&lsf_results);
  63. GRID_LOOP(x, y) {
  64. if (!isnan(bedlevel.z_values[x][y])) {
  65. xy_pos_t rpos = { bedlevel.get_mesh_x(x), bedlevel.get_mesh_y(y) };
  66. incremental_LSF(&lsf_results, rpos, bedlevel.z_values[x][y]);
  67. }
  68. }
  69. if (finish_incremental_LSF(&lsf_results)) {
  70. SERIAL_ECHOPGM("Could not complete LSF!");
  71. return true;
  72. }
  73. bedlevel.set_all_mesh_points_to_value(0);
  74. matrix_3x3 rotation = matrix_3x3::create_look_at(vector_3(lsf_results.A, lsf_results.B, 1));
  75. GRID_LOOP(i, j) {
  76. float mx = bedlevel.get_mesh_x(i),
  77. my = bedlevel.get_mesh_y(j),
  78. mz = bedlevel.z_values[i][j];
  79. if (DEBUGGING(LEVELING)) {
  80. DEBUG_ECHOPAIR_F("before rotation = [", mx, 7);
  81. DEBUG_CHAR(',');
  82. DEBUG_ECHO_F(my, 7);
  83. DEBUG_CHAR(',');
  84. DEBUG_ECHO_F(mz, 7);
  85. DEBUG_ECHOPGM("] ---> ");
  86. DEBUG_DELAY(20);
  87. }
  88. rotation.apply_rotation_xyz(mx, my, mz);
  89. if (DEBUGGING(LEVELING)) {
  90. DEBUG_ECHOPAIR_F("after rotation = [", mx, 7);
  91. DEBUG_CHAR(',');
  92. DEBUG_ECHO_F(my, 7);
  93. DEBUG_CHAR(',');
  94. DEBUG_ECHO_F(mz, 7);
  95. DEBUG_ECHOLNPGM("]");
  96. DEBUG_DELAY(20);
  97. }
  98. bedlevel.z_values[i][j] = mz - lsf_results.D;
  99. }
  100. return false;
  101. }
  102. #else
  103. void UBLMeshToolsClass::manual_value_update(const uint8_t mesh_x, const uint8_t mesh_y) {
  104. sprintf_P(cmd, PSTR("G29 I%i J%i Z%s"), mesh_x, mesh_y, dtostrf(current_position.z, 1, 3, str_1));
  105. gcode.process_subcommands_now(cmd);
  106. planner.synchronize();
  107. }
  108. #endif
  109. void UBLMeshToolsClass::manual_move(const uint8_t mesh_x, const uint8_t mesh_y, bool zmove/*=false*/) {
  110. if (zmove) {
  111. planner.synchronize();
  112. current_position.z = goto_mesh_value ? bedlevel.z_values[mesh_x][mesh_y] : Z_CLEARANCE_BETWEEN_PROBES;
  113. planner.buffer_line(current_position, homing_feedrate(Z_AXIS), active_extruder);
  114. planner.synchronize();
  115. }
  116. else {
  117. DWIN_Show_Popup(ICON_BLTouch, F("Moving to Point"), F("Please wait until done."));
  118. HMI_SaveProcessID(NothingToDo);
  119. sprintf_P(cmd, PSTR("G0 F300 Z%s"), dtostrf(Z_CLEARANCE_BETWEEN_PROBES, 1, 3, str_1));
  120. gcode.process_subcommands_now(cmd);
  121. sprintf_P(cmd, PSTR("G42 F4000 I%i J%i"), mesh_x, mesh_y);
  122. gcode.process_subcommands_now(cmd);
  123. planner.synchronize();
  124. current_position.z = goto_mesh_value ? bedlevel.z_values[mesh_x][mesh_y] : Z_CLEARANCE_BETWEEN_PROBES;
  125. planner.buffer_line(current_position, homing_feedrate(Z_AXIS), active_extruder);
  126. planner.synchronize();
  127. HMI_ReturnScreen();
  128. }
  129. }
  130. float UBLMeshToolsClass::get_max_value() {
  131. float max = __FLT_MIN__;
  132. GRID_LOOP(x, y) {
  133. if (!isnan(bedlevel.z_values[x][y]) && bedlevel.z_values[x][y] > max)
  134. max = bedlevel.z_values[x][y];
  135. }
  136. return max;
  137. }
  138. float UBLMeshToolsClass::get_min_value() {
  139. float min = __FLT_MAX__;
  140. GRID_LOOP(x, y) {
  141. if (!isnan(bedlevel.z_values[x][y]) && bedlevel.z_values[x][y] < min)
  142. min = bedlevel.z_values[x][y];
  143. }
  144. return min;
  145. }
  146. bool UBLMeshToolsClass::validate() {
  147. float min = __FLT_MAX__, max = __FLT_MIN__;
  148. GRID_LOOP(x, y) {
  149. if (isnan(bedlevel.z_values[x][y])) return false;
  150. if (bedlevel.z_values[x][y] < min) min = bedlevel.z_values[x][y];
  151. if (bedlevel.z_values[x][y] > max) max = bedlevel.z_values[x][y];
  152. }
  153. return max <= UBL_Z_OFFSET_MAX && min >= UBL_Z_OFFSET_MIN;
  154. }
  155. #if ENABLED(USE_UBL_VIEWER)
  156. void UBLMeshToolsClass::Draw_Bed_Mesh(int16_t selected /*= -1*/, uint8_t gridline_width /*= 1*/, uint16_t padding_x /*= 8*/, uint16_t padding_y_top /*= 40 + 53 - 7*/) {
  157. drawing_mesh = true;
  158. const uint16_t total_width_px = DWIN_WIDTH - padding_x - padding_x;
  159. const uint16_t cell_width_px = total_width_px / (GRID_MAX_POINTS_X);
  160. const uint16_t cell_height_px = total_width_px / (GRID_MAX_POINTS_Y);
  161. const float v_max = abs(get_max_value()), v_min = abs(get_min_value()), range = _MAX(v_min, v_max);
  162. // Clear background from previous selection and select new square
  163. DWIN_Draw_Rectangle(1, Color_Bg_Black, _MAX(0, padding_x - gridline_width), _MAX(0, padding_y_top - gridline_width), padding_x + total_width_px, padding_y_top + total_width_px);
  164. if (selected >= 0) {
  165. const auto selected_y = selected / (GRID_MAX_POINTS_X);
  166. const auto selected_x = selected - (GRID_MAX_POINTS_X) * selected_y;
  167. const auto start_y_px = padding_y_top + selected_y * cell_height_px;
  168. const auto start_x_px = padding_x + selected_x * cell_width_px;
  169. DWIN_Draw_Rectangle(1, Color_White, _MAX(0, start_x_px - gridline_width), _MAX(0, start_y_px - gridline_width), start_x_px + cell_width_px, start_y_px + cell_height_px);
  170. }
  171. // Draw value square grid
  172. char buf[8];
  173. GRID_LOOP(x, y) {
  174. const auto start_x_px = padding_x + x * cell_width_px;
  175. const auto end_x_px = start_x_px + cell_width_px - 1 - gridline_width;
  176. const auto start_y_px = padding_y_top + ((GRID_MAX_POINTS_Y) - y - 1) * cell_height_px;
  177. const auto end_y_px = start_y_px + cell_height_px - 1 - gridline_width;
  178. DWIN_Draw_Rectangle(1, // RGB565 colors: http://www.barth-dev.de/online/rgb565-color-picker/
  179. isnan(bedlevel.z_values[x][y]) ? Color_Grey : ( // gray if undefined
  180. (bedlevel.z_values[x][y] < 0 ?
  181. (uint16_t)round(0x1F * -bedlevel.z_values[x][y] / (!viewer_asymmetric_range ? range : v_min)) << 11 : // red if mesh point value is negative
  182. (uint16_t)round(0x3F * bedlevel.z_values[x][y] / (!viewer_asymmetric_range ? range : v_max)) << 5) | // green if mesh point value is positive
  183. _MIN(0x1F, (((uint8_t)abs(bedlevel.z_values[x][y]) / 10) * 4))), // + blue stepping for every mm
  184. start_x_px, start_y_px, end_x_px, end_y_px
  185. );
  186. safe_delay(10);
  187. LCD_SERIAL.flushTX();
  188. // Draw value text on
  189. if (viewer_print_value) {
  190. int8_t offset_x, offset_y = cell_height_px / 2 - 6;
  191. if (isnan(bedlevel.z_values[x][y])) { // undefined
  192. DWIN_Draw_String(false, font6x12, Color_White, Color_Bg_Blue, start_x_px + cell_width_px / 2 - 5, start_y_px + offset_y, F("X"));
  193. }
  194. else { // has value
  195. if (GRID_MAX_POINTS_X < 10)
  196. sprintf_P(buf, PSTR("%s"), dtostrf(abs(bedlevel.z_values[x][y]), 1, 2, str_1));
  197. else
  198. sprintf_P(buf, PSTR("%02i"), (uint16_t)(abs(bedlevel.z_values[x][y] - (int16_t)bedlevel.z_values[x][y]) * 100));
  199. offset_x = cell_width_px / 2 - 3 * (strlen(buf)) - 2;
  200. if (!(GRID_MAX_POINTS_X < 10))
  201. DWIN_Draw_String(false, font6x12, Color_White, Color_Bg_Blue, start_x_px - 2 + offset_x, start_y_px + offset_y /*+ square / 2 - 6*/, F("."));
  202. DWIN_Draw_String(false, font6x12, Color_White, Color_Bg_Blue, start_x_px + 1 + offset_x, start_y_px + offset_y /*+ square / 2 - 6*/, buf);
  203. }
  204. safe_delay(10);
  205. LCD_SERIAL.flushTX();
  206. }
  207. }
  208. }
  209. void UBLMeshToolsClass::Set_Mesh_Viewer_Status() { // TODO: draw gradient with values as a legend instead
  210. float v_max = abs(get_max_value()), v_min = abs(get_min_value()), range = _MAX(v_min, v_max);
  211. if (v_min > 3e+10F) v_min = 0.0000001;
  212. if (v_max > 3e+10F) v_max = 0.0000001;
  213. if (range > 3e+10F) range = 0.0000001;
  214. char msg[46];
  215. if (viewer_asymmetric_range) {
  216. dtostrf(-v_min, 1, 3, str_1);
  217. dtostrf( v_max, 1, 3, str_2);
  218. }
  219. else {
  220. dtostrf(-range, 1, 3, str_1);
  221. dtostrf( range, 1, 3, str_2);
  222. }
  223. sprintf_P(msg, PSTR("Red %s..0..%s Green"), str_1, str_2);
  224. ui.set_status(msg);
  225. drawing_mesh = false;
  226. }
  227. #endif
  228. #endif // DWIN_LCD_PROUI && AUTO_BED_LEVELING_UBL