*-----------------------------------------------------------------------------
*  TC Magazine controlled by Pulse Counter
*-----------------------------------------------------------------------------
*  Module Description
*    PLC status (tool table):
*    #Bit 1 (%00000010) - oversized tool (no pocket out after TOOL DEF)
*                       - (no pocket out after TOOL DEF)
*  $WS
*  error messages, timer (array) magazine depending
*-----------------------------------------------------------------------------

#define /s    KL_nominal_posit_cw                      K -1000
#define /s    KL_nominal_posit_ccw                     K +1000
#define /s    KL_string_count                          K +3

#define /s    ML_TC_mag_diagnosis                      M
#define /s    ML_debug_WZM_01                          M

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

#define /s    WL_line_dialog_file                      W

#define /s /r WL_TC_mag_actual_pocket_remanent         W[8]

EXTERN Absolute

GLOBAL Tool_magazine
GLOBAL TC_mag_cw_turn
GLOBAL TC_mag_ccw_turn

;----------------------------
LBL Tool_magazine
;----------------------------

  LN     MG_TC_auto_active
  EMT

  CM     TC_mag_n_read_input

  LN     NP_M4074_strobe_TOOL_DEF
  R      PN_M4094_quit_TOOL_DEF

  L      K+0
  =      BL_WZ_mag_index

  L      MG_marker_one
  R      MG_TC_mag_n_posit_active
  R      MG_TC_magazine_n_start_active
  R      TS_TC_magazine_timeout
  R      PN_error_TC_magazine_reference

  REPEAT

#if TC_magazine_reference_sensor = &inactive
    L      WL_TC_mag_actual_pocket_remanent[BL_WZ_mag_index]
    =      STG_TC_mag[BL_WZ_mag_index].WG_TC_magazine_actual_2_pocket
#endif

    L      STG_TC_mag[BL_WZ_mag_index].WG_TC_magazine_actual_2_pocket
    ==     K+0
    A[
      L      STG_TC_mag[BL_WZ_mag_index].WG_MP7261_TC_magazine_max_pocket
      >      K+0
    ]
    S      STG_TC_mag[BL_WZ_mag_index].MG_TC_mag_reference_necessary

    L      STG_TC_mag[BL_WZ_mag_index].MG_TC_mag_reference_necessary
    A      MG_control_operational
    AN     MG_TC_help
    S      PN_error_TC_magazine_reference
    IFT
      L      STG_TC_mag[BL_WZ_mag_index].MG_O_TC_magazine_cw
      O      STG_TC_mag[BL_WZ_mag_index].MG_O_TC_magazine_ccw
      A      MG_clock_1_s
      R      PN_error_TC_magazine_reference
    ENDI

    L      STG_TC_mag[BL_WZ_mag_index].MG_I_TC_magazine_reference
*    AN     STG_TC_mag[BL_WZ_mag_index].ML_TC_magazine_reference
#if TC_magazine_reference_sensor = &active
    A      STG_TC_mag[BL_WZ_mag_index].MG_TC_mag_reference_necessary
#endif
    IFT
      L      K+0
      =      STG_TC_mag[BL_WZ_mag_index].WG_TC_mag_act_nom_pocket_difference

      L      K+1
      =      STG_TC_mag[BL_WZ_mag_index].WG_TC_magazine_actual_pocket
      =      STG_TC_mag[BL_WZ_mag_index].WG_TC_magazine_actual_2_pocket
      =      STG_TC_mag[BL_WZ_mag_index].WG_TC_magazine_nominal_pocket
    ENDI

    L      STG_TC_mag[BL_WZ_mag_index].MG_I_TC_magazine_reference
    =      STG_TC_mag[BL_WZ_mag_index].ML_TC_magazine_reference

    L      STG_TC_mag[BL_WZ_mag_index].MG_I_TC_magazine_counter
    AN     STG_TC_mag[BL_WZ_mag_index].ML_TC_magazine_counter
    A      STG_TC_mag[BL_WZ_mag_index].MG_O_TC_magazine_cw
#if TC_magazine_reference_sensor = &active
    AN     STG_TC_mag[BL_WZ_mag_index].MG_TC_mag_reference_necessary
#endif
    IFT
      INC    STG_TC_mag[BL_WZ_mag_index].WG_TC_magazine_actual_pocket
      DEC    STG_TC_mag[BL_WZ_mag_index].WG_TC_mag_act_nom_pocket_difference

      L      STG_TC_mag[BL_WZ_mag_index].WG_TC_magazine_actual_pocket
      >      STG_TC_mag[BL_WZ_mag_index].WG_MP7261_TC_magazine_max_pocket
      IFT
        L      K+1
        =      STG_TC_mag[BL_WZ_mag_index].WG_TC_magazine_actual_pocket
      ENDI
    ENDI

    L      STG_TC_mag[BL_WZ_mag_index].MG_I_TC_magazine_counter
    AN     STG_TC_mag[BL_WZ_mag_index].ML_TC_magazine_counter
    A      STG_TC_mag[BL_WZ_mag_index].MG_O_TC_magazine_ccw
#if TC_magazine_reference_sensor = &active
    AN     STG_TC_mag[BL_WZ_mag_index].MG_TC_mag_reference_necessary
