|
1 | 1 | package pid
|
2 | 2 |
|
3 | 3 | import (
|
| 4 | + "math" |
4 | 5 | "testing"
|
5 | 6 | "time"
|
6 | 7 |
|
@@ -78,6 +79,59 @@ func TestTrackingController_PControllerUpdate(t *testing.T) {
|
78 | 79 | }
|
79 | 80 | }
|
80 | 81 |
|
| 82 | +func TestTrackingController_NaN(t *testing.T) { |
| 83 | + // Given a saturated I controller with a low AntiWindUpGain |
| 84 | + c := &TrackingController{ |
| 85 | + Config: TrackingControllerConfig{ |
| 86 | + LowPassTimeConstant: 1 * time.Second, |
| 87 | + IntegralGain: 10, |
| 88 | + IntegralDischargeTimeConstant: 10, |
| 89 | + MinOutput: -10, |
| 90 | + MaxOutput: 10, |
| 91 | + AntiWindUpGain: 0.01, |
| 92 | + }, |
| 93 | + } |
| 94 | + for _, tt := range []struct { |
| 95 | + input TrackingControllerInput |
| 96 | + expectedState TrackingControllerState |
| 97 | + }{ |
| 98 | + { |
| 99 | + // Negative faulty measurement |
| 100 | + input: TrackingControllerInput{ |
| 101 | + ReferenceSignal: 5.0, |
| 102 | + ActualSignal: -math.MaxFloat64, |
| 103 | + FeedForwardSignal: 2.0, |
| 104 | + SamplingInterval: dtTest, |
| 105 | + }, |
| 106 | + }, |
| 107 | + { |
| 108 | + // Positive faulty measurement |
| 109 | + input: TrackingControllerInput{ |
| 110 | + ReferenceSignal: 5.0, |
| 111 | + ActualSignal: math.MaxFloat64, |
| 112 | + FeedForwardSignal: 2.0, |
| 113 | + SamplingInterval: dtTest, |
| 114 | + }, |
| 115 | + }, |
| 116 | + } { |
| 117 | + tt := tt |
| 118 | + // When enough iterations have passed |
| 119 | + c.Reset() |
| 120 | + for i := 0; i < 220; i++ { |
| 121 | + c.Update(TrackingControllerInput{ |
| 122 | + ReferenceSignal: tt.input.ReferenceSignal, |
| 123 | + ActualSignal: tt.input.ActualSignal, |
| 124 | + FeedForwardSignal: tt.input.FeedForwardSignal, |
| 125 | + SamplingInterval: tt.input.SamplingInterval, |
| 126 | + }) |
| 127 | + } |
| 128 | + // Then |
| 129 | + assert.Assert(t, !math.IsNaN(c.State.UnsaturatedControlSignal)) |
| 130 | + assert.Assert(t, !math.IsNaN(c.State.ControlSignal)) |
| 131 | + assert.Assert(t, !math.IsNaN(c.State.ControlErrorIntegral)) |
| 132 | + } |
| 133 | +} |
| 134 | + |
81 | 135 | func TestTrackingController_Reset(t *testing.T) {
|
82 | 136 | // Given a SaturatedPIDController with stored values not equal to 0
|
83 | 137 | c := &TrackingController{}
|
|
0 commit comments