*-----------------------------------------------------------------------------
*  Toolchange Automatic Mode (double arm)
*-----------------------------------------------------------------------------
*  Module Description
*
*-----------------------------------------------------------------------------

#define /s    KL_tool_without_pocket                   K -1
#define /s    KL_tool_with_pocket                      K +1
#define /s    KL_absolute_with_software_limit_switch   K %010
#define /s    KL_absolute_without_software_limit_switch K %000
#define /s    KL_TC_magazine_1_index                   K +0
#define /s    KL_string_count                          K +3

#define /s    ML_2nd_TOOL_CALL_following               M
#define /s    ML_TC_arm_cw_stop                        M
#define /s    ML_TC_arm_ccw_stop                       M
#define /s    ML_TOOL_CALL_old                         M
#define /s    ML_stack_synchronize                     M
#define /s    ML_step_start                            M
#define /s    ML_TC_diagnosis                          M

#define /s    BL_TC_mag_axis_index                     B
#define /s    BL_WZ_mag_index                          B
#define /s    BL_step_T_change                         B
#define /s    BL_string_index                          B

#define /s    WL_line_dialog_file                      W

#include Mac_Lib.inc

EXTERN Absolute

GLOBAL TC_auto

;----------------------------
LBL TC_auto
;----------------------------

  LN     MG_TC_auto_active
  EMT

#if Middle_position_exchange = &active
  LN     PN_M4540_T_new_1st_if_pocket_fixed
  S      PN_M4540_T_new_1st_if_pocket_fixed
#else
  L      PN_M4540_T_new_1st_if_pocket_fixed
  R      PN_M4540_T_new_1st_if_pocket_fixed
#endif

  L      NP_M4521_TOOL_CALL_T_0_programmed
  A      NP_M4520_TOOL_CALL_2nd_strobe
  A      NP_M4073_strobe_TOOL_CALL
  A      MG_TC_T0_auto_manu_necessary
  S      PN_error_TOOL_CALL_T0_necessary
  EMT

  L      NP_M4073_strobe_TOOL_CALL
  IFT
    L      NP_W262_tool_pocket_number
    >=     KL_tool_with_pocket
    AN     NP_M4175_control_in_operation_flashes
    AN     MG_TC_automatic
    S      MG_TC_automatic
    IFT
      L      NP_W262_tool_pocket_number
      =      WG_TC_pocket_number
      L      NP_W264_tool_number
      =      WG_TC_auto_tool_number
    ENDI
  ENDI

  L      STG_TC_mag[KG_TC_magazine_1_index].WG_MP7261_TC_magazine_max_pocket
  >=     KL_tool_with_pocket
  A      MG_TC_manual
  A      NP_M4073_strobe_TOOL_CALL
  O      MG_TC_automatic
  A      MG_pulse_internal_stop
  S      MG_pocket_table_checking

  L      MG_pocket_table_selected
  A      MG_TC_standby_position
  AN     MG_TC_manual
  AN     MG_TC_automatic
  R      MG_pocket_table_checking

  L      MG_pocket_table_checking
  AN     MG_TC_manual
  AN     MG_TC_automatic
  AN     MG_TC_help
  A      MG_TC_standby_position
  =      PN_error_check_pocket_table

  L      MG_TC_automatic
  R      TS_TC_timeout
  S      TS_TC_timer_universal

#if CFG_TC_transport_carriage = "inactive"

  CASE   BG_step_toolchange
    CM     TC_auto_start               ;00
    CM     Tool_spindle_ok             ;01
    CM     Spi_full_empty              ;02
    CM     T_mag_start                 ;03
    CM     S_start                     ;04
    CM     Z_start                     ;05
    CM     Tool_lock_open              ;06
    CM     Z_in_TC_Pos                 ;07
    CM     XY_start                    ;08
    CM     T_status_read               ;09
    CM     Tool_lock_opened            ;10
    CM     Magazine_in_pos             ;11
    CM     T_pocket_put_out            ;12
    CM     T_pocket_time               ;13
    CM     S_in_TC_pos                 ;14
    CM     X_in_TC_Pos                 ;15
    CM     Y_in_TC_Pos                 ;16
    CM     Mag_full_empty              ;17
    CM     T_pocket_pos_out            ;18
    CM     T_arm_to_spindle            ;19
    CM     T_arm_on_spindle            ;20
    CM     T_gripper_close             ;21
    CM     T_gripper_closed            ;22
    CM     Tool_unclamping             ;23
    CM     Tool_unclamped              ;24
    CM     TC_auto_time                ;25
    CM     T_arm_put_out               ;26
    CM     Tnew_store                  ;27
    CM     T0_in_status                ;28
    CM     T_status_ok                 ;29
    CM     T_arm_pos_out               ;30
#if Middle_position_exchange = &active
    CM     Tool_Call_2nd               ;31
    CM     Tool_Call_wait              ;32
    CM     Jump_TC_arm                 ;33      ---> (1)
    CM     T_arm_to_middle             ;34
    CM     T_arm_in_middle             ;35
    CM     T_mag_start                 ;36
    CM     Magazine_in_pos             ;37
    CM     T_pocket_put_out            ;38
    CM     T_pocket_pos_out            ;39
    CM     Mag_empty                   ;40
#endif                                 ;
    CM     T_arm_to_cw_ccw             ;41 ;31  <--- (1)
    CM     T_arm_in_cw_ccw             ;42 ;32
    CM     T_arm_put_in                ;43 ;33
    CM     T_arm_pos_in                ;44 ;34
    CM     Tool_clamping               ;45 ;35
    CM     Tool_clamped                ;46 ;36
    CM     Tnew_in_status              ;47 ;37
    CM     T_status_ok                 ;48 ;38
    CM     T_arm_to_basic              ;49 ;39
    CM     T_standby_read              ;50 ;40
    CM     T_arm_on_basic              ;51 ;41
    CM     T_gripper_open              ;52 ;42
    CM     T_gripper_opened            ;53 ;43
    CM     T_pocket_in_out             ;54 ;44
    CM     T_pocket_pos_ok             ;55 ;45
    CM     Tool_lock_close             ;56 ;46
    CM     Tool_lock_closed            ;57 ;47
    CM     Spi_empty_full              ;58 ;48
    CM     TOOL_CALL_quit              ;59 ;49
    CM     TC_auto_end                 ;60 ;50
    CM     TC_auto_time                ;reserviert
  ENDC

