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

MODULE SWUSRC (SYSMODULE)

! ----------------
! $Revision: 1.12 $ 
! ----------------
!

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

  CONST string Version_SWUSRC := "1.0.0";
! Description: 
! CONST string Version_XXXXXXXX := "1.2.3";
!
!          XXXXXXXX - Module name
!                 1 - version number  (changed of SEROP) 
!                 2 - revision number (changed of SEROP)
!                 3 - revision number (for external use)

  PERS gunmdata sw_gun{4} :=
   [[TRUE, 0, 0, 1, 4, TRUE, TRUE, 0.100, 0.100, 0.100, 0.100, 0],
   [TRUE, 0, 0, 1, 4, TRUE, TRUE, 0.100, 0.100, 0.100, 0.100, 0],
   [TRUE, 0, 0, 1, 4, TRUE, TRUE, 0.100, 0.100, 0.100, 0.100, 0],
   [TRUE, 0, 0, 1, 4, TRUE, TRUE, 0.100, 0.100, 0.100, 0.100, 0]]; 
  
  PERS num sw_gp_timeout := 2;
  PERS num sw_wr_timeout := 2;
  PERS num sw_go_timeout := 2;
  PERS num sw_sim_time := 0.5;
  PERS num sw_aut_reweld := 0;
  PERS num sw_prog_max := 63;
  PERS num sw_parity:= 0;
  PERS num sw_reset_time1 := 0.5;
  PERS num sw_reset_time2 := 0.5;
  PERS num sw_start_type:= 0;
  PERS bool sw_user_recover := FALSE;
  PERS bool sw_inhib_weld := FALSE;
  PERS bool sw_sim_weld := FALSE;
  PERS bool sw_async_weld := FALSE;
  PERS bool sw_forced_seq := FALSE;
  PERS bool sw_ext_hooks := FALSE;
  
  VAR signaldo p2_req{4};
  VAR signaldo p3_req{4};
  VAR signaldo p4_req{4};
  VAR signaldo close_tip1{4};
  VAR signaldo close_tip2{4};
  VAR signaldo work_select{4};
  VAR signalgo pressure_group{4};
  VAR signaldi p1_ok;
  VAR signaldi p2_ok;
  VAR signaldi p3_ok;
  VAR signaldi p4_ok;
  VAR signaldi tip1_open;
  VAR signaldi tip2_open;          
  
!---------------------------------------
! L O C A L  D A T A
!---------------------------------------

  LOCAL VAR intnum imotor_on;
  LOCAL VAR intnum imotor_off;
  
  LOCAl VAR spotmdata current_spot; 
  
  
!---------------------------------------
! I O  I N I T  R O U T I N E S
!---------------------------------------


!  Besides the I/O configuration only 
!  these routines has to be changed if
!  other signal names are used. 

!
! sw_init_outputs is called after power on from the MAIN Task.
!
PROC sw_init_outputs()

! Define signals for gun equipment 1:
  SwDefStartSig 1\DOStart1:=g1_start1\DOStart2:=g1_start2;
  SwDefPrognoSig 1\GOProgNo:=g1_prog_no_group\DOProgParity:=g1_prog_parity;
  SwDefProcSig 1\DOProcError:=g1_process_error\DOWeldError:=g1_weld_error\DOProcRun:=g1_proc_run
               \DOcurr_enable:=current_enable\DOreset_fault:=reset_fault;  
! Define signals for gun equipment 2 (if used):  
  SwDefStartSig 2\DOStart1:=g2_start1\DOStart2:=g2_start2;
  SwDefPrognoSig 2\GOProgNo:=g2_prog_no_group\DOProgParity:=g2_prog_parity;
  SwDefProcSig 2\DOProcError:=g2_process_error\DOWeldError:=g2_weld_error\DOProcRun:=g2_proc_run
               \DOcurr_enable:=current_enable\DOreset_fault:=reset_fault;
