home *** CD-ROM | disk | FTP | other *** search
/ Programmer 7500 / MAX_PROGRAMMERS.iso / PASCAL / OOP-TP55.ZIP / LIST2_3.PAS < prev    next >
Encoding:
Pascal/Delphi Source File  |  1990-03-08  |  6.6 KB  |  262 lines

  1. program Listing2_3;
  2.  
  3. uses Crt; { for KeyPressed }
  4.  
  5. const
  6.  
  7. ON  = true;
  8. OFF = false;
  9. MinR = 0;       { Minimum number of counts in an analog input }
  10. MaxR = 4095;    { Maximum number of counts in an analog input }
  11.  
  12. var
  13.     f : text;  {predefined file type}
  14.  
  15.  
  16. type
  17.  
  18. Resolution = 0..4095;  { Range for analog input }
  19. String12 = string[12];
  20.  
  21. Tag = object
  22.       TagNumber : String12;
  23.       procedure Init( ATag : String12 );
  24.       end;
  25.  
  26. Digital = object(Tag)
  27.           Status : boolean;
  28.           procedure Init( ATag : String12; AStatus : boolean );
  29.           procedure PutStatus( AStatus : boolean );
  30.           function GetStatus : boolean;
  31.           end;
  32.  
  33. Analog = object(Tag)
  34.          Value : Resolution;
  35.          ZeroVal : real;
  36.          MaxVal : real;
  37.          Slope  : real;
  38.          procedure Init( ATag : String12; AValue : Resolution;
  39.                          Min, Max : real );
  40.          procedure PutValue( AValue : real );
  41.          function GetValue : real;
  42.          end;
  43.  
  44. DOutput = object(Digital)
  45.           end;
  46.  
  47. Pump = object(DOutput)
  48.        FlowRate : real;
  49.        procedure Init( ATag : String12; AStatus : boolean; AFlow : real );
  50.        function Flow : real;
  51.        end;
  52.  
  53.  
  54. DInput = object(Digital)
  55.             Setpoint  : real;
  56.             Reading   : real;
  57.             procedure PutSetpoint( NewSetpoint : real );
  58.             end;
  59.  
  60. HiSwitch = object(DInput)
  61.            procedure Init( ATag : string;
  62.                          ASetpoint : real;
  63.                          AReading : real);
  64.            procedure PutReading( NewReading : real );
  65.            end;
  66.  
  67. LoSwitch = object(DInput)
  68.            procedure Init( ATag : string;
  69.                          ASetpoint : real;
  70.                          AReading : real);
  71.            procedure PutReading( NewReading : real );
  72.            end;
  73.  
  74. procedure Tag.Init( ATag : String12 );
  75. begin
  76.      TagNumber := ATag;
  77. end;
  78.  
  79. procedure Digital.Init( ATag : String12; AStatus : boolean );
  80. begin
  81.      Tag.Init( ATag );
  82.      Status := AStatus;
  83. end;
  84. procedure Digital.PutStatus( AStatus : boolean );
  85. begin
  86.      Status := AStatus;
  87. end;
  88. function Digital.GetStatus : boolean;
  89. begin
  90.      if Status = on then
  91.         writeln( f, TagNumber, ' is ON.' )
  92.      else
  93.         writeln( f, TagNumber, ' is OFF.' );
  94.      GetStatus := Status;
  95. end;
  96.  
  97. procedure Pump.Init( ATag : String12; AStatus : boolean; AFlow : real );
  98. begin
  99.      Digital.Init( ATag, AStatus );
  100.      FlowRate := AFlow;
  101. end;
  102.  
  103. function Pump.Flow : real;
  104. begin
  105.      Flow := FlowRate;
  106. end;
  107.  
  108. procedure Analog.Init( ATag : String12;
  109.                        AValue : Resolution;
  110.                        Min, Max : real );
  111. begin
  112.      Tag.Init( ATag );
  113.      Value := AValue;
  114.      MaxVal := Max;
  115.      ZeroVal := Min;
  116.      Slope := (Max-Min)/(MaxR-MinR);
  117. end;
  118.  
  119. procedure Analog.PutValue( AValue : real );
  120. begin
  121.      if AValue > MaxVal then
  122.         AVAlue := MaxVal
  123.      else
  124.         if AValue < ZeroVal then
  125.            AValue := ZeroVal;
  126.      Value := Round((AValue - ZeroVal)/Slope);
  127. end;
  128.  
  129. function Analog.GetValue : real;
  130. begin
  131.      GetValue := Slope*Value + ZeroVal;
  132. end;
  133.  
  134.  
  135. procedure DInput.PutSetpoint( NewSetpoint : real );
  136. begin
  137.      Setpoint := NewSetpoint;
  138. end;
  139.  
  140. procedure LoSwitch.Init( ATag : string;
  141.                          ASetpoint : real;
  142.                          AReading : real);
  143. begin
  144.      Tag.Init( ATag );
  145.      DInput.PutSetpoint( ASetpoint );
  146.      PutReading( AReading );
  147. end;
  148.  
  149. procedure LoSwitch.PutReading( NewReading : real );
  150. begin
  151.      Reading := NewReading;
  152.      if Reading <= Setpoint then
  153.         Status := true
  154.      else
  155.         Status := false;
  156. end;
  157.  
  158. procedure HiSwitch.Init( ATag : string;
  159.                          ASetpoint : real;
  160.                          AReading : real);
  161. begin
  162.      Tag.Init( ATag );
  163.      DInput.PutSetpoint( ASetpoint );
  164.      PutReading( AReading );
  165. end;
  166.  
  167. procedure HiSwitch.PutReading( NewReading : real );
  168. begin
  169.      Reading := NewReading;
  170.      if Reading >= Setpoint then
  171.         Status := true
  172.      else
  173.         Status := false;
  174. end;
  175.  
  176. { HtToPSI converts a height (of a column of water) into a pressure.
  177.   The math is pretty simple:  A column of water 2.31 feet high exerts
  178.   a force of one pound per square inch at the bottom. }
  179.  
  180. function HtToPSI( Height : real ) : real;
  181. begin
  182.      HtToPSI := Height/2.31;
  183. end;
  184.  
  185. { FlowToDeltaHt converts a flow into (or out of) our system into a
  186.   change in height in the tank.
  187.   The math is as follows:  Divide the flow, in gpm, by 7.48 gal/cu.ft.
  188.   to get the number of cubic feet pumped per minute.  Divide this
  189.   number by the volume of 1 vertical foot of the tank, which is
  190.   the radius squared times 'pi' (15*15*3.1416). }
  191.  
  192. function FlowToDeltaHt( Flow : real ) : real;
  193. begin
  194.      FlowToDeltaHt := Flow/(7.48 * 706) ;
  195. end;
  196.  
  197. var
  198.    LT100,
  199.    FT100  : Analog;
  200.    PSL100 : LoSwitch;
  201.    PSH100 : HiSwitch;
  202.    EY100  : Pump;
  203.    time   : integer;
  204.  
  205. begin
  206.  
  207.      { If no file name is passed to the program, then ParamStr(1)
  208.        will be the null string, which will cause Assign to output
  209.        to the standard output (the screen).  If a file name is
  210.        passed, the output will be saved in the file for later
  211.        reference. }
  212.  
  213.      Assign( f, ParamStr(1) );
  214.      Rewrite( f );
  215.  
  216.      LT100.Init( 'LT100', 512, 50, 250 );
  217.      FT100.Init( 'FT100', 2048, 0, 10000 );
  218.      PSL100.Init( 'PSL100', 60, HtToPSI(LT100.GetValue) );
  219.      PSH100.Init( 'PSH100', 75, HtToPSI(LT100.GetValue) );
  220.      EY100.Init( 'EY100', off, 8000);
  221.      time := 0;
  222.  
  223.      repeat
  224.  
  225.      Inc( time );  { increment the time }
  226.      { First, adjust the reading in LT-100 by reducing the level in
  227.        LT100.Value by an amount corresponding to the flow out of the
  228.        system.  We do this by fetching the value of FT100, converting
  229.        it to a height in the tank, and subtracting from the actual
  230.        height... }
  231.      LT100.PutValue( LT100.GetValue - FlowToDeltaHt(FT100.GetValue) );
  232.  
  233.      { We take the result and update the switches }
  234.      PSL100.PutReading( HtToPSI(LT100.GetValue) );
  235.      PSH100.PutReading( HtToPSI(LT100.GetValue) );
  236.  
  237.      {Let's publish the level in the tank...}
  238.      writeln( f, 'Level in tank is ', LT100.GetValue:2:2, ' feet at minute ', time );
  239.  
  240.      { If the pressure is too low, turn on the pump }
  241.      if PSL100.GetStatus = on then
  242.         EY100.PutStatus( ON );
  243.      { If the pressure is too high, turn off the pump }
  244.      if PSH100.GetStatus = on then
  245.         EY100.PutStatus( OFF );
  246.  
  247.      LT100.PutValue( LT100.GetValue + FlowToDeltaHt( EY100.Flow ));
  248.  
  249.      until KeyPressed;
  250.      Close ( f );
  251. end.
  252.  
  253.  
  254.  
  255.  
  256.  
  257.  
  258.  
  259.  
  260.  
  261.  
  262.  
  263.