Skip to content

Commit

Permalink
⚡️ Apply PTC on all probing (MarlinFirmware#23764)
Browse files Browse the repository at this point in the history
Co-authored-by: Scott Lahteine <thinkyhead@users.noreply.github.com>
  • Loading branch information
2 people authored and Omkar Dhekne committed Mar 25, 2024
1 parent 683bc26 commit e4b496a
Show file tree
Hide file tree
Showing 8 changed files with 48 additions and 14 deletions.
9 changes: 9 additions & 0 deletions Marlin/src/feature/probe_temp_comp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@

#include "probe_temp_comp.h"
#include <math.h>
#include "../module/temperature.h"

ProbeTempComp ptc;

Expand Down Expand Up @@ -62,6 +63,7 @@ constexpr temp_calib_t ProbeTempComp::cali_info[TSI_COUNT];

uint8_t ProbeTempComp::calib_idx; // = 0
float ProbeTempComp::init_measurement; // = 0.0
bool ProbeTempComp::enabled = true;

void ProbeTempComp::reset() {
TERN_(PTC_PROBE, LOOP_L_N(i, PTC_PROBE_COUNT) z_offsets_probe[i] = z_offsets_probe_default[i]);
Expand Down Expand Up @@ -169,6 +171,13 @@ bool ProbeTempComp::finish_calibration(const TempSensorID tsi) {
return true;
}

void ProbeTempComp::apply_compensation(float &meas_z) {
if (!enabled) return;
TERN_(PTC_BED, compensate_measurement(TSI_BED, thermalManager.degBed(), meas_z));
TERN_(PTC_PROBE, compensate_measurement(TSI_PROBE, thermalManager.degProbe(), meas_z));
TERN_(PTC_HOTEND, compensate_measurement(TSI_EXT, thermalManager.degHotend(0), meas_z));
}

void ProbeTempComp::compensate_measurement(const TempSensorID tsi, const celsius_t temp, float &meas_z) {
const uint8_t measurements = cali_info[tsi].measurements;
const celsius_t start_temp = cali_info[tsi].start_temp,
Expand Down
11 changes: 9 additions & 2 deletions Marlin/src/feature/probe_temp_comp.h
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,6 @@ class ProbeTempComp {
static void reset_index() { calib_idx = 0; };
static uint8_t get_index() { return calib_idx; }
static void reset();
static void clear_offsets(const TempSensorID tsi);
static void clear_all_offsets() {
TERN_(PTC_PROBE, clear_offsets(TSI_PROBE));
TERN_(PTC_BED, clear_offsets(TSI_BED));
Expand All @@ -88,10 +87,16 @@ class ProbeTempComp {
static void prepare_new_calibration(const_float_t init_meas_z);
static void push_back_new_measurement(const TempSensorID tsi, const_float_t meas_z);
static bool finish_calibration(const TempSensorID tsi);
static void compensate_measurement(const TempSensorID tsi, const celsius_t temp, float &meas_z);
static void set_enabled(const bool ena) { enabled = ena; }

// Apply all temperature compensation adjustments
static void apply_compensation(float &meas_z);

private:
static uint8_t calib_idx;
static bool enabled;

static void clear_offsets(const TempSensorID tsi);

/**
* Base value. Temperature compensation values will be deltas
Expand All @@ -104,6 +109,8 @@ class ProbeTempComp {
* to allow generating values of higher temperatures.
*/
static bool linear_regression(const TempSensorID tsi, float &k, float &d);

static void compensate_measurement(const TempSensorID tsi, const celsius_t temp, float &meas_z);
};

extern ProbeTempComp ptc;
9 changes: 0 additions & 9 deletions Marlin/src/gcode/bedlevel/abl/G29.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,6 @@
#include "../../../module/probe.h"
#include "../../queue.h"

#if HAS_PTC
#include "../../../feature/probe_temp_comp.h"
#include "../../../module/temperature.h"
#endif

#if HAS_STATUS_MESSAGE
#include "../../../lcd/marlinui.h"
#endif
Expand Down Expand Up @@ -658,10 +653,6 @@ G29_TYPE GcodeSuite::G29() {
break; // Breaks out of both loops
}

TERN_(PTC_BED, ptc.compensate_measurement(TSI_BED, thermalManager.degBed(), abl.measured_z));
TERN_(PTC_PROBE, ptc.compensate_measurement(TSI_PROBE, thermalManager.degProbe(), abl.measured_z));
TERN_(PTC_HOTEND, ptc.compensate_measurement(TSI_EXT, thermalManager.degHotend(0), abl.measured_z));

#if ENABLED(AUTO_BED_LEVELING_LINEAR)

abl.mean += abl.measured_z;
Expand Down
2 changes: 2 additions & 0 deletions Marlin/src/gcode/calibrate/G76_M871.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,9 @@ static void say_failed_to_calibrate() { SERIAL_ECHOPGM("!Failed to calibra

auto g76_probe = [](const TempSensorID sid, celsius_t &targ, const xy_pos_t &nozpos) {
do_z_clearance(5.0); // Raise nozzle before probing
ptc.set_enabled(false);
const float measured_z = probe.probe_at_point(nozpos, PROBE_PT_STOW, 0, false); // verbose=0, probe_relative=false
ptc.set_enabled(true);
if (isnan(measured_z))
SERIAL_ECHOLNPGM("!Received NAN. Aborting.");
else {
Expand Down
12 changes: 11 additions & 1 deletion Marlin/src/gcode/calibrate/M48.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,18 +35,23 @@
#include "../../module/planner.h"
#endif

#if HAS_PTC
#include "../../feature/probe_temp_comp.h"
#endif

/**
* M48: Z probe repeatability measurement function.
*
* Usage:
* M48 <P#> <X#> <Y#> <V#> <E> <L#> <S>
* M48 <P#> <X#> <Y#> <V#> <E> <L#> <S> <C#>
* P = Number of sampled points (4-50, default 10)
* X = Sample X position
* Y = Sample Y position
* V = Verbose level (0-4, default=1)
* E = Engage Z probe for each reading
* L = Number of legs of movement before probe
* S = Schizoid (Or Star if you prefer)
* C = Enable probe temperature compensation (0 or 1, default 1)
*
* This function requires the machine to be homed before invocation.
*/
Expand Down Expand Up @@ -107,6 +112,8 @@ void GcodeSuite::M48() {
set_bed_leveling_enabled(false);
#endif

TERN_(HAS_PTC, ptc.set_enabled(!parser.seen('C') || parser.value_bool()));

// Work with reasonable feedrates
remember_feedrate_scaling_off();

Expand Down Expand Up @@ -269,6 +276,9 @@ void GcodeSuite::M48() {
// Re-enable bed level correction if it had been on
TERN_(HAS_LEVELING, set_bed_leveling_enabled(was_enabled));

// Re-enable probe temperature correction
TERN_(HAS_PTC, ptc.set_enabled(true));

report_current_position();
}

Expand Down
8 changes: 8 additions & 0 deletions Marlin/src/gcode/probe/G30.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,10 @@
#include "../../module/probe.h"
#include "../../feature/bedlevel/bedlevel.h"

#if HAS_PTC
#include "../../feature/probe_temp_comp.h"
#endif

/**
* G30: Do a single Z probe at the current XY
*
Expand All @@ -37,6 +41,7 @@
* X Probe X position (default current X)
* Y Probe Y position (default current Y)
* E Engage the probe for each probe (default 1)
* C Enable probe temperature compensation (0 or 1, default 1)
*/
void GcodeSuite::G30() {

Expand All @@ -51,7 +56,10 @@ void GcodeSuite::G30() {
remember_feedrate_scaling_off();

const ProbePtRaise raise_after = parser.boolval('E', true) ? PROBE_PT_STOW : PROBE_PT_NONE;

TERN_(HAS_PTC, ptc.set_enabled(!parser.seen('C') || parser.value_bool()));
const float measured_z = probe.probe_at_point(pos, raise_after, 1);
TERN_(HAS_PTC, ptc.set_enabled(true));
if (!isnan(measured_z))
SERIAL_ECHOLNPGM("Bed X: ", pos.x, " Y: ", pos.y, " Z: ", measured_z);

Expand Down
2 changes: 1 addition & 1 deletion Marlin/src/lcd/menu/menu_probe_offset.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ void probe_offset_wizard_menu() {
if (LCD_HEIGHT >= 4)
STATIC_ITEM(MSG_MOVE_NOZZLE_TO_BED, SS_CENTER|SS_INVERT);

STATIC_ITEM_P(PSTR("Z="), SS_CENTER, ftostr42_52(current_position.z));
STATIC_ITEM_P(PSTR("Z"), SS_CENTER, ftostr42_52(current_position.z));
STATIC_ITEM(MSG_ZPROBE_ZOFFSET, SS_LEFT, ftostr42_52(calculated_z_offset));

SUBMENU(MSG_MOVE_1MM, []{ _goto_manual_move_z( 1); });
Expand Down
9 changes: 8 additions & 1 deletion Marlin/src/module/probe.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,10 @@
#include "servo.h"
#endif

#if HAS_PTC
#include "../feature/probe_temp_comp.h"
#endif

#if ENABLED(EXTENSIBLE_UI)
#include "../lcd/extui/ui_api.h"
#elif ENABLED(DWIN_CREALITY_LCD_ENHANCED)
Expand Down Expand Up @@ -801,7 +805,10 @@ float Probe::probe_at_point(const_float_t rx, const_float_t ry, const ProbePtRai
do_blocking_move_to(npos, feedRate_t(XY_PROBE_FEEDRATE_MM_S));

float measured_z = NAN;
if (!deploy()) measured_z = run_z_probe(sanity_check) + offset.z;
if (!deploy()) {
measured_z = run_z_probe(sanity_check) + offset.z;
TERN_(HAS_PTC, ptc.apply_compensation(measured_z));
}
if (!isnan(measured_z)) {
const bool big_raise = raise_after == PROBE_PT_BIG_RAISE;
if (big_raise || raise_after == PROBE_PT_RAISE)
Expand Down

0 comments on commit e4b496a

Please sign in to comment.