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.8KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. /**
  2. * Marlin 3D Printer Firmware
  3. * Copyright (C) 2019 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. #pragma once
  23. /**
  24. * Incremental 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. And even better... the data
  30. * fed into the algorithm does not need to all be present at the same time.
  31. * A point can be probed and its values fed into the algorithm and then discarded.
  32. *
  33. */
  34. #include "../inc/MarlinConfig.h"
  35. #include <math.h>
  36. struct linear_fit_data {
  37. float xbar, ybar, zbar,
  38. x2bar, y2bar, z2bar,
  39. xybar, xzbar, yzbar,
  40. max_absx, max_absy,
  41. A, B, D, N;
  42. };
  43. void inline incremental_LSF_reset(struct linear_fit_data *lsf) {
  44. memset(lsf, 0, sizeof(linear_fit_data));
  45. }
  46. void inline incremental_WLSF(struct linear_fit_data *lsf, const float &x, const float &y, const float &z, const float &w) {
  47. // weight each accumulator by factor w, including the "number" of samples
  48. // (analogous to calling inc_LSF twice with same values to weight it by 2X)
  49. lsf->xbar += w * x;
  50. lsf->ybar += w * y;
  51. lsf->zbar += w * z;
  52. lsf->x2bar += w * x * x; // don't use sq(x) -- let compiler re-use w*x four times
  53. lsf->y2bar += w * y * y;
  54. lsf->z2bar += w * z * z;
  55. lsf->xybar += w * x * y;
  56. lsf->xzbar += w * x * z;
  57. lsf->yzbar += w * y * z;
  58. lsf->N += w;
  59. lsf->max_absx = MAX(ABS(w * x), lsf->max_absx);
  60. lsf->max_absy = MAX(ABS(w * y), lsf->max_absy);
  61. }
  62. void inline incremental_LSF(struct linear_fit_data *lsf, const float &x, const float &y, const float &z) {
  63. lsf->xbar += x;
  64. lsf->ybar += y;
  65. lsf->zbar += z;
  66. lsf->x2bar += sq(x);
  67. lsf->y2bar += sq(y);
  68. lsf->z2bar += sq(z);
  69. lsf->xybar += x * y;
  70. lsf->xzbar += x * z;
  71. lsf->yzbar += y * z;
  72. lsf->max_absx = MAX(ABS(x), lsf->max_absx);
  73. lsf->max_absy = MAX(ABS(y), lsf->max_absy);
  74. lsf->N += 1.0;
  75. }
  76. int finish_incremental_LSF(struct linear_fit_data *);