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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  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 <https://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. #include "../inc/MarlinConfig.h"
  34. #include <math.h>
  35. struct linear_fit_data {
  36. float xbar, ybar, zbar,
  37. x2bar, y2bar,
  38. xybar, xzbar, yzbar,
  39. max_absx, max_absy,
  40. A, B, D, N;
  41. };
  42. inline void incremental_LSF_reset(struct linear_fit_data *lsf) {
  43. memset(lsf, 0, sizeof(linear_fit_data));
  44. }
  45. inline void incremental_WLSF(struct linear_fit_data *lsf, const_float_t x, const_float_t y, const_float_t z, const_float_t w) {
  46. // weight each accumulator by factor w, including the "number" of samples
  47. // (analogous to calling inc_LSF twice with same values to weight it by 2X)
  48. const float wx = w * x, wy = w * y, wz = w * z;
  49. lsf->xbar += wx;
  50. lsf->ybar += wy;
  51. lsf->zbar += wz;
  52. lsf->x2bar += wx * x;
  53. lsf->y2bar += wy * y;
  54. lsf->xybar += wx * y;
  55. lsf->xzbar += wx * z;
  56. lsf->yzbar += wy * z;
  57. lsf->N += w;
  58. lsf->max_absx = _MAX(ABS(wx), lsf->max_absx);
  59. lsf->max_absy = _MAX(ABS(wy), lsf->max_absy);
  60. }
  61. inline void incremental_WLSF(struct linear_fit_data *lsf, const xy_pos_t &pos, const_float_t z, const_float_t w) {
  62. incremental_WLSF(lsf, pos.x, pos.y, z, w);
  63. }
  64. inline void incremental_LSF(struct linear_fit_data *lsf, const_float_t x, const_float_t y, const_float_t 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->xybar += x * y;
  71. lsf->xzbar += x * z;
  72. lsf->yzbar += y * z;
  73. lsf->max_absx = _MAX(ABS(x), lsf->max_absx);
  74. lsf->max_absy = _MAX(ABS(y), lsf->max_absy);
  75. lsf->N += 1.0;
  76. }
  77. inline void incremental_LSF(struct linear_fit_data *lsf, const xy_pos_t &pos, const_float_t z) {
  78. incremental_LSF(lsf, pos.x, pos.y, z);
  79. }
  80. int finish_incremental_LSF(struct linear_fit_data *);