#else

  CASE   BG_step_toolchange
    CM     TC_auto_start               ;00
    CM     Tool_spindle_ok             ;01
    CM     Spi_full_empty              ;02
    CM     T_mag_start                 ;03
    CM     S_start                     ;04
    CM     Z_start                     ;05
    CM     Tool_lock_open              ;06
    CM     Z_in_TC_Pos                 ;07
    CM     XY_start                    ;08
    CM     T_status_read               ;09
    CM     Tool_lock_opened            ;10
    CM     Magazine_in_pos             ;11
    CM     T_pocket_put_out            ;12
    CM     T_pocket_time               ;13
    CM     Mag_full_empty              ;14
    CM     T_pocket_pos_out            ;15
    CM     T_gri_mag_open              ;16
    CM     T_gri_mag_opened            ;17
    CM     T_arm_to_magazine           ;18
    CM     T_arm_on_magazine           ;19
    CM     T_gri_mag_close             ;20
    CM     T_gri_mag_closed            ;21
    CM     T_arm_put_out               ;22
    CM     T_arm_pos_out               ;23
    CM     T_arm_to_basic              ;24
    CM     T_arm_on_basic              ;25
    CM     T_arm_put_in                ;26
    CM     T_arm_pos_in                ;27
    CM     T_gri_spi_open              ;28
    CM     T_gri_spi_opened            ;29
    CM     S_in_TC_pos                 ;30
    CM     X_in_TC_Pos                 ;31
    CM     Y_in_TC_Pos                 ;32
    CM     Tnew_store                  ;33
    CM     T0_in_status                ;34
    CM     T_status_ok                 ;35
    CM     Tool_Call_2nd               ;36
    CM     Tool_Call_wait              ;37
    CM     Jump_TC_arm                 ;38    ---> (1)
    CM     T_mag_start                 ;39
    CM     T_arm_to_spindle            ;40   <---  (1)
    CM     T_arm_on_spindle            ;41
    CM     T_gri_spi_close             ;42
    CM     T_gri_spi_closed            ;43
    CM     Tool_unclamping             ;44
    CM     Tool_unclamped              ;45
    CM     TC_auto_time                ;46
    CM     T_arm_put_out               ;47
    CM     T_arm_pos_out               ;48
    CM     T_arm_to_cw_ccw             ;49
    CM     T_arm_in_cw_ccw             ;50
    CM     T_arm_put_in                ;51
    CM     T_arm_pos_in                ;52
    CM     Tool_clamping               ;53
    CM     Tool_clamped                ;54
    CM     Tnew_in_status              ;55
    CM     T_status_ok                 ;56
    CM     T_gri_spi_open              ;57
    CM     T_gri_spi_opened            ;58
    CM     T_arm_to_basic              ;59
    CM     T_standby_read              ;60
    CM     T_arm_on_basic              ;61
    CM     T_arm_put_out               ;62
    CM     T_arm_pos_out               ;63
    CM     Magazine_in_pos             ;64
    CM     T_pocket_put_out            ;65
    CM     T_pocket_pos_out            ;66
    CM     Mag_empty                   ;67
    CM     T_arm_to_magazine           ;68
    CM     T_arm_on_magazine           ;69
    CM     T_arm_put_in                ;70
    CM     T_arm_pos_in                ;71
    CM     T_gri_mag_open              ;72
    CM     T_gri_mag_opened            ;73
    CM     T_arm_to_basic              ;74
    CM     T_arm_on_basic              ;75
    CM     T_pocket_in_out             ;76
    CM     T_pocket_pos_ok             ;77
    CM     T_gri_spi_close             ;78
    CM     T_gri_mag_close             ;79
    CM     T_gri_spi_closed            ;80
    CM     T_gri_mag_closed            ;81
    CM     Tool_lock_close             ;82
    CM     Tool_lock_closed            ;83
    CM     Spi_empty_full              ;84
    CM     TOOL_CALL_quit              ;85
    CM     TC_auto_end                 ;86
    CM     TC_auto_time                ;reserviert
    CM     X_start                     ;reserviert
    CM     X_in_TC_Pos                 ;reserviert
  ENDC
#endif

  PLL
  S      TS_TC_timeout
  =      ML_step_start
  IFT
    INC    BG_step_toolchange
  ENDI

  L      BG_step_toolchange
  <>     BL_step_T_change
  ON     ML_TC_diagnosis
  A      MG_TC_diagnosis
  IFT
    L      K +0
    =      BL_string_index

    L      BG_step_toolchange
    =      BL_step_T_change
    REPEAT
      L      BL_step_T_change
      =X

      L      KF Diagnostic[X]
      =      WL_line_dialog_file
      =X

      L      S#D0[X]
      =      SG_diagnostics_line_1[BL_string_index]

      INC    BL_step_T_change
      INC    BL_string_index
      INCX

      L      BL_string_index
      >=     KL_string_count
    UNTILT
  ENDI

  L      BG_step_toolchange
  =      BL_step_T_change

  L      MG_TC_diagnosis
  =      ML_TC_diagnosis

  LN     MG_TC_automatic
  EMT

  L      MG_NC_start_pulse
  R      MG_TC_interruption

  L      MG_key_NC_start
  O      MG_pulse_CE_key
  S      TS_TC_timeout

  LN     TR_TC_timeout
  AN     TS_TC_timeout
  =      PN_error_TC_timeout
  S      MG_TC_interruption

  L      MG_key_NC_stop
  O      MG_pulse_external_stop
  S      MG_TC_interruption

  L      MG_pulse_internal_stop
  O      MG_auto_manu_mode_change
*$WS testen
  O      MG_guard_open
  ON     MG_control_operational
  CMT    TC_auto_reset

EM

#if CFG_TC_transport_carriage = "active"
;----------------------------
LBL Jump_TC_arm                        ;33

  PS     MG_marker_one

  LN     ML_2nd_TOOL_CALL_following
  O      NP_M4523_TOOL_CALL_without_pocket_number
  IFT
    L      BG_step_toolchange
    +      K+1
    =      BG_step_toolchange
  ENDI

EM

#else

#if Middle_position_exchange = &active
;----------------------------
LBL Jump_TC_arm                        ;33

  PS     MG_marker_one

  LN     ML_2nd_TOOL_CALL_following
  O      NP_M4523_TOOL_CALL_without_pocket_number
  IFT
    L      BG_step_toolchange
    +      K+7
    =      BG_step_toolchange
  ENDI

