Skip to content

Commit

Permalink
fixed ruff
Browse files Browse the repository at this point in the history
  • Loading branch information
Zeanon committed Nov 25, 2024
1 parent 3c4d97c commit 2551506
Showing 1 changed file with 86 additions and 41 deletions.
127 changes: 86 additions & 41 deletions klippy/extras/heaters.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,14 +57,18 @@ def __init__(self, config, sensor):
minval=self.min_temp,
maxval=self.max_temp,
)
is_fileoutput = self.printer.get_start_args().get("debugoutput") is not None
is_fileoutput = (
self.printer.get_start_args().get("debugoutput") is not None
)
self.can_extrude = self.min_extrude_temp <= 0.0 or is_fileoutput
self.disable_if_connected = []
if hasattr(config, "getlist"):
self.disable_if_connected = config.getlist(
"disable_if_connected", self.disable_if_connected
)
self.max_power = config.getfloat("max_power", 1.0, above=0.0, maxval=1.0)
self.max_power = config.getfloat(
"max_power", 1.0, above=0.0, maxval=1.0
)
self.config_smooth_time = config.getfloat("smooth_time", 1.0, above=0.0)
self.smooth_time = self.config_smooth_time
self.inv_smooth_time = 1.0 / self.smooth_time
Expand Down Expand Up @@ -95,7 +99,9 @@ def __init__(self, config, sensor):
self.printer.load_object(config, "pid_calibrate")
self.gcode = self.printer.lookup_object("gcode")
self.pmgr = self.ProfileManager(self)
self.control = self.lookup_control(self.pmgr.init_default_profile(), True)
self.control = self.lookup_control(
self.pmgr.init_default_profile(), True
)
self.gcode.register_mux_command(
"SET_HEATER_TEMPERATURE",
"HEATER",
Expand Down Expand Up @@ -125,7 +131,9 @@ def __init__(self, config, sensor):
desc=self.cmd_SET_HEATER_PID_help,
)

self.printer.register_event_handler("klippy:shutdown", self._handle_shutdown)
self.printer.register_event_handler(
"klippy:shutdown", self._handle_shutdown
)

def notify_disabled(self, mcu_name):
raise self.printer.command_error(
Expand Down Expand Up @@ -203,7 +211,9 @@ def set_temp(self, degrees):
self.target_temp = degrees

def get_temp(self, eventtime):
print_time = self.mcu_pwm.get_mcu().estimated_print_time(eventtime) - 5.0
print_time = (
self.mcu_pwm.get_mcu().estimated_print_time(eventtime) - 5.0
)
with self.lock:
if self.last_temp_time < print_time:
return 0.0, self.target_temp
Expand Down Expand Up @@ -312,7 +322,9 @@ def __init__(self, outer_instance):
"pid_profile %s" % self.outer_instance.short_name
)
for profile in stored_profs:
self._init_profile(profile, profile.get_name().split(" ", 2)[-1])
self._init_profile(
profile, profile.get_name().split(" ", 2)[-1]
)

