Reorg5.pas

[From Bill Powers (970714.1337 MDT)]

Here is reorg5.pas, which reorganizes the output function (ko, output gain)
of a single control system, the input function (ki) being fixed at 1. This
is the complement of reorg4.pas which reorganizes the input function.

Best,

Bill P.

···

----------------------------------------------------------------------
program reorg5;

{
A single control system, reorganizing output function.
}

uses dos, crt,graph, grutils;

const SPEED = 2e-4;
      SLOWREF = 0.01;
      SLOWOUT = 0.1;
      HSCALE = 50; {scale of time plot, iterations per point}

var qi,ki,p,r,e,ko,qo,ke: real;
    iteration: longint;
    newersq,oldersq,deltako: real;
    ch: char;

procedure makeref;
const r1: real = 0.0;
      r2: real = 0.0;
begin
r1 := r1 + slowref * (2000*(random - 0.5) - r1);
r2 := r2 + slowref * (r1 - r2);
r := r + slowref * (r2 - r);
end;

procedure initialize;
begin
p := 0.0;
qo := 0.0;
qi := 0.0;
ki := 1.0;
ke := 1.0;
ko := 0.0;
deltako := 1e-15;
oldersq := 0.0;
iteration := 0;
end;

procedure control;
begin
e := r - p;
qo := qo + (ko*e - qo)*slowout;
qi := ke*qo;
p := ki*qi;
oldersq := newersq;
newersq := abs(e);
end;

procedure reorganize;
begin
  if newersq > oldersq then deltako := 2*(random - 0.5);
  ko := ko + deltako*abs(e)*SPEED - 2e-7*ko;
end;

procedure show;
begin
if (iteration mod HSCALE) = 0 then
begin
  putpixel(iteration div HSCALE,479 - round(abs(e)),white);
  putpixel(iteration div HSCALE,240,lightgray);
  putpixel(iteration div HSCALE,240 - round(r), lightred);
  putpixel(iteration div HSCALE,240 - round(p), lightgreen);
  putpixel(iteration div HSCALE,240 - round(10*ko), yellow);
  putpixel(iteration div HSCALE,240 - round(qi), lightblue);
end;
end;

procedure legend;
begin
clearviewport;
setcolor(white); outtextxy(0,0,'abs(error)');
setcolor(lightred); outtextxy(0,10,'ref');
setcolor(lightgreen); outtextxy(0,20,'p');
setcolor(yellow); outtextxy(0,30,'ki');
setcolor(lightblue); outtextxy(0,40,'qi');
setcolor(lightgray); outtextxy(0,50,'zero');
setcolor(white); outtextxy(200,0,'PRESS ANY KEY TO QUIT');
end;

begin
clrscr;
initgraphics;
setcolor(white);
initialize;
repeat
iteration := 0;
legend;
while iteration < 639*HSCALE + 1 do
begin
  makeref;
  control;
  reorganize;
  show;
  inc(iteration);
end;
until keypressed;
ch := readkey;
closegraph;
end.