Skip to content

Commit

Permalink
🔧 Optional MPC_AUTOTUNE to save 5.5 - 5.8KB
Browse files Browse the repository at this point in the history
  • Loading branch information
thinkyhead committed Mar 18, 2023
1 parent 6f5b7a8 commit b5836b2
Show file tree
Hide file tree
Showing 12 changed files with 105 additions and 75 deletions.
1 change: 1 addition & 0 deletions Marlin/Configuration.h
Original file line number Diff line number Diff line change
Expand Up @@ -691,6 +691,7 @@
* @section mpctemp
*/
#if ENABLED(MPCTEMP)
//#define MPC_AUTOTUNE // Include a method to do MPC auto-tuning (~5664-5882 bytes of flash)
//#define MPC_EDIT_MENU // Add MPC editing to the "Advanced Settings" menu. (~1300 bytes of flash)
//#define MPC_AUTOTUNE_MENU // Add MPC auto-tuning to the "Advanced Settings" menu. (~350 bytes of flash)

Expand Down
7 changes: 3 additions & 4 deletions Marlin/src/core/language.h
Original file line number Diff line number Diff line change
Expand Up @@ -230,10 +230,9 @@
#define STR_PID_DEBUG_INPUT ": Input "
#define STR_PID_DEBUG_OUTPUT " Output "
#define STR_INVALID_EXTRUDER_NUM " - Invalid extruder number !"
#define STR_MPC_AUTOTUNE "MPC Autotune"
#define STR_MPC_AUTOTUNE_START " start for " STR_E
#define STR_MPC_AUTOTUNE_INTERRUPTED " interrupted!"
#define STR_MPC_AUTOTUNE_FINISHED " finished! Put the constants below into Configuration.h"
#define STR_MPC_AUTOTUNE_START "MPC Autotune start for " STR_E
#define STR_MPC_AUTOTUNE_INTERRUPTED "MPC Autotune interrupted!"
#define STR_MPC_AUTOTUNE_FINISHED "MPC Autotune finished! Put the constants below into Configuration.h"
#define STR_MPC_COOLING_TO_AMBIENT "Cooling to ambient"
#define STR_MPC_HEATING_PAST_200 "Heating to over 200C"
#define STR_MPC_MEASURING_AMBIENT "Measuring ambient heatloss at "
Expand Down
19 changes: 11 additions & 8 deletions Marlin/src/gcode/temp/M306.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,15 +33,16 @@
*
* E<extruder> Extruder index. (Default: Active Extruder)
*
* T Autotune the specified or active extruder.
*
* Set MPC values manually for the specified or active extruder:
* A<watts/kelvin> Ambient heat transfer coefficient (no fan).
* C<joules/kelvin> Block heat capacity.
* F<watts/kelvin> Ambient heat transfer coefficient (fan on full).
* H<joules/kelvin/mm> Filament heat capacity per mm.
* P<watts> Heater power.
* R<kelvin/second/kelvin> Sensor responsiveness (= transfer coefficient / heat capcity).
*
* With MPC_AUTOTUNE:
* T Autotune the specified or active extruder.
*/

