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

MODULE SWUSRC (SYSMODULE)

! ----------------
! $Revision: 1.12 $ 
! ----------------
! Copyright (c) ABB Robotics Products AB 1995  All rights reserved
!

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

CONST string Version_SWUSRC := "4.2.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 gundata man_gun := [1, 4, TRUE, TRUE, 0, 0, 20000, 20000,
                       0.050, 0.050, 0.050, 0.050, 0, 0, 0, 0, 0];
  PERS spotdata man_spot := [1, 1, 1, 1];
  PERS num man_retr := 0;

  PERS num sw_weld_counter := 0;

  PERS num sw_gp_timeout := 2;
  PERS num sw_wr_timeout := 2;
  PERS num sw_go_timeout := 2;
  PERS bool sw_inhib_weld := FALSE;
  PERS bool sw_sim_weld := FALSE;
  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 bool sw_async_weld := FALSE;
  PERS num sw_service_rqst := 0;
  PERS string errtext := "";
  PERS string errtext_close := "";
  PERS string errtext_open := "";
  PERS num sw_servo_corr := 0.060;
  PERS num sw_start_type:= 0;
  PERS bool sw_user_recover := FALSE;
  PERS bool sw_spot_id_req := FALSE;
  PERS bool sw_noconc_req := FALSE;

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

  LOCAL VAR intnum imotor_on;
  LOCAL VAR intnum imotor_off;
  LOCAL VAR intnum iproc_ok;
  LOCAL VAR intnum iproc_error;
  LOCAL VAR intnum icurr_enable;
  LOCAL VAR intnum icurr_disable;

!---------------------------------------
!      PROCESS TASK USER ROUTINES
!---------------------------------------

! user defined process actions
!
PROC sw_set_pressure()
  VAR spotdata spot;

  spot := SwGetCurrSpot ();
  TEST spot.gun_pressure
  CASE 1:
    SetGO pressure_group,0;
  CASE 2:
    SetGO pressure_group,1;
  CASE 3:
    SetGO pressure_group,2;
  CASE 4:
    SetGO pressure_group,4;
  ENDTEST

ENDPROC

PROC sw_close_gun ()
! if anything goes wrong, just set an
! error message in errtext_close
!User defined code:
  VAR bool err:= FALSE;
  VAR spotdata spot;
  VAR gundata gun;
  VAR num status := SW_OK;

  spot := SwGetCurrSpot ();
  gun := SwGetCurrGun ();

  TEST spot.tip_no
  CASE 1:
    Set close_tip1;
  CASE 2:
    Set close_tip2;
  CASE 12:
    Set close_tip1;
    Set close_tip2;
  CASE 21:
    Set close_tip1;
    Set close_tip2;
  DEFAULT:
    errtext_close := "Non existent tip no";
    err := TRUE;
  ENDTEST

  IF err = FALSE THEN
    IF gun.close_request = TRUE THEN
      TEST spot.gun_pressure
      CASE 1:
        status := SwWaitInput (p1_ok, 1 \MaxWait:=sw_gp_timeout);
      CASE 2:
        status := SwWaitInput (p2_ok, 1 \MaxWait:=sw_gp_timeout);
      CASE 3:
        status := SwWaitInput (p3_ok, 1 \MaxWait:=sw_gp_timeout);
      CASE 4:
        status := SwWaitInput (p4_ok, 1 \MaxWait:=sw_gp_timeout);
      DEFAULT:
        errtext_close := "Non existent gun pressure";
      ENDTEST
      IF status = SW_TIMOUT errtext_close := "Gun pressure not reached";
    ENDIF
  ENDIF
ENDPROC

PROC sw_open_gun (num context)
!parameters: context: >= 0 Normal
!                     < 0  Quick (no check, return value is ignored)
! if anything goes wrong, just set an
! error message in errtext_open
!User defined code:

  VAR gundata gun;
  VAR num status := SW_OK;
  VAR num status2 := SW_OK;  

  gun := SwGetCurrGun ();

  Reset close_tip1;
  Reset close_tip2;

  IF (context >= 0) THEN
    WaitTime gun.open_time;

    IF gun.open_request = TRUE THEN
      status := SwWaitInput (tip1_open, 1 \MaxWait:=sw_go_timeout);
      IF gun.nof_tips = 2 THEN
        status2 := SwWaitInput (tip2_open, 1 \MaxWait:=sw_go_timeout);
      ENDIF
      IF status = SW_TIMOUT OR status2 = SW_TIMOUT errtext_open := "Gun open timeout";
    ENDIF
  ENDIF

  IF SwGetCurrRetr () = 0 THEN
    Set work_select;
  ELSE
    Reset work_select;
  ENDIF
