%%%
  VERSION: 1
  LANGUAGE: ENGLISH
%%%

MODULE SWSUP (NOVIEW)

!---------------------------------------
!  G L O B A L  D A T A
!--------------------------------------- 

  ! Description: 
  ! CONST string Version_XXXXXXXX := "1.2";
  !
  !          XXXXXXXX - Module name
  !                 1 - version number  (changed of SEROP) 
  !                 2 - revision number (changed of SEROP)
  CONST string Version_SWSUP := "4.1";

!---------------------------------------
!  L O C A L  D A T A
!---------------------------------------

  ! The size of the arrays should be equal to
  ! the variable SW_NOF_GUNS, the number
  ! of gun equipments used in the system.
  LOCAL VAR bool forever := TRUE;
  LOCAL VAR intnum imotor_on;
  LOCAL VAR intnum imotor_off;
  LOCAL VAR intnum iproc_ok{2};
  LOCAL VAR intnum iproc_error{2};
  LOCAL VAR intnum icurr_enable{2};
  LOCAL VAR intnum icurr_disable{2};

  LOCAL VAR signaldo process_fault{2};
  LOCAL VAR signaldo enable_curr{2};
  LOCAL VAR signaldo start_water{2};
  LOCAL VAR signaldo weld_power{2};

!---------------------------
!  M A I N   R O U T I N E
!---------------------------

  ! Main routine.
  PROC main()

    DefSupIO;
    SupInit;

    WHILE forever = TRUE DO
      WaitTime 10;
    ENDWHILE

  ENDPROC

!----------------------------------------------------
!  S U P E R V I S I O N  T A S K  R O U T I N E S
!----------------------------------------------------

  ! Define the supervision signals used for all gun equipments.
  ! e.g. AliasIO g1_process_fault, process_fault{1};
  PROC DefSupIO()

    AliasIO g1_process_fault, process_fault{1};
    AliasIO g2_process_fault, process_fault{2};
    AliasIO g1_enable_curr, enable_curr{1};
    AliasIO g2_enable_curr, enable_curr{2};
    AliasIO g1_start_water, start_water{1};
    AliasIO g2_start_water, start_water{2};
    AliasIO g1_weld_power, weld_power{1};
    AliasIO g2_weld_power, weld_power{2};

  ENDPROC

  ! Called on power-on, perform here all
  ! initialisations you need, e.g.
  ! CONNECT myInterrupt WITH myTrapRoutine;
  ! IsignalDO mySignal, 1, myInterrupt;
  PROC SupInit ()

    CONNECT imotor_on WITH MotorState;
    CONNECT imotor_off WITH MotorState;
    IsignalDO motor_on, 1, imotor_on;
    IsignalDO motor_on, 0, imotor_off;

    FOR i FROM 1 TO SW_NOF_GUNS DO
      CONNECT iproc_ok{i} WITH ProcState;
      CONNECT iproc_error{i} WITH ProcState;
      CONNECT icurr_enable{i} WITH ProcState;
      CONNECT icurr_disable{i} WITH ProcState;
      IsignalDO process_fault{i}, 0, iproc_ok{i};
      IsignalDO process_fault{i}, 1, iproc_error{i};
      IsignalDO enable_curr{i}, 1, icurr_enable{i};
      IsignalDO enable_curr{i}, 0, icurr_disable{i};
    ENDFOR

  ENDPROC

!------------------------------------------------
!  E V E N T  D R I V E N  S U P E R V I S I O N
!------------------------------------------------

  ! Called in motors on state.
  PROC SwMotorOn ()

    FOR i FROM 1 TO SW_NOF_GUNS DO
      IF DOutput(process_fault{i}) = 0 AND DOutput(enable_curr{i}) = 1 THEN
        SetDO weld_power{i}, 1;
        TurnOnWater i;
      ELSE
        SetDO weld_power{i}, 0;
      ENDIF
    ENDFOR

  ENDPROC

  ! Called in motors off state.
  PROC SwMotorOff ()

    FOR i FROM 1 TO SW_NOF_GUNS DO
      SetDO weld_power{i}, 0;
    ENDFOR

  ENDPROC

  ! Called if process is OK.
  PROC SwProcOk (num GunNum)

    IF DOutput(motor_on) = 1 AND DOutput(enable_curr{GunNum}) = 1 THEN
      SetDO weld_power{GunNum}, 1;
      TurnOnWater GunNum;
    ELSE
      SetDO weld_power{GunNum}, 0;
    ENDIF

  ENDPROC

  ! Called if an error has occurred.
  PROC SwProcError (num GunNum)

    SetDO weld_power{GunNum}, 0;

  ENDPROC

  ! Called if the enable_curr signal is set.
  PROC SwSupCurrEn (num GunNum)

    IF DOutput(motor_on) = 1 AND DOutput(process_fault{GunNum}) = 0 THEN
      SetDO weld_power{GunNum}, 1;
      TurnOnWater GunNum;
    ELSE
      SetDO weld_power{GunNum}, 0;
    ENDIF

  ENDPROC

  ! Called if the enable_curr signal is reset.
  PROC SwSupCurrDis (num GunNum)

    SetDO weld_power{GunNum}, 0;

  ENDPROC

!---------------------------------------
!  L O C A L  R O U T I N E S
!---------------------------------------

  LOCAL PROC TurnOnWater(num GunNum)

    PulseDO \High\PLength:=2.0, start_water{GunNum};

  ENDPROC

!---------------------------------------
! T R A P  R O U T I N E S
!---------------------------------------

  TRAP MotorState

    TEST INTNO
    CASE imotor_on:
      SwMotorOn;
    CASE imotor_off:
      SwMotorOff;
    ENDTEST
 
  ENDTRAP

  TRAP ProcState

    FOR i FROM 1 TO SW_NOF_GUNS DO
      TEST INTNO
      CASE iproc_ok{i}:
        SwProcOk i;
      CASE iproc_error{i}:
        SwProcError i;
      CASE icurr_enable{i}:
        SwSupCurrEn i;
      CASE icurr_disable{i}:
        SwSupCurrDis i;
      ENDTEST
    ENDFOR
 
  ENDTRAP

ENDMODULE
