def closedloop3(): # Closed loop, setpoint 1.0, incremental controller (non-PID) def setpoint(t): return 1.0 class SpecialController(fb.Component): def __init__(self, period1, period2): self.period1 = period1 self.period2 = period2 self.t = 0 def work(self, u): if u > 0: self.t = self.period1 return +1 self.t -= 1 # At this point: u <= 0 guaranteed! if self.t == 0: self.t = self.period2 return -1 return 0 p = ServerPool(0, consume_queue, load_queue) c = SpecialController(100, 10) fb.closed_loop(setpoint, c, p, actuator=fb.Integrator())
def closedloop3(): # Closed loop, setpoint 1.0, incremental controller (non-PID) def setpoint( t ): return 1.0 class SpecialController( fb.Component ): def __init__( self, period1, period2 ): self.period1 = period1 self.period2 = period2 self.t = 0 def work( self, u ): if u > 0: self.t = self.period1 return +1 self.t -= 1 # At this point: u <= 0 guaranteed! if self.t == 0: self.t = self.period2 return -1 return 0 p = ServerPool( 0, consume_queue, load_queue ) c = SpecialController( 100, 10 ) fb.closed_loop( setpoint, c, p, actuator=fb.Integrator() )
def closedloop( kp, ki, f=fb.Identity() ): def setpoint( t ): if t > 1000: return 125 return 100 k = 1.0/20.0 p = AdPublisher( 100, 2 ) c = fb.PidController( k*kp, k*ki ) fb.closed_loop( setpoint, c, p, returnfilter=f )
def production(): def setpoint(t): if t*fb.DT < 6*60: return 50 else: return 45 # if t < 40000: return 50 # else: return 45 p = CpuWithCooler( True, True ); p.temp = 50 # Initial temp c = fb.AdvController( 2, 0.5, 0, clamp=(0,10) ) fb.closed_loop( setpoint, c, p, 100000, inverted=True, actuator=fb.Limiter( 0, 10 ) )
def closedloop(): def demand(t): return int(random.gauss(0, 15)) def setpoint(t): if t > 5000: return 0.5 return 0.7 p = SmoothedCache(0, demand, 100) c = fb.PidController(100, 250) fb.closed_loop(setpoint, c, p, 10000)
def closedloop_accumul( kp, ki ): def setpoint( t ): global accumul_goal if t > 1000: accumul_goal += 125 else: accumul_goal += 100 return accumul_goal k = 1.0/20.0 p = AdPublisher( 100, 2 ) c = fb.PidController( k*kp, k*ki ) fb.closed_loop( setpoint, c, p, returnfilter=fb.Integrator() )
def nestedloops(): def setpoint(t): return 200 if t < 2000: return 100 elif t < 3000: return 125 else: return 25 p = InnerLoop(0.5, 0.25, load_queue) # InnerLoop is "plant" for outer loop # c = fb.PidController( 0.06, 0.001 ) c = fb.AdvController(0.35, 0.0025, 4.5, smooth=0.15) # fb.closed_loop( setpoint, c, p ) fb.closed_loop(setpoint, c, p, actuator=fb.RecursiveFilter(0.5))
def nestedloops(): def setpoint( t ): return 200 if t < 2000: return 100 elif t < 3000: return 125 else: return 25 p = InnerLoop(0.5, 0.25, load_queue) # InnerLoop is "plant" for outer loop # c = fb.PidController( 0.06, 0.001 ) c = fb.AdvController( 0.35, 0.0025, 4.5, smooth=0.15 ) # fb.closed_loop( setpoint, c, p ) fb.closed_loop( setpoint, c, p, actuator=fb.RecursiveFilter(0.5) )
def closedloop_jumps(): def demand(t): if t < 3000: return int(random.gauss(0, 15)) elif t < 5000: return int(random.gauss(0, 35)) else: return int(random.gauss(100, 15)) def setpoint(t): return 0.7 p = SmoothedCache(0, demand, 100) c = fb.PidController(270, 7.5) # Ziegler-Nichols - closedloop1 # c = fb.PidController( 100, 4.3 ) # Cohen-Coon - 2 # c = fb.PidController( 80, 2.0 ) # AMIGO - 3 # c = fb.PidController( 150, 2 ) # 4 fb.closed_loop(setpoint, c, p, 10000)
def closedloop1(): # Closed loop, setpoint 0.6-0.8, PID Controller def loadqueue(): global global_time global_time += 1 if global_time > 2100: return random.gauss(1200, 5) return random.gauss(1000, 5) def setpoint(t): if t > 2000: return 0.6 else: return 0.8 p = ServerPool(8, consume_queue, loadqueue) c = fb.PidController(1, 5) fb.closed_loop(setpoint, c, p, 10000)
def closedloop2(): # Closed loop, setpoint 0.999x, Asymm (!) Controller def setpoint( t ): if t < 1000: # Switch on slowly, to avoid initial overshoot return t/1000.0 return 0.9995 class AsymmController( fb.PidController ): def work( self, e ): if e > 0: e /= 20.0 self.i += fb.DT*e self.d = ( self.prev - e )/fb.DT self.prev = e return self.kp*e + self.ki*self.i + self.kd*self.d p = ServerPool( 0, consume_queue, load_queue ) c = AsymmController( 10, 200 ) fb.closed_loop( setpoint, c, p )
def closedloop2(): # Closed loop, setpoint 0.999x, Asymm (!) Controller def setpoint(t): if t < 1000: # Switch on slowly, to avoid initial overshoot return t / 1000.0 return 0.9995 class AsymmController(fb.PidController): def work(self, e): if e > 0: e /= 20.0 self.i += fb.DT * e self.d = (self.prev - e) / fb.DT self.prev = e return self.kp * e + self.ki * self.i + self.kd * self.d p = ServerPool(0, consume_queue, load_queue) c = AsymmController(10, 200) fb.closed_loop(setpoint, c, p)
def closedloop1(): # Closed loop, setpoint 0.6-0.8, PID Controller def loadqueue(): global global_time global_time += 1 if global_time > 2100: return random.gauss( 1200, 5 ) return random.gauss( 1000, 5 ) def setpoint( t ): if t > 2000: return 0.6 else: return 0.8 p = ServerPool( 8, consume_queue, loadqueue ) c = fb.PidController( 1, 5 ) fb.closed_loop( setpoint, c, p, 10000 )
# ============================================================ def closed_loop( c, p, tm=5000 ): def setpoint( t ): return 810 y = 0 for t in range( tm ): r = setpoint(t) e = r - y u = c.work(e,t) y = p.work(u,t) print t, r, e, u, y xplot.append(t) yplot.append(y) ytarget.append(setpoint(t)) # ============================================================ def setpoint(t): return 810 fb.DT = 0.1 c = Controller( 1, 5.5, 4.5, 810 ) p = System() fb.closed_loop(setpoint, c, p, 50 )
def setpoint(t): #return 2.05 return 56.7 fb.DT = 0.1 tm = 100 c = Controller( 1, 5.5, 4.5, setpoint(0),noise=True,delay=3 ) c2 = Controller( 1, 5.5, 4.5, setpoint(0),noise=False ) #p = System() #p2= System() p = Device() p2 = Device() fb.closed_loop(setpoint, c, p, tm) fb.closed_loop(setpoint, c2, p2, tm) mp.plot(c.yplot,'r') mp.plot(c.reference,'b') mp.plot(c2.yplot,'c') mp.plot(c2.reference,'m') mp.show() mp.plot(c.eplot,'r') mp.step(c.usignal,'b') mp.plot(c2.eplot,'c') mp.step(c2.usignal,'m') mp.show()
def __init__(self): self.state = 0 def work(self, u): self.state += u self.state = max(0, min(self.state, 4)) # Constrain to 0..4 return self.state class Logarithm(fb.Component): def work(self, u): if u <= 0: return 0 return math.log(u) if __name__ == '__main__': fb.DT = 1 def setpoint(t): return 3.5 * math.log(10.0) c = DeadzoneController(0.5 * math.log(8.0)) p = GameEngine() fb.closed_loop(setpoint, c, p, actuator=ConstrainingIntegrator(), returnfilter=Logarithm())
class ConstrainingIntegrator( fb.Component ): def __init__( self ): self.state = 0 def work( self, u ): self.state += u self.state = max(0, min( self.state, 4 ) ) # Constrain to 0..4 return self.state class Logarithm( fb.Component ): def work( self, u ): if u <= 0: return 0 return math.log(u) if __name__ == '__main__': fb.DT = 1 def setpoint(t): return 3.5*math.log( 10.0 ) c = DeadzoneController( 0.5*math.log(8.0) ) p = GameEngine() fb.closed_loop( setpoint, c, p,actuator=ConstrainingIntegrator(), returnfilter=Logarithm() )