Skip to content

Commit f9bc276

Browse files
MarkMan0thinkyhead
authored andcommitted
🐛 Fix, Refactor PID scaling (MarlinFirmware#25096)
1 parent 9609c2d commit f9bc276

File tree

4 files changed

+203
-102
lines changed

4 files changed

+203
-102
lines changed

Marlin/src/module/settings.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -1115,7 +1115,7 @@ void MarlinSettings::postprocess() {
11151115
{
11161116
_FIELD_TEST(bedPID);
11171117
#if ENABLED(PIDTEMPBED)
1118-
const PID_t &pid = thermalManager.temp_bed.pid;
1118+
const auto &pid = thermalManager.temp_bed.pid;
11191119
const raw_pid_t bed_pid = { pid.p(), pid.i(), pid.d() };
11201120
#else
11211121
const raw_pid_t bed_pid = { NAN, NAN, NAN };
@@ -1129,7 +1129,7 @@ void MarlinSettings::postprocess() {
11291129
{
11301130
_FIELD_TEST(chamberPID);
11311131
#if ENABLED(PIDTEMPCHAMBER)
1132-
const PID_t &pid = thermalManager.temp_chamber.pid;
1132+
const auto &pid = thermalManager.temp_chamber.pid;
11331133
const raw_pid_t chamber_pid = { pid.p(), pid.i(), pid.d() };
11341134
#else
11351135
const raw_pid_t chamber_pid = { NAN, NAN, NAN };

Marlin/src/module/temperature.cpp

+18-41
Original file line numberDiff line numberDiff line change
@@ -534,10 +534,6 @@ PGMSTR(str_t_heating_failed, STR_T_HEATING_FAILED);
534534

535535
volatile bool Temperature::raw_temps_ready = false;
536536

537-
#if ENABLED(PID_EXTRUSION_SCALING)
538-
int32_t Temperature::pes_e_position, Temperature::lpq[LPQ_MAX_LEN];
539-
lpq_ptr_t Temperature::lpq_ptr = 0;
540-
#endif
541537

542538
#if ENABLED(MPCTEMP)
543539
int32_t Temperature::mpc_e_position; // = 0
@@ -1338,50 +1334,33 @@ void Temperature::mintemp_error(const heater_id_t heater_id) {
13381334

13391335
#if HAS_PID_HEATING
13401336

1341-
template<typename TT, int MIN_POW, int MAX_POW>
1337+
template<typename TT>
13421338
class PIDRunner {
13431339
public:
13441340
TT &tempinfo;
1345-
__typeof__(TT::pid) work_pid{0};
1346-
float temp_iState = 0, temp_dState = 0;
1347-
bool pid_reset = true;
13481341

13491342
PIDRunner(TT &t) : tempinfo(t) { }
13501343

1351-
float get_pid_output() {
1352-
1344+
float get_pid_output(const uint8_t extr=0) {
13531345
#if ENABLED(PID_OPENLOOP)
13541346

13551347
return constrain(tempinfo.target, 0, MAX_POW);
13561348

13571349
#else // !PID_OPENLOOP
13581350

1359-
const float pid_error = tempinfo.target - tempinfo.celsius;
1360-
if (!tempinfo.target || pid_error < -(PID_FUNCTIONAL_RANGE)) {
1361-
pid_reset = true;
1362-
return 0;
1363-
}
1364-
else if (pid_error > PID_FUNCTIONAL_RANGE) {
1365-
pid_reset = true;
1366-
return MAX_POW;
1367-
}
1351+
float out = tempinfo.pid.get_pid_output(tempinfo.target, tempinfo.celsius);
13681352

1369-
if (pid_reset) {
1370-
pid_reset = false;
1371-
temp_iState = 0.0;
1372-
work_pid.Kd = 0.0;
1373-
}
1374-
1375-
const float max_power_over_i_gain = float(MAX_POW) / tempinfo.pid.Ki - float(MIN_POW);
1376-
temp_iState = constrain(temp_iState + pid_error, 0, max_power_over_i_gain);
1377-
1378-
work_pid.Kp = tempinfo.pid.Kp * pid_error;
1379-
work_pid.Ki = tempinfo.pid.Ki * temp_iState;
1380-
work_pid.Kd = work_pid.Kd + PID_K2 * (tempinfo.pid.Kd * (temp_dState - tempinfo.celsius) - work_pid.Kd);
1353+
#if ENABLED(PID_FAN_SCALING)
1354+
out += tempinfo.pid.get_fan_scale_output(thermalManager.fan_speed[extr]);
1355+
#endif
13811356

1382-
temp_dState = tempinfo.celsius;
1357+
#if ENABLED(PID_EXTRUSION_SCALING)
1358+
out += tempinfo.pid.get_extrusion_scale_output(
1359+
extr == active_extruder, stepper.position(E_AXIS), planner.mm_per_step[E_AXIS], thermalManager.lpq_len
1360+
);
1361+
#endif
13831362

1384-
return constrain(work_pid.Kp + work_pid.Ki + work_pid.Kd + float(MIN_POW), 0, MAX_POW);
1363+
return constrain(out, tempinfo.pid.low(), tempinfo.pid.high());
13851364

13861365
#endif // !PID_OPENLOOP
13871366
}
@@ -1395,7 +1374,8 @@ void Temperature::mintemp_error(const heater_id_t heater_id) {
13951374
STR_PID_DEBUG_INPUT, c,
13961375
STR_PID_DEBUG_OUTPUT, pid_out
13971376
#if DISABLED(PID_OPENLOOP)
1398-
, " pTerm ", work_pid.Kp, " iTerm ", work_pid.Ki, " dTerm ", work_pid.Kd
1377+
, " pTerm ", tempinfo.pid.pTerm(), " iTerm ", tempinfo.pid.iTerm(), " dTerm ", tempinfo.pid.dTerm()
1378+
, " cTerm ", tempinfo.pid.cTerm(), " fTerm ", tempinfo.pid.fTerm()
13991379
#endif
14001380
);
14011381
}
@@ -1413,14 +1393,14 @@ void Temperature::mintemp_error(const heater_id_t heater_id) {
14131393

14141394
#if ENABLED(PIDTEMP)
14151395

1416-
typedef PIDRunner<hotend_info_t, 0, PID_MAX> PIDRunnerHotend;
1396+
typedef PIDRunner<hotend_info_t> PIDRunnerHotend;
14171397

14181398
static PIDRunnerHotend hotend_pid[HOTENDS] = {
14191399
#define _HOTENDPID(E) temp_hotend[E],
14201400
REPEAT(HOTENDS, _HOTENDPID)
14211401
};
14221402

1423-
const float pid_output = is_idling ? 0 : hotend_pid[ee].get_pid_output();
1403+
const float pid_output = is_idling ? 0 : hotend_pid[ee].get_pid_output(ee);
14241404

14251405
#if ENABLED(PID_DEBUG)
14261406
if (ee == active_extruder)
@@ -1521,7 +1501,7 @@ void Temperature::mintemp_error(const heater_id_t heater_id) {
15211501
#if ENABLED(PIDTEMPBED)
15221502

15231503
float Temperature::get_pid_output_bed() {
1524-
static PIDRunner<bed_info_t, MIN_BED_POWER, MAX_BED_POWER> bed_pid(temp_bed);
1504+
static PIDRunner<bed_info_t> bed_pid(temp_bed);
15251505
const float pid_output = bed_pid.get_pid_output();
15261506
TERN_(PID_BED_DEBUG, bed_pid.debug(temp_bed.celsius, pid_output, F("(Bed)")));
15271507
return pid_output;
@@ -1532,7 +1512,7 @@ void Temperature::mintemp_error(const heater_id_t heater_id) {
15321512
#if ENABLED(PIDTEMPCHAMBER)
15331513

15341514
float Temperature::get_pid_output_chamber() {
1535-
static PIDRunner<chamber_info_t, MIN_CHAMBER_POWER, MAX_CHAMBER_POWER> chamber_pid(temp_chamber);
1515+
static PIDRunner<chamber_info_t> chamber_pid(temp_chamber);
15361516
const float pid_output = chamber_pid.get_pid_output();
15371517
TERN_(PID_CHAMBER_DEBUG, chamber_pid.debug(temp_chamber.celsius, pid_output, F("(Chamber)")));
15381518
return pid_output;
@@ -2471,9 +2451,6 @@ void Temperature::init() {
24712451

24722452
TERN_(PROBING_HEATERS_OFF, paused_for_probing = false);
24732453

2474-
#if BOTH(PIDTEMP, PID_EXTRUSION_SCALING)
2475-
pes_e_position = 0;
2476-
#endif
24772454

24782455
// Init (and disable) SPI thermocouples
24792456
#if TEMP_SENSOR_IS_ANY_MAX_TC(0) && PIN_EXISTS(TEMP_0_CS)

0 commit comments

Comments
 (0)