*-----------------------------------------------------------------------------
*  Alternating Table Mode
*-----------------------------------------------------------------------------
*  Module Description
*
*-----------------------------------------------------------------------------

#define /s    KL_absolute_without_software_limit_switch K +0

#define /s    ML_altern_table_mode_was_off             M
#define /s    ML_key_pressed                           M

#define /s    BL_step_alternating_table                B

#include Mac_Lib.inc

EXTERN Absolute

GLOBAL Alternat_table

;----------------------------
LBL Alternat_table
;----------------------------

  L      I_guard_1_closed_locked
  =      MG_guard_1_closed_locked

  L      I_guard_2_closed_locked
  =      MG_guard_2_closed_locked

  L      MG_pallet_change_alternat_table_inactive
  R      MG_alternating_table_on

  L      MG_alternating_table_on
  A      ML_altern_table_mode_was_off
  IFT
    L      DG_position_axis_X_ref
    >      DG_pos_1_X_alternating_table
    A[
      L      DG_position_axis_X_ref
      <      DG_pos_2_X_alternating_table
    ]
    R      MG_alternating_table_on
    =      PN_error_zone_alternating_table
    EMT

    LN     MG_manual_mode
    ON     MG_axes_in_position
    R      MG_alternating_table_on
  ENDI

*$WS
*  L      MG_manual_mode
*  A      MG_axes_in_position
*  IFT
*    LN    I_alternat_table_partition_removed
*    AN    NP_pallet_change_alternat_table_inactive
*    =     MG_alternating_table_on
*  ELSE
*    LN    I_alternat_table_partition_removed
*    XO    MG_alternating_table_on
*    S     PN_error_alternat_table_separation
*  ENDI

  L      MG_M61_alternating_table_1
  O      MG_M62_alternating_table_2
  AN     MG_alternating_table_on
  S      PN_error_inactive_alternating_table
  EMT

  L      MG_alternating_table_on
  A      ML_altern_table_mode_was_off
  IFT
    R      MG_axis_limit_3
    S      PN_M4135_strobe_axes_limit
    R      PN_M4575_axis_limit_Bit_1

    L      DG_position_axis_X_ref
    <      DG_limit_X_alternating_table
    =      MG_axis_limit_1
    =N     MG_axis_limit_2
    =N     PN_M4574_axis_limit_Bit_0
  ENDI

  LN     MG_alternating_table_on
  R      MG_new_workpiece_table_1
  R      MG_new_workpiece_table_2
  R      MG_interrupt_alternating_table
  R      O_lamp_table_1
  R      O_lamp_table_2
  =      ML_altern_table_mode_was_off
  EMT

  L      DG_position_axis_X_ref
  >=     DG_limit_X_alternating_table
  SN     O_lamp_table_1
  IFT
    L      MG_new_workpiece_table_1
    A      MG_clock_1_s
    =      O_lamp_table_1

    L      MG_M61_alternating_table_1
    IFT
      L      MG_clock_500_ms
      =      O_lamp_table_1
    ENDI
    LN     I_guard_1_closed_locked
    R      O_lamp_table_1
  ENDI

  L      DG_position_axis_X_ref
  <      DG_limit_X_alternating_table
  SN     O_lamp_table_2
  IFT
    L      MG_new_workpiece_table_2
    A      MG_clock_1_s
    =      O_lamp_table_2

    L      MG_M62_alternating_table_2
    IFT
      L      MG_clock_500_ms
      =      O_lamp_table_2
    ENDI
    LN     I_guard_2_closed_locked
    R      O_lamp_table_2
  ENDI

  L      MG_key_new_workpiece_inactive
  IFF
    L      MG_new_workpiece_table_1
    A      MG_axis_limit_2
    XO[
      L      MG_key_enable_table_1
      AN     ML_key_pressed
    ]
    A      I_guard_1_closed_locked
    =      MG_new_workpiece_table_1

    L      MG_new_workpiece_table_2
    A      MG_axis_limit_1
    XO[
      L      MG_key_enable_table_2
      AN     ML_key_pressed
    ]
    A      I_guard_2_closed_locked
    =      MG_new_workpiece_table_2

    L      MG_key_enable_table_1
    O      MG_key_enable_table_2
    =      ML_key_pressed
  ENDI

  CASE   BL_step_alternating_table
    CM     Alternat_table_on           ;00
    CM     Release                     ;01
    CM     Z_start_alt_table           ;02
    CM     Z_in_Pos                    ;03
    CM     Y_start_alt_table           ;04
    CM     Y_in_Pos                    ;05
    CM     X_1_2_Start                 ;06
    CM     X_1_2_in_Pos                ;07
    CM     Limit_1_2_Selection         ;08
    CM     Alt_table_quit              ;09
    CM     Alt_table_end               ;10
  ENDC

  PLL
  AN     MG_interrupt_alternating_table
  =      TS_PC_timeout
  IFT
    INC    BL_step_alternating_table
  ENDI

  LN     MG_enable_alternating_table
  S      TS_PC_timeout
  EMT

  L      MG_NC_start_pulse
  R      MG_interrupt_alternating_table

  L      MG_key_NC_start
  O      MG_pulse_CE_key
  S      TS_PC_timeout

  LN     TR_PC_timeout
  AN     TS_PC_timeout
  =      PN_error_timeout_alternating_table
  S      MG_interrupt_alternating_table

  L      MG_key_NC_stop
  O      MG_pulse_external_stop
  S      MG_interrupt_alternating_table

  L      MG_auto_manu_mode_change
  O      MG_pulse_internal_stop
  ON     MG_control_operational
  CMT    Alt_table_reset