#endif
    IFT
      DEC    STG_TC_mag[BL_WZ_mag_index].WG_TC_magazine_actual_pocket
      DEC    STG_TC_mag[BL_WZ_mag_index].WG_TC_mag_act_nom_pocket_difference

      L      STG_TC_mag[BL_WZ_mag_index].WG_TC_magazine_actual_pocket
      ==     K+0
      IFT
        L      STG_TC_mag[BL_WZ_mag_index].WG_MP7261_TC_magazine_max_pocket
        =      STG_TC_mag[BL_WZ_mag_index].WG_TC_magazine_actual_pocket
      ENDI
    ENDI

    L      STG_TC_mag[BL_WZ_mag_index].MG_I_TC_magazine_counter
    =      STG_TC_mag[BL_WZ_mag_index].ML_TC_magazine_counter

    L      STG_TC_mag[BL_WZ_mag_index].MG_I_TC_magazine_exact_position
    AN     STG_TC_mag[BL_WZ_mag_index].ML_TC_magazine_exact_position
    A      STG_TC_mag[BL_WZ_mag_index].MG_O_TC_magazine_cw
#if TC_magazine_reference_sensor = &active
    AN     STG_TC_mag[BL_WZ_mag_index].MG_TC_mag_reference_necessary
#endif
    IFT
      INC    STG_TC_mag[BL_WZ_mag_index].WG_TC_magazine_actual_2_pocket

      L      STG_TC_mag[BL_WZ_mag_index].WG_TC_magazine_actual_2_pocket
      >      STG_TC_mag[BL_WZ_mag_index].WG_MP7261_TC_magazine_max_pocket
      IFT
        L      K+1
        =      STG_TC_mag[BL_WZ_mag_index].WG_TC_magazine_actual_2_pocket
      ENDI
    ENDI

    L      STG_TC_mag[BL_WZ_mag_index].MG_I_TC_magazine_exact_position
    AN     STG_TC_mag[BL_WZ_mag_index].ML_TC_magazine_exact_position
    A      STG_TC_mag[BL_WZ_mag_index].MG_O_TC_magazine_ccw
#if TC_magazine_reference_sensor = &active
    AN     STG_TC_mag[BL_WZ_mag_index].MG_TC_mag_reference_necessary
#endif
    IFT
      DEC    STG_TC_mag[BL_WZ_mag_index].WG_TC_magazine_actual_2_pocket

      L      STG_TC_mag[BL_WZ_mag_index].WG_TC_magazine_actual_2_pocket
      ==     K+0
      IFT
        L      STG_TC_mag[BL_WZ_mag_index].WG_MP7261_TC_magazine_max_pocket
        =      STG_TC_mag[BL_WZ_mag_index].WG_TC_magazine_actual_2_pocket
      ENDI
    ENDI

    L      STG_TC_mag[BL_WZ_mag_index].MG_I_TC_magazine_exact_position
    =      STG_TC_mag[BL_WZ_mag_index].ML_TC_magazine_exact_position

    L      BG_TC_mag_index_Tool_Call
    ==     BL_WZ_mag_index
    A      NP_M4074_strobe_TOOL_DEF
    AN     STG_TC_mag[BL_WZ_mag_index].MG_TC_mag_interruption
    AN     MG_SK_TC_mag_load_active
    AN     STG_TC_mag[BL_WZ_mag_index].MG_TM_started_by_Tool_Def
    S      STG_TC_mag[BL_WZ_mag_index].MG_TM_started_by_Tool_Def
    IFT
      L      NP_W262_tool_pocket_number
      =      STG_TC_mag[BL_WZ_mag_index].WL_Tool_Def_pocket_number
      L      NP_W264_tool_number
      =      STG_TC_mag[BL_WZ_mag_index].WL_Tool_Def_tool_number
    ENDI

    L      MG_marker_one
    S      TS_TC_magazine_1_exact_position[BL_WZ_mag_index]

    L      STG_TC_mag[BL_WZ_mag_index].MG_TC_mag_start
