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.

planner_bezier.cpp 7.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  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. * planner_bezier.cpp
  24. *
  25. * Compute and buffer movement commands for bezier curves
  26. *
  27. */
  28. #include "../inc/MarlinConfig.h"
  29. #if ENABLED(BEZIER_CURVE_SUPPORT)
  30. #include "../Marlin.h"
  31. #include "../module/planner.h"
  32. #include "../core/language.h"
  33. #include "../module/temperature.h"
  34. // See the meaning in the documentation of cubic_b_spline().
  35. #define MIN_STEP 0.002
  36. #define MAX_STEP 0.1
  37. #define SIGMA 0.1
  38. /* Compute the linear interpolation between to real numbers.
  39. */
  40. inline static float interp(float a, float b, float t) { return (1.0 - t) * a + t * b; }
  41. /**
  42. * Compute a Bézier curve using the De Casteljau's algorithm (see
  43. * https://en.wikipedia.org/wiki/De_Casteljau%27s_algorithm), which is
  44. * easy to code and has good numerical stability (very important,
  45. * since Arudino works with limited precision real numbers).
  46. */
  47. inline static float eval_bezier(float a, float b, float c, float d, float t) {
  48. float iab = interp(a, b, t);
  49. float ibc = interp(b, c, t);
  50. float icd = interp(c, d, t);
  51. float iabc = interp(iab, ibc, t);
  52. float ibcd = interp(ibc, icd, t);
  53. float iabcd = interp(iabc, ibcd, t);
  54. return iabcd;
  55. }
  56. /**
  57. * We approximate Euclidean distance with the sum of the coordinates
  58. * offset (so-called "norm 1"), which is quicker to compute.
  59. */
  60. inline static float dist1(float x1, float y1, float x2, float y2) { return FABS(x1 - x2) + FABS(y1 - y2); }
  61. /**
  62. * The algorithm for computing the step is loosely based on the one in Kig
  63. * (See https://sources.debian.net/src/kig/4:15.08.3-1/misc/kigpainter.cpp/#L759)
  64. * However, we do not use the stack.
  65. *
  66. * The algorithm goes as it follows: the parameters t runs from 0.0 to
  67. * 1.0 describing the curve, which is evaluated by eval_bezier(). At
  68. * each iteration we have to choose a step, i.e., the increment of the
  69. * t variable. By default the step of the previous iteration is taken,
  70. * and then it is enlarged or reduced depending on how straight the
  71. * curve locally is. The step is always clamped between MIN_STEP/2 and
  72. * 2*MAX_STEP. MAX_STEP is taken at the first iteration.
  73. *
  74. * For some t, the step value is considered acceptable if the curve in
  75. * the interval [t, t+step] is sufficiently straight, i.e.,
  76. * sufficiently close to linear interpolation. In practice the
  77. * following test is performed: the distance between eval_bezier(...,
  78. * t+step/2) is evaluated and compared with 0.5*(eval_bezier(...,
  79. * t)+eval_bezier(..., t+step)). If it is smaller than SIGMA, then the
  80. * step value is considered acceptable, otherwise it is not. The code
  81. * seeks to find the larger step value which is considered acceptable.
  82. *
  83. * At every iteration the recorded step value is considered and then
  84. * iteratively halved until it becomes acceptable. If it was already
  85. * acceptable in the beginning (i.e., no halving were done), then
  86. * maybe it was necessary to enlarge it; then it is iteratively
  87. * doubled while it remains acceptable. The last acceptable value
  88. * found is taken, provided that it is between MIN_STEP and MAX_STEP
  89. * and does not bring t over 1.0.
  90. *
  91. * Caveat: this algorithm is not perfect, since it can happen that a
  92. * step is considered acceptable even when the curve is not linear at
  93. * all in the interval [t, t+step] (but its mid point coincides "by
  94. * chance" with the midpoint according to the parametrization). This
  95. * kind of glitches can be eliminated with proper first derivative
  96. * estimates; however, given the improbability of such configurations,
  97. * the mitigation offered by MIN_STEP and the small computational
  98. * power available on Arduino, I think it is not wise to implement it.
  99. */
  100. void cubic_b_spline(const float position[NUM_AXIS], const float target[NUM_AXIS], const float offset[4], float fr_mm_s, uint8_t extruder) {
  101. // Absolute first and second control points are recovered.
  102. float first0 = position[X_AXIS] + offset[0];
  103. float first1 = position[Y_AXIS] + offset[1];
  104. float second0 = target[X_AXIS] + offset[2];
  105. float second1 = target[Y_AXIS] + offset[3];
  106. float t = 0.0;
  107. float bez_target[4];
  108. bez_target[X_AXIS] = position[X_AXIS];
  109. bez_target[Y_AXIS] = position[Y_AXIS];
  110. float step = MAX_STEP;
  111. millis_t next_idle_ms = millis() + 200UL;
  112. while (t < 1.0) {
  113. thermalManager.manage_heater();
  114. millis_t now = millis();
  115. if (ELAPSED(now, next_idle_ms)) {
  116. next_idle_ms = now + 200UL;
  117. idle();
  118. }
  119. // First try to reduce the step in order to make it sufficiently
  120. // close to a linear interpolation.
  121. bool did_reduce = false;
  122. float new_t = t + step;
  123. NOMORE(new_t, 1.0);
  124. float new_pos0 = eval_bezier(position[X_AXIS], first0, second0, target[X_AXIS], new_t);
  125. float new_pos1 = eval_bezier(position[Y_AXIS], first1, second1, target[Y_AXIS], new_t);
  126. for (;;) {
  127. if (new_t - t < (MIN_STEP)) break;
  128. float candidate_t = 0.5 * (t + new_t);
  129. float candidate_pos0 = eval_bezier(position[X_AXIS], first0, second0, target[X_AXIS], candidate_t);
  130. float candidate_pos1 = eval_bezier(position[Y_AXIS], first1, second1, target[Y_AXIS], candidate_t);
  131. float interp_pos0 = 0.5 * (bez_target[X_AXIS] + new_pos0);
  132. float interp_pos1 = 0.5 * (bez_target[Y_AXIS] + new_pos1);
  133. if (dist1(candidate_pos0, candidate_pos1, interp_pos0, interp_pos1) <= (SIGMA)) break;
  134. new_t = candidate_t;
  135. new_pos0 = candidate_pos0;
  136. new_pos1 = candidate_pos1;
  137. did_reduce = true;
  138. }
  139. // If we did not reduce the step, maybe we should enlarge it.
  140. if (!did_reduce) for (;;) {
  141. if (new_t - t > MAX_STEP) break;
  142. float candidate_t = t + 2.0 * (new_t - t);
  143. if (candidate_t >= 1.0) break;
  144. float candidate_pos0 = eval_bezier(position[X_AXIS], first0, second0, target[X_AXIS], candidate_t);
  145. float candidate_pos1 = eval_bezier(position[Y_AXIS], first1, second1, target[Y_AXIS], candidate_t);
  146. float interp_pos0 = 0.5 * (bez_target[X_AXIS] + candidate_pos0);
  147. float interp_pos1 = 0.5 * (bez_target[Y_AXIS] + candidate_pos1);
  148. if (dist1(new_pos0, new_pos1, interp_pos0, interp_pos1) > (SIGMA)) break;
  149. new_t = candidate_t;
  150. new_pos0 = candidate_pos0;
  151. new_pos1 = candidate_pos1;
  152. }
  153. // Check some postcondition; they are disabled in the actual
  154. // Marlin build, but if you test the same code on a computer you
  155. // may want to check they are respect.
  156. /*
  157. assert(new_t <= 1.0);
  158. if (new_t < 1.0) {
  159. assert(new_t - t >= (MIN_STEP) / 2.0);
  160. assert(new_t - t <= (MAX_STEP) * 2.0);
  161. }
  162. */
  163. step = new_t - t;
  164. t = new_t;
  165. // Compute and send new position
  166. bez_target[X_AXIS] = new_pos0;
  167. bez_target[Y_AXIS] = new_pos1;
  168. // FIXME. The following two are wrong, since the parameter t is
  169. // not linear in the distance.
  170. bez_target[Z_AXIS] = interp(position[Z_AXIS], target[Z_AXIS], t);
  171. bez_target[E_AXIS] = interp(position[E_AXIS], target[E_AXIS], t);
  172. clamp_to_software_endstops(bez_target);
  173. planner.buffer_line_kinematic(bez_target, fr_mm_s, extruder);
  174. }
  175. }
  176. #endif // BEZIER_CURVE_SUPPORT