EM
#endif
#endif

;----------------------------
LBL TC_auto_start                      ;00

  LN     MG_TC_standby_position
  A      MG_TC_automatic
  S      PN_error_TC_basic_position
  S      MG_TC_interruption

  L      MG_TC_magazine_n_start_active
  S      TS_TC_timeout

  L      MG_TC_automatic
  AN     MG_TC_manual
  AN     MG_TC_magazine_n_start_active
  A      MG_TC_standby_position
  AN     MG_TC_interruption
  PSL

EM

;----------------------------
LBL Tool_spindle_ok                    ;01

  L      MG_key_NC_start_p_pulse
  A      MG_automatic_mode
  R      MG_TC_without_Tool_Call

  L      MG_TC_without_Tool_Call
  =      PN_error_tool_spindle_ok
  =      MG_win_window_yes_no
  S      TS_TC_timeout

  LN     MG_reference_axes_traversed
  S      PN_error_reference_necessary

  LN     MG_TC_without_Tool_Call
  AN     PN_error_reference_necessary
  AN     MG_TC_interruption
  PSL

  L      NP_M4175_control_in_operation_flashes
  JPT    TC_auto_reset

EM

;----------------------------
LBL Spi_full_empty                     ;02

  L      MG_tool_status_reading
  IFT
    PS     MG_marker_zero
  ELSE
    L      WG_tool_in_spindle
    >      K+0
    CMT    Spi_full
    CMF    Spi_empty
  ENDI

EM

;----------------------------
LBL Spi_empty_full                     ;37 ;47

  L      WG_TC_auto_new_tool_number
  >      K+0
  CMT    Spi_full
  CMF    Spi_empty

EM

;----------------------------
LBL Spi_full

  LN     MG_Sn_I_tool_located_in_spindle
  AN     MG_commissioning
  AN     MG_S1_tool_monitoring_inactive[BG_number_active_spindle]
  S      PN_error_spindle_without_tool

  LN     PN_error_spindle_without_tool
  PSL

  L      NP_M4175_control_in_operation_flashes
  JPT    TC_auto_reset

EM

;----------------------------
LBL Spi_empty

  L      MG_Sn_I_tool_located_in_spindle
  AN     MG_commissioning
  AN     MG_S1_tool_monitoring_inactive[BG_number_active_spindle]
  S      PN_error_spindle_not_empty

  LN     PN_error_spindle_not_empty
  PSL

  L      NP_M4175_control_in_operation_flashes
  JPT    TC_auto_reset

EM

;----------------------------
LBL Mag_full_empty                     ;17

  L      NP_M4521_TOOL_CALL_T_0_programmed
  CMT    Mag_empty
  CMF    Mag_full

EM

;----------------------------
LBL Mag_empty                          ;40

  L      STG_TC_mag[BG_TC_mag_index_Tool_Call].MG_I_TC_pocket_not_empty
  AN     MG_commissioning
  AN     MG_TC_mag_1_monitoring_inactive[BG_number_active_spindle]
  S      PN_error_TC_pocket_not_empty

  LN     PN_error_TC_pocket_not_empty
  PSL

  L      NP_M4175_control_in_operation_flashes
  JPT    TC_auto_reset

EM

;----------------------------
LBL Mag_full                           ;30

  LN     STG_TC_mag[BG_TC_mag_index_Tool_Call].MG_I_TC_pocket_not_empty
  AN     MG_commissioning
  AN     MG_TC_mag_1_monitoring_inactive[BG_number_active_spindle]
  S      PN_error_TC_pocket_without_tool

  LN     PN_error_TC_pocket_without_tool
  PSL

  L      NP_M4175_control_in_operation_flashes
  JPT    TC_auto_reset

EM

;----------------------------
LBL T_mag_start                        ;03 ;36

  CM     Magazine_posit
  PLL
  =N     STG_TC_mag[BG_TC_mag_index_Tool_Call].MG_TM_started_by_Tool_Call

  LN     MG_TC_interruption
  PSL

EM

;----------------------------
LBL Magazine_in_pos                    ;11 ;37

  CM     Magazine_posit

  L      STG_TC_mag[BG_TC_mag_index_Tool_Call].MG_TC_mag_start
  AN     MG_TC_interruption
  S      TS_TC_timeout

  PLL
  AN     MG_TC_interruption
  PSL

  L      MG_TC_interruption
  =      MG_win_TC_magazine_change_position

EM

#if Tool_magazine = &PLC_axis
;----------------------------
LBL Magazine_posit

  L      STG_TC_mag[BG_TC_mag_index_Tool_Call].BG_TC_magazine_axis_index
  =      BL_TC_mag_axis_index

  L      WG_TC_pocket_number
  X      STG_TC_mag[BG_TC_mag_index_Tool_Call].DG_TC_magazine_pocket_distance
  -      DG_position_axis_X_ref[BL_TC_mag_axis_index]
  PSW
  CM     Absolute
  PLW
  <      DG_MP1030_pos_window_X[BL_TC_mag_axis_index]
  O[
    L      DG_position_axis_X_ref[BL_TC_mag_axis_index]
    <      DG_MP1030_pos_window_X[BL_TC_mag_axis_index]
    A[
      L      WG_TC_pocket_number
      ==     STG_TC_mag[BG_TC_mag_index_Tool_Call].WG_MP7261_TC_magazine_max_pocket
    ]
  ]
  AN     STG_TC_mag[BG_TC_mag_index_Tool_Call].MG_status[KG_posit_active]
  AN     STG_TC_mag[BG_TC_mag_index_Tool_Call].MG_TC_mag_start
  AN     STG_TC_mag[BG_TC_mag_index_Tool_Call].MG_TM_started_by_Tool_Call
  PSL

EM
#endif

#if Tool_magazine = &Pulse
;----------------------------
LBL Magazine_posit

  L      STG_TC_mag[BG_TC_mag_index_Tool_Call].WG_TC_magazine_actual_pocket
  ==     WG_TC_pocket_number
  AN     STG_TC_mag[BG_TC_mag_index_Tool_Call].MG_TC_mag_start
  AN     STG_TC_mag[BG_TC_mag_index_Tool_Call].MG_TM_started_by_Tool_Call
  AN     STG_TC_mag[BG_TC_mag_index_Tool_Call].MG_TC_mag_reference_necessary
  PSL

