Patches to bring UBL closer to compliance

This commit is contained in:
Scott Lahteine
2017-03-24 00:53:37 -05:00
parent d076c1b604
commit b5711a99a2
10 changed files with 493 additions and 543 deletions

View File

@@ -29,35 +29,97 @@
#include <avr/io.h>
#include <math.h>
extern float destination[XYZE];
extern void set_current_to_destination();
extern void debug_current_and_destination(char *title);
void debug_current_and_destination(char *title) {
// if the title message starts with a '!' it is so important, we are going to
// ignore the status of the g26_debug_flag
if (*title != '!' && !g26_debug_flag) return;
const float de = destination[E_AXIS] - current_position[E_AXIS];
if (de == 0.0) return;
const float dx = current_position[X_AXIS] - destination[X_AXIS],
dy = current_position[Y_AXIS] - destination[Y_AXIS],
xy_dist = HYPOT(dx, dy);
if (xy_dist == 0.0) {
return;
//SERIAL_ECHOPGM(" FPMM=");
//const float fpmm = de / xy_dist;
//SERIAL_PROTOCOL_F(fpmm, 6);
}
else {
SERIAL_ECHOPGM(" fpmm=");
const float fpmm = de / xy_dist;
SERIAL_ECHO_F(fpmm, 6);
}
SERIAL_ECHOPGM(" current=( ");
SERIAL_ECHO_F(current_position[X_AXIS], 6);
SERIAL_ECHOPGM(", ");
SERIAL_ECHO_F(current_position[Y_AXIS], 6);
SERIAL_ECHOPGM(", ");
SERIAL_ECHO_F(current_position[Z_AXIS], 6);
SERIAL_ECHOPGM(", ");
SERIAL_ECHO_F(current_position[E_AXIS], 6);
SERIAL_ECHOPGM(" ) destination=( ");
if (current_position[X_AXIS] == destination[X_AXIS])
SERIAL_ECHOPGM("-------------");
else
SERIAL_ECHO_F(destination[X_AXIS], 6);
SERIAL_ECHOPGM(", ");
if (current_position[Y_AXIS] == destination[Y_AXIS])
SERIAL_ECHOPGM("-------------");
else
SERIAL_ECHO_F(destination[Y_AXIS], 6);
SERIAL_ECHOPGM(", ");
if (current_position[Z_AXIS] == destination[Z_AXIS])
SERIAL_ECHOPGM("-------------");
else
SERIAL_ECHO_F(destination[Z_AXIS], 6);
SERIAL_ECHOPGM(", ");
if (current_position[E_AXIS] == destination[E_AXIS])
SERIAL_ECHOPGM("-------------");
else
SERIAL_ECHO_F(destination[E_AXIS], 6);
SERIAL_ECHOPGM(" ) ");
SERIAL_ECHO(title);
SERIAL_EOL;
SET_INPUT_PULLUP(66); // Roxy's Left Switch is on pin 66. Right Switch is on pin 65
//if (been_to_2_6) {
//while ((digitalRead(66) & 0x01) != 0)
// idle();
//}
}
void ubl_line_to_destination(const float &x_end, const float &y_end, const float &z_end, const float &e_end, const float &feed_rate, uint8_t extruder) {
int cell_start_xi, cell_start_yi, cell_dest_xi, cell_dest_yi,
current_xi, current_yi,
dxi, dyi, xi_cnt, yi_cnt;
float x_start, y_start,
x, y, z1, z2, z0 /*, z_optimized */,
next_mesh_line_x, next_mesh_line_y, a0ma1diva2ma1,
on_axis_distance, e_normalized_dist, e_position, e_start, z_normalized_dist, z_position, z_start,
dx, dy, adx, ady, m, c;
/**
* Much of the nozzle movement will be within the same cell. So we will do as little computation
* as possible to determine if this is the case. If this move is within the same cell, we will
* just do the required Z-Height correction, call the Planner's buffer_line() routine, and leave
*/
const float x_start = current_position[X_AXIS],
y_start = current_position[Y_AXIS],
z_start = current_position[Z_AXIS],
e_start = current_position[E_AXIS];
x_start = current_position[X_AXIS];
y_start = current_position[Y_AXIS];
z_start = current_position[Z_AXIS];
e_start = current_position[E_AXIS];
cell_start_xi = ubl.get_cell_index_x(x_start);
cell_start_yi = ubl.get_cell_index_y(y_start);
cell_dest_xi = ubl.get_cell_index_x(x_end);
cell_dest_yi = ubl.get_cell_index_y(y_end);
const int cell_start_xi = ubl.get_cell_index_x(RAW_X_POSITION(x_start)),
cell_start_yi = ubl.get_cell_index_y(RAW_Y_POSITION(y_start)),
cell_dest_xi = ubl.get_cell_index_x(RAW_X_POSITION(x_end)),
cell_dest_yi = ubl.get_cell_index_y(RAW_Y_POSITION(y_end));
if (g26_debug_flag) {
SERIAL_ECHOPGM(" ubl_line_to_destination(xe=");
@@ -68,7 +130,7 @@
SERIAL_ECHO(z_end);
SERIAL_ECHOPGM(", ee=");
SERIAL_ECHO(e_end);
SERIAL_ECHOPGM(")\n");
SERIAL_ECHOLNPGM(")");
debug_current_and_destination((char*)"Start of ubl_line_to_destination()");
}
@@ -82,7 +144,7 @@
if (cell_dest_xi < 0 || cell_dest_yi < 0 || cell_dest_xi >= UBL_MESH_NUM_X_POINTS || cell_dest_yi >= UBL_MESH_NUM_Y_POINTS) {
// Note: There is no Z Correction in this case. We are off the grid and don't know what
// Note: There is no Z Correction in this case. We are off the grid and don't know what
// a reasonable correction would be.
planner.buffer_line(x_end, y_end, z_end + ubl.state.z_offset, e_end, feed_rate, extruder);
@@ -105,20 +167,18 @@
* to create a 1-over number for us. That will allow us to do a floating point multiply instead of a floating point divide.
*/
a0ma1diva2ma1 = (x_end - mesh_index_to_x_location[cell_dest_xi]) * 0.1 * (MESH_X_DIST);
z1 = z_values[cell_dest_xi ][cell_dest_yi ] + a0ma1diva2ma1 *
(z_values[cell_dest_xi + 1][cell_dest_yi ] - z_values[cell_dest_xi][cell_dest_yi ]);
z2 = z_values[cell_dest_xi ][cell_dest_yi + 1] + a0ma1diva2ma1 *
(z_values[cell_dest_xi + 1][cell_dest_yi + 1] - z_values[cell_dest_xi][cell_dest_yi + 1]);
const float xratio = (RAW_X_POSITION(x_end) - mesh_index_to_x_location[cell_dest_xi]) * (1.0 / (MESH_X_DIST)),
z1 = z_values[cell_dest_xi ][cell_dest_yi ] + xratio *
(z_values[cell_dest_xi + 1][cell_dest_yi ] - z_values[cell_dest_xi][cell_dest_yi ]),
z2 = z_values[cell_dest_xi ][cell_dest_yi + 1] + xratio *
(z_values[cell_dest_xi + 1][cell_dest_yi + 1] - z_values[cell_dest_xi][cell_dest_yi + 1]);
// we are done with the fractional X distance into the cell. Now with the two Z-Heights we have calculated, we
// are going to apply the Y-Distance into the cell to interpolate the final Z correction.
a0ma1diva2ma1 = (y_end - mesh_index_to_y_location[cell_dest_yi]) * 0.1 * (MESH_Y_DIST);
const float yratio = (RAW_Y_POSITION(y_end) - mesh_index_to_y_location[cell_dest_yi]) * (1.0 / (MESH_Y_DIST));
z0 = z1 + (z2 - z1) * a0ma1diva2ma1;
float z0 = z1 + (z2 - z1) * yratio;
/**
* Debug code to use non-optimized get_z_correction() and to do a sanity check
@@ -126,7 +186,7 @@
*/
/*
z_optimized = z0;
z0 = ubl.get_z_correction( x_end, y_end);
z0 = ubl.get_z_correction(x_end, y_end);
if (fabs(z_optimized - z0) > .01 || isnan(z0) || isnan(z_optimized)) {
debug_current_and_destination((char*)"FINAL_MOVE: z_correction()");
if (isnan(z0)) SERIAL_ECHO(" z0==NAN ");
@@ -139,7 +199,7 @@
SERIAL_EOL;
}
//*/
z0 = z0 * ubl.fade_scaling_factor_for_z(z_end);
z0 *= ubl.fade_scaling_factor_for_z(z_end);
/**
* If part of the Mesh is undefined, it will show up as NAN
@@ -167,31 +227,17 @@
* blocks of code:
*/
dx = x_end - x_start;
dy = y_end - y_start;
const float dx = x_end - x_start,
dy = y_end - y_start;
const int left_flag = dx < 0.0 ? 1 : 0,
down_flag = dy < 0.0 ? 1 : 0;
if (left_flag) { // figure out which way we need to move to get to the next cell
dxi = -1;
adx = -dx; // absolute value of dx. We already need to check if dx and dy are negative.
}
else { // We may as well generate the appropriate values for adx and ady right now
dxi = 1; // to save setting up the abs() function call and actually doing the call.
adx = dx;
}
if (dy < 0.0) {
dyi = -1;
ady = -dy; // absolute value of dy
}
else {
dyi = 1;
ady = dy;
}
const float adx = left_flag ? -dx : dx,
ady = down_flag ? -dy : dy;
if (cell_start_xi == cell_dest_xi) dxi = 0;
if (cell_start_yi == cell_dest_yi) dyi = 0;
const int dxi = cell_start_xi == cell_dest_xi ? 0 : left_flag ? -1 : 1,
dyi = cell_start_yi == cell_dest_yi ? 0 : down_flag ? -1 : 1;
/**
* Compute the scaling factor for the extruder for each partial move.
@@ -204,22 +250,20 @@
const bool use_x_dist = adx > ady;
on_axis_distance = use_x_dist ? x_end - x_start : y_end - y_start;
float on_axis_distance = use_x_dist ? dx : dy,
e_position = e_end - e_start,
z_position = z_end - z_start;
e_position = e_end - e_start;
e_normalized_dist = e_position / on_axis_distance;
const float e_normalized_dist = e_position / on_axis_distance,
z_normalized_dist = z_position / on_axis_distance;
z_position = z_end - z_start;
z_normalized_dist = z_position / on_axis_distance;
int current_xi = cell_start_xi, current_yi = cell_start_yi;
const bool inf_normalized_flag = e_normalized_dist == INFINITY || e_normalized_dist == -INFINITY;
const float m = dy / dx,
c = y_start - m * x_start;
current_xi = cell_start_xi;
current_yi = cell_start_yi;
m = dy / dx;
c = y_start - m * x_start;
const bool inf_m_flag = (m == INFINITY || m == -INFINITY);
const bool inf_normalized_flag = NEAR_ZERO(on_axis_distance),
inf_m_flag = NEAR_ZERO(dx);
/**
* This block handles vertical lines. These are lines that stay within the same
@@ -230,16 +274,16 @@
current_yi += down_flag; // Line is heading down, we just want to go to the bottom
while (current_yi != cell_dest_yi + down_flag) {
current_yi += dyi;
next_mesh_line_y = mesh_index_to_y_location[current_yi];
const float next_mesh_line_y = LOGICAL_Y_POSITION(mesh_index_to_y_location[current_yi]);
/**
* inf_m_flag? the slope of the line is infinite, we won't do the calculations
* else, we know the next X is the same so we can recover and continue!
* Calculate X at the next Y mesh line
*/
x = inf_m_flag ? x_start : (next_mesh_line_y - c) / m;
const float x = inf_m_flag ? x_start : (next_mesh_line_y - c) / m;
z0 = ubl.get_z_correction_along_horizontal_mesh_line_at_specific_X(x, current_xi, current_yi);
float z0 = ubl.get_z_correction_along_horizontal_mesh_line_at_specific_X(x, current_xi, current_yi);
/**
* Debug code to use non-optimized get_z_correction() and to do a sanity check
@@ -247,7 +291,7 @@
*/
/*
z_optimized = z0;
z0 = ubl.get_z_correction( x, next_mesh_line_y);
z0 = ubl.get_z_correction(x, next_mesh_line_y);
if (fabs(z_optimized - z0) > .01 || isnan(z0) || isnan(z_optimized)) {
debug_current_and_destination((char*)"VERTICAL z_correction()");
if (isnan(z0)) SERIAL_ECHO(" z0==NAN ");
@@ -261,7 +305,7 @@
}
//*/
z0 = z0 * ubl.fade_scaling_factor_for_z(z_end);
z0 *= ubl.fade_scaling_factor_for_z(z_end);
/**
* If part of the Mesh is undefined, it will show up as NAN
@@ -272,7 +316,7 @@
*/
if (isnan(z0)) z0 = 0.0;
y = mesh_index_to_y_location[current_yi];
const float y = LOGICAL_Y_POSITION(mesh_index_to_y_location[current_yi]);
/**
* Without this check, it is possible for the algorithm to generate a zero length move in the case
@@ -321,10 +365,10 @@
// edge of this cell for the first move.
while (current_xi != cell_dest_xi + left_flag) {
current_xi += dxi;
next_mesh_line_x = mesh_index_to_x_location[current_xi];
y = m * next_mesh_line_x + c; // Calculate X at the next Y mesh line
const float next_mesh_line_x = LOGICAL_X_POSITION(mesh_index_to_x_location[current_xi]),
y = m * next_mesh_line_x + c; // Calculate X at the next Y mesh line
z0 = ubl.get_z_correction_along_vertical_mesh_line_at_specific_Y(y, current_xi, current_yi);
float z0 = ubl.get_z_correction_along_vertical_mesh_line_at_specific_Y(y, current_xi, current_yi);
/**
* Debug code to use non-optimized get_z_correction() and to do a sanity check
@@ -332,7 +376,7 @@
*/
/*
z_optimized = z0;
z0 = ubl.get_z_correction( next_mesh_line_x, y);
z0 = ubl.get_z_correction(next_mesh_line_x, y);
if (fabs(z_optimized - z0) > .01 || isnan(z0) || isnan(z_optimized)) {
debug_current_and_destination((char*)"HORIZONTAL z_correction()");
if (isnan(z0)) SERIAL_ECHO(" z0==NAN ");
@@ -357,7 +401,7 @@
*/
if (isnan(z0)) z0 = 0.0;
x = mesh_index_to_x_location[current_xi];
const float x = LOGICAL_X_POSITION(mesh_index_to_x_location[current_xi]);
/**
* Without this check, it is possible for the algorithm to generate a zero length move in the case
@@ -396,10 +440,10 @@
*
*/
xi_cnt = cell_start_xi - cell_dest_xi;
if (xi_cnt < 0) xi_cnt = -xi_cnt;
int xi_cnt = cell_start_xi - cell_dest_xi,
yi_cnt = cell_start_yi - cell_dest_yi;
yi_cnt = cell_start_yi - cell_dest_yi;
if (xi_cnt < 0) xi_cnt = -xi_cnt;
if (yi_cnt < 0) yi_cnt = -yi_cnt;
current_xi += left_flag;
@@ -407,20 +451,19 @@
while (xi_cnt > 0 || yi_cnt > 0) {
next_mesh_line_x = mesh_index_to_x_location[current_xi + dxi];
next_mesh_line_y = mesh_index_to_y_location[current_yi + dyi];
y = m * next_mesh_line_x + c; // Calculate Y at the next X mesh line
x = (next_mesh_line_y - c) / m; // Calculate X at the next Y mesh line (we don't have to worry
// about m being equal to 0.0 If this was the case, we would have
// detected this as a vertical line move up above and we wouldn't
// be down here doing a generic type of move.
const float next_mesh_line_x = LOGICAL_X_POSITION(mesh_index_to_x_location[current_xi + dxi]),
next_mesh_line_y = LOGICAL_Y_POSITION(mesh_index_to_y_location[current_yi + dyi]),
y = m * next_mesh_line_x + c, // Calculate Y at the next X mesh line
x = (next_mesh_line_y - c) / m; // Calculate X at the next Y mesh line (we don't have to worry
// about m being equal to 0.0 If this was the case, we would have
// detected this as a vertical line move up above and we wouldn't
// be down here doing a generic type of move.
if (left_flag == (x > next_mesh_line_x)) { // Check if we hit the Y line first
//
// Yes! Crossing a Y Mesh Line next
//
z0 = ubl.get_z_correction_along_horizontal_mesh_line_at_specific_X(x, current_xi - left_flag, current_yi + dyi);
float z0 = ubl.get_z_correction_along_horizontal_mesh_line_at_specific_X(x, current_xi - left_flag, current_yi + dyi);
/**
* Debug code to use non-optimized get_z_correction() and to do a sanity check
@@ -428,7 +471,7 @@
*/
/*
z_optimized = z0;
z0 = ubl.get_z_correction( x, next_mesh_line_y);
z0 = ubl.get_z_correction(x, next_mesh_line_y);
if (fabs(z_optimized - z0) > .01 || isnan(z0) || isnan(z_optimized)) {
debug_current_and_destination((char*)"General_1: z_correction()");
if (isnan(z0)) SERIAL_ECHO(" z0==NAN ");
@@ -471,7 +514,7 @@
//
// Yes! Crossing a X Mesh Line next
//
z0 = ubl.get_z_correction_along_vertical_mesh_line_at_specific_Y(y, current_xi + dxi, current_yi - down_flag);
float z0 = ubl.get_z_correction_along_vertical_mesh_line_at_specific_Y(y, current_xi + dxi, current_yi - down_flag);
/**
* Debug code to use non-optimized get_z_correction() and to do a sanity check
@@ -479,7 +522,7 @@
*/
/*
z_optimized = z0;
z0 = ubl.get_z_correction( next_mesh_line_x, y);
z0 = ubl.get_z_correction(next_mesh_line_x, y);
if (fabs(z_optimized - z0) > .01 || isnan(z0) || isnan(z_optimized)) {
debug_current_and_destination((char*)"General_2: z_correction()");
if (isnan(z0)) SERIAL_ECHO(" z0==NAN ");
@@ -493,7 +536,7 @@
}
//*/
z0 = z0 * ubl.fade_scaling_factor_for_z(z_end);
z0 *= ubl.fade_scaling_factor_for_z(z_end);
/**
* If part of the Mesh is undefined, it will show up as NAN