*$WS   =      TS_TC_mag_timeout
    =      TS_TC_magazine_timeout

    LN     STG_TC_mag[BL_WZ_mag_index].MG_O_TC_mag_lock_pin_out
    AN     STG_TC_mag[BL_WZ_mag_index].MG_I_TC_mag_lock_pin_in
    =      STG_TC_mag[BL_WZ_mag_index].MG_O_TC_mag_lock_pin_in

    CASE   STG_TC_mag[BL_WZ_mag_index].BG_step_TC_magazine
      CM     Magazine_start            ;00
      CM     Tool_def_quit             ;01
      CM     Pocket_put_in             ;02
      CM     Pocket_pos_in             ;03
      CM     Magazine_direct           ;04
      CM     Lock_pin_out              ;05
      CM     Lock_pin_pos_out          ;06
      CM     Magazine_nom_pos          ;07
      CM     Time_exact_stop           ;08
      CM     Lock_pin_pos_in           ;09
      CM     Mag_exact_stop            ;10
      CM     Standby_T_read            ;11
      CM     Pocket_in_out             ;12
      CM     Pocket_posit              ;13
      CM     Magazine_end              ;14
    ENDC

    PLL
    AN     STG_TC_mag[BL_WZ_mag_index].MG_TC_mag_interruption
    S      TS_TC_magazine_timeout
    =      STG_TC_mag[BL_WZ_mag_index].ML_step_start
    IFT
      INC    STG_TC_mag[BL_WZ_mag_index].BG_step_TC_magazine
    ENDI

    L      STG_TC_mag[BL_WZ_mag_index].BG_step_TC_magazine
    <>     STG_TC_mag[BL_WZ_mag_index].BL_step_TC_magazine
    ON     ML_TC_mag_diagnosis
    A      MG_TC_magazine_diagnosis
    IFT
      L      K +0
      =      BL_string_index

      L      STG_TC_mag[BL_WZ_mag_index].BG_step_TC_magazine
      =      STG_TC_mag[BL_WZ_mag_index].BL_step_TC_magazine
      REPEAT
        L      STG_TC_mag[BL_WZ_mag_index].BL_step_TC_magazine
        =X

        L      KF Diagnostic[X]
        =      WL_line_dialog_file
        =X

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

        INC    STG_TC_mag[BL_WZ_mag_index].BL_step_TC_magazine
        INC    BL_string_index
        INCX

        L      BL_string_index
        >=     KL_string_count
      UNTILT
    ENDI

    L      STG_TC_mag[BL_WZ_mag_index].BG_step_TC_magazine
    =      STG_TC_mag[BL_WZ_mag_index].BL_step_TC_magazine

    L      MG_TC_magazine_diagnosis
    =      ML_TC_mag_diagnosis

    L      STG_TC_mag[BL_WZ_mag_index].MG_TC_mag_start
    S      MG_TC_magazine_n_start_active
    IFT
      L      STG_TC_mag[BL_WZ_mag_index].MG_status[KG_posit_error]
      =      PN_error_positioning_TC_magacine

      L      MG_key_NC_start_p_pulse
      R      STG_TC_mag[BL_WZ_mag_index].MG_TC_mag_interruption

      L      MG_key_NC_start
      O      MG_pulse_CE_key
      S      TS_TC_magazine_timeout

      LN     TS_TC_magazine_timeout
      AN     TR_TC_magazine_timeout
      =      PN_error_TC_magazine_timeout
      S      STG_TC_mag[BL_WZ_mag_index].MG_TC_mag_interruption

      L      MG_pulse_external_stop
      AN     NP_M4157_mode_restore_position
      O      MG_key_NC_stop
      S      STG_TC_mag[BL_WZ_mag_index].MG_TC_mag_interruption

      L      STG_TC_mag[BL_WZ_mag_index].MG_TC_mag_interruption
      O      MG_pulse_END_PGM_M02_M30
      O      MG_guard_open
      R      STG_TC_mag[BL_WZ_mag_index].MG_TM_started_by_Tool_Def

      L      MG_pulse_internal_stop
      O      MG_pulse_TC_help
      ON     MG_control_operational
      CMT    TC_magazine_reset
    ENDI

#if TC_magazine_reference_sensor = &inactive
    L      STG_TC_mag[BL_WZ_mag_index].WG_TC_magazine_actual_2_pocket
    =      WL_TC_mag_actual_pocket_remanent[BL_WZ_mag_index]
#endif

    INC    BL_WZ_mag_index

    L      BL_WZ_mag_index
    >=     WG_TC_magazine_number
    O      ML_debug_WZM_01
  UNTILT

  CM     TC_mag_n_write_output

EM

;----------------------------
LBL Magazine_start                     ;00

  LN     STG_TC_mag[BL_WZ_mag_index].MG_I_TC_magazine_counter
  ON     STG_TC_mag[BL_WZ_mag_index].MG_I_TC_magazine_exact_position
  AN     MG_TC_automatic
  S      STG_TC_mag[BL_WZ_mag_index].MG_TC_mag_reference_necessary

#if TC_magazine_reference_sensor = &active
  L      STG_TC_mag[BL_WZ_mag_index].MG_TC_mag_reference_necessary
  A      MG_key_NC_start_p_pulse
  A      MG_control_operational
  A      MG_TC_standby_position
  AN     MG_TC_help
  S      STG_TC_mag[BL_WZ_mag_index].MG_TC_mag_start
#endif

  L      STG_TC_mag[BL_WZ_mag_index].MG_TM_started_by_Tool_Def
  O      STG_TC_mag[BL_WZ_mag_index].MG_WZ_mag_load_unload_start
  A      MG_TC_standby_position
  AN     MG_TC_automatic
  O      STG_TC_mag[BL_WZ_mag_index].MG_TM_started_by_Tool_Call
  S      STG_TC_mag[BL_WZ_mag_index].MG_TC_mag_start

  L      STG_TC_mag[BL_WZ_mag_index].MG_TC_mag_start
  PSL
  EMF

  L      STG_TC_mag[BL_WZ_mag_index].MG_WZ_mag_load_unload_start
  IFT
    L      STG_TC_mag[BL_WZ_mag_index].WG_TC_mag_load_nominal_pocket
    =      STG_TC_mag[BL_WZ_mag_index].WG_TC_magazine_nominal_pocket
  ENDI

  L      STG_TC_mag[BL_WZ_mag_index].MG_TM_started_by_Tool_Def
  AN     STG_TC_mag[BL_WZ_mag_index].MG_TM_started_by_Tool_Call
  R      STG_TC_mag[BL_WZ_mag_index].MG_TM_started_by_Tool_Def
  IFT
    L      STG_TC_mag[BL_WZ_mag_index].WL_Tool_Def_pocket_number
    =      STG_TC_mag[BL_WZ_mag_index].WG_TC_magazine_nominal_pocket
  ENDI

  L      STG_TC_mag[BL_WZ_mag_index].MG_TM_started_by_Tool_Call
  R      STG_TC_mag[BL_WZ_mag_index].MG_TM_started_by_Tool_Call
  IFT
    L      WG_TC_pocket_number
    =      STG_TC_mag[BL_WZ_mag_index].WG_TC_magazine_nominal_pocket
  ENDI

  L      STG_TC_mag[BL_WZ_mag_index].WG_TC_magazine_nominal_pocket
  <=     K+0
  A      MG_automatic_mode
