Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CF3B5 reverse fan #51

Merged
merged 6 commits into from
Sep 5, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ Features merged into the master branch:

- [fan: Normalising Fan PWM power](https://github.com/DangerKlippers/danger-klipper/pull/44) ([klipper#6307](https://github.com/Klipper3d/klipper/pull/6307))

- [fan: Reverse FAN](https://github.com/DangerKlippers/danger-klipper/pull/51) ([klipper#4983](https://github.com/Klipper3d/klipper/pull/4983))

- [heater: Modify PID without reload](https://github.com/DangerKlippers/danger-klipper/pull/35)

- [heater: Velocity PID](https://github.com/DangerKlippers/danger-klipper/pull/47) ([klipper#6272](https://github.com/Klipper3d/klipper/pull/6272))
Expand All @@ -22,6 +24,7 @@ Features merged into the master branch:

- [probe: Drop the first result](https://github.com/DangerKlippers/danger-klipper/pull/2) ([klipper#3397](https://github.com/Klipper3d/klipper/issues/3397))


"Dangerous Klipper for dangerous users"

[![Klipper](docs/img/klipper-logo-small.png)](https://DangerKlippers.github.io/danger-klipper/)
Expand Down
5 changes: 5 additions & 0 deletions docs/Config_Reference.md
Original file line number Diff line number Diff line change
Expand Up @@ -2892,6 +2892,11 @@ information.
#gcode_id:
# If set, the temperature will be reported in M105 queries using the
# given id. The default is to not report the temperature via M105.
#reverse: False
# If true, the working mode of the fan is reversed. If the temperature
# is lower than the target temperature, the fan speed increases;
# otherwise, the fan speed decreases.
# The default is False.
```

### [fan_generic]
Expand Down
42 changes: 28 additions & 14 deletions klippy/extras/temperature_fan.py
Original file line number Diff line number Diff line change
Expand Up @@ -139,15 +139,22 @@ def set_max_speed(self, speed):
class ControlBangBang:
def __init__(self, temperature_fan, config):
self.temperature_fan = temperature_fan
self.reverse = config.getboolean("reverse", False)
self.max_delta = config.getfloat("max_delta", 2.0, above=0.0)
self.heating = False

def temperature_callback(self, read_time, temp):
current_temp, target_temp = self.temperature_fan.get_temp(read_time)
if self.heating and temp >= target_temp + self.max_delta:
self.heating = False
elif not self.heating and temp <= target_temp - self.max_delta:
self.heating = True
if (
self.heating != self.reverse
and temp >= target_temp + self.max_delta
):
self.heating = self.reverse
elif (
not self.heating == self.reverse
and temp <= target_temp - self.max_delta
):
self.heating = not self.reverse
if self.heating:
self.temperature_fan.set_speed(read_time, 0.0)
else:
Expand All @@ -167,13 +174,15 @@ def temperature_callback(self, read_time, temp):
class ControlPID:
def __init__(self, temperature_fan, config):
self.temperature_fan = temperature_fan
self.reverse = config.getboolean("reverse", False)
self.Kp = config.getfloat("pid_Kp") / PID_PARAM_BASE
self.Ki = config.getfloat("pid_Ki") / PID_PARAM_BASE
self.Kd = config.getfloat("pid_Kd") / PID_PARAM_BASE
self.min_deriv_time = config.getfloat("pid_deriv_time", 2.0, above=0.0)
self.temp_integ_max = 0.0
if self.Ki:
self.temp_integ_max = self.temperature_fan.get_max_speed() / self.Ki
imax = config.getfloat(
"pid_integral_max", self.temperature_fan.get_max_speed(), minval=0.0
)
self.temp_integ_max = imax / self.Ki
self.prev_temp = AMBIENT_TEMP
self.prev_temp_time = 0.0
self.prev_temp_deriv = 0.0
Expand All @@ -198,13 +207,18 @@ def temperature_callback(self, read_time, temp):
# Calculate output
co = self.Kp * temp_err + self.Ki * temp_integ - self.Kd * temp_deriv
bounded_co = max(0.0, min(self.temperature_fan.get_max_speed(), co))
self.temperature_fan.set_speed(
read_time,
max(
self.temperature_fan.get_min_speed(),
self.temperature_fan.get_max_speed() - bounded_co,
),
)
if not self.reverse:
self.temperature_fan.set_speed(
read_time,
max(
self.temperature_fan.get_min_speed(),
self.temperature_fan.get_max_speed() - bounded_co,
),
)
else:
self.temperature_fan.set_speed(
read_time, max(self.temperature_fan.get_min_speed(), bounded_co)
)
# Store state for next measurement
self.prev_temp = temp
self.prev_temp_time = read_time
Expand Down