| 
									
										
										
										
											2018-03-18 11:23:20 -04:00
										 |  |  | # Calibration of heater PID settings | 
					
						
							|  |  |  | # | 
					
						
							|  |  |  | # Copyright (C) 2016-2018  Kevin O'Connor <kevin@koconnor.net> | 
					
						
							|  |  |  | # | 
					
						
							|  |  |  | # This file may be distributed under the terms of the GNU GPLv3 license. | 
					
						
							|  |  |  | import math, logging | 
					
						
							| 
									
										
										
										
											2020-04-25 14:49:50 -04:00
										 |  |  | import heaters | 
					
						
							| 
									
										
										
										
											2018-03-18 11:23:20 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | class PIDCalibrate: | 
					
						
							|  |  |  |     def __init__(self, config): | 
					
						
							|  |  |  |         self.printer = config.get_printer() | 
					
						
							|  |  |  |         self.gcode = self.printer.lookup_object('gcode') | 
					
						
							|  |  |  |         self.gcode.register_command( | 
					
						
							|  |  |  |             'PID_CALIBRATE', self.cmd_PID_CALIBRATE, | 
					
						
							|  |  |  |             desc=self.cmd_PID_CALIBRATE_help) | 
					
						
							|  |  |  |     cmd_PID_CALIBRATE_help = "Run PID calibration test" | 
					
						
							|  |  |  |     def cmd_PID_CALIBRATE(self, params): | 
					
						
							|  |  |  |         heater_name = self.gcode.get_str('HEATER', params) | 
					
						
							|  |  |  |         target = self.gcode.get_float('TARGET', params) | 
					
						
							|  |  |  |         write_file = self.gcode.get_int('WRITE_FILE', params, 0) | 
					
						
							| 
									
										
										
										
											2020-04-25 13:27:41 -04:00
										 |  |  |         pheaters = self.printer.lookup_object('heaters') | 
					
						
							| 
									
										
										
										
											2018-03-18 11:23:20 -04:00
										 |  |  |         try: | 
					
						
							| 
									
										
										
										
											2020-04-25 13:27:41 -04:00
										 |  |  |             heater = pheaters.lookup_heater(heater_name) | 
					
						
							| 
									
										
										
										
											2018-03-18 11:23:20 -04:00
										 |  |  |         except self.printer.config_error as e: | 
					
						
							|  |  |  |             raise self.gcode.error(str(e)) | 
					
						
							| 
									
										
										
										
											2020-01-02 19:17:47 -05:00
										 |  |  |         self.printer.lookup_object('toolhead').get_last_move_time() | 
					
						
							| 
									
										
										
										
											2018-06-27 14:39:20 -04:00
										 |  |  |         calibrate = ControlAutoTune(heater, target) | 
					
						
							| 
									
										
										
										
											2018-03-18 11:23:20 -04:00
										 |  |  |         old_control = heater.set_control(calibrate) | 
					
						
							|  |  |  |         try: | 
					
						
							| 
									
										
										
										
											2020-01-02 19:17:47 -05:00
										 |  |  |             heater.set_temp(target) | 
					
						
							| 
									
										
										
										
											2019-12-16 19:16:58 -05:00
										 |  |  |         except self.printer.command_error as e: | 
					
						
							| 
									
										
										
										
											2018-04-06 10:56:34 -04:00
										 |  |  |             heater.set_control(old_control) | 
					
						
							| 
									
										
										
										
											2019-12-16 19:16:58 -05:00
										 |  |  |             raise | 
					
						
							| 
									
										
										
										
											2020-04-25 13:27:41 -04:00
										 |  |  |         pheaters.wait_for_temperature(heater) | 
					
						
							| 
									
										
										
										
											2018-03-18 11:23:20 -04:00
										 |  |  |         heater.set_control(old_control) | 
					
						
							|  |  |  |         if write_file: | 
					
						
							|  |  |  |             calibrate.write_file('/tmp/heattest.txt') | 
					
						
							| 
									
										
										
										
											2018-09-17 19:28:07 -04:00
										 |  |  |         # Log and report results | 
					
						
							| 
									
										
										
										
											2018-03-18 11:23:20 -04:00
										 |  |  |         Kp, Ki, Kd = calibrate.calc_final_pid() | 
					
						
							|  |  |  |         logging.info("Autotune: final: Kp=%f Ki=%f Kd=%f", Kp, Ki, Kd) | 
					
						
							|  |  |  |         self.gcode.respond_info( | 
					
						
							|  |  |  |             "PID parameters: pid_Kp=%.3f pid_Ki=%.3f pid_Kd=%.3f\n" | 
					
						
							| 
									
										
										
										
											2018-09-17 19:28:07 -04:00
										 |  |  |             "The SAVE_CONFIG command will update the printer config file\n" | 
					
						
							|  |  |  |             "with these parameters and restart the printer." % (Kp, Ki, Kd)) | 
					
						
							|  |  |  |         # Store results for SAVE_CONFIG | 
					
						
							|  |  |  |         configfile = self.printer.lookup_object('configfile') | 
					
						
							|  |  |  |         configfile.set(heater_name, 'control', 'pid') | 
					
						
							|  |  |  |         configfile.set(heater_name, 'pid_Kp', "%.3f" % (Kp,)) | 
					
						
							|  |  |  |         configfile.set(heater_name, 'pid_Ki', "%.3f" % (Ki,)) | 
					
						
							|  |  |  |         configfile.set(heater_name, 'pid_Kd', "%.3f" % (Kd,)) | 
					
						
							| 
									
										
										
										
											2018-03-18 11:23:20 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | TUNE_PID_DELTA = 5.0 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | class ControlAutoTune: | 
					
						
							| 
									
										
										
										
											2018-06-27 14:39:20 -04:00
										 |  |  |     def __init__(self, heater, target): | 
					
						
							| 
									
										
										
										
											2018-03-18 11:23:20 -04:00
										 |  |  |         self.heater = heater | 
					
						
							| 
									
										
										
										
											2018-06-27 14:32:10 -04:00
										 |  |  |         self.heater_max_power = heater.get_max_power() | 
					
						
							| 
									
										
										
										
											2018-06-27 14:39:20 -04:00
										 |  |  |         self.calibrate_temp = target | 
					
						
							| 
									
										
										
										
											2018-03-18 11:23:20 -04:00
										 |  |  |         # Heating control | 
					
						
							|  |  |  |         self.heating = False | 
					
						
							|  |  |  |         self.peak = 0. | 
					
						
							|  |  |  |         self.peak_time = 0. | 
					
						
							|  |  |  |         # Peak recording | 
					
						
							|  |  |  |         self.peaks = [] | 
					
						
							|  |  |  |         # Sample recording | 
					
						
							|  |  |  |         self.last_pwm = 0. | 
					
						
							|  |  |  |         self.pwm_samples = [] | 
					
						
							|  |  |  |         self.temp_samples = [] | 
					
						
							|  |  |  |     # Heater control | 
					
						
							|  |  |  |     def set_pwm(self, read_time, value): | 
					
						
							|  |  |  |         if value != self.last_pwm: | 
					
						
							| 
									
										
										
										
											2018-06-27 14:32:10 -04:00
										 |  |  |             self.pwm_samples.append( | 
					
						
							|  |  |  |                 (read_time + self.heater.get_pwm_delay(), value)) | 
					
						
							| 
									
										
										
										
											2018-03-18 11:23:20 -04:00
										 |  |  |             self.last_pwm = value | 
					
						
							|  |  |  |         self.heater.set_pwm(read_time, value) | 
					
						
							| 
									
										
										
										
											2018-06-27 14:32:10 -04:00
										 |  |  |     def temperature_update(self, read_time, temp, target_temp): | 
					
						
							| 
									
										
										
										
											2018-03-18 11:23:20 -04:00
										 |  |  |         self.temp_samples.append((read_time, temp)) | 
					
						
							| 
									
										
										
										
											2018-10-19 21:30:15 -04:00
										 |  |  |         # Check if the temperature has crossed the target and | 
					
						
							|  |  |  |         # enable/disable the heater if so. | 
					
						
							| 
									
										
										
										
											2018-06-27 14:32:10 -04:00
										 |  |  |         if self.heating and temp >= target_temp: | 
					
						
							| 
									
										
										
										
											2018-03-18 11:23:20 -04:00
										 |  |  |             self.heating = False | 
					
						
							|  |  |  |             self.check_peaks() | 
					
						
							| 
									
										
										
										
											2018-06-27 14:39:20 -04:00
										 |  |  |             self.heater.alter_target(self.calibrate_temp - TUNE_PID_DELTA) | 
					
						
							|  |  |  |         elif not self.heating and temp <= target_temp: | 
					
						
							| 
									
										
										
										
											2018-03-18 11:23:20 -04:00
										 |  |  |             self.heating = True | 
					
						
							|  |  |  |             self.check_peaks() | 
					
						
							| 
									
										
										
										
											2018-06-27 14:39:20 -04:00
										 |  |  |             self.heater.alter_target(self.calibrate_temp) | 
					
						
							| 
									
										
										
										
											2018-10-19 21:30:15 -04:00
										 |  |  |         # Check if this temperature is a peak and record it if so | 
					
						
							| 
									
										
										
										
											2018-03-18 11:23:20 -04:00
										 |  |  |         if self.heating: | 
					
						
							| 
									
										
										
										
											2018-06-27 14:32:10 -04:00
										 |  |  |             self.set_pwm(read_time, self.heater_max_power) | 
					
						
							| 
									
										
										
										
											2018-03-18 11:23:20 -04:00
										 |  |  |             if temp < self.peak: | 
					
						
							|  |  |  |                 self.peak = temp | 
					
						
							|  |  |  |                 self.peak_time = read_time | 
					
						
							|  |  |  |         else: | 
					
						
							|  |  |  |             self.set_pwm(read_time, 0.) | 
					
						
							|  |  |  |             if temp > self.peak: | 
					
						
							|  |  |  |                 self.peak = temp | 
					
						
							|  |  |  |                 self.peak_time = read_time | 
					
						
							| 
									
										
										
										
											2018-07-06 13:27:05 -04:00
										 |  |  |     def check_busy(self, eventtime, smoothed_temp, target_temp): | 
					
						
							| 
									
										
										
										
											2018-03-18 11:23:20 -04:00
										 |  |  |         if self.heating or len(self.peaks) < 12: | 
					
						
							|  |  |  |             return True | 
					
						
							|  |  |  |         return False | 
					
						
							|  |  |  |     # Analysis | 
					
						
							|  |  |  |     def check_peaks(self): | 
					
						
							|  |  |  |         self.peaks.append((self.peak, self.peak_time)) | 
					
						
							|  |  |  |         if self.heating: | 
					
						
							|  |  |  |             self.peak = 9999999. | 
					
						
							|  |  |  |         else: | 
					
						
							|  |  |  |             self.peak = -9999999. | 
					
						
							|  |  |  |         if len(self.peaks) < 4: | 
					
						
							|  |  |  |             return | 
					
						
							|  |  |  |         self.calc_pid(len(self.peaks)-1) | 
					
						
							|  |  |  |     def calc_pid(self, pos): | 
					
						
							|  |  |  |         temp_diff = self.peaks[pos][0] - self.peaks[pos-1][0] | 
					
						
							|  |  |  |         time_diff = self.peaks[pos][1] - self.peaks[pos-2][1] | 
					
						
							| 
									
										
										
										
											2018-10-19 21:30:15 -04:00
										 |  |  |         # Use Astrom-Hagglund method to estimate Ku and Tu | 
					
						
							|  |  |  |         amplitude = .5 * abs(temp_diff) | 
					
						
							|  |  |  |         Ku = 4. * self.heater_max_power / (math.pi * amplitude) | 
					
						
							| 
									
										
										
										
											2018-03-18 11:23:20 -04:00
										 |  |  |         Tu = time_diff | 
					
						
							| 
									
										
										
										
											2018-10-19 21:30:15 -04:00
										 |  |  |         # Use Ziegler-Nichols method to generate PID parameters | 
					
						
							| 
									
										
										
										
											2018-03-18 11:23:20 -04:00
										 |  |  |         Ti = 0.5 * Tu | 
					
						
							|  |  |  |         Td = 0.125 * Tu | 
					
						
							| 
									
										
										
										
											2020-04-25 14:49:50 -04:00
										 |  |  |         Kp = 0.6 * Ku * heaters.PID_PARAM_BASE | 
					
						
							| 
									
										
										
										
											2018-03-18 11:23:20 -04:00
										 |  |  |         Ki = Kp / Ti | 
					
						
							|  |  |  |         Kd = Kp * Td | 
					
						
							|  |  |  |         logging.info("Autotune: raw=%f/%f Ku=%f Tu=%f  Kp=%f Ki=%f Kd=%f", | 
					
						
							| 
									
										
										
										
											2018-06-27 14:32:10 -04:00
										 |  |  |                      temp_diff, self.heater_max_power, Ku, Tu, Kp, Ki, Kd) | 
					
						
							| 
									
										
										
										
											2018-03-18 11:23:20 -04:00
										 |  |  |         return Kp, Ki, Kd | 
					
						
							|  |  |  |     def calc_final_pid(self): | 
					
						
							|  |  |  |         cycle_times = [(self.peaks[pos][1] - self.peaks[pos-2][1], pos) | 
					
						
							|  |  |  |                        for pos in range(4, len(self.peaks))] | 
					
						
							|  |  |  |         midpoint_pos = sorted(cycle_times)[len(cycle_times)/2][1] | 
					
						
							|  |  |  |         return self.calc_pid(midpoint_pos) | 
					
						
							|  |  |  |     # Offline analysis helper | 
					
						
							|  |  |  |     def write_file(self, filename): | 
					
						
							|  |  |  |         pwm = ["pwm: %.3f %.3f" % (time, value) | 
					
						
							|  |  |  |                for time, value in self.pwm_samples] | 
					
						
							|  |  |  |         out = ["%.3f %.3f" % (time, temp) for time, temp in self.temp_samples] | 
					
						
							|  |  |  |         f = open(filename, "wb") | 
					
						
							|  |  |  |         f.write('\n'.join(pwm + out)) | 
					
						
							|  |  |  |         f.close() | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | def load_config(config): | 
					
						
							|  |  |  |     return PIDCalibrate(config) |