EM

;----------------------------
LBL Alternat_table_on                  ;00

  L      MG_axis_limit_1
  A      MG_reference_axes_traversed
  S      MG_guard_2_closed_locked

  L      MG_axis_limit_2
  A      MG_reference_axes_traversed
  S      MG_guard_1_closed_locked

  L      MG_M61_alternating_table_1
  O      MG_M62_alternating_table_2
  AN     PN_M4092_quit_M_function
  S      MG_enable_alternating_table
  PSL

EM

;----------------------------
LBL Release                            ;01

  L      MG_axis_limit_1
  A      MG_M61_alternating_table_1
  O[
    L      MG_axis_limit_2
    A      MG_M62_alternating_table_2
  ]
  S      PN_M4092_quit_M_function
  JPT    Alt_table_end

  L      MG_key_new_workpiece_inactive
  S      MG_new_workpiece_table_1
  S      MG_new_workpiece_table_2

  L      MG_M61_alternating_table_1
  AN     MG_new_workpiece_table_1
  S      PN_error_enable_table_1

  L      MG_new_workpiece_table_1
  R      PN_error_enable_table_1

  L      MG_M62_alternating_table_2
  AN     MG_new_workpiece_table_2
  S      PN_error_enable_table_2

  L      MG_new_workpiece_table_2
  R      PN_error_enable_table_2

  L      I_guard_1_closed_locked
  A      I_guard_2_closed_locked
  O      MG_guard_inactive
  AN     PN_error_enable_table_1
  AN     PN_error_enable_table_2
  A      MG_control_in_operation_on
  AN     NP_M4157_mode_restore_position
  AN     NP_M4193_RUNCANCEL_macro_active
  =N     TS_PC_timeout
  PSL

EM

;----------------------------
LBL Z_start_alt_table                  ;02

  PS     KG_axis_Z
  PS     DG_pos_Z_alternating_table
  PS     DG_feed_PLC_Z
  PS     KL_absolute_without_software_limit_switch
  CM     9221
  PLW
  ==     K+0
  PSL
  M_display_module_error

EM