EM
#endif

;----------------------------
LBL S_start                            ;04

  LN     MG_TC_interruption
  PSL
  EMF

  L      MG_spindle_with_ROD
  S      MG_TC_spindle_orientation_on

EM

;----------------------------
LBL S_in_TC_pos                        ;14

  LN     MG_spindle_with_ROD
  A      MG_spindle_off
  O      MG_spindle_in_TC_position
  AN     MG_TC_spindle_orientation_on
  AN     MG_TC_interruption
  PSL

  L      MG_TC_interruption
  =      MG_win_TC_pos_S

EM

;----------------------------
LBL XY_start                           ;08

  CM     X_start
  CM     Y_start
  PL     ML_stack_synchronize

EM

;----------------------------
LBL Z_start                            ;05

  L      MG_TC_interruption
  =      MG_win_TC_pos_Z_put_in
  IFT
    PS     MG_marker_zero
  ELSE
    PS     KG_axis_Z
    PS     DG_TC_position_Z_put_in
    PS     DG_feed_PLC_Z
#if CFG_PLC_pos_Z_software_end_switch = "active"
    PS     KL_absolute_with_software_limit_switch
#else
    PS     KL_absolute_without_software_limit_switch
#endif
    CM     9221
    PLW
    ==     K+0
    PSL
    M_display_module_error
  ENDI

EM

;----------------------------
LBL Z_in_TC_Pos                        ;07

  L      DG_position_axis_Z_ref
  -      DG_TC_position_Z_put_in
  PSW
  CM     Absolute
  PLW
  <      DG_MP1030_pos_window_Z
  AN     PN_M4122_strobe_PLC_posit_axis_Z
  AN     MG_TC_interruption
  PSL

  L      MG_TC_interruption
  =      MG_win_TC_pos_Z_put_in

EM

;----------------------------
LBL Y_start

  L      MG_TC_interruption
  =      MG_win_TC_pos_Y
  IFT
    PS     MG_marker_zero
  ELSE
    PS     KG_axis_Y
    PS     DG_TC_pos_Y
    PS     DG_feed_PLC_Y
#if CFG_PLC_pos_Y_software_end_switch = "active"
    PS     KL_absolute_with_software_limit_switch
#else
    PS     KL_absolute_without_software_limit_switch
#endif
    CM     9221
    PLW
    ==     K+0
    PSL
    M_display_module_error
  ENDI

EM

;----------------------------
LBL Y_in_TC_Pos                        ;16

  L      MG_TC_interruption
  =      MG_win_TC_pos_Y

  L      DG_position_axis_Y_ref
  -      DG_TC_pos_Y
  PSW
  CM     Absolute
  PLW
  <      DG_MP1030_pos_window_Y
  AN     PN_M4121_strobe_PLC_posit_axis_Y
  AN     MG_TC_interruption
  PSL

EM

;----------------------------
LBL X_start

  L      MG_TC_interruption
  =      MG_win_TC_pos_X
  IFT
    PS     MG_marker_zero
  ELSE
    PS     KG_axis_X
    PS     DG_TC_position_X
    PS     DG_feed_PLC_X
#if CFG_PLC_pos_X_software_end_switch = "active"
    PS     KL_absolute_with_software_limit_switch
#else
    PS     KL_absolute_without_software_limit_switch
#endif
    CM     9221
    PLW
    ==     K+0
    PSL
    M_display_module_error
  ENDI

EM

;----------------------------
LBL X_in_TC_Pos                        ;15

  L      MG_TC_interruption
  =      MG_win_TC_pos_X

  L      DG_position_axis_X_ref
  -      DG_TC_position_X
  PSW
  CM     Absolute
  PLW
  <      DG_MP1030_pos_window_X
  AN     PN_M4120_strobe_PLC_posit_axis_X
  AN     MG_TC_interruption
  PSL

EM

;----------------------------
LBL T_status_read                      ;09

  L      MG_marker_one
  S      MG_tool_status_reading
  PSL

EM

;----------------------------
LBL T_standby_read                     ;40 ;50

  L      MG_marker_one
#if TC_pocket_90_degree_shift = &active
  S      MG_standby_tool_status_reading
#endif
  PSL

EM

;----------------------------
LBL T_pocket_put_out                   ;12 ;38

  L      MG_marker_one
  R      STG_TC_mag[BG_TC_mag_index_Tool_Call].MG_O_TC_pocket_put_in
  S      STG_TC_mag[BG_TC_mag_index_Tool_Call].MG_O_TC_pocket_put_out
  PSL

EM

;----------------------------
LBL T_pocket_in_out                    ;44 ;54

  LN     MG_standby_tool_status_reading
  PSL
  EMF

  L      BG_standby_tool_status
  BT     K+1
  =      MG_tool_oversized

  L      MG_tool_oversized
  S      STG_TC_mag[BG_TC_mag_index_Tool_Call].MG_O_TC_pocket_put_in
  R      STG_TC_mag[BG_TC_mag_index_Tool_Call].MG_O_TC_pocket_put_out

EM

;----------------------------
LBL T_pocket_pos_ok                    ;45 ;55

  L      MG_tool_oversized
  CMT    T_pocket_pos_in
  CMF    T_pocket_pos_out

EM

;----------------------------
LBL T_pocket_pos_out                   ;18 ;39

#if TC_pocket_90_degree_shift = &active
  L      STG_TC_mag[BG_TC_mag_index_Tool_Call].MG_I_TC_pocket_out
  AN     STG_TC_mag[BG_TC_mag_index_Tool_Call].MG_I_TC_pocket_in
  AN     MG_TC_interruption
  PSL

  L      MG_TC_interruption
  =      MG_win_TC_pocket_put_out
#else
  PS     MG_marker_one
#endif

EM

;----------------------------
LBL T_pocket_pos_in

#if TC_pocket_90_degree_shift = &active
  LN     STG_TC_mag[BG_TC_mag_index_Tool_Call].MG_I_TC_pocket_out
  A      STG_TC_mag[BG_TC_mag_index_Tool_Call].MG_I_TC_pocket_in
  AN     MG_TC_interruption
  PSL

  L      MG_TC_interruption
  =      MG_win_TC_pocket_put_in
#else
  PS     MG_marker_one
#endif

EM

