def Pade2(t): """Second order Pade approximation""" num = poly([t*t, -6*t, 12]) den = poly([t*t, 6*t, 12]) num = num.mult(1.0/den[0]) den = den.mult(1.0/den[0]) return tf(num, den)
def Pade4(t): """Fourth order Pade approximation""" num = poly([t*t*t*t, -20*t*t*t, 180*t*t, -840*t, 1680]) den = poly([t*t*t*t, 20*t*t*t, 180*t*t, 840*t, 1680]) num = num.mult(1.0/den[0]) den = den.mult(1.0/den[0]) return tf(num, den)
def Pade3(t): """Third order Pade approximation""" num = poly([-t*t*t, 12*t*t, -60*t, 120]) den = poly([t*t*t, 12*t*t, 60*t, 120]) num = num.mult(1.0/den[0]) den = den.mult(1.0/den[0]) return tf(num, den)
def Pade5(t): """Fifth order Pade approximation""" num = poly([-t*t*t*t*t, 30*t*t*t*t, -420*t*t*t, 3360*t*t, -15120*t, 30240]) den = poly([t*t*t*t*t, 30*t*t*t*t, 420*t*t*t, 3360*t*t, 15120*t, 30240]) num = num.mult(1.0/den[0]) den = den.mult(1.0/den[0]) return tf(num, den)
def Pade4(t): """Fourth order Pade approximation""" num = poly([t * t * t * t, -20 * t * t * t, 180 * t * t, -840 * t, 1680]) den = poly([t * t * t * t, 20 * t * t * t, 180 * t * t, 840 * t, 1680]) num = num.mult(1.0 / den[0]) den = den.mult(1.0 / den[0]) return tf(num, den)
def Pade3(t): """Third order Pade approximation""" num = poly([-t * t * t, 12 * t * t, -60 * t, 120]) den = poly([t * t * t, 12 * t * t, 60 * t, 120]) num = num.mult(1.0 / den[0]) den = den.mult(1.0 / den[0]) return tf(num, den)
def Pade2(t): """Second order Pade approximation""" num = poly([t * t, -6 * t, 12]) den = poly([t * t, 6 * t, 12]) num = num.mult(1.0 / den[0]) den = den.mult(1.0 / den[0]) return tf(num, den)
def Pade1(t): """First order Pade approximation""" num = poly([-t, 2]) den = poly([t, 2]) num = num.mult(1.0/den[0]) den = den.mult(1.0/den[0]) return tf(num, den)
def Pade1(t): """First order Pade approximation""" num = poly([-t, 2]) den = poly([t, 2]) num = num.mult(1.0 / den[0]) den = den.mult(1.0 / den[0]) return tf(num, den)
def run(self, output_dir): model = models_index[self.model]('pt_BR') g = model.callback(**self.kwargs) t, y = self.nmethod(g, self.sample_time, self.total_time) fig = Figure(figsize=(8, 6), dpi=300) ax = fig.add_subplot(111, xlabel='Tempo (seg)', ylabel='Amplitude') ax.plot(t, y, label='Resposta ao Degrau') if self.kp is None or self.ki is None or self.kd is None: kp, ki, kd = ZieglerNichols(t, y, Smith).gains else: kp, ki, kd = self.kp, self.ki, self.kd if self.simulate: # transfer function of the PID controller g_ = tf([kd, kp, ki], [1, 0]) my_g = (g_ * g).feedback_unit() # discretize the controlled system t1, y1 = self.nmethod(my_g, self.sample_time, self.total_time) ax.plot(t1, y1, label='Resposta ao Degrau Controlada') else: # generate the tuning line ident = Smith(t, y) t1, y1 = ident.tuning_line ax.plot(t1, y1, label='Reta de Sintonia') ax.annotate( '%.1f%%' % ident.point1, xy=(t1[1], y1[1]), xycoords='data', xytext=(t1[1] + (self.total_time / 15.0), y1[1]), arrowprops=dict(facecolor='black', shrink=0.05), ) ax.annotate( '%.1f%%' % ident.point2, xy=(t1[2], y1[2]), xycoords='data', xytext=(t1[2] + (self.total_time / 15.0), y1[2]), arrowprops=dict(facecolor='black', shrink=0.05), ) legend = [] for arg in model.args: if arg in self.blacklist_args: continue legend.append('%s=%s' % (arg, self.kwargs[arg])) ax.legend( loc='best', prop={'size': 'x-small'}, title='kp=%.1f; ki=%.1f; kd=%.1f;' % (kp, ki, kd), ) canvas = FigureCanvas(fig) filename = 'cap4_model%i_%i.eps' % (self.model, self.plot_id) canvas.print_eps(os.path.join(output_dir, filename))
def Pade5(t): """Fifth order Pade approximation""" num = poly([ -t * t * t * t * t, 30 * t * t * t * t, -420 * t * t * t, 3360 * t * t, -15120 * t, 30240 ]) den = poly([ t * t * t * t * t, 30 * t * t * t * t, 420 * t * t * t, 3360 * t * t, 15120 * t, 30240 ]) num = num.mult(1.0 / den[0]) den = den.mult(1.0 / den[0]) return tf(num, den)
def callback(self): return tf([1], [1, 0, -1])
def _plot(id): # get the discretization method _nmethod = request.args['n_method'] if _nmethod == '1': nmethod = Euler elif _nmethod == '2': nmethod = RungeKutta2 elif _nmethod == '3': nmethod = RungeKutta3 else: nmethod = RungeKutta4 # get the identification method _imethod = request.args['i_method'] if _imethod == '1': imethod = Alfaro elif _imethod == '2': imethod = Broida elif _imethod == '3': imethod = ChenYang elif _imethod == '4': imethod = Ho elif _imethod == '5': imethod = Smith else: imethod = Viteckova # get the pid_simulation method _tmethod = request.args['t_method'] if _tmethod == '0': tmethod = None elif _tmethod == '1': tmethod = ZieglerNichols elif _tmethod == '2': tmethod = CohenCoon elif _tmethod == '3': tmethod = ChienHronesReswick0 else: tmethod = ChienHronesReswick20 # get process parameters parameters = {} model = models.index[id](str(get_locale())) if model.args is not None: for arg in model.args: if arg in request.args: parameters[arg] = float(request.args[arg]) # get transfer function g = model.callback(**parameters) # get options sample = float(request.args.get('Sample_Time', 0.01)) time = float(request.args.get('Total_Time', 10)) what = int(request.args.get('what', 1)) # validation of parameters if float(time) / float(sample) > 5000: abort(404) # create the figure fig = Figure(figsize=(8, 6), dpi=100) ax = fig.add_subplot( 111, xlabel = _('Tempo (seg)'), ylabel = _('Amplitude'), ) # discretize the transfer function and plot it t, y = nmethod(g, sample, time) ax.plot(t, y, label=_('Resposta ao Degrau')) # simulate the PID controller, if wanted if tmethod is not None: kp, ki, kd = tmethod(t, y, imethod).gains else: try: kp = float(request.args.get('kp', 0)) except: kp = 0 try: ki = float(request.args.get('ki', 0)) except: ki = 0 try: kd = float(request.args.get('kd', 0)) except: kd = 0 # plot the simulated controller if what == 2 or tmethod is None: # transfer function of the PID controller g_ = tf([kd, kp, ki], [1, 0]) my_g = (g_ * g).feedback_unit() # discretize the controlled system t1, y1 = nmethod(my_g, sample, time) ax.plot(t1, y1, label=_('Resposta ao Degrau Controlada')) else: # generate the tuning line ident = imethod(t, y) t1, y1 = ident.tuning_line ax.plot(t1, y1, label=_('Reta de Sintonia')) ax.annotate( '%.1f%%' % ident.point1, xy=(t1[1], y1[1]), xycoords='data', xytext=(t1[1]+(time/15.0), y1[1]), arrowprops=dict(facecolor='black', shrink=0.05), ) ax.annotate( '%.1f%%' % ident.point2, xy=(t1[2], y1[2]), xycoords='data', xytext=(t1[2]+(time/15.0), y1[2]), arrowprops=dict(facecolor='black', shrink=0.05), ) # add the legend ax.legend( loc = 'best', prop = {'size': 'x-small'}, title = 'kp=%.1f; ki=%.1f; kd=%.1f;' % (kp, ki, kd), ) # add the grid ax.grid(True) # generate a PNG file with the graph canvas = FigureCanvas(fig) image = StringIO() canvas.print_png(image) # return the generated image response = make_response(image.getvalue()) response.headers['Content-Type'] = 'image/png' return response
def callback(self, Alpha): num = (poly([1, 1]) * poly([Alpha, 1])) * (poly([Alpha * Alpha, 1]) * \ poly([Alpha * Alpha * Alpha, 1])) return tf([1], num)
def callback(self, n): a = poly([1, 1]) for i in range(1, int(n)): a = a * poly([1, 1]) return tf([1], list(a))
def callback(self, Tau, pade_order): return tf([1], poly([Tau, 1]) * poly([Tau, 1])) * \ pade_index[int(pade_order)](1)
def callback(self, Alpha): return tf([-Alpha, 1], (poly([1, 1]) * poly([1, 1])) * poly([1, 1]))
def callback(self, k, T1, T2): return tf([-T1 * k, k], poly([T1, 1]) * poly([T2, 1]))
def callback(self, k, T1, T2, T3, T4, Tt, pade_order): num = poly([k * T4, k]) den = poly([T1, 1]) * poly([T2, 1]) * poly([T3, 1]) return tf(num, den) * pade_index[int(pade_order)](Tt)
def callback(self, k, Tau): return tf([k], [Tau, 1])
def callback(self, Tau, pade_order): return tf([1], [Tau, 1, 0]) * pade_index[int(pade_order)](1)
def callback(self): p1 = tf([100], poly([1, 10]) * poly([1, 10])) p2 = tf([1], [1, 1]) p3 = tf([0.5], [1, 0.05]) return p1 * (p2 + p3)
def callback(self, Omega, Zeta): return tf([Omega * Omega], poly([1, 1]) * poly([1, 2 * Zeta * Omega, Omega * Omega]))
def callback(self): den = (poly([1, 0]) * poly([1, 1])) * (poly([1, 1]) * poly([1, 36])) return tf(poly([1, 6]) * poly([1, 6]), den)