Browse Source

Protect pid_tuning_finished behind temperature.cpp

Setting pid_tuning_finished can result in the heaters stuck to full
power. As a result, we need to ensure that when PID management is
disabled, heaters are also.
Yuri D'Elia 2 years ago
parent
commit
f1d88ebd40
3 changed files with 18 additions and 7 deletions
  1. 13 4
      Firmware/temperature.cpp
  2. 3 1
      Firmware/temperature.h
  3. 2 2
      Firmware/ultralcd.cpp

+ 13 - 4
Firmware/temperature.cpp

@@ -85,7 +85,17 @@ float current_temperature_bed = 0.0;
 #ifdef PIDTEMP
   float _Kp, _Ki, _Kd;
   int pid_cycle, pid_number_of_cycles;
-  bool pid_tuning_finished = true;
+  static bool pid_tuning_finished = true;
+
+  bool pidTuningRunning() {
+      return !pid_tuning_finished;
+  }
+
+  void preparePidTuning() {
+      // ensure heaters are disabled before we switch off PID management!
+      disable_heater();
+      pid_tuning_finished = false;
+  }
 #endif //PIDTEMP
   
 unsigned char soft_pwm_bed;
@@ -207,8 +217,9 @@ bool checkAllHotends(void)
 //          codegen bug causing a stack overwrite issue in process_commands()
 void __attribute__((noinline)) PID_autotune(float temp, int extruder, int ncycles)
 {
+  preparePidTuning();
+
   pid_number_of_cycles = ncycles;
-  pid_tuning_finished = false;
   float input = 0.0;
   pid_cycle=0;
   bool heating = true;
@@ -242,8 +253,6 @@ void __attribute__((noinline)) PID_autotune(float temp, int extruder, int ncycle
         }
 	
   SERIAL_ECHOLNPGM("PID Autotune start");
-  
-  disable_heater(); // switch off all heaters.
 
   if (extruder<0)
   {

+ 3 - 1
Firmware/temperature.h

@@ -84,11 +84,13 @@ extern bool bedPWMDisabled;
 #ifdef PIDTEMP
   extern int pid_cycle, pid_number_of_cycles;
   extern float _Kp,_Ki,_Kd;
-  extern bool pid_tuning_finished;
   float scalePID_i(float i);
   float scalePID_d(float d);
   float unscalePID_i(float i);
   float unscalePID_d(float d);
+
+  bool pidTuningRunning(); // returns true if PID tuning is still running
+  void preparePidTuning(); // non-blocking call to set "pidTuningRunning" to true immediately
 #endif
 
 

+ 2 - 2
Firmware/ultralcd.cpp

@@ -1018,14 +1018,14 @@ void lcd_commands()
 			lcd_commands_step = 3;
 		}
 		if (lcd_commands_step == 3 && !blocks_queued()) { //PID calibration
-			pid_tuning_finished = false; // ensure we don't move to the next step early
+			preparePidTuning(); // ensure we don't move to the next step early
 			sprintf_P(cmd1, PSTR("M303 E0 S%3u"), pid_temp);
 			// setting the correct target temperature (for visualization) is done in PID_autotune
 			enquecommand(cmd1);
 			lcd_setstatuspgm(_i("PID cal."));////MSG_PID_RUNNING c=20
 			lcd_commands_step = 2;
 		}
-		if (lcd_commands_step == 2 && pid_tuning_finished) { //saving to eeprom
+		if (lcd_commands_step == 2 && !pidTuningRunning()) { //saving to eeprom
 			custom_message_state = 0;
 			lcd_setstatuspgm(_i("PID cal. finished"));////MSG_PID_FINISHED c=20
 			setAllTargetHotends(0);  // reset all hotends temperature including the number displayed on the main screen