;----------------------------
LBL T_arm_put_out                      ;26

  L      MG_marker_one
  R      O_TC_arm_put_in
  S      O_TC_arm_put_out
  S      O_spindle_blow_clean
  PSL

EM

;----------------------------
LBL T_arm_put_in                       ;33 ;43

  L      MG_marker_one
  S      O_TC_arm_put_in
  R      O_TC_arm_put_out
  S      O_spindle_blow_clean
  PSL

EM

;----------------------------
LBL T_arm_pos_out                      ;30

  L      I_TC_arm_put_out
  AN     I_TC_arm_put_in
  AN     MG_TC_interruption
  R      O_spindle_blow_clean
  PSL

  L      MG_TC_interruption
  R      O_spindle_blow_clean
  =      MG_win_TC_arm_put_out

EM

;----------------------------
LBL T_arm_pos_in                       ;34 ;44

  LN     I_TC_arm_put_out
  A      I_TC_arm_put_in
  AN     MG_TC_interruption
  R      O_spindle_blow_clean
  PSL

  L      MG_TC_interruption
  R      O_spindle_blow_clean
  =      MG_win_TC_arm_put_in

EM

;----------------------------
LBL Tool_unclamping                    ;23

  L      MG_marker_one
  S      MG_Sn_O_tool_unclamping
  PSL

EM

;----------------------------
LBL Tool_clamping                      ;35 ;45

  L      MG_marker_one
  R      MG_Sn_O_tool_unclamping
  PSL

EM

;----------------------------
LBL Tool_unclamped                     ;24

  LN     MG_Sn_I_tool_clamped
  A      MG_Sn_I_tool_unclamped
  AN     MG_TC_interruption
  PSL

  L      MG_TC_interruption
  =      MG_win_tool_unclamping

EM

;----------------------------
LBL Tool_clamped                       ;36 ;46

  L      MG_Sn_I_tool_clamped
  AN     MG_Sn_I_tool_unclamped
  AN     MG_TC_interruption
  PSL

  L      MG_TC_interruption
  =      MG_win_tool_clamping

EM

;----------------------------
LBL Tool_lock_open                     ;06

  L      MG_marker_one
  S      O_tool_lock_open
  R      O_tool_lock_close
  PSL

EM

;----------------------------
LBL Tool_lock_close                    ;47 ;57

  L      MG_marker_one
  R      O_tool_lock_open
  S      O_tool_lock_close
  PSL

EM

;----------------------------
LBL Tool_lock_opened                   ;10

#if CFG_magazine_tool_lock = "active"
  L      I_tool_lock_open
  AN     I_tool_lock_close
  AN     MG_TC_interruption
  PSL

  L      MG_TC_interruption
  =      MG_win_tool_lock_open
#else
  PS     MG_marker_one
#endif

EM

;----------------------------
LBL Tool_lock_closed                   ;48 ;58

#if CFG_magazine_tool_lock = "active"
  LN     I_tool_lock_open
  A      I_tool_lock_close
  AN     MG_TC_interruption
  PSL

  L      MG_TC_interruption
  =      MG_win_tool_lock_close
#else
  PS     MG_marker_one
#endif

EM

#if CFG_TC_transport_carriage = "inactive"

;----------------------------
LBL T_gripper_close                    ;21

  L      MG_marker_one
  S      O_TC_arm_grippers_close
  PSL

EM

;----------------------------
LBL T_gripper_open                     ;42  ;52

  L      MG_marker_one
  R      O_TC_arm_grippers_close
  PSL

EM

;----------------------------
LBL T_gripper_closed                   ;22

#if TC_gripper = &active
  LN     I_TC_arm_grippers_opened
  AN     MG_TC_interruption
  PSL

  L      MG_TC_interruption
  =      MG_win_TC_grippers_close
#else
  PS     MG_marker_one
#endif

EM

;----------------------------
LBL T_gripper_opened                   ;43  ;53

#if TC_gripper = &active
  L      I_TC_arm_grippers_opened
  AN     MG_TC_interruption
  PSL

  L      MG_TC_interruption
  =      MG_win_TC_grippers_open
#else
  PS     MG_marker_one
#endif

EM

#if Middle_position_exchange = &active
;----------------------------
LBL T_arm_to_middle                    ;34

  L      MG_marker_one
  R      O_TC_arm_turn_cw
  R      O_TC_arm_turn_ccw

  L      I_TC_arm_cw
  AN     I_TC_arm_ccw
  S      ML_TC_arm_ccw_stop
  S      O_TC_arm_to_middle_position
  R      O_TC_arm_turn_cw
  S      O_TC_arm_turn_ccw

  LN     I_TC_arm_cw
  A      I_TC_arm_ccw
  S      ML_TC_arm_cw_stop
  S      O_TC_arm_to_middle_position
  S      O_TC_arm_turn_cw
  R      O_TC_arm_turn_ccw

  L      O_TC_arm_turn_cw
  O      O_TC_arm_turn_ccw
  AN     MG_TC_interruption
  PSL
  EMT

  L      MG_TC_interruption
  R      O_TC_arm_turn_cw
  R      O_TC_arm_turn_ccw
  =      MG_win_TC_arm_turn

EM
#endif

;----------------------------
LBL T_arm_to_spindle                   ;19

  L      MG_marker_one
  S      O_TC_arm_to_spindle_position
  R      O_TC_arm_to_basic_position
  PSL

EM

;----------------------------
LBL T_arm_to_basic                     ;39 ;49

  L      MG_marker_one
  R      O_TC_arm_to_spindle_position
  S      O_TC_arm_to_basic_position
  PSL

EM

;----------------------------
LBL T_arm_on_spindle                   ;20

#if Middle_position_basic = &active
  LN     I_TC_arm_basic_pos
  A      I_TC_arm_spindle_pos
  AN     MG_TC_interruption
  PSL

  L      MG_TC_interruption
  =      MG_win_TC_arm_to_spindle_position
#else
  PS     MG_marker_one
#endif

EM

;----------------------------
LBL T_arm_on_basic                     ;41 ;51

#if Middle_position_basic = &active
  L      I_TC_arm_basic_pos
  AN     I_TC_arm_spindle_pos
  AN     MG_TC_interruption
  PSL

  L      MG_TC_interruption
  =      MG_win_TC_arm_to_basic_position
#else
  PS     MG_marker_one
#endif

EM

