Skip to content

Commit

Permalink
PID adjustments
Browse files Browse the repository at this point in the history
  • Loading branch information
thinkyhead committed Jan 15, 2023
1 parent a1736f4 commit 9de4db7
Show file tree
Hide file tree
Showing 10 changed files with 123 additions and 112 deletions.
17 changes: 3 additions & 14 deletions Marlin/src/gcode/temp/M303.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ void GcodeSuite::M303() {
SERIAL_ECHOPGM(STR_PID_AUTOTUNE);
SERIAL_ECHOLNPGM(STR_PID_BAD_HEATER_ID);
TERN_(EXTENSIBLE_UI, ExtUI::onPidTuning(ExtUI::result_t::PID_BAD_EXTRUDER_NUM));
TERN_(DWIN_LCD_PROUI, DWIN_PidTuning(PID_BAD_EXTRUDER_NUM));
TERN_(DWIN_PID_TUNE, DWIN_PidTuning(PID_BAD_EXTRUDER_NUM));
return;
}

Expand All @@ -78,20 +78,9 @@ void GcodeSuite::M303() {
const celsius_t temp = seenS ? parser.value_celsius() : default_temp;
const bool u = parser.boolval('U');

#if ENABLED(DWIN_LCD_PROUI) && EITHER(PIDTEMP, PIDTEMPBED)
if (seenC) HMI_data.PidCycles = c;
if (seenS) {
switch (hid) {
OPTCODE(PIDTEMP, case 0 ... HOTENDS - 1: HMI_data.HotendPidT = temp; break)
OPTCODE(PIDTEMPBED, case H_BED: HMI_data.BedPidT = temp; break)
default: break;
}
}
#endif
TERN_(DWIN_PID_TUNE, DWIN_StartM303(seenC, c, seenS, hid, temp));

#if DISABLED(BUSY_WHILE_HEATING)
KEEPALIVE_STATE(NOT_BUSY);
#endif
IF_DISABLED(BUSY_WHILE_HEATING, KEEPALIVE_STATE(NOT_BUSY));

LCD_MESSAGE(MSG_PID_AUTOTUNE);
thermalManager.PID_autotune(temp, hid, c, u);
Expand Down
28 changes: 14 additions & 14 deletions Marlin/src/gcode/temp/M306.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -52,15 +52,15 @@ void GcodeSuite::M306() {

if (parser.seen("ACFPRH")) {
const heater_id_t hid = (heater_id_t)parser.intval('E', 0);
MPC_t &constants = thermalManager.temp_hotend[hid].constants;
if (parser.seenval('P')) constants.heater_power = parser.value_float();
if (parser.seenval('C')) constants.block_heat_capacity = parser.value_float();
if (parser.seenval('R')) constants.sensor_responsiveness = parser.value_float();
if (parser.seenval('A')) constants.ambient_xfer_coeff_fan0 = parser.value_float();
MPC_t &mpc = thermalManager.temp_hotend[hid].mpc;
if (parser.seenval('P')) mpc.heater_power = parser.value_float();
if (parser.seenval('C')) mpc.block_heat_capacity = parser.value_float();
if (parser.seenval('R')) mpc.sensor_responsiveness = parser.value_float();
if (parser.seenval('A')) mpc.ambient_xfer_coeff_fan0 = parser.value_float();
#if ENABLED(MPC_INCLUDE_FAN)
if (parser.seenval('F')) constants.fan255_adjustment = parser.value_float() - constants.ambient_xfer_coeff_fan0;
if (parser.seenval('F')) mpc.applyFanAdjustment(parser.value_float());
#endif
if (parser.seenval('H')) constants.filament_heat_capacity_permm = parser.value_float();
if (parser.seenval('H')) mpc.filament_heat_capacity_permm = parser.value_float();
return;
}

Expand All @@ -71,16 +71,16 @@ void GcodeSuite::M306_report(const bool forReplay/*=true*/) {
report_heading(forReplay, F("Model predictive control"));
HOTEND_LOOP() {
report_echo_start(forReplay);
MPC_t& constants = thermalManager.temp_hotend[e].constants;
MPC_t &mpc = thermalManager.temp_hotend[e].mpc;
SERIAL_ECHOPGM(" M306 E", e);
SERIAL_ECHOPAIR_F(" P", constants.heater_power, 2);
SERIAL_ECHOPAIR_F(" C", constants.block_heat_capacity, 2);
SERIAL_ECHOPAIR_F(" R", constants.sensor_responsiveness, 4);
SERIAL_ECHOPAIR_F(" A", constants.ambient_xfer_coeff_fan0, 4);
SERIAL_ECHOPAIR_F(" P", mpc.heater_power, 2);
SERIAL_ECHOPAIR_F(" C", mpc.block_heat_capacity, 2);
SERIAL_ECHOPAIR_F(" R", mpc.sensor_responsiveness, 4);
SERIAL_ECHOPAIR_F(" A", mpc.ambient_xfer_coeff_fan0, 4);
#if ENABLED(MPC_INCLUDE_FAN)
SERIAL_ECHOPAIR_F(" F", constants.ambient_xfer_coeff_fan0 + constants.fan255_adjustment, 4);
SERIAL_ECHOPAIR_F(" F", mpc.fanCoefficient(), 4);
#endif
SERIAL_ECHOPAIR_F(" H", constants.filament_heat_capacity_permm, 4);
SERIAL_ECHOPAIR_F(" H", mpc.filament_heat_capacity_permm, 4);
SERIAL_EOL();
}
}
Expand Down
3 changes: 3 additions & 0 deletions Marlin/src/inc/Conditionals_post.h
Original file line number Diff line number Diff line change
Expand Up @@ -2504,6 +2504,9 @@
// PID heating
#if ANY(PIDTEMP, PIDTEMPBED, PIDTEMPCHAMBER)
#define HAS_PID_HEATING 1
#if ENABLED(DWIN_LCD_PROUI) && EITHER(PIDTEMP, PIDTEMPBED)
#define DWIN_PID_TUNE 1
#endif
#endif

// Thermal protection
Expand Down
42 changes: 27 additions & 15 deletions Marlin/src/lcd/e3v2/proui/dwin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1472,7 +1472,7 @@ void DWIN_LevelingDone() {

// PID/MPC process

#if HAS_PIDPLOT && EITHER(HAS_PID_HEATING, MPCTEMP)
#if HAS_PIDPLOT && EITHER(DWIN_PID_TUNE, MPCTEMP)

celsius_t _maxtemp, _target;
void DWIN_Draw_PID_MPC_Popup() {
Expand Down Expand Up @@ -1527,7 +1527,18 @@ void DWIN_LevelingDone() {

#endif

#if HAS_PID_HEATING
#if DWIN_PID_TUNE

void DWIN_StartM303(const bool seenC, const int c, const bool seenS, const heater_id_t hid, const celsius_t temp) {
if (seenC) HMI_data.PidCycles = c;
if (seenS) {
switch (hid) {
OPTCODE(PIDTEMP, case 0 ... HOTENDS - 1: HMI_data.HotendPidT = temp; break)
OPTCODE(PIDTEMPBED, case H_BED: HMI_data.BedPidT = temp; break)
default: break;
}
}
}

void DWIN_PidTuning(tempcontrol_t result) {
HMI_value.pidresult = result;
Expand Down Expand Up @@ -1570,7 +1581,7 @@ void DWIN_LevelingDone() {
}
}

#endif // HAS_PID_HEATING
#endif // DWIN_PID_TUNE

#if ENABLED(MPCTEMP)

Expand Down Expand Up @@ -1702,7 +1713,7 @@ void DWIN_SetDataDefaults() {
DWINUI::SetColors(HMI_data.Text_Color, HMI_data.Background_Color, HMI_data.StatusBg_Color);
TERN_(PIDTEMP, HMI_data.HotendPidT = DEF_HOTENDPIDT);
TERN_(PIDTEMPBED, HMI_data.BedPidT = DEF_BEDPIDT);
TERN_(HAS_PID_HEATING, HMI_data.PidCycles = DEF_PIDCYCLES);
TERN_(DWIN_PID_TUNE, HMI_data.PidCycles = DEF_PIDCYCLES);
#if ENABLED(PREVENT_COLD_EXTRUSION)
HMI_data.ExtMinT = EXTRUDE_MINTEMP;
ApplyExtMinT();
Expand Down Expand Up @@ -2092,7 +2103,7 @@ void SetMoveZ() { HMI_value.axis = Z_AXIS; SetPFloatOnClick(Z_MIN_POS, Z_MAX_POS
}
#endif

#if EITHER(PIDTEMP, PIDTEMPBED)
#if DWIN_PID_TUNE
void SetPID(celsius_t t, heater_id_t h) {
char cmd[53] = "";
char str_1[5] = "", str_2[5] = "";
Expand Down Expand Up @@ -2558,7 +2569,7 @@ void SetStepsZ() { HMI_value.axis = Z_AXIS, SetPFloatOnClick( MIN_STEP, MAX_STEP
void SetBedPidT() { SetPIntOnClick(MIN_BEDTEMP, MAX_BEDTEMP); }
#endif

#if EITHER(PIDTEMP, PIDTEMPBED)
#if DWIN_PID_TUNE
void SetPidCycles() { SetPIntOnClick(3, 50); }
void SetKp() { SetPFloatOnClick(0, 1000, 2); }
void ApplyPIDi() {
Expand Down Expand Up @@ -2729,7 +2740,7 @@ void onDrawGetColorItem(MenuItemClass* menuitem, int8_t line) {
DWIN_Draw_HLine(HMI_data.SplitLine_Color, 16, MYPOS(line + 1), 240);
}

#if EITHER(PIDTEMP, PIDTEMPBED)
#if DWIN_PID_TUNE
void onDrawPIDi(MenuItemClass* menuitem, int8_t line) { onDrawFloatMenu(menuitem, line, 2, unscalePID_i(*(float*)static_cast<MenuItemPtrClass*>(menuitem)->value)); }
void onDrawPIDd(MenuItemClass* menuitem, int8_t line) { onDrawFloatMenu(menuitem, line, 2, unscalePID_d(*(float*)static_cast<MenuItemPtrClass*>(menuitem)->value)); }
#endif
Expand Down Expand Up @@ -3531,22 +3542,23 @@ void Draw_Steps_Menu() {
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].constants.ambient_xfer_coeff_fan0 + thermalManager.temp_hotend[0].constants.fan255_adjustment); }
void ApplyFanAdj() { thermalManager.temp_hotend[0].constants.fan255_adjustment = MenuData.Value / POW(10, 4) - thermalManager.temp_hotend[0].constants.ambient_xfer_coeff_fan0; }
void SetFanAdj() { SetFloatOnClick(0, 1, 4, thermalManager.temp_hotend[0].constants.ambient_xfer_coeff_fan0 + thermalManager.temp_hotend[0].constants.fan255_adjustment, ApplyFanAdj); }
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

void Draw_HotendMPC_Menu() {
checkkey = Menu;
if (SET_MENU_F(HotendMPCMenu, STR_MPC_AUTOTUNE " 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, &thermalManager.temp_hotend[0].constants.heater_power);
EDIT_ITEM(ICON_MPCHeatCap, MSG_MPC_BLOCK_HEAT_CAPACITY, onDrawPFloat2Menu, SetBlkHeatCapacity, &thermalManager.temp_hotend[0].constants.block_heat_capacity);
EDIT_ITEM(ICON_MPCValue, MSG_SENSOR_RESPONSIVENESS, onDrawPFloat4Menu, SetSensorRespons, &thermalManager.temp_hotend[0].constants.sensor_responsiveness);
EDIT_ITEM(ICON_MPCValue, MSG_MPC_AMBIENT_XFER_COEFF, onDrawPFloat4Menu, SetAmbientXfer, &thermalManager.temp_hotend[0].constants.ambient_xfer_coeff_fan0);
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, &thermalManager.temp_hotend[0].constants.fan255_adjustment);
EDIT_ITEM(ICON_MPCFan, MSG_MPC_AMBIENT_XFER_COEFF_FAN, onDrawFanAdj, SetFanAdj, &mpc.fan255_adjustment);
#endif
}
UpdateMenu(HotendMPCMenu);
Expand Down
30 changes: 17 additions & 13 deletions Marlin/src/lcd/e3v2/proui/dwin.h
Original file line number Diff line number Diff line change
Expand Up @@ -74,22 +74,24 @@ enum processID : uint8_t {
NothingToDo
};

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

enum tempcontrol_t : uint8_t {
#if HAS_PID_HEATING
#if ENABLED(PIDTEMP)
PIDTEMP_START = 0,
PIDTEMPBED_START,
PID_BAD_EXTRUDER_NUM,
PID_TEMP_TOO_HIGH,
PID_TUNING_TIMEOUT,
PID_DONE,
#endif
#if ENABLED(PIDTEMPBED)
PIDTEMPBED_START,
#endif
#if ENABLED(MPCTEMP)
MPCTEMP_START,
MPC_TEMP_ERROR,
MPC_INTERRUPTED,
MPC_DONE,
MPC_DONE
#endif
};

Expand Down Expand Up @@ -120,14 +122,14 @@ typedef struct {
uint16_t Coordinate_Color;

// Temperatures
#if ENABLED(PIDTEMP)
int16_t HotendPidT = DEF_HOTENDPIDT;
#endif
#if ENABLED(PIDTEMPBED)
int16_t BedPidT = DEF_BEDPIDT;
#endif
#if (HAS_HOTEND || HAS_HEATED_BED) && HAS_PID_HEATING
#if DWIN_PID_TUNE
int16_t PidCycles = DEF_PIDCYCLES;
#if ENABLED(PIDTEMP)
int16_t HotendPidT = DEF_HOTENDPIDT;
#endif
#if ENABLED(PIDTEMPBED)
int16_t BedPidT = DEF_BEDPIDT;
#endif
#endif
#if ENABLED(PREVENT_COLD_EXTRUSION)
int16_t ExtMinT = EXTRUDE_MINTEMP;
Expand All @@ -154,7 +156,7 @@ static constexpr size_t eeprom_data_size = sizeof(HMI_data_t);

typedef struct {
int8_t Color[3]; // Color components
#if HAS_PID_HEATING
#if DWIN_PID_TUNE
tempcontrol_t pidresult = PID_DONE;
#endif
uint8_t Select = 0; // Auxiliary selector variable
Expand Down Expand Up @@ -371,7 +373,9 @@ void Draw_Steps_Menu();
#endif

// PID
#if HAS_PID_HEATING
#if DWIN_PID_TUNE
#include "../../../module/temperature.h"
void DWIN_StartM303(const bool seenC, const int c, const bool seenS, const heater_id_t hid, const celsius_t temp);
void DWIN_PidTuning(tempcontrol_t result);
#if ENABLED(PIDTEMP)
void Draw_HotendPID_Menu();
Expand Down
1 change: 0 additions & 1 deletion Marlin/src/lcd/language/language_en.h
Original file line number Diff line number Diff line change
Expand Up @@ -376,7 +376,6 @@ namespace Language_en {
LSTR MSG_MPC_COOLING_TO_AMBIENT = _UxGT("Cooling to ambient");
LSTR MSG_MPC_AUTOTUNE = _UxGT("MPC Autotune");
LSTR MSG_MPC_EDIT = _UxGT("Edit * MPC");
LSTR MSG_MPC_POWER = _UxGT("Heater Power");
LSTR MSG_MPC_POWER_E = _UxGT("Power *");
LSTR MSG_MPC_BLOCK_HEAT_CAPACITY = _UxGT("Heat Capacity");
LSTR MSG_MPC_BLOCK_HEAT_CAPACITY_E = _UxGT("Heat Cap. *");
Expand Down
22 changes: 11 additions & 11 deletions Marlin/src/lcd/menu/menu_advanced.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@
#include "../../module/probe.h"
#endif

#if ANY(PIDTEMP, PIDTEMPBED, PIDTEMPCHAMBER)
#if HAS_PID_HEATING
#include "../../module/temperature.h"
#endif

Expand Down Expand Up @@ -277,10 +277,10 @@ void menu_backlash();
//
#if SHOW_MENU_ADVANCED_TEMPERATURE

#if ENABLED(MPC_EDIT_MENU)
#define MPC_EDIT_DEFS(N) \
MPC_t &c = thermalManager.temp_hotend[N].constants; \
TERN_(MPC_INCLUDE_FAN, editable.decimal = c.ambient_xfer_coeff_fan0 + c.fan255_adjustment)
#if BOTH(MPC_EDIT_MENU, MPC_INCLUDE_FAN)
#define MPC_EDIT_DEFS(N) editable.decimal = thermalManager.temp_hotend[N].fanCoefficient()
#else
#define MPC_EDIT_DEFS(...)
#endif

void menu_advanced_temperature() {
Expand Down Expand Up @@ -370,17 +370,17 @@ void menu_backlash();
#if ENABLED(MPC_EDIT_MENU)

#define _MPC_EDIT_ITEMS(N) \
EDIT_ITEM_FAST_N(float31sign, N, MSG_MPC_POWER_E, &c.heater_power, 1, 200); \
EDIT_ITEM_FAST_N(float31sign, N, MSG_MPC_BLOCK_HEAT_CAPACITY_E, &c.block_heat_capacity, 0, 40); \
EDIT_ITEM_FAST_N(float43, N, MSG_SENSOR_RESPONSIVENESS_E, &c.sensor_responsiveness, 0, 1); \
EDIT_ITEM_FAST_N(float43, N, MSG_MPC_AMBIENT_XFER_COEFF_E, &c.ambient_xfer_coeff_fan0, 0, 1)
EDIT_ITEM_FAST_N(float31sign, N, MSG_MPC_POWER_E, &mpc.heater_power, 1, 200); \
EDIT_ITEM_FAST_N(float31sign, N, MSG_MPC_BLOCK_HEAT_CAPACITY_E, &mpc.block_heat_capacity, 0, 40); \
EDIT_ITEM_FAST_N(float43, N, MSG_SENSOR_RESPONSIVENESS_E, &mpc.sensor_responsiveness, 0, 1); \
EDIT_ITEM_FAST_N(float43, N, MSG_MPC_AMBIENT_XFER_COEFF_E, &mpc.ambient_xfer_coeff_fan0, 0, 1)

#if ENABLED(MPC_INCLUDE_FAN)
#define MPC_EDIT_ITEMS(N) \
MPC_t &mpc = thermalManager.temp_hotend[MenuItemBase::itemIndex].constants; \
_MPC_EDIT_ITEMS(N); \
EDIT_ITEM_FAST_N(float43, N, MSG_MPC_AMBIENT_XFER_COEFF_FAN_E, &editable.decimal, 0, 1, []{ \
MPC_t &c = thermalManager.temp_hotend[MenuItemBase::itemIndex].constants; \
c.fan255_adjustment = editable.decimal - c.ambient_xfer_coeff_fan0; \
thermalManager.temp_hotend[MenuItemBase::itemIndex].applyFanAdjustment(editable.decimal); \
})
#else
#define MPC_EDIT_ITEMS _MPC_EDIT_ITEMS
Expand Down
20 changes: 9 additions & 11 deletions Marlin/src/module/settings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1632,8 +1632,7 @@ void MarlinSettings::postprocess() {
// Model predictive control
//
#if ENABLED(MPCTEMP)
HOTEND_LOOP()
EEPROM_WRITE(thermalManager.temp_hotend[e].constants);
HOTEND_LOOP() EEPROM_WRITE(thermalManager.temp_hotend[e].mpc);
#endif

//
Expand Down Expand Up @@ -2627,8 +2626,7 @@ void MarlinSettings::postprocess() {
//
#if ENABLED(MPCTEMP)
{
HOTEND_LOOP()
EEPROM_READ(thermalManager.temp_hotend[e].constants);
HOTEND_LOOP() EEPROM_READ(thermalManager.temp_hotend[e].mpc);
}
#endif

Expand Down Expand Up @@ -3416,15 +3414,15 @@ void MarlinSettings::reset() {
static_assert(COUNT(_filament_heat_capacity_permm) == HOTENDS, "FILAMENT_HEAT_CAPACITY_PERMM must have HOTENDS items.");

HOTEND_LOOP() {
MPC_t &constants = thermalManager.temp_hotend[e].constants;
constants.heater_power = _mpc_heater_power[e];
constants.block_heat_capacity = _mpc_block_heat_capacity[e];
constants.sensor_responsiveness = _mpc_sensor_responsiveness[e];
constants.ambient_xfer_coeff_fan0 = _mpc_ambient_xfer_coeff[e];
MPC_t &mpc = thermalManager.temp_hotend[e].mpc;
mpc.heater_power = _mpc_heater_power[e];
mpc.block_heat_capacity = _mpc_block_heat_capacity[e];
mpc.sensor_responsiveness = _mpc_sensor_responsiveness[e];
mpc.ambient_xfer_coeff_fan0 = _mpc_ambient_xfer_coeff[e];
#if ENABLED(MPC_INCLUDE_FAN)
constants.fan255_adjustment = _mpc_ambient_xfer_coeff_fan255[e] - _mpc_ambient_xfer_coeff[e];
mpc.fan255_adjustment = _mpc_ambient_xfer_coeff_fan255[e] - _mpc_ambient_xfer_coeff[e];
#endif
constants.filament_heat_capacity_permm = _filament_heat_capacity_permm[e];
mpc.filament_heat_capacity_permm = _filament_heat_capacity_permm[e];
}
#endif

Expand Down
Loading

0 comments on commit 9de4db7

Please sign in to comment.