Skip to content

Commit

Permalink
🩹 Fix PID / MPC tune background tasks (MarlinFirmware#26652)
Browse files Browse the repository at this point in the history
Co-authored-by: Scott Lahteine <thinkyhead@users.noreply.github.com>
  • Loading branch information
ellensp and thinkyhead authored Jan 10, 2024
1 parent 12d7995 commit 25caae1
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 16 deletions.
56 changes: 40 additions & 16 deletions Marlin/src/module/temperature.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -628,6 +628,36 @@ volatile bool Temperature::raw_temps_ready = false;
* Class and Instance Methods
*/

#if ANY(HAS_PID_HEATING, MPC_AUTOTUNE)

/**
* Run the minimal required activities during a tuning loop.
* TODO: Allow tuning routines to call idle() for more complete keepalive.
*/
bool Temperature::tuning_idle(const millis_t &ms) {

// Run HAL idle tasks
hal.idletask();

const bool temp_ready = updateTemperaturesIfReady();

#if HAS_FAN_LOGIC
if (temp_ready) manage_extruder_fans(ms);
#else
UNUSED(ms);
#endif

// Run Controller Fan check (normally handled by manage_inactivity)
TERN_(USE_CONTROLLER_FAN, controllerFan.update());

// Run UI update
ui.update();

return temp_ready;
}

#endif

#if HAS_PID_HEATING

inline void say_default_() { SERIAL_ECHOPGM("#define DEFAULT_"); }
Expand Down Expand Up @@ -727,7 +757,11 @@ volatile bool Temperature::raw_temps_ready = false;

const millis_t ms = millis();

if (updateTemperaturesIfReady()) { // temp sample ready
// Run minimal necessary machine tasks
const bool temp_ready = tuning_idle(ms);

// If a new sample has arrived process things
if (temp_ready) {

// Get the current temperature and constrain it
current_temp = GHV(degChamber(), degBed(), degHotend(heater_id));
Expand All @@ -738,8 +772,6 @@ volatile bool Temperature::raw_temps_ready = false;
ONHEATING(start_temp, current_temp, target);
#endif

TERN_(HAS_FAN_LOGIC, manage_extruder_fans(ms));

if (heating && current_temp > target && ELAPSED(ms, t2 + 5000UL)) {
heating = false;
SHV((bias - d) >> 1);
Expand Down Expand Up @@ -885,12 +917,6 @@ volatile bool Temperature::raw_temps_ready = false;

goto EXIT_M303;
}

// Run HAL idle tasks
hal.idletask();

// Run UI update
ui.update();
}
wait_for_heatup = false;

Expand Down Expand Up @@ -1142,20 +1168,18 @@ volatile bool Temperature::raw_temps_ready = false;
constexpr millis_t report_interval_ms = 1000UL;
curr_time_ms = millis();

if (updateTemperaturesIfReady()) { // temp sample ready
current_temp = degHotend(e);
TERN_(HAS_FAN_LOGIC, manage_extruder_fans(curr_time_ms));
}
// Run minimal necessary machine tasks
const bool temp_ready = tuning_idle(curr_time_ms);

// Set MPC temp if a new sample is ready
if (temp_ready) current_temp = degHotend(e);

if (ELAPSED(curr_time_ms, next_report_ms)) {
next_report_ms += report_interval_ms;
print_heater_states(e);
SERIAL_EOL();
}

hal.idletask();
ui.update();

if (!wait_for_heatup) {
SERIAL_ECHOLNPGM(STR_MPC_AUTOTUNE_INTERRUPTED);
TERN_(DWIN_LCD_PROUI, dwinMPCTuning(MPC_INTERRUPTED));
Expand Down
2 changes: 2 additions & 0 deletions Marlin/src/module/temperature.h
Original file line number Diff line number Diff line change
Expand Up @@ -1197,6 +1197,8 @@ class Temperature {
static constexpr bool adaptive_fan_slowing = true;
#endif

static bool tuning_idle(const millis_t &ms);

/**
* M303 PID auto-tuning for hotends or bed
*/
Expand Down

0 comments on commit 25caae1

Please sign in to comment.