From 25c7c43a829e3bbc581aae20d24b724a7ad8fbdb Mon Sep 17 00:00:00 2001 From: Fabio Santos Date: Mon, 22 Jun 2020 19:12:45 -0700 Subject: [PATCH] Add SERIAL_FLOAT_PRECISION option (#18367) --- Marlin/Configuration_adv.h | 3 +++ Marlin/src/MarlinCore.cpp | 2 +- Marlin/src/core/debug_out.h | 3 +++ Marlin/src/core/macros.h | 2 +- Marlin/src/core/serial.cpp | 4 ++-- Marlin/src/core/serial.h | 6 ++++++ Marlin/src/core/utility.cpp | 4 ++-- Marlin/src/feature/bedlevel/ubl/ubl_G29.cpp | 2 +- Marlin/src/feature/encoder_i2c.cpp | 2 +- Marlin/src/feature/encoder_i2c.h | 4 ++-- Marlin/src/feature/powerloss.cpp | 6 +++--- Marlin/src/gcode/config/M92.cpp | 2 +- Marlin/src/gcode/feature/advance/M900.cpp | 2 +- Marlin/src/gcode/probe/G30.cpp | 2 +- Marlin/src/module/probe.cpp | 7 ++----- 15 files changed, 30 insertions(+), 21 deletions(-) diff --git a/Marlin/Configuration_adv.h b/Marlin/Configuration_adv.h index 15297f7e64..07736c91ca 100644 --- a/Marlin/Configuration_adv.h +++ b/Marlin/Configuration_adv.h @@ -1901,6 +1901,9 @@ // This option inserts short delays between lines of serial output. #define SERIAL_OVERRUN_PROTECTION +// For serial echo, the number of digits after the decimal point +//#define SERIAL_FLOAT_PRECISION 4 + // @section extras /** diff --git a/Marlin/src/MarlinCore.cpp b/Marlin/src/MarlinCore.cpp index aceb0af58f..744e637249 100644 --- a/Marlin/src/MarlinCore.cpp +++ b/Marlin/src/MarlinCore.cpp @@ -857,7 +857,7 @@ void setup() { #if ENABLED(MARLIN_DEV_MODE) auto log_current_ms = [&](PGM_P const msg) { SERIAL_ECHO_START(); - SERIAL_CHAR('['); SERIAL_ECHO(millis()); SERIAL_ECHO("] "); + SERIAL_CHAR('['); SERIAL_ECHO(millis()); SERIAL_ECHOPGM("] "); serialprintPGM(msg); SERIAL_EOL(); }; diff --git a/Marlin/src/core/debug_out.h b/Marlin/src/core/debug_out.h index b7506a8304..8be6523ca7 100644 --- a/Marlin/src/core/debug_out.h +++ b/Marlin/src/core/debug_out.h @@ -31,6 +31,7 @@ #undef DEBUG_ERROR_START #undef DEBUG_CHAR #undef DEBUG_ECHO +#undef DEBUG_DECIMAL #undef DEBUG_ECHO_F #undef DEBUG_ECHOLN #undef DEBUG_ECHOPGM @@ -57,6 +58,7 @@ #define DEBUG_ERROR_START SERIAL_ERROR_START #define DEBUG_CHAR SERIAL_CHAR #define DEBUG_ECHO SERIAL_ECHO + #define DEBUG_DECIMAL SERIAL_DECIMAL #define DEBUG_ECHO_F SERIAL_ECHO_F #define DEBUG_ECHOLN SERIAL_ECHOLN #define DEBUG_ECHOPGM SERIAL_ECHOPGM @@ -82,6 +84,7 @@ #define DEBUG_ERROR_START() NOOP #define DEBUG_CHAR(...) NOOP #define DEBUG_ECHO(...) NOOP + #define DEBUG_DECIMAL(...) NOOP #define DEBUG_ECHO_F(...) NOOP #define DEBUG_ECHOLN(...) NOOP #define DEBUG_ECHOPGM(...) NOOP diff --git a/Marlin/src/core/macros.h b/Marlin/src/core/macros.h index cf0fc2c5db..2f9273ec80 100644 --- a/Marlin/src/core/macros.h +++ b/Marlin/src/core/macros.h @@ -270,7 +270,7 @@ #define NEAR(x,y) NEAR_ZERO((x)-(y)) #define RECIPROCAL(x) (NEAR_ZERO(x) ? 0 : (1 / float(x))) -#define FIXFLOAT(f) ({__typeof__(f) _f = (f); _f + (_f < 0 ? -0.00005f : 0.00005f);}) +#define FIXFLOAT(f) ({__typeof__(f) _f = (f); _f + (_f < 0 ? -0.0000005f : 0.0000005f);}) // // Maths macros that can be overridden by HAL diff --git a/Marlin/src/core/serial.cpp b/Marlin/src/core/serial.cpp index 313d6ce20e..dee452d954 100644 --- a/Marlin/src/core/serial.cpp +++ b/Marlin/src/core/serial.cpp @@ -42,8 +42,8 @@ void serial_echopair_PGM(PGM_P const s_P, const char *v) { serialprintPGM(s_P) void serial_echopair_PGM(PGM_P const s_P, char v) { serialprintPGM(s_P); SERIAL_CHAR(v); } void serial_echopair_PGM(PGM_P const s_P, int v) { serialprintPGM(s_P); SERIAL_ECHO(v); } void serial_echopair_PGM(PGM_P const s_P, long v) { serialprintPGM(s_P); SERIAL_ECHO(v); } -void serial_echopair_PGM(PGM_P const s_P, float v) { serialprintPGM(s_P); SERIAL_ECHO(v); } -void serial_echopair_PGM(PGM_P const s_P, double v) { serialprintPGM(s_P); SERIAL_ECHO(v); } +void serial_echopair_PGM(PGM_P const s_P, float v) { serialprintPGM(s_P); SERIAL_DECIMAL(v); } +void serial_echopair_PGM(PGM_P const s_P, double v) { serialprintPGM(s_P); SERIAL_DECIMAL(v); } void serial_echopair_PGM(PGM_P const s_P, unsigned int v) { serialprintPGM(s_P); SERIAL_ECHO(v); } void serial_echopair_PGM(PGM_P const s_P, unsigned long v) { serialprintPGM(s_P); SERIAL_ECHO(v); } diff --git a/Marlin/src/core/serial.h b/Marlin/src/core/serial.h index be4c000b52..8b72e46a75 100644 --- a/Marlin/src/core/serial.h +++ b/Marlin/src/core/serial.h @@ -286,6 +286,12 @@ extern uint8_t marlin_debug_flags; #define SERIAL_ECHO_TERNARY(TF, PRE, ON, OFF, POST) serial_ternary(TF, PSTR(PRE), PSTR(ON), PSTR(OFF), PSTR(POST)) +#if SERIAL_FLOAT_PRECISION + #define SERIAL_DECIMAL(V) SERIAL_PRINT(V, SERIAL_FLOAT_PRECISION) +#else + #define SERIAL_DECIMAL(V) SERIAL_ECHO(V) +#endif + // // Functions for serial printing from PROGMEM. (Saves loads of SRAM.) // diff --git a/Marlin/src/core/utility.cpp b/Marlin/src/core/utility.cpp index 62e79e42ea..e64d6fb5e6 100644 --- a/Marlin/src/core/utility.cpp +++ b/Marlin/src/core/utility.cpp @@ -123,10 +123,10 @@ void safe_delay(millis_t ms) { #if ABL_PLANAR SERIAL_ECHOPGM("ABL Adjustment X"); LOOP_XYZ(a) { - float v = planner.get_axis_position_mm(AxisEnum(a)) - current_position[a]; + const float v = planner.get_axis_position_mm(AxisEnum(a)) - current_position[a]; SERIAL_CHAR(' ', XYZ_CHAR(a)); if (v > 0) SERIAL_CHAR('+'); - SERIAL_ECHO(v); + SERIAL_DECIMAL(v); } #else #if ENABLED(AUTO_BED_LEVELING_UBL) diff --git a/Marlin/src/feature/bedlevel/ubl/ubl_G29.cpp b/Marlin/src/feature/bedlevel/ubl/ubl_G29.cpp index 95addd6c12..3c604168a6 100644 --- a/Marlin/src/feature/bedlevel/ubl/ubl_G29.cpp +++ b/Marlin/src/feature/bedlevel/ubl/ubl_G29.cpp @@ -433,7 +433,7 @@ if (g29_verbose_level > 1) { SERIAL_ECHOPAIR("Probing around (", g29_pos.x); SERIAL_CHAR(','); - SERIAL_ECHO(g29_pos.y); + SERIAL_DECIMAL(g29_pos.y); SERIAL_ECHOLNPGM(").\n"); } const xy_pos_t near_probe_xy = g29_pos + probe.offset_xy; diff --git a/Marlin/src/feature/encoder_i2c.cpp b/Marlin/src/feature/encoder_i2c.cpp index cfac36a5d1..d5a8f168db 100644 --- a/Marlin/src/feature/encoder_i2c.cpp +++ b/Marlin/src/feature/encoder_i2c.cpp @@ -107,7 +107,7 @@ void I2CPositionEncoder::update() { SERIAL_ECHOLNPAIR("New zero-offset of ", zeroOffset); SERIAL_ECHOPAIR("New position reads as ", get_position()); SERIAL_CHAR('('); - SERIAL_ECHO(mm_from_count(get_position())); + SERIAL_DECIMAL(mm_from_count(get_position())); SERIAL_ECHOLNPGM(")"); #endif } diff --git a/Marlin/src/feature/encoder_i2c.h b/Marlin/src/feature/encoder_i2c.h index 0665ee809d..f404954cae 100644 --- a/Marlin/src/feature/encoder_i2c.h +++ b/Marlin/src/feature/encoder_i2c.h @@ -280,13 +280,13 @@ class I2CPositionEncodersMgr { static void set_ec_threshold(const int8_t idx, const float newThreshold, const AxisEnum axis) { CHECK_IDX(); encoders[idx].set_ec_threshold(newThreshold); - SERIAL_ECHOLNPAIR("Error correct threshold for ", axis_codes[axis], " axis set to ", FIXFLOAT(newThreshold), "mm."); + SERIAL_ECHOLNPAIR("Error correct threshold for ", axis_codes[axis], " axis set to ", newThreshold, "mm."); } static void get_ec_threshold(const int8_t idx, const AxisEnum axis) { CHECK_IDX(); const float threshold = encoders[idx].get_ec_threshold(); - SERIAL_ECHOLNPAIR("Error correct threshold for ", axis_codes[axis], " axis is ", FIXFLOAT(threshold), "mm."); + SERIAL_ECHOLNPAIR("Error correct threshold for ", axis_codes[axis], " axis is ", threshold, "mm."); } static int8_t idx_from_axis(const AxisEnum axis) { diff --git a/Marlin/src/feature/powerloss.cpp b/Marlin/src/feature/powerloss.cpp index 12de2d26ff..0ae1280213 100644 --- a/Marlin/src/feature/powerloss.cpp +++ b/Marlin/src/feature/powerloss.cpp @@ -472,7 +472,7 @@ void PrintJobRecovery::resume() { DEBUG_ECHOPGM("current_position: "); LOOP_XYZE(i) { if (i) DEBUG_CHAR(','); - DEBUG_ECHO(info.current_position[i]); + DEBUG_DECIMAL(info.current_position[i]); } DEBUG_EOL(); @@ -480,7 +480,7 @@ void PrintJobRecovery::resume() { DEBUG_ECHOPGM("home_offset: "); LOOP_XYZ(i) { if (i) DEBUG_CHAR(','); - DEBUG_ECHO(info.home_offset[i]); + DEBUG_DECIMAL(info.home_offset[i]); } DEBUG_EOL(); #endif @@ -489,7 +489,7 @@ void PrintJobRecovery::resume() { DEBUG_ECHOPGM("position_shift: "); LOOP_XYZ(i) { if (i) DEBUG_CHAR(','); - DEBUG_ECHO(info.position_shift[i]); + DEBUG_DECIMAL(info.position_shift[i]); } DEBUG_EOL(); #endif diff --git a/Marlin/src/gcode/config/M92.cpp b/Marlin/src/gcode/config/M92.cpp index bc86ff4d32..4f7279fc34 100644 --- a/Marlin/src/gcode/config/M92.cpp +++ b/Marlin/src/gcode/config/M92.cpp @@ -105,7 +105,7 @@ void GcodeSuite::M92() { if (wanted) { const float best = uint16_t(wanted / z_full_step_mm) * z_full_step_mm; SERIAL_ECHOPAIR(", best:[", best); - if (best != wanted) { SERIAL_CHAR(','); SERIAL_ECHO(best + z_full_step_mm); } + if (best != wanted) { SERIAL_CHAR(','); SERIAL_DECIMAL(best + z_full_step_mm); } SERIAL_CHAR(']'); } SERIAL_ECHOLNPGM(" }"); diff --git a/Marlin/src/gcode/feature/advance/M900.cpp b/Marlin/src/gcode/feature/advance/M900.cpp index b3985401cf..683f6fff1b 100644 --- a/Marlin/src/gcode/feature/advance/M900.cpp +++ b/Marlin/src/gcode/feature/advance/M900.cpp @@ -134,7 +134,7 @@ void GcodeSuite::M900() { SERIAL_ECHOPGM("Advance K"); LOOP_L_N(i, EXTRUDERS) { SERIAL_CHAR(' ', '0' + i, ':'); - SERIAL_ECHO(planner.extruder_advance_K[i]); + SERIAL_DECIMAL(planner.extruder_advance_K[i]); } SERIAL_EOL(); #endif diff --git a/Marlin/src/gcode/probe/G30.cpp b/Marlin/src/gcode/probe/G30.cpp index 5a01289dea..0d8049158c 100644 --- a/Marlin/src/gcode/probe/G30.cpp +++ b/Marlin/src/gcode/probe/G30.cpp @@ -53,7 +53,7 @@ void GcodeSuite::G30() { const ProbePtRaise raise_after = parser.boolval('E', true) ? PROBE_PT_STOW : PROBE_PT_NONE; const float measured_z = probe.probe_at_point(pos, raise_after, 1); if (!isnan(measured_z)) - SERIAL_ECHOLNPAIR("Bed X: ", FIXFLOAT(pos.x), " Y: ", FIXFLOAT(pos.y), " Z: ", FIXFLOAT(measured_z)); + SERIAL_ECHOLNPAIR("Bed X: ", pos.x, " Y: ", pos.y, " Z: ", measured_z); restore_feedrate_and_scaling(); diff --git a/Marlin/src/module/probe.cpp b/Marlin/src/module/probe.cpp index 6a75ef4685..08aa53fc96 100644 --- a/Marlin/src/module/probe.cpp +++ b/Marlin/src/module/probe.cpp @@ -715,11 +715,8 @@ float Probe::probe_at_point(const float &rx, const float &ry, const ProbePtRaise else if (raise_after == PROBE_PT_STOW) if (stow()) measured_z = NAN; // Error on stow? - if (verbose_level > 2) { - SERIAL_ECHOPAIR_F("Bed X: ", LOGICAL_X_POSITION(rx), 3); - SERIAL_ECHOPAIR_F( " Y: ", LOGICAL_Y_POSITION(ry), 3); - SERIAL_ECHOLNPAIR_F( " Z: ", measured_z, 3); - } + if (verbose_level > 2) + SERIAL_ECHOLNPAIR("Bed X: ", LOGICAL_X_POSITION(rx), " Y: ", LOGICAL_Y_POSITION(ry), " Z: ", measured_z); } feedrate_mm_s = old_feedrate_mm_s;