Skip to content

Commit

Permalink
Encapsulate probe as singleton class
Browse files Browse the repository at this point in the history
  • Loading branch information
thinkyhead committed Feb 1, 2020
1 parent 43d3463 commit 97eaa41
Show file tree
Hide file tree
Showing 33 changed files with 338 additions and 300 deletions.
2 changes: 1 addition & 1 deletion Marlin/src/MarlinCore.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -977,7 +977,7 @@ void setup() {
#endif

#if HAS_Z_SERVO_PROBE
servo_probe_init();
probe.servo_probe_init();
#endif

#if HAS_PHOTOGRAPH
Expand Down
20 changes: 10 additions & 10 deletions Marlin/src/core/utility.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -83,42 +83,42 @@ void safe_delay(millis_t ms) {
#if HAS_BED_PROBE

#if !HAS_PROBE_XY_OFFSET
SERIAL_ECHOPAIR("Probe Offset X0 Y0 Z", probe_offset.z, " (");
SERIAL_ECHOPAIR("Probe Offset X0 Y0 Z", probe.offset.z, " (");
#else
SERIAL_ECHOPAIR_P(PSTR("Probe Offset X"), probe_offset.x, SP_Y_STR, probe_offset.y, SP_Z_STR, probe_offset.z);
if (probe_offset.x > 0)
SERIAL_ECHOPAIR_P(PSTR("Probe Offset X"), probe.offset_xy.x, SP_Y_STR, probe.offset_xy.y, SP_Z_STR, probe.offset.z);
if (probe.offset_xy.x > 0)
SERIAL_ECHOPGM(" (Right");
else if (probe_offset.x < 0)
else if (probe.offset_xy.x < 0)
SERIAL_ECHOPGM(" (Left");
else if (probe_offset.y != 0)
else if (probe.offset_xy.y != 0)
SERIAL_ECHOPGM(" (Middle");
else
SERIAL_ECHOPGM(" (Aligned With");

if (probe_offset.y > 0) {
if (probe.offset_xy.y > 0) {
#if IS_SCARA
SERIAL_ECHOPGM("-Distal");
#else
SERIAL_ECHOPGM("-Back");
#endif
}
else if (probe_offset.y < 0) {
else if (probe.offset_xy.y < 0) {
#if IS_SCARA
SERIAL_ECHOPGM("-Proximal");
#else
SERIAL_ECHOPGM("-Front");
#endif
}
else if (probe_offset.x != 0)
else if (probe.offset_xy.x != 0)
SERIAL_ECHOPGM("-Center");

SERIAL_ECHOPGM(" & ");

#endif

if (probe_offset.z < 0)
if (probe.offset.z < 0)
SERIAL_ECHOPGM("Below");
else if (probe_offset.z > 0)
else if (probe.offset.z > 0)
SERIAL_ECHOPGM("Above");
else
SERIAL_ECHOPGM("Same Z as");
Expand Down
4 changes: 2 additions & 2 deletions Marlin/src/feature/bedlevel/ubl/ubl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -173,10 +173,10 @@
serialprintPGM(csv ? PSTR("CSV:\n") : PSTR("LCD:\n"));
}

// Add XY probe offset from extruder because probe_at_point() subtracts them when
// Add XY probe offset from extruder because probe.probe_at_point() subtracts them when
// moving to the XY position to be measured. This ensures better agreement between
// the current Z position after G28 and the mesh values.
const xy_int8_t curr = closest_indexes(xy_pos_t(current_position) + probe_offset_xy);
const xy_int8_t curr = closest_indexes(xy_pos_t(current_position) + probe.offset_xy);

if (!lcd) SERIAL_EOL();
for (int8_t j = GRID_MAX_POINTS_Y - 1; j >= 0; j--) {
Expand Down
52 changes: 26 additions & 26 deletions Marlin/src/feature/bedlevel/ubl/ubl_G29.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -450,7 +450,7 @@
SERIAL_ECHO(g29_pos.y);
SERIAL_ECHOLNPGM(").\n");
}
const xy_pos_t near = g29_pos + probe_offset_xy;
const xy_pos_t near = g29_pos + probe.offset_xy;
probe_entire_mesh(near, parser.seen('T'), parser.seen('E'), parser.seen('U'));

report_current_position();
Expand Down Expand Up @@ -480,8 +480,8 @@
#if IS_KINEMATIC
X_HOME_POS, Y_HOME_POS
#else
probe_offset_xy.x > 0 ? X_BED_SIZE : 0,
probe_offset_xy.y < 0 ? Y_BED_SIZE : 0
probe.offset_xy.x > 0 ? X_BED_SIZE : 0,
probe.offset_xy.y < 0 ? Y_BED_SIZE : 0
#endif
);
}
Expand Down Expand Up @@ -742,7 +742,7 @@
* This attempts to fill in locations closest to the nozzle's start location first.
*/
void unified_bed_leveling::probe_entire_mesh(const xy_pos_t &near, const bool do_ubl_mesh_map, const bool stow_probe, const bool do_furthest) {
DEPLOY_PROBE(); // Deploy before ui.capture() to allow for PAUSE_BEFORE_DEPLOY_STOW
probe.deploy(); // Deploy before ui.capture() to allow for PAUSE_BEFORE_DEPLOY_STOW

#if HAS_LCD_MENU
ui.capture();
Expand All @@ -768,7 +768,7 @@
ui.wait_for_release();
ui.quick_feedback();
ui.release();
STOW_PROBE(); // Release UI before stow to allow for PAUSE_BEFORE_DEPLOY_STOW
probe.stow(); // Release UI before stow to allow for PAUSE_BEFORE_DEPLOY_STOW
return restore_ubl_active_state_and_leave();
}
#endif
Expand All @@ -778,7 +778,7 @@
: find_closest_mesh_point_of_type(INVALID, near, true);

if (best.pos.x >= 0) { // mesh point found and is reachable by probe
const float measured_z = probe_at_point(
const float measured_z = probe.probe_at_point(
best.meshpos(),
stow_probe ? PROBE_PT_STOW : PROBE_PT_RAISE, g29_verbose_level
);
Expand All @@ -794,20 +794,20 @@
#if HAS_LCD_MENU
ui.release();
#endif
STOW_PROBE(); // Release UI during stow to allow for PAUSE_BEFORE_DEPLOY_STOW
probe.stow(); // Release UI during stow to allow for PAUSE_BEFORE_DEPLOY_STOW
#if HAS_LCD_MENU
ui.capture();
#endif

#ifdef Z_AFTER_PROBING
move_z_after_probing();
probe.move_z_after_probing();
#endif

restore_ubl_active_state_and_leave();

do_blocking_move_to_xy(
constrain(near.x - probe_offset_xy.x, MESH_MIN_X, MESH_MAX_X),
constrain(near.y - probe_offset_xy.y, MESH_MIN_Y, MESH_MAX_Y)
constrain(near.x - probe.offset_xy.x, MESH_MIN_X, MESH_MAX_X),
constrain(near.y - probe.offset_xy.y, MESH_MIN_Y, MESH_MAX_Y)
);
}

Expand Down Expand Up @@ -907,7 +907,7 @@
ui.return_to_status();

mesh_index_pair location;
xy_int8_t &lpos = location.pos;
const xy_int8_t &lpos = location.pos;
do {
location = find_closest_mesh_point_of_type(INVALID, pos);
// It doesn't matter if the probe can't reach the NAN location. This is a manual probe.
Expand Down Expand Up @@ -1006,7 +1006,7 @@
#endif

MeshFlags done_flags{0};
xy_int8_t &lpos = location.pos;
const xy_int8_t &lpos = location.pos;
do {
location = find_closest_mesh_point_of_type(SET_IN_BITMAP, pos, false, &done_flags);

Expand Down Expand Up @@ -1294,7 +1294,7 @@
closest.distance = -99999.9f;

// Get the reference position, either nozzle or probe
const xy_pos_t ref = probe_relative ? pos + probe_offset_xy : pos;
const xy_pos_t ref = probe_relative ? pos + probe.offset_xy : pos;

float best_so_far = 99999.99f;

Expand Down Expand Up @@ -1393,13 +1393,13 @@
#include "../../../libs/vector_3.h"

void unified_bed_leveling::tilt_mesh_based_on_probed_grid(const bool do_3_pt_leveling) {
const float x_min = probe_min_x(), x_max = probe_max_x(),
y_min = probe_min_y(), y_max = probe_max_y(),
const float x_min = probe.min_x(), x_max = probe.max_x(),
y_min = probe.min_y(), y_max = probe.max_y(),
dx = (x_max - x_min) / (g29_grid_size - 1),
dy = (y_max - y_min) / (g29_grid_size - 1);

xy_float_t points[3];
get_three_probe_points(points);
probe.get_three_points(points);

float measured_z;
bool abort_flag = false;
Expand All @@ -1417,7 +1417,7 @@
ui.status_printf_P(0, PSTR(S_FMT " 1/3"), GET_TEXT(MSG_LCD_TILTING_MESH));
#endif

measured_z = probe_at_point(points[0], PROBE_PT_RAISE, g29_verbose_level);
measured_z = probe.probe_at_point(points[0], PROBE_PT_RAISE, g29_verbose_level);
if (isnan(measured_z))
abort_flag = true;
else {
Expand All @@ -1438,7 +1438,7 @@
ui.status_printf_P(0, PSTR(S_FMT " 2/3"), GET_TEXT(MSG_LCD_TILTING_MESH));
#endif

measured_z = probe_at_point(points[1], PROBE_PT_RAISE, g29_verbose_level);
measured_z = probe.probe_at_point(points[1], PROBE_PT_RAISE, g29_verbose_level);
#ifdef VALIDATE_MESH_TILT
z2 = measured_z;
#endif
Expand All @@ -1460,7 +1460,7 @@
ui.status_printf_P(0, PSTR(S_FMT " 3/3"), GET_TEXT(MSG_LCD_TILTING_MESH));
#endif

measured_z = probe_at_point(points[2], PROBE_PT_STOW, g29_verbose_level);
measured_z = probe.probe_at_point(points[2], PROBE_PT_STOW, g29_verbose_level);
#ifdef VALIDATE_MESH_TILT
z3 = measured_z;
#endif
Expand All @@ -1476,9 +1476,9 @@
}
}

STOW_PROBE();
probe.stow();
#ifdef Z_AFTER_PROBING
move_z_after_probing();
probe.move_z_after_probing();
#endif

if (abort_flag) {
Expand All @@ -1504,7 +1504,7 @@
ui.status_printf_P(0, PSTR(S_FMT " %i/%i"), GET_TEXT(MSG_LCD_TILTING_MESH), point_num, total_points);
#endif

measured_z = probe_at_point(rpos, parser.seen('E') ? PROBE_PT_STOW : PROBE_PT_RAISE, g29_verbose_level); // TODO: Needs error handling
measured_z = probe.probe_at_point(rpos, parser.seen('E') ? PROBE_PT_STOW : PROBE_PT_RAISE, g29_verbose_level); // TODO: Needs error handling

abort_flag = isnan(measured_z);

Expand All @@ -1523,7 +1523,7 @@
}
#endif

measured_z -= get_z_correction(rpos) /* + probe_offset.z */ ;
measured_z -= get_z_correction(rpos) /* + probe.offset.z */ ;

if (DEBUGGING(LEVELING)) DEBUG_ECHOLNPAIR_F(" final >>>---> ", measured_z, 7);

Expand All @@ -1540,9 +1540,9 @@
zig_zag ^= true;
}
}
STOW_PROBE();
probe.stow();
#ifdef Z_AFTER_PROBING
move_z_after_probing();
probe.move_z_after_probing();
#endif

if (abort_flag || finish_incremental_LSF(&lsf_results)) {
Expand Down Expand Up @@ -1728,7 +1728,7 @@
adjust_mesh_to_mean(g29_c_flag, g29_constant);

#if HAS_BED_PROBE
SERIAL_ECHOLNPAIR_F("Probe Offset M851 Z", probe_offset.z, 7);
SERIAL_ECHOLNPAIR_F("Probe Offset M851 Z", probe.offset.z, 7);
#endif

SERIAL_ECHOLNPAIR("MESH_MIN_X " STRINGIFY(MESH_MIN_X) "=", MESH_MIN_X); serial_delay(50);
Expand Down
6 changes: 3 additions & 3 deletions Marlin/src/gcode/bedlevel/G42.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
#include "../gcode.h"
#include "../../MarlinCore.h" // for IsRunning()
#include "../../module/motion.h"
#include "../../module/probe.h" // for probe_offset
#include "../../module/probe.h" // for probe.offset
#include "../../feature/bedlevel/bedlevel.h"

/**
Expand All @@ -53,8 +53,8 @@ void GcodeSuite::G42() {

#if HAS_PROBE_XY_OFFSET
if (parser.boolval('P')) {
if (hasI) destination.x -= probe_offset_xy.x;
if (hasJ) destination.y -= probe_offset_xy.y;
if (hasI) destination.x -= probe.offset_xy.x;
if (hasJ) destination.y -= probe.offset_xy.y;
}
#endif

Expand Down
4 changes: 2 additions & 2 deletions Marlin/src/gcode/bedlevel/M420.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,8 @@ void GcodeSuite::M420() {

#if ENABLED(MARLIN_DEV_MODE)
if (parser.intval('S') == 2) {
const float x_min = probe_min_x(), x_max = probe_max_x(),
y_min = probe_min_y(), y_max = probe_max_y();
const float x_min = probe.min_x(), x_max = probe.max_x(),
y_min = probe.min_y(), y_max = probe.max_y();
#if ENABLED(AUTO_BED_LEVELING_BILINEAR)
bilinear_start.set(x_min, y_min);
bilinear_grid_spacing.set((x_max - x_min) / (GRID_MAX_POINTS_X - 1),
Expand Down
20 changes: 10 additions & 10 deletions Marlin/src/gcode/bedlevel/abl/G29.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -269,7 +269,7 @@ G29_TYPE GcodeSuite::G29() {
#endif

vector_3 points[3];
get_three_probe_points(points);
probe.get_three_points(points);

#endif // AUTO_BED_LEVELING_3POINT

Expand Down Expand Up @@ -392,8 +392,8 @@ G29_TYPE GcodeSuite::G29() {

xy_probe_feedrate_mm_s = MMM_TO_MMS(parser.linearval('S', XY_PROBE_SPEED));

const float x_min = probe_min_x(), x_max = probe_max_x(),
y_min = probe_min_y(), y_max = probe_max_y();
const float x_min = probe.min_x(), x_max = probe.max_x(),
y_min = probe.min_y(), y_max = probe.max_y();

if (parser.seen('H')) {
const int16_t size = (int16_t)parser.value_linear_units();
Expand Down Expand Up @@ -452,7 +452,7 @@ G29_TYPE GcodeSuite::G29() {

#if HAS_BED_PROBE
// Deploy the probe. Probe will raise if needed.
if (DEPLOY_PROBE()) {
if (probe.deploy()) {
set_bed_leveling_enabled(abl_should_enable);
G29_RETURN(false);
}
Expand Down Expand Up @@ -712,7 +712,7 @@ G29_TYPE GcodeSuite::G29() {
ui.status_printf_P(0, PSTR(S_FMT " %i/%i"), GET_TEXT(MSG_PROBING_MESH), int(pt_index), int(GRID_MAX_POINTS));
#endif

measured_z = faux ? 0.001f * random(-100, 101) : probe_at_point(probePos, raise_after, verbose_level);
measured_z = faux ? 0.001f * random(-100, 101) : probe.probe_at_point(probePos, raise_after, verbose_level);

if (isnan(measured_z)) {
set_bed_leveling_enabled(abl_should_enable);
Expand Down Expand Up @@ -764,7 +764,7 @@ G29_TYPE GcodeSuite::G29() {

// Retain the last probe position
probePos = points[i];
measured_z = faux ? 0.001 * random(-100, 101) : probe_at_point(probePos, raise_after, verbose_level);
measured_z = faux ? 0.001 * random(-100, 101) : probe.probe_at_point(probePos, raise_after, verbose_level);
if (isnan(measured_z)) {
set_bed_leveling_enabled(abl_should_enable);
break;
Expand All @@ -788,7 +788,7 @@ G29_TYPE GcodeSuite::G29() {
#endif

// Stow the probe. No raise for FIX_MOUNTED_PROBE.
if (STOW_PROBE()) {
if (probe.stow()) {
set_bed_leveling_enabled(abl_should_enable);
measured_z = NAN;
}
Expand Down Expand Up @@ -923,8 +923,8 @@ G29_TYPE GcodeSuite::G29() {
planner.force_unapply_leveling(converted); // use conversion machinery

// Use the last measured distance to the bed, if possible
if ( NEAR(current_position.x, probePos.x - probe_offset_xy.x)
&& NEAR(current_position.y, probePos.y - probe_offset_xy.y)
if ( NEAR(current_position.x, probePos.x - probe.offset_xy.x)
&& NEAR(current_position.y, probePos.y - probe.offset_xy.y)
) {
const float simple_z = current_position.z - measured_z;
if (DEBUGGING(LEVELING)) DEBUG_ECHOLNPAIR("Probed Z", simple_z, " Matrix Z", converted.z, " Discrepancy ", simple_z - converted.z);
Expand Down Expand Up @@ -964,7 +964,7 @@ G29_TYPE GcodeSuite::G29() {
sync_plan_position();

#if HAS_BED_PROBE && defined(Z_AFTER_PROBING)
move_z_after_probing();
probe.move_z_after_probing();
#endif

#ifdef Z_PROBE_END_SCRIPT
Expand Down
4 changes: 2 additions & 2 deletions Marlin/src/gcode/calibrate/G28.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@
destination.set(safe_homing_xy, current_position.z);

#if HOMING_Z_WITH_PROBE
destination -= probe_offset_xy;
destination -= probe.offset_xy;
#endif

if (position_is_reachable(destination)) {
Expand Down Expand Up @@ -416,7 +416,7 @@ void GcodeSuite::G28(const bool always_home_all) {
#endif

#if HOMING_Z_WITH_PROBE && defined(Z_AFTER_PROBING)
move_z_after_probing();
probe.move_z_after_probing();
#endif

} // doZ
Expand Down
Loading

0 comments on commit 97eaa41

Please sign in to comment.