! Define signals for gun equipment 3 (if used):  
!  SwDefStartSig 3\DOStart1:=g3_start1\DOStart2:=g3_start2;
!  SwDefPrognoSig 3\GOProgNo:=g3_prog_no_group\DOProgParity:=g3_prog_parity;
!  SwDefProcSig 3\DOProcError:=g3_process_error\DOWeldError:=g3_weld_error\DOProcRun:=g3_proc_run
!               \DOcurr_enable:=current_enable\DOreset_fault:=reset_fault;
! Define signals for gun equipment 4 (if used):  
!  SwDefStartSig 4\DOStart1:=g4_start1\DOStart2:=g4_start2;   
!  SwDefPrognoSig 4\GOProgNo:=g4_prog_no_group\DOProgParity:=g4_prog_parity;
!  SwDefProcSig 4\DOProcError:=g4_process_error\DOWeldError:=g4_weld_error\DOProcRun:=g4_proc_run
!               \DOcurr_enable:=current_enable\DOreset_fault:=reset_fault;
  
  
ENDPROC    
  

!
! sw_init_outputs2 is called from the different process tasks and from
! the Start shelf routine in task MAIN.
!
PROC sw_init_outputs2()

  AliasIO g1_p2_req,p2_req{1};
  AliasIO g2_p2_req,p2_req{2};
  
  AliasIO g1_p3_req,p3_req{1};
  AliasIO g2_p3_req,p3_req{2};
  
  AliasIO g1_p4_req,p4_req{1};
  AliasIO g2_p4_req,p4_req{2};
  
  AliasIO g1_close_tip1,close_tip1{1};
  AliasIO g2_close_tip1,close_tip1{2}; 
  
  AliasIO g1_close_tip2,close_tip2{1};
  AliasIO g2_close_tip2,close_tip2{2};
  
  AliasIO g1_work_select,work_select{1};
  AliasIO g2_work_select,work_select{2};
    
  AliasIO g1_press_group,pressure_group{1};
  AliasIO g2_press_group,pressure_group{2}; 
  
ENDPROC  

!
! sw_init_inputs is called after power on from the MAIN Task.
!  
PROC sw_init_inputs(num eqid)

  TEST eqid
  CASE 1:
    SwDefReadySig \DIWeldready1:=g1_weld_ready1\DIWeldready2:=g1_weld_ready2;
  CASE 2:
    SwDefReadySig \DIWeldready1:=g2_weld_ready1\DIWeldready2:=g2_weld_ready2;
  ENDTEST
ENDPROC  
  
!
! sw_init_inp1 is called before the pressure inputs are used.
!
LOCAL PROC sw_init_inp1(num eqid)

  TEST eqid
  CASE 1:
    AliasIO g1_p1_ok,p1_ok;
    AliasIO g1_p2_ok,p2_ok;
    AliasIO g1_p3_ok,p3_ok;
    AliasIO g1_p4_ok,p4_ok;
  CASE 2:
    AliasIO g2_p1_ok,p1_ok;
    AliasIO g2_p2_ok,p2_ok;
    AliasIO g2_p3_ok,p3_ok;
    AliasIO g2_p4_ok,p4_ok;
  ENDTEST
ENDPROC 

!
! sw_init_inp2 is called before the gun inputs are used.
!
LOCAL PROC sw_init_inp2(num eqid)

  TEST eqid
  CASE 1:
    AliasIO g1_tip1_open,tip1_open;
    AliasIO g1_tip2_open,tip2_open;
  CASE 2:
    AliasIO g2_tip1_open,tip1_open;
    AliasIO g2_tip2_open,tip2_open;
  ENDTEST
ENDPROC  

!---------------------------------------
! G U N   R O U T I N E S
!---------------------------------------

! sw_set_pressure:
! This routine is called at the beginning of
! the motion to the weld position.
! User defined code:
PROC sw_set_pressure(num eqid)

  current_spot := SwGetCurrSpot(eqid);
  
  IF sw_gun{eqid}.equip_act = TRUE THEN
    TEST current_spot.gun_pressure
    CASE 1:
      SetGO pressure_group{eqid},0;
    CASE 2:
      SetGO pressure_group{eqid},1;
    CASE 3:
      SetGO pressure_group{eqid},2;
    CASE 4:
      SetGO pressure_group{eqid},4;
    ENDTEST
  ENDIF
  
ENDPROC


