Skip to content

Commit 4e68473

Browse files
StevilKnevilAndy-Big
authored andcommitted
🚸 Improved MPCTEMP autotune (MarlinFirmware#25503)
1 parent 1957e5b commit 4e68473

File tree

5 files changed

+386
-182
lines changed

5 files changed

+386
-182
lines changed

Marlin/Configuration.h

+31-25
Original file line numberDiff line numberDiff line change
@@ -679,14 +679,18 @@
679679

680680
// @section hotend temp
681681

682-
// Enable PIDTEMP for PID control or MPCTEMP for Predictive Model.
683-
// temperature control. Disable both for bang-bang heating.
684-
#define PIDTEMP // See the PID Tuning Guide at https://reprap.org/wiki/PID_Tuning
685-
//#define MPCTEMP // ** EXPERIMENTAL **
682+
/**
683+
* Temperature Control
684+
*
685+
* (NONE) : Bang-bang heating
686+
* PIDTEMP : PID temperature control (~4.1K)
687+
* MPCTEMP : Predictive Model temperature control. (~1.8K without auto-tune)
688+
*/
689+
#define PIDTEMP // See the PID Tuning Guide at https://reprap.org/wiki/PID_Tuning
690+
//#define MPCTEMP // ** EXPERIMENTAL ** See https://marlinfw.org/docs/features/model_predictive_control.html
686691

687-
#define BANG_MAX 255 // Limits current to nozzle while in bang-bang mode; 255=full current
688-
#define PID_MAX BANG_MAX // Limits current to nozzle while PID is active (see PID_FUNCTIONAL_RANGE below); 255=full current
689-
#define PID_K1 0.95 // Smoothing factor within any PID loop
692+
#define PID_MAX 255 // Limit hotend current while PID is active (see PID_FUNCTIONAL_RANGE below); 255=full current
693+
#define PID_K1 0.95 // Smoothing factor within any PID loop
690694

691695
#if ENABLED(PIDTEMP)
692696
//#define PID_DEBUG // Print PID debug data to the serial port. Use 'M303 D' to toggle activation.
@@ -708,6 +712,8 @@
708712
// #define DEFAULT_Ki 1.08
709713
// #define DEFAULT_Kd 114.00
710714
#endif
715+
#else
716+
#define BANG_MAX 255 // Limit hotend current while in bang-bang mode; 255=full current
711717
#endif
712718

713719
/**
@@ -719,11 +725,11 @@
719725
* @section mpctemp
720726
*/
721727
#if ENABLED(MPCTEMP)
722-
//#define MPC_AUTOTUNE // Include a method to do MPC auto-tuning (~5664-5882 bytes of flash)
723-
//#define MPC_EDIT_MENU // Add MPC editing to the "Advanced Settings" menu. (~1300 bytes of flash)
728+
//#define MPC_AUTOTUNE // Include a method to do MPC auto-tuning (~6.3K bytes of flash)
729+
//#define MPC_EDIT_MENU // Add MPC editing to the "Advanced Settings" menu. (~1.3K bytes of flash)
724730
//#define MPC_AUTOTUNE_MENU // Add MPC auto-tuning to the "Advanced Settings" menu. (~350 bytes of flash)
725731

726-
#define MPC_MAX BANG_MAX // (0..255) Current to nozzle while MPC is active.
732+
#define MPC_MAX 255 // (0..255) Current to nozzle while MPC is active.
727733
#define MPC_HEATER_POWER { 40.0f } // (W) Heat cartridge powers.
728734

729735
#define MPC_INCLUDE_FAN // Model the fan speed?
@@ -758,32 +764,30 @@
758764
//====================== PID > Bed Temperature Control ======================
759765
//===========================================================================
760766

767+
// @section bed temp
768+
769+
/**
770+
* Max Bed Power
771+
* Applies to all forms of bed control (PID, bang-bang, and bang-bang with hysteresis).
772+
* When set to any value below 255, enables a form of PWM to the bed that acts like a divider
773+
* so don't use it unless you are OK with PWM on your bed. (See the comment on enabling PIDTEMPBED)
774+
*/
775+
#define MAX_BED_POWER 235 // limits duty cycle to bed; 255=full current
776+
761777
/**
762778
* PID Bed Heating
763779
*
764-
* If this option is enabled set PID constants below.
765-
* If this option is disabled, bang-bang will be used and BED_LIMIT_SWITCHING will enable hysteresis.
766-
*
767780
* The PID frequency will be the same as the extruder PWM.
768781
* If PID_dT is the default, and correct for the hardware/configuration, that means 7.689Hz,
769782
* which is fine for driving a square wave into a resistive load and does not significantly
770783
* impact FET heating. This also works fine on a Fotek SSR-10DA Solid State Relay into a 250W
771784
* heater. If your configuration is significantly different than this and you don't understand
772785
* the issues involved, don't use bed PID until someone else verifies that your hardware works.
773-
* @section bed temp
786+
*
787+
* With this option disabled, bang-bang will be used. BED_LIMIT_SWITCHING enables hysteresis.
774788
*/
775789
#define PIDTEMPBED
776790

777-
//#define BED_LIMIT_SWITCHING
778-
779-
/**
780-
* Max Bed Power
781-
* Applies to all forms of bed control (PID, bang-bang, and bang-bang with hysteresis).
782-
* When set to any value below 255, enables a form of PWM to the bed that acts like a divider
783-
* so don't use it unless you are OK with PWM on your bed. (See the comment on enabling PIDTEMPBED)
784-
*/
785-
#define MAX_BED_POWER 235 // limits duty cycle to bed; 255=full current
786-
787791
#if ENABLED(PIDTEMPBED)
788792
//#define MIN_BED_POWER 0
789793
//#define PID_BED_DEBUG // Print Bed PID debug data to the serial port.
@@ -799,7 +803,9 @@
799803
// #define DEFAULT_bedKd 305.4
800804

801805
// FIND YOUR OWN: "M303 E-1 C8 S90" to run autotune on the bed at 90 degreesC for 8 cycles.
802-
#endif // PIDTEMPBED
806+
#else
807+
//#define BED_LIMIT_SWITCHING // Keep the bed temperature within BED_HYSTERESIS of the target
808+
#endif
803809

804810
//===========================================================================
805811
//==================== PID > Chamber Temperature Control ====================

Marlin/src/core/millis_t.h

+1
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ typedef uint32_t millis_t;
2828
#define SEC_TO_MS(N) millis_t((N)*1000UL)
2929
#define MIN_TO_MS(N) SEC_TO_MS((N)*60UL)
3030
#define MS_TO_SEC(N) millis_t((N)/1000UL)
31+
#define MS_TO_SEC_PRECISE(N) (float(N)/1000.0f)
3132

3233
#define PENDING(NOW,SOON) ((int32_t)(NOW-(SOON))<0)
3334
#define ELAPSED(NOW,SOON) (!PENDING(NOW,SOON))

Marlin/src/gcode/temp/M306.cpp

+12-2
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,10 @@
4242
* R<kelvin/second/kelvin> Sensor responsiveness (= transfer coefficient / heat capcity).
4343
*
4444
* With MPC_AUTOTUNE:
45-
* T Autotune the specified or active extruder.
45+
* T Autotune the extruder specified with 'E' or the active extruder.
46+
* S0 : Autotuning method AUTO (default)
47+
* S1 : Autotuning method DIFFERENTIAL
48+
* S2 : Autotuning method ASYMPTOTIC
4649
*/
4750

4851
void GcodeSuite::M306() {
@@ -54,8 +57,15 @@ void GcodeSuite::M306() {
5457

5558
#if ENABLED(MPC_AUTOTUNE)
5659
if (parser.seen_test('T')) {
60+
Temperature::MPCTuningType tuning_type;
61+
const uint8_t type = parser.byteval('S', 0);
62+
switch (type) {
63+
case 1: tuning_type = Temperature::MPCTuningType::FORCE_DIFFERENTIAL; break;
64+
case 2: tuning_type = Temperature::MPCTuningType::FORCE_ASYMPTOTIC; break;
65+
default: tuning_type = Temperature::MPCTuningType::AUTO; break;
66+
}
5767
LCD_MESSAGE(MSG_MPC_AUTOTUNE);
58-
thermalManager.MPC_autotune(e);
68+
thermalManager.MPC_autotune(e, tuning_type);
5969
ui.reset_status();
6070
return;
6171
}

0 commit comments

Comments
 (0)