forked from einride/pid-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
controller_test.go
74 lines (69 loc) · 1.87 KB
/
controller_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
package pid
import (
"testing"
"time"
"gotest.tools/v3/assert"
)
func TestSpeedControl_ControlLoop_OutputIncrease(t *testing.T) {
// Given a pidControl with reference value and update interval, dt
pidControl := Controller{
Config: ControllerConfig{
ProportionalGain: 2.0,
IntegralGain: 1.0,
DerivativeGain: 1.0,
},
}
// Check output value when output increase is needed
pidControl.Update(ControllerInput{
ReferenceSignal: 10,
ActualSignal: 0,
SamplingInterval: 100 * time.Millisecond,
})
assert.Equal(t, float64(121), pidControl.State.ControlSignal)
assert.Equal(t, float64(10), pidControl.State.ControlError)
}
func TestSpeedControl_ControlLoop_OutputDecrease(t *testing.T) {
// Given a pidControl with reference output and update interval, dt
pidControl := Controller{
Config: ControllerConfig{
ProportionalGain: 2.0,
IntegralGain: 1.0,
DerivativeGain: 1.0,
},
}
// Check output value when output value decrease is needed
pidControl.Update(ControllerInput{
ReferenceSignal: 10,
ActualSignal: 10,
SamplingInterval: 100 * time.Millisecond,
})
assert.Equal(t, float64(0), pidControl.State.ControlSignal)
assert.Equal(t, float64(0), pidControl.State.ControlError)
}
func TestSimpleController_Reset(t *testing.T) {
// Given a Controller with stored values not equal to 0
c := &Controller{
Config: ControllerConfig{
ProportionalGain: 2.0,
IntegralGain: 1.0,
DerivativeGain: 1.0,
},
State: ControllerState{
ControlErrorIntegral: 10,
ControlErrorDerivative: 10,
ControlError: 10,
},
}
// And a duplicate Controller with empty values
expectedController := &Controller{
Config: ControllerConfig{
ProportionalGain: 2.0,
IntegralGain: 1.0,
DerivativeGain: 1.0,
},
}
// When resetting stored values
c.Reset()
// Then
assert.Equal(t, expectedController.State, c.State)
}