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.cpp 3.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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. * 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. It also does not require all of
  29. * coordinates to be present during the calculations. Each point can be
  30. * probed and then discarded.
  31. *
  32. */
  33. #include "MarlinConfig.h"
  34. #if ENABLED(AUTO_BED_LEVELING_UBL) // Currently only used by UBL, but is applicable to Grid Based (Linear) Bed Leveling
  35. #include "macros.h"
  36. #include <math.h>
  37. #include "least_squares_fit.h"
  38. void incremental_LSF_reset(struct linear_fit_data *lsf) {
  39. lsf->n = 0;
  40. lsf->A = 0.0; // probably a memset() can be done to zero
  41. lsf->B = 0.0; // this whole structure
  42. lsf->D = 0.0;
  43. lsf->xbar = lsf->ybar = lsf->zbar = 0.0;
  44. lsf->x2bar = lsf->y2bar = lsf->z2bar = 0.0;
  45. lsf->xybar = lsf->xzbar = lsf->yzbar = 0.0;
  46. lsf->max_absx = lsf->max_absy = 0.0;
  47. }
  48. void incremental_LSF(struct linear_fit_data *lsf, float x, float y, float z) {
  49. lsf->xbar += x;
  50. lsf->ybar += y;
  51. lsf->zbar += z;
  52. lsf->x2bar += x*x;
  53. lsf->y2bar += y*y;
  54. lsf->z2bar += z*z;
  55. lsf->xybar += x*y;
  56. lsf->xzbar += x*z;
  57. lsf->yzbar += y*z;
  58. lsf->max_absx = (fabs(x) > lsf->max_absx) ? fabs(x) : lsf->max_absx;
  59. lsf->max_absy = (fabs(y) > lsf->max_absy) ? fabs(y) : lsf->max_absy;
  60. lsf->n++;
  61. return;
  62. }
  63. int finish_incremental_LSF(struct linear_fit_data *lsf) {
  64. float DD, N;
  65. N = (float) lsf->n;
  66. lsf->xbar /= N;
  67. lsf->ybar /= N;
  68. lsf->zbar /= N;
  69. lsf->x2bar = lsf->x2bar/N - lsf->xbar*lsf->xbar;
  70. lsf->y2bar = lsf->y2bar/N - lsf->ybar*lsf->ybar;
  71. lsf->z2bar = lsf->z2bar/N - lsf->zbar*lsf->zbar;
  72. lsf->xybar = lsf->xybar/N - lsf->xbar*lsf->ybar;
  73. lsf->yzbar = lsf->yzbar/N - lsf->ybar*lsf->zbar;
  74. lsf->xzbar = lsf->xzbar/N - lsf->xbar*lsf->zbar;
  75. DD = lsf->x2bar*lsf->y2bar - lsf->xybar*lsf->xybar;
  76. if (fabs(DD) <= 1e-10*(lsf->max_absx+lsf->max_absy))
  77. return -1;
  78. lsf->A = (lsf->yzbar*lsf->xybar - lsf->xzbar*lsf->y2bar) / DD;
  79. lsf->B = (lsf->xzbar*lsf->xybar - lsf->yzbar*lsf->x2bar) / DD;
  80. lsf->D = -(lsf->zbar + lsf->A*lsf->xbar + lsf->B*lsf->ybar);
  81. return 0;
  82. }
  83. #endif