*$WS
  AN     MG_edit_pocket_table
  IFT
    L      STG_TC_mag[BL_WZ_mag_index].WG_TC_magazine_actual_pocket
    =      STG_TC_mag[BL_WZ_mag_index].WG_TC_magazine_nominal_pocket
  ENDI

#if TC_magazine_reference_sensor = &active
  L      STG_TC_mag[BL_WZ_mag_index].MG_TC_mag_reference_necessary
  IFT
    L      K-1000
    =      STG_TC_mag[BL_WZ_mag_index].WG_TC_magazine_nominal_pocket
  ENDI
#endif

  L      STG_TC_mag[BL_WZ_mag_index].WG_TC_magazine_actual_pocket
  ==     STG_TC_mag[BL_WZ_mag_index].WG_TC_magazine_nominal_pocket
  IFT
    L      NP_M4074_strobe_TOOL_DEF
    S      PN_M4094_quit_TOOL_DEF

    PLL
    A      MG_marker_zero
    PSL

    LN     NP_M4074_strobe_TOOL_DEF
    JPT    TC_magazine_reset
  ENDI

EM

;----------------------------
LBL Tool_def_quit                      ;01

  L      NP_M4074_strobe_TOOL_DEF
  S      PN_M4094_quit_TOOL_DEF

  LN     NP_M4074_strobe_TOOL_DEF
  AN     STG_TC_mag[BL_WZ_mag_index].MG_TC_mag_interruption
  R      STG_TC_mag[BL_WZ_mag_index].MG_TM_started_by_Tool_Def
  PSL

EM

;----------------------------
LBL Pocket_put_in                      ;02

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

EM

;----------------------------
LBL Pocket_pos_out

#if TC_pocket_90_degree_shift = &active
  L      STG_TC_mag[BL_WZ_mag_index].MG_I_TC_pocket_out
  AN     STG_TC_mag[BL_WZ_mag_index].MG_I_TC_pocket_in
  AN     STG_TC_mag[BL_WZ_mag_index].MG_TC_mag_interruption
  PSL

  L      STG_TC_mag[BL_WZ_mag_index].MG_TC_mag_interruption
  =      MG_win_TC_pocket_put_out
#else
  PS     MG_marker_one
#endif

EM

;----------------------------
LBL Pocket_pos_in                      ;03

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

  L      STG_TC_mag[BL_WZ_mag_index].MG_TC_mag_interruption
  =      MG_win_TC_pocket_put_in
#else
  PS     MG_marker_one
#endif

EM

;----------------------------
LBL Magazine_direct                    ;04

  PS     MG_marker_one

  L      STG_TC_mag[BL_WZ_mag_index].WG_TC_magazine_actual_pocket
  -      STG_TC_mag[BL_WZ_mag_index].WG_TC_magazine_nominal_pocket
  PSW
  CM     Absolute
  PLW
  =      STG_TC_mag[BL_WZ_mag_index].WG_TC_mag_act_nom_pocket_difference
  <=[
    L      STG_TC_mag[BL_WZ_mag_index].WG_MP7261_TC_magazine_max_pocket
    /      K+2
  ]
  =      STG_TC_mag[BL_WZ_mag_index].ML_diff_lower_equal_half_mag
  IFF
    L      STG_TC_mag[BL_WZ_mag_index].WG_MP7261_TC_magazine_max_pocket
    -      STG_TC_mag[BL_WZ_mag_index].WG_TC_mag_act_nom_pocket_difference
    =      STG_TC_mag[BL_WZ_mag_index].WG_TC_mag_act_nom_pocket_difference

    PS     STG_TC_mag[BL_WZ_mag_index].WG_TC_mag_act_nom_pocket_difference
    CM     Absolute
    PL     STG_TC_mag[BL_WZ_mag_index].WG_TC_mag_act_nom_pocket_difference
  ENDI

  L      STG_TC_mag[BL_WZ_mag_index].WG_TC_magazine_actual_pocket
  >      STG_TC_mag[BL_WZ_mag_index].WG_TC_magazine_nominal_pocket
  =      STG_TC_mag[BL_WZ_mag_index].ML_nominal_greater_actual_posit

  L      STG_TC_mag[BL_WZ_mag_index].ML_diff_lower_equal_half_mag
  XO     STG_TC_mag[BL_WZ_mag_index].ML_nominal_greater_actual_posit
  =      STG_TC_mag[BL_WZ_mag_index].ML_TC_magazine_direction_cw

