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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  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. #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. inline void incremental_LSF_reset(struct linear_fit_data *lsf) {
  44. memset(lsf, 0, sizeof(linear_fit_data));
  45. }
  46. inline void 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. const float wx = w * x, wy = w * y, wz = w * z;
  50. lsf->xbar += wx;
  51. lsf->ybar += wy;
  52. lsf->zbar += wz;
  53. lsf->x2bar += wx * x;
  54. lsf->y2bar += wy * y;
  55. lsf->z2bar += wz * z;
  56. lsf->xybar += wx * y;
  57. lsf->xzbar += wx * z;
  58. lsf->yzbar += wy * z;
  59. lsf->N += w;
  60. lsf->max_absx = _MAX(ABS(wx), lsf->max_absx);
  61. lsf->max_absy = _MAX(ABS(wy), lsf->max_absy);
  62. }
  63. inline void incremental_WLSF(struct linear_fit_data *lsf, const xy_pos_t &pos, const float &z, const float &w) {
  64. incremental_WLSF(lsf, pos.x, pos.y, z, w);
  65. }
  66. inline void incremental_LSF(struct linear_fit_data *lsf, const float &x, const float &y, const float &z) {
  67. lsf->xbar += x;
  68. lsf->ybar += y;
  69. lsf->zbar += z;
  70. lsf->x2bar += sq(x);
  71. lsf->y2bar += sq(y);
  72. lsf->z2bar += sq(z);
  73. lsf->xybar += x * y;
  74. lsf->xzbar += x * z;
  75. lsf->yzbar += y * z;
  76. lsf->max_absx = _MAX(ABS(x), lsf->max_absx);
  77. lsf->max_absy = _MAX(ABS(y), lsf->max_absy);
  78. lsf->N += 1.0;
  79. }
  80. inline void incremental_LSF(struct linear_fit_data *lsf, const xy_pos_t &pos, const float &z) {
  81. incremental_LSF(lsf, pos.x, pos.y, z);
  82. }
  83. int finish_incremental_LSF(struct linear_fit_data *);