;----------------------------
LBL Z_in_Pos                           ;03

  L      DG_position_axis_Z_ref
  -      DG_pos_Z_alternating_table
  PSW
  CM     Absolute
  PLW
  <      DG_MP1030_pos_window_Z
  AN     PN_M4122_strobe_PLC_posit_axis_Z
  AN     MG_interrupt_alternating_table
  PSL

EM

;----------------------------
LBL Y_start_alt_table                  ;04

  PS     KG_axis_Y
  PS     DG_pos_Y_alternating_table
  PS     DG_feed_PLC_Y
  PS     KL_absolute_without_software_limit_switch
  CM     9221
  PLW
  ==     K+0
  PSL
  M_display_module_error

EM

;----------------------------
LBL Y_in_Pos                           ;05

  L      DG_position_axis_Y_ref
  -      DG_pos_Y_alternating_table
  PSW
  CM     Absolute
  PLW
  <      DG_MP1030_pos_window_Y
  AN     PN_M4121_strobe_PLC_posit_axis_Y
  AN     MG_interrupt_alternating_table
  PSL

EM

;----------------------------
LBL X_1_2_Start                        ;06

  L      MG_M61_alternating_table_1
  CMT    X_Limit_1_Start
  CMF    X_Limit_2_Start

EM

;----------------------------
LBL X_1_2_in_Pos                       ;07

  L      MG_M61_alternating_table_1
  CMT    X_in_Limit_1_Pos
  CMF    X_in_Limit_2_Pos

EM

;----------------------------
LBL X_Limit_1_Start

  PS     KG_axis_X
  PS     DG_pos_1_X_alternating_table
  PS     DG_feed_PLC_X
  PS     KL_absolute_without_software_limit_switch
  CM     9221
  PLW
  ==     K+0
  PSL
  M_display_module_error

EM

;----------------------------
LBL X_in_Limit_1_Pos

  L      DG_position_axis_X_ref
  -      DG_pos_1_X_alternating_table
  PSW
  CM     Absolute
  PLW
  <      DG_MP1030_pos_window_X
  AN     PN_M4120_strobe_PLC_posit_axis_X
  AN     MG_interrupt_alternating_table
  PSL

EM

;----------------------------
LBL X_Limit_2_Start

  PS     KG_axis_X
  PS     DG_pos_2_X_alternating_table
  PS     DG_feed_PLC_X
  PS     KL_absolute_without_software_limit_switch
  CM     9221
  PLW
  ==     K+0
  PSL
  M_display_module_error

EM

;----------------------------
LBL X_in_Limit_2_Pos

  L      DG_position_axis_X_ref
  -      DG_pos_2_X_alternating_table
  PSW
  CM     Absolute
  PLW
  <      DG_MP1030_pos_window_X
  AN     PN_M4120_strobe_PLC_posit_axis_X
  AN     MG_interrupt_alternating_table
  PSL

EM

;----------------------------
LBL Limit_1_2_Selection                ;08

  L      MG_M61_alternating_table_1
  =      MG_axis_limit_1
  =N     MG_axis_limit_2
  =N     PN_M4574_axis_limit_Bit_0

  PS     MG_marker_one

EM

;----------------------------
LBL Alt_table_quit                     ;09

  L      MG_M61_alternating_table_1
  O      MG_M62_alternating_table_2
  AN     PN_M4135_strobe_axes_limit
  S      PN_M4092_quit_M_function

  LN     NP_M4072_strobe_M_function
  AN     PN_M4135_strobe_axes_limit
  R      MG_new_workpiece_table_1
  R      MG_new_workpiece_table_2
  PSL

EM

;----------------------------
LBL Alt_table_end                      ;10

  PS     MG_marker_zero

LBL Alt_table_reset

  L      MG_marker_one
  R      MG_enable_alternating_table
  R      MG_interrupt_alternating_table

  L      K+0
  =      BL_step_alternating_table

EM
