Example #1
0
def plot_behavior(xset,kp,ki,kd,m,b,k,g=1,s=1, total_time=10, npoints=100):
    
    dev = Device()
    dev.m = m
    dev.b = b
    dev.k = k
    dev.g = g
    dev.s = s
    
    cont = Controller()
    cont.kp = kp
    cont.ki = ki
    cont.kd = kd
    
    tt = np.linspace(0,total_time, npoints)
    
    xx0 = np.zeros(tt.shape)
    xx0[5:] = xset
    
    xx = [0]
    vv = [0]
    aa = [0]
    
    t0 = tt[0]
    for t,x0 in zip(tt[1:],xx0[1:]):
        cont.set_point = x0
        a = cont(dev.x,t)
        x,v = dev(a,t-t0)
        aa.append(a)
        xx.append(x)
        vv.append(v)
        t0 = t
    
    fig,ax = pyplot.subplots(1,2,figsize=(10,3))
    ptset, = ax[1].plot(tt, xx0, color='blue',label='set point')
    ptcont, = ax[1].plot(tt, aa, color='red',label='control')
    ptval, = ax[0].plot(tt, xx, color='green',label='actual')
    ptvel, = ax[1].plot(tt, vv, color='magenta',label='velocity')
    ax[0].margins(0,0.1)
    ax[1].margins(0,0.1)
    l = ax[1].legend((ptset,ptcont,ptval,ptvel), ('set point','control','actual','velocity'),
              loc='best')
    l.set_zorder(1)
    
    return tt, xx0, aa, xx, vv