! sw_close_gun:
! User defined code:
PROC sw_close_gun (num eqid)
  
  IF sw_gun{eqid}.equip_act = TRUE THEN
  
    IF SwGetCurrCRetr () = FALSE THEN 
      Set work_select{eqid};     
    ENDIF
    
    TEST current_spot.tip_no
    CASE 1:
      Set close_tip1{eqid};
    CASE 2:
      Set close_tip2{eqid};
    CASE 12,21:
      Set close_tip1{eqid};
      Set close_tip2{eqid};
    CASE 0:  
    DEFAULT:
    ENDTEST
  ENDIF
ENDPROC


! sw_check_close:
! if anything goes wrong, just set an
! error message in errtext_close
! User defined code:
PROC sw_check_close (num eqid, VAR string errtext_close)
  VAR bool timeout;
  
  IF sw_gun{eqid}.equip_act = TRUE THEN
    IF sw_gun{eqid}.close_request = TRUE THEN
      sw_init_inp1 eqid;
      TEST current_spot.gun_pressure
      CASE 0:
      CASE 1:
	WaitDI p1_ok, 1\MaxTime:=sw_gp_timeout\TimeFlag:=timeout;
      CASE 2:
	WaitDI p2_ok, 1\MaxTime:=sw_gp_timeout\TimeFlag:=timeout;
      CASE 3:
	WaitDI p3_ok, 1\MaxTime:=sw_gp_timeout\TimeFlag:=timeout;
      CASE 4:
	WaitDI p4_ok, 1\MaxTime:=sw_gp_timeout\TimeFlag:=timeout;
      DEFAULT:
        errtext_close := "Non existent gun pressure";
      ENDTEST
      IF timeout = TRUE THEN
	TEST eqid
        CASE 1:
	  errtext_close := "Gun equipment 1: Pressure not reached";
        CASE 2:
	  errtext_close := "Gun equipment 2: Pressure not reached";
        CASE 3:
	  errtext_close := "Gun equipment 3: Pressure not reached";
        CASE 4:
	  errtext_close := "Gun equipment 4: Pressure not reached";
        DEFAULT:
          errtext_close := "Non existent eqid";
	ENDTEST  
      ENDIF
    ENDIF
  ENDIF
ENDPROC


! sw_open_gun:
! User defined code:
PROC sw_open_gun (num eqid)

  sw_init_outputs2;

  Reset close_tip1{eqid};
  Reset close_tip2{eqid};
  
  IF SwGetCurrRetr () = FALSE THEN 
     Set work_select{eqid};     
  ELSE  
     Reset work_select{eqid};     
  ENDIF
  
ENDPROC


! sw_check_open:
! if anything goes wrong, just set an
! error message in errtext_open.
! User defined code:  
PROC sw_check_open (num eqid, VAR string errtext_open)
  VAR bool timeout1;
  VAR bool timeout2;
  
  WaitTime sw_gun{eqid}.open_time;
  
  IF sw_gun{eqid}.open_request = TRUE THEN
    sw_init_inp2 eqid;
    WaitDI tip1_open, 1\MaxTime:=sw_go_timeout\TimeFlag:=timeout1;
    IF sw_gun{eqid}.nof_tips = 2 THEN
      WaitDI tip2_open, 1\MaxTime:=sw_go_timeout\TimeFlag:=timeout2;
    ENDIF
    IF timeout1 = TRUE OR timeout2 = TRUE THEN
      TEST eqid
      CASE 1:
	errtext_open := "Gun equipment 1: Open timeout";
      CASE 2:
	errtext_open := "Gun equipment 2: Open timeout";
      CASE 3:
	errtext_open := "Gun equipment 3: Open timeout";
      CASE 4:
	errtext_open := "Gun equipment 4: Open timeout";
      DEFAULT:
        errtext_open := "Non existent eqid";
      ENDTEST  	 
    ENDIF
  ENDIF
ENDPROC

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

