title: Python PID simulation
body: <p>I am trying to figure out how to simulate a control system in python.  This is what I have so far:</p>
  
  <p>PID.py - functions to set Kp, Ki, Kd, and a function to generate an output based on an error. ex. PID.genout(err)</p>
  
  <p>PIDPlot.py - plots the output based on a step input. here is a sample of the code:</p>
  
  <pre><code>for i in range(1, 51):
  
      # summing node
      err = sp - fb
  
      # PID block
      outv = pid.GenOut(err)
  
      # control feedback
      if sp &gt; 0:
          fb += (outv - (1.0/i))
  
      # start with sp = 0, simulate a step input at t(10)
      if i &gt; 5:
          sp = 10
  
      # add sp and outv to there lists
      sp_list.append(sp)
      fb_list.append(fb)
      sample_list.append(i * .05)
  
  
  
      time.sleep(.05)
  </code></pre>
  
  <p>I would like to change the part that controls the feedback so that it models my system (Plant) more closely, but that is where I get stuck.  I determined the transfer function of the system(plant) by applying a step and logging the output and plotting it. Based on the curve, dc gain and time constant I came up with  0.92/16s +1 for the transfer function.
  When I plot the step response in Python like so:</p>
  
  <pre><code>from numpy import min, max
  from scipy import linspace
  from scipy.signal import lti, step
  
  # making transfer function
  # example from Ogata Modern Control Engineering 
  # 4th edition, International Edition page 307
  
  # num and den, can be list or numpy array type
  num = [-0.93] 
  
  #denominator of the form s^2 + s +1
  den = [16, 1]
  
  tf = lti(num, den)
  
  # get t = time, s = unit-step response
  t, s = step(tf)
  
  # recalculate t and s to get smooth plot
  t, s = step(tf, T = linspace(min(t), t[-1], 500))
  
  # get i = impulse
  #t, i = impulse(tf, T = linspace(min(t), t[-1], 500))
  
  from matplotlib import pyplot as plt
  
  #plt.plot(t, s, t, i)
  plt.plot(t, s)
  plt.title('Transient-Response Analysis')
  plt.xlabel('Time(sec)')
  plt.ylabel('Amplitude')
  plt.hlines(1, min(t), max(t), colors='r')
  plt.hlines(0, min(t), max(t))
  plt.xlim(xmax=max(t))
  plt.legend(('Unit-Step Response',), loc=0)
  plt.grid()
  plt.show()
  </code></pre>
  
  <p>It matches my plotted data nicely.  Now how can I use this transfer function to generate the feedback in the PIDPlot? Then from there I can try to write code to auto tune the pid based on the system(plant).</p>