123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585 |
- #include "MarlinConfig.h"
- #if ENABLED(AUTO_BED_LEVELING_UBL)
- #include "Marlin.h"
- #include "ubl.h"
- #include "planner.h"
- #include "stepper.h"
- #include <avr/io.h>
- #include <math.h>
- #if AVR_AT90USB1286_FAMILY
- inline void set_current_from_destination() { COPY(current_position, destination); }
- #else
- extern void set_current_from_destination();
- #endif
- #if !UBL_SEGMENTED
- void unified_bed_leveling::line_to_destination_cartesian(const float &feed_rate, const uint8_t extruder) {
-
- #if ENABLED(SKEW_CORRECTION)
-
- float start[XYZE] = { current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS], current_position[E_CART] },
- end[XYZE] = { destination[X_AXIS], destination[Y_AXIS], destination[Z_AXIS], destination[E_CART] };
- planner.skew(start[X_AXIS], start[Y_AXIS], start[Z_AXIS]);
- planner.skew(end[X_AXIS], end[Y_AXIS], end[Z_AXIS]);
- #else
- const float (&start)[XYZE] = current_position,
- (&end)[XYZE] = destination;
- #endif
- const int cell_start_xi = get_cell_index_x(start[X_AXIS]),
- cell_start_yi = get_cell_index_y(start[Y_AXIS]),
- cell_dest_xi = get_cell_index_x(end[X_AXIS]),
- cell_dest_yi = get_cell_index_y(end[Y_AXIS]);
- if (g26_debug_flag) {
- SERIAL_ECHOPAIR(" ubl.line_to_destination_cartesian(xe=", destination[X_AXIS]);
- SERIAL_ECHOPAIR(", ye=", destination[Y_AXIS]);
- SERIAL_ECHOPAIR(", ze=", destination[Z_AXIS]);
- SERIAL_ECHOPAIR(", ee=", destination[E_CART]);
- SERIAL_CHAR(')');
- SERIAL_EOL();
- debug_current_and_destination(PSTR("Start of ubl.line_to_destination_cartesian()"));
- }
-
- if (cell_start_xi == cell_dest_xi && cell_start_yi == cell_dest_yi) {
-
- if (!WITHIN(cell_dest_xi, 0, GRID_MAX_POINTS_X - 1) || !WITHIN(cell_dest_yi, 0, GRID_MAX_POINTS_Y - 1)) {
-
-
-
- const float z_raise = 0.0
- #ifdef UBL_Z_RAISE_WHEN_OFF_MESH
- + UBL_Z_RAISE_WHEN_OFF_MESH
- #endif
- ;
- planner.buffer_segment(end[X_AXIS], end[Y_AXIS], end[Z_AXIS] + z_raise, end[E_CART], feed_rate, extruder);
- set_current_from_destination();
- if (g26_debug_flag)
- debug_current_and_destination(PSTR("out of bounds in ubl.line_to_destination_cartesian()"));
- return;
- }
- FINAL_MOVE:
-
- const float xratio = (end[X_AXIS] - mesh_index_to_xpos(cell_dest_xi)) * (1.0f / (MESH_X_DIST));
- float z1 = z_values[cell_dest_xi ][cell_dest_yi ] + xratio *
- (z_values[cell_dest_xi + 1][cell_dest_yi ] - z_values[cell_dest_xi][cell_dest_yi ]),
- z2 = z_values[cell_dest_xi ][cell_dest_yi + 1] + xratio *
- (z_values[cell_dest_xi + 1][cell_dest_yi + 1] - z_values[cell_dest_xi][cell_dest_yi + 1]);
- if (cell_dest_xi >= GRID_MAX_POINTS_X - 1) z1 = z2 = 0.0;
-
- const float yratio = (end[Y_AXIS] - mesh_index_to_ypos(cell_dest_yi)) * (1.0f / (MESH_Y_DIST)),
- z0 = cell_dest_yi < GRID_MAX_POINTS_Y - 1 ? (z1 + (z2 - z1) * yratio) * planner.fade_scaling_factor_for_z(end[Z_AXIS]) : 0.0;
-
-
- planner.buffer_segment(end[X_AXIS], end[Y_AXIS], end[Z_AXIS] + (isnan(z0) ? 0.0 : z0), end[E_CART], feed_rate, extruder);
- if (g26_debug_flag)
- debug_current_and_destination(PSTR("FINAL_MOVE in ubl.line_to_destination_cartesian()"));
- set_current_from_destination();
- return;
- }
-
- const float dx = end[X_AXIS] - start[X_AXIS],
- dy = end[Y_AXIS] - start[Y_AXIS];
- const int left_flag = dx < 0.0 ? 1 : 0,
- down_flag = dy < 0.0 ? 1 : 0;
- const float adx = left_flag ? -dx : dx,
- ady = down_flag ? -dy : dy;
- const int dxi = cell_start_xi == cell_dest_xi ? 0 : left_flag ? -1 : 1,
- dyi = cell_start_yi == cell_dest_yi ? 0 : down_flag ? -1 : 1;
-
- const bool use_x_dist = adx > ady;
- float on_axis_distance = use_x_dist ? dx : dy,
- e_position = end[E_CART] - start[E_CART],
- z_position = end[Z_AXIS] - start[Z_AXIS];
- const float e_normalized_dist = e_position / on_axis_distance,
- z_normalized_dist = z_position / on_axis_distance;
- int current_xi = cell_start_xi,
- current_yi = cell_start_yi;
- const float m = dy / dx,
- c = start[Y_AXIS] - m * start[X_AXIS];
- const bool inf_normalized_flag = (isinf(e_normalized_dist) != 0),
- inf_m_flag = (isinf(m) != 0);
-
- if (dxi == 0) {
- current_yi += down_flag;
- while (current_yi != cell_dest_yi + down_flag) {
- current_yi += dyi;
- const float next_mesh_line_y = mesh_index_to_ypos(current_yi);
-
- const float rx = inf_m_flag ? start[X_AXIS] : (next_mesh_line_y - c) / m;
- float z0 = z_correction_for_x_on_horizontal_mesh_line(rx, current_xi, current_yi)
- * planner.fade_scaling_factor_for_z(end[Z_AXIS]);
-
-
- if (isnan(z0)) z0 = 0.0;
- const float ry = mesh_index_to_ypos(current_yi);
-
- if (ry != start[Y_AXIS]) {
- if (!inf_normalized_flag) {
- on_axis_distance = use_x_dist ? rx - start[X_AXIS] : ry - start[Y_AXIS];
- e_position = start[E_CART] + on_axis_distance * e_normalized_dist;
- z_position = start[Z_AXIS] + on_axis_distance * z_normalized_dist;
- }
- else {
- e_position = end[E_CART];
- z_position = end[Z_AXIS];
- }
- planner.buffer_segment(rx, ry, z_position + z0, e_position, feed_rate, extruder);
- }
- }
- if (g26_debug_flag)
- debug_current_and_destination(PSTR("vertical move done in ubl.line_to_destination_cartesian()"));
-
- if (current_position[X_AXIS] != end[X_AXIS] || current_position[Y_AXIS] != end[Y_AXIS])
- goto FINAL_MOVE;
- set_current_from_destination();
- return;
- }
-
- if (dyi == 0) {
- current_xi += left_flag;
- while (current_xi != cell_dest_xi + left_flag) {
- current_xi += dxi;
- const float next_mesh_line_x = mesh_index_to_xpos(current_xi),
- ry = m * next_mesh_line_x + c;
- float z0 = z_correction_for_y_on_vertical_mesh_line(ry, current_xi, current_yi)
- * planner.fade_scaling_factor_for_z(end[Z_AXIS]);
-
-
- if (isnan(z0)) z0 = 0.0;
- const float rx = mesh_index_to_xpos(current_xi);
-
- if (rx != start[X_AXIS]) {
- if (!inf_normalized_flag) {
- on_axis_distance = use_x_dist ? rx - start[X_AXIS] : ry - start[Y_AXIS];
- e_position = start[E_CART] + on_axis_distance * e_normalized_dist;
- z_position = start[Z_AXIS] + on_axis_distance * z_normalized_dist;
- }
- else {
- e_position = end[E_CART];
- z_position = end[Z_AXIS];
- }
- if (!planner.buffer_segment(rx, ry, z_position + z0, e_position, feed_rate, extruder))
- break;
- }
- }
- if (g26_debug_flag)
- debug_current_and_destination(PSTR("horizontal move done in ubl.line_to_destination_cartesian()"));
- if (current_position[X_AXIS] != end[X_AXIS] || current_position[Y_AXIS] != end[Y_AXIS])
- goto FINAL_MOVE;
- set_current_from_destination();
- return;
- }
-
- int xi_cnt = cell_start_xi - cell_dest_xi,
- yi_cnt = cell_start_yi - cell_dest_yi;
- if (xi_cnt < 0) xi_cnt = -xi_cnt;
- if (yi_cnt < 0) yi_cnt = -yi_cnt;
- current_xi += left_flag;
- current_yi += down_flag;
- while (xi_cnt || yi_cnt) {
- const float next_mesh_line_x = mesh_index_to_xpos(current_xi + dxi),
- next_mesh_line_y = mesh_index_to_ypos(current_yi + dyi),
- ry = m * next_mesh_line_x + c,
- rx = (next_mesh_line_y - c) / m;
-
-
-
- if (left_flag == (rx > next_mesh_line_x)) {
-
- float z0 = z_correction_for_x_on_horizontal_mesh_line(rx, current_xi - left_flag, current_yi + dyi)
- * planner.fade_scaling_factor_for_z(end[Z_AXIS]);
-
-
- if (isnan(z0)) z0 = 0.0;
- if (!inf_normalized_flag) {
- on_axis_distance = use_x_dist ? rx - start[X_AXIS] : next_mesh_line_y - start[Y_AXIS];
- e_position = start[E_CART] + on_axis_distance * e_normalized_dist;
- z_position = start[Z_AXIS] + on_axis_distance * z_normalized_dist;
- }
- else {
- e_position = end[E_CART];
- z_position = end[Z_AXIS];
- }
- if (!planner.buffer_segment(rx, next_mesh_line_y, z_position + z0, e_position, feed_rate, extruder))
- break;
- current_yi += dyi;
- yi_cnt--;
- }
- else {
-
- float z0 = z_correction_for_y_on_vertical_mesh_line(ry, current_xi + dxi, current_yi - down_flag)
- * planner.fade_scaling_factor_for_z(end[Z_AXIS]);
-
-
- if (isnan(z0)) z0 = 0.0;
- if (!inf_normalized_flag) {
- on_axis_distance = use_x_dist ? next_mesh_line_x - start[X_AXIS] : ry - start[Y_AXIS];
- e_position = start[E_CART] + on_axis_distance * e_normalized_dist;
- z_position = start[Z_AXIS] + on_axis_distance * z_normalized_dist;
- }
- else {
- e_position = end[E_CART];
- z_position = end[Z_AXIS];
- }
- if (!planner.buffer_segment(next_mesh_line_x, ry, z_position + z0, e_position, feed_rate, extruder))
- break;
- current_xi += dxi;
- xi_cnt--;
- }
- if (xi_cnt < 0 || yi_cnt < 0) break;
- }
- if (g26_debug_flag)
- debug_current_and_destination(PSTR("generic move done in ubl.line_to_destination_cartesian()"));
- if (current_position[X_AXIS] != end[X_AXIS] || current_position[Y_AXIS] != end[Y_AXIS])
- goto FINAL_MOVE;
- set_current_from_destination();
- }
- #else
- #if IS_SCARA
- static float scara_feed_factor, scara_oldA, scara_oldB;
- #endif
-
-
- inline void _O2 ubl_buffer_segment_raw(const float (&in_raw)[XYZE], const float &fr) {
- #if ENABLED(SKEW_CORRECTION)
- float raw[XYZE] = { in_raw[X_AXIS], in_raw[Y_AXIS], in_raw[Z_AXIS] };
- planner.skew(raw[X_AXIS], raw[Y_AXIS], raw[Z_AXIS]);
- #else
- const float (&raw)[XYZE] = in_raw;
- #endif
- #if ENABLED(DELTA)
- DELTA_IK(raw);
- planner.buffer_segment(delta[A_AXIS], delta[B_AXIS], delta[C_AXIS], in_raw[E_CART], fr, active_extruder);
- #elif ENABLED(HANGPRINTER)
- HANGPRINTER_IK(raw);
- planner.buffer_segment(line_lengths[A_AXIS], line_lengths[B_AXIS], line_lengths[C_AXIS], line_lengths[D_AXIS], in_raw[E_CART], fr, active_extruder);
- #elif IS_SCARA
- inverse_kinematics(raw);
-
- const float adiff = ABS(delta[A_AXIS] - scara_oldA),
- bdiff = ABS(delta[B_AXIS] - scara_oldB);
- scara_oldA = delta[A_AXIS];
- scara_oldB = delta[B_AXIS];
- float s_feedrate = MAX(adiff, bdiff) * scara_feed_factor;
- planner.buffer_segment(delta[A_AXIS], delta[B_AXIS], delta[C_AXIS], in_raw[E_CART], s_feedrate, active_extruder);
- #else
- planner.buffer_segment(raw[X_AXIS], raw[Y_AXIS], raw[Z_AXIS], in_raw[E_CART], fr, active_extruder);
- #endif
- }
- #if IS_SCARA
- #define DELTA_SEGMENT_MIN_LENGTH 0.25
- #elif ENABLED(DELTA)
- #define DELTA_SEGMENT_MIN_LENGTH 0.10
- #else
- #ifdef LEVELED_SEGMENT_LENGTH
- #define DELTA_SEGMENT_MIN_LENGTH LEVELED_SEGMENT_LENGTH
- #else
- #define DELTA_SEGMENT_MIN_LENGTH 1.00
- #endif
- #endif
-
- bool _O2 unified_bed_leveling::prepare_segmented_line_to(const float (&rtarget)[XYZE], const float &feedrate) {
- if (!position_is_reachable(rtarget[X_AXIS], rtarget[Y_AXIS]))
- return true;
- const float total[XYZE] = {
- rtarget[X_AXIS] - current_position[X_AXIS],
- rtarget[Y_AXIS] - current_position[Y_AXIS],
- rtarget[Z_AXIS] - current_position[Z_AXIS],
- rtarget[E_CART] - current_position[E_CART]
- };
- const float cartesian_xy_mm = HYPOT(total[X_AXIS], total[Y_AXIS]);
- #if IS_KINEMATIC
- const float seconds = cartesian_xy_mm / feedrate;
- uint16_t segments = lroundf(delta_segments_per_second * seconds),
- seglimit = lroundf(cartesian_xy_mm * (1.0f / (DELTA_SEGMENT_MIN_LENGTH)));
- NOMORE(segments, seglimit);
- #else
- uint16_t segments = lroundf(cartesian_xy_mm * (1.0f / (DELTA_SEGMENT_MIN_LENGTH)));
- #endif
- NOLESS(segments, 1U);
- const float inv_segments = 1.0f / segments;
- #if IS_SCARA
- scara_feed_factor = cartesian_xy_mm * inv_segments * feedrate;
- scara_oldA = planner.get_axis_position_degrees(A_AXIS);
- scara_oldB = planner.get_axis_position_degrees(B_AXIS);
- #endif
- const float diff[XYZE] = {
- total[X_AXIS] * inv_segments,
- total[Y_AXIS] * inv_segments,
- total[Z_AXIS] * inv_segments,
- total[E_CART] * inv_segments
- };
-
-
- float raw[XYZE] = {
- current_position[X_AXIS],
- current_position[Y_AXIS],
- current_position[Z_AXIS],
- current_position[E_CART]
- };
-
- if (!planner.leveling_active || !planner.leveling_active_at_z(rtarget[Z_AXIS])) {
- while (--segments) {
- LOOP_XYZE(i) raw[i] += diff[i];
- ubl_buffer_segment_raw(raw, feedrate);
- }
- ubl_buffer_segment_raw(rtarget, feedrate);
- return false;
- }
-
- #if ENABLED(ENABLE_LEVELING_FADE_HEIGHT)
- const float fade_scaling_factor = planner.fade_scaling_factor_for_z(rtarget[Z_AXIS]);
- #endif
-
- LOOP_XYZE(i) raw[i] += diff[i];
- for (;;) {
-
-
-
-
-
-
- int8_t cell_xi = (raw[X_AXIS] - (MESH_MIN_X)) * (1.0f / (MESH_X_DIST)),
- cell_yi = (raw[Y_AXIS] - (MESH_MIN_Y)) * (1.0f / (MESH_Y_DIST));
- cell_xi = constrain(cell_xi, 0, (GRID_MAX_POINTS_X) - 1);
- cell_yi = constrain(cell_yi, 0, (GRID_MAX_POINTS_Y) - 1);
- const float x0 = mesh_index_to_xpos(cell_xi),
- y0 = mesh_index_to_ypos(cell_yi);
- float z_x0y0 = z_values[cell_xi ][cell_yi ],
- z_x1y0 = z_values[cell_xi+1][cell_yi ],
- z_x0y1 = z_values[cell_xi ][cell_yi+1],
- z_x1y1 = z_values[cell_xi+1][cell_yi+1];
- if (isnan(z_x0y0)) z_x0y0 = 0;
- if (isnan(z_x1y0)) z_x1y0 = 0;
- if (isnan(z_x0y1)) z_x0y1 = 0;
- if (isnan(z_x1y1)) z_x1y1 = 0;
- float cx = raw[X_AXIS] - x0,
- cy = raw[Y_AXIS] - y0;
- const float z_xmy0 = (z_x1y0 - z_x0y0) * (1.0f / (MESH_X_DIST)),
- z_xmy1 = (z_x1y1 - z_x0y1) * (1.0f / (MESH_X_DIST));
- float z_cxy0 = z_x0y0 + z_xmy0 * cx;
- const float z_cxy1 = z_x0y1 + z_xmy1 * cx,
- z_cxyd = z_cxy1 - z_cxy0;
- float z_cxym = z_cxyd * (1.0f / (MESH_Y_DIST));
-
-
-
-
- const float z_sxy0 = z_xmy0 * diff[X_AXIS],
- z_sxym = (z_xmy1 - z_xmy0) * (1.0f / (MESH_Y_DIST)) * diff[X_AXIS];
- for (;;) {
- if (--segments == 0)
- COPY(raw, rtarget);
- const float z_cxcy = (z_cxy0 + z_cxym * cy)
- #if ENABLED(ENABLE_LEVELING_FADE_HEIGHT)
- * fade_scaling_factor
- #endif
- ;
- const float z = raw[Z_AXIS];
- raw[Z_AXIS] += z_cxcy;
- ubl_buffer_segment_raw(raw, feedrate);
- raw[Z_AXIS] = z;
- if (segments == 0)
- return false;
- LOOP_XYZE(i) raw[i] += diff[i];
- cx += diff[X_AXIS];
- cy += diff[Y_AXIS];
- if (!WITHIN(cx, 0, MESH_X_DIST) || !WITHIN(cy, 0, MESH_Y_DIST))
- break;
-
-
- z_cxy0 += z_sxy0;
- z_cxym += z_sxym;
- }
- }
- return false;
- }
- #endif
- #endif
|