diff options
Diffstat (limited to 'klippy/extras/pid_calibrate.py')
-rw-r--r-- | klippy/extras/pid_calibrate.py | 97 |
1 files changed, 60 insertions, 37 deletions
diff --git a/klippy/extras/pid_calibrate.py b/klippy/extras/pid_calibrate.py index 20641167..a29f0b71 100644 --- a/klippy/extras/pid_calibrate.py +++ b/klippy/extras/pid_calibrate.py @@ -6,23 +6,27 @@ import math, logging from . import heaters + class PIDCalibrate: def __init__(self, config): self.printer = config.get_printer() - gcode = self.printer.lookup_object('gcode') - gcode.register_command('PID_CALIBRATE', self.cmd_PID_CALIBRATE, - desc=self.cmd_PID_CALIBRATE_help) + gcode = self.printer.lookup_object("gcode") + 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, gcmd): - heater_name = gcmd.get('HEATER') - target = gcmd.get_float('TARGET') - write_file = gcmd.get_int('WRITE_FILE', 0) - pheaters = self.printer.lookup_object('heaters') + heater_name = gcmd.get("HEATER") + target = gcmd.get_float("TARGET") + write_file = gcmd.get_int("WRITE_FILE", 0) + pheaters = self.printer.lookup_object("heaters") try: heater = pheaters.lookup_heater(heater_name) except self.printer.config_error as e: raise gcmd.error(str(e)) - self.printer.lookup_object('toolhead').get_last_move_time() + self.printer.lookup_object("toolhead").get_last_move_time() calibrate = ControlAutoTune(heater, target) old_control = heater.set_control(calibrate) try: @@ -32,8 +36,8 @@ class PIDCalibrate: raise heater.set_control(old_control) if write_file: - calibrate.write_file('/tmp/heattest.txt') - if calibrate.check_busy(0., 0., 0.): + calibrate.write_file("/tmp/heattest.txt") + if calibrate.check_busy(0.0, 0.0, 0.0): raise gcmd.error("pid_calibrate interrupted") # Log and report results Kp, Ki, Kd = calibrate.calc_final_pid() @@ -41,17 +45,20 @@ class PIDCalibrate: gcmd.respond_info( "PID parameters: pid_Kp=%.3f pid_Ki=%.3f pid_Kd=%.3f\n" "The SAVE_CONFIG command will update the printer config file\n" - "with these parameters and restart the printer." % (Kp, Ki, Kd)) + "with these parameters and restart the printer." % (Kp, Ki, Kd) + ) # Store results for SAVE_CONFIG cfgname = heater.get_name() - configfile = self.printer.lookup_object('configfile') - configfile.set(cfgname, 'control', 'pid') - configfile.set(cfgname, 'pid_Kp', "%.3f" % (Kp,)) - configfile.set(cfgname, 'pid_Ki', "%.3f" % (Ki,)) - configfile.set(cfgname, 'pid_Kd', "%.3f" % (Kd,)) + configfile = self.printer.lookup_object("configfile") + configfile.set(cfgname, "control", "pid") + configfile.set(cfgname, "pid_Kp", "%.3f" % (Kp,)) + configfile.set(cfgname, "pid_Ki", "%.3f" % (Ki,)) + configfile.set(cfgname, "pid_Kd", "%.3f" % (Kd,)) + TUNE_PID_DELTA = 5.0 + class ControlAutoTune: def __init__(self, heater, target): self.heater = heater @@ -59,21 +66,22 @@ class ControlAutoTune: self.calibrate_temp = target # Heating control self.heating = False - self.peak = 0. - self.peak_time = 0. + self.peak = 0.0 + self.peak_time = 0.0 # Peak recording self.peaks = [] # Sample recording - self.last_pwm = 0. + self.last_pwm = 0.0 self.pwm_samples = [] self.temp_samples = [] + # Heater control def set_pwm(self, read_time, value): if value != self.last_pwm: - self.pwm_samples.append( - (read_time + self.heater.get_pwm_delay(), value)) + self.pwm_samples.append((read_time + self.heater.get_pwm_delay(), value)) self.last_pwm = value self.heater.set_pwm(read_time, value) + def temperature_update(self, read_time, temp, target_temp): self.temp_samples.append((read_time, temp)) # Check if the temperature has crossed the target and @@ -93,30 +101,33 @@ class ControlAutoTune: self.peak = temp self.peak_time = read_time else: - self.set_pwm(read_time, 0.) + self.set_pwm(read_time, 0.0) if temp > self.peak: self.peak = temp self.peak_time = read_time + def check_busy(self, eventtime, smoothed_temp, target_temp): 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. + self.peak = 9999999.0 else: - self.peak = -9999999. + self.peak = -9999999.0 if len(self.peaks) < 4: return - self.calc_pid(len(self.peaks)-1) + 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] + temp_diff = self.peaks[pos][0] - self.peaks[pos - 1][0] + time_diff = self.peaks[pos][1] - self.peaks[pos - 2][1] # Use Astrom-Hagglund method to estimate Ku and Tu - amplitude = .5 * abs(temp_diff) - Ku = 4. * self.heater_max_power / (math.pi * amplitude) + amplitude = 0.5 * abs(temp_diff) + Ku = 4.0 * self.heater_max_power / (math.pi * amplitude) Tu = time_diff # Use Ziegler-Nichols method to generate PID parameters Ti = 0.5 * Tu @@ -124,22 +135,34 @@ class ControlAutoTune: Kp = 0.6 * Ku * heaters.PID_PARAM_BASE Ki = Kp / Ti Kd = Kp * Td - logging.info("Autotune: raw=%f/%f Ku=%f Tu=%f Kp=%f Ki=%f Kd=%f", - temp_diff, self.heater_max_power, Ku, Tu, Kp, Ki, Kd) + logging.info( + "Autotune: raw=%f/%f Ku=%f Tu=%f Kp=%f Ki=%f Kd=%f", + temp_diff, + self.heater_max_power, + Ku, + Tu, + Kp, + Ki, + Kd, + ) 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] + 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] + 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, "w") - f.write('\n'.join(pwm + out)) + f.write("\n".join(pwm + out)) f.close() + def load_config(config): return PIDCalibrate(config) |