! sw_preweld_sup:
! if anything goes wrong, just set an
! error message in errtext.
! User defined code:
PROC sw_preweld_sup(num eqid, VAR string errtext)

  IF sw_inhib_weld=FALSE AND sw_sim_weld=FALSE THEN
    IF timer_ready = 0 THEN
      Set reset_fault;
      WaitTime sw_reset_time1;
      Reset reset_fault;
      WaitTime sw_reset_time2;
    ENDIF
    IF timer_ready = 0 THEN
      errtext := "Timer not ready";
    ENDIF
    IF flow_ok = 0 THEN
      errtext := "Water supply error";
    ENDIF
    IF temp_ok = 0 THEN
      errtext := "Overtemperature";
    ENDIF
    IF current_ok = 0 THEN
      errtext := "Weld current error";
    ENDIF
  ENDIF
  
ENDPROC


! sw_postweld_sup:
! if anything goes wrong, just set an
! error message in errtext
PROC sw_postweld_sup(num eqid, VAR string errtext)
!User defined code:

ENDPROC




!----------------------------------------------------
! U S E R  D E F I N E D  E R R O R  R E C O V E R
!----------------------------------------------------

! sw_error_recover:
! This routine is used if sw_user_recover = TRUE.
! Return values: /SW_CANCEL/SW_RETRY/SW_SERVICE/SW_SKIP
PROC sw_error_recover (num eqid, num error_type, string err_text, VAR num status)
  VAR num input_key; 

  TPErase;

  WaitUntil DOutput (i_cycle_on) = 1;
  TEST error_type
  CASE SW_WELD_ERR:
    TPReadFK input_key, "USER WELD FAULT", "", "", "", "SKIP", "REWELD";
			  
  CASE SW_CG_ERR:
    TPReadFK input_key, err_text, "", "", "", "SKIP", "RETRY";

  CASE SW_OG_ERR:
    TPReadFK input_key, err_text, "", "", "", "SKIP", "RETRY";
    
  CASE SW_PRESUP_ERR:
    TPReadFK input_key, err_text, "", "", "", "SKIP", "RETRY";
    
  CASE SW_POSTSUP_ERR:
    TPReadFK input_key, err_text, "", "", "", "SKIP", "RETRY";
    
  DEFAULT:
  ENDTEST
  
  TEST input_key
  CASE 4:
    status := SW_SKIP;
  CASE 5:
    status := SW_RETRY;
  DEFAULT:
  ENDTEST
  TPErase;
ENDPROC


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

!sw_sup_init:
!  Called on power-on; perform here all
!  initialisations you need; as an example:
!  CONNECT myInterrupt WITH myTrapRoutine;
!  IsignalDI mySignal, 1, myInterrupt;
PROC sw_sup_init ()
  CONNECT imotor_on WITH sw_sup_trap;
  CONNECT imotor_off WITH sw_sup_trap;
  IsignalDO motor_on, 1, imotor_on;
  IsignalDO motor_on, 0, imotor_off;
ENDPROC


PROC sw_motor_on ()
  SetDO weld_power, 1;
ENDPROC


PROC sw_motor_off ()
  SetDO weld_power, 0;
ENDPROC

TRAP sw_sup_trap
  TEST INTNO
  CASE imotor_on:
    sw_motor_on;
  CASE imotor_off:
    sw_motor_off;
  ENDTEST
ENDTRAP



!---------------------------------------
!  E X T E N D E D   U S E R   H O O K S
!---------------------------------------

! These user hooks are not activated in the default setup.
! They are only activated if sw_ext_hooks = TRUE.


! sw_event2:
! Called at the second time event of the motion.
! The event time is calculated in routine sw_event2_time
! in module SWUSRF.
! Return values: SW_OK/SW_CANCEL/SW_EVT1/SW_EVT2
PROC sw_event2 (num eqid, VAR num Status)
		     
  status := SW_OK;
  
ENDPROC	


! sw_event3:
! Called at the third time event of the motion.
! The event time is calculated in routine sw_event3_time
! in module SWUSRF.
! Return values: SW_OK/SW_CANCEL/SW_EVT1/SW_EVT2/SW_EVT3
PROC sw_event3 (num eqid, VAR num Status)
		     
  status := SW_OK;
  
ENDPROC	


! sw_startp:
! Called before the start signal is set by the kernal.
! Return values: SW_OK/SW_CANCEL/SW_EVT1/SW_EVT2/SW_EVT3/SW_START
PROC sw_startp (num eqid, VAR num Status)
		     
  status := SW_OK;
  
ENDPROC

ENDMODULE