EM

;----------------------------
LBL Lock_pin_out                       ;05

  L      MG_marker_one
  S      STG_TC_mag[BL_WZ_mag_index].MG_O_TC_mag_lock_pin_out
  PSL

EM

;----------------------------
LBL Lock_pin_pos_out                   ;06

#if TC_lock_pin = &active
  LN     STG_TC_mag[BL_WZ_mag_index].MG_I_TC_mag_lock_pin_in
  A      STG_TC_mag[BL_WZ_mag_index].MG_I_TC_mag_lock_pin_out
  AN     STG_TC_mag[BL_WZ_mag_index].MG_TC_mag_interruption
  PSL

  L      STG_TC_mag[BL_WZ_mag_index].MG_TC_mag_interruption
  =      MG_win_TC_mag_lock_pin_out
#else
  PS     MG_marker_one
#endif

EM

;----------------------------
LBL Magazine_nom_pos                   ;07

  L      STG_TC_mag[BL_WZ_mag_index].MG_TC_mag_start
  A      STG_TC_mag[BL_WZ_mag_index].ML_TC_magazine_direction_cw
  =      STG_TC_mag[BL_WZ_mag_index].MG_O_TC_magazine_cw

  L      STG_TC_mag[BL_WZ_mag_index].MG_TC_mag_start
  AN     STG_TC_mag[BL_WZ_mag_index].ML_TC_magazine_direction_cw
  =      STG_TC_mag[BL_WZ_mag_index].MG_O_TC_magazine_ccw

  L      STG_TC_mag[BL_WZ_mag_index].MG_I_TC_magazine_counter
  O      STG_TC_mag[BL_WZ_mag_index].MG_I_TC_magazine_exact_position
  AN     I_TC_magazine_guard_closed
  AN     MG_guard_inactive
  S      STG_TC_mag[BL_WZ_mag_index].MG_TC_mag_interruption

  L      STG_TC_mag[BL_WZ_mag_index].MG_I_TC_magazine_counter
  O      STG_TC_mag[BL_WZ_mag_index].MG_I_TC_magazine_exact_position
  A      STG_TC_mag[BL_WZ_mag_index].MG_TC_mag_interruption
  R      STG_TC_mag[BL_WZ_mag_index].MG_O_TC_magazine_cw
  R      STG_TC_mag[BL_WZ_mag_index].MG_O_TC_magazine_ccw

#if TC_lock_pin = &active
  L      STG_TC_mag[BL_WZ_mag_index].MG_I_TC_mag_lock_pin_in
  ON     STG_TC_mag[BL_WZ_mag_index].MG_I_TC_mag_lock_pin_out
  R      STG_TC_mag[BL_WZ_mag_index].MG_O_TC_magazine_cw
  R      STG_TC_mag[BL_WZ_mag_index].MG_O_TC_magazine_ccw
#endif

  L      STG_TC_mag[BL_WZ_mag_index].WG_TC_magazine_actual_pocket
  <>     STG_TC_mag[BL_WZ_mag_index].WG_TC_magazine_nominal_pocket
  ON     STG_TC_mag[BL_WZ_mag_index].MG_I_TC_magazine_counter
  ON     STG_TC_mag[BL_WZ_mag_index].MG_I_TC_magazine_exact_position
  =      TS_TC_magazine_1_stop_delay[BL_WZ_mag_index]

  LN     TS_TC_magazine_1_stop_delay[BL_WZ_mag_index]
  AN     TR_TC_magazine_1_stop_delay[BL_WZ_mag_index]
  R      STG_TC_mag[BL_WZ_mag_index].MG_O_TC_magazine_cw
  R      STG_TC_mag[BL_WZ_mag_index].MG_O_TC_magazine_ccw
#if TC_lock_pin = &active
  R      STG_TC_mag[BL_WZ_mag_index].MG_O_TC_mag_lock_pin_out
#endif

  PSL

  L      STG_TC_mag[BL_WZ_mag_index].WG_TC_mag_act_nom_pocket_difference
  >      K+1
  AN     STG_TC_mag[BL_WZ_mag_index].MG_TC_mag_interruption
#if TC_lock_pin = &active
  AN     STG_TC_mag[BL_WZ_mag_index].MG_I_TC_mag_lock_pin_in
  A      STG_TC_mag[BL_WZ_mag_index].MG_I_TC_mag_lock_pin_out
  A      STG_TC_mag[BL_WZ_mag_index].MG_O_TC_mag_lock_pin_out
#endif
  =      STG_TC_mag[BL_WZ_mag_index].MG_O_TC_magazine_rapid_traverse

  L      STG_TC_mag[BL_WZ_mag_index].MG_O_TC_magazine_cw
  O      STG_TC_mag[BL_WZ_mag_index].MG_O_TC_magazine_ccw
  AN     STG_TC_mag[BL_WZ_mag_index].MG_O_TC_magazine_rapid_traverse
