Skip to content

Commit 9af67e2

Browse files
authored
Save 7714 bytes of program memory when doing AUTO_BED_LEVELING_LINEAR (MarlinFirmware#7276)
We can save more and a pile of RAM by eleminating the eqnBVector and EqnAMatrix arrays next.
1 parent e5904c4 commit 9af67e2

File tree

5 files changed

+28
-1653
lines changed

5 files changed

+28
-1653
lines changed

Marlin/Marlin_main.cpp

Lines changed: 25 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -261,7 +261,7 @@
261261
#if HAS_ABL
262262
#include "vector_3.h"
263263
#if ENABLED(AUTO_BED_LEVELING_LINEAR)
264-
#include "qr_solve.h"
264+
#include "least_squares_fit.h"
265265
#endif
266266
#elif ENABLED(MESH_BED_LEVELING)
267267
#include "mesh_bed_leveling.h"
@@ -4336,8 +4336,8 @@ void home_all_axes() { gcode_G28(true); }
43364336
ABL_VAR int indexIntoAB[GRID_MAX_POINTS_X][GRID_MAX_POINTS_Y];
43374337

43384338
ABL_VAR float eqnAMatrix[GRID_MAX_POINTS * 3], // "A" matrix of the linear system of equations
4339-
eqnBVector[GRID_MAX_POINTS], // "B" vector of Z points
4340-
mean;
4339+
eqnBVector[GRID_MAX_POINTS], // "B" vector of Z points
4340+
mean;
43414341
#endif
43424342

43434343
#elif ENABLED(AUTO_BED_LEVELING_3POINT)
@@ -4353,6 +4353,11 @@ void home_all_axes() { gcode_G28(true); }
43534353

43544354
#endif // AUTO_BED_LEVELING_3POINT
43554355

4356+
#if ENABLED(AUTO_BED_LEVELING_LINEAR)
4357+
struct linear_fit_data lsf_results;
4358+
incremental_LSF_reset(&lsf_results);
4359+
#endif
4360+
43564361
/**
43574362
* On the initial G29 fetch command parameters.
43584363
*/
@@ -4549,11 +4554,7 @@ void home_all_axes() { gcode_G28(true); }
45494554
abl_should_enable = false;
45504555
}
45514556

4552-
#elif ENABLED(AUTO_BED_LEVELING_LINEAR)
4553-
4554-
mean = 0.0;
4555-
4556-
#endif // AUTO_BED_LEVELING_LINEAR
4557+
#endif // AUTO_BED_LEVELING_BILINEAR
45574558

45584559
#if ENABLED(AUTO_BED_LEVELING_3POINT)
45594560

@@ -4616,11 +4617,11 @@ void home_all_axes() { gcode_G28(true); }
46164617

46174618
#if ENABLED(AUTO_BED_LEVELING_LINEAR)
46184619

4619-
mean += measured_z;
4620-
eqnBVector[abl_probe_index] = measured_z;
4621-
eqnAMatrix[abl_probe_index + 0 * abl2] = xProbe;
4622-
eqnAMatrix[abl_probe_index + 1 * abl2] = yProbe;
4623-
eqnAMatrix[abl_probe_index + 2 * abl2] = 1;
4620+
// mean += measured_z; // I believe this is unused code?
4621+
// eqnBVector[abl_probe_index] = measured_z; // I believe this is unused code?
4622+
// eqnAMatrix[abl_probe_index + 0 * abl2] = xProbe; // I believe this is unused code?
4623+
// eqnAMatrix[abl_probe_index + 1 * abl2] = yProbe; // I believe this is unused code?
4624+
// eqnAMatrix[abl_probe_index + 2 * abl2] = 1; // I believe this is unused code?
46244625

46254626
#elif ENABLED(AUTO_BED_LEVELING_BILINEAR)
46264627

@@ -4794,6 +4795,11 @@ void home_all_axes() { gcode_G28(true); }
47944795
eqnAMatrix[abl_probe_index + 1 * abl2] = yProbe;
47954796
eqnAMatrix[abl_probe_index + 2 * abl2] = 1;
47964797

4798+
incremental_LSF(&lsf_results, xProbe, yProbe, measured_z);
4799+
4800+
#if ENABLED(AUTO_BED_LEVELING_LINEAR)
4801+
indexIntoAB[xCount][yCount] = abl_probe_index;
4802+
#endif
47974803
#elif ENABLED(AUTO_BED_LEVELING_BILINEAR)
47984804

47994805
z_values[xCount][yCount] = measured_z + zoffset;
@@ -4894,7 +4900,11 @@ void home_all_axes() { gcode_G28(true); }
48944900
* so Vx = -a Vy = -b Vz = 1 (we want the vector facing towards positive Z
48954901
*/
48964902
float plane_equation_coefficients[3];
4897-
qr_solve(plane_equation_coefficients, abl2, 3, eqnAMatrix, eqnBVector);
4903+
4904+
finish_incremental_LSF(&lsf_results);
4905+
plane_equation_coefficients[0] = -lsf_results.A; // We should be able to eliminate the '-' on these three lines and down below
4906+
plane_equation_coefficients[1] = -lsf_results.B; // but that is not yet tested.
4907+
plane_equation_coefficients[2] = -lsf_results.D;
48984908

48994909
mean /= abl2;
49004910

@@ -4916,7 +4926,7 @@ void home_all_axes() { gcode_G28(true); }
49164926
// Create the matrix but don't correct the position yet
49174927
if (!dryrun) {
49184928
planner.bed_level_matrix = matrix_3x3::create_look_at(
4919-
vector_3(-plane_equation_coefficients[0], -plane_equation_coefficients[1], 1)
4929+
vector_3(-plane_equation_coefficients[0], -plane_equation_coefficients[1], 1) // We can eleminate the '-' here and up above
49204930
);
49214931
}
49224932

Marlin/least_squares_fit.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@
3434

3535
#include "MarlinConfig.h"
3636

37-
#if ENABLED(AUTO_BED_LEVELING_UBL) // Currently only used by UBL, but is applicable to Grid Based (Linear) Bed Leveling
37+
#if ENABLED(AUTO_BED_LEVELING_UBL) || ENABLED(AUTO_BED_LEVELING_LINEAR)
3838

3939
#include "macros.h"
4040
#include <math.h>
@@ -68,4 +68,4 @@ int finish_incremental_LSF(struct linear_fit_data *lsf) {
6868
return 0;
6969
}
7070

71-
#endif // AUTO_BED_LEVELING_UBL
71+
#endif // AUTO_BED_LEVELING_UBL || ENABLED(AUTO_BED_LEVELING_LINEAR)

Marlin/least_squares_fit.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@
3434

3535
#include "MarlinConfig.h"
3636

37-
#if ENABLED(AUTO_BED_LEVELING_UBL) // Currently only used by UBL, but is applicable to Grid Based (Linear) Bed Leveling
37+
#if ENABLED(AUTO_BED_LEVELING_UBL) || ENABLED(AUTO_BED_LEVELING_LINEAR)
3838

3939
#include "Marlin.h"
4040
#include "macros.h"

0 commit comments

Comments
 (0)