UBL's Grid Based Leveling code

Pretty much...  The code is in place.  Still more work to do.    But it
has a lot of hooks and variables in other code, so commit and merge
before I pick up a million 'Conflicts'.
This commit is contained in:
Roxy-3D
2017-04-06 18:46:47 -05:00
committed by Roxy-3D
parent 549055f20c
commit 1451b7eacf
8 changed files with 501 additions and 67 deletions

View File

@ -40,6 +40,7 @@
bool lcd_clicked();
void lcd_implementation_clear();
void lcd_mesh_edit_setup(float initial);
void tilt_mesh_based_on_probed_grid( const bool );
float lcd_mesh_edit();
void lcd_z_offset_edit_setup(float);
float lcd_z_offset_edit();
@ -50,8 +51,7 @@
extern bool code_has_value();
extern float probe_pt(float x, float y, bool, int);
extern bool set_probe_deployed(bool);
#define DEPLOY_PROBE() set_probe_deployed(true)
#define STOW_PROBE() set_probe_deployed(false)
bool ProbeStay = true;
constexpr float ubl_3_point_1_X = UBL_PROBE_PT_1_X,
@ -302,7 +302,7 @@
// The simple parameter flags and values are 'static' so parameter parsing can be in a support routine.
static int g29_verbose_level, phase_value = -1, repetition_cnt,
storage_slot = 0, map_type; //unlevel_value = -1;
storage_slot = 0, map_type, grid_size_G ; //unlevel_value = -1;
static bool repeat_flag, c_flag, x_flag, y_flag;
static float x_pos, y_pos, measured_z, card_thickness = 0.0, ubl_constant = 0.0;
@ -385,7 +385,20 @@
// return;
//}
}
//*/
*/
if (code_seen('G')) {
uint8_t grid_size_G = code_has_value() ? code_value_int() : 3;
if (grid_size_G < 2) {
SERIAL_PROTOCOLLNPGM("ERROR - grid size must be 2 or more");
return;
}
if (grid_size_G > GRID_MAX_POINTS_X || grid_size_G > GRID_MAX_POINTS_Y ) {
SERIAL_PROTOCOLLNPGM("ERROR - grid size can NOT exceed GRID_MAX_POINTS_X nor GRID_MAX_POINTS_Y");
return;
}
tilt_mesh_based_on_probed_grid( code_seen('O')||code_seen('M'));
}
if (code_seen('P')) {
phase_value = code_value_int();
@ -488,7 +501,7 @@
SERIAL_ECHO_START;
SERIAL_ECHOLNPGM("Checking G29 has control of LCD Panel:");
KEEPALIVE_STATE(PAUSED_FOR_USER);
ubl.has_control_of_lcd_panel++;
ubl.has_control_of_lcd_panel = true;
while (!ubl_lcd_clicked()) {
safe_delay(250);
if (ubl.encoder_diff) {
@ -630,7 +643,7 @@
save_ubl_active_state_and_disable();
//measured_z = probe_pt(x_pos + X_PROBE_OFFSET_FROM_EXTRUDER, y_pos + Y_PROBE_OFFSET_FROM_EXTRUDER, ProbeDeployAndStow, g29_verbose_level);
ubl.has_control_of_lcd_panel++; // Grab the LCD Hardware
ubl.has_control_of_lcd_panel = true; // Grab the LCD Hardware
measured_z = 1.5;
do_blocking_move_to_z(measured_z); // Get close to the bed, but leave some space so we don't damage anything
// The user is not going to be locking in a new Z-Offset very often so
@ -646,7 +659,7 @@
do_blocking_move_to_z(measured_z);
} while (!ubl_lcd_clicked());
ubl.has_control_of_lcd_panel++; // There is a race condition for the Encoder Wheel getting clicked.
ubl.has_control_of_lcd_panel = true; // There is a race condition for the Encoder Wheel getting clicked.
// It could get detected in lcd_mesh_edit (actually _lcd_mesh_fine_tune)
// or here. So, until we are done looking for a long Encoder Wheel Press,
// we need to take control of the panel
@ -744,7 +757,7 @@
void probe_entire_mesh(const float &lx, const float &ly, const bool do_ubl_mesh_map, const bool stow_probe, bool do_furthest) {
mesh_index_pair location;
ubl.has_control_of_lcd_panel++;
ubl.has_control_of_lcd_panel = true;
save_ubl_active_state_and_disable(); // we don't do bed level correction because we want the raw data when we probe
DEPLOY_PROBE();
@ -872,7 +885,7 @@
float measure_business_card_thickness(const float &in_height) {
ubl.has_control_of_lcd_panel++;
ubl.has_control_of_lcd_panel = true;
save_ubl_active_state_and_disable(); // we don't do bed level correction because we want the raw data when we probe
SERIAL_PROTOCOLLNPGM("Place Shim Under Nozzle and Perform Measurement.");
@ -899,7 +912,7 @@
void manually_probe_remaining_mesh(const float &lx, const float &ly, const float &z_clearance, const float &card_thickness, const bool do_ubl_mesh_map) {
ubl.has_control_of_lcd_panel++;
ubl.has_control_of_lcd_panel = true;
save_ubl_active_state_and_disable(); // we don't do bed level correction because we want the raw data when we probe
do_blocking_move_to_z(z_clearance);
do_blocking_move_to_xy(lx, ly);
@ -994,6 +1007,16 @@
return UBL_ERR;
}
if (code_seen('G')) {
grid_size_G = 3;
if (code_has_value())
grid_size_G = code_value_int();
if (!WITHIN(grid_size_G, 2, 10)) {
SERIAL_PROTOCOLLNPGM("Invalid grid probe points specified.\n");
return UBL_ERR;
}
}
x_flag = code_seen('X') && code_has_value();
x_pos = x_flag ? code_value_float() : current_position[X_AXIS];
if (!WITHIN(RAW_X_POSITION(x_pos), X_MIN_POS, X_MAX_POS)) {
@ -1053,7 +1076,6 @@
return UBL_ERR;
}
/*
if (code_seen('M')) { // Check if a map type was specified
map_type = code_has_value() ? code_value_int() : 0;
if (!WITHIN(map_type, 0, 1)) {
@ -1061,7 +1083,6 @@
return UBL_ERR;
}
}
//*/
return UBL_OK;
}
@ -1070,6 +1091,7 @@
* This function goes away after G29 debug is complete. But for right now, it is a handy
* routine to dump binary data structures.
*/
/*
void dump(char * const str, const float &f) {
char *ptr;
@ -1087,6 +1109,7 @@
SERIAL_EOL;
}
*/
static int ubl_state_at_invocation = 0,
ubl_state_recursion_chk = 0;
@ -1438,4 +1461,178 @@
SERIAL_ECHOLNPGM("Done Editing Mesh");
}
void tilt_mesh_based_on_probed_grid( const bool do_ubl_mesh_map) {
int8_t grid_G_index_to_xpos[grid_size_G]; // UBL MESH X index to be probed
int8_t grid_G_index_to_ypos[grid_size_G]; // UBL MESH Y index to be probed
int8_t i, j ,k, xCount, yCount, G_X_index, G_Y_index; // counter variables
float z_values_G[grid_size_G][grid_size_G];
struct linear_fit *results;
for (G_Y_index = 0; G_Y_index < grid_size_G; G_Y_index++)
for (G_X_index = 0; G_X_index < grid_size_G; G_X_index++)
z_values_G[G_X_index][G_Y_index] = NAN;
uint8_t x_min = GRID_MAX_POINTS_X - 1;
uint8_t x_max = 0;
uint8_t y_min = GRID_MAX_POINTS_Y - 1;
uint8_t y_max = 0;
//find min & max probeable points in the mesh
for (xCount = 0; xCount < GRID_MAX_POINTS_X ; xCount++) {
for (yCount = 0; yCount < GRID_MAX_POINTS_Y ; yCount++) {
if (WITHIN(ubl.mesh_index_to_xpos[xCount], MIN_PROBE_X, MAX_PROBE_X) && WITHIN(ubl.mesh_index_to_ypos[yCount], MIN_PROBE_Y, MAX_PROBE_Y)) {
if (x_min > xCount) x_min = xCount;
if (x_max < xCount) x_max = xCount;
if (y_min > yCount) y_min = yCount;
if (y_max < yCount) y_max = yCount;
}
}
}
if ((x_max - x_min + 1) < (grid_size_G) || (y_max - y_min + 1) < (grid_size_G)) {
SERIAL_PROTOCOLPAIR("ERROR - probeable UBL MESH smaller than grid - X points: ", x_max - x_min + 1);
SERIAL_PROTOCOLPAIR(" Y points: ", y_max - y_min + 1);
SERIAL_PROTOCOLLNPAIR(" grid: ", grid_size_G);
return;
}
// populate X matrix
for (G_X_index = 0; G_X_index < grid_size_G; G_X_index++) {
grid_G_index_to_xpos[G_X_index] = x_min + G_X_index * (x_max - x_min)/(grid_size_G - 1);
if (G_X_index > 0 && grid_G_index_to_xpos[G_X_index - 1] == grid_G_index_to_xpos[G_X_index] ) {
grid_G_index_to_xpos[G_X_index] = grid_G_index_to_xpos[G_X_index - 1] + 1;
}
}
// populate Y matrix
for (G_Y_index = 0; G_Y_index < grid_size_G; G_Y_index++) {
grid_G_index_to_ypos[G_Y_index] = y_min + G_Y_index * (y_max - y_min)/(grid_size_G - 1);
if (G_Y_index > 0 && grid_G_index_to_ypos[G_Y_index -1] == grid_G_index_to_ypos[G_Y_index] ) {
grid_G_index_to_ypos[G_Y_index] = grid_G_index_to_ypos[G_Y_index - 1] + 1;
}
}
ubl.has_control_of_lcd_panel = true;
save_ubl_active_state_and_disable(); // we don't do bed level correction because we want the raw data when we probe
DEPLOY_PROBE();
// this is a copy of the G29 AUTO_BED_LEVELING_BILINEAR method/code
#undef PROBE_Y_FIRST
#if ENABLED(PROBE_Y_FIRST)
#define PR_OUTER_VAR xCount
#define PR_OUTER_NUM grid_size_G
#define PR_INNER_VAR yCount
#define PR_INNER_NUM grid_size_G
#else
#define PR_OUTER_VAR yCount
#define PR_OUTER_NUM grid_size_G
#define PR_INNER_VAR xCount
#define PR_INNER_NUM grid_size_G
#endif
bool zig = PR_OUTER_NUM & 1; // Always end at RIGHT and BACK_PROBE_BED_POSITION
// Outer loop is Y with PROBE_Y_FIRST disabled
for (PR_OUTER_VAR = 0; PR_OUTER_VAR < PR_OUTER_NUM; PR_OUTER_VAR++) {
int8_t inStart, inStop, inInc;
SERIAL_PROTOCOLPAIR("\nPR_OUTER_VAR: ", PR_OUTER_VAR);
if (zig) { // away from origin
inStart = 0;
inStop = PR_INNER_NUM;
inInc = 1;
}
else { // towards origin
inStart = PR_INNER_NUM - 1;
inStop = -1;
inInc = -1;
}
zig = !zig; // zag
// Inner loop is Y with PROBE_Y_FIRST enabled
for (PR_INNER_VAR = inStart; PR_INNER_VAR != inStop; PR_INNER_VAR += inInc) {
SERIAL_PROTOCOLPAIR("\nPR_INNER_VAR: ", PR_INNER_VAR);
SERIAL_PROTOCOLPAIR("\nCheckpoint: ", 1);
// end of G29 AUTO_BED_LEVELING_BILINEAR method/code
if (ubl_lcd_clicked()) {
SERIAL_PROTOCOLPAIR("\nCheckpoint: ", 2);
SERIAL_PROTOCOLLNPGM("\nGrid only partially populated.\n");
lcd_quick_feedback();
STOW_PROBE();
SERIAL_PROTOCOLPAIR("\nCheckpoint: ", 3);
while (ubl_lcd_clicked()) idle();
SERIAL_PROTOCOLPAIR("\nCheckpoint: ", 4);
ubl.has_control_of_lcd_panel = false;
restore_ubl_active_state_and_leave();
safe_delay(50); // Debounce the Encoder wheel
return;
}
SERIAL_PROTOCOLPAIR("\nCheckpoint: ", 5);
const float probeX = ubl.mesh_index_to_xpos[grid_G_index_to_xpos[xCount]], //where we want the probe to be
probeY = ubl.mesh_index_to_ypos[grid_G_index_to_ypos[yCount]];
SERIAL_PROTOCOLPAIR("\nCheckpoint: ", 6);
const float measured_z = probe_pt(LOGICAL_X_POSITION(probeX), LOGICAL_Y_POSITION(probeY), code_seen('E'), (code_seen('V') && code_has_value()) ? code_value_int() : 0 ); // takes into account the offsets
SERIAL_PROTOCOLPAIR("\nmeasured_z: ", measured_z );
z_values_G[xCount][yCount] = measured_z;
//SERIAL_PROTOCOLLNPGM("\nFine Tuning of Mesh Stopped.");
}
}
SERIAL_PROTOCOL("\nDone probing...\n");
STOW_PROBE();
restore_ubl_active_state_and_leave();
// ?? ubl.has_control_of_lcd_panel = true;
// do_blocking_move_to_xy(ubl.mesh_index_to_xpos[grid_G_index_to_xpos[0]], ubl.mesh_index_to_ypos[grid_G_index_to_ypos[0]]);
// least squares code
double xxx9[] = { 0,50,100,150,200, 20,70,120,165,195, 0,50,100,150,200, 0,55,100,150,200, 0,65,100,150,205 };
double yyy9[] = { 0, 1, 2, 3, 4, 50, 51, 52, 53, 54, 100, 101,102,103,104, 150,151,152,153,154, 200,201,202,203,204 };
double zzz9[] = { 0.01,.002,-.01,-.02,0, 0.01,.002,-.01,-.02,0, 0.01,.002,-.01,-.02,0, 0.01,.002,-.01,-.02,0, 0.01,.002,-.01,-.012,0.01};
int nine_size = sizeof(xxx9) / sizeof(double);
double xxx0[] = { 0.0, 0.0, 1.0 }; // Expect [0,0,0.1,0]
double yyy0[] = { 0.0, 1.0, 0.0 };
double zzz0[] = { 0.1, 0.1, 0.1 };
int zero_size = sizeof(xxx0) / sizeof(double);
double xxx[] = { 0.0, 0.0, 1.0, 1.0 }; // Expect [0.1,0,0.05,0]
double yyy[] = { 0.0, 1.0, 0.0, 1.0 };
double zzz[] = { 0.05, 0.05, 0.15, 0.15 };
int three_size = sizeof(xxx) / sizeof(double);
results = lsf_linear_fit(xxx9, yyy9, zzz9, nine_size);
SERIAL_PROTOCOLPAIR("\nxxx9->A =", results->A);
SERIAL_PROTOCOLPAIR("\nxxx9->B =", results->B);
SERIAL_PROTOCOLPAIR("\nxxx9->D =", results->D);
SERIAL_PROTOCOL("\n");
results = lsf_linear_fit(xxx0, yyy0, zzz0, zero_size);
SERIAL_PROTOCOLPAIR("\nxxx0->A =", results->A);
SERIAL_PROTOCOLPAIR("\nxxx0->B =", results->B);
SERIAL_PROTOCOLPAIR("\nxxx0->D =", results->D);
SERIAL_PROTOCOL("\n");
results = lsf_linear_fit(xxx, yyy, zzz, three_size);
SERIAL_PROTOCOLPAIR("\nxxx->A =", results->A);
SERIAL_PROTOCOLPAIR("\nxxx->B =", results->B);
SERIAL_PROTOCOLPAIR("\nxxx->D =", results->D);
SERIAL_PROTOCOL("\n");
return;
} // end of tilt_mesh_based_on_probed_grid()
#endif // AUTO_BED_LEVELING_UBL