#if TC_lock_pin = &active
  AN     STG_TC_mag[BL_WZ_mag_index].MG_I_TC_mag_lock_pin_in
  A      STG_TC_mag[BL_WZ_mag_index].MG_I_TC_mag_lock_pin_out
  A      STG_TC_mag[BL_WZ_mag_index].MG_O_TC_mag_lock_pin_out
#endif
  =      STG_TC_mag[BL_WZ_mag_index].MG_O_TC_magazine_slow_motion

#if TC_lock_pin = &active
  L      STG_TC_mag[BL_WZ_mag_index].MG_I_TC_mag_lock_pin_in
  ON     STG_TC_mag[BL_WZ_mag_index].MG_I_TC_mag_lock_pin_out
  A      STG_TC_mag[BL_WZ_mag_index].MG_O_TC_mag_lock_pin_out
  A      STG_TC_mag[BL_WZ_mag_index].MG_TC_mag_interruption
  =      MG_win_TC_mag_lock_pin_out
#endif

  LN     STG_TC_mag[BL_WZ_mag_index].ML_TC_magazine_start_manual
  A      STG_TC_mag[BL_WZ_mag_index].MG_I_TC_magazine_counter
  A      STG_TC_mag[BL_WZ_mag_index].ML_TC_magazine_cw
  R      STG_TC_mag[BL_WZ_mag_index].ML_TC_magazine_cw
  CMT    Magazine_cw_stop

  LN     STG_TC_mag[BL_WZ_mag_index].ML_TC_magazine_start_manual
  A      STG_TC_mag[BL_WZ_mag_index].MG_I_TC_magazine_counter
  A      STG_TC_mag[BL_WZ_mag_index].ML_TC_magazine_ccw
  R      STG_TC_mag[BL_WZ_mag_index].ML_TC_magazine_ccw
  CMT    Magazine_ccw_stop

  L      MG_marker_one
  R      STG_TC_mag[BL_WZ_mag_index].ML_TC_magazine_start_manual

EM

;----------------------------
LBL Time_exact_stop                    ;08

  L      MG_marker_one
  R      TS_TC_magazine_1_exact_position[BL_WZ_mag_index]

  LN     TR_TC_magazine_1_exact_position[BL_WZ_mag_index]
  PSL

EM

;----------------------------
LBL Lock_pin_pos_in                    ;09

#if TC_lock_pin = &active
  L      STG_TC_mag[BL_WZ_mag_index].MG_I_TC_mag_lock_pin_in
  AN     STG_TC_mag[BL_WZ_mag_index].MG_I_TC_mag_lock_pin_out
  AN     STG_TC_mag[BL_WZ_mag_index].MG_TC_mag_interruption
  PSL

  L      STG_TC_mag[BL_WZ_mag_index].MG_TC_mag_interruption
  =      MG_win_TC_mag_lock_pin_in
#else
  PS     MG_marker_one
#endif

EM

;----------------------------
LBL Mag_exact_stop                     ;10

  L      STG_TC_mag[BL_WZ_mag_index].WG_TC_magazine_actual_pocket
  <>     STG_TC_mag[BL_WZ_mag_index].WG_TC_magazine_actual_2_pocket
  ON     STG_TC_mag[BL_WZ_mag_index].MG_I_TC_magazine_counter
  ON     STG_TC_mag[BL_WZ_mag_index].MG_I_TC_magazine_exact_position
  =      STG_TC_mag[BL_WZ_mag_index].MG_TC_mag_reference_necessary

  PS     MG_marker_one

EM

;----------------------------
LBL Standby_T_read                     ;11

  L      MG_marker_one
  S      MG_standby_tool_status_reading
  PSL

EM

;----------------------------
LBL Pocket_in_out                      ;12

  LN     MG_standby_tool_status_reading
  PSL
  EMF

#if TC_pocket_90_degree_shift = &active
  L      MG_manual_mode
  O      MG_SK_TC_mag_load_active
  EMT

  L      BG_standby_tool_status
  BT     K+1
  =      MG_tool_oversized

  LN     MG_tool_oversized
  AN     MG_pocket_up_always_after_TC
  R      STG_TC_mag[BL_WZ_mag_index].MG_O_TC_pocket_put_in
  S      STG_TC_mag[BL_WZ_mag_index].MG_O_TC_pocket_put_out
#endif

EM

;----------------------------
LBL Pocket_posit                       ;13

  L      MG_tool_oversized
  O      MG_pocket_up_always_after_TC
  O      MG_manual_mode
  O      MG_SK_TC_mag_load_active
  O      STG_TC_mag[BL_WZ_mag_index].MG_TC_mag_reference_necessary
  CMT    Pocket_pos_in
  CMF    Pocket_pos_out

EM

;----------------------------
LBL Magazine_end                       ;14

  PS     MG_marker_zero

