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

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