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 2.4KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. /**
  2. * Marlin 3D Printer Firmware
  3. * Copyright (c) 2020 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 "../inc/MarlinConfig.h"
  34. #if ANY(AUTO_BED_LEVELING_UBL, AUTO_BED_LEVELING_LINEAR, Z_STEPPER_ALIGN_KNOWN_STEPPER_POSITIONS)
  35. #include "least_squares_fit.h"
  36. #include <math.h>
  37. int finish_incremental_LSF(struct linear_fit_data *lsf) {
  38. const float N = lsf->N;
  39. if (N == 0.0)
  40. return 1;
  41. lsf->xbar /= N;
  42. lsf->ybar /= N;
  43. lsf->zbar /= N;
  44. lsf->x2bar = lsf->x2bar / N - sq(lsf->xbar);
  45. lsf->y2bar = lsf->y2bar / N - sq(lsf->ybar);
  46. lsf->z2bar = lsf->z2bar / N - sq(lsf->zbar);
  47. lsf->xybar = lsf->xybar / N - lsf->xbar * lsf->ybar;
  48. lsf->yzbar = lsf->yzbar / N - lsf->ybar * lsf->zbar;
  49. lsf->xzbar = lsf->xzbar / N - lsf->xbar * lsf->zbar;
  50. const float DD = lsf->x2bar * lsf->y2bar - sq(lsf->xybar);
  51. if (ABS(DD) <= 1e-10 * (lsf->max_absx + lsf->max_absy))
  52. return 1;
  53. lsf->A = (lsf->yzbar * lsf->xybar - lsf->xzbar * lsf->y2bar) / DD;
  54. lsf->B = (lsf->xzbar * lsf->xybar - lsf->yzbar * lsf->x2bar) / DD;
  55. lsf->D = -(lsf->zbar + lsf->A * lsf->xbar + lsf->B * lsf->ybar);
  56. return 0;
  57. }
  58. #endif // AUTO_BED_LEVELING_UBL || ENABLED(AUTO_BED_LEVELING_LINEAR)