#if Middle_position_exchange = &active
;----------------------------
LBL T_arm_in_middle                    ;35

  L      I_TC_arm_middle_pos
  AN     MG_TC_interruption
  PSL

  LN     I_TC_arm_middle_pos
  A      MG_TC_interruption
  =      MG_win_TC_arm_turn

EM
#endif

#else

;----------------------------
LBL T_gri_spi_close                    ;24

  L      I_TC_arm_cw
  R      O_TC_arm_gripper_1_open

  L      I_TC_arm_ccw
  R      O_TC_arm_gripper_2_open

  PS     MG_marker_one

EM

;----------------------------
LBL T_gri_spi_closed                   ;25

  L      I_TC_arm_cw
  AN     I_TC_arm_gripper_1_opened
  O[
    L      I_TC_arm_ccw
    AN     I_TC_arm_gripper_2_opened
  ]
  AN     MG_TC_interruption
  PSL

  L      MG_TC_interruption
  =      MG_win_TC_gripper_spi_close

EM

;----------------------------
LBL T_gri_spi_open                     ;46

  L      I_TC_arm_cw
  S      O_TC_arm_gripper_1_open

  L      I_TC_arm_ccw
  S      O_TC_arm_gripper_2_open

  PS     MG_marker_one

EM

;----------------------------
LBL T_gri_spi_opened                   ;47

  L      I_TC_arm_cw
  A      I_TC_arm_gripper_1_opened
  O[
    L      I_TC_arm_ccw
    A      I_TC_arm_gripper_2_opened
  ]
  AN     MG_TC_interruption
  PSL

  L      MG_TC_interruption
  =      MG_win_TC_gripper_spi_open

EM

;----------------------------
LBL T_gri_mag_close                    ;28

  L      I_TC_arm_ccw
  R      O_TC_arm_gripper_1_open

  L      I_TC_arm_cw
  R      O_TC_arm_gripper_2_open

  PS     MG_marker_one

EM

;----------------------------
LBL T_gri_mag_closed                   ;29

  L      I_TC_arm_ccw
  AN     I_TC_arm_gripper_1_opened
  O[
    L      I_TC_arm_cw
    AN     I_TC_arm_gripper_2_opened
  ]
  AN     MG_TC_interruption
  PSL

  L      MG_TC_interruption
  =      MG_win_TC_gripper_mag_close

EM

;----------------------------
LBL T_gri_mag_open                     ;13

  L      I_TC_arm_ccw
  S      O_TC_arm_gripper_1_open

  L      I_TC_arm_cw
  S      O_TC_arm_gripper_2_open

  PS     MG_marker_one

EM

;----------------------------
LBL T_gri_mag_opened                   ;14

  L      I_TC_arm_ccw
  A      I_TC_arm_gripper_1_opened
  O[
    L      I_TC_arm_cw
    A      I_TC_arm_gripper_2_opened
  ]
  AN     MG_TC_interruption
  PSL

  L      MG_TC_interruption
  =      MG_win_TC_gripper_mag_open

EM

;----------------------------
LBL T_arm_to_spindle                   ;17

  L      MG_marker_one
  S      O_TC_arm_to_spindle_position
  R      O_TC_arm_to_magazine_position
  R      O_TC_arm_to_basic_position
  PSL

EM

;----------------------------
LBL T_arm_to_magazine                  ;17

  L      MG_marker_one
  R      O_TC_arm_to_spindle_position
  S      O_TC_arm_to_magazine_position
  R      O_TC_arm_to_basic_position
  PSL

EM

;----------------------------
LBL T_arm_to_basic                     ;38 ;48

  L      MG_marker_one
  S      O_TC_arm_to_basic_position
  PSL

EM

;----------------------------
LBL T_arm_on_spindle                   ;18

  L      I_TC_arm_spindle_pos
  AN     I_TC_arm_magazine_pos
  AN     I_TC_arm_basic_pos
  AN     MG_TC_interruption
  PSL

  L      MG_TC_interruption
  =      MG_win_TC_arm_to_spindle_position

EM

;----------------------------
LBL T_arm_on_magazine                  ;18

  LN     I_TC_arm_spindle_pos
  A      I_TC_arm_magazine_pos
  AN     I_TC_arm_basic_pos
  AN     MG_TC_interruption
  PSL

  L      MG_TC_interruption
  =      MG_win_TC_arm_to_magazin

EM

;----------------------------
LBL T_arm_on_basic                     ;40 ;50

  LN     I_TC_arm_spindle_pos
  AN     I_TC_arm_magazine_pos
  A      I_TC_arm_basic_pos
  AN     MG_TC_interruption
  PSL

  L      MG_TC_interruption
  =      MG_win_TC_arm_to_basic_position

EM

#endif

;----------------------------
LBL T_arm_to_cw_ccw                    ;31 ;41

  L      MG_marker_one
  R      O_TC_arm_turn_cw
  R      O_TC_arm_turn_ccw

  L      I_TC_arm_cw
  AN     I_TC_arm_ccw
  O      ML_TC_arm_ccw_stop
  R      O_TC_arm_to_middle_position
  R      O_TC_arm_turn_cw
  S      O_TC_arm_turn_ccw

  LN     I_TC_arm_cw
  A      I_TC_arm_ccw
  O      ML_TC_arm_cw_stop
  R      O_TC_arm_to_middle_position
  S      O_TC_arm_turn_cw
  R      O_TC_arm_turn_ccw

  L      O_TC_arm_turn_cw
  O      O_TC_arm_turn_ccw
  AN     MG_TC_interruption
  R      ML_TC_arm_cw_stop
  R      ML_TC_arm_ccw_stop
  PSL
  EMT

  LN     I_TC_arm_cw
  XO     I_TC_arm_ccw
  O      MG_TC_interruption
  R      O_TC_arm_turn_cw
  R      O_TC_arm_turn_ccw
  =      MG_win_TC_arm_turn

EM

;----------------------------
LBL T_arm_in_cw_ccw                    ;32 ;42

  LN     I_TC_arm_cw
  A      I_TC_arm_ccw
  A      O_TC_arm_turn_ccw
  O[
    L      I_TC_arm_cw
    AN     I_TC_arm_ccw
    A      O_TC_arm_turn_cw
  ]
  AN     MG_TC_interruption
  PSL

  L      MG_TC_interruption
  =      MG_win_TC_arm_turn

EM

;----------------------------
LBL Tnew_store                         ;27


  PS     MG_marker_one

  L      WG_TC_auto_tool_number
  =      WG_TC_auto_new_tool_number

