-
Notifications
You must be signed in to change notification settings - Fork 48
/
feedback.py
311 lines (228 loc) · 7.5 KB
/
feedback.py
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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
import math
import random
DT = None # Sampling interval - defaults to None, must be set explicitly
# ============================================================
# Components
class Component:
def work( self, u ):
return u
def monitoring( self ):
return "" # Overload, to include addtl monitoring info in output
# ============================================================
# Controllers
# --- PID Controllers
class PidController( Component ):
def __init__( self, kp, ki, kd=0 ):
self.kp, self.ki, self.kd = kp, ki, kd
self.i = 0
self.d = 0
self.prev = 0
def work( self, e ):
self.i += DT*e
self.d = ( e - self.prev )/DT
self.prev = e
return self.kp*e + self.ki*self.i + self.kd*self.d
class AdvController( Component ):
def __init__( self, kp, ki, kd=0, clamp=(-1e10,1e10), smooth=1 ):
self.kp, self.ki, self.kd = kp, ki, kd
self.i = 0
self.d = 0
self.prev = 0
self.unclamped = True
self.clamp_lo, self.clamp_hi = clamp
self.alpha = smooth
def work( self, e ):
if self.unclamped:
self.i += DT*e
self.d = self.alpha*(e - self.prev)/DT + (1.0-self.alpha)*self.d
u = self.kp*e + self.ki*self.i + self.kd*self.d
self.unclamped = ( self.clamp_lo < u < self.clamp_hi )
self.prev = e
return u
# --- Relay and Band Controllers
class DeadbandController( Component ):
def __init__( self, zone ):
self.zone = zone
def work( self, e ):
if e>self.zone:
return e - self.zone
elif e<-self.zone:
return e + self.zone
else:
return 0
class RelayController( Component ):
def work( self, e ):
if e == 0:
return 0
return e/abs(e)
class DeadbandRelayController( Component ):
def __init__( self, zone ):
self.zone = zone
def work( self, e ):
if e>self.zone:
return 1
elif e<-self.zone:
return -1
else:
return 0
class HysteresisRelayController( Component ):
def __init__( self, zone ):
self.zone = zone
self.prev = None
def work( self, e ):
if e > self.prev: # raising
if e < self.zone:
u = 0
else:
u = 1
else: # falling
if e > -self.zone:
u = 0
else:
u = -1
self.prev = e
return u
# ============================================================
# Simple Systems
class Boiler( Component ):
# Default g: temp drops to 1/e in 100 secs (for water: approx 1 deg/sec)
# Work u: input is change in temp (per sec), if no heat loss
def __init__( self, g=0.01 ):
self.y = 0 # initial state, "temperature" (above ambient)
self.g = g # constant of proportionality (time constant)
def work( self, u ):
self.y += DT*( -self.g*self.y + u )
return self.y
class Spring( Component ):
# In mks units (defaults: 100g, 1N/m, approx 10 periods to fall to 1/e)
def __init__( self, m=0.1, k=1, g=0.05 ):
self.x = 0 # position
self.v = 0 # velocity
self.m = m # mass
self.k = k # spring constant: Newton/meter
self.g = g # damping factor
def work( self, u ):
a = ( - self.k*self.x - self.g*self.v + u )/self.m
self.v += DT*a
self.x += DT*self.v
return self.x
# ============================================================
# Filters and Actuators
class Identity( Component ):
def work( self, x ): return x
class Limiter( Component ):
def __init__( self, lo, hi ):
self.lo = lo
self.hi = hi
def work( self, x ):
return max( self.lo, min( x, self.hi ) )
class Discretizer( Component ):
def __init__( self, binwidth ):
self.binwidth = binwidth
def work( self, u ):
return self.binwidth*int( u/self.binwidth )
class Hysteresis( Component ):
def __init__( self, threshold ):
self.threshold = threshold
self.prev = 0
def work( self, u ):
y = self.prev
if abs(u - self.prev) > self.threshold:
y = u
self.prev = u
return y
class Integrator( Component ):
def __init__( self ):
self.data = 0
def work( self, u ):
self.data += u
return DT*self.data
class FixedFilter( Component ):
def __init__( self, n ):
self.n = n
self.data = []
def work( self, x ):
self.data.append(x)
if len(self.data) > self.n:
self.data.pop(0)
return float(sum(self.data))/len(self.data)
class RecursiveFilter( Component ):
def __init__( self, alpha ):
self.alpha = alpha
self.y = 0
def work( self, x ):
self.y = self.alpha*x + (1-self.alpha)*self.y
return self.y
# ============================================================
# Setpoints
def impulse( t, t0 ):
if abs(t-t0) < DT: return 1 # Floating point or integer time?
return 0
def step( t, t0 ):
if t >= t0: return 1
return 0
def double_step( t, t0, t1 ):
if t>=t0 and t<t1: return 1
return 0
# def pulses( t, t0, tp ):
# if t >= t0 and (t-t0)%tp == 0: return 1
# return 0
def harmonic( t, t0, tp ):
if t>=t0: return math.sin(2*math.pi*(t-t0)/tp)
return 0
def relay( t, t0, tp ):
if t>=t0:
if math.ceil(math.sin(2*math.pi*(t-t0)/tp)) > 0:
return 1
else:
return 0
return 0
# ============================================================
# Loop functions
# def setpoint( t ):
# return step( t, 0 )
def static_test( plant_ctor, ctor_args, umax, steps, repeats, tmax ):
# Complete test for static process characteristic
# From u=0 to umax taking steps steps, each one repeated repeats
for i in range( 0, steps ):
u = float(i)*umax/float(steps)
for r in range( repeats ):
p = apply( plant_ctor, ctor_args ) # this is: p = Plant( a, b, c )
for t in range( tmax ):
y = p.work(u)
print u, y
quit()
def step_response( setpoint, plant, tm=5000 ):
for t in range( tm ):
r = setpoint(t) # This is the plant input, not really the setpoint!
u = r
y = plant.work( u )
print t, t*DT, r, 0, u, u, y, y, plant.monitoring()
quit()
def open_loop( setpoint, controller, plant, tm=5000 ):
for t in range( tm ):
r = setpoint(t) # This is the controller input, not really the setpt!
u = controller.work( r )
y = plant.work( u )
print t, t*DT, r, 0, u, u, y, y, plant.monitoring()
quit()
def closed_loop( setpoint, controller, plant, tm=5000, inverted=False,
actuator=Identity(), returnfilter=Identity() ):
z = 0
for t in range( tm ):
r = setpoint(t)
e = r - z
if inverted == True: e = -e
u = controller.work(e)
v = actuator.work(u)
y = plant.work(v)
z = returnfilter.work(y)
print t, t*DT, r, e, u, v, y, z, plant.monitoring()
quit()
# ============================================================
if __name__ == '__main__':
def setpoint( t ):
return 10*double_step( t, 1000, 6000 )
p = Boiler()
c = PidController( 0.45, 0.01 )
closed_loop( setpoint, c, p, 15000 )