2017-04-26 03:50:17 +02:00
|
|
|
/**
|
|
|
|
* Marlin 3D Printer Firmware
|
2020-02-03 15:00:57 +01:00
|
|
|
* Copyright (c) 2020 MarlinFirmware [https://github.com/MarlinFirmware/Marlin]
|
2017-04-26 03:50:17 +02:00
|
|
|
*
|
|
|
|
* Based on Sprinter and grbl.
|
2019-06-28 06:57:50 +02:00
|
|
|
* Copyright (c) 2011 Camiel Gubbels / Erik van der Zalm
|
2017-04-26 03:50:17 +02:00
|
|
|
*
|
|
|
|
* 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
|
2020-07-23 05:20:14 +02:00
|
|
|
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
2017-04-26 03:50:17 +02:00
|
|
|
*
|
|
|
|
*/
|
2018-11-04 09:25:55 +01:00
|
|
|
#pragma once
|
2017-04-26 03:50:17 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Incremental 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. And even better... the data
|
2017-04-29 00:33:28 +02:00
|
|
|
* fed into the algorithm does not need to all be present at the same time.
|
2017-04-26 03:50:17 +02:00
|
|
|
* A point can be probed and its values fed into the algorithm and then discarded.
|
|
|
|
*/
|
|
|
|
|
2017-09-06 13:28:32 +02:00
|
|
|
#include "../inc/MarlinConfig.h"
|
2017-04-26 03:50:17 +02:00
|
|
|
#include <math.h>
|
|
|
|
|
|
|
|
struct linear_fit_data {
|
2017-04-29 00:33:28 +02:00
|
|
|
float xbar, ybar, zbar,
|
2021-03-25 04:56:48 +01:00
|
|
|
x2bar, y2bar,
|
2017-04-29 00:33:28 +02:00
|
|
|
xybar, xzbar, yzbar,
|
|
|
|
max_absx, max_absy,
|
2017-05-22 19:33:50 +02:00
|
|
|
A, B, D, N;
|
2017-04-26 03:50:17 +02:00
|
|
|
};
|
|
|
|
|
2019-09-18 01:16:28 +02:00
|
|
|
inline void incremental_LSF_reset(struct linear_fit_data *lsf) {
|
2017-05-22 19:33:50 +02:00
|
|
|
memset(lsf, 0, sizeof(linear_fit_data));
|
|
|
|
}
|
|
|
|
|
2021-04-02 00:59:57 +02:00
|
|
|
inline void incremental_WLSF(struct linear_fit_data *lsf, const_float_t x, const_float_t y, const_float_t z, const_float_t w) {
|
2017-05-22 19:33:50 +02:00
|
|
|
// weight each accumulator by factor w, including the "number" of samples
|
2017-09-06 13:28:32 +02:00
|
|
|
// (analogous to calling inc_LSF twice with same values to weight it by 2X)
|
2019-09-14 10:05:10 +02:00
|
|
|
const float wx = w * x, wy = w * y, wz = w * z;
|
|
|
|
lsf->xbar += wx;
|
|
|
|
lsf->ybar += wy;
|
|
|
|
lsf->zbar += wz;
|
|
|
|
lsf->x2bar += wx * x;
|
|
|
|
lsf->y2bar += wy * y;
|
|
|
|
lsf->xybar += wx * y;
|
|
|
|
lsf->xzbar += wx * z;
|
|
|
|
lsf->yzbar += wy * z;
|
2017-05-22 19:33:50 +02:00
|
|
|
lsf->N += w;
|
2019-09-14 10:05:10 +02:00
|
|
|
lsf->max_absx = _MAX(ABS(wx), lsf->max_absx);
|
|
|
|
lsf->max_absy = _MAX(ABS(wy), lsf->max_absy);
|
2017-05-22 19:33:50 +02:00
|
|
|
}
|
2021-04-02 00:59:57 +02:00
|
|
|
inline void incremental_WLSF(struct linear_fit_data *lsf, const xy_pos_t &pos, const_float_t z, const_float_t w) {
|
2019-09-29 11:25:39 +02:00
|
|
|
incremental_WLSF(lsf, pos.x, pos.y, z, w);
|
|
|
|
}
|
2017-05-22 19:33:50 +02:00
|
|
|
|
2021-04-02 00:59:57 +02:00
|
|
|
inline void incremental_LSF(struct linear_fit_data *lsf, const_float_t x, const_float_t y, const_float_t z) {
|
2017-05-22 19:33:50 +02:00
|
|
|
lsf->xbar += x;
|
|
|
|
lsf->ybar += y;
|
|
|
|
lsf->zbar += z;
|
|
|
|
lsf->x2bar += sq(x);
|
|
|
|
lsf->y2bar += sq(y);
|
|
|
|
lsf->xybar += x * y;
|
|
|
|
lsf->xzbar += x * z;
|
|
|
|
lsf->yzbar += y * z;
|
2019-07-06 01:01:21 +02:00
|
|
|
lsf->max_absx = _MAX(ABS(x), lsf->max_absx);
|
|
|
|
lsf->max_absy = _MAX(ABS(y), lsf->max_absy);
|
2017-05-22 19:33:50 +02:00
|
|
|
lsf->N += 1.0;
|
|
|
|
}
|
2021-04-02 00:59:57 +02:00
|
|
|
inline void incremental_LSF(struct linear_fit_data *lsf, const xy_pos_t &pos, const_float_t z) {
|
2019-09-29 11:25:39 +02:00
|
|
|
incremental_LSF(lsf, pos.x, pos.y, z);
|
|
|
|
}
|
2017-05-22 19:33:50 +02:00
|
|
|
|
2017-04-26 03:50:17 +02:00
|
|
|
int finish_incremental_LSF(struct linear_fit_data *);
|