123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203 |
- #include "MarlinConfig.h"
- #if ENABLED(BEZIER_CURVE_SUPPORT)
- #include "planner.h"
- #include "language.h"
- #include "temperature.h"
- #include "Marlin.h"
- #define MIN_STEP 0.002f
- #define MAX_STEP 0.1f
- #define SIGMA 0.1f
- inline static float interp(float a, float b, float t) { return (1.0f - t) * a + t * b; }
- inline static float eval_bezier(float a, float b, float c, float d, float t) {
- float iab = interp(a, b, t);
- float ibc = interp(b, c, t);
- float icd = interp(c, d, t);
- float iabc = interp(iab, ibc, t);
- float ibcd = interp(ibc, icd, t);
- float iabcd = interp(iabc, ibcd, t);
- return iabcd;
- }
- inline static float dist1(float x1, float y1, float x2, float y2) { return ABS(x1 - x2) + ABS(y1 - y2); }
- void cubic_b_spline(const float pos[XYZE], const float cart_target[XYZE], const float offset[4], float fr_mm_s, uint8_t extruder) {
-
- const float first0 = pos[X_AXIS] + offset[0],
- first1 = pos[Y_AXIS] + offset[1],
- second0 = cart_target[X_AXIS] + offset[2],
- second1 = cart_target[Y_AXIS] + offset[3];
- float t = 0;
- float bez_target[XYZE];
- bez_target[X_AXIS] = pos[X_AXIS];
- bez_target[Y_AXIS] = pos[Y_AXIS];
- float step = MAX_STEP;
- millis_t next_idle_ms = millis() + 200UL;
- while (t < 1) {
- thermalManager.manage_heater();
- millis_t now = millis();
- if (ELAPSED(now, next_idle_ms)) {
- next_idle_ms = now + 200UL;
- idle();
- }
-
-
- bool did_reduce = false;
- float new_t = t + step;
- NOMORE(new_t, 1);
- float new_pos0 = eval_bezier(pos[X_AXIS], first0, second0, cart_target[X_AXIS], new_t),
- new_pos1 = eval_bezier(pos[Y_AXIS], first1, second1, cart_target[Y_AXIS], new_t);
- for (;;) {
- if (new_t - t < (MIN_STEP)) break;
- const float candidate_t = 0.5f * (t + new_t),
- candidate_pos0 = eval_bezier(pos[X_AXIS], first0, second0, cart_target[X_AXIS], candidate_t),
- candidate_pos1 = eval_bezier(pos[Y_AXIS], first1, second1, cart_target[Y_AXIS], candidate_t),
- interp_pos0 = 0.5f * (bez_target[X_AXIS] + new_pos0),
- interp_pos1 = 0.5f * (bez_target[Y_AXIS] + new_pos1);
- if (dist1(candidate_pos0, candidate_pos1, interp_pos0, interp_pos1) <= (SIGMA)) break;
- new_t = candidate_t;
- new_pos0 = candidate_pos0;
- new_pos1 = candidate_pos1;
- did_reduce = true;
- }
-
- if (!did_reduce) for (;;) {
- if (new_t - t > MAX_STEP) break;
- const float candidate_t = t + 2 * (new_t - t);
- if (candidate_t >= 1) break;
- const float candidate_pos0 = eval_bezier(pos[X_AXIS], first0, second0, cart_target[X_AXIS], candidate_t),
- candidate_pos1 = eval_bezier(pos[Y_AXIS], first1, second1, cart_target[Y_AXIS], candidate_t),
- interp_pos0 = 0.5f * (bez_target[X_AXIS] + candidate_pos0),
- interp_pos1 = 0.5f * (bez_target[Y_AXIS] + candidate_pos1);
- if (dist1(new_pos0, new_pos1, interp_pos0, interp_pos1) > (SIGMA)) break;
- new_t = candidate_t;
- new_pos0 = candidate_pos0;
- new_pos1 = candidate_pos1;
- }
-
-
-
-
- step = new_t - t;
- t = new_t;
-
- bez_target[X_AXIS] = new_pos0;
- bez_target[Y_AXIS] = new_pos1;
-
-
- bez_target[Z_AXIS] = interp(pos[Z_AXIS], cart_target[Z_AXIS], t);
- bez_target[E_CART] = interp(pos[E_CART], cart_target[E_CART], t);
- clamp_to_software_endstops(bez_target);
- #if HAS_UBL_AND_CURVES
- float bez_copy[XYZ] = { bez_target[X_AXIS], bez_target[Y_AXIS], bez_target[Z_AXIS] };
- planner.apply_leveling(bez_copy);
- if (!planner.buffer_segment(bez_copy[X_AXIS], bez_copy[Y_AXIS], bez_copy[Z_AXIS], bez_target[E_CART], fr_mm_s, active_extruder))
- break;
- #else
- if (!planner.buffer_line_kinematic(bez_target, fr_mm_s, extruder))
- break;
- #endif
- }
- }
- #endif
|