Skip to content

Commit

Permalink
✨ Z Offset Wizard for TFT_LVGL_UI (English) (#23489)
Browse files Browse the repository at this point in the history
  • Loading branch information
protektwar authored Jan 16, 2022
1 parent c79174e commit cc87695
Show file tree
Hide file tree
Showing 11 changed files with 343 additions and 43 deletions.
55 changes: 27 additions & 28 deletions Marlin/Configuration_adv.h
Original file line number Diff line number Diff line change
Expand Up @@ -1280,37 +1280,36 @@
#define FEEDRATE_CHANGE_BEEP_FREQUENCY 440
#endif

#if HAS_BED_PROBE && EITHER(HAS_LCD_MENU, HAS_TFT_LVGL_UI)
//#define PROBE_OFFSET_WIZARD // Add a Probe Z Offset calibration option to the LCD menu
#if ENABLED(PROBE_OFFSET_WIZARD)
/**
* Enable to init the Probe Z-Offset when starting the Wizard.
* Use a height slightly above the estimated nozzle-to-probe Z offset.
* For example, with an offset of -5, consider a starting height of -4.
*/
//#define PROBE_OFFSET_WIZARD_START_Z -4.0

// Set a convenient position to do the calibration (probing point and nozzle/bed-distance)
//#define PROBE_OFFSET_WIZARD_XY_POS { X_CENTER, Y_CENTER }
#endif
#endif

#if HAS_LCD_MENU

// Add Probe Z Offset calibration to the Z Probe Offsets menu
#if HAS_BED_PROBE
//#define PROBE_OFFSET_WIZARD
#if ENABLED(PROBE_OFFSET_WIZARD)
//
// Enable to init the Probe Z-Offset when starting the Wizard.
// Use a height slightly above the estimated nozzle-to-probe Z offset.
// For example, with an offset of -5, consider a starting height of -4.
//
//#define PROBE_OFFSET_WIZARD_START_Z -4.0

// Set a convenient position to do the calibration (probing point and nozzle/bed-distance)
//#define PROBE_OFFSET_WIZARD_XY_POS { X_CENTER, Y_CENTER }
#endif

#if ENABLED(AUTO_BED_LEVELING_BILINEAR)
// Add a calibration procedure in the Probe Offsets menu
// to compensate for twist in the X-axis.
//#define X_AXIS_TWIST_COMPENSATION
#if ENABLED(X_AXIS_TWIST_COMPENSATION)
/**
* Enable to init the Probe Z-Offset when starting the Wizard.
* Use a height slightly above the estimated nozzle-to-probe Z offset.
* For example, with an offset of -5, consider a starting height of -4.
*/
#define XATC_START_Z 0.0
#define XATC_MAX_POINTS 3 // Number of points to probe in the wizard
#define XATC_Y_POSITION Y_CENTER // (mm) Y position to probe
#endif
#if BOTH(HAS_BED_PROBE, AUTO_BED_LEVELING_BILINEAR)
// Add calibration in the Probe Offsets menu to compensate for X-axis twist.
//#define X_AXIS_TWIST_COMPENSATION
#if ENABLED(X_AXIS_TWIST_COMPENSATION)
/**
* Enable to init the Probe Z-Offset when starting the Wizard.
* Use a height slightly above the estimated nozzle-to-probe Z offset.
* For example, with an offset of -5, consider a starting height of -4.
*/
#define XATC_START_Z 0.0
#define XATC_MAX_POINTS 3 // Number of points to probe in the wizard
#define XATC_Y_POSITION Y_CENTER // (mm) Y position to probe
#endif
#endif

Expand Down
11 changes: 10 additions & 1 deletion Marlin/src/lcd/extui/mks_ui/draw_level_settings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,8 @@ enum {
ID_LEVEL_RETURN = 1,
ID_LEVEL_POSITION,
ID_LEVEL_COMMAND,
ID_LEVEL_ZOFFSET
ID_LEVEL_ZOFFSET,
ID_Z_OFFSET_WIZARD
};

static void event_handler(lv_obj_t *obj, lv_event_t event) {
Expand All @@ -57,6 +58,11 @@ static void event_handler(lv_obj_t *obj, lv_event_t event) {
case ID_LEVEL_ZOFFSET:
lv_draw_auto_level_offset_settings();
break;
#if ENABLED(PROBE_OFFSET_WIZARD)
case ID_Z_OFFSET_WIZARD:
lv_draw_z_offset_wizard();
break;
#endif
#endif
}
}
Expand All @@ -67,6 +73,9 @@ void lv_draw_level_settings() {
lv_screen_menu_item(scr, machine_menu.LevelingAutoCommandConf, PARA_UI_POS_X, PARA_UI_POS_Y * 2, event_handler, ID_LEVEL_COMMAND, 1);
#if HAS_BED_PROBE
lv_screen_menu_item(scr, machine_menu.LevelingAutoZoffsetConf, PARA_UI_POS_X, PARA_UI_POS_Y * 3, event_handler, ID_LEVEL_ZOFFSET, 2);
#if ENABLED(PROBE_OFFSET_WIZARD)
lv_screen_menu_item(scr, machine_menu.LevelingZoffsetWizard, PARA_UI_POS_X, PARA_UI_POS_Y * 4, event_handler, ID_Z_OFFSET_WIZARD, 3);
#endif
#endif
lv_big_button_create(scr, "F:/bmp_back70x40.bin", common_menu.text_back, PARA_UI_BACK_POS_X + 10, PARA_UI_BACK_POS_Y, event_handler, ID_LEVEL_RETURN, true);
}
Expand Down
14 changes: 14 additions & 0 deletions Marlin/src/lcd/extui/mks_ui/draw_ui.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -456,6 +456,10 @@ char *getDispText(int index) {
}
break;
case MOVE_MOTOR_UI: strcpy(public_buf_l, move_menu.title); break;

#if ENABLED(PROBE_OFFSET_WIZARD)
case Z_OFFSET_WIZARD_UI: break;
#endif
case OPERATE_UI:
switch (disp_state_stack._disp_state[disp_state_stack._disp_index]) {
IF_DISABLED(TFT35, case OPERATE_UI: case PAUSE_UI:)
Expand Down Expand Up @@ -785,6 +789,10 @@ void GUI_RefreshPage() {

case MOVE_MOTOR_UI: break;

#if ENABLED(PROBE_OFFSET_WIZARD)
case Z_OFFSET_WIZARD_UI: break;
#endif

#if ENABLED(MKS_WIFI_MODULE)
case WIFI_UI:
if (temps_update_flag) {
Expand Down Expand Up @@ -885,6 +893,9 @@ void clear_cur_ui() {
case PRINT_FILE_UI: lv_clear_print_file(); break;
case PRINTING_UI: lv_clear_printing(); break;
case MOVE_MOTOR_UI: lv_clear_move_motor(); break;
#if ENABLED(PROBE_OFFSET_WIZARD)
case Z_OFFSET_WIZARD_UI: lv_clear_z_offset_wizard(); break;
#endif
case OPERATE_UI: lv_clear_operation(); break;
case PAUSE_UI: break;
case EXTRUSION_UI: lv_clear_extrusion(); break;
Expand Down Expand Up @@ -993,6 +1004,9 @@ void draw_return_ui() {
break;

case MOVE_MOTOR_UI: lv_draw_move_motor(); break;
#if ENABLED(PROBE_OFFSET_WIZARD)
case Z_OFFSET_WIZARD_UI: lv_draw_z_offset_wizard(); break;
#endif
case OPERATE_UI: lv_draw_operation(); break;
case PAUSE_UI: break;
case EXTRUSION_UI: lv_draw_extrusion(); break;
Expand Down
2 changes: 2 additions & 0 deletions Marlin/src/lcd/extui/mks_ui/draw_ui.h
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@
#include "draw_max_feedrate_settings.h"
#include "draw_tmc_step_mode_settings.h"
#include "draw_level_settings.h"
#include "draw_z_offset_wizard.h"
#include "draw_tramming_pos_settings.h"
#include "draw_auto_level_offset_settings.h"
#include "draw_filament_change.h"
Expand Down Expand Up @@ -266,6 +267,7 @@ typedef enum {
PRINT_FILE_UI,
PRINTING_UI,
MOVE_MOTOR_UI,
Z_OFFSET_WIZARD_UI,
OPERATE_UI,
PAUSE_UI,
EXTRUSION_UI,
Expand Down
228 changes: 228 additions & 0 deletions Marlin/src/lcd/extui/mks_ui/draw_z_offset_wizard.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,228 @@
/**
* Marlin 3D Printer Firmware
* Copyright (c) 2022 MarlinFirmware [https://github.com/MarlinFirmware/Marlin]
*
* Based on Sprinter and grbl.
* Copyright (c) 2011 Camiel Gubbels / Erik van der Zalm
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*
*/

#include "../../../inc/MarlinConfigPre.h"

#if BOTH(HAS_TFT_LVGL_UI, PROBE_OFFSET_WIZARD)

#include "draw_ui.h"
#include <lv_conf.h>

#include "../../../gcode/queue.h"
#include "../../../module/motion.h"

#include "../../../module/planner.h"
#include "../../../inc/MarlinConfig.h"
#include "../../../module/probe.h"

#if HAS_LEVELING
#include "../../../feature/bedlevel/bedlevel.h"
bool leveling_was_active;
#endif

extern lv_group_t *g;
static lv_obj_t *scr;

static lv_obj_t *labelV, *buttonV, *labelP;
static lv_obj_t *labelP_z_offset_ref;
static lv_task_t *updatePosTask;
static char cur_label = 'Z';
static float cur_pos = 0;
static float cur_OffsetPos = 0;

// Global storage
float z_offset_backup, calculated_z_offset, z_offset_ref;

enum {
ID_M_Z_P = 1,
ID_M_Z_N,
ID_M_STEP,
ID_M_SAVE,
ID_M_RETURN
};

void disp_cur_wizard_pos() {
char str_1[18];
sprintf_P(public_buf_l, PSTR("%c:%s mm"), cur_label, dtostrf(cur_pos, 1, 3, str_1));
if (labelP) lv_label_set_text(labelP, public_buf_l);

sprintf_P(public_buf_l, PSTR("%c Offset:%s mm"), cur_label, dtostrf(cur_OffsetPos, 1, 3, str_1));
if (labelP_z_offset_ref) lv_label_set_text(labelP_z_offset_ref, public_buf_l);
}

static void event_handler(lv_obj_t *obj, lv_event_t event) {
char str_1[16];
if (event != LV_EVENT_RELEASED) return;
//lv_clear_z_offset_wizard();
if (!queue.ring_buffer.full(3)) {
bool do_inject = true;
float dist = uiCfg.move_dist;
switch (obj->mks_obj_id) {
case ID_M_Z_N: dist *= -1; case ID_M_Z_P: cur_label = 'Z'; break;
default: do_inject = false;
}
if (do_inject) {
sprintf_P(public_buf_l, PSTR("G91\nG1 %c%s F%d\nG90"), cur_label, dtostrf(dist, 1, 3, str_1), uiCfg.moveSpeed);
queue.inject(public_buf_l);
//calculated_z_offset = probe.offset.z + current_position.z - z_offset_ref;
disp_cur_wizard_pos();
}
}

switch (obj->mks_obj_id) {
case ID_M_STEP:
if (ABS((int)(10 * uiCfg.move_dist)) == 10)
uiCfg.move_dist = 0.025;
else if (ABS((int)(1000 * uiCfg.move_dist)) == 25)
uiCfg.move_dist = 0.1;
else
uiCfg.move_dist *= 10.0f;
disp_move_wizard_dist();
break;
case ID_M_SAVE:
current_position.z = z_offset_ref; // Set Z to z_offset_ref, as we can expect it is at probe height
probe.offset.z = calculated_z_offset;
sync_plan_position();
// Raise Z as if it was homed
do_z_clearance(Z_POST_CLEARANCE);
watchdog_refresh();
draw_return_ui();
return;
case ID_M_RETURN:
probe.offset.z = z_offset_backup;
SET_SOFT_ENDSTOP_LOOSE(false);
TERN_(HAS_LEVELING, set_bed_leveling_enabled(leveling_was_active));
#if HOMING_Z_WITH_PROBE && defined(PROBE_OFFSET_WIZARD_START_Z)
set_axis_never_homed(Z_AXIS); // On cancel the Z position needs correction
queue.inject_P(PSTR("G28Z"));
#else // Otherwise do a Z clearance move like after Homing
do_z_clearance(Z_POST_CLEARANCE);
#endif
watchdog_refresh();
draw_return_ui();
return;
}
disp_cur_wizard_pos();
}

void refresh_wizard_pos(lv_task_t *) {
switch (cur_label) {
case 'Z':
cur_pos = current_position.z;
calculated_z_offset = probe.offset.z + current_position.z - z_offset_ref;
cur_OffsetPos = calculated_z_offset;
break;
default: return;
}
disp_cur_wizard_pos();
}

void lv_draw_z_offset_wizard() {

set_all_unhomed();

// Store probe.offset.z for Case: Cancel
z_offset_backup = probe.offset.z;

#ifdef PROBE_OFFSET_WIZARD_START_Z
probe.offset.z = PROBE_OFFSET_WIZARD_START_Z;
#endif

// Store Bed-Leveling-State and disable
#if HAS_LEVELING
leveling_was_active = planner.leveling_active;
set_bed_leveling_enabled(leveling_was_active);
#endif

queue.inject_P(PSTR("G28"));

z_offset_ref = 0; // Set Z Value for Wizard Position to 0
calculated_z_offset = probe.offset.z + current_position.z - z_offset_ref;
cur_OffsetPos = calculated_z_offset;

scr = lv_screen_create(Z_OFFSET_WIZARD_UI, machine_menu.LevelingZoffsetTitle);

lv_obj_t *buttonXI = lv_big_button_create(scr, "F:/bmp_zAdd.bin", move_menu.z_add, INTERVAL_V, titleHeight, event_handler, ID_M_Z_P);
lv_obj_clear_protect(buttonXI, LV_PROTECT_FOLLOW);
lv_big_button_create(scr, "F:/bmp_zDec.bin", move_menu.z_dec, INTERVAL_V * 3, BTN_Y_PIXEL + INTERVAL_H + titleHeight, event_handler, ID_M_Z_N);

// button with image and label changed dynamically by disp_move_dist
buttonV = lv_imgbtn_create(scr, nullptr, BTN_X_PIXEL * 3 + INTERVAL_V * 4, titleHeight, event_handler, ID_M_STEP);
labelV = lv_label_create_empty(buttonV);
#if HAS_ROTARY_ENCODER
if (gCfgItems.encoder_enable) lv_group_add_obj(g, buttonV);
#endif

// save and back
lv_big_button_create(scr, "F:/bmp_return.bin", common_menu.text_save, BTN_X_PIXEL * 2 + INTERVAL_V * 3, BTN_Y_PIXEL + INTERVAL_H + titleHeight, event_handler, ID_M_SAVE);
// cancel and back
lv_big_button_create(scr, "F:/bmp_return.bin", common_menu.text_back, BTN_X_PIXEL * 3 + INTERVAL_V * 4, BTN_Y_PIXEL + INTERVAL_H + titleHeight, event_handler, ID_M_RETURN);

// We need to patch the title to leave some space on the right for displaying the status
lv_obj_t * z_offset_ref_title = lv_obj_get_child_back(scr, nullptr);
if (z_offset_ref_title != nullptr) lv_obj_set_width(z_offset_ref_title, (int)(TFT_WIDTH / 2) - 101);
labelP_z_offset_ref = lv_label_create(scr, (int)(TFT_WIDTH / 2) - 80, (int)(TFT_HEIGHT/2)-20, "Z Offset:0.0mm");

// We need to patch the Z Offset to leave some space in the middle for displaying the status
lv_obj_t * title= lv_obj_get_child_back(scr, nullptr);
if (title != nullptr) lv_obj_set_width(title, TFT_WIDTH - 101);
labelP = lv_label_create(scr, TFT_WIDTH - 100, TITLE_YPOS, "Z:0.0mm");

if (labelP != nullptr)
updatePosTask = lv_task_create(refresh_wizard_pos, 300, LV_TASK_PRIO_LOWEST, 0);

disp_move_wizard_dist();
disp_cur_wizard_pos();
}

void disp_move_wizard_dist() {
if ((int)(1000 * uiCfg.move_dist) == 25)
lv_imgbtn_set_src_both(buttonV, "F:/bmp_step_move0_1.bin");
else if ((int)(10 * uiCfg.move_dist) == 1)
lv_imgbtn_set_src_both(buttonV, "F:/bmp_step_move1.bin");
else if ((int)(10 * uiCfg.move_dist) == 10)
lv_imgbtn_set_src_both(buttonV, "F:/bmp_step_move10.bin");

if (gCfgItems.multiple_language) {
if ((int)(1000 * uiCfg.move_dist) == 25) {
lv_label_set_text(labelV, move_menu.step_0025mm);
lv_obj_align(labelV, buttonV, LV_ALIGN_IN_BOTTOM_MID, 0, BUTTON_TEXT_Y_OFFSET);
}
else if ((int)(10 * uiCfg.move_dist) == 1) {
lv_label_set_text(labelV, move_menu.step_01mm);
lv_obj_align(labelV, buttonV, LV_ALIGN_IN_BOTTOM_MID, 0, BUTTON_TEXT_Y_OFFSET);
}
else if ((int)(10 * uiCfg.move_dist) == 10) {
lv_label_set_text(labelV, move_menu.step_1mm);
lv_obj_align(labelV, buttonV, LV_ALIGN_IN_BOTTOM_MID, 0, BUTTON_TEXT_Y_OFFSET);
}
}
}

void lv_clear_z_offset_wizard() {
#if HAS_ROTARY_ENCODER
if (gCfgItems.encoder_enable) lv_group_remove_all_objs(g);
#endif
lv_obj_del(scr);
}

#endif // HAS_TFT_LVGL_UI && PROBE_OFFSET_WIZARD
Loading

0 comments on commit cc87695

Please sign in to comment.