Skip to content

Commit

Permalink
feat!: clean up Controller API
Browse files Browse the repository at this point in the history
Remove return value from Controller.Update to align with the API of the
other controllers.
  • Loading branch information
odsod committed Dec 22, 2020
1 parent c2ec3fe commit 56f8a85
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 19 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ PID controllers for Go.

### `pid.Controller`

A standard PID controller.
A basic PID controller.

```go
import (
Expand Down
26 changes: 14 additions & 12 deletions controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@ import "time"

// Controller implements a basic PID controller.
type Controller struct {
// Config for the Controller.
Config ControllerConfig
State ControllerState
// State of the Controller.
State ControllerState
}

// ControllerConfig contains configurable parameters for a Controller.
Expand Down Expand Up @@ -41,18 +43,18 @@ type ControllerInput struct {
}

// Update the controller state.
func (s *Controller) Update(input ControllerInput) float64 {
previousError := s.State.ControlError
s.State.ControlError = input.ReferenceSignal - input.ActualSignal
s.State.ControlErrorDerivative = (s.State.ControlError - previousError) / input.SamplingInterval.Seconds()
s.State.ControlErrorIntegral += s.State.ControlError * input.SamplingInterval.Seconds()
s.State.ControlSignal = s.Config.ProportionalGain*s.State.ControlError +
s.Config.IntegralGain*s.State.ControlErrorIntegral +
s.Config.DerivativeGain*s.State.ControlErrorDerivative
return s.State.ControlSignal
func (c *Controller) Update(input ControllerInput) {
previousError := c.State.ControlError
c.State.ControlError = input.ReferenceSignal - input.ActualSignal
c.State.ControlErrorDerivative = (c.State.ControlError - previousError) / input.SamplingInterval.Seconds()
c.State.ControlErrorIntegral += c.State.ControlError * input.SamplingInterval.Seconds()
c.State.ControlSignal =
c.Config.ProportionalGain*c.State.ControlError +
c.Config.IntegralGain*c.State.ControlErrorIntegral +
c.Config.DerivativeGain*c.State.ControlErrorDerivative
}

// Reset the controller state.
func (s *Controller) Reset() {
s.State = ControllerState{}
func (c *Controller) Reset() {
c.State = ControllerState{}
}
12 changes: 6 additions & 6 deletions controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,12 @@ func TestSpeedControl_ControlLoop_OutputIncrease(t *testing.T) {
},
}
// Check output value when output increase is needed
assert.Equal(t, float64(121), pidControl.Update(ControllerInput{
pidControl.Update(ControllerInput{
ReferenceSignal: 10,
ActualSignal: 0,
SamplingInterval: 100 * time.Millisecond,
}))
// Check proportional error
})
assert.Equal(t, float64(121), pidControl.State.ControlSignal)
assert.Equal(t, float64(10), pidControl.State.ControlError)
}

Expand All @@ -36,12 +36,12 @@ func TestSpeedControl_ControlLoop_OutputDecrease(t *testing.T) {
},
}
// Check output value when output value decrease is needed
assert.Equal(t, float64(0), pidControl.Update(ControllerInput{
pidControl.Update(ControllerInput{
ReferenceSignal: 10,
ActualSignal: 10,
SamplingInterval: 100 * time.Millisecond,
}))
// Check proportional error
})
assert.Equal(t, float64(0), pidControl.State.ControlSignal)
assert.Equal(t, float64(0), pidControl.State.ControlError)
}

Expand Down

0 comments on commit 56f8a85

Please sign in to comment.