🐛 Fix / refactor shared PID (#24673)

Co-authored-by: Scott Lahteine <thinkyhead@users.noreply.github.com>
This commit is contained in:
Giuliano Zaro
2022-09-04 02:51:53 +02:00
committed by Scott Lahteine
parent e04e18a590
commit aa28358267
15 changed files with 323 additions and 266 deletions

View File

@ -364,18 +364,18 @@ typedef struct SettingsDataStruct {
//
// PIDTEMP
//
PIDCF_t hotendPID[HOTENDS]; // M301 En PIDCF / M303 En U
raw_pidcf_t hotendPID[HOTENDS]; // M301 En PIDCF / M303 En U
int16_t lpq_len; // M301 L
//
// PIDTEMPBED
//
PID_t bedPID; // M304 PID / M303 E-1 U
raw_pid_t bedPID; // M304 PID / M303 E-1 U
//
// PIDTEMPCHAMBER
//
PID_t chamberPID; // M309 PID / M303 E-2 U
raw_pid_t chamberPID; // M309 PID / M303 E-2 U
//
// User-defined Thermistors
@ -1052,27 +1052,20 @@ void MarlinSettings::postprocess() {
//
{
_FIELD_TEST(hotendPID);
#if DISABLED(PIDTEMP)
raw_pidcf_t pidcf = { NAN, NAN, NAN, NAN, NAN };
#endif
HOTEND_LOOP() {
PIDCF_t pidcf = {
#if DISABLED(PIDTEMP)
NAN, NAN, NAN,
NAN, NAN
#else
PID_PARAM(Kp, e),
unscalePID_i(PID_PARAM(Ki, e)),
unscalePID_d(PID_PARAM(Kd, e)),
PID_PARAM(Kc, e),
PID_PARAM(Kf, e)
#endif
};
#if ENABLED(PIDTEMP)
const hotend_pid_t &pid = thermalManager.temp_hotend[e].pid;
raw_pidcf_t pidcf = { pid.p(), pid.i(), pid.d(), pid.c(), pid.f() };
#endif
EEPROM_WRITE(pidcf);
}
_FIELD_TEST(lpq_len);
#if DISABLED(PID_EXTRUSION_SCALING)
const int16_t lpq_len = 20;
#endif
EEPROM_WRITE(TERN(PID_EXTRUSION_SCALING, thermalManager.lpq_len, lpq_len));
const int16_t lpq_len = TERN(PID_EXTRUSION_SCALING, thermalManager.lpq_len, 20);
EEPROM_WRITE(lpq_len);
}
//
@ -1080,17 +1073,12 @@ void MarlinSettings::postprocess() {
//
{
_FIELD_TEST(bedPID);
const PID_t bed_pid = {
#if DISABLED(PIDTEMPBED)
NAN, NAN, NAN
#else
// Store the unscaled PID values
thermalManager.temp_bed.pid.Kp,
unscalePID_i(thermalManager.temp_bed.pid.Ki),
unscalePID_d(thermalManager.temp_bed.pid.Kd)
#endif
};
#if ENABLED(PIDTEMPBED)
const PID_t &pid = thermalManager.temp_bed.pid;
const raw_pid_t bed_pid = { pid.p(), pid.i(), pid.d() };
#else
const raw_pid_t bed_pid = { NAN, NAN, NAN };
#endif
EEPROM_WRITE(bed_pid);
}
@ -1099,17 +1087,12 @@ void MarlinSettings::postprocess() {
//
{
_FIELD_TEST(chamberPID);
const PID_t chamber_pid = {
#if DISABLED(PIDTEMPCHAMBER)
NAN, NAN, NAN
#else
// Store the unscaled PID values
thermalManager.temp_chamber.pid.Kp,
unscalePID_i(thermalManager.temp_chamber.pid.Ki),
unscalePID_d(thermalManager.temp_chamber.pid.Kd)
#endif
};
#if ENABLED(PIDTEMPCHAMBER)
const PID_t &pid = thermalManager.temp_chamber.pid;
const raw_pid_t chamber_pid = { pid.p(), pid.i(), pid.d() };
#else
const raw_pid_t chamber_pid = { NAN, NAN, NAN };
#endif
EEPROM_WRITE(chamber_pid);
}
@ -1117,10 +1100,8 @@ void MarlinSettings::postprocess() {
// User-defined Thermistors
//
#if HAS_USER_THERMISTORS
{
_FIELD_TEST(user_thermistor);
EEPROM_WRITE(thermalManager.user_thermistor);
}
#endif
//
@ -2003,17 +1984,11 @@ void MarlinSettings::postprocess() {
//
{
HOTEND_LOOP() {
PIDCF_t pidcf;
raw_pidcf_t pidcf;
EEPROM_READ(pidcf);
#if ENABLED(PIDTEMP)
if (!validating && !isnan(pidcf.Kp)) {
// Scale PID values since EEPROM values are unscaled
PID_PARAM(Kp, e) = pidcf.Kp;
PID_PARAM(Ki, e) = scalePID_i(pidcf.Ki);
PID_PARAM(Kd, e) = scalePID_d(pidcf.Kd);
TERN_(PID_EXTRUSION_SCALING, PID_PARAM(Kc, e) = pidcf.Kc);
TERN_(PID_FAN_SCALING, PID_PARAM(Kf, e) = pidcf.Kf);
}
if (!validating && !isnan(pidcf.p))
thermalManager.temp_hotend[e].pid.set(pidcf);
#endif
}
}
@ -2035,15 +2010,11 @@ void MarlinSettings::postprocess() {
// Heated Bed PID
//
{
PID_t pid;
raw_pid_t pid;
EEPROM_READ(pid);
#if ENABLED(PIDTEMPBED)
if (!validating && !isnan(pid.Kp)) {
// Scale PID values since EEPROM values are unscaled
thermalManager.temp_bed.pid.Kp = pid.Kp;
thermalManager.temp_bed.pid.Ki = scalePID_i(pid.Ki);
thermalManager.temp_bed.pid.Kd = scalePID_d(pid.Kd);
}
if (!validating && !isnan(pid.p))
thermalManager.temp_bed.pid.set(pid);
#endif
}
@ -2051,15 +2022,11 @@ void MarlinSettings::postprocess() {
// Heated Chamber PID
//
{
PID_t pid;
raw_pid_t pid;
EEPROM_READ(pid);
#if ENABLED(PIDTEMPCHAMBER)
if (!validating && !isnan(pid.Kp)) {
// Scale PID values since EEPROM values are unscaled
thermalManager.temp_chamber.pid.Kp = pid.Kp;
thermalManager.temp_chamber.pid.Ki = scalePID_i(pid.Ki);
thermalManager.temp_chamber.pid.Kd = scalePID_d(pid.Kd);
}
if (!validating && !isnan(pid.p))
thermalManager.temp_chamber.pid.set(pid);
#endif
}
@ -3142,11 +3109,13 @@ void MarlinSettings::reset() {
#define PID_DEFAULT(N,E) DEFAULT_##N
#endif
HOTEND_LOOP() {
PID_PARAM(Kp, e) = float(PID_DEFAULT(Kp, ALIM(e, defKp)));
PID_PARAM(Ki, e) = scalePID_i(PID_DEFAULT(Ki, ALIM(e, defKi)));
PID_PARAM(Kd, e) = scalePID_d(PID_DEFAULT(Kd, ALIM(e, defKd)));
TERN_(PID_EXTRUSION_SCALING, PID_PARAM(Kc, e) = float(PID_DEFAULT(Kc, ALIM(e, defKc))));
TERN_(PID_FAN_SCALING, PID_PARAM(Kf, e) = float(PID_DEFAULT(Kf, ALIM(e, defKf))));
thermalManager.temp_hotend[e].pid.set(
PID_DEFAULT(Kp, ALIM(e, defKp)),
PID_DEFAULT(Ki, ALIM(e, defKi)),
PID_DEFAULT(Kd, ALIM(e, defKd))
OPTARG(PID_EXTRUSION_SCALING, PID_DEFAULT(Kc, ALIM(e, defKc)))
OPTARG(PID_FAN_SCALING, PID_DEFAULT(Kf, ALIM(e, defKf)))
);
}
#endif
@ -3160,9 +3129,7 @@ void MarlinSettings::reset() {
//
#if ENABLED(PIDTEMPBED)
thermalManager.temp_bed.pid.Kp = DEFAULT_bedKp;
thermalManager.temp_bed.pid.Ki = scalePID_i(DEFAULT_bedKi);
thermalManager.temp_bed.pid.Kd = scalePID_d(DEFAULT_bedKd);
thermalManager.temp_bed.pid.set(DEFAULT_bedKp, DEFAULT_bedKi, DEFAULT_bedKd);
#endif
//
@ -3170,9 +3137,7 @@ void MarlinSettings::reset() {
//
#if ENABLED(PIDTEMPCHAMBER)
thermalManager.temp_chamber.pid.Kp = DEFAULT_chamberKp;
thermalManager.temp_chamber.pid.Ki = scalePID_i(DEFAULT_chamberKi);
thermalManager.temp_chamber.pid.Kd = scalePID_d(DEFAULT_chamberKd);
thermalManager.temp_chamber.pid.set(DEFAULT_chamberKp, DEFAULT_chamberKi, DEFAULT_chamberKd);
#endif
//
@ -3342,14 +3307,15 @@ void MarlinSettings::reset() {
static_assert(COUNT(_filament_heat_capacity_permm) == HOTENDS, "FILAMENT_HEAT_CAPACITY_PERMM must have HOTENDS items.");
HOTEND_LOOP() {
thermalManager.temp_hotend[e].constants.heater_power = _mpc_heater_power[e];
thermalManager.temp_hotend[e].constants.block_heat_capacity = _mpc_block_heat_capacity[e];
thermalManager.temp_hotend[e].constants.sensor_responsiveness = _mpc_sensor_responsiveness[e];
thermalManager.temp_hotend[e].constants.ambient_xfer_coeff_fan0 = _mpc_ambient_xfer_coeff[e];
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];
#if ENABLED(MPC_INCLUDE_FAN)
thermalManager.temp_hotend[e].constants.fan255_adjustment = _mpc_ambient_xfer_coeff_fan255[e] - _mpc_ambient_xfer_coeff[e];
constants.fan255_adjustment = _mpc_ambient_xfer_coeff_fan255[e] - _mpc_ambient_xfer_coeff[e];
#endif
thermalManager.temp_hotend[e].constants.filament_heat_capacity_permm = _filament_heat_capacity_permm[e];
constants.filament_heat_capacity_permm = _filament_heat_capacity_permm[e];
}
#endif