-
Notifications
You must be signed in to change notification settings - Fork 23
/
controller-component.go
189 lines (164 loc) · 5.8 KB
/
controller-component.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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
package controller
import (
"math"
"github.com/fluxninja/aperture/v2/pkg/config"
"github.com/fluxninja/aperture/v2/pkg/notifiers"
"github.com/fluxninja/aperture/v2/pkg/policies/controlplane/iface"
"github.com/fluxninja/aperture/v2/pkg/policies/controlplane/runtime"
)
// ControllerComponent provides a runtime.Component for Controllers. It can be initialized with a Controller implementation. It exposes a ControllerStateReadAPI for Controllers.
type ControllerComponent struct {
controller Controller
// Signal's last reading
signal runtime.Reading
// Setpoint's last reading
setpoint runtime.Reading
// Control variable's last reading
controlVariable runtime.Reading
// Controller output's last reading
output runtime.Reading
policyReadAPI iface.Policy
dynamicConfigKey string
componentName string
shortDescription string
componentID string
defaultManualMode bool
manualMode bool
}
// NewControllerComponent creates a new ControllerComponent.
func NewControllerComponent(
controller Controller,
componentName string,
shortDescription string,
componentID string,
policyReadAPI iface.Policy,
dynamicConfigKey string,
manualMode bool,
) *ControllerComponent {
cc := &ControllerComponent{
signal: runtime.InvalidReading(),
setpoint: runtime.InvalidReading(),
controlVariable: runtime.InvalidReading(),
output: runtime.InvalidReading(),
controller: controller,
componentName: componentName,
shortDescription: shortDescription,
componentID: componentID,
policyReadAPI: policyReadAPI,
dynamicConfigKey: dynamicConfigKey,
defaultManualMode: manualMode,
manualMode: manualMode,
}
return cc
}
// Name implements runtime.Component.
func (cc *ControllerComponent) Name() string { return cc.componentName }
// Type implements runtime.Component.
func (cc *ControllerComponent) Type() runtime.ComponentType {
return runtime.ComponentTypeSignalProcessor
}
// ShortDescription implements runtime.Component.
func (cc *ControllerComponent) ShortDescription() string {
return cc.shortDescription
}
// IsActuator implements runtime.Component.
func (*ControllerComponent) IsActuator() bool { return false }
// Execute implements runtime.Component.Execute.
func (cc *ControllerComponent) Execute(inPortReadings runtime.PortToReading, circuitAPI runtime.CircuitAPI) (outPortReadings runtime.PortToReading, err error) {
retErr := func(err error) (runtime.PortToReading, error) {
return runtime.PortToReading{
"output": []runtime.Reading{runtime.InvalidReading()},
}, err
}
tickInfo := circuitAPI.GetTickInfo()
signal := inPortReadings.ReadSingleReadingPort("signal")
setpoint := inPortReadings.ReadSingleReadingPort("setpoint")
optimize := inPortReadings.ReadSingleReadingPort("optimize")
max := inPortReadings.ReadSingleReadingPort("max")
min := inPortReadings.ReadSingleReadingPort("min")
controlVariable := inPortReadings.ReadSingleReadingPort("control_variable")
output := runtime.InvalidReading()
prevSetpoint := cc.setpoint
// Save readings for the current tick so that Controller may access them via ControllerStateReadAPI
cc.signal = signal
cc.setpoint = setpoint
cc.controlVariable = controlVariable
if signal.Valid() && setpoint.Valid() {
// ComputeOutput
computedOutput, err := cc.controller.ComputeOutput(signal, setpoint, controlVariable, cc, tickInfo)
if err != nil {
return retErr(err)
}
output = computedOutput
}
// Check if the setpoint has changed
if setpoint.Valid() && setpoint.Value() != prevSetpoint.Value() {
// Try to maintain output
err := cc.controller.MaintainOutput(prevSetpoint, setpoint, cc, tickInfo)
if err != nil {
return retErr(err)
}
}
// Optimize
if output.Valid() && optimize.Valid() {
targetOutput := runtime.NewReading(output.Value() + optimize.Value())
// Wind output
windedOutput, err := cc.controller.WindOutput(output, targetOutput, cc, tickInfo)
output = windedOutput
if err != nil {
return retErr(err)
}
}
if output.Valid() {
outputReading := output
// Constrain output
if max.Valid() {
outputReading = runtime.NewReading(math.Min(output.Value(), max.Value()))
}
if min.Valid() {
outputReading = runtime.NewReading(math.Max(outputReading.Value(), min.Value()))
}
if outputReading.Value() != output.Value() {
// Wind output
windedOutput, err := cc.controller.WindOutput(output, outputReading, cc, tickInfo)
output = windedOutput
if err != nil {
return retErr(err)
}
}
}
// Set output to control variable in-case of Manual mode
if cc.manualMode {
// wind the controller output to the control variable
windedOutput, err := cc.controller.WindOutput(output, controlVariable, cc, tickInfo)
if err != nil {
return retErr(err)
}
output = windedOutput
}
// Save readings for the next tick so that Controller may access them via ControllerStateReadAPI
cc.output = output
return runtime.PortToReading{
"output": []runtime.Reading{output},
}, nil
}
// DynamicConfigUpdate handles setting of controller.ControllerMode.
func (cc *ControllerComponent) DynamicConfigUpdate(event notifiers.Event, unmarshaller config.Unmarshaller) {
cc.manualMode = config.GetBoolValue(unmarshaller, cc.dynamicConfigKey, cc.defaultManualMode)
}
// GetSignal returns the signal's last reading.
func (cc *ControllerComponent) GetSignal() runtime.Reading {
return cc.signal
}
// GetSetpoint returns the setpoint's last reading.
func (cc *ControllerComponent) GetSetpoint() runtime.Reading {
return cc.setpoint
}
// GetControlVariable returns the control variable's last reading.
func (cc *ControllerComponent) GetControlVariable() runtime.Reading {
return cc.controlVariable
}
// GetControllerOutput returns the controller output's last reading.
func (cc *ControllerComponent) GetControllerOutput() runtime.Reading {
return cc.output
}