Forth & Real-Time Control
by William M. Stein


Listing One 
U(i) = .98 U(i-1) + .01 [ X(i) + x(i-1)] 

Listing Two 
: FILTER.LOWER ( THE BIT COUNT IS SAVED IN ARRAY U AND IS X100 )
8 0 DO I U-1 + @ 98 100 */ 
I X-1 + @ I 2 / 
ADR1 + C@ DUP I 
1 + X + C! 
DUM C@ I X + C! 
+ + I U + ! 2 +LOOP ; 

Listing Three
: LOWER.HISTORY 
8 0 DO 
      I U + @ I U-1 + ! 
      I X + @ I X-1 + ! 
      2 
    +LOOP ; 

Listing Four
: SCAN.SENSORS ( INCLUDES FILTERING )
  SCAN-1-TO-4 
  DONE? 
  FILTER.LOWER 
  LOWER.HISTORY 
  SCAN-5-TO-8 
  DONE? 
  FILTER.UPPER 
  UPPER.HISTORY ; 

Listing Five
: OVER.TEMPERATURE ( -- F )
  PORT.A C@ 1 AND 
  TEMPERATURE.MASK @ 
  AND 1 = DUP 
  IF 
    ." Excess temperature fault " 
    CRLF PORT.A C@ .  
    CRLF 20 PORT.C C! 
  THEN ; 

Listing Six
: AUTO 
  ?FAULTS IF 
             MASK.INTERRUPT 
             SHUT.DOWN ABORT 
          ELSE 
             RELEASE.T.BRAKE 
             RELEASE.P.BRAKE 
             ?GENERATOR.COMMANDED.ON 
             IF 
                 MASK.INTERRUPT ." INT.  MASKED " 
                 CRLF 
             THEN 
             ?MOTORING 
             IF 
                 DISCONNECT 
                 CLEAR.INTERRUPT.MASK 
                 ." UNMASKED " 
                 CRLF 
             THEN 
          THEN ; 
: DETERMINE.MODE ( LOOK AT ROTARY SW CONNECTED TO PA1 ) 
                 ( 00=OFF 01=AUTO ) 
  PORT.A C@ 2 / ( DO A RIGHT SHIFT TO TOSS OUT PA0 ) 
  1 AND DUP ( MASK OUT ALL BUT THE LSB ) 
  0 = IF 
        DROP OFF ." Off " CRLF 
      ELSE DUP 1 = 
           IF DROP AUTO ." Auto " CRLF
           ELSE ." Mode switch fault " CRLF ABORT 
           THEN 
      THEN ; 
: CONTROL.TURBINE ( THE MAIN WORD ) 
  MASK.INTERRUPT 
  0 DUM ! 
  SHUT.DOWN
  CLEAN.SLATE ( SET ALL THE FILTERED VARS INITIALLY TO 0 ) 
  EMIT.STATUS 
  0 PACTL C! ( MAKE PA7 AN INPUT ) 
  255 DDR.C C! ( MAKE C AN OUTPUT PORT ) 
  CLEAR.FAULT.LIGHTS 
  10000 MSEC ( GIVE THE ELEC, ESP PLL10 SEC TO STABILIZE ) 
  300 0 DO 
          SCAN.SENSORS 
        LOOP ( LOAD UP THE FILTERS ) 
  EMIT.STATUS ( LET US KNOW WHERE ITS AT ) 
  CLEAR.INTERRUPT.MASK ( SHOULD BE READY TO GO ! ) 
  BEGIN 
     DETERMINE.MODE 
     ?TERMINAL ( ESCAPE TO FORTH INTERPRETER ) 
  UNTIL 
  MASK.INTERRUPT ( OPERATOR SHUT IT DOWN, DON'T LET SYNC ) 
  ABORT ;

2


