Skip to content

Commit

Permalink
Merge pull request #327 from markusressel/bugfix/#326-maxPwmChangePer…
Browse files Browse the repository at this point in the history
…Cycle-keeps-fan-off

reworked DirectControlLoop to fix custom values for "maxPwmChangePerCycle"
  • Loading branch information
markusressel authored Nov 28, 2024
2 parents b0508e0 + 8be8aad commit 087689e
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 14 deletions.
20 changes: 6 additions & 14 deletions internal/control_loop/direct.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,27 +31,19 @@ func NewDirectControlLoop(
func (l *DirectControlLoop) Cycle(target int, current int) int {
loopTime := time.Now()

dt := loopTime.Sub(l.lastTime).Seconds()
//dt := loopTime.Sub(l.lastTime).Seconds()

l.lastTime = loopTime

var stepTarget = float64(target)
if l.maxPwmChangePerCycle != nil {
// the pwm adjustment depends on the direction and
// the time-based change speed limit.
stepTarget = float64(*l.maxPwmChangePerCycle) * dt
maxChangeValue := *l.maxPwmChangePerCycle
// TODO: figure out how to normalize the change to the timedelta
//stepTarget = float64(*l.maxPwmChangePerCycle) * dt

err := float64(target - current)
// 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
if err > 0 {
// below desired speed, add pwms
stepTarget = util.Coerce(stepTarget, 0, err)
} else {
// above or at desired speed, subtract pwms
stepTarget = util.Coerce(-stepTarget, err, 0)
}
clampedErr := util.Coerce(err, -float64(maxChangeValue), +float64(maxChangeValue))
stepTarget = float64(current) + clampedErr
}

// ensure we are within sane bounds
Expand Down
41 changes: 41 additions & 0 deletions internal/control_loop/direct_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package control_loop

import (
"github.com/stretchr/testify/assert"
"testing"
)

func TestSimple(t *testing.T) {
// GIVEN
loop := NewDirectControlLoop(nil)

// WHEN
newTarget := loop.Cycle(10, 0)

// THEN
assert.Equal(t, 10, newTarget)

// WHEN
newTarget = loop.Cycle(10, newTarget)

// THEN
assert.Equal(t, 10, newTarget)
}

func TestMaxChange(t *testing.T) {
// GIVEN
maxChangePerCycle := 2
loop := NewDirectControlLoop(&maxChangePerCycle)

// WHEN
newTarget := loop.Cycle(10, 0)

// THEN
assert.Equal(t, 2, newTarget)

// WHEN
newTarget = loop.Cycle(10, newTarget)

// THEN
assert.Equal(t, 4, newTarget)
}

0 comments on commit 087689e

Please sign in to comment.