My Marlin configs for Fabrikator Mini and CTC i3 Pro B
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

least_squares_fit.h 2.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  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. * Incremental Least Squares Best Fit By Roxy and Ed Williams
  24. *
  25. * This algorithm is high speed and has a very small code footprint.
  26. * Its results are identical to both the Iterative Least-Squares published
  27. * earlier by Roxy and the QR_SOLVE solution. If used in place of QR_SOLVE
  28. * it saves roughly 10K of program memory. And even better... the data
  29. * fed into the algorithm does not need to all be present at the same time.
  30. * A point can be probed and its values fed into the algorithm and then discarded.
  31. *
  32. */
  33. #include "MarlinConfig.h"
  34. #if ENABLED(AUTO_BED_LEVELING_UBL) || ENABLED(AUTO_BED_LEVELING_LINEAR)
  35. #include "Marlin.h"
  36. #include "macros.h"
  37. #include <math.h>
  38. struct linear_fit_data {
  39. float xbar, ybar, zbar,
  40. x2bar, y2bar, z2bar,
  41. xybar, xzbar, yzbar,
  42. max_absx, max_absy,
  43. A, B, D, N;
  44. };
  45. void inline incremental_LSF_reset(struct linear_fit_data *lsf) {
  46. memset(lsf, 0, sizeof(linear_fit_data));
  47. }
  48. void inline incremental_WLSF(struct linear_fit_data *lsf, const float &x, const float &y, const float &z, const float &w) {
  49. // weight each accumulator by factor w, including the "number" of samples
  50. // (analagous to calling inc_LSF twice with same values to weight it by 2X)
  51. lsf->xbar += w * x;
  52. lsf->ybar += w * y;
  53. lsf->zbar += w * z;
  54. lsf->x2bar += w * x * x; // don't use sq(x) -- let compiler re-use w*x four times
  55. lsf->y2bar += w * y * y;
  56. lsf->z2bar += w * z * z;
  57. lsf->xybar += w * x * y;
  58. lsf->xzbar += w * x * z;
  59. lsf->yzbar += w * y * z;
  60. lsf->N += w;
  61. lsf->max_absx = max(FABS(w * x), lsf->max_absx);
  62. lsf->max_absy = max(FABS(w * y), lsf->max_absy);
  63. }
  64. void inline incremental_LSF(struct linear_fit_data *lsf, const float &x, const float &y, const float &z) {
  65. lsf->xbar += x;
  66. lsf->ybar += y;
  67. lsf->zbar += z;
  68. lsf->x2bar += sq(x);
  69. lsf->y2bar += sq(y);
  70. lsf->z2bar += sq(z);
  71. lsf->xybar += x * y;
  72. lsf->xzbar += x * z;
  73. lsf->yzbar += y * z;
  74. lsf->max_absx = max(FABS(x), lsf->max_absx);
  75. lsf->max_absy = max(FABS(y), lsf->max_absy);
  76. lsf->N += 1.0;
  77. }
  78. int finish_incremental_LSF(struct linear_fit_data *);
  79. #endif