Skip to content

Commit 98f3028

Browse files
committed
Merge pull request #3650 from thinkyhead/rc_temp_change_during_m109_m190
Prevent stuck M109/M190 when target is changed
2 parents eeef571 + 6d3e4e1 commit 98f3028

File tree

1 file changed

+36
-18
lines changed

1 file changed

+36
-18
lines changed

Marlin/Marlin_main.cpp

Lines changed: 36 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -4463,15 +4463,6 @@ inline void gcode_M109() {
44634463
if (code_seen('B')) autotemp_max = code_value();
44644464
#endif
44654465

4466-
bool wants_to_cool = isCoolingHotend(target_extruder);
4467-
4468-
// Exit if S<lower>, continue if S<higher>, R<lower>, or R<higher>
4469-
if (no_wait_for_cooling && wants_to_cool) return;
4470-
4471-
// Prevents a wait-forever situation if R is misused i.e. M109 R0
4472-
// Try to calculate a ballpark safe margin by halving EXTRUDE_MINTEMP
4473-
if (wants_to_cool && degTargetHotend(target_extruder) < (EXTRUDE_MINTEMP)/2) return;
4474-
44754466
#if TEMP_RESIDENCY_TIME > 0
44764467
millis_t residency_start_ms = 0;
44774468
// Loop until the temperature has stabilized
@@ -4481,11 +4472,15 @@ inline void gcode_M109() {
44814472
#define TEMP_CONDITIONS (wants_to_cool ? isCoolingHotend(target_extruder) : isHeatingHotend(target_extruder))
44824473
#endif //TEMP_RESIDENCY_TIME > 0
44834474

4484-
KEEPALIVE_STATE(NOT_BUSY);
4485-
4475+
float theTarget = -1;
4476+
bool wants_to_cool;
44864477
cancel_heatup = false;
44874478
millis_t now, next_temp_ms = 0;
4479+
4480+
KEEPALIVE_STATE(NOT_BUSY);
4481+
44884482
do {
4483+
44894484
now = millis();
44904485
if (ELAPSED(now, next_temp_ms)) { //Print temp & remaining time every 1s while waiting
44914486
next_temp_ms = now + 1000UL;
@@ -4506,12 +4501,25 @@ inline void gcode_M109() {
45064501
#endif
45074502
}
45084503

4504+
// Target temperature might be changed during the loop
4505+
if (theTarget != degTargetHotend(target_extruder)) {
4506+
theTarget = degTargetHotend(target_extruder);
4507+
wants_to_cool = isCoolingHotend(target_extruder);
4508+
4509+
// Exit if S<lower>, continue if S<higher>, R<lower>, or R<higher>
4510+
if (no_wait_for_cooling && wants_to_cool) break;
4511+
4512+
// Prevent a wait-forever situation if R is misused i.e. M109 R0
4513+
// Try to calculate a ballpark safe margin by halving EXTRUDE_MINTEMP
4514+
if (wants_to_cool && theTarget < (EXTRUDE_MINTEMP)/2) break;
4515+
}
4516+
45094517
idle();
45104518
refresh_cmd_timeout(); // to prevent stepper_inactive_time from running out
45114519

45124520
#if TEMP_RESIDENCY_TIME > 0
45134521

4514-
float temp_diff = fabs(degTargetHotend(target_extruder) - degHotend(target_extruder));
4522+
float temp_diff = fabs(theTarget - degHotend(target_extruder));
45154523

45164524
if (!residency_start_ms) {
45174525
// Start the TEMP_RESIDENCY_TIME timer when we reach target temp for the first time.
@@ -4543,11 +4551,6 @@ inline void gcode_M109() {
45434551
bool no_wait_for_cooling = code_seen('S');
45444552
if (no_wait_for_cooling || code_seen('R')) setTargetBed(code_value());
45454553

4546-
bool wants_to_cool = isCoolingBed();
4547-
4548-
// Exit if S<lower>, continue if S<higher>, R<lower>, or R<higher>
4549-
if (no_wait_for_cooling && wants_to_cool) return;
4550-
45514554
#if TEMP_BED_RESIDENCY_TIME > 0
45524555
millis_t residency_start_ms = 0;
45534556
// Loop until the temperature has stabilized
@@ -4557,11 +4560,13 @@ inline void gcode_M109() {
45574560
#define TEMP_BED_CONDITIONS (wants_to_cool ? isCoolingBed() : isHeatingBed())
45584561
#endif //TEMP_BED_RESIDENCY_TIME > 0
45594562

4563+
float theTarget = -1;
4564+
bool wants_to_cool;
45604565
cancel_heatup = false;
45614566
millis_t now, next_temp_ms = 0;
45624567

4563-
// Wait for temperature to come close enough
45644568
KEEPALIVE_STATE(NOT_BUSY);
4569+
45654570
do {
45664571
now = millis();
45674572
if (ELAPSED(now, next_temp_ms)) { //Print Temp Reading every 1 second while heating up.
@@ -4581,6 +4586,19 @@ inline void gcode_M109() {
45814586
#endif
45824587
}
45834588

4589+
// Target temperature might be changed during the loop
4590+
if (theTarget != degTargetBed()) {
4591+
theTarget = degTargetBed();
4592+
wants_to_cool = isCoolingBed();
4593+
4594+
// Exit if S<lower>, continue if S<higher>, R<lower>, or R<higher>
4595+
if (no_wait_for_cooling && wants_to_cool) break;
4596+
4597+
// Prevent a wait-forever situation if R is misused i.e. M190 R0
4598+
// Simply don't wait for cooling below 30C
4599+
if (wants_to_cool && theTarget < (EXTRUDE_MINTEMP)/2) break;
4600+
}
4601+
45844602
idle();
45854603
refresh_cmd_timeout(); // to prevent stepper_inactive_time from running out
45864604

0 commit comments

Comments
 (0)