Skip to content

Commit

Permalink
refactoring and cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
markusressel committed Sep 23, 2024
1 parent 020acd3 commit c59c80e
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 12 deletions.
7 changes: 5 additions & 2 deletions internal/control_loop/common.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package control_loop

// ControlLoop defines how a FanController approaches the target value of its curve.
type ControlLoop interface {
// Loop advances the control loop
Loop(target float64, measured float64) float64
// Cycle advances the control loop to the next step and returns the new pwm value.
// Note: multiple calls to Loop may not result in the same output, as
// the control loop may take time into account or have other stateful properties.
Cycle(target int, lastSetPwm int) int
}
16 changes: 12 additions & 4 deletions internal/control_loop/direct.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package control_loop

import (
"github.com/markusressel/fan2go/internal/util"
"math"
"time"
)

Expand All @@ -27,7 +28,7 @@ func NewDirectControlLoop(
}
}

func (l *DirectControlLoop) Loop(target float64, measured float64) float64 {
func (l *DirectControlLoop) Cycle(target int, measured int) int {
loopTime := time.Now()

dt := loopTime.Sub(l.lastTime).Seconds()
Expand All @@ -37,15 +38,22 @@ func (l *DirectControlLoop) Loop(target float64, measured float64) float64 {
// the pwm adjustment depends on the direction and
// the time-based change speed limit.
maxPwmChangeThiStep := float64(l.maxPwmChangePerCycle) * dt
err := target - measured
err := float64(target - measured)
// we can be above or below the target pwm value,
// so we substract or add at most the max pwm change,
// capped to having reached the target
var stepTarget float64
if err > 0 {
// below desired speed, add pwms
return util.Coerce(maxPwmChangeThiStep, 0, err)
stepTarget = util.Coerce(maxPwmChangeThiStep, 0, err)
} else {
// above or at desired speed, subtract pwms
return util.Coerce(-maxPwmChangeThiStep, err, 0)
stepTarget = util.Coerce(-maxPwmChangeThiStep, err, 0)
}

// ensure we are within sane bounds
coerced := util.Coerce(stepTarget, 0, 255)
result := int(math.Round(coerced))

return result
}
9 changes: 4 additions & 5 deletions internal/control_loop/pid.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,12 @@ func NewPidControlLoop(
}
}

func (l *PidControlLoop) Loop(target float64, lastSetPwm float64) float64 {
result := l.pidLoop.Loop(target, lastSetPwm)
func (l *PidControlLoop) Cycle(target int, lastSetPwm int) int {
result := l.pidLoop.Loop(float64(target), float64(lastSetPwm))

// TODO: verify this
// ensure we are within sane bounds
coerced := util.Coerce(lastSetPwm+result, 0, 255)
coerced := util.Coerce(float64(lastSetPwm)+result, 0, 255)
stepTarget := int(math.Round(coerced))

return float64(stepTarget)
return stepTarget
}
2 changes: 1 addition & 1 deletion internal/controller/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -248,7 +248,7 @@ func (f *PidFanController) UpdateFanSpeed() error {
target := f.calculateTargetPwm()

// the target pwm, approaching the actual target smoothly
stepTarget := int(f.controlLoop.Loop(float64(target), float64(lastSetPwm)))
stepTarget := f.controlLoop.Cycle(target, lastSetPwm)

if target >= 0 {
_ = trySetManualPwm(f.fan)
Expand Down

0 comments on commit c59c80e

Please sign in to comment.