From 6b12be42d943d6541470c2d33c0aa3be170244d8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gu=C3=B0ni=20M=C3=A1r=20Gilbert?= Date: Sat, 6 May 2023 20:34:38 +0000 Subject: [PATCH] optimisation: Make BED_X and BED_Y into functions Previously these preprocessor macros were always being inlined. By making these into a function we can control the inlining more directly. The number of points on the mesh is also now constant. This means 'n' can now be float at compile time. This removes one uint8_t to float conversion. Change in memory: Flash: -208 bytes SRAM: 0 bytes --- Firmware/Marlin_main.cpp | 8 ++++---- Firmware/mesh_bed_calibration.cpp | 22 ++++++++++++++++------ Firmware/mesh_bed_calibration.h | 14 +++++++++++--- 3 files changed, 31 insertions(+), 13 deletions(-) diff --git a/Firmware/Marlin_main.cpp b/Firmware/Marlin_main.cpp index 41511e6f6..1020b2ee1 100644 --- a/Firmware/Marlin_main.cpp +++ b/Firmware/Marlin_main.cpp @@ -2865,8 +2865,8 @@ static void gcode_G80() if (!isOn3x3Mesh) continue; } else { - const float x_pos = BED_X(col, MESH_NUM_X_POINTS); - const float y_pos = BED_Y(row, MESH_NUM_Y_POINTS); + const float x_pos = BED_X(col); + const float y_pos = BED_Y(row); if ((x_pos < area_min_x || x_pos > area_max_x || y_pos < area_min_y || y_pos > area_max_y) && (!isOn3x3Mesh || has_z)) { continue; } @@ -2898,8 +2898,8 @@ static void gcode_G80() uint8_t iy = mesh_point / MESH_NUM_X_POINTS; if (iy & 1) ix = (MESH_NUM_X_POINTS - 1) - ix; // Zig zag bool isOn3x3Mesh = ((ix % 3 == 0) && (iy % 3 == 0)); - float x_pos = BED_X(ix, MESH_NUM_X_POINTS); - float y_pos = BED_Y(iy, MESH_NUM_Y_POINTS); + float x_pos = BED_X(ix); + float y_pos = BED_Y(iy); if ((nMeasPoints == 3) && !isOn3x3Mesh) { mesh_point++; diff --git a/Firmware/mesh_bed_calibration.cpp b/Firmware/mesh_bed_calibration.cpp index fbe9fdc03..4808a66f1 100644 --- a/Firmware/mesh_bed_calibration.cpp +++ b/Firmware/mesh_bed_calibration.cpp @@ -2184,6 +2184,16 @@ inline void scan_bed_induction_sensor_point() #define MESH_BED_CALIBRATION_SHOW_LCD +float __attribute__((noinline)) BED_X(const uint8_t col) +{ + return ((float)col * (BED_Xn - BED_X0) / (MESH_NUM_X_POINTS - 1) + BED_X0); +} + +float __attribute__((noinline)) BED_Y(const uint8_t row) +{ + return ((float)row * (BED_Yn - BED_Y0) / (MESH_NUM_Y_POINTS - 1) + BED_Y0); +} + BedSkewOffsetDetectionResultType find_bed_offset_and_skew(int8_t verbosity_level, uint8_t &too_far_mask) { // Don't let the manage_inactivity() function remove power from the motors. @@ -2481,8 +2491,8 @@ BedSkewOffsetDetectionResultType find_bed_offset_and_skew(int8_t verbosity_level uint8_t ix = mesh_point % MESH_MEAS_NUM_X_POINTS; // from 0 to MESH_NUM_X_POINTS - 1 uint8_t iy = mesh_point / MESH_MEAS_NUM_X_POINTS; if (iy & 1) ix = (MESH_MEAS_NUM_X_POINTS - 1) - ix; - current_position[X_AXIS] = BED_X(ix, MESH_MEAS_NUM_X_POINTS); - current_position[Y_AXIS] = BED_Y(iy, MESH_MEAS_NUM_Y_POINTS); + current_position[X_AXIS] = BED_X(ix); + current_position[Y_AXIS] = BED_Y(iy); go_to_current(homing_feedrate[X_AXIS] / 60); delay_keep_alive(3000); } @@ -2884,8 +2894,8 @@ bool sample_mesh_and_store_reference() uint8_t ix = mesh_point % MESH_MEAS_NUM_X_POINTS; uint8_t iy = mesh_point / MESH_MEAS_NUM_X_POINTS; if (iy & 1) ix = (MESH_MEAS_NUM_X_POINTS - 1) - ix; // Zig zag - current_position[X_AXIS] = BED_X(ix, MESH_MEAS_NUM_X_POINTS); - current_position[Y_AXIS] = BED_Y(iy, MESH_MEAS_NUM_Y_POINTS); + current_position[X_AXIS] = BED_X(ix); + current_position[Y_AXIS] = BED_Y(iy); world2machine_clamp(current_position[X_AXIS], current_position[Y_AXIS]); go_to_current(homing_feedrate[X_AXIS]/60); #ifdef MESH_BED_CALIBRATION_SHOW_LCD @@ -3003,8 +3013,8 @@ bool scan_bed_induction_points(int8_t verbosity_level) uint8_t ix = mesh_point % MESH_MEAS_NUM_X_POINTS; // from 0 to MESH_NUM_X_POINTS - 1 uint8_t iy = mesh_point / MESH_MEAS_NUM_X_POINTS; if (iy & 1) ix = (MESH_MEAS_NUM_X_POINTS - 1) - ix; - float bedX = BED_X(ix, MESH_MEAS_NUM_X_POINTS); - float bedY = BED_Y(iy, MESH_MEAS_NUM_Y_POINTS); + float bedX = BED_X(ix); + float bedY = BED_Y(iy); current_position[X_AXIS] = vec_x[0] * bedX + vec_y[0] * bedY + cntr[0]; current_position[Y_AXIS] = vec_x[1] * bedX + vec_y[1] * bedY + cntr[1]; // The calibration points are very close to the min Y. diff --git a/Firmware/mesh_bed_calibration.h b/Firmware/mesh_bed_calibration.h index 41f81016a..c6c7c87e7 100644 --- a/Firmware/mesh_bed_calibration.h +++ b/Firmware/mesh_bed_calibration.h @@ -21,9 +21,6 @@ #endif //not HEATBED_V2 -#define BED_X(i, n) ((float)i * (BED_Xn - BED_X0) / (n - 1) + BED_X0) -#define BED_Y(i, n) ((float)i * (BED_Yn - BED_Y0) / (n - 1) + BED_Y0) - // Exact positions of the print head above the bed reference points, in the world coordinates. // The world coordinates match the machine coordinates only in case, when the machine // is built properly, the end stops are at the correct positions and the axes are perpendicular. @@ -145,6 +142,17 @@ inline bool world2machine_clamp(float &x, float &y) machine2world(tmpx, tmpy, x, y); return clamped; } + +/// @brief For a given column on the mesh calculate the bed X coordinate +/// @param col column index on mesh +/// @return Bed X coordinate +float BED_X(const uint8_t col); + +/// @brief For a given row on the mesh calculate the bed Y coordinate +/// @param row row index on mesh +/// @return Bed Y coordinate +float BED_Y(const uint8_t row); + /** * @brief Bed skew and offest detection result *