123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563 |
-
- #include "MarlinConfig.h"
-
- #if ENABLED(AUTO_BED_LEVELING_UBL)
-
- #include "Marlin.h"
- #include "ubl.h"
- #include "planner.h"
- #include <avr/io.h>
- #include <math.h>
-
- extern float destination[XYZE];
- extern void set_current_to_destination();
-
- static void debug_echo_axis(const AxisEnum axis) {
- if (current_position[axis] == destination[axis])
- SERIAL_ECHOPGM("-------------");
- else
- SERIAL_ECHO_F(destination[X_AXIS], 6);
- }
-
- void debug_current_and_destination(const char *title) {
-
-
-
- if (*title != '!' && !ubl.g26_debug_flag) return;
-
- const float de = destination[E_AXIS] - current_position[E_AXIS];
-
- if (de == 0.0) return;
-
- const float dx = current_position[X_AXIS] - destination[X_AXIS],
- dy = current_position[Y_AXIS] - destination[Y_AXIS],
- xy_dist = HYPOT(dx, dy);
-
- if (xy_dist == 0.0) {
- return;
-
-
-
- }
- else {
- SERIAL_ECHOPGM(" fpmm=");
- const float fpmm = de / xy_dist;
- SERIAL_ECHO_F(fpmm, 6);
- }
-
- SERIAL_ECHOPGM(" current=( ");
- SERIAL_ECHO_F(current_position[X_AXIS], 6);
- SERIAL_ECHOPGM(", ");
- SERIAL_ECHO_F(current_position[Y_AXIS], 6);
- SERIAL_ECHOPGM(", ");
- SERIAL_ECHO_F(current_position[Z_AXIS], 6);
- SERIAL_ECHOPGM(", ");
- SERIAL_ECHO_F(current_position[E_AXIS], 6);
- SERIAL_ECHOPGM(" ) destination=( ");
- debug_echo_axis(X_AXIS);
- SERIAL_ECHOPGM(", ");
- debug_echo_axis(Y_AXIS);
- SERIAL_ECHOPGM(", ");
- debug_echo_axis(Z_AXIS);
- SERIAL_ECHOPGM(", ");
- debug_echo_axis(E_AXIS);
- SERIAL_ECHOPGM(" ) ");
- SERIAL_ECHO(title);
- SERIAL_EOL;
-
- }
-
- void ubl_line_to_destination(const float &feed_rate, uint8_t extruder) {
-
-
- const float start[XYZE] = {
- current_position[X_AXIS],
- current_position[Y_AXIS],
- current_position[Z_AXIS],
- current_position[E_AXIS]
- },
- end[XYZE] = {
- destination[X_AXIS],
- destination[Y_AXIS],
- destination[Z_AXIS],
- destination[E_AXIS]
- };
-
- const int cell_start_xi = ubl.get_cell_index_x(RAW_X_POSITION(start[X_AXIS])),
- cell_start_yi = ubl.get_cell_index_y(RAW_Y_POSITION(start[Y_AXIS])),
- cell_dest_xi = ubl.get_cell_index_x(RAW_X_POSITION(end[X_AXIS])),
- cell_dest_yi = ubl.get_cell_index_y(RAW_Y_POSITION(end[Y_AXIS]));
-
- if (ubl.g26_debug_flag) {
- SERIAL_ECHOPAIR(" ubl_line_to_destination(xe=", end[X_AXIS]);
- SERIAL_ECHOPAIR(", ye=", end[Y_AXIS]);
- SERIAL_ECHOPAIR(", ze=", end[Z_AXIS]);
- SERIAL_ECHOPAIR(", ee=", end[E_AXIS]);
- SERIAL_CHAR(')');
- SERIAL_EOL;
- debug_current_and_destination(PSTR("Start of ubl_line_to_destination()"));
- }
-
- 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)) {
-
-
-
-
- planner.buffer_line(end[X_AXIS], end[Y_AXIS], end[Z_AXIS] + ubl.state.z_offset, end[E_AXIS], feed_rate, extruder);
- set_current_to_destination();
-
- if (ubl.g26_debug_flag)
- debug_current_and_destination(PSTR("out of bounds in ubl_line_to_destination()"));
-
- return;
- }
-
- FINAL_MOVE:
-
-
-
-
- const float xratio = (RAW_X_POSITION(end[X_AXIS]) - ubl.mesh_index_to_xpos[cell_dest_xi]) * (1.0 / (MESH_X_DIST)),
- z1 = ubl.z_values[cell_dest_xi ][cell_dest_yi ] + xratio *
- (ubl.z_values[cell_dest_xi + 1][cell_dest_yi ] - ubl.z_values[cell_dest_xi][cell_dest_yi ]),
- z2 = ubl.z_values[cell_dest_xi ][cell_dest_yi + 1] + xratio *
- (ubl.z_values[cell_dest_xi + 1][cell_dest_yi + 1] - ubl.z_values[cell_dest_xi][cell_dest_yi + 1]);
-
-
-
-
- const float yratio = (RAW_Y_POSITION(end[Y_AXIS]) - ubl.mesh_index_to_ypos[cell_dest_yi]) * (1.0 / (MESH_Y_DIST));
-
- float z0 = z1 + (z2 - z1) * yratio;
-
-
-
-
-
-
- z0 *= ubl.fade_scaling_factor_for_z(end[Z_AXIS]);
-
-
-
- if (isnan(z0)) z0 = 0.0;
-
- planner.buffer_line(end[X_AXIS], end[Y_AXIS], end[Z_AXIS] + z0 + ubl.state.z_offset, end[E_AXIS], feed_rate, extruder);
-
- if (ubl.g26_debug_flag)
- debug_current_and_destination(PSTR("FINAL_MOVE in ubl_line_to_destination()"));
-
- set_current_to_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_AXIS] - start[E_AXIS],
- 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 = NEAR_ZERO(on_axis_distance),
- inf_m_flag = NEAR_ZERO(dx);
-
-
-
- if (dxi == 0) {
- current_yi += down_flag;
- while (current_yi != cell_dest_yi + down_flag) {
- current_yi += dyi;
- const float next_mesh_line_y = LOGICAL_Y_POSITION(ubl.mesh_index_to_ypos[current_yi]);
-
-
-
- const float x = inf_m_flag ? start[X_AXIS] : (next_mesh_line_y - c) / m;
-
- float z0 = ubl.z_correction_for_x_on_horizontal_mesh_line(x, current_xi, current_yi);
-
-
-
-
-
-
- z0 *= ubl.fade_scaling_factor_for_z(end[Z_AXIS]);
-
-
-
- if (isnan(z0)) z0 = 0.0;
-
- const float y = LOGICAL_Y_POSITION(ubl.mesh_index_to_ypos[current_yi]);
-
-
-
- if (y != start[Y_AXIS]) {
- if (!inf_normalized_flag) {
- on_axis_distance = y - start[Y_AXIS];
- e_position = start[E_AXIS] + on_axis_distance * e_normalized_dist;
- z_position = start[Z_AXIS] + on_axis_distance * z_normalized_dist;
- }
- else {
- e_position = start[E_AXIS];
- z_position = start[Z_AXIS];
- }
-
- planner.buffer_line(x, y, z_position + z0 + ubl.state.z_offset, e_position, feed_rate, extruder);
- }
- }
-
- if (ubl.g26_debug_flag)
- debug_current_and_destination(PSTR("vertical move done in ubl_line_to_destination()"));
-
-
-
-
- if (current_position[X_AXIS] != end[X_AXIS] || current_position[Y_AXIS] != end[Y_AXIS])
- goto FINAL_MOVE;
-
- set_current_to_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 = LOGICAL_X_POSITION(ubl.mesh_index_to_xpos[current_xi]),
- y = m * next_mesh_line_x + c;
-
- float z0 = ubl.z_correction_for_y_on_vertical_mesh_line(y, current_xi, current_yi);
-
-
-
-
-
-
- z0 *= ubl.fade_scaling_factor_for_z(end[Z_AXIS]);
-
-
-
- if (isnan(z0)) z0 = 0.0;
-
- const float x = LOGICAL_X_POSITION(ubl.mesh_index_to_xpos[current_xi]);
-
-
-
- if (x != start[X_AXIS]) {
- if (!inf_normalized_flag) {
- on_axis_distance = x - start[X_AXIS];
- e_position = start[E_AXIS] + on_axis_distance * e_normalized_dist;
- z_position = start[Z_AXIS] + on_axis_distance * z_normalized_dist;
- }
- else {
- e_position = start[E_AXIS];
- z_position = start[Z_AXIS];
- }
-
- planner.buffer_line(x, y, z_position + z0 + ubl.state.z_offset, e_position, feed_rate, extruder);
- }
- }
-
- if (ubl.g26_debug_flag)
- debug_current_and_destination(PSTR("horizontal move done in ubl_line_to_destination()"));
-
- if (current_position[X_AXIS] != end[X_AXIS] || current_position[Y_AXIS] != end[Y_AXIS])
- goto FINAL_MOVE;
-
- set_current_to_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 > 0 || yi_cnt > 0) {
-
- const float next_mesh_line_x = LOGICAL_X_POSITION(ubl.mesh_index_to_xpos[current_xi + dxi]),
- next_mesh_line_y = LOGICAL_Y_POSITION(ubl.mesh_index_to_ypos[current_yi + dyi]),
- y = m * next_mesh_line_x + c,
- x = (next_mesh_line_y - c) / m;
-
-
-
-
- if (left_flag == (x > next_mesh_line_x)) {
-
-
-
- float z0 = ubl.z_correction_for_x_on_horizontal_mesh_line(x, current_xi - left_flag, current_yi + dyi);
-
-
-
-
-
-
- z0 *= ubl.fade_scaling_factor_for_z(end[Z_AXIS]);
-
-
-
- if (isnan(z0)) z0 = 0.0;
-
- if (!inf_normalized_flag) {
- on_axis_distance = use_x_dist ? x - start[X_AXIS] : next_mesh_line_y - start[Y_AXIS];
- e_position = start[E_AXIS] + on_axis_distance * e_normalized_dist;
- z_position = start[Z_AXIS] + on_axis_distance * z_normalized_dist;
- }
- else {
- e_position = start[E_AXIS];
- z_position = start[Z_AXIS];
- }
- planner.buffer_line(x, next_mesh_line_y, z_position + z0 + ubl.state.z_offset, e_position, feed_rate, extruder);
- current_yi += dyi;
- yi_cnt--;
- }
- else {
-
-
-
- float z0 = ubl.z_correction_for_y_on_vertical_mesh_line(y, current_xi + dxi, current_yi - down_flag);
-
-
-
-
-
-
- z0 *= ubl.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] : y - start[Y_AXIS];
- e_position = start[E_AXIS] + on_axis_distance * e_normalized_dist;
- z_position = start[Z_AXIS] + on_axis_distance * z_normalized_dist;
- }
- else {
- e_position = start[E_AXIS];
- z_position = start[Z_AXIS];
- }
-
- planner.buffer_line(next_mesh_line_x, y, z_position + z0 + ubl.state.z_offset, e_position, feed_rate, extruder);
- current_xi += dxi;
- xi_cnt--;
- }
- }
-
- if (ubl.g26_debug_flag)
- debug_current_and_destination(PSTR("generic move done in ubl_line_to_destination()"));
-
- if (current_position[X_AXIS] != end[X_AXIS] || current_position[Y_AXIS] != end[Y_AXIS])
- goto FINAL_MOVE;
-
- set_current_to_destination();
- }
-
- #endif
|