1 |
ron |
1 |
/**
|
|
|
2 |
* Marlin 3D Printer Firmware
|
|
|
3 |
* Copyright (C) 2016 MarlinFirmware [https://github.com/MarlinFirmware/Marlin]
|
|
|
4 |
*
|
|
|
5 |
* Based on Sprinter and grbl.
|
|
|
6 |
* Copyright (C) 2011 Camiel Gubbels / Erik van der Zalm
|
|
|
7 |
*
|
|
|
8 |
* This program is free software: you can redistribute it and/or modify
|
|
|
9 |
* it under the terms of the GNU General Public License as published by
|
|
|
10 |
* the Free Software Foundation, either version 3 of the License, or
|
|
|
11 |
* (at your option) any later version.
|
|
|
12 |
*
|
|
|
13 |
* This program is distributed in the hope that it will be useful,
|
|
|
14 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
15 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
16 |
* GNU General Public License for more details.
|
|
|
17 |
*
|
|
|
18 |
* You should have received a copy of the GNU General Public License
|
|
|
19 |
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
20 |
*
|
|
|
21 |
*/
|
|
|
22 |
|
|
|
23 |
/**
|
|
|
24 |
* Incremental Least Squares Best Fit By Roxy and Ed Williams
|
|
|
25 |
*
|
|
|
26 |
* This algorithm is high speed and has a very small code footprint.
|
|
|
27 |
* Its results are identical to both the Iterative Least-Squares published
|
|
|
28 |
* earlier by Roxy and the QR_SOLVE solution. If used in place of QR_SOLVE
|
|
|
29 |
* it saves roughly 10K of program memory. And even better... the data
|
|
|
30 |
* fed into the algorithm does not need to all be present at the same time.
|
|
|
31 |
* A point can be probed and its values fed into the algorithm and then discarded.
|
|
|
32 |
*
|
|
|
33 |
*/
|
|
|
34 |
|
|
|
35 |
#include "MarlinConfig.h"
|
|
|
36 |
|
|
|
37 |
#if ENABLED(AUTO_BED_LEVELING_UBL) || ENABLED(AUTO_BED_LEVELING_LINEAR)
|
|
|
38 |
|
|
|
39 |
#include "Marlin.h"
|
|
|
40 |
#include "macros.h"
|
|
|
41 |
#include <math.h>
|
|
|
42 |
|
|
|
43 |
struct linear_fit_data {
|
|
|
44 |
float xbar, ybar, zbar,
|
|
|
45 |
x2bar, y2bar, z2bar,
|
|
|
46 |
xybar, xzbar, yzbar,
|
|
|
47 |
max_absx, max_absy,
|
|
|
48 |
A, B, D, N;
|
|
|
49 |
};
|
|
|
50 |
|
|
|
51 |
void inline incremental_LSF_reset(struct linear_fit_data *lsf) {
|
|
|
52 |
memset(lsf, 0, sizeof(linear_fit_data));
|
|
|
53 |
}
|
|
|
54 |
|
|
|
55 |
void inline incremental_WLSF(struct linear_fit_data *lsf, const float &x, const float &y, const float &z, const float &w) {
|
|
|
56 |
// weight each accumulator by factor w, including the "number" of samples
|
|
|
57 |
// (analagous to calling inc_LSF twice with same values to weight it by 2X)
|
|
|
58 |
lsf->xbar += w * x;
|
|
|
59 |
lsf->ybar += w * y;
|
|
|
60 |
lsf->zbar += w * z;
|
|
|
61 |
lsf->x2bar += w * x * x; // don't use sq(x) -- let compiler re-use w*x four times
|
|
|
62 |
lsf->y2bar += w * y * y;
|
|
|
63 |
lsf->z2bar += w * z * z;
|
|
|
64 |
lsf->xybar += w * x * y;
|
|
|
65 |
lsf->xzbar += w * x * z;
|
|
|
66 |
lsf->yzbar += w * y * z;
|
|
|
67 |
lsf->N += w;
|
|
|
68 |
lsf->max_absx = MAX(ABS(w * x), lsf->max_absx);
|
|
|
69 |
lsf->max_absy = MAX(ABS(w * y), lsf->max_absy);
|
|
|
70 |
}
|
|
|
71 |
|
|
|
72 |
void inline incremental_LSF(struct linear_fit_data *lsf, const float &x, const float &y, const float &z) {
|
|
|
73 |
lsf->xbar += x;
|
|
|
74 |
lsf->ybar += y;
|
|
|
75 |
lsf->zbar += z;
|
|
|
76 |
lsf->x2bar += sq(x);
|
|
|
77 |
lsf->y2bar += sq(y);
|
|
|
78 |
lsf->z2bar += sq(z);
|
|
|
79 |
lsf->xybar += x * y;
|
|
|
80 |
lsf->xzbar += x * z;
|
|
|
81 |
lsf->yzbar += y * z;
|
|
|
82 |
lsf->max_absx = MAX(ABS(x), lsf->max_absx);
|
|
|
83 |
lsf->max_absy = MAX(ABS(y), lsf->max_absy);
|
|
|
84 |
lsf->N += 1.0;
|
|
|
85 |
}
|
|
|
86 |
|
|
|
87 |
int finish_incremental_LSF(struct linear_fit_data *);
|
|
|
88 |
|
|
|
89 |
#endif
|
|
|
90 |
|