# -*- coding: utf-8 -*-
"""
Created on Tue Sep 20 21:57:29 2016

@author: mtkes
"""

import matplotlib.pyplot as plot
import numpy as np

tmax = 1000
ts = np.arange(0, tmax, 1)
pvs = np.zeros(tmax)


kp = 0.2
ki = 0.01
kd = 0.0  

dt = ts[1] - ts[0]

Gp = 2
delay = 50 * dt
tau = 10 * dt
 

sp  = 1.0
err = 0.0
intErr = 0.0
lastErr = 0.0
lastT = ts[0]

lastG = 0.0

i = 0
d = 0
exp = -np.exp(-1/tau)
for t in ts:
    derr = err - lastErr
    intErr = intErr + err
    mv = (kp * err) + (ki * intErr) + (kd * (derr/dt))
    G = 0.0
    if (t >= delay):
        G = mv * Gp * (1.0 + exp) - (lastG * exp)
    else:
        d += 1
    
    pvs[i] = G
    lastG = G

    i += 1
    
    lastErr = err
    err = 0.0
    if (t >= delay):
        err = sp - pvs[i-d]
        
    #err += np.random.randn(1)*0.04
    
    
plot.figure(1)
plot.cla()
plot.grid()
plot.plot(ts,pvs)
    
    
    
@@ -25,59 +25,1475 @@
 %22%22%22%0A
-Created on Tue Sep 20 21:57:29 2016%0A%0A@author: mtkes
+pid - example of PID control of a simple process with a time constant%0A%0ACopyright (c) 2016 - RocketRedNeck.com RocketRedNeck.net %0A%0ARocketRedNeck and MIT Licenses %0A%0ARocketRedNeck hereby grants license for others to copy and modify this source code for %0Awhatever purpose other's deem worthy as long as RocketRedNeck is given credit where %0Awhere credit is due and you leave RocketRedNeck out of it for all other nefarious purposes. %0A%0APermission is hereby granted, free of charge, to any person obtaining a copy %0Aof this software and associated documentation files (the %22Software%22), to deal %0Ain the Software without restriction, including without limitation the rights %0Ato use, copy, modify, merge, publish, distribute, sublicense, and/or sell %0Acopies of the Software, and to permit persons to whom the Software is %0Afurnished to do so, subject to the following conditions: %0A%0AThe above copyright notice and this permission notice shall be included in all %0Acopies or substantial portions of the Software. %0A%0ATHE SOFTWARE IS PROVIDED %22AS IS%22, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR %0AIMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, %0AFITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE %0AAUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER %0ALIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, %0AOUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE %0ASOFTWARE. 
 %0A%22%22%22
@@ -1625,17 +1625,16 @@
 %0Aki = 0.
-0
 1%0Akd = 0
@@ -1664,17 +1664,17 @@
 %5D%0A%0AGp = 
-2
+1
 %0Adelay =
@@ -1678,10 +1678,9 @@
 y = 
-50
+1
  * d
@@ -1688,17 +1688,16 @@
 %0Atau = 1
-0
  * dt%0A %0A

Update PID example