EM

;----------------------------
LBL T0_in_status                       ;28

  LN     MG_tool_status_reading
  AN     MG_TC_interruption
  S      PN_M4538_tool_selection_from_PLC_W264
  S      PN_M4542_pocket_table_not_update
  PSL
  EMF

  L      K+0
  =      NP_W264_tool_number

EM

;----------------------------
LBL Tnew_in_status                     ;37 ;47

  LN     MG_tool_status_reading
  AN     MG_TC_interruption
  PSL
  EMF

  L      MG_marker_one
  R      PN_M4542_pocket_table_not_update
  S      PN_M4538_tool_selection_from_PLC_W264

  L      NP_W262_tool_pocket_number
  ==     KL_tool_without_pocket
  IFT
    L      NP_W264_tool_number
    =      WG_TC_auto_tool_number
  ENDI

  L      WG_TC_auto_new_tool_number
  =      NP_W264_tool_number
  =      WG_tool_in_spindle

EM

;----------------------------
LBL T_status_ok                        ;29 ;38 ;48

  LN     PN_M4538_tool_selection_from_PLC_W264
  PSL

  L      NP_W262_tool_pocket_number
  >=     KL_tool_with_pocket
  O      NP_M4520_TOOL_CALL_2nd_strobe
  EMT

  L      WG_TC_auto_tool_number
  =      NP_W264_tool_number

EM

#if Middle_position_exchange = &active
;----------------------------
LBL Tool_Call_2nd                      ;31

  LN     NP_M4520_TOOL_CALL_2nd_strobe
  ON     NP_M4073_strobe_TOOL_CALL
  PSL
  EMT

  L      NP_M4520_TOOL_CALL_2nd_strobe
  AN     MG_TC_interruption
  S      ML_2nd_TOOL_CALL_following
  S      PN_M4093_quit_TOOL_CALL

EM

;----------------------------
LBL Tool_Call_wait                     ;32

  L      NP_M4073_strobe_TOOL_CALL
  PSL
  EMF

  L      NP_W262_tool_pocket_number
  >=     KL_tool_with_pocket
  IFT
    L      NP_W262_tool_pocket_number
    =      WG_TC_pocket_number
    L      NP_W264_tool_number
    =      WG_TC_auto_tool_number
  ENDI

#endif

EM

;----------------------------
LBL TOOL_CALL_quit                     ;49 ;59

  L      NP_W262_tool_pocket_number
  ==     KL_tool_without_pocket
  ON     NP_M4073_strobe_TOOL_CALL
  S      MG_standby_tool_status_reading
  PSL
  EMT

  L      NP_M4073_strobe_TOOL_CALL
  AN     MG_TC_interruption
  S      PN_M4093_quit_TOOL_CALL

EM

;----------------------------
LBL TC_auto_end                        ;50 ;60

  PS     MG_marker_zero

;----------------------------
LBL TC_auto_reset

  L      MG_marker_one
  R      ML_2nd_TOOL_CALL_following
  R      MG_TC_automatic
  R      MG_TC_interruption
  R      MG_TC_spindle_orientation_on
  R      MG_win_window_yes_no
  R      ML_TC_arm_cw_stop
  R      ML_TC_arm_ccw_stop
  R      PN_error_tool_spindle_ok
  R      PN_M4542_pocket_table_not_update
  R      O_TC_arm_to_middle_position
  R      O_spindle_blow_clean

  L      K+0
  =      BG_step_toolchange
  =      BL_WZ_mag_index

  REPEAT
    L      MG_marker_one
    R      STG_TC_mag[BL_WZ_mag_index].MG_TM_started_by_Tool_Call

    INC    BL_WZ_mag_index

    L      BL_WZ_mag_index
    >=     KG_TC_magazine_max_count
  UNTILT

  L      K+0
  =      BL_WZ_mag_index

EM

;----------------------------
LBL T_pocket_time                      ;13

#if TC_pocket_90_degree_shift = &active
  CM     TC_auto_time
#else
  PS     MG_marker_one
#endif

EM
;----------------------------
LBL TC_auto_time                       ;25

  LN     TR_TC_timer_universal
  =      TS_TC_timer_universal
  PSL

EM

#if CFG_TC_transport_carriage = "inactive"

KFIELD Diagnostic
  KG_dia_TC_auto_double_arm            ;-1
  KG_dia_TC_auto_start                 ;00
  KG_dia_tool_spindle_ok               ;01
  KG_dia_spi_full_empty                ;02
  KG_dia_T_mag_start                   ;03
  KG_dia_S_start                       ;04
  KG_dia_Z_start                       ;05
  KG_dia_tool_lock_open                ;06
  KG_dia_Z_in_TC_pos                   ;07
  KG_dia_XY_start                      ;08
  KG_dia_T_status_read                 ;09
  KG_dia_tool_lock_opened              ;10
  KG_dia_magazine_in_pos               ;11
  KG_dia_T_pocket_put_out              ;12
  KG_dia_T_pocket_time                 ;13
  KG_dia_S_in_TC_posit                 ;14
  KG_dia_X_in_TC_posit                 ;15
  KG_dia_Y_in_TC_pos                   ;16
  KG_dia_mag_full_empty                ;17
  KG_dia_T_pocket_pos_out              ;18
  KG_dia_T_arm_to_spindle              ;19
  KG_dia_T_arm_on_spindle              ;20
  KG_dia_T_grip_close                  ;21
  KG_dia_T_grip_closed                 ;22
  KG_dia_tool_unclamping               ;23
  KG_dia_tool_unclamped                ;24
  KG_dia_TC_auto_time                  ;25
  KG_dia_T_arm_put_out                 ;26
  KG_dia_Tnew_latch                    ;27
  KG_dia_T0_in_status                  ;28
  KG_dia_T_status_ok                   ;29
  KG_dia_T_arm_pos_out                 ;30
#if Middle_position_exchange = &active
  KG_dia_Tool_Call_2nd                 ;31
  KG_dia_Tool_Call_wait                ;32
  KG_dia_Jump_TC_arm                   ;33      ---> (1)
  KG_dia_T_arm_to_middle               ;34
  KG_dia_T_arm_in_middle               ;35
  KG_dia_T_mag_start                   ;36
  KG_dia_magazine_in_pos               ;37
  KG_dia_T_pocket_put_out              ;38
  KG_dia_T_pocket_pos_out              ;39
  KG_dia_mag_empty                     ;40
