  function u = dpid(delt,par,r,y,uprev)
% digital pid
% b.w. bequette
% 5 sept 07
% 10 sept 09 - revised for more logical b subscripts
% 12 sept 13 - used in homework 2, MPC course
%     y is a vector of the previous outputs (incl current)
%     uprev is the previous (step k-1) manipulated input
%
  kc = par(1);
  taui = par(2);
  taud = par(3);
  b2 = kc*(1+(delt/taui)+(taud/delt));
  b1 = -kc*(1+2*(taud/delt));
  b0 = kc*taud/delt;
%
  ekm2 = r(1) - y(1);  % step k-2
  ekm1 = r(2) - y(2);  % step k-1
  ek   = r(3) - y(3);  % step k
  u = uprev + b2*ek + b1*ekm1 + b0*ekm2;
  