Skip to content

Commit

Permalink
Revert experimental NAN patch
Browse files Browse the repository at this point in the history
Hold changes from MarlinFirmware#21575 (24a095c) for more testing.
  • Loading branch information
thinkyhead committed Apr 16, 2021
1 parent 32dba5e commit 528b9bd
Show file tree
Hide file tree
Showing 31 changed files with 104 additions and 109 deletions.
3 changes: 0 additions & 3 deletions Marlin/src/core/macros.h
Original file line number Diff line number Diff line change
Expand Up @@ -112,9 +112,6 @@
#define SIGN(a) ({__typeof__(a) _a = (a); (_a>0)-(_a<0);})
#define IS_POWER_OF_2(x) ((x) && !((x) & ((x) - 1)))

#define MFNAN 999999.0f
#define ISNAN(V) ((V) == MFNAN)

// Macros to constrain values
#ifdef __cplusplus

Expand Down
14 changes: 7 additions & 7 deletions Marlin/src/feature/bedlevel/abl/abl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ bed_mesh_t z_values;
* Extrapolate a single point from its neighbors
*/
static void extrapolate_one_point(const uint8_t x, const uint8_t y, const int8_t xdir, const int8_t ydir) {
if (!ISNAN(z_values[x][y])) return;
if (!isnan(z_values[x][y])) return;
if (DEBUGGING(LEVELING)) {
DEBUG_ECHOPGM("Extrapolate [");
if (x < 10) DEBUG_CHAR(' ');
Expand All @@ -63,12 +63,12 @@ static void extrapolate_one_point(const uint8_t x, const uint8_t y, const int8_t
c1 = z_values[x1][y1], c2 = z_values[x2][y2];

// Treat far unprobed points as zero, near as equal to far
if (ISNAN(a2)) a2 = 0.0;
if (ISNAN(a1)) a1 = a2;
if (ISNAN(b2)) b2 = 0.0;
if (ISNAN(b1)) b1 = b2;
if (ISNAN(c2)) c2 = 0.0;
if (ISNAN(c1)) c1 = c2;
if (isnan(a2)) a2 = 0.0;
if (isnan(a1)) a1 = a2;
if (isnan(b2)) b2 = 0.0;
if (isnan(b1)) b1 = b2;
if (isnan(c2)) c2 = 0.0;
if (isnan(c1)) c1 = c2;

const float a = 2 * a1 - a2, b = 2 * b1 - b2, c = 2 * c1 - c2;

Expand Down
4 changes: 2 additions & 2 deletions Marlin/src/feature/bedlevel/bedlevel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ void reset_bed_level() {
bilinear_start.reset();
bilinear_grid_spacing.reset();
GRID_LOOP(x, y) {
z_values[x][y] = MFNAN;
z_values[x][y] = NAN;
TERN_(EXTENSIBLE_UI, ExtUI::onMeshUpdate(x, y, 0));
}
#elif ABL_PLANAR
Expand Down Expand Up @@ -177,7 +177,7 @@ void reset_bed_level() {
LOOP_L_N(x, sx) {
SERIAL_CHAR(' ');
const float offset = fn(x, y);
if (!ISNAN(offset)) {
if (!isnan(offset)) {
if (offset >= 0) SERIAL_CHAR('+');
SERIAL_ECHO_F(offset, int(precision));
}
Expand Down
4 changes: 1 addition & 3 deletions Marlin/src/feature/bedlevel/mbl/mesh_bed_leveling.h
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,6 @@ class mesh_bed_leveling {
static inline xy_int8_t probe_indexes(const xy_pos_t &xy) { return probe_indexes(xy.x, xy.y); }

static float calc_z0(const_float_t a0, const_float_t a1, const_float_t z1, const_float_t a2, const_float_t z2) {
if (ISNAN(a0) || ISNAN(a1) || ISNAN(z1) || ISNAN(a2) || ISNAN(z2)) return MFNAN;
const float delta_z = (z2 - z1) / (a2 - a1),
delta_a = a0 - a1;
return z1 + delta_a * delta_z;
Expand All @@ -118,8 +117,7 @@ class mesh_bed_leveling {
z2 = calc_z0(pos.x, x1, z_values[ind.x][ind.y+1], x2, z_values[ind.x+1][ind.y+1]),
zf = calc_z0(pos.y, y1, z1, y2, z2);


return ISNAN(zf) ? zf : z_offset + zf * factor;
return z_offset + zf * factor;
}

#if IS_CARTESIAN && DISABLED(SEGMENT_LEVELED_MOVES)
Expand Down
10 changes: 5 additions & 5 deletions Marlin/src/feature/bedlevel/ubl/ubl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ void unified_bed_leveling::report_current_mesh() {
if (!leveling_is_valid()) return;
SERIAL_ECHO_MSG(" G29 I999");
GRID_LOOP(x, y)
if (!ISNAN(z_values[x][y])) {
if (!isnan(z_values[x][y])) {
SERIAL_ECHO_START();
SERIAL_ECHOPAIR(" M421 I", x, " J", y);
SERIAL_ECHOLNPAIR_F_P(SP_Z_STR, z_values[x][y], 4);
Expand Down Expand Up @@ -99,7 +99,7 @@ void unified_bed_leveling::reset() {

void unified_bed_leveling::invalidate() {
set_bed_leveling_enabled(false);
set_all_mesh_points_to_value(MFNAN);
set_all_mesh_points_to_value(NAN);
}

void unified_bed_leveling::set_all_mesh_points_to_value(const_float_t value) {
Expand All @@ -116,7 +116,7 @@ void unified_bed_leveling::set_all_mesh_points_to_value(const_float_t value) {

void unified_bed_leveling::set_store_from_mesh(const bed_mesh_t &in_values, mesh_store_t &stored_values) {
auto z_to_store = [](const_float_t z) {
if (ISNAN(z)) return Z_STEPS_NAN;
if (isnan(z)) return Z_STEPS_NAN;
const int32_t z_scaled = TRUNC(z * mesh_store_scaling);
if (z_scaled == Z_STEPS_NAN || !WITHIN(z_scaled, INT16_MIN, INT16_MAX))
return Z_STEPS_NAN; // If Z is out of range, return our custom 'NaN'
Expand All @@ -127,7 +127,7 @@ void unified_bed_leveling::set_all_mesh_points_to_value(const_float_t value) {

void unified_bed_leveling::set_mesh_from_store(const mesh_store_t &stored_values, bed_mesh_t &out_values) {
auto store_to_z = [](const int16_t z_scaled) {
return z_scaled == Z_STEPS_NAN ? MFNAN : z_scaled / mesh_store_scaling;
return z_scaled == Z_STEPS_NAN ? NAN : z_scaled / mesh_store_scaling;
};
GRID_LOOP(x, y) out_values[x][y] = store_to_z(stored_values[x][y]);
}
Expand Down Expand Up @@ -211,7 +211,7 @@ void unified_bed_leveling::display_map(const int map_type) {
if (lcd) {
// TODO: Display on Graphical LCD
}
else if (ISNAN(f))
else if (isnan(f))
SERIAL_ECHOPGM_P(human ? PSTR(" . ") : PSTR("NAN"));
else if (human || csv) {
if (human && f >= 0.0) SERIAL_CHAR(f > 0 ? '+' : ' '); // Display sign also for positive numbers (' ' for 0)
Expand Down
6 changes: 3 additions & 3 deletions Marlin/src/feature/bedlevel/ubl/ubl.h
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,7 @@ class unified_bed_leveling {
#ifdef UBL_Z_RAISE_WHEN_OFF_MESH
#define _UBL_OUTER_Z_RAISE UBL_Z_RAISE_WHEN_OFF_MESH
#else
#define _UBL_OUTER_Z_RAISE MFNAN
#define _UBL_OUTER_Z_RAISE NAN
#endif

/**
Expand Down Expand Up @@ -269,7 +269,7 @@ class unified_bed_leveling {
const float z2 = calc_z0(rx0, mesh_index_to_xpos(cx), z_values[cx][my], mesh_index_to_xpos(cx + 1), z_values[mx][my]);
float z0 = calc_z0(ry0, mesh_index_to_ypos(cy), z1, mesh_index_to_ypos(cy + 1), z2);

if (ISNAN(z0)) { // if part of the Mesh is undefined, it will show up as MFNAN
if (isnan(z0)) { // if part of the Mesh is undefined, it will show up as NAN
z0 = 0.0; // in ubl.z_values[][] and propagate through the
// calculations. If our correction is NAN, we throw it out
// because part of the Mesh is undefined and we don't have the
Expand Down Expand Up @@ -301,7 +301,7 @@ class unified_bed_leveling {
#endif

static inline bool mesh_is_valid() {
GRID_LOOP(x, y) if (ISNAN(z_values[x][y])) return false;
GRID_LOOP(x, y) if (isnan(z_values[x][y])) return false;
return true;
}

Expand Down
40 changes: 20 additions & 20 deletions Marlin/src/feature/bedlevel/ubl/ubl_G29.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -331,7 +331,7 @@ void unified_bed_leveling::G29() {
// to invalidate the ENTIRE mesh, which can't be done with
// find_closest_mesh_point (which only returns REAL points).
if (closest.pos.x < 0) { invalidate_all = true; break; }
z_values[closest.pos.x][closest.pos.y] = MFNAN;
z_values[closest.pos.x][closest.pos.y] = NAN;
TERN_(EXTENSIBLE_UI, ExtUI::onMeshUpdate(closest.pos, 0.0f));
}
}
Expand Down Expand Up @@ -516,7 +516,7 @@ void unified_bed_leveling::G29() {
if (cpos.x < 0) {
// No more REAL INVALID mesh points to populate, so we ASSUME
// user meant to populate ALL INVALID mesh points to value
GRID_LOOP(x, y) if (ISNAN(z_values[x][y])) z_values[x][y] = param.C_constant;
GRID_LOOP(x, y) if (isnan(z_values[x][y])) z_values[x][y] = param.C_constant;
break; // No more invalid Mesh Points to populate
}
else {
Expand Down Expand Up @@ -675,7 +675,7 @@ void unified_bed_leveling::adjust_mesh_to_mean(const bool cflag, const_float_t o
float sum = 0;
int n = 0;
GRID_LOOP(x, y)
if (!ISNAN(z_values[x][y])) {
if (!isnan(z_values[x][y])) {
sum += z_values[x][y];
n++;
}
Expand All @@ -687,7 +687,7 @@ void unified_bed_leveling::adjust_mesh_to_mean(const bool cflag, const_float_t o
//
float sum_of_diff_squared = 0;
GRID_LOOP(x, y)
if (!ISNAN(z_values[x][y]))
if (!isnan(z_values[x][y]))
sum_of_diff_squared += sq(z_values[x][y] - mean);

SERIAL_ECHOLNPAIR("# of samples: ", n);
Expand All @@ -698,7 +698,7 @@ void unified_bed_leveling::adjust_mesh_to_mean(const bool cflag, const_float_t o

if (cflag)
GRID_LOOP(x, y)
if (!ISNAN(z_values[x][y])) {
if (!isnan(z_values[x][y])) {
z_values[x][y] -= mean + offset;
TERN_(EXTENSIBLE_UI, ExtUI::onMeshUpdate(x, y, z_values[x][y]));
}
Expand All @@ -709,7 +709,7 @@ void unified_bed_leveling::adjust_mesh_to_mean(const bool cflag, const_float_t o
*/
void unified_bed_leveling::shift_mesh_height() {
GRID_LOOP(x, y)
if (!ISNAN(z_values[x][y])) {
if (!isnan(z_values[x][y])) {
z_values[x][y] += param.C_constant;
TERN_(EXTENSIBLE_UI, ExtUI::onMeshUpdate(x, y, z_values[x][y]));
}
Expand Down Expand Up @@ -1017,7 +1017,7 @@ void set_message_with_feedback(PGM_P const msg_P) {
ui.refresh();

float new_z = z_values[lpos.x][lpos.y];
if (ISNAN(new_z)) new_z = 0; // Invalid points begin at 0
if (isnan(new_z)) new_z = 0; // Invalid points begin at 0
new_z = FLOOR(new_z * 1000) * 0.001f; // Chop off digits after the 1000ths place

ui.ubl_mesh_edit_start(new_z);
Expand Down Expand Up @@ -1227,7 +1227,7 @@ mesh_index_pair unified_bed_leveling::find_furthest_invalid_mesh_point() {
mesh_index_pair farthest { -1, -1, -99999.99 };

GRID_LOOP(i, j) {
if (!ISNAN(z_values[i][j])) continue; // Skip valid mesh points
if (!isnan(z_values[i][j])) continue; // Skip valid mesh points

// Skip unreachable points
if (!probe.can_reach(mesh_index_to_xpos(i), mesh_index_to_ypos(j)))
Expand All @@ -1238,7 +1238,7 @@ mesh_index_pair unified_bed_leveling::find_furthest_invalid_mesh_point() {
xy_int8_t nearby { -1, -1 };
float d1, d2 = 99999.9f;
GRID_LOOP(k, l) {
if (ISNAN(z_values[k][l])) continue;
if (isnan(z_values[k][l])) continue;

found_a_real = true;

Expand Down Expand Up @@ -1282,7 +1282,7 @@ mesh_index_pair unified_bed_leveling::find_furthest_invalid_mesh_point() {

static bool test_func(uint8_t i, uint8_t j, void *data) {
find_closest_t *d = (find_closest_t*)data;
if ( (d->type == (ISNAN(ubl.z_values[i][j]) ? INVALID : REAL))
if ( (d->type == (isnan(ubl.z_values[i][j]) ? INVALID : REAL))
|| (d->type == SET_IN_BITMAP && !d->done_flags->marked(i, j))
) {
// Found a Mesh Point of the specified type!
Expand Down Expand Up @@ -1326,7 +1326,7 @@ mesh_index_pair unified_bed_leveling::find_closest_mesh_point_of_type(const Mesh
float best_so_far = 99999.99f;

GRID_LOOP(i, j) {
if ( (type == (ISNAN(z_values[i][j]) ? INVALID : REAL))
if ( (type == (isnan(z_values[i][j]) ? INVALID : REAL))
|| (type == SET_IN_BITMAP && !done_flags->marked(i, j))
) {
// Found a Mesh Point of the specified type!
Expand Down Expand Up @@ -1367,12 +1367,12 @@ mesh_index_pair unified_bed_leveling::find_closest_mesh_point_of_type(const Mesh

bool unified_bed_leveling::smart_fill_one(const uint8_t x, const uint8_t y, const int8_t xdir, const int8_t ydir) {
const float v = z_values[x][y];
if (ISNAN(v)) { // A NAN...
if (isnan(v)) { // A NAN...
const int8_t dx = x + xdir, dy = y + ydir;
const float v1 = z_values[dx][dy];
if (!ISNAN(v1)) { // ...next to a pair of real values?
if (!isnan(v1)) { // ...next to a pair of real values?
const float v2 = z_values[dx + xdir][dy + ydir];
if (!ISNAN(v2)) {
if (!isnan(v2)) {
z_values[x][y] = v1 < v2 ? v1 : v1 + v1 - v2;
TERN_(EXTENSIBLE_UI, ExtUI::onMeshUpdate(x, y, z_values[x][y]));
return true;
Expand Down Expand Up @@ -1441,7 +1441,7 @@ void unified_bed_leveling::smart_fill_mesh() {
TERN_(HAS_STATUS_MESSAGE, ui.status_printf_P(0, PSTR(S_FMT " 1/3"), GET_TEXT(MSG_LCD_TILTING_MESH)));

measured_z = probe.probe_at_point(points[0], PROBE_PT_RAISE, param.V_verbosity);
if (ISNAN(measured_z))
if (isnan(measured_z))
abort_flag = true;
else {
measured_z -= get_z_correction(points[0]);
Expand All @@ -1463,7 +1463,7 @@ void unified_bed_leveling::smart_fill_mesh() {
#ifdef VALIDATE_MESH_TILT
z2 = measured_z;
#endif
if (ISNAN(measured_z))
if (isnan(measured_z))
abort_flag = true;
else {
measured_z -= get_z_correction(points[1]);
Expand All @@ -1483,7 +1483,7 @@ void unified_bed_leveling::smart_fill_mesh() {
#ifdef VALIDATE_MESH_TILT
z3 = measured_z;
#endif
if (ISNAN(measured_z))
if (isnan(measured_z))
abort_flag = true;
else {
measured_z -= get_z_correction(points[2]);
Expand Down Expand Up @@ -1522,7 +1522,7 @@ void unified_bed_leveling::smart_fill_mesh() {

measured_z = probe.probe_at_point(rpos, parser.seen('E') ? PROBE_PT_STOW : PROBE_PT_RAISE, param.V_verbosity); // TODO: Needs error handling

abort_flag = ISNAN(measured_z);
abort_flag = isnan(measured_z);

#if ENABLED(DEBUG_LEVELING_FEATURE)
if (DEBUGGING(LEVELING)) {
Expand Down Expand Up @@ -1673,14 +1673,14 @@ void unified_bed_leveling::smart_fill_mesh() {

const float weight_scaled = weight_factor * _MAX(MESH_X_DIST, MESH_Y_DIST);

GRID_LOOP(jx, jy) if (!ISNAN(z_values[jx][jy])) SBI(bitmap[jx], jy);
GRID_LOOP(jx, jy) if (!isnan(z_values[jx][jy])) SBI(bitmap[jx], jy);

xy_pos_t ppos;
LOOP_L_N(ix, GRID_MAX_POINTS_X) {
ppos.x = mesh_index_to_xpos(ix);
LOOP_L_N(iy, GRID_MAX_POINTS_Y) {
ppos.y = mesh_index_to_ypos(iy);
if (ISNAN(z_values[ix][iy])) {
if (isnan(z_values[ix][iy])) {
// undefined mesh point at (ppos.x,ppos.y), compute weighted LSF from original valid mesh points.
incremental_LSF_reset(&lsf_results);
xy_pos_t rpos;
Expand Down
18 changes: 9 additions & 9 deletions Marlin/src/feature/bedlevel/ubl/ubl_motion.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@

// Undefined parts of the Mesh in z_values[][] are NAN.
// Replace NAN corrections with 0.0 to prevent NAN propagation.
if (!ISNAN(z0)) end.z += z0;
if (!isnan(z0)) end.z += z0;
planner.buffer_segment(end, scaled_fr_mm_s, extruder);
current_position = destination;
return;
Expand Down Expand Up @@ -150,7 +150,7 @@

// Undefined parts of the Mesh in z_values[][] are NAN.
// Replace NAN corrections with 0.0 to prevent NAN propagation.
if (ISNAN(z0)) z0 = 0.0;
if (isnan(z0)) z0 = 0.0;

const float ry = mesh_index_to_ypos(icell.y);

Expand Down Expand Up @@ -198,7 +198,7 @@

// Undefined parts of the Mesh in z_values[][] are NAN.
// Replace NAN corrections with 0.0 to prevent NAN propagation.
if (ISNAN(z0)) z0 = 0.0;
if (isnan(z0)) z0 = 0.0;

/**
* Without this check, it's possible to generate a zero length move, as in the case where
Expand Down Expand Up @@ -253,7 +253,7 @@

// Undefined parts of the Mesh in z_values[][] are NAN.
// Replace NAN corrections with 0.0 to prevent NAN propagation.
if (ISNAN(z0)) z0 = 0.0;
if (isnan(z0)) z0 = 0.0;

if (!inf_normalized_flag) {
on_axis_distance = use_x_dist ? rx - start.x : next_mesh_line_y - start.y;
Expand All @@ -276,7 +276,7 @@

// Undefined parts of the Mesh in z_values[][] are NAN.
// Replace NAN corrections with 0.0 to prevent NAN propagation.
if (ISNAN(z0)) z0 = 0.0;
if (isnan(z0)) z0 = 0.0;

if (!inf_normalized_flag) {
on_axis_distance = use_x_dist ? next_mesh_line_x - start.x : ry - start.y;
Expand Down Expand Up @@ -405,10 +405,10 @@
z_x0y1 = z_values[icell.x ][icell.y+1], // z at lower right corner
z_x1y1 = z_values[icell.x+1][icell.y+1]; // z at upper right corner

if (ISNAN(z_x0y0)) z_x0y0 = 0; // ideally activating planner.leveling_active (G29 A)
if (ISNAN(z_x1y0)) z_x1y0 = 0; // should refuse if any invalid mesh points
if (ISNAN(z_x0y1)) z_x0y1 = 0; // in order to avoid ISNAN tests per cell,
if (ISNAN(z_x1y1)) z_x1y1 = 0; // thus guessing zero for undefined points
if (isnan(z_x0y0)) z_x0y0 = 0; // ideally activating planner.leveling_active (G29 A)
if (isnan(z_x1y0)) z_x1y0 = 0; // should refuse if any invalid mesh points
if (isnan(z_x0y1)) z_x0y1 = 0; // in order to avoid isnan tests per cell,
if (isnan(z_x1y1)) z_x1y1 = 0; // thus guessing zero for undefined points

const xy_pos_t pos = { mesh_index_to_xpos(icell.x), mesh_index_to_ypos(icell.y) };
xy_pos_t cell = raw - pos;
Expand Down
2 changes: 1 addition & 1 deletion Marlin/src/gcode/bedlevel/G35.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ void GcodeSuite::G35() {
do_blocking_move_to_z(SUM_TERN(BLTOUCH_HS_MODE, Z_CLEARANCE_BETWEEN_PROBES, 7));
const float z_probed_height = probe.probe_at_point(screws_tilt_adjust_pos[i], PROBE_PT_RAISE, 0, true);

if (ISNAN(z_probed_height)) {
if (isnan(z_probed_height)) {
SERIAL_ECHOPAIR("G35 failed at point ", i, " (");
SERIAL_ECHOPGM_P((char *)pgm_read_ptr(&tramming_point_name[i]));
SERIAL_CHAR(')');
Expand Down
Loading

0 comments on commit 528b9bd

Please sign in to comment.