LBL TC_magazine_reset

  L      MG_marker_one
  R      STG_TC_mag[BL_WZ_mag_index].MG_TC_mag_start
  R      STG_TC_mag[BL_WZ_mag_index].MG_TC_mag_interruption
  R      STG_TC_mag[BL_WZ_mag_index].MG_WZ_mag_load_unload_start
  R      MG_SK_T_load_unload
  R      STG_TC_mag[BL_WZ_mag_index].ML_TC_magazine_start_manual
  R      STG_TC_mag[BL_WZ_mag_index].ML_TC_magazine_ccw
  R      STG_TC_mag[BL_WZ_mag_index].ML_TC_magazine_cw
  R      STG_TC_mag[BL_WZ_mag_index].MG_O_TC_magazine_cw
  R      STG_TC_mag[BL_WZ_mag_index].MG_O_TC_magazine_ccw
  R      STG_TC_mag[BL_WZ_mag_index].MG_O_TC_magazine_rapid_traverse
  R      STG_TC_mag[BL_WZ_mag_index].MG_O_TC_magazine_slow_motion

  L      K+0
  =      STG_TC_mag[BL_WZ_mag_index].BG_step_TC_magazine

  L      S"            "
  =      SG_diagnostics_line_1
  =      SG_diagnostics_line_2
  =      SG_diagnostics_line_3

EM

;----------------------------
LBL TC_mag_cw_turn

  L      BG_TC_mag_index_Tool_Call
  =      BL_WZ_mag_index

#if TC_magazine_reference_sensor = &active
  L      STG_TC_mag[BL_WZ_mag_index].MG_TC_mag_reference_necessary
  EMT
#endif

  L      MG_manual_mode
  O      MG_edit_pocket_table
  =      STG_TC_mag[BL_WZ_mag_index].ML_TC_magazine_start_manual
  S      STG_TC_mag[BL_WZ_mag_index].ML_TC_magazine_cw

  LN     STG_TC_mag[BL_WZ_mag_index].MG_TC_mag_start
  S      STG_TC_mag[BL_WZ_mag_index].MG_TC_mag_start
  IFT
    L      KL_nominal_posit_cw
    =      STG_TC_mag[BL_WZ_mag_index].WG_TC_magazine_nominal_pocket
  ENDI

EM

;----------------------------
LBL Magazine_cw_stop

  L      STG_TC_mag[BL_WZ_mag_index].WG_TC_magazine_actual_pocket
  +      K+1
  =      STG_TC_mag[BL_WZ_mag_index].WG_TC_magazine_nominal_pocket

  L      STG_TC_mag[BL_WZ_mag_index].WG_TC_magazine_nominal_pocket
  >      STG_TC_mag[BL_WZ_mag_index].WG_MP7261_TC_magazine_max_pocket
  IFT
    L      K+1
    =      STG_TC_mag[BL_WZ_mag_index].WG_TC_magazine_nominal_pocket
  ENDI

  L      K+1
  =      STG_TC_mag[BL_WZ_mag_index].WG_TC_mag_act_nom_pocket_difference

EM

;----------------------------
LBL TC_mag_ccw_turn

  L      BG_TC_mag_index_Tool_Call
  =      BL_WZ_mag_index

#if TC_magazine_reference_sensor = &active
  L      STG_TC_mag[BL_WZ_mag_index].MG_TC_mag_reference_necessary
  EMT
#endif

  L      MG_manual_mode
  O      MG_edit_pocket_table
  =      STG_TC_mag[BL_WZ_mag_index].ML_TC_magazine_start_manual
  S      STG_TC_mag[BL_WZ_mag_index].ML_TC_magazine_ccw

  LN     STG_TC_mag[BL_WZ_mag_index].MG_TC_mag_start
  S      STG_TC_mag[BL_WZ_mag_index].MG_TC_mag_start
  IFT
    L      KL_nominal_posit_ccw
    =      STG_TC_mag[BL_WZ_mag_index].WG_TC_magazine_nominal_pocket
  ENDI

EM

;----------------------------
LBL Magazine_ccw_stop

  L      STG_TC_mag[BL_WZ_mag_index].WG_TC_magazine_actual_pocket
  -      K+1
  =      STG_TC_mag[BL_WZ_mag_index].WG_TC_magazine_nominal_pocket

  L      STG_TC_mag[BL_WZ_mag_index].WG_TC_magazine_nominal_pocket
  ==     K+0
  IFT
    L      STG_TC_mag[BL_WZ_mag_index].WG_MP7261_TC_magazine_max_pocket
    =      STG_TC_mag[BL_WZ_mag_index].WG_TC_magazine_nominal_pocket
  ENDI

  L      K+1
  =      STG_TC_mag[BL_WZ_mag_index].WG_TC_mag_act_nom_pocket_difference

EM

;----------------------------
LBL TC_mag_n_read_input