void GcodeSuite::M306() {
Expand All @@ -51,12 +52,14 @@ void GcodeSuite::M306() {
return;
}

if (parser.seen_test('T')) {
LCD_MESSAGE(MSG_MPC_AUTOTUNE);
thermalManager.MPC_autotune(e);
ui.reset_status();
return;
}
#if ENABLED(MPC_AUTOTUNE)
if (parser.seen_test('T')) {
LCD_MESSAGE(MSG_MPC_AUTOTUNE);
thermalManager.MPC_autotune(e);
ui.reset_status();
return;
}
#endif

if (parser.seen("ACFPRH")) {
MPC_t &mpc = thermalManager.temp_hotend[e].mpc;
Expand Down
4 changes: 4 additions & 0 deletions Marlin/src/inc/Conditionals_LCD.h
Original file line number Diff line number Diff line change
Expand Up @@ -1471,6 +1471,10 @@
#define HAS_PID_DEBUG 1
#endif

#if DISABLED(MPC_AUTOTUNE)
#undef MPC_AUTOTUNE_MENU
#endif

/**
* TFT Displays
*
Expand Down
2 changes: 1 addition & 1 deletion Marlin/src/inc/Conditionals_post.h
Original file line number Diff line number Diff line change
Expand Up @@ -2499,7 +2499,7 @@
#if EITHER(PIDTEMP, PIDTEMPBED)
#define DWIN_PID_TUNE 1
#endif
#if EITHER(DWIN_PID_TUNE, MPCTEMP) && DISABLED(DISABLE_TUNING_GRAPH)
#if EITHER(DWIN_PID_TUNE, MPC_AUTOTUNE) && DISABLED(DISABLE_TUNING_GRAPH)
#define SHOW_TUNING_GRAPH 1
#endif
#endif
Expand Down
4 changes: 4 additions & 0 deletions Marlin/src/inc/SanityCheck.h
Original file line number Diff line number Diff line change
Expand Up @@ -1560,6 +1560,10 @@ static_assert(X_MAX_LENGTH >= X_BED_SIZE, "Movement bounds (X_MIN_POS, X_MAX_POS
*/
#if BOTH(PIDTEMP, MPCTEMP)
#error "Only enable PIDTEMP or MPCTEMP, but not both."
#undef MPCTEMP
#undef MPC_AUTOTUNE
#undef MPC_EDIT_MENU
#undef MPC_AUTOTUNE_MENU
#endif

#if ENABLED(MPC_INCLUDE_FAN)
Expand Down
117 changes: 69 additions & 48 deletions Marlin/src/lcd/e3v2/proui/dwin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -266,7 +266,7 @@ MenuClass *MaxAccelMenu = nullptr;
MenuClass *MaxJerkMenu = nullptr;
#endif
MenuClass *StepsMenu = nullptr;
#if ENABLED(MPCTEMP)
#if EITHER(MPC_EDIT_MENU, MPC_AUTOTUNE_MENU)
MenuClass *HotendMPCMenu = nullptr;
#endif
#if ENABLED(PIDTEMP)
Expand Down Expand Up @@ -1480,46 +1480,59 @@ void DWIN_LevelingDone() {
DWINUI::ClearMainArea();
Draw_Popup_Bkgd();

#if ENABLED(MPCTEMP)

switch (HMI_value.pidresult) {
switch (HMI_value.pidresult) {
default: return;
#if ENABLED(MPC_AUTOTUNE)
case MPCTEMP_START:
_maxtemp = thermalManager.hotend_maxtemp[0];
_target = 200;
DWINUI::Draw_CenteredString(HMI_data.PopupTxt_Color, 100, GET_TEXT_F(MSG_MPC_AUTOTUNE));
DWINUI::Draw_String(HMI_data.PopupTxt_Color, gfrm.x, gfrm.y - DWINUI::fontHeight() - 4, F("MPC target: Celsius"));
DWINUI::Draw_CenteredString(HMI_data.PopupTxt_Color, 120, F("for Nozzle is running."));
break;
case PIDTEMPBED_START:
_maxtemp = BED_MAXTEMP;
_target = HMI_data.BedPidT;
#endif
#if EITHER(PIDTEMP, PIDTEMPBED)
TERN_(PIDTEMP, case PIDTEMP_START:)
TERN_(PIDTEMPBED, case PIDTEMPBED_START:)
DWINUI::Draw_CenteredString(HMI_data.PopupTxt_Color, 100, GET_TEXT_F(MSG_PID_AUTOTUNE));
DWINUI::Draw_String(HMI_data.PopupTxt_Color, gfrm.x, gfrm.y - DWINUI::fontHeight() - 4, F("PID target: Celsius"));
DWINUI::Draw_CenteredString(HMI_data.PopupTxt_Color, 120, F("for BED is running."));
break;
default: return;
}

#else // PID
#endif
}

DWINUI::Draw_CenteredString(HMI_data.PopupTxt_Color, 100, GET_TEXT_F(MSG_PID_AUTOTUNE));
DWINUI::Draw_String(HMI_data.PopupTxt_Color, gfrm.x, gfrm.y - DWINUI::fontHeight() - 4, F("PID target: Celsius"));
switch (HMI_value.pidresult) {
default: break;
#if EITHER(PIDTEMP, MPC_AUTOTUNE)
TERN_(PIDTEMP, case PIDTEMP_START:)
TERN_(MPC_AUTOTUNE, case MPCTEMP_START:)
DWINUI::Draw_CenteredString(HMI_data.PopupTxt_Color, 120, F("for Nozzle is running."));
break;
#endif
#if ENABLED(PIDTEMPBED)
case PIDTEMPBED_START:
DWINUI::Draw_CenteredString(HMI_data.PopupTxt_Color, 120, F("for BED is running."));
break;
#endif
}

switch (HMI_value.pidresult) {
switch (HMI_value.pidresult) {
default: break;
#if ENABLED(MPC_AUTOTUNE)
case MPCTEMP_START:
_maxtemp = thermalManager.hotend_maxtemp[0];
_target = 200;
break;
#endif
#if ENABLED(PIDTEMP)
case PIDTEMP_START:
_maxtemp = thermalManager.hotend_maxtemp[0];
_target = HMI_data.HotendPidT;
DWINUI::Draw_CenteredString(HMI_data.PopupTxt_Color, 120, F("for Nozzle is running."));
break;
#endif
#if ENABLED(PIDTEMPBED)
case PIDTEMPBED_START:
_maxtemp = BED_MAXTEMP;
_target = HMI_data.BedPidT;
DWINUI::Draw_CenteredString(HMI_data.PopupTxt_Color, 120, F("for BED is running."));
break;
default: return;
}

#endif // PID
#endif
}

plot.Draw(gfrm, _maxtemp, _target);
DWINUI::Draw_Int(HMI_data.PopupTxt_Color, 3, gfrm.x + 90, gfrm.y - DWINUI::fontHeight() - 4, _target);
Expand Down Expand Up @@ -1587,7 +1600,7 @@ void DWIN_LevelingDone() {

#endif // DWIN_PID_TUNE

#if ENABLED(MPCTEMP)
#if ENABLED(MPC_AUTOTUNE)

void DWIN_MPCTuning(tempcontrol_t result) {
HMI_value.pidresult = result;
Expand All @@ -1607,7 +1620,7 @@ void DWIN_LevelingDone() {
break;
case MPC_INTERRUPTED:
checkkey = last_checkkey;
DWIN_Popup_Confirm(ICON_TempTooHigh, GET_TEXT_F(MSG_ERROR), F(STR_MPC_AUTOTUNE STR_MPC_AUTOTUNE_INTERRUPTED));
DWIN_Popup_Confirm(ICON_TempTooHigh, GET_TEXT_F(MSG_ERROR), F(STR_MPC_AUTOTUNE_INTERRUPTED));
ui.reset_alert_level();
break;
case MPC_DONE:
Expand All @@ -1622,7 +1635,7 @@ void DWIN_LevelingDone() {
}
}

#endif // MPCTEMP
#endif // MPC_AUTOTUNE

// Started a Print Job
void DWIN_Print_Started() {
Expand Down Expand Up @@ -3090,8 +3103,8 @@ void Draw_AdvancedSettings_Menu() {
#if ENABLED(PIDTEMP)
MENU_ITEM_F(ICON_PIDNozzle, STR_HOTEND_PID " Settings", onDrawSubMenu, Draw_HotendPID_Menu);
#endif
#if ENABLED(MPCTEMP)
MENU_ITEM_F(ICON_MPCNozzle, STR_MPC_AUTOTUNE " Settings", onDrawSubMenu, Draw_HotendMPC_Menu);
#if EITHER(MPC_EDIT_MENU, MPC_AUTOTUNE_MENU)
MENU_ITEM_F(ICON_MPCNozzle, "MPC Settings", onDrawSubMenu, Draw_HotendMPC_Menu);
#endif
#if ENABLED(PIDTEMPBED)
MENU_ITEM_F(ICON_PIDBed, STR_BED_PID " Settings", onDrawSubMenu, Draw_BedPID_Menu);
Expand Down Expand Up @@ -3524,37 +3537,45 @@ void Draw_Steps_Menu() {

#endif

#if ENABLED(MPCTEMP)
#if EITHER(MPC_EDIT_MENU, MPC_AUTOTUNE_MENU)

void HotendMPC() { thermalManager.MPC_autotune(active_extruder); }
void SetHeaterPower() { SetPFloatOnClick(1, 200, 1); }
void SetBlkHeatCapacity() { SetPFloatOnClick(0, 40, 2); }
void SetSensorRespons() { SetPFloatOnClick(0, 1, 4); }
void SetAmbientXfer() { SetPFloatOnClick(0, 1, 4); }
#if ENABLED(MPC_INCLUDE_FAN)
void onDrawFanAdj(MenuItemClass* menuitem, int8_t line) { onDrawFloatMenu(menuitem, line, 4, thermalManager.temp_hotend[0].fanCoefficient()); }
void ApplyFanAdj() { thermalManager.temp_hotend[0].applyFanAdjustment(MenuData.Value / POW(10, 4)); }
void SetFanAdj() { SetFloatOnClick(0, 1, 4, thermalManager.temp_hotend[0].fanCoefficient(), ApplyFanAdj); }
#if ENABLED(MPC_AUTOTUNE_MENU)
void HotendMPC() { thermalManager.MPC_autotune(active_extruder); }
#endif
#if ENABLED(MPC_EDIT_MENU)
void SetHeaterPower() { SetPFloatOnClick(1, 200, 1); }
void SetBlkHeatCapacity() { SetPFloatOnClick(0, 40, 2); }
void SetSensorResponse() { SetPFloatOnClick(0, 1, 4); }
void SetAmbientXfer() { SetPFloatOnClick(0, 1, 4); }
#if ENABLED(MPC_INCLUDE_FAN)
void onDrawFanAdj(MenuItemClass* menuitem, int8_t line) { onDrawFloatMenu(menuitem, line, 4, thermalManager.temp_hotend[0].fanCoefficient()); }
void ApplyFanAdj() { thermalManager.temp_hotend[0].applyFanAdjustment(MenuData.Value / POW(10, 4)); }
void SetFanAdj() { SetFloatOnClick(0, 1, 4, thermalManager.temp_hotend[0].fanCoefficient(), ApplyFanAdj); }
#endif
#endif

void Draw_HotendMPC_Menu() {
checkkey = Menu;
if (SET_MENU_F(HotendMPCMenu, STR_MPC_AUTOTUNE " Settings", 7)) {
if (SET_MENU_F(HotendMPCMenu, "MPC Settings", 7)) {
MPC_t &mpc = thermalManager.temp_hotend[0].mpc;
BACK_ITEM(Draw_AdvancedSettings_Menu);
MENU_ITEM(ICON_MPCNozzle, MSG_MPC_AUTOTUNE, onDrawMenuItem, HotendMPC);
EDIT_ITEM(ICON_MPCHeater, MSG_MPC_POWER, onDrawPFloatMenu, SetHeaterPower, &mpc.heater_power);
EDIT_ITEM(ICON_MPCHeatCap, MSG_MPC_BLOCK_HEAT_CAPACITY, onDrawPFloat2Menu, SetBlkHeatCapacity, &mpc.block_heat_capacity);
EDIT_ITEM(ICON_MPCValue, MSG_SENSOR_RESPONSIVENESS, onDrawPFloat4Menu, SetSensorRespons, &mpc.sensor_responsiveness);
EDIT_ITEM(ICON_MPCValue, MSG_MPC_AMBIENT_XFER_COEFF, onDrawPFloat4Menu, SetAmbientXfer, &mpc.ambient_xfer_coeff_fan0);
#if ENABLED(MPC_INCLUDE_FAN)
EDIT_ITEM(ICON_MPCFan, MSG_MPC_AMBIENT_XFER_COEFF_FAN, onDrawFanAdj, SetFanAdj, &mpc.fan255_adjustment);
#if ENABLED(MPC_AUTOTUNE_MENU)
MENU_ITEM(ICON_MPCNozzle, MSG_MPC_AUTOTUNE, onDrawMenuItem, HotendMPC);
#endif
#if ENABLED(MPC_EDIT_MENU)
EDIT_ITEM(ICON_MPCHeater, MSG_MPC_POWER, onDrawPFloatMenu, SetHeaterPower, &mpc.heater_power);
EDIT_ITEM(ICON_MPCHeatCap, MSG_MPC_BLOCK_HEAT_CAPACITY, onDrawPFloat2Menu, SetBlkHeatCapacity, &mpc.block_heat_capacity);
EDIT_ITEM(ICON_MPCValue, MSG_SENSOR_RESPONSIVENESS, onDrawPFloat4Menu, SetSensorResponse, &mpc.sensor_responsiveness);
EDIT_ITEM(ICON_MPCValue, MSG_MPC_AMBIENT_XFER_COEFF, onDrawPFloat4Menu, SetAmbientXfer, &mpc.ambient_xfer_coeff_fan0);
#if ENABLED(MPC_INCLUDE_FAN)
EDIT_ITEM(ICON_MPCFan, MSG_MPC_AMBIENT_XFER_COEFF_FAN, onDrawFanAdj, SetFanAdj, &mpc.fan255_adjustment);
#endif
#endif
}
UpdateMenu(HotendMPCMenu);
}

#endif // MPCTEMP
#endif // MPC_EDIT_MENU || MPC_AUTOTUNE_MENU

#if ENABLED(PIDTEMPBED)

Expand Down
10 changes: 6 additions & 4 deletions Marlin/src/lcd/e3v2/proui/dwin.h
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ enum processID : uint8_t {
NothingToDo
};

#if EITHER(DWIN_PID_TUNE, MPCTEMP)
#if EITHER(DWIN_PID_TUNE, MPC_AUTOTUNE)

enum tempcontrol_t : uint8_t {
#if DWIN_PID_TUNE
Expand All @@ -85,7 +85,7 @@ enum processID : uint8_t {
PID_TEMP_TOO_HIGH,
PID_TUNING_TIMEOUT,
#endif
#if ENABLED(MPCTEMP)
#if ENABLED(MPC_AUTOTUNE)
MPC_DONE,
MPCTEMP_START,
MPC_TEMP_ERROR,
Expand Down Expand Up @@ -384,7 +384,9 @@ void Draw_Steps_Menu();
#endif

// MPC
#if ENABLED(MPCTEMP)
void DWIN_MPCTuning(tempcontrol_t result);
#if EITHER(MPC_EDIT_MENU, MPC_AUTOTUNE_MENU)
void Draw_HotendMPC_Menu();
#endif
#if ENABLED(MPC_AUTOTUNE)
void DWIN_MPCTuning(tempcontrol_t result);
#endif
10 changes: 3 additions & 7 deletions Marlin/src/module/temperature.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -580,7 +580,6 @@ PGMSTR(str_t_heating_failed, STR_T_HEATING_FAILED);

volatile bool Temperature::raw_temps_ready = false;


#if ENABLED(MPCTEMP)
int32_t Temperature::mpc_e_position; // = 0
#endif
Expand Down Expand Up @@ -925,7 +924,7 @@ volatile bool Temperature::raw_temps_ready = false;

#endif // HAS_PID_HEATING

#if ENABLED(MPCTEMP)
#if ENABLED(MPC_AUTOTUNE)

#if EITHER(MPC_FAN_0_ALL_HOTENDS, MPC_FAN_0_ACTIVE_HOTEND)
#define SINGLEFAN 1
Expand All @@ -951,7 +950,6 @@ volatile bool Temperature::raw_temps_ready = false;
TERN(DWIN_CREALITY_LCD, DWIN_Update(), ui.update());

if (!wait_for_heatup) {
SERIAL_ECHOPGM(STR_MPC_AUTOTUNE);
SERIAL_ECHOLNPGM(STR_MPC_AUTOTUNE_INTERRUPTED);
TERN_(DWIN_LCD_PROUI, DWIN_MPCTuning(MPC_INTERRUPTED));
return true;
Expand Down Expand Up @@ -981,7 +979,6 @@ volatile bool Temperature::raw_temps_ready = false;
}
} on_exit(e);

SERIAL_ECHOPGM(STR_MPC_AUTOTUNE);
SERIAL_ECHOLNPGM(STR_MPC_AUTOTUNE_START, e);
MPCHeaterInfo &hotend = temp_hotend[e];
MPC_t &mpc = hotend.mpc;
Expand Down Expand Up @@ -1150,7 +1147,6 @@ volatile bool Temperature::raw_temps_ready = false;
mpc.block_heat_capacity = mpc.ambient_xfer_coeff_fan0 / block_responsiveness;
mpc.sensor_responsiveness = block_responsiveness / (1.0f - (ambient_temp - asymp_temp) * exp(-block_responsiveness * t1_time) / (t1 - asymp_temp));

SERIAL_ECHOPGM(STR_MPC_AUTOTUNE);
SERIAL_ECHOLNPGM(STR_MPC_AUTOTUNE_FINISHED);
TERN_(DWIN_LCD_PROUI, DWIN_MPCTuning(MPC_DONE));

Expand All @@ -1171,7 +1167,7 @@ volatile bool Temperature::raw_temps_ready = false;
TERN_(HAS_FAN, SERIAL_ECHOLNPAIR_F("MPC_AMBIENT_XFER_COEFF_FAN255 ", ambient_xfer_coeff_fan255, 4));
}

#endif // MPCTEMP
#endif // MPC_AUTOTUNE

int16_t Temperature::getHeaterPower(const heater_id_t heater_id) {
switch (heater_id) {
Expand Down Expand Up @@ -1509,7 +1505,7 @@ void Temperature::mintemp_error(const heater_id_t heater_id) {
const int32_t e_position = stepper.position(E_AXIS);
const float e_speed = (e_position - mpc_e_position) * planner.mm_per_step[E_AXIS] / MPC_dT;

// The position can appear to make big jumps when, e.g. homing
// The position can appear to make big jumps when, e.g., homing
if (fabs(e_speed) > planner.settings.max_feedrate_mm_s[E_AXIS])
mpc_e_position = e_position;
else if (e_speed > 0.0f) { // Ignore retract/recover moves
Expand Down
2 changes: 1 addition & 1 deletion Marlin/src/module/temperature.h
Original file line number Diff line number Diff line change
Expand Up @@ -1194,7 +1194,7 @@ class Temperature {

#endif

#if ENABLED(MPCTEMP)
#if ENABLED(MPC_AUTOTUNE)
void MPC_autotune(const uint8_t e);
#endif

Expand Down
2 changes: 1 addition & 1 deletion buildroot/tests/BIGTREE_GTR_V1_0
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ opt_set MOTHERBOARD BOARD_BTT_GTR_V1_0 SERIAL_PORT -1 \
MPC_AMBIENT_XFER_COEFF '{ 0.068f, 0.068f, 0.068f }' \
MPC_AMBIENT_XFER_COEFF_FAN255 '{ 0.097f, 0.097f, 0.097f }' \
FILAMENT_HEAT_CAPACITY_PERMM '{ 5.6e-3f, 3.6e-3f, 5.6e-3f }'
opt_enable SWITCHING_TOOLHEAD TOOL_SENSOR MPCTEMP MPC_EDIT_MENU MPC_AUTOTUNE_MENU
opt_enable SWITCHING_TOOLHEAD TOOL_SENSOR MPCTEMP MPC_EDIT_MENU MPC_AUTOTUNE MPC_AUTOTUNE_MENU
opt_disable PIDTEMP
exec_test $1 $2 "BigTreeTech GTR | MPC | Switching Toolhead | Tool Sensors" "$3"

Expand Down
Loading

0 comments on commit b5836b2

Please sign in to comment.