2017-04-07 01:46:47 +02:00
|
|
|
/**
|
|
|
|
* Marlin 3D Printer Firmware
|
|
|
|
* Copyright (C) 2016 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/>.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
2017-04-29 00:33:28 +02:00
|
|
|
* Least Squares Best Fit by Roxy and Ed Williams
|
2017-04-07 01:46:47 +02:00
|
|
|
*
|
2017-04-08 10:16:13 +02:00
|
|
|
* 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
|
2017-04-29 00:33:28 +02:00
|
|
|
* 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
|
2017-04-26 03:50:17 +02:00
|
|
|
* probed and then discarded.
|
2017-04-07 01:46:47 +02:00
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2017-09-06 13:28:32 +02:00
|
|
|
#include "../inc/MarlinConfig.h"
|
2017-04-07 01:46:47 +02:00
|
|
|
|
2017-07-11 02:05:11 +02:00
|
|
|
#if ENABLED(AUTO_BED_LEVELING_UBL) || ENABLED(AUTO_BED_LEVELING_LINEAR)
|
2017-04-08 10:16:13 +02:00
|
|
|
|
2017-04-26 03:50:17 +02:00
|
|
|
#include "least_squares_fit.h"
|
|
|
|
|
2017-09-06 13:28:32 +02:00
|
|
|
#include <math.h>
|
|
|
|
|
2017-05-22 19:33:50 +02:00
|
|
|
int finish_incremental_LSF(struct linear_fit_data *lsf) {
|
2017-04-08 10:16:13 +02:00
|
|
|
|
2017-05-22 19:33:50 +02:00
|
|
|
const float N = lsf->N;
|
2017-04-07 01:46:47 +02:00
|
|
|
|
2017-05-22 19:33:50 +02:00
|
|
|
if (N == 0.0)
|
|
|
|
return 1;
|
2017-04-26 03:50:17 +02:00
|
|
|
|
2017-04-29 00:33:28 +02:00
|
|
|
lsf->xbar /= N;
|
|
|
|
lsf->ybar /= N;
|
|
|
|
lsf->zbar /= N;
|
2017-05-03 01:51:23 +02:00
|
|
|
lsf->x2bar = lsf->x2bar / N - sq(lsf->xbar);
|
|
|
|
lsf->y2bar = lsf->y2bar / N - sq(lsf->ybar);
|
|
|
|
lsf->z2bar = lsf->z2bar / N - sq(lsf->zbar);
|
2017-05-04 02:21:44 +02:00
|
|
|
lsf->xybar = lsf->xybar / N - lsf->xbar * lsf->ybar;
|
|
|
|
lsf->yzbar = lsf->yzbar / N - lsf->ybar * lsf->zbar;
|
|
|
|
lsf->xzbar = lsf->xzbar / N - lsf->xbar * lsf->zbar;
|
2017-04-29 00:33:28 +02:00
|
|
|
const float DD = lsf->x2bar * lsf->y2bar - sq(lsf->xybar);
|
2017-05-07 01:59:16 +02:00
|
|
|
|
2017-06-20 05:39:23 +02:00
|
|
|
if (FABS(DD) <= 1e-10 * (lsf->max_absx + lsf->max_absy))
|
2017-05-07 01:59:16 +02:00
|
|
|
return 1;
|
2017-04-07 01:46:47 +02:00
|
|
|
|
2017-04-29 00:33:28 +02:00
|
|
|
lsf->A = (lsf->yzbar * lsf->xybar - lsf->xzbar * lsf->y2bar) / DD;
|
|
|
|
lsf->B = (lsf->xzbar * lsf->xybar - lsf->yzbar * lsf->x2bar) / DD;
|
|
|
|
lsf->D = -(lsf->zbar + lsf->A * lsf->xbar + lsf->B * lsf->ybar);
|
|
|
|
return 0;
|
|
|
|
}
|
2017-04-07 01:46:47 +02:00
|
|
|
|
2017-07-13 04:39:37 +02:00
|
|
|
#endif // AUTO_BED_LEVELING_UBL || ENABLED(AUTO_BED_LEVELING_LINEAR)
|