1## Copyright (C) 2009-2016   Lukas F. Reichlin
2##
3## This file is part of LTI Syncope.
4##
5## LTI Syncope is free software: you can redistribute it and/or modify
6## it under the terms of the GNU General Public License as published by
7## the Free Software Foundation, either version 3 of the License, or
8## (at your option) any later version.
9##
10## LTI Syncope is distributed in the hope that it will be useful,
11## but WITHOUT ANY WARRANTY; without even the implied warranty of
12## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13## GNU General Public License for more details.
14##
15## You should have received a copy of the GNU General Public License
16## along with LTI Syncope.  If not, see <http://www.gnu.org/licenses/>.
17
18## -*- texinfo -*-
19## @deftypefn{Function File} {@var{C} =} pid (@var{Kp})
20## @deftypefnx{Function File} {@var{C} =} pid (@var{Kp}, @var{Ki})
21## @deftypefnx{Function File} {@var{C} =} pid (@var{Kp}, @var{Ki}, @var{Kd})
22## @deftypefnx{Function File} {@var{C} =} pid (@var{Kp}, @var{Ki}, @var{Kd}, @var{Tf})
23## @deftypefnx{Function File} {@var{C} =} pid (@var{Kp}, @var{Ki}, @var{Kd}, @var{Tf}, @var{Ts})
24## Return the transfer function @var{C} of the @acronym{PID} controller
25## in parallel form with first-order roll-off.
26## With a valid @var{Ts} a discrete-time system is created.
27##
28## @example
29## @group
30##              Ki      Kd s
31## C(s) = Kp + ---- + --------
32##              s     Tf s + 1
33## @end group
34## @end example
35## @end deftypefn
36
37## Author: Lukas Reichlin <lukas.reichlin@gmail.com>
38## Created: June 2015
39## Version: 0.1
40
41## TODO: dozens of options, ...
42##       If you wish to kill time with this repetitive task,
43##       I'm happy to add your work :-)
44
45function C = pid (Kp = 1, Ki = 0, Kd = 0, Tf = 0, Ts = 0)
46
47  if (! is_real_scalar (Kp, Ki, Kd, Tf, Ts) || nargin > 5 )
48    print_usage ();
49  endif
50
51  if (Kd == 0)    # catch cases like  pid (2, 0, 0, 3)
52    Tf = 0;
53  endif
54
55  if (Ki == 0)    # minimal realization if  num(3) == 0  and  den(3) == 0
56    C = tf ([Kp*Tf+Kd, Kp], [Tf, 1], Ts);
57  else
58    C = tf ([Kp*Tf+Kd, Kp+Ki*Tf, Ki], [Tf, 1, 0], Ts);
59  endif
60
61endfunction
62