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

Functions

int finish_incremental_LSF (struct linear_fit_data *lsf)
 

Function Documentation

◆ 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 }
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
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::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
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
linear_fit_data::max_absy
float max_absy
Definition: least_squares_fit.h:40
linear_fit_data::N
float N
Definition: least_squares_fit.h:40