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 9.8KB

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