Subversion Repositories MK-Marlin

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
1 ron 1
/**
2
 * Marlin 3D Printer Firmware
3
 * Copyright (C) 2016 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
 
23
/**
24
 * Least Squares Best Fit by Roxy and Ed Williams
25
 *
26
 * This algorithm is high speed and has a very small code footprint.
27
 * Its results are identical to both the Iterative Least-Squares published
28
 * earlier by Roxy and the QR_SOLVE solution. If used in place of QR_SOLVE
29
 * it saves roughly 10K of program memory. It also does not require all of
30
 * coordinates to be present during the calculations. Each point can be
31
 * probed and then discarded.
32
 *
33
 */
34
 
35
#include "MarlinConfig.h"
36
 
37
#if ENABLED(AUTO_BED_LEVELING_UBL) || ENABLED(AUTO_BED_LEVELING_LINEAR)
38
 
39
#include "macros.h"
40
#include <math.h>
41
 
42
#include "least_squares_fit.h"
43
 
44
int finish_incremental_LSF(struct linear_fit_data *lsf) {
45
 
46
  const float N = lsf->N;
47
 
48
  if (N == 0.0)
49
    return 1;
50
 
51
  lsf->xbar /= N;
52
  lsf->ybar /= N;
53
  lsf->zbar /= N;
54
  lsf->x2bar = lsf->x2bar / N - sq(lsf->xbar);
55
  lsf->y2bar = lsf->y2bar / N - sq(lsf->ybar);
56
  lsf->z2bar = lsf->z2bar / N - sq(lsf->zbar);
57
  lsf->xybar = lsf->xybar / N - lsf->xbar * lsf->ybar;
58
  lsf->yzbar = lsf->yzbar / N - lsf->ybar * lsf->zbar;
59
  lsf->xzbar = lsf->xzbar / N - lsf->xbar * lsf->zbar;
60
  const float DD = lsf->x2bar * lsf->y2bar - sq(lsf->xybar);
61
 
62
  if (ABS(DD) <= 1e-10 * (lsf->max_absx + lsf->max_absy))
63
    return 1;
64
 
65
  lsf->A = (lsf->yzbar * lsf->xybar - lsf->xzbar * lsf->y2bar) / DD;
66
  lsf->B = (lsf->xzbar * lsf->xybar - lsf->yzbar * lsf->x2bar) / DD;
67
  lsf->D = -(lsf->zbar + lsf->A * lsf->xbar + lsf->B * lsf->ybar);
68
  return 0;
69
}
70
 
71
#endif // AUTO_BED_LEVELING_UBL || ENABLED(AUTO_BED_LEVELING_LINEAR)