#endif                                 ;
  KG_dia_T_arm_to_cw_ccw               ;41 ;31  <--- (1)
  KG_dia_T_arm_in_cw_ccw               ;42 ;32
  KG_dia_T_arm_put_in                  ;43 ;33
  KG_dia_T_arm_pos_in                  ;44 ;34
  KG_dia_tool_clamping                 ;45 ;35
  KG_dia_tool_clamped                  ;46 ;36
  KG_dia_Tnew_in_status                ;47 ;37
  KG_dia_T_status_ok                   ;48 ;38
  KG_dia_T_arm_to_basic                ;49 ;39
  KG_dia_T_standby_read                ;50 ;40
  KG_dia_T_arm_in_basic                ;51 ;41
  KG_dia_T_grip_open                   ;52 ;42
  KG_dia_T_grip_opened                 ;53 ;43
  KG_dia_T_pocket_in_out               ;54 ;44
  KG_dia_T_pocket_pos_ok               ;55 ;45
  KG_dia_tool_lock_close               ;56 ;46
  KG_dia_tool_lock_closed              ;57 ;47
  KG_dia_spi_empty_full                ;58 ;48
  KG_dia_TOOL_CALL_quit                ;59 ;49
  KG_dia_TC_auto_end                   ;60 ;50
  KG_dia_empty                         ;61
  KG_dia_TC_auto_time                  ;reserviert
ENDK

#else

KFIELD Diagnostic
  KG_dia_TC_auto_double_arm            ;-1
  KG_dia_TC_auto_start                 ;00
  KG_dia_tool_spindle_ok               ;01
  KG_dia_spi_full_empty                ;02
  KG_dia_T_mag_start                   ;03
  KG_dia_S_start                       ;04
  KG_dia_Z_start                       ;05
  KG_dia_tool_lock_open                ;06
  KG_dia_Z_in_TC_pos                   ;07
  KG_dia_XY_start                      ;08
  KG_dia_T_status_read                 ;09
  KG_dia_tool_lock_opened              ;10
  KG_dia_magazine_in_pos               ;11
  KG_dia_T_pocket_put_out              ;12
  KG_dia_T_pocket_time                 ;13
  KG_dia_mag_full_empty                ;14
  KG_dia_T_pocket_pos_out              ;15
  KG_dia_T_grip_mag_open               ;16
  KG_dia_T_grip_mag_opened             ;17
  KG_dia_T_arm_to_magazine             ;18
  KG_dia_T_arm_on_magazine             ;19
  KG_dia_T_grip_mag_close              ;20
  KG_dia_T_grip_mag_closed             ;21
  KG_dia_T_arm_put_out                 ;22
  KG_dia_T_arm_pos_out                 ;23
  KG_dia_T_arm_to_basic                ;24
  KG_dia_T_arm_in_basic                ;25
  KG_dia_T_arm_put_in                  ;26
  KG_dia_T_arm_pos_in                  ;27
  KG_dia_T_grip_spi_open               ;28
  KG_dia_T_grip_spi_opened             ;29
  KG_dia_S_in_TC_posit                 ;30
  KG_dia_X_in_TC_posit                 ;31
  KG_dia_Y_in_TC_pos                   ;32
  KG_dia_Tnew_latch                    ;33
  KG_dia_T0_in_status                  ;34
  KG_dia_T_status_ok                   ;35
  KG_dia_Tool_Call_2nd                 ;36
  KG_dia_Tool_Call_wait                ;37
  KG_dia_Jump_TC_arm                   ;38    ---> (1)
  KG_dia_T_mag_start                   ;39
  KG_dia_T_arm_to_spindle              ;40   <---  (1)
  KG_dia_T_arm_on_spindle              ;41
  KG_dia_T_grip_spi_close              ;42
  KG_dia_T_grip_spi_closed             ;43
  KG_dia_tool_unclamping               ;44
  KG_dia_tool_unclamped                ;45
  KG_dia_TC_auto_time                  ;46
  KG_dia_T_arm_put_out                 ;47
  KG_dia_T_arm_pos_out                 ;48
  KG_dia_T_arm_to_cw_ccw               ;49
  KG_dia_T_arm_in_cw_ccw               ;50
  KG_dia_T_arm_put_in                  ;51
  KG_dia_T_arm_pos_in                  ;52
  KG_dia_tool_clamping                 ;53
  KG_dia_tool_clamped                  ;54
  KG_dia_Tnew_in_status                ;55
  KG_dia_T_status_ok                   ;56
  KG_dia_T_grip_spi_open               ;57
  KG_dia_T_grip_spi_opened             ;58
  KG_dia_T_arm_to_basic                ;59
  KG_dia_T_standby_read                ;60
  KG_dia_T_arm_in_basic                ;61
  KG_dia_T_arm_put_out                 ;62
  KG_dia_T_arm_pos_out                 ;63
  KG_dia_magazine_in_pos               ;64
  KG_dia_T_pocket_put_out              ;65
  KG_dia_T_pocket_pos_out              ;66
  KG_dia_mag_empty                     ;67
  KG_dia_T_arm_to_magazine             ;68
  KG_dia_T_arm_on_magazine             ;69
  KG_dia_T_arm_put_in                  ;70
  KG_dia_T_arm_pos_in                  ;71
  KG_dia_T_grip_mag_open               ;72
  KG_dia_T_grip_mag_opened             ;73
  KG_dia_T_arm_to_basic                ;74
  KG_dia_T_arm_in_basic                ;75
  KG_dia_T_pocket_in_out               ;76
  KG_dia_T_pocket_pos_ok               ;77
  KG_dia_T_grip_spi_close              ;78
  KG_dia_T_grip_mag_close              ;79
  KG_dia_T_grip_spi_closed             ;80
  KG_dia_T_grip_mag_closed             ;81
  KG_dia_tool_lock_close               ;82
  KG_dia_tool_lock_closed              ;83
  KG_dia_spi_empty_full                ;84
  KG_dia_TOOL_CALL_quit                ;85
  KG_dia_TC_auto_end                   ;86
  KG_dia_empty                         ;87
  KG_dia_TC_auto_time                  ;reserviert
  KG_dia_X_start                       ;reserviert
  KG_dia_X_in_TC_posit                 ;reserviert
ENDK
#endif