ENDPROC

!---------------------------------------
!    SUPERVISION TASK USER ROUTINES
!---------------------------------------

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

! Return values: /SW_CANCEL/SW_RETRY/SW_SERVICE/SW_REWELD
FUNC num sw_error_recover (num error_type, string err_text)
  VAR num status := SW_RETRY;
  VAR num input_key; 
  VAR num status1;  

  TPErase;

  IF manual_action = FALSE status1 := SwWaitOutput (swb_cycle_on, 1);
  TEST error_type
  CASE SW_WELD_ERR:
    status := SwErrorAck (input_key, "USER WELD FAULT", "", "", "REWELD",
                          "SKIP", "");
  CASE SW_CG_ERR:
    status := SwErrorAck (input_key, err_text, "", "", "REWELD",
                          "SKIP", "RETRY");
  CASE SW_OG_ERR:
    status := SwErrorAck (input_key, err_text, "", "", "REWELD",
                          "SKIP", "RETRY");
  CASE SW_PRESUP_ERR:
    status := SwErrorAck (input_key, err_text, "", "", "REWELD",
                          "SKIP", "RETRY");
  CASE SW_POSTSUP_ERR:
    status := SwErrorAck (input_key, err_text, "", "", "REWELD",
                          "SKIP", "RETRY");
  DEFAULT:
  ENDTEST

  IF status = SW_CANCEL RETURN SW_CANCEL;
  TEST input_key
  CASE 3:
    RETURN SW_REWELD;
  CASE 4:
    RETURN SW_CANCEL;
  CASE 5:
    RETURN SW_RETRY;
  DEFAULT:
  ENDTEST
  TPErase;
  RETURN status;
ENDFUNC

! *************************************
!  pre-weld and post-weld supervision
! *************************************

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

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

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


! ****************************************
!  miscellaneaous event driven supervision
! ****************************************

PROC sw_motor_on ()
  IF DOutput(process_error) = 0 AND DOutput(current_enable) = 1 THEN
    SetDO weld_power, 1;
    turn_on_water;
  ELSE
    SetDO weld_power, 0;
  ENDIF
ENDPROC

! ***************************
PROC sw_motor_off ()
  SetDO weld_power, 0;
ENDPROC

! ***************************
PROC sw_proc_ok ()
  IF DOutput(motor_on) = 1 AND DOutput(current_enable) = 1 THEN
    SetDO weld_power, 1;
    turn_on_water;
  ELSE
    SetDO weld_power, 0;
  ENDIF
ENDPROC

! ***************************
PROC sw_proc_error ()
  SetDO weld_power, 0;
ENDPROC

! ***************************
PROC sw_sup_curren ()
  IF DOutput(motor_on) = 1 AND DOutput(process_error) = 0 THEN
    SetDO weld_power, 1;
    turn_on_water;
  ELSE
    SetDO weld_power, 0;
  ENDIF
ENDPROC

! ***************************
PROC sw_sup_currdis ()
  SetDO weld_power, 0;
ENDPROC

! ****************************************
!  local user routines and traps
! ****************************************
LOCAL PROC turn_on_water()
  PulseDO \PLength:=2.0, water_start;
ENDPROC

TRAP sw_sup_trap
  TEST INTNO
  CASE imotor_on:
    sw_motor_on;
  CASE imotor_off:
    sw_motor_off;
  CASE iproc_ok:
    sw_proc_ok;
  CASE iproc_error:
    sw_proc_error;
  CASE icurr_enable:
    sw_sup_curren;
  CASE icurr_disable:
    sw_sup_currdis;
  ENDTEST
ENDTRAP


ENDMODULE