#if CFG_Simulation = "inactive"
  L      I_TC_mag_1_pocket_out
  =      STG_TC_mag[KG_TC_magazine_1_index].MG_I_TC_pocket_out

  L      I_TC_mag_1_pocket_in
  =      STG_TC_mag[KG_TC_magazine_1_index].MG_I_TC_pocket_in

  L      I_TC_mag_1_exact_position
  =      STG_TC_mag[KG_TC_magazine_1_index].MG_I_TC_magazine_exact_position

  L      I_TC_mag_1_counter
  =      STG_TC_mag[KG_TC_magazine_1_index].MG_I_TC_magazine_counter

  L      I_TC_mag_1_lock_pin_in
  =      STG_TC_mag[KG_TC_magazine_1_index].MG_I_TC_mag_lock_pin_in

  L      I_TC_mag_1_lock_pin_out
  =      STG_TC_mag[KG_TC_magazine_1_index].MG_I_TC_mag_lock_pin_out

  L      I_TC_mag_1_pocket_not_empty
  =      STG_TC_mag[BG_TC_mag_index_Tool_Call].MG_I_TC_pocket_not_empty

  L      I_TC_mag_2_pocket_out
  =      STG_TC_mag[KG_TC_magazine_2_index].MG_I_TC_pocket_out

  L      I_TC_mag_2_pocket_in
  =      STG_TC_mag[KG_TC_magazine_2_index].MG_I_TC_pocket_in

  L      I_TC_mag_2_exact_position
  =      STG_TC_mag[KG_TC_magazine_2_index].MG_I_TC_magazine_exact_position

  L      I_TC_mag_2_counter
  =      STG_TC_mag[KG_TC_magazine_2_index].MG_I_TC_magazine_counter

  L      I_TC_mag_2_lock_pin_in
  =      STG_TC_mag[KG_TC_magazine_2_index].MG_I_TC_mag_lock_pin_in

  L      I_TC_mag_2_lock_pin_out
  =      STG_TC_mag[KG_TC_magazine_2_index].MG_I_TC_mag_lock_pin_out

  L      I_TC_mag_2_pocket_not_empty
  =      STG_TC_mag[BG_TC_mag_index_Tool_Call].MG_I_TC_pocket_not_empty

#if TC_magazine_reference_sensor = &active
  L      I_TC_mag_1_reference
  =      STG_TC_mag[KG_TC_magazine_1_index].MG_I_TC_magazine_reference

  L      I_TC_mag_2_reference
  =      STG_TC_mag[KG_TC_magazine_2_index].MG_I_TC_magazine_reference
#endif
#endif

EM

;----------------------------
LBL TC_mag_n_write_output

  L      STG_TC_mag[KG_TC_magazine_1_index].MG_O_TC_pocket_put_out
  =      O_TC_mag_1_pocket_put_out

  L      STG_TC_mag[KG_TC_magazine_1_index].MG_O_TC_pocket_put_in
  =      O_TC_mag_1_pocket_put_in

  L      STG_TC_mag[KG_TC_magazine_1_index].MG_O_TC_magazine_cw
  =      O_TC_magazine_1_cw

  L      STG_TC_mag[KG_TC_magazine_1_index].MG_O_TC_magazine_ccw
  =      O_TC_magazine_1_ccw

  L      STG_TC_mag[KG_TC_magazine_1_index].MG_O_TC_magazine_rapid_traverse
  =      O_TC_magazine_1_rapid_traverse

  L      STG_TC_mag[KG_TC_magazine_1_index].MG_O_TC_magazine_slow_motion
  =      O_TC_magazine_1_slow_motion

  L      STG_TC_mag[KG_TC_magazine_1_index].MG_O_TC_mag_lock_pin_out
  =      O_TC_mag_1_lock_pin_out

  L      STG_TC_mag[KG_TC_magazine_1_index].MG_O_TC_mag_lock_pin_in
  =      O_TC_mag_1_lock_pin_in

  L      STG_TC_mag[KG_TC_magazine_2_index].MG_O_TC_pocket_put_out
  =      O_TC_mag_2_pocket_put_out

  L      STG_TC_mag[KG_TC_magazine_2_index].MG_O_TC_pocket_put_in
  =      O_TC_mag_2_pocket_put_in

  L      STG_TC_mag[KG_TC_magazine_2_index].MG_O_TC_magazine_cw
  =      O_TC_magazine_2_cw

  L      STG_TC_mag[KG_TC_magazine_2_index].MG_O_TC_magazine_ccw
  =      O_TC_magazine_2_ccw

  L      STG_TC_mag[KG_TC_magazine_2_index].MG_O_TC_magazine_rapid_traverse
  =      O_TC_magazine_2_rapid_traverse

  L      STG_TC_mag[KG_TC_magazine_2_index].MG_O_TC_magazine_slow_motion
  =      O_TC_magazine_2_slow_motion

  L      STG_TC_mag[KG_TC_magazine_2_index].MG_O_TC_mag_lock_pin_out
  =      O_TC_mag_2_lock_pin_out

  L      STG_TC_mag[KG_TC_magazine_2_index].MG_O_TC_mag_lock_pin_in
  =      O_TC_mag_2_lock_pin_in

EM

KFIELD Diagnostic
  KG_dia_TC_magazine_count_axis        ;-1
  KG_dia_magazine_start                ;00
  KG_dia_Tool_Def_quit                 ;01
  KG_dia_pocket_put_in                 ;02
  KG_dia_pocket_pos_in                 ;03
  KG_dia_magazine_direct               ;04
  KG_dia_lock_pin_out                  ;05
  KG_dia_lock_pin_pos_out              ;06
  KG_dia_magazine_nom_pos              ;07
  KG_dia_time_exact_stop               ;08
  KG_dia_lock_pin_pos_in               ;09
  KG_dia_mag_exact_stop                ;10
  KG_dia_standby_T_read                ;11
  KG_dia_pocket_in_out                 ;12
  KG_dia_pocket_posit                  ;13
  KG_dia_magazine_end                  ;14
  KG_dia_empty                         ;15
ENDK