def _init_profile(self, config_section, name):
version = config_section.getint("pid_version", 1)
Expand All @@ -326,7 +338,9 @@ def _init_profile(self, config_section, name):
self.incompatible_profiles.append(name)
return None
temp_profile = {}
control = self._check_value_config("control", config_section, str, False)
control = self._check_value_config(
"control", config_section, str, False
)
if control == "watermark":
temp_profile["max_delta"] = config_section.getfloat(
"max_delta", 2.0, above=0.0
Expand Down Expand Up @@ -362,8 +376,10 @@ def _init_profile(self, config_section, name):
temp_profile["filament_density"] = config_section.getfloat(
"filament_density", above=0.0, default=1.2
)
temp_profile["filament_heat_capacity"] = config_section.getfloat(
"filament_heat_capacity", above=0.0, default=1.8
temp_profile["filament_heat_capacity"] = (
config_section.getfloat(
"filament_heat_capacity", above=0.0, default=1.8
)
)
temp_profile["maximum_retract"] = config_section.getfloat(
"maximum_retract", above=0.0, default=2.0
Expand All @@ -387,7 +403,9 @@ def _init_profile(self, config_section, name):
filament_temp_src = (FILAMENT_TEMP_SRC_FIXED, value)
temp_profile["filament_temp_src"] = filament_temp_src

ambient_sensor_name = config_section.get("ambient_temp_sensor", None)
ambient_sensor_name = config_section.get(
"ambient_temp_sensor", None
)
ambient_sensor = None
if ambient_sensor_name is not None:
ambient_sensor = config_section.get_printer().load_object(
Expand All @@ -396,8 +414,10 @@ def _init_profile(self, config_section, name):
None,
)
if ambient_sensor is None:
ambient_sensor = config_section.get_printer().lookup_object(
ambient_sensor_name, None
ambient_sensor = (
config_section.get_printer().lookup_object(
ambient_sensor_name, None
)
)
if ambient_sensor is None:
raise config_section.error(
Expand Down Expand Up @@ -430,8 +450,8 @@ def _init_profile(self, config_section, name):
fan = fan_obj.fan
temp_profile["cooling_fan"] = fan

temp_profile["fan_ambient_transfer"] = config_section.getfloatlist(
"fan_ambient_transfer", []
temp_profile["fan_ambient_transfer"] = (
config_section.getfloatlist("fan_ambient_transfer", [])
)
elif control == "pid" or control == "pid_v":
for key, (type, placeholder) in PID_PROFILE_OPTIONS.items():
Expand Down Expand Up @@ -478,7 +498,10 @@ def _compute_section_name(self, profile_name):
self.outer_instance.short_name
if profile_name == "default"
else (
"pid_profile " + self.outer_instance.short_name + " " + profile_name
"pid_profile "
+ self.outer_instance.short_name
+ " "
+ profile_name
)
)

Expand All @@ -493,9 +516,13 @@ def _check_value_gcmd(
maxval=None,
):
if type is int:
value = gcmd.get_int(name, default, minval=minval, maxval=maxval)
value = gcmd.get_int(
name, default, minval=minval, maxval=maxval
)
elif type is float:
value = gcmd.get_float(name, default, minval=minval, maxval=maxval)
value = gcmd.get_float(
name, default, minval=minval, maxval=maxval
)
else:
value = gcmd.get(name, default)
if not can_be_none and value is None:
Expand Down Expand Up @@ -523,7 +550,9 @@ def set_values(self, profile_name, gcmd, verbose):
kp = self._check_value_gcmd("KP", None, gcmd, float, False)
ki = self._check_value_gcmd("KI", None, gcmd, float, False)
kd = self._check_value_gcmd("KD", None, gcmd, float, False)
smooth_time = self._check_value_gcmd("SMOOTH_TIME", None, gcmd, float, True)
smooth_time = self._check_value_gcmd(
"SMOOTH_TIME", None, gcmd, float, True
)
keep_target = self._check_value_gcmd(
"KEEP_TARGET", 0, gcmd, int, True, minval=0, maxval=1
)
Expand All @@ -539,7 +568,9 @@ def set_values(self, profile_name, gcmd, verbose):
"pid_ki": ki,
"pid_kd": kd,
}
temp_control = self.outer_instance.lookup_control(temp_profile, load_clean)
temp_control = self.outer_instance.lookup_control(
temp_profile, load_clean
)
self.outer_instance.set_control(temp_control, keep_target)
msg = (
"PID Parameters:\n"
Expand Down Expand Up @@ -577,7 +608,8 @@ def get_values(self, profile_name, gcmd, verbose):
"Control: %s\n"
"Smooth Time: %.3f\n"
"pid_Kp=%.3f pid_Ki=%.3f pid_Kd=%.3f\n"
"name: %s" % (target, tolerance, control, smooth_time, kp, ki, kd, name)
"name: %s"
% (target, tolerance, control, smooth_time, kp, ki, kd, name)
)

def save_profile(self, profile_name=None, gcmd=None, verbose=True):
Expand Down Expand Up @@ -606,12 +638,15 @@ def save_profile(self, profile_name=None, gcmd=None, verbose=True):
)

def load_profile(self, profile_name, gcmd, verbose):
verbose = self._check_value_gcmd("VERBOSE", "low", gcmd, "lower", True)
verbose = self._check_value_gcmd(
"VERBOSE", "low", gcmd, "lower", True
)
load_clean = self._check_value_gcmd(
"LOAD_CLEAN", 0, gcmd, int, True, minval=0, maxval=1
)
if (
profile_name == self.outer_instance.get_control().get_profile()["name"]
profile_name
== self.outer_instance.get_control().get_profile()["name"]
and not load_clean
):
if verbose == "high" or verbose == "low":
Expand Down Expand Up @@ -662,22 +697,20 @@ def load_profile(self, profile_name, gcmd, verbose):
if profile["smooth_time"] is None
else profile["smooth_time"]
)
msg = (
"Target: %.2f\n"
"Tolerance: %.4f\n"
"Control: %s\n"
% (
profile["pid_target"],
profile["pid_tolerance"],
profile["control"],
)
msg = "Target: %.2f\n" "Tolerance: %.4f\n" "Control: %s\n" % (
profile["pid_target"],
profile["pid_tolerance"],
profile["control"],
)
if smooth_time is not None:
msg += "Smooth Time: %.3f\n" % smooth_time
msg += "PID Parameters: pid_Kp=%.3f pid_Ki=%.3f pid_Kd=%.3f\n" % (
profile["pid_kp"],
profile["pid_ki"],
profile["pid_kd"],
msg += (
"PID Parameters: pid_Kp=%.3f pid_Ki=%.3f pid_Kd=%.3f\n"
% (
profile["pid_kp"],
profile["pid_ki"],
profile["pid_kd"],
)
)
self.outer_instance.gcode.respond_info(msg)

Expand Down Expand Up @@ -804,7 +837,8 @@ def temperature_update(self, read_time, temp, target_temp):
temp_deriv = temp_diff / time_diff
else:
temp_deriv = (
self.prev_temp_deriv * (self.min_deriv_time - time_diff) + temp_diff
self.prev_temp_deriv * (self.min_deriv_time - time_diff)
+ temp_diff
) / self.min_deriv_time
# Calculate accumulated temperature "error"
temp_err = target_temp - temp
Expand Down Expand Up @@ -863,7 +897,9 @@ def __init__(self, profile, heater, load_clean=False):
self.temps = (
([AMBIENT_TEMP] * 3)
if load_clean
else ([self.heater.get_temp(self.heater.reactor.monotonic())[0]] * 3)
else (
[self.heater.get_temp(self.heater.reactor.monotonic())[0]] * 3
)
)
self.times = [0.0] * 3 # temperature reading times
self.d1 = 0.0 # previous smoothed 1st derivative
Expand Down Expand Up @@ -912,7 +948,9 @@ def temperature_update(self, read_time, temp, target_temp):

def check_busy(self, eventtime, smoothed_temp, target_temp):
temp_diff = target_temp - smoothed_temp
return abs(temp_diff) > PID_SETTLE_DELTA or abs(self.d1) > PID_SETTLE_SLOPE
return (
abs(temp_diff) > PID_SETTLE_DELTA or abs(self.d1) > PID_SETTLE_SLOPE
)

def update_smooth_time(self):
self.smooth_time = self.heater.get_smooth_time() # smoothing window
Expand Down Expand Up @@ -993,7 +1031,9 @@ def lookup_heater(self, heater_name):
if " " in heater_name:
heater_name = heater_name.split(" ", 1)[1]
if heater_name not in self.heaters:
raise self.printer.config_error("Unknown heater '%s'" % (heater_name,))
raise self.printer.config_error(
"Unknown heater '%s'" % (heater_name,)
)
return self.heaters[heater_name]

def setup_sensor(self, config):
Expand Down Expand Up @@ -1077,7 +1117,10 @@ def set_temperature(self, heater, temp, wait=False):
if not mcu_name.startswith("mcu"):
mcu_name = "mcu " + mcu_name
mcu_object = self.printer.lookup_object(mcu_name, None)
if mcu_object is not None and not mcu_object.non_critical_disconnected:
if (
mcu_object is not None
and not mcu_object.non_critical_disconnected
):
heater.notify_disabled(mcu_name)
return
toolhead = self.printer.lookup_object("toolhead")
Expand All @@ -1096,7 +1139,9 @@ def cmd_TEMPERATURE_WAIT(self, gcmd):
max_temp = gcmd.get_float("MAXIMUM", float("inf"), above=min_temp)
error_on_cancel = gcmd.get("ALLOW_CANCEL", None) is None
if min_temp == float("-inf") and max_temp == float("inf"):
raise gcmd.error("Error on 'TEMPERATURE_WAIT': missing MINIMUM or MAXIMUM.")
raise gcmd.error(
"Error on 'TEMPERATURE_WAIT': missing MINIMUM or MAXIMUM."
)
if self.printer.get_start_args().get("debugoutput") is not None:
return
if sensor_name in self.heaters:
Expand Down

0 comments on commit 2551506

Please sign in to comment.