Prusa MINI Firmware overview
least_squares_fit.h File Reference
#include "../inc/MarlinConfig.h"
#include <math.h>

Go to the source code of this file.

Classes

struct  linear_fit_data
 

Functions

void incremental_LSF_reset (struct linear_fit_data *lsf)
 
void incremental_WLSF (struct linear_fit_data *lsf, const float &x, const float &y, const float &z, const float &w)
 
void incremental_WLSF (struct linear_fit_data *lsf, const xy_pos_t &pos, const float &z, const float &w)
 
void incremental_LSF (struct linear_fit_data *lsf, const float &x, const float &y, const float &z)
 
void incremental_LSF (struct linear_fit_data *lsf, const xy_pos_t &pos, const float &z)
 
int finish_incremental_LSF (struct linear_fit_data *)
 

Function Documentation

◆ incremental_LSF_reset()

void incremental_LSF_reset ( struct linear_fit_data lsf)
47  {
48  memset(lsf, 0, sizeof(linear_fit_data));
49 }

◆ incremental_WLSF() [1/2]

void incremental_WLSF ( struct linear_fit_data lsf,
const float &  x,
const float &  y,
const float &  z,
const float &  w 
)
51  {
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 }
Here is the caller graph for this function:

◆ incremental_WLSF() [2/2]

void incremental_WLSF ( struct linear_fit_data lsf,
const xy_pos_t pos,
const float &  z,
const float &  w 
)
68  {
69  incremental_WLSF(lsf, pos.x, pos.y, z, w);
70 }
Here is the call graph for this function:

◆ incremental_LSF() [1/2]

void incremental_LSF ( struct linear_fit_data lsf,
const float &  x,
const float &  y,
const float &  z 
)
72  {
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 }
Here is the caller graph for this function:

◆ incremental_LSF() [2/2]

void incremental_LSF ( struct linear_fit_data lsf,
const xy_pos_t pos,
const float &  z 
)
86  {
87  incremental_LSF(lsf, pos.x, pos.y, z);
88 }
Here is the call graph for this function:

◆ finish_incremental_LSF()

int finish_incremental_LSF ( struct linear_fit_data lsf)

Marlin 3D Printer Firmware Copyright (c) 2019 MarlinFirmware [https://github.com/MarlinFirmware/Marlin]

Based on Sprinter and grbl. Copyright (c) 2011 Camiel Gubbels / Erik van der Zalm

This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.

You should have received a copy of the GNU General Public License along with this program. If not, see http://www.gnu.org/licenses/. Least Squares Best Fit by Roxy and Ed Williams

This algorithm is high speed and has a very small code footprint. Its results are identical to both the Iterative Least-Squares published earlier by Roxy and the QR_SOLVE solution. If used in place of QR_SOLVE it saves roughly 10K of program memory. It also does not require all of coordinates to be present during the calculations. Each point can be probed and then discarded.

43  {
44 
45  const float N = lsf->N;
46 
47  if (N == 0.0)
48  return 1;
49 
50  lsf->xbar /= N;
51  lsf->ybar /= N;
52  lsf->zbar /= N;
53  lsf->x2bar = lsf->x2bar / N - sq(lsf->xbar);
54  lsf->y2bar = lsf->y2bar / N - sq(lsf->ybar);
55  lsf->z2bar = lsf->z2bar / N - sq(lsf->zbar);
56  lsf->xybar = lsf->xybar / N - lsf->xbar * lsf->ybar;
57  lsf->yzbar = lsf->yzbar / N - lsf->ybar * lsf->zbar;
58  lsf->xzbar = lsf->xzbar / N - lsf->xbar * lsf->zbar;
59  const float DD = lsf->x2bar * lsf->y2bar - sq(lsf->xybar);
60 
61  if (ABS(DD) <= 1e-10 * (lsf->max_absx + lsf->max_absy))
62  return 1;
63 
64  lsf->A = (lsf->yzbar * lsf->xybar - lsf->xzbar * lsf->y2bar) / DD;
65  lsf->B = (lsf->xzbar * lsf->xybar - lsf->yzbar * lsf->x2bar) / DD;
66  lsf->D = -(lsf->zbar + lsf->A * lsf->xbar + lsf->B * lsf->ybar);
67  return 0;
68 }
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
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
ABS
#define ABS(a)
Definition: macros.h:266
linear_fit_data::x2bar
float x2bar
Definition: least_squares_fit.h:40
linear_fit_data::A
float A
Definition: least_squares_fit.h:40
XYval::x
T x
Definition: types.h:185
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
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