Prusa MINI Firmware overview
least_squares_fit.h
Go to the documentation of this file.
1 /**
2  * Marlin 3D Printer Firmware
3  * Copyright (c) 2019 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 #pragma once
23 
24 /**
25  * Incremental Least Squares Best Fit By Roxy and Ed Williams
26  *
27  * This algorithm is high speed and has a very small code footprint.
28  * Its results are identical to both the Iterative Least-Squares published
29  * earlier by Roxy and the QR_SOLVE solution. If used in place of QR_SOLVE
30  * it saves roughly 10K of program memory. And even better... the data
31  * fed into the algorithm does not need to all be present at the same time.
32  * A point can be probed and its values fed into the algorithm and then discarded.
33  *
34  */
35 
36 #include "../inc/MarlinConfig.h"
37 #include <math.h>
38 
40  float xbar, ybar, zbar,
41  x2bar, y2bar, z2bar,
42  xybar, xzbar, yzbar,
44  A, B, D, N;
45 };
46 
47 inline void incremental_LSF_reset(struct linear_fit_data *lsf) {
48  memset(lsf, 0, sizeof(linear_fit_data));
49 }
50 
51 inline void incremental_WLSF(struct linear_fit_data *lsf, const float &x, const float &y, const float &z, const float &w) {
52  // weight each accumulator by factor w, including the "number" of samples
53  // (analogous to calling inc_LSF twice with same values to weight it by 2X)
54  const float wx = w * x, wy = w * y, wz = w * z;
55  lsf->xbar += wx;
56  lsf->ybar += wy;
57  lsf->zbar += wz;
58  lsf->x2bar += wx * x;
59  lsf->y2bar += wy * y;
60  lsf->z2bar += wz * z;
61  lsf->xybar += wx * y;
62  lsf->xzbar += wx * z;
63  lsf->yzbar += wy * z;
64  lsf->N += w;
65  lsf->max_absx = _MAX(ABS(wx), lsf->max_absx);
66  lsf->max_absy = _MAX(ABS(wy), lsf->max_absy);
67 }
68 inline void incremental_WLSF(struct linear_fit_data *lsf, const xy_pos_t &pos, const float &z, const float &w) {
69  incremental_WLSF(lsf, pos.x, pos.y, z, w);
70 }
71 
72 inline void 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 inline void incremental_LSF(struct linear_fit_data *lsf, const xy_pos_t &pos, const float &z) {
87  incremental_LSF(lsf, pos.x, pos.y, z);
88 }
89 
incremental_LSF
void incremental_LSF(struct linear_fit_data *lsf, const float &x, const float &y, const float &z)
Definition: least_squares_fit.h:72
linear_fit_data::D
float D
Definition: least_squares_fit.h:40
sq
#define sq(x)
Definition: wiring_constants.h:83
linear_fit_data::xbar
float xbar
Definition: least_squares_fit.h:40
linear_fit_data::ybar
float ybar
Definition: least_squares_fit.h:40
_MAX
#define _MAX(V...)
Definition: macros.h:346
incremental_LSF_reset
void incremental_LSF_reset(struct linear_fit_data *lsf)
Definition: least_squares_fit.h:47
linear_fit_data::xybar
float xybar
Definition: least_squares_fit.h:40
linear_fit_data::zbar
float zbar
Definition: least_squares_fit.h:40
linear_fit_data
Definition: least_squares_fit.h:39
linear_fit_data::B
float B
Definition: least_squares_fit.h:40
math.h
ABS
#define ABS(a)
Definition: macros.h:266
least_squares_fit.h
linear_fit_data::x2bar
float x2bar
Definition: least_squares_fit.h:40
XYval
Definition: types.h:99
linear_fit_data::A
float A
Definition: least_squares_fit.h:40
XYval::x
T x
Definition: types.h:185
finish_incremental_LSF
int finish_incremental_LSF(struct linear_fit_data *lsf)
Definition: least_squares_fit.cpp:43
linear_fit_data::xzbar
float xzbar
Definition: least_squares_fit.h:40
linear_fit_data::max_absx
float max_absx
Definition: least_squares_fit.h:40
linear_fit_data::yzbar
float yzbar
Definition: least_squares_fit.h:40
linear_fit_data::y2bar
float y2bar
Definition: least_squares_fit.h:40
linear_fit_data::z2bar
float z2bar
Definition: least_squares_fit.h:40
XYval::y
T y
Definition: types.h:185
linear_fit_data::max_absy
float max_absy
Definition: least_squares_fit.h:40
finish_incremental_LSF
int finish_incremental_LSF(struct linear_fit_data *)
Definition: least_squares_fit.cpp:43
incremental_WLSF
void incremental_WLSF(struct linear_fit_data *lsf, const float &x, const float &y, const float &z, const float &w)
Definition: least_squares_fit.h:51
linear_fit_data::N
float N
Definition: least_squares_fit.h:40