Browse Source

🐛 Fix / refactor shared PID (#24673)

Co-authored-by: Scott Lahteine <thinkyhead@users.noreply.github.com>
Giuliano Zaro 1 year ago
parent
commit
094701cc71
No account linked to committer's email address

+ 10
- 12
Marlin/src/gcode/config/M301.cpp View File

@@ -57,19 +57,18 @@ void GcodeSuite::M301() {
57 57
 
58 58
   if (e < HOTENDS) { // catch bad input value
59 59
 
60
-    if (parser.seenval('P')) PID_PARAM(Kp, e) = parser.value_float();
61
-    if (parser.seenval('I')) PID_PARAM(Ki, e) = scalePID_i(parser.value_float());
62
-    if (parser.seenval('D')) PID_PARAM(Kd, e) = scalePID_d(parser.value_float());
60
+    if (parser.seenval('P')) SET_HOTEND_PID(Kp, e, parser.value_float());
61
+    if (parser.seenval('I')) SET_HOTEND_PID(Ki, e, parser.value_float());
62
+    if (parser.seenval('D')) SET_HOTEND_PID(Kd, e, parser.value_float());
63 63
 
64 64
     #if ENABLED(PID_EXTRUSION_SCALING)
65
-      if (parser.seenval('C')) PID_PARAM(Kc, e) = parser.value_float();
65
+      if (parser.seenval('C')) SET_HOTEND_PID(Kc, e, parser.value_float());
66 66
       if (parser.seenval('L')) thermalManager.lpq_len = parser.value_int();
67
-      NOMORE(thermalManager.lpq_len, LPQ_MAX_LEN);
68
-      NOLESS(thermalManager.lpq_len, 0);
67
+      LIMIT(thermalManager.lpq_len, 0, LPQ_MAX_LEN);
69 68
     #endif
70 69
 
71 70
     #if ENABLED(PID_FAN_SCALING)
72
-      if (parser.seenval('F')) PID_PARAM(Kf, e) = parser.value_float();
71
+      if (parser.seenval('F')) SET_HOTEND_PID(Kf, e, parser.value_float());
73 72
     #endif
74 73
 
75 74
     thermalManager.updatePID();
@@ -83,6 +82,7 @@ void GcodeSuite::M301_report(const bool forReplay/*=true*/ E_OPTARG(const int8_t
83 82
   IF_DISABLED(HAS_MULTI_EXTRUDER, constexpr int8_t eindex = -1);
84 83
   HOTEND_LOOP() {
85 84
     if (e == eindex || eindex == -1) {
85
+      const hotend_pid_t &pid = thermalManager.temp_hotend[e].pid;
86 86
       report_echo_start(forReplay);
87 87
       SERIAL_ECHOPGM_P(
88 88
         #if ENABLED(PID_PARAMS_PER_HOTEND)
@@ -90,16 +90,14 @@ void GcodeSuite::M301_report(const bool forReplay/*=true*/ E_OPTARG(const int8_t
90 90
         #else
91 91
           PSTR("  M301 P")
92 92
         #endif
93
-        ,                          PID_PARAM(Kp, e)
94
-        , PSTR(" I"), unscalePID_i(PID_PARAM(Ki, e))
95
-        , PSTR(" D"), unscalePID_d(PID_PARAM(Kd, e))
93
+        , pid.p(), PSTR(" I"), pid.i(), PSTR(" D"), pid.d()
96 94
       );
97 95
       #if ENABLED(PID_EXTRUSION_SCALING)
98
-        SERIAL_ECHOPGM_P(SP_C_STR, PID_PARAM(Kc, e));
96
+        SERIAL_ECHOPGM_P(SP_C_STR, pid.c());
99 97
         if (e == 0) SERIAL_ECHOPGM(" L", thermalManager.lpq_len);
100 98
       #endif
101 99
       #if ENABLED(PID_FAN_SCALING)
102
-        SERIAL_ECHOPGM(" F", PID_PARAM(Kf, e));
100
+        SERIAL_ECHOPGM(" F", pid.f());
103 101
       #endif
104 102
       SERIAL_EOL();
105 103
     }

+ 7
- 7
Marlin/src/gcode/config/M304.cpp View File

@@ -36,17 +36,17 @@
36 36
  */
37 37
 void GcodeSuite::M304() {
38 38
   if (!parser.seen("PID")) return M304_report();
39
-  if (parser.seenval('P')) thermalManager.temp_bed.pid.Kp = parser.value_float();
40
-  if (parser.seenval('I')) thermalManager.temp_bed.pid.Ki = scalePID_i(parser.value_float());
41
-  if (parser.seenval('D')) thermalManager.temp_bed.pid.Kd = scalePID_d(parser.value_float());
39
+  if (parser.seenval('P')) thermalManager.temp_bed.pid.set_Kp(parser.value_float());
40
+  if (parser.seenval('I')) thermalManager.temp_bed.pid.set_Ki(parser.value_float());
41
+  if (parser.seenval('D')) thermalManager.temp_bed.pid.set_Kd(parser.value_float());
42 42
 }
43 43
 
44 44
 void GcodeSuite::M304_report(const bool forReplay/*=true*/) {
45 45
   report_heading_etc(forReplay, F(STR_BED_PID));
46
-  SERIAL_ECHOLNPGM(
47
-      "  M304 P", thermalManager.temp_bed.pid.Kp
48
-    , " I", unscalePID_i(thermalManager.temp_bed.pid.Ki)
49
-    , " D", unscalePID_d(thermalManager.temp_bed.pid.Kd)
46
+  SERIAL_ECHOLNPGM("  M304"
47
+      " P", thermalManager.temp_bed.pid.p()
48
+    , " I", thermalManager.temp_bed.pid.i()
49
+    , " D", thermalManager.temp_bed.pid.d()
50 50
   );
51 51
 }
52 52
 

+ 7
- 7
Marlin/src/gcode/config/M309.cpp View File

@@ -36,17 +36,17 @@
36 36
  */
37 37
 void GcodeSuite::M309() {
38 38
   if (!parser.seen("PID")) return M309_report();
39
-  if (parser.seen('P')) thermalManager.temp_chamber.pid.Kp = parser.value_float();
40
-  if (parser.seen('I')) thermalManager.temp_chamber.pid.Ki = scalePID_i(parser.value_float());
41
-  if (parser.seen('D')) thermalManager.temp_chamber.pid.Kd = scalePID_d(parser.value_float());
39
+  if (parser.seenval('P')) thermalManager.temp_chamber.pid.set_Kp(parser.value_float());
40
+  if (parser.seenval('I')) thermalManager.temp_chamber.pid.set_Ki(parser.value_float());
41
+  if (parser.seenval('D')) thermalManager.temp_chamber.pid.set_Kd(parser.value_float());
42 42
 }
43 43
 
44 44
 void GcodeSuite::M309_report(const bool forReplay/*=true*/) {
45 45
   report_heading_etc(forReplay, F(STR_CHAMBER_PID));
46
-  SERIAL_ECHOLNPGM(
47
-      "  M309 P", thermalManager.temp_chamber.pid.Kp
48
-    , " I", unscalePID_i(thermalManager.temp_chamber.pid.Ki)
49
-    , " D", unscalePID_d(thermalManager.temp_chamber.pid.Kd)
46
+  SERIAL_ECHOLNPGM("  M309"
47
+      " P", thermalManager.temp_chamber.pid.p()
48
+    , " I", thermalManager.temp_chamber.pid.i()
49
+    , " D", thermalManager.temp_chamber.pid.d()
50 50
   );
51 51
 }
52 52
 

+ 6
- 6
Marlin/src/lcd/e3v2/jyersui/dwin.cpp View File

@@ -2140,7 +2140,7 @@ void CrealityDWINClass::Menu_Item_Handler(uint8_t menu, uint8_t item, bool draw/
2140 2140
           case HOTENDPID_KP:
2141 2141
             if (draw) {
2142 2142
               Draw_Menu_Item(row, ICON_Version, F("Kp Value"));
2143
-              Draw_Float(thermalManager.temp_hotend[0].pid.Kp, row, false, 100);
2143
+              Draw_Float(thermalManager.temp_hotend[0].pid.p(), row, false, 100);
2144 2144
             }
2145 2145
             else
2146 2146
               Modify_Value(thermalManager.temp_hotend[0].pid.Kp, 0, 5000, 100, thermalManager.updatePID);
@@ -2148,7 +2148,7 @@ void CrealityDWINClass::Menu_Item_Handler(uint8_t menu, uint8_t item, bool draw/
2148 2148
           case HOTENDPID_KI:
2149 2149
             if (draw) {
2150 2150
               Draw_Menu_Item(row, ICON_Version, F("Ki Value"));
2151
-              Draw_Float(unscalePID_i(thermalManager.temp_hotend[0].pid.Ki), row, false, 100);
2151
+              Draw_Float(thermalManager.temp_hotend[0].pid.i(), row, false, 100);
2152 2152
             }
2153 2153
             else
2154 2154
               Modify_Value(thermalManager.temp_hotend[0].pid.Ki, 0, 5000, 100, thermalManager.updatePID);
@@ -2156,7 +2156,7 @@ void CrealityDWINClass::Menu_Item_Handler(uint8_t menu, uint8_t item, bool draw/
2156 2156
           case HOTENDPID_KD:
2157 2157
             if (draw) {
2158 2158
               Draw_Menu_Item(row, ICON_Version, F("Kd Value"));
2159
-              Draw_Float(unscalePID_d(thermalManager.temp_hotend[0].pid.Kd), row, false, 100);
2159
+              Draw_Float(thermalManager.temp_hotend[0].pid.d(), row, false, 100);
2160 2160
             }
2161 2161
             else
2162 2162
               Modify_Value(thermalManager.temp_hotend[0].pid.Kd, 0, 5000, 100, thermalManager.updatePID);
@@ -2207,7 +2207,7 @@ void CrealityDWINClass::Menu_Item_Handler(uint8_t menu, uint8_t item, bool draw/
2207 2207
           case BEDPID_KP:
2208 2208
             if (draw) {
2209 2209
               Draw_Menu_Item(row, ICON_Version, F("Kp Value"));
2210
-              Draw_Float(thermalManager.temp_bed.pid.Kp, row, false, 100);
2210
+              Draw_Float(thermalManager.temp_bed.pid.p(), row, false, 100);
2211 2211
             }
2212 2212
             else {
2213 2213
               Modify_Value(thermalManager.temp_bed.pid.Kp, 0, 5000, 100, thermalManager.updatePID);
@@ -2216,7 +2216,7 @@ void CrealityDWINClass::Menu_Item_Handler(uint8_t menu, uint8_t item, bool draw/
2216 2216
           case BEDPID_KI:
2217 2217
             if (draw) {
2218 2218
               Draw_Menu_Item(row, ICON_Version, F("Ki Value"));
2219
-              Draw_Float(unscalePID_i(thermalManager.temp_bed.pid.Ki), row, false, 100);
2219
+              Draw_Float(thermalManager.temp_bed.pid.i(), row, false, 100);
2220 2220
             }
2221 2221
             else
2222 2222
               Modify_Value(thermalManager.temp_bed.pid.Ki, 0, 5000, 100, thermalManager.updatePID);
@@ -2224,7 +2224,7 @@ void CrealityDWINClass::Menu_Item_Handler(uint8_t menu, uint8_t item, bool draw/
2224 2224
           case BEDPID_KD:
2225 2225
             if (draw) {
2226 2226
               Draw_Menu_Item(row, ICON_Version, F("Kd Value"));
2227
-              Draw_Float(unscalePID_d(thermalManager.temp_bed.pid.Kd), row, false, 100);
2227
+              Draw_Float(thermalManager.temp_bed.pid.d(), row, false, 100);
2228 2228
             }
2229 2229
             else
2230 2230
               Modify_Value(thermalManager.temp_bed.pid.Kd, 0, 5000, 100, thermalManager.updatePID);

+ 11
- 9
Marlin/src/lcd/e3v2/proui/dwin.cpp View File

@@ -2667,13 +2667,15 @@ void SetStepsY() { HMI_value.axis = Y_AXIS, SetPFloatOnClick( MIN_STEP, MAX_STEP
2667 2667
 void SetStepsZ() { HMI_value.axis = Z_AXIS, SetPFloatOnClick( MIN_STEP, MAX_STEP, UNITFDIGITS); }
2668 2668
 #if HAS_HOTEND
2669 2669
   void SetStepsE() { HMI_value.axis = E_AXIS; SetPFloatOnClick( MIN_STEP, MAX_STEP, UNITFDIGITS); }
2670
-  void SetHotendPidT() { SetPIntOnClick(MIN_ETEMP, MAX_ETEMP); }
2670
+  #if ENABLED(PIDTEMP)
2671
+    void SetHotendPidT() { SetPIntOnClick(MIN_ETEMP, MAX_ETEMP); }
2672
+  #endif
2671 2673
 #endif
2672
-#if HAS_HEATED_BED
2674
+#if ENABLED(PIDTEMPBED)
2673 2675
   void SetBedPidT() { SetPIntOnClick(MIN_BEDTEMP, MAX_BEDTEMP); }
2674 2676
 #endif
2675 2677
 
2676
-#if HAS_HOTEND || HAS_HEATED_BED
2678
+#if EITHER(PIDTEMP, PIDTEMPBED)
2677 2679
   void SetPidCycles() { SetPIntOnClick(3, 50); }
2678 2680
   void SetKp() { SetPFloatOnClick(0, 1000, 2); }
2679 2681
   void ApplyPIDi() {
@@ -3222,10 +3224,10 @@ void Draw_AdvancedSettings_Menu() {
3222 3224
     #if HAS_HOME_OFFSET
3223 3225
       MENU_ITEM_F(ICON_HomeOffset, MSG_SET_HOME_OFFSETS, onDrawSubMenu, Draw_HomeOffset_Menu);
3224 3226
     #endif
3225
-    #if HAS_HOTEND
3227
+    #if ENABLED(PIDTEMP)
3226 3228
       MENU_ITEM(ICON_PIDNozzle, F(STR_HOTEND_PID " Settings"), onDrawSubMenu, Draw_HotendPID_Menu);
3227 3229
     #endif
3228
-    #if HAS_HEATED_BED
3230
+    #if ENABLED(PIDTEMPBED)
3229 3231
       MENU_ITEM(ICON_PIDbed, F(STR_BED_PID " Settings"), onDrawSubMenu, Draw_BedPID_Menu);
3230 3232
     #endif
3231 3233
       MENU_ITEM_F(ICON_FilSet, MSG_FILAMENT_SET, onDrawSubMenu, Draw_FilSet_Menu);
@@ -3668,10 +3670,10 @@ void Draw_Steps_Menu() {
3668 3670
   CurrentMenu->draw();
3669 3671
 }
3670 3672
 
3671
-#if HAS_HOTEND
3673
+#if ENABLED(PIDTEMP)
3672 3674
   void Draw_HotendPID_Menu() {
3673 3675
     checkkey = Menu;
3674
-    if (SetMenu(HotendPIDMenu, F(STR_HOTEND_PID " Settings"),8)) {
3676
+    if (SetMenu(HotendPIDMenu, F(STR_HOTEND_PID " Settings"), 8)) {
3675 3677
       BACK_ITEM(Draw_AdvancedSettings_Menu);
3676 3678
       MENU_ITEM(ICON_PIDNozzle, F(STR_HOTEND_PID), onDrawMenuItem, HotendPID);
3677 3679
       EDIT_ITEM(ICON_PIDValue, F("Set" STR_KP), onDrawPFloat2Menu, SetKp, &thermalManager.temp_hotend[0].pid.Kp);
@@ -3687,10 +3689,10 @@ void Draw_Steps_Menu() {
3687 3689
   }
3688 3690
 #endif
3689 3691
 
3690
-#if HAS_HEATED_BED
3692
+#if ENABLED(PIDTEMPBED)
3691 3693
   void Draw_BedPID_Menu() {
3692 3694
     checkkey = Menu;
3693
-    if (SetMenu(BedPIDMenu, F(STR_BED_PID " Settings"),8)) {
3695
+    if (SetMenu(BedPIDMenu, F(STR_BED_PID " Settings"), 8)) {
3694 3696
       BACK_ITEM(Draw_AdvancedSettings_Menu);
3695 3697
       MENU_ITEM(ICON_PIDNozzle, F(STR_BED_PID), onDrawMenuItem,BedPID);
3696 3698
       EDIT_ITEM(ICON_PIDValue, F("Set" STR_KP), onDrawPFloat2Menu, SetKp, &thermalManager.temp_bed.pid.Kp);

+ 4
- 2
Marlin/src/lcd/e3v2/proui/dwin.h View File

@@ -264,7 +264,9 @@ void Draw_Motion_Menu();
264 264
   void Draw_Preheat1_Menu();
265 265
   void Draw_Preheat2_Menu();
266 266
   void Draw_Preheat3_Menu();
267
-  void Draw_HotendPID_Menu();
267
+  #if ENABLED(PIDTEMP)
268
+    void Draw_HotendPID_Menu();
269
+  #endif
268 270
 #endif
269 271
 void Draw_Temperature_Menu();
270 272
 void Draw_MaxSpeed_Menu();
@@ -273,7 +275,7 @@ void Draw_MaxAccel_Menu();
273 275
   void Draw_MaxJerk_Menu();
274 276
 #endif
275 277
 void Draw_Steps_Menu();
276
-#if HAS_HEATED_BED
278
+#if ENABLED(PIDTEMPBED)
277 279
   void Draw_BedPID_Menu();
278 280
 #endif
279 281
 #if EITHER(HAS_BED_PROBE, BABYSTEPPING)

+ 9
- 9
Marlin/src/lcd/extui/dgus_reloaded/DGUSTxHandler.cpp View File

@@ -421,16 +421,16 @@ void DGUSTxHandler::PIDKp(DGUS_VP &vp) {
421 421
     default: return;
422 422
     #if ENABLED(PIDTEMPBED)
423 423
       case DGUS_Data::Heater::BED:
424
-        value = ExtUI::getBedPIDValues_Kp();
424
+        value = ExtUI::getBedPID_Kp();
425 425
         break;
426 426
     #endif
427 427
     #if ENABLED(PIDTEMP)
428 428
       case DGUS_Data::Heater::H0:
429
-        value = ExtUI::getPIDValues_Kp(ExtUI::E0);
429
+        value = ExtUI::getPID_Kp(ExtUI::E0);
430 430
         break;
431 431
       #if HAS_MULTI_HOTEND
432 432
         case DGUS_Data::Heater::H1:
433
-          value = ExtUI::getPIDValues_Kp(ExtUI::E1);
433
+          value = ExtUI::getPID_Kp(ExtUI::E1);
434 434
           break;
435 435
       #endif
436 436
     #endif
@@ -447,16 +447,16 @@ void DGUSTxHandler::PIDKi(DGUS_VP &vp) {
447 447
     default: return;
448 448
     #if ENABLED(PIDTEMPBED)
449 449
       case DGUS_Data::Heater::BED:
450
-        value = ExtUI::getBedPIDValues_Ki();
450
+        value = ExtUI::getBedPID_Ki();
451 451
         break;
452 452
     #endif
453 453
     #if ENABLED(PIDTEMP)
454 454
       case DGUS_Data::Heater::H0:
455
-        value = ExtUI::getPIDValues_Ki(ExtUI::E0);
455
+        value = ExtUI::getPID_Ki(ExtUI::E0);
456 456
         break;
457 457
       #if HAS_MULTI_HOTEND
458 458
         case DGUS_Data::Heater::H1:
459
-          value = ExtUI::getPIDValues_Ki(ExtUI::E1);
459
+          value = ExtUI::getPID_Ki(ExtUI::E1);
460 460
           break;
461 461
       #endif
462 462
     #endif
@@ -473,16 +473,16 @@ void DGUSTxHandler::PIDKd(DGUS_VP &vp) {
473 473
     default: return;
474 474
     #if ENABLED(PIDTEMPBED)
475 475
       case DGUS_Data::Heater::BED:
476
-        value = ExtUI::getBedPIDValues_Kd();
476
+        value = ExtUI::getBedPID_Kd();
477 477
         break;
478 478
     #endif
479 479
     #if ENABLED(PIDTEMP)
480 480
       case DGUS_Data::Heater::H0:
481
-        value = ExtUI::getPIDValues_Kd(ExtUI::E0);
481
+        value = ExtUI::getPID_Kd(ExtUI::E0);
482 482
         break;
483 483
       #if HAS_MULTI_HOTEND
484 484
         case DGUS_Data::Heater::H1:
485
-          value = ExtUI::getPIDValues_Kd(ExtUI::E1);
485
+          value = ExtUI::getPID_Kd(ExtUI::E1);
486 486
           break;
487 487
       #endif
488 488
     #endif

+ 3
- 3
Marlin/src/lcd/extui/nextion/nextion_tft.cpp View File

@@ -459,17 +459,17 @@ void NextionTFT::PanelInfo(uint8_t req) {
459 459
 
460 460
   case 37: // PID
461 461
     #if ENABLED(PIDTEMP)
462
-      #define SEND_PID_INFO_0(A, B) SEND_VALasTXT(A, getPIDValues_K##B(E0))
462
+      #define SEND_PID_INFO_0(A, B) SEND_VALasTXT(A, getPID_K##B(E0))
463 463
     #else
464 464
       #define SEND_PID_INFO_0(A, B) SEND_NA(A)
465 465
     #endif
466 466
     #if BOTH(PIDTEMP, HAS_MULTI_EXTRUDER)
467
-      #define SEND_PID_INFO_1(A, B) SEND_VALasTXT(A, getPIDValues_K##B(E1))
467
+      #define SEND_PID_INFO_1(A, B) SEND_VALasTXT(A, getPID_K##B(E1))
468 468
     #else
469 469
       #define SEND_PID_INFO_1(A, B) SEND_NA(A)
470 470
     #endif
471 471
     #if ENABLED(PIDTEMPBED)
472
-      #define SEND_PID_INFO_BED(A, B) SEND_VALasTXT(A, getBedPIDValues_K##B())
472
+      #define SEND_PID_INFO_BED(A, B) SEND_VALasTXT(A, getBedPID_K##B())
473 473
     #else
474 474
       #define SEND_PID_INFO_BED(A, B) SEND_NA(A)
475 475
     #endif

+ 13
- 19
Marlin/src/lcd/extui/ui_api.cpp View File

@@ -976,32 +976,26 @@ namespace ExtUI {
976 976
   float getFeedrate_percent() { return feedrate_percentage; }
977 977
 
978 978
   #if ENABLED(PIDTEMP)
979
-    float getPIDValues_Kp(const extruder_t tool) { return PID_PARAM(Kp, tool); }
980
-    float getPIDValues_Ki(const extruder_t tool) { return unscalePID_i(PID_PARAM(Ki, tool)); }
981
-    float getPIDValues_Kd(const extruder_t tool) { return unscalePID_d(PID_PARAM(Kd, tool)); }
982
-
983
-    void setPIDValues(const_float_t p, const_float_t i, const_float_t d, extruder_t tool) {
984
-      thermalManager.temp_hotend[tool].pid.Kp = p;
985
-      thermalManager.temp_hotend[tool].pid.Ki = scalePID_i(i);
986
-      thermalManager.temp_hotend[tool].pid.Kd = scalePID_d(d);
987
-      thermalManager.updatePID();
979
+    float getPID_Kp(const extruder_t tool) { return thermalManager.temp_hotend[tool].pid.p(); }
980
+    float getPID_Ki(const extruder_t tool) { return thermalManager.temp_hotend[tool].pid.i(); }
981
+    float getPID_Kd(const extruder_t tool) { return thermalManager.temp_hotend[tool].pid.d(); }
982
+
983
+    void setPID(const_float_t p, const_float_t i, const_float_t d, extruder_t tool) {
984
+      thermalManager.setPID(uint8_t(tool), p, i, d);
988 985
     }
989 986
 
990 987
     void startPIDTune(const celsius_t temp, extruder_t tool) {
991
-      thermalManager.PID_autotune(temp, (heater_id_t)tool, 8, true);
988
+      thermalManager.PID_autotune(temp, heater_id_t(tool), 8, true);
992 989
     }
993 990
   #endif
994 991
 
995 992
   #if ENABLED(PIDTEMPBED)
996
-    float getBedPIDValues_Kp() { return thermalManager.temp_bed.pid.Kp; }
997
-    float getBedPIDValues_Ki() { return unscalePID_i(thermalManager.temp_bed.pid.Ki); }
998
-    float getBedPIDValues_Kd() { return unscalePID_d(thermalManager.temp_bed.pid.Kd); }
999
-
1000
-    void setBedPIDValues(const_float_t p, const_float_t i, const_float_t d) {
1001
-      thermalManager.temp_bed.pid.Kp = p;
1002
-      thermalManager.temp_bed.pid.Ki = scalePID_i(i);
1003
-      thermalManager.temp_bed.pid.Kd = scalePID_d(d);
1004
-      thermalManager.updatePID();
993
+    float getBedPID_Kp() { return thermalManager.temp_bed.pid.p(); }
994
+    float getBedPID_Ki() { return thermalManager.temp_bed.pid.i(); }
995
+    float getBedPID_Kd() { return thermalManager.temp_bed.pid.d(); }
996
+
997
+    void setBedPID(const_float_t p, const_float_t i, const_float_t d) {
998
+      thermalManager.temp_bed.pid.set(p, i, d);
1005 999
     }
1006 1000
 
1007 1001
     void startBedPIDTune(const celsius_t temp) {

+ 8
- 8
Marlin/src/lcd/extui/ui_api.h View File

@@ -324,18 +324,18 @@ namespace ExtUI {
324 324
   #endif
325 325
 
326 326
   #if ENABLED(PIDTEMP)
327
-    float getPIDValues_Kp(const extruder_t);
328
-    float getPIDValues_Ki(const extruder_t);
329
-    float getPIDValues_Kd(const extruder_t);
330
-    void setPIDValues(const_float_t, const_float_t , const_float_t , extruder_t);
327
+    float getPID_Kp(const extruder_t);
328
+    float getPID_Ki(const extruder_t);
329
+    float getPID_Kd(const extruder_t);
330
+    void setPID(const_float_t, const_float_t , const_float_t , extruder_t);
331 331
     void startPIDTune(const celsius_t, extruder_t);
332 332
   #endif
333 333
 
334 334
   #if ENABLED(PIDTEMPBED)
335
-    float getBedPIDValues_Kp();
336
-    float getBedPIDValues_Ki();
337
-    float getBedPIDValues_Kd();
338
-    void setBedPIDValues(const_float_t, const_float_t , const_float_t);
335
+    float getBedPID_Kp();
336
+    float getBedPID_Ki();
337
+    float getBedPID_Kd();
338
+    void setBedPID(const_float_t, const_float_t , const_float_t);
339 339
     void startBedPIDTune(const celsius_t);
340 340
   #endif
341 341
 

+ 49
- 23
Marlin/src/lcd/menu/menu_advanced.cpp View File

@@ -209,37 +209,59 @@ void menu_backlash();
209 209
 
210 210
 #if ENABLED(PID_EDIT_MENU)
211 211
 
212
-  float raw_Ki, raw_Kd; // place-holders for Ki and Kd edits
212
+  // Placeholders for PID editing
213
+  float raw_Kp, raw_Ki, raw_Kd;
214
+  #if ENABLED(PID_EXTRUSION_SCALING)
215
+    float raw_Kc;
216
+  #endif
217
+  #if ENABLED(PID_FAN_SCALING)
218
+    float raw_Kf;
219
+  #endif
213 220
 
214
-  // Helpers for editing PID Ki & Kd values
215
-  // grab the PID value out of the temp variable; scale it; then update the PID driver
216
-  void copy_and_scalePID_i(const int8_t e) {
221
+  // Helpers for editing PID Kp, Ki and Kd values
222
+  void apply_PID_p(const int8_t e) {
223
+    switch (e) {
224
+      #if ENABLED(PIDTEMPBED)
225
+        case H_BED: thermalManager.temp_bed.pid.set_Ki(raw_Ki); break;
226
+      #endif
227
+      #if ENABLED(PIDTEMPCHAMBER)
228
+        case H_CHAMBER: thermalManager.temp_chamber.pid.set_Ki(raw_Ki); break;
229
+      #endif
230
+      default:
231
+        #if ENABLED(PIDTEMP)
232
+          SET_HOTEND_PID(Kp, e, raw_Kp);
233
+          thermalManager.updatePID();
234
+        #endif
235
+        break;
236
+    }
237
+  }
238
+  void apply_PID_i(const int8_t e) {
217 239
     switch (e) {
218 240
       #if ENABLED(PIDTEMPBED)
219
-        case H_BED: thermalManager.temp_bed.pid.Ki = scalePID_i(raw_Ki); break;
241
+        case H_BED: thermalManager.temp_bed.pid.set_Ki(raw_Ki); break;
220 242
       #endif
221 243
       #if ENABLED(PIDTEMPCHAMBER)
222
-        case H_CHAMBER: thermalManager.temp_chamber.pid.Ki = scalePID_i(raw_Ki); break;
244
+        case H_CHAMBER: thermalManager.temp_chamber.pid.set_Ki(raw_Ki); break;
223 245
       #endif
224 246
       default:
225 247
         #if ENABLED(PIDTEMP)
226
-          PID_PARAM(Ki, e) = scalePID_i(raw_Ki);
248
+          SET_HOTEND_PID(Ki, e, raw_Ki);
227 249
           thermalManager.updatePID();
228 250
         #endif
229 251
         break;
230 252
     }
231 253
   }
232
-  void copy_and_scalePID_d(const int8_t e) {
254
+  void apply_PID_d(const int8_t e) {
233 255
     switch (e) {
234 256
       #if ENABLED(PIDTEMPBED)
235
-        case H_BED: thermalManager.temp_bed.pid.Kd = scalePID_d(raw_Kd); break;
257
+        case H_BED: thermalManager.temp_bed.pid.set_Kd(raw_Kd); break;
236 258
       #endif
237 259
       #if ENABLED(PIDTEMPCHAMBER)
238
-        case H_CHAMBER: thermalManager.temp_chamber.pid.Kd = scalePID_d(raw_Kd); break;
260
+        case H_CHAMBER: thermalManager.temp_chamber.pid.set_Kd(raw_Kd); break;
239 261
       #endif
240 262
       default:
241 263
         #if ENABLED(PIDTEMP)
242
-          PID_PARAM(Kd, e) = scalePID_d(raw_Kd);
264
+          SET_HOTEND_PID(Kd, e, raw_Kd);
243 265
           thermalManager.updatePID();
244 266
         #endif
245 267
         break;
@@ -291,16 +313,18 @@ void menu_backlash();
291 313
 
292 314
     #if BOTH(PIDTEMP, PID_EDIT_MENU)
293 315
       #define __PID_HOTEND_MENU_ITEMS(N) \
294
-        raw_Ki = unscalePID_i(PID_PARAM(Ki, N)); \
295
-        raw_Kd = unscalePID_d(PID_PARAM(Kd, N)); \
296
-        EDIT_ITEM_FAST_N(float41sign, N, MSG_PID_P_E, &PID_PARAM(Kp, N), 1, 9990); \
297
-        EDIT_ITEM_FAST_N(float52sign, N, MSG_PID_I_E, &raw_Ki, 0.01f, 9990, []{ copy_and_scalePID_i(N); }); \
298
-        EDIT_ITEM_FAST_N(float41sign, N, MSG_PID_D_E, &raw_Kd, 1, 9990, []{ copy_and_scalePID_d(N); })
316
+        raw_Kp = thermalManager.temp_hotend[N].pid.p(); \
317
+        raw_Ki = thermalManager.temp_hotend[N].pid.i(); \
318
+        raw_Kd = thermalManager.temp_hotend[N].pid.d(); \
319
+        EDIT_ITEM_FAST_N(float41sign, N, MSG_PID_P_E, &raw_Kp, 1, 9990, []{ apply_PID_p(N); }); \
320
+        EDIT_ITEM_FAST_N(float52sign, N, MSG_PID_I_E, &raw_Ki, 0.01f, 9990, []{ apply_PID_i(N); }); \
321
+        EDIT_ITEM_FAST_N(float41sign, N, MSG_PID_D_E, &raw_Kd, 1, 9990, []{ apply_PID_d(N); })
299 322
 
300 323
       #if ENABLED(PID_EXTRUSION_SCALING)
301 324
         #define _PID_HOTEND_MENU_ITEMS(N) \
302 325
           __PID_HOTEND_MENU_ITEMS(N); \
303
-          EDIT_ITEM_N(float4, N, MSG_PID_C_E, &PID_PARAM(Kc, N), 1, 9990)
326
+          raw_Kc = thermalManager.temp_hotend[N].pid.c(); \
327
+          EDIT_ITEM_N(float4, N, MSG_PID_C_E, &raw_Kc, 1, 9990, []{ SET_HOTEND_PID(Kc, N, raw_Kc); thermalManager.updatePID(); });
304 328
       #else
305 329
         #define _PID_HOTEND_MENU_ITEMS(N) __PID_HOTEND_MENU_ITEMS(N)
306 330
       #endif
@@ -308,7 +332,8 @@ void menu_backlash();
308 332
       #if ENABLED(PID_FAN_SCALING)
309 333
         #define _HOTEND_PID_EDIT_MENU_ITEMS(N) \
310 334
           _PID_HOTEND_MENU_ITEMS(N); \
311
-          EDIT_ITEM_N(float4, N, MSG_PID_F_E, &PID_PARAM(Kf, N), 1, 9990)
335
+          raw_Kf = thermalManager.temp_hotend[N].pid.f(); \
336
+          EDIT_ITEM_N(float4, N, MSG_PID_F_E, &raw_Kf, 1, 9990, []{ SET_HOTEND_PID(Kf, N, raw_Kf); thermalManager.updatePID(); });
312 337
       #else
313 338
         #define _HOTEND_PID_EDIT_MENU_ITEMS(N) _PID_HOTEND_MENU_ITEMS(N)
314 339
       #endif
@@ -321,11 +346,12 @@ void menu_backlash();
321 346
 
322 347
     #if ENABLED(PID_EDIT_MENU) && EITHER(PIDTEMPBED, PIDTEMPCHAMBER)
323 348
       #define _PID_EDIT_ITEMS_TMPL(N,T) \
324
-        raw_Ki = unscalePID_i(T.pid.Ki); \
325
-        raw_Kd = unscalePID_d(T.pid.Kd); \
326
-        EDIT_ITEM_FAST_N(float41sign, N, MSG_PID_P_E, &T.pid.Kp, 1, 9990); \
327
-        EDIT_ITEM_FAST_N(float52sign, N, MSG_PID_I_E, &raw_Ki, 0.01f, 9990, []{ copy_and_scalePID_i(N); }); \
328
-        EDIT_ITEM_FAST_N(float41sign, N, MSG_PID_D_E, &raw_Kd, 1, 9990, []{ copy_and_scalePID_d(N); })
349
+        raw_Kp = T.pid.p(); \
350
+        raw_Ki = T.pid.i(); \
351
+        raw_Kd = T.pid.d(); \
352
+        EDIT_ITEM_FAST_N(float41sign, N, MSG_PID_P_E, &raw_Kp, 1, 9990, []{ apply_PID_p(N); }); \
353
+        EDIT_ITEM_FAST_N(float52sign, N, MSG_PID_I_E, &raw_Ki, 0.01f, 9990, []{ apply_PID_i(N); }); \
354
+        EDIT_ITEM_FAST_N(float41sign, N, MSG_PID_D_E, &raw_Kd, 1, 9990, []{ apply_PID_d(N); })
329 355
     #endif
330 356
 
331 357
     #if ENABLED(PIDTEMP)

+ 49
- 83
Marlin/src/module/settings.cpp View File

@@ -364,18 +364,18 @@ typedef struct SettingsDataStruct {
364 364
   //
365 365
   // PIDTEMP
366 366
   //
367
-  PIDCF_t hotendPID[HOTENDS];                           // M301 En PIDCF / M303 En U
367
+  raw_pidcf_t hotendPID[HOTENDS];                       // M301 En PIDCF / M303 En U
368 368
   int16_t lpq_len;                                      // M301 L
369 369
 
370 370
   //
371 371
   // PIDTEMPBED
372 372
   //
373
-  PID_t bedPID;                                         // M304 PID / M303 E-1 U
373
+  raw_pid_t bedPID;                                     // M304 PID / M303 E-1 U
374 374
 
375 375
   //
376 376
   // PIDTEMPCHAMBER
377 377
   //
378
-  PID_t chamberPID;                                     // M309 PID / M303 E-2 U
378
+  raw_pid_t chamberPID;                                 // M309 PID / M303 E-2 U
379 379
 
380 380
   //
381 381
   // User-defined Thermistors
@@ -1052,27 +1052,20 @@ void MarlinSettings::postprocess() {
1052 1052
     //
1053 1053
     {
1054 1054
       _FIELD_TEST(hotendPID);
1055
+      #if DISABLED(PIDTEMP)
1056
+        raw_pidcf_t pidcf = { NAN, NAN, NAN, NAN, NAN };
1057
+      #endif
1055 1058
       HOTEND_LOOP() {
1056
-        PIDCF_t pidcf = {
1057
-          #if DISABLED(PIDTEMP)
1058
-            NAN, NAN, NAN,
1059
-            NAN, NAN
1060
-          #else
1061
-                         PID_PARAM(Kp, e),
1062
-            unscalePID_i(PID_PARAM(Ki, e)),
1063
-            unscalePID_d(PID_PARAM(Kd, e)),
1064
-                         PID_PARAM(Kc, e),
1065
-                         PID_PARAM(Kf, e)
1066
-          #endif
1067
-        };
1059
+        #if ENABLED(PIDTEMP)
1060
+          const hotend_pid_t &pid = thermalManager.temp_hotend[e].pid;
1061
+          raw_pidcf_t pidcf = { pid.p(), pid.i(), pid.d(), pid.c(), pid.f() };
1062
+        #endif
1068 1063
         EEPROM_WRITE(pidcf);
1069 1064
       }
1070 1065
 
1071 1066
       _FIELD_TEST(lpq_len);
1072
-      #if DISABLED(PID_EXTRUSION_SCALING)
1073
-        const int16_t lpq_len = 20;
1074
-      #endif
1075
-      EEPROM_WRITE(TERN(PID_EXTRUSION_SCALING, thermalManager.lpq_len, lpq_len));
1067
+      const int16_t lpq_len = TERN(PID_EXTRUSION_SCALING, thermalManager.lpq_len, 20);
1068
+      EEPROM_WRITE(lpq_len);
1076 1069
     }
1077 1070
 
1078 1071
     //
@@ -1080,17 +1073,12 @@ void MarlinSettings::postprocess() {
1080 1073
     //
1081 1074
     {
1082 1075
       _FIELD_TEST(bedPID);
1083
-
1084
-      const PID_t bed_pid = {
1085
-        #if DISABLED(PIDTEMPBED)
1086
-          NAN, NAN, NAN
1087
-        #else
1088
-          // Store the unscaled PID values
1089
-          thermalManager.temp_bed.pid.Kp,
1090
-          unscalePID_i(thermalManager.temp_bed.pid.Ki),
1091
-          unscalePID_d(thermalManager.temp_bed.pid.Kd)
1092
-        #endif
1093
-      };
1076
+      #if ENABLED(PIDTEMPBED)
1077
+        const PID_t &pid = thermalManager.temp_bed.pid;
1078
+        const raw_pid_t bed_pid = { pid.p(), pid.i(), pid.d() };
1079
+      #else
1080
+        const raw_pid_t bed_pid = { NAN, NAN, NAN };
1081
+      #endif
1094 1082
       EEPROM_WRITE(bed_pid);
1095 1083
     }
1096 1084
 
@@ -1099,17 +1087,12 @@ void MarlinSettings::postprocess() {
1099 1087
     //
1100 1088
     {
1101 1089
       _FIELD_TEST(chamberPID);
1102
-
1103
-      const PID_t chamber_pid = {
1104
-        #if DISABLED(PIDTEMPCHAMBER)
1105
-          NAN, NAN, NAN
1106
-        #else
1107
-          // Store the unscaled PID values
1108
-          thermalManager.temp_chamber.pid.Kp,
1109
-          unscalePID_i(thermalManager.temp_chamber.pid.Ki),
1110
-          unscalePID_d(thermalManager.temp_chamber.pid.Kd)
1111
-        #endif
1112
-      };
1090
+      #if ENABLED(PIDTEMPCHAMBER)
1091
+        const PID_t &pid = thermalManager.temp_chamber.pid;
1092
+        const raw_pid_t chamber_pid = { pid.p(), pid.i(), pid.d() };
1093
+      #else
1094
+        const raw_pid_t chamber_pid = { NAN, NAN, NAN };
1095
+      #endif
1113 1096
       EEPROM_WRITE(chamber_pid);
1114 1097
     }
1115 1098
 
@@ -1117,10 +1100,8 @@ void MarlinSettings::postprocess() {
1117 1100
     // User-defined Thermistors
1118 1101
     //
1119 1102
     #if HAS_USER_THERMISTORS
1120
-    {
1121 1103
       _FIELD_TEST(user_thermistor);
1122 1104
       EEPROM_WRITE(thermalManager.user_thermistor);
1123
-    }
1124 1105
     #endif
1125 1106
 
1126 1107
     //
@@ -2003,17 +1984,11 @@ void MarlinSettings::postprocess() {
2003 1984
       //
2004 1985
       {
2005 1986
         HOTEND_LOOP() {
2006
-          PIDCF_t pidcf;
1987
+          raw_pidcf_t pidcf;
2007 1988
           EEPROM_READ(pidcf);
2008 1989
           #if ENABLED(PIDTEMP)
2009
-            if (!validating && !isnan(pidcf.Kp)) {
2010
-              // Scale PID values since EEPROM values are unscaled
2011
-              PID_PARAM(Kp, e) = pidcf.Kp;
2012
-              PID_PARAM(Ki, e) = scalePID_i(pidcf.Ki);
2013
-              PID_PARAM(Kd, e) = scalePID_d(pidcf.Kd);
2014
-              TERN_(PID_EXTRUSION_SCALING, PID_PARAM(Kc, e) = pidcf.Kc);
2015
-              TERN_(PID_FAN_SCALING, PID_PARAM(Kf, e) = pidcf.Kf);
2016
-            }
1990
+            if (!validating && !isnan(pidcf.p))
1991
+              thermalManager.temp_hotend[e].pid.set(pidcf);
2017 1992
           #endif
2018 1993
         }
2019 1994
       }
@@ -2035,15 +2010,11 @@ void MarlinSettings::postprocess() {
2035 2010
       // Heated Bed PID
2036 2011
       //
2037 2012
       {
2038
-        PID_t pid;
2013
+        raw_pid_t pid;
2039 2014
         EEPROM_READ(pid);
2040 2015
         #if ENABLED(PIDTEMPBED)
2041
-          if (!validating && !isnan(pid.Kp)) {
2042
-            // Scale PID values since EEPROM values are unscaled
2043
-            thermalManager.temp_bed.pid.Kp = pid.Kp;
2044
-            thermalManager.temp_bed.pid.Ki = scalePID_i(pid.Ki);
2045
-            thermalManager.temp_bed.pid.Kd = scalePID_d(pid.Kd);
2046
-          }
2016
+          if (!validating && !isnan(pid.p))
2017
+            thermalManager.temp_bed.pid.set(pid);
2047 2018
         #endif
2048 2019
       }
2049 2020
 
@@ -2051,15 +2022,11 @@ void MarlinSettings::postprocess() {
2051 2022
       // Heated Chamber PID
2052 2023
       //
2053 2024
       {
2054
-        PID_t pid;
2025
+        raw_pid_t pid;
2055 2026
         EEPROM_READ(pid);
2056 2027
         #if ENABLED(PIDTEMPCHAMBER)
2057
-          if (!validating && !isnan(pid.Kp)) {
2058
-            // Scale PID values since EEPROM values are unscaled
2059
-            thermalManager.temp_chamber.pid.Kp = pid.Kp;
2060
-            thermalManager.temp_chamber.pid.Ki = scalePID_i(pid.Ki);
2061
-            thermalManager.temp_chamber.pid.Kd = scalePID_d(pid.Kd);
2062
-          }
2028
+          if (!validating && !isnan(pid.p))
2029
+            thermalManager.temp_chamber.pid.set(pid);
2063 2030
         #endif
2064 2031
       }
2065 2032
 
@@ -3142,11 +3109,13 @@ void MarlinSettings::reset() {
3142 3109
       #define PID_DEFAULT(N,E) DEFAULT_##N
3143 3110
     #endif
3144 3111
     HOTEND_LOOP() {
3145
-      PID_PARAM(Kp, e) =      float(PID_DEFAULT(Kp, ALIM(e, defKp)));
3146
-      PID_PARAM(Ki, e) = scalePID_i(PID_DEFAULT(Ki, ALIM(e, defKi)));
3147
-      PID_PARAM(Kd, e) = scalePID_d(PID_DEFAULT(Kd, ALIM(e, defKd)));
3148
-      TERN_(PID_EXTRUSION_SCALING, PID_PARAM(Kc, e) = float(PID_DEFAULT(Kc, ALIM(e, defKc))));
3149
-      TERN_(PID_FAN_SCALING, PID_PARAM(Kf, e) = float(PID_DEFAULT(Kf, ALIM(e, defKf))));
3112
+      thermalManager.temp_hotend[e].pid.set(
3113
+        PID_DEFAULT(Kp, ALIM(e, defKp)),
3114
+        PID_DEFAULT(Ki, ALIM(e, defKi)),
3115
+        PID_DEFAULT(Kd, ALIM(e, defKd))
3116
+        OPTARG(PID_EXTRUSION_SCALING, PID_DEFAULT(Kc, ALIM(e, defKc)))
3117
+        OPTARG(PID_FAN_SCALING, PID_DEFAULT(Kf, ALIM(e, defKf)))
3118
+      );
3150 3119
     }
3151 3120
   #endif
3152 3121
 
@@ -3160,9 +3129,7 @@ void MarlinSettings::reset() {
3160 3129
   //
3161 3130
 
3162 3131
   #if ENABLED(PIDTEMPBED)
3163
-    thermalManager.temp_bed.pid.Kp = DEFAULT_bedKp;
3164
-    thermalManager.temp_bed.pid.Ki = scalePID_i(DEFAULT_bedKi);
3165
-    thermalManager.temp_bed.pid.Kd = scalePID_d(DEFAULT_bedKd);
3132
+    thermalManager.temp_bed.pid.set(DEFAULT_bedKp, DEFAULT_bedKi, DEFAULT_bedKd);
3166 3133
   #endif
3167 3134
 
3168 3135
   //
@@ -3170,9 +3137,7 @@ void MarlinSettings::reset() {
3170 3137
   //
3171 3138
 
3172 3139
   #if ENABLED(PIDTEMPCHAMBER)
3173
-    thermalManager.temp_chamber.pid.Kp = DEFAULT_chamberKp;
3174
-    thermalManager.temp_chamber.pid.Ki = scalePID_i(DEFAULT_chamberKi);
3175
-    thermalManager.temp_chamber.pid.Kd = scalePID_d(DEFAULT_chamberKd);
3140
+    thermalManager.temp_chamber.pid.set(DEFAULT_chamberKp, DEFAULT_chamberKi, DEFAULT_chamberKd);
3176 3141
   #endif
3177 3142
 
3178 3143
   //
@@ -3342,14 +3307,15 @@ void MarlinSettings::reset() {
3342 3307
     static_assert(COUNT(_filament_heat_capacity_permm) == HOTENDS, "FILAMENT_HEAT_CAPACITY_PERMM must have HOTENDS items.");
3343 3308
 
3344 3309
     HOTEND_LOOP() {
3345
-      thermalManager.temp_hotend[e].constants.heater_power = _mpc_heater_power[e];
3346
-      thermalManager.temp_hotend[e].constants.block_heat_capacity = _mpc_block_heat_capacity[e];
3347
-      thermalManager.temp_hotend[e].constants.sensor_responsiveness = _mpc_sensor_responsiveness[e];
3348
-      thermalManager.temp_hotend[e].constants.ambient_xfer_coeff_fan0 = _mpc_ambient_xfer_coeff[e];
3310
+      MPC_t &constants = thermalManager.temp_hotend[e].constants;
3311
+      constants.heater_power = _mpc_heater_power[e];
3312
+      constants.block_heat_capacity = _mpc_block_heat_capacity[e];
3313
+      constants.sensor_responsiveness = _mpc_sensor_responsiveness[e];
3314
+      constants.ambient_xfer_coeff_fan0 = _mpc_ambient_xfer_coeff[e];
3349 3315
       #if ENABLED(MPC_INCLUDE_FAN)
3350
-        thermalManager.temp_hotend[e].constants.fan255_adjustment = _mpc_ambient_xfer_coeff_fan255[e] - _mpc_ambient_xfer_coeff[e];
3316
+        constants.fan255_adjustment = _mpc_ambient_xfer_coeff_fan255[e] - _mpc_ambient_xfer_coeff[e];
3351 3317
       #endif
3352
-      thermalManager.temp_hotend[e].constants.filament_heat_capacity_permm = _filament_heat_capacity_permm[e];
3318
+      constants.filament_heat_capacity_permm = _filament_heat_capacity_permm[e];
3353 3319
     }
3354 3320
   #endif
3355 3321
 

+ 22
- 25
Marlin/src/module/temperature.cpp View File

@@ -597,7 +597,7 @@ volatile bool Temperature::raw_temps_ready = false;
597 597
     millis_t next_temp_ms = millis(), t1 = next_temp_ms, t2 = next_temp_ms;
598 598
     long t_high = 0, t_low = 0;
599 599
 
600
-    PID_t tune_pid = { 0, 0, 0 };
600
+    raw_pid_t tune_pid = { 0, 0, 0 };
601 601
     celsius_float_t maxT = 0, minT = 10000;
602 602
 
603 603
     const bool isbed = (heater_id == H_BED),
@@ -716,16 +716,16 @@ volatile bool Temperature::raw_temps_ready = false;
716 716
                           pf = (ischamber || isbed) ? 0.2f : 0.6f,
717 717
                           df = (ischamber || isbed) ? 1.0f / 3.0f : 1.0f / 8.0f;
718 718
 
719
-              tune_pid.Kp = Ku * pf;
720
-              tune_pid.Ki = tune_pid.Kp * 2.0f / Tu;
721
-              tune_pid.Kd = tune_pid.Kp * Tu * df;
719
+              tune_pid.p = Ku * pf;
720
+              tune_pid.i = tune_pid.p * 2.0f / Tu;
721
+              tune_pid.d = tune_pid.p * Tu * df;
722 722
 
723 723
               SERIAL_ECHOLNPGM(STR_KU, Ku, STR_TU, Tu);
724 724
               if (ischamber || isbed)
725 725
                 SERIAL_ECHOLNPGM(" No overshoot");
726 726
               else
727 727
                 SERIAL_ECHOLNPGM(STR_CLASSIC_PID);
728
-              SERIAL_ECHOLNPGM(STR_KP, tune_pid.Kp, STR_KI, tune_pid.Ki, STR_KD, tune_pid.Kd);
728
+              SERIAL_ECHOLNPGM(STR_KP, tune_pid.p, STR_KI, tune_pid.i, STR_KD, tune_pid.d);
729 729
             }
730 730
           }
731 731
           SHV((bias + d) >> 1);
@@ -795,39 +795,36 @@ volatile bool Temperature::raw_temps_ready = false;
795 795
 
796 796
         #if EITHER(PIDTEMPBED, PIDTEMPCHAMBER)
797 797
           FSTR_P const estring = GHV(F("chamber"), F("bed"), FPSTR(NUL_STR));
798
-          say_default_(); SERIAL_ECHOF(estring); SERIAL_ECHOLNPGM("Kp ", tune_pid.Kp);
799
-          say_default_(); SERIAL_ECHOF(estring); SERIAL_ECHOLNPGM("Ki ", tune_pid.Ki);
800
-          say_default_(); SERIAL_ECHOF(estring); SERIAL_ECHOLNPGM("Kd ", tune_pid.Kd);
798
+          say_default_(); SERIAL_ECHOF(estring); SERIAL_ECHOLNPGM("Kp ", tune_pid.p);
799
+          say_default_(); SERIAL_ECHOF(estring); SERIAL_ECHOLNPGM("Ki ", tune_pid.i);
800
+          say_default_(); SERIAL_ECHOF(estring); SERIAL_ECHOLNPGM("Kd ", tune_pid.d);
801 801
         #else
802
-          say_default_(); SERIAL_ECHOLNPGM("Kp ", tune_pid.Kp);
803
-          say_default_(); SERIAL_ECHOLNPGM("Ki ", tune_pid.Ki);
804
-          say_default_(); SERIAL_ECHOLNPGM("Kd ", tune_pid.Kd);
802
+          say_default_(); SERIAL_ECHOLNPGM("Kp ", tune_pid.p);
803
+          say_default_(); SERIAL_ECHOLNPGM("Ki ", tune_pid.i);
804
+          say_default_(); SERIAL_ECHOLNPGM("Kd ", tune_pid.d);
805 805
         #endif
806 806
 
807
-        auto _set_hotend_pid = [](const uint8_t e, const PID_t &in_pid) {
807
+        auto _set_hotend_pid = [](const uint8_t tool, const raw_pid_t &in_pid) {
808 808
           #if ENABLED(PIDTEMP)
809
-            PID_PARAM(Kp, e) = in_pid.Kp;
810
-            PID_PARAM(Ki, e) = scalePID_i(in_pid.Ki);
811
-            PID_PARAM(Kd, e) = scalePID_d(in_pid.Kd);
809
+            #if ENABLED(PID_PARAMS_PER_HOTEND)
810
+              thermalManager.temp_hotend[tool].pid.set(in_pid);
811
+            #else
812
+              HOTEND_LOOP() thermalManager.temp_hotend[e].pid.set(in_pid);
813
+            #endif
812 814
             updatePID();
813
-          #else
814
-            UNUSED(e); UNUSED(in_pid);
815 815
           #endif
816
+          UNUSED(tool); UNUSED(in_pid);
816 817
         };
817 818
 
818 819
         #if ENABLED(PIDTEMPBED)
819
-          auto _set_bed_pid = [](const PID_t &in_pid) {
820
-            temp_bed.pid.Kp = in_pid.Kp;
821
-            temp_bed.pid.Ki = scalePID_i(in_pid.Ki);
822
-            temp_bed.pid.Kd = scalePID_d(in_pid.Kd);
820
+          auto _set_bed_pid = [](const raw_pid_t &in_pid) {
821
+            temp_bed.pid.set(in_pid);
823 822
           };
824 823
         #endif
825 824
 
826 825
         #if ENABLED(PIDTEMPCHAMBER)
827
-          auto _set_chamber_pid = [](const PID_t &in_pid) {
828
-            temp_chamber.pid.Kp = in_pid.Kp;
829
-            temp_chamber.pid.Ki = scalePID_i(in_pid.Ki);
830
-            temp_chamber.pid.Kd = scalePID_d(in_pid.Kd);
826
+          auto _set_chamber_pid = [](const raw_pid_t &in_pid) {
827
+            temp_chamber.pid.set(in_pid);
831 828
           };
832 829
         #endif
833 830
 

+ 126
- 54
Marlin/src/module/temperature.h View File

@@ -60,53 +60,6 @@ typedef enum : int8_t {
60 60
   H_NONE = -128
61 61
 } heater_id_t;
62 62
 
63
-// PID storage
64
-typedef struct { float Kp, Ki, Kd;     } PID_t;
65
-typedef struct { float Kp, Ki, Kd, Kc; } PIDC_t;
66
-typedef struct { float Kp, Ki, Kd, Kf; } PIDF_t;
67
-typedef struct { float Kp, Ki, Kd, Kc, Kf; } PIDCF_t;
68
-
69
-typedef
70
-  #if BOTH(PID_EXTRUSION_SCALING, PID_FAN_SCALING)
71
-    PIDCF_t
72
-  #elif ENABLED(PID_EXTRUSION_SCALING)
73
-    PIDC_t
74
-  #elif ENABLED(PID_FAN_SCALING)
75
-    PIDF_t
76
-  #else
77
-    PID_t
78
-  #endif
79
-hotend_pid_t;
80
-
81
-#if ENABLED(PID_EXTRUSION_SCALING)
82
-  typedef IF<(LPQ_MAX_LEN > 255), uint16_t, uint8_t>::type lpq_ptr_t;
83
-#endif
84
-
85
-#define PID_PARAM(F,H) _PID_##F(TERN(PID_PARAMS_PER_HOTEND, H, 0 & H)) // Always use 'H' to suppress warning
86
-#define _PID_Kp(H) TERN(PIDTEMP, Temperature::temp_hotend[H].pid.Kp, NAN)
87
-#define _PID_Ki(H) TERN(PIDTEMP, Temperature::temp_hotend[H].pid.Ki, NAN)
88
-#define _PID_Kd(H) TERN(PIDTEMP, Temperature::temp_hotend[H].pid.Kd, NAN)
89
-#if ENABLED(PIDTEMP)
90
-  #define _PID_Kc(H) TERN(PID_EXTRUSION_SCALING, Temperature::temp_hotend[H].pid.Kc, 1)
91
-  #define _PID_Kf(H) TERN(PID_FAN_SCALING,       Temperature::temp_hotend[H].pid.Kf, 0)
92
-#else
93
-  #define _PID_Kc(H) 1
94
-  #define _PID_Kf(H) 0
95
-#endif
96
-
97
-#if ENABLED(MPCTEMP)
98
-  typedef struct {
99
-    float heater_power;                 // M306 P
100
-    float block_heat_capacity;          // M306 C
101
-    float sensor_responsiveness;        // M306 R
102
-    float ambient_xfer_coeff_fan0;      // M306 A
103
-    #if ENABLED(MPC_INCLUDE_FAN)
104
-      float fan255_adjustment;          // M306 F
105
-    #endif
106
-    float filament_heat_capacity_permm; // M306 H
107
-  } MPC_t;
108
-#endif
109
-
110 63
 /**
111 64
  * States for ADC reading in the ISR
112 65
  */
@@ -188,7 +141,15 @@ enum ADCSensorState : char {
188 141
 
189 142
 #define ACTUAL_ADC_SAMPLES _MAX(int(MIN_ADC_ISR_LOOPS), int(SensorsReady))
190 143
 
144
+//
145
+// PID
146
+//
147
+
148
+typedef struct { float p, i, d; } raw_pid_t;
149
+typedef struct { float p, i, d, c, f; } raw_pidcf_t;
150
+
191 151
 #if HAS_PID_HEATING
152
+
192 153
   #define PID_K2 (1-float(PID_K1))
193 154
   #define PID_dT ((OVERSAMPLENR * float(ACTUAL_ADC_SAMPLES)) / (TEMP_TIMER_FREQUENCY))
194 155
 
@@ -197,10 +158,116 @@ enum ADCSensorState : char {
197 158
   #define unscalePID_i(i) ( float(i) / PID_dT )
198 159
   #define scalePID_d(d)   ( float(d) / PID_dT )
199 160
   #define unscalePID_d(d) ( float(d) * PID_dT )
161
+
162
+  typedef struct {
163
+    float Kp, Ki, Kd;
164
+    float p() const { return Kp; }
165
+    float i() const { return unscalePID_i(Ki); }
166
+    float d() const { return unscalePID_d(Kd); }
167
+    float c() const { return 1; }
168
+    float f() const { return 0; }
169
+    void set_Kp(float p) { Kp = p; }
170
+    void set_Ki(float i) { Ki = scalePID_i(i); }
171
+    void set_Kd(float d) { Kd = scalePID_d(d); }
172
+    void set_Kc(float) {}
173
+    void set_Kf(float) {}
174
+    void set(float p, float i, float d, float c=1, float f=0) { set_Kp(p); set_Ki(i); set_Kd(d); UNUSED(c); UNUSED(f); }
175
+    void set(const raw_pid_t &raw) { set(raw.p, raw.i, raw.d); }
176
+    void set(const raw_pidcf_t &raw) { set(raw.p, raw.i, raw.d); }
177
+  } PID_t;
178
+
200 179
 #endif
201 180
 
202
-#if ENABLED(MPCTEMP)
181
+#if ENABLED(PIDTEMP)
182
+
183
+  typedef struct {
184
+    float Kp, Ki, Kd, Kc;
185
+    float p() const { return Kp; }
186
+    float i() const { return unscalePID_i(Ki); }
187
+    float d() const { return unscalePID_d(Kd); }
188
+    float c() const { return Kc; }
189
+    float f() const { return 0; }
190
+    void set_Kp(float p) { Kp = p; }
191
+    void set_Ki(float i) { Ki = scalePID_i(i); }
192
+    void set_Kd(float d) { Kd = scalePID_d(d); }
193
+    void set_Kc(float c) { Kc = c; }
194
+    void set_Kf(float) {}
195
+    void set(float p, float i, float d, float c=1, float f=0) { set_Kp(p); set_Ki(i); set_Kd(d); set_Kc(c); set_Kf(f); }
196
+    void set(const raw_pid_t &raw) { set(raw.p, raw.i, raw.d); }
197
+    void set(const raw_pidcf_t &raw) { set(raw.p, raw.i, raw.d, raw.c); }
198
+  } PIDC_t;
199
+
200
+  typedef struct {
201
+    float Kp, Ki, Kd, Kf;
202
+    float p() const { return Kp; }
203
+    float i() const { return unscalePID_i(Ki); }
204
+    float d() const { return unscalePID_d(Kd); }
205
+    float c() const { return 1; }
206
+    float f() const { return Kf; }
207
+    void set_Kp(float p) { Kp = p; }
208
+    void set_Ki(float i) { Ki = scalePID_i(i); }
209
+    void set_Kd(float d) { Kd = scalePID_d(d); }
210
+    void set_Kc(float) {}
211
+    void set_Kf(float f) { Kf = f; }
212
+    void set(float p, float i, float d, float c=1, float f=0) { set_Kp(p); set_Ki(i); set_Kd(d); set_Kf(f); }
213
+    void set(const raw_pid_t &raw) { set(raw.p, raw.i, raw.d); }
214
+    void set(const raw_pidcf_t &raw) { set(raw.p, raw.i, raw.d, raw.f); }
215
+  } PIDF_t;
216
+
217
+  typedef struct {
218
+    float Kp, Ki, Kd, Kc, Kf;
219
+    float p() const { return Kp; }
220
+    float i() const { return unscalePID_i(Ki); }
221
+    float d() const { return unscalePID_d(Kd); }
222
+    float c() const { return Kc; }
223
+    float f() const { return Kf; }
224
+    void set_Kp(float p) { Kp = p; }
225
+    void set_Ki(float i) { Ki = scalePID_i(i); }
226
+    void set_Kd(float d) { Kd = scalePID_d(d); }
227
+    void set_Kc(float c) { Kc = c; }
228
+    void set_Kf(float f) { Kf = f; }
229
+    void set(float p, float i, float d, float c=1, float f=0) { set_Kp(p); set_Ki(i); set_Kd(d); set_Kc(c); set_Kf(f); }
230
+    void set(const raw_pid_t &raw) { set(raw.p, raw.i, raw.d); }
231
+    void set(const raw_pidcf_t &raw) { set(raw.p, raw.i, raw.d, raw.c, raw.f); }
232
+  } PIDCF_t;
233
+
234
+  typedef
235
+    #if BOTH(PID_EXTRUSION_SCALING, PID_FAN_SCALING)
236
+      PIDCF_t
237
+    #elif ENABLED(PID_EXTRUSION_SCALING)
238
+      PIDC_t
239
+    #elif ENABLED(PID_FAN_SCALING)
240
+      PIDF_t
241
+    #else
242
+      PID_t
243
+    #endif
244
+  hotend_pid_t;
245
+
246
+  #if ENABLED(PID_EXTRUSION_SCALING)
247
+    typedef IF<(LPQ_MAX_LEN > 255), uint16_t, uint8_t>::type lpq_ptr_t;
248
+  #endif
249
+
250
+  #if ENABLED(PID_PARAMS_PER_HOTEND)
251
+    #define SET_HOTEND_PID(F,H,V) thermalManager.temp_hotend[H].pid.set_##F(V)
252
+  #else
253
+    #define SET_HOTEND_PID(F,_,V) do{ HOTEND_LOOP() thermalManager.temp_hotend[e].pid.set_##F(V); }while(0)
254
+  #endif
255
+
256
+#elif ENABLED(MPCTEMP)
257
+
258
+  typedef struct {
259
+    float heater_power;                 // M306 P
260
+    float block_heat_capacity;          // M306 C
261
+    float sensor_responsiveness;        // M306 R
262
+    float ambient_xfer_coeff_fan0;      // M306 A
263
+    #if ENABLED(MPC_INCLUDE_FAN)
264
+      float fan255_adjustment;          // M306 F
265
+    #endif
266
+    float filament_heat_capacity_permm; // M306 H
267
+  } MPC_t;
268
+
203 269
   #define MPC_dT ((OVERSAMPLENR * float(ACTUAL_ADC_SAMPLES)) / (TEMP_TIMER_FREQUENCY))
270
+
204 271
 #endif
205 272
 
206 273
 #if ENABLED(G26_MESH_VALIDATION) && EITHER(HAS_MARLINUI_MENU, EXTENSIBLE_UI)
@@ -218,7 +285,7 @@ public:
218 285
   inline void sample(const raw_adc_t s) { acc += s; }
219 286
   inline void update() { raw = acc; }
220 287
   void setraw(const raw_adc_t r) { raw = r; }
221
-  raw_adc_t getraw() { return raw; }
288
+  raw_adc_t getraw() const { return raw; }
222 289
 } temp_info_t;
223 290
 
224 291
 #if HAS_TEMP_REDUNDANT
@@ -393,6 +460,7 @@ class Temperature {
393 460
       static const celsius_t hotend_maxtemp[HOTENDS];
394 461
       static celsius_t hotend_max_target(const uint8_t e) { return hotend_maxtemp[e] - (HOTEND_OVERSHOOT); }
395 462
     #endif
463
+
396 464
     #if HAS_HEATED_BED
397 465
       static bed_info_t temp_bed;
398 466
     #endif
@@ -965,12 +1033,16 @@ class Temperature {
965 1033
         static constexpr bool adaptive_fan_slowing = true;
966 1034
       #endif
967 1035
 
968
-      /**
969
-       * Update the temp manager when PID values change
970
-       */
1036
+      // Update the temp manager when PID values change
971 1037
       #if ENABLED(PIDTEMP)
972
-        static void updatePID() {
973
-          TERN_(PID_EXTRUSION_SCALING, pes_e_position = 0);
1038
+        static void updatePID() { TERN_(PID_EXTRUSION_SCALING, pes_e_position = 0); }
1039
+        static void setPID(const uint8_t hotend, const_float_t p, const_float_t i, const_float_t d) {
1040
+          #if ENABLED(PID_PARAMS_PER_HOTEND)
1041
+            temp_hotend[hotend].pid.set(p, i, d);
1042
+          #else
1043
+            HOTEND_LOOP() temp_hotend[e].pid.set(p, i, d);
1044
+          #endif
1045
+          updatePID();
974 1046
         }
975 1047
       #endif
976 1048
 

+ 1
- 1
buildroot/tests/rambo View File

@@ -23,7 +23,7 @@ opt_enable USE_ZMAX_PLUG REPRAP_DISCOUNT_SMART_CONTROLLER LCD_PROGRESS_BAR LCD_P
23 23
            EEPROM_SETTINGS SDSUPPORT SD_REPRINT_LAST_SELECTED_FILE BINARY_FILE_TRANSFER \
24 24
            BLINKM PCA9533 PCA9632 RGB_LED RGB_LED_R_PIN RGB_LED_G_PIN RGB_LED_B_PIN LED_CONTROL_MENU \
25 25
            NEOPIXEL_LED NEOPIXEL_PIN CASE_LIGHT_ENABLE CASE_LIGHT_USE_NEOPIXEL CASE_LIGHT_MENU \
26
-           PID_PARAMS_PER_HOTEND PID_AUTOTUNE_MENU PID_EDIT_MENU LCD_SHOW_E_TOTAL \
26
+           PID_PARAMS_PER_HOTEND PID_AUTOTUNE_MENU PID_EDIT_MENU PID_EXTRUSION_SCALING LCD_SHOW_E_TOTAL \
27 27
            PRINTCOUNTER SERVICE_NAME_1 SERVICE_INTERVAL_1 LCD_BED_TRAMMING BED_TRAMMING_INCLUDE_CENTER \
28 28
            NOZZLE_PARK_FEATURE FILAMENT_RUNOUT_SENSOR FILAMENT_RUNOUT_DISTANCE_MM \
29 29
            ADVANCED_PAUSE_FEATURE FILAMENT_LOAD_UNLOAD_GCODES FILAMENT_UNLOAD_ALL_EXTRUDERS \

Loading…
Cancel
Save