Skip to content

Commit 8f8427e

Browse files
⚡️ Apply PTC on all probing (MarlinFirmware#23764)
Co-authored-by: Scott Lahteine <[email protected]>
1 parent a9682f2 commit 8f8427e

File tree

8 files changed

+48
-14
lines changed

8 files changed

+48
-14
lines changed

Marlin/src/feature/probe_temp_comp.cpp

+9
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828

2929
#include "probe_temp_comp.h"
3030
#include <math.h>
31+
#include "../module/temperature.h"
3132

3233
ProbeTempComp ptc;
3334

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

6364
uint8_t ProbeTempComp::calib_idx; // = 0
6465
float ProbeTempComp::init_measurement; // = 0.0
66+
bool ProbeTempComp::enabled = true;
6567

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

174+
void ProbeTempComp::apply_compensation(float &meas_z) {
175+
if (!enabled) return;
176+
TERN_(PTC_BED, compensate_measurement(TSI_BED, thermalManager.degBed(), meas_z));
177+
TERN_(PTC_PROBE, compensate_measurement(TSI_PROBE, thermalManager.degProbe(), meas_z));
178+
TERN_(PTC_HOTEND, compensate_measurement(TSI_EXT, thermalManager.degHotend(0), meas_z));
179+
}
180+
172181
void ProbeTempComp::compensate_measurement(const TempSensorID tsi, const celsius_t temp, float &meas_z) {
173182
const uint8_t measurements = cali_info[tsi].measurements;
174183
const celsius_t start_temp = cali_info[tsi].start_temp,

Marlin/src/feature/probe_temp_comp.h

+9-2
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,6 @@ class ProbeTempComp {
7777
static void reset_index() { calib_idx = 0; };
7878
static uint8_t get_index() { return calib_idx; }
7979
static void reset();
80-
static void clear_offsets(const TempSensorID tsi);
8180
static void clear_all_offsets() {
8281
TERN_(PTC_PROBE, clear_offsets(TSI_PROBE));
8382
TERN_(PTC_BED, clear_offsets(TSI_BED));
@@ -88,10 +87,16 @@ class ProbeTempComp {
8887
static void prepare_new_calibration(const_float_t init_meas_z);
8988
static void push_back_new_measurement(const TempSensorID tsi, const_float_t meas_z);
9089
static bool finish_calibration(const TempSensorID tsi);
91-
static void compensate_measurement(const TempSensorID tsi, const celsius_t temp, float &meas_z);
90+
static void set_enabled(const bool ena) { enabled = ena; }
91+
92+
// Apply all temperature compensation adjustments
93+
static void apply_compensation(float &meas_z);
9294

9395
private:
9496
static uint8_t calib_idx;
97+
static bool enabled;
98+
99+
static void clear_offsets(const TempSensorID tsi);
95100

96101
/**
97102
* Base value. Temperature compensation values will be deltas
@@ -104,6 +109,8 @@ class ProbeTempComp {
104109
* to allow generating values of higher temperatures.
105110
*/
106111
static bool linear_regression(const TempSensorID tsi, float &k, float &d);
112+
113+
static void compensate_measurement(const TempSensorID tsi, const celsius_t temp, float &meas_z);
107114
};
108115

109116
extern ProbeTempComp ptc;

Marlin/src/gcode/bedlevel/abl/G29.cpp

-9
Original file line numberDiff line numberDiff line change
@@ -36,11 +36,6 @@
3636
#include "../../../module/probe.h"
3737
#include "../../queue.h"
3838

39-
#if HAS_PTC
40-
#include "../../../feature/probe_temp_comp.h"
41-
#include "../../../module/temperature.h"
42-
#endif
43-
4439
#if HAS_STATUS_MESSAGE
4540
#include "../../../lcd/marlinui.h"
4641
#endif
@@ -658,10 +653,6 @@ G29_TYPE GcodeSuite::G29() {
658653
break; // Breaks out of both loops
659654
}
660655

661-
TERN_(PTC_BED, ptc.compensate_measurement(TSI_BED, thermalManager.degBed(), abl.measured_z));
662-
TERN_(PTC_PROBE, ptc.compensate_measurement(TSI_PROBE, thermalManager.degProbe(), abl.measured_z));
663-
TERN_(PTC_HOTEND, ptc.compensate_measurement(TSI_EXT, thermalManager.degHotend(0), abl.measured_z));
664-
665656
#if ENABLED(AUTO_BED_LEVELING_LINEAR)
666657

667658
abl.mean += abl.measured_z;

Marlin/src/gcode/calibrate/G76_M871.cpp

+2
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,9 @@ static void say_failed_to_calibrate() { SERIAL_ECHOPGM("!Failed to calibra
109109

110110
auto g76_probe = [](const TempSensorID sid, celsius_t &targ, const xy_pos_t &nozpos) {
111111
do_z_clearance(5.0); // Raise nozzle before probing
112+
ptc.set_enabled(false);
112113
const float measured_z = probe.probe_at_point(nozpos, PROBE_PT_STOW, 0, false); // verbose=0, probe_relative=false
114+
ptc.set_enabled(true);
113115
if (isnan(measured_z))
114116
SERIAL_ECHOLNPGM("!Received NAN. Aborting.");
115117
else {

Marlin/src/gcode/calibrate/M48.cpp

+11-1
Original file line numberDiff line numberDiff line change
@@ -35,18 +35,23 @@
3535
#include "../../module/planner.h"
3636
#endif
3737

38+
#if HAS_PTC
39+
#include "../../feature/probe_temp_comp.h"
40+
#endif
41+
3842
/**
3943
* M48: Z probe repeatability measurement function.
4044
*
4145
* Usage:
42-
* M48 <P#> <X#> <Y#> <V#> <E> <L#> <S>
46+
* M48 <P#> <X#> <Y#> <V#> <E> <L#> <S> <C#>
4347
* P = Number of sampled points (4-50, default 10)
4448
* X = Sample X position
4549
* Y = Sample Y position
4650
* V = Verbose level (0-4, default=1)
4751
* E = Engage Z probe for each reading
4852
* L = Number of legs of movement before probe
4953
* S = Schizoid (Or Star if you prefer)
54+
* C = Enable probe temperature compensation (0 or 1, default 1)
5055
*
5156
* This function requires the machine to be homed before invocation.
5257
*/
@@ -107,6 +112,8 @@ void GcodeSuite::M48() {
107112
set_bed_leveling_enabled(false);
108113
#endif
109114

115+
TERN_(HAS_PTC, ptc.set_enabled(!parser.seen('C') || parser.value_bool()));
116+
110117
// Work with reasonable feedrates
111118
remember_feedrate_scaling_off();
112119

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

279+
// Re-enable probe temperature correction
280+
TERN_(HAS_PTC, ptc.set_enabled(true));
281+
272282
report_current_position();
273283
}
274284

Marlin/src/gcode/probe/G30.cpp

+8
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,10 @@
2929
#include "../../module/probe.h"
3030
#include "../../feature/bedlevel/bedlevel.h"
3131

32+
#if HAS_PTC
33+
#include "../../feature/probe_temp_comp.h"
34+
#endif
35+
3236
/**
3337
* G30: Do a single Z probe at the current XY
3438
*
@@ -37,6 +41,7 @@
3741
* X Probe X position (default current X)
3842
* Y Probe Y position (default current Y)
3943
* E Engage the probe for each probe (default 1)
44+
* C Enable probe temperature compensation (0 or 1, default 1)
4045
*/
4146
void GcodeSuite::G30() {
4247

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

5358
const ProbePtRaise raise_after = parser.boolval('E', true) ? PROBE_PT_STOW : PROBE_PT_NONE;
59+
60+
TERN_(HAS_PTC, ptc.set_enabled(!parser.seen('C') || parser.value_bool()));
5461
const float measured_z = probe.probe_at_point(pos, raise_after, 1);
62+
TERN_(HAS_PTC, ptc.set_enabled(true));
5563
if (!isnan(measured_z))
5664
SERIAL_ECHOLNPGM("Bed X: ", pos.x, " Y: ", pos.y, " Z: ", measured_z);
5765

Marlin/src/lcd/menu/menu_probe_offset.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ void probe_offset_wizard_menu() {
6262
if (LCD_HEIGHT >= 4)
6363
STATIC_ITEM(MSG_MOVE_NOZZLE_TO_BED, SS_CENTER|SS_INVERT);
6464

65-
STATIC_ITEM_P(PSTR("Z="), SS_CENTER, ftostr42_52(current_position.z));
65+
STATIC_ITEM_P(PSTR("Z"), SS_CENTER, ftostr42_52(current_position.z));
6666
STATIC_ITEM(MSG_ZPROBE_ZOFFSET, SS_LEFT, ftostr42_52(calculated_z_offset));
6767

6868
SUBMENU(MSG_MOVE_1MM, []{ _goto_manual_move_z( 1); });

Marlin/src/module/probe.cpp

+8-1
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,10 @@
7777
#include "servo.h"
7878
#endif
7979

80+
#if HAS_PTC
81+
#include "../feature/probe_temp_comp.h"
82+
#endif
83+
8084
#if ENABLED(EXTENSIBLE_UI)
8185
#include "../lcd/extui/ui_api.h"
8286
#elif ENABLED(DWIN_CREALITY_LCD_ENHANCED)
@@ -801,7 +805,10 @@ float Probe::probe_at_point(const_float_t rx, const_float_t ry, const ProbePtRai
801805
do_blocking_move_to(npos, feedRate_t(XY_PROBE_FEEDRATE_MM_S));
802806

803807
float measured_z = NAN;
804-
if (!deploy()) measured_z = run_z_probe(sanity_check) + offset.z;
808+
if (!deploy()) {
809+
measured_z = run_z_probe(sanity_check) + offset.z;
810+
TERN_(HAS_PTC, ptc.apply_compensation(measured_z));
811+
}
805812
if (!isnan(measured_z)) {
806813
const bool big_raise = raise_after == PROBE_PT_BIG_RAISE;
807814
if (big_raise || raise_after == PROBE_PT_RAISE)

0 commit comments

Comments
 (0)