*-----------------------------------------------------------------------------
*  Pallet changer linear
*-----------------------------------------------------------------------------
*  Module Description
*
*-----------------------------------------------------------------------------

#define /s    KL_absolute_with_software_limit_switch   K %010
#define /s    KL_PC_number_pallet_2                    K +2
#define /s    KL_pallet_0                              K +0
#define /s    KL_pallet_1                              K +1
#define /s    KL_pallet_2                              K +2

#define /s    ML_stack_synchronize                     M
#define /s    ML_PC_ready                              M
#define /s    ML_key_pressed                           M
#define /s    ML_X_in_PC1_position                     M
#define /s    ML_X_in_PC2_position                     M

#include Mac_Lib.inc

EXTERN Absolute
GLOBAL Pal_changer

;----------------------------
LBL Pal_changer
;----------------------------

  L      MG_pallet_change_alternat_table_inactive
  IFT
    L      MG_M60_pallet_change
    O      MG_M61_pallet_1_to_machine
    O      MG_M62_pallet_2_to_machine
    S      PN_error_M_function_not_used
EM
  ENDI

  L      MG_new_workpiece_table_1
  O      MG_new_workpiece_table_2
  =      O_lamp_PC_release

  L      MG_M60_pallet_change
  O      MG_M61_pallet_1_to_machine
  O      MG_M62_pallet_2_to_machine
  IFT
    L      MG_clock_500_ms
    R      O_lamp_PC_release
  ENDI

  L      MG_key_new_workpiece_inactive
  S      MG_new_workpiece_table_1
  S      MG_new_workpiece_table_2
  IFF
    L      MG_key_enable_new_workpiece
    AN     ML_key_pressed
    A      I_PC_position_1_occupy
    XO     MG_new_workpiece_table_1
    =      MG_new_workpiece_table_1
  ENDI

  L      MG_key_enable_new_workpiece
  AN     ML_key_pressed
  A      I_PC_position_2_occupy
  XO     MG_new_workpiece_table_2
  =      MG_new_workpiece_table_2

  L      MG_key_enable_new_workpiece
  =      ML_key_pressed

  L      MG_PC_active
  R      TS_PC_timeout
  S      TS_PC_timer_universal

  CASE   BG_step_pallet_change
    CM     PC_activate                 ;00
    CM     PC_release                  ;01
    CM     Z_start                     ;02
    CM     Z_in_PC_pos                 ;03
    CM     B_start                     ;04
    CM     B_in_PC_pos                 ;05
    CM     PC_door_open                ;06
    CM     Jump_machine_is_empty       ;07 ---> (1)
    CM     X_PC_start_out              ;08
    CM     X_in_PC_pos_out             ;09
    CM     PC_door_opened              ;10
    CM     PC_ma_to_stop               ;11
    CM     PC_in_stop_pos              ;12
    CM     Pal_unclamp                 ;13
    CM     Pal_unclamped               ;14
    CM     PC_time                     ;15
    CM     PC_to_load                  ;16
    CM     PC_in_load_pos              ;17
    CM     Jump_machine_empty          ;18 ---> (2)
    CM     Pal_unclamp                 ;19 <--- (1)
    CM     Pal_unclamped               ;20
    CM     X_PC_start_in               ;21
    CM     X_in_PC_pos_in              ;22
    CM     PC_load_to_stop             ;23
    CM     PC_in_stop_pos              ;24
    CM     Pal_clamp                   ;25
    CM     Pal_clamped                 ;26
    CM     PC_to_machine               ;27
    CM     PC_in_machine               ;28
    CM     Pal_clamp                   ;29 <--- (2)
    CM     Pal_clamped                 ;30
    CM     PC_door_close               ;31
    CM     PC_door_closed              ;32
    CM     PC_quit                     ;33
    CM     PC_end                      ;34
  ENDC

  PLL
  S      TS_PC_timeout
  IFT
    INC    BG_step_pallet_change
  ENDI

*  L      MG_PW_diagnosis
*  IFT
*    L      KL_offset_PC_translat_1
*    +      BG_step_PC
*    =X
*
*    L      S#D0[X]
*    =      SG_diagnosis_line_1
*    INCX
*
*    L      S#D0[X]
*    =      SG_diagnosis_line_2
*    INCX
*
*    L      S#D0[X]
*    =      SG_diagnosis_line_3
*  ENDI

  LN     MG_PC_active
  EMT

  L      MG_NC_start_pulse
  R      MG_PC_interruption

  L      MG_key_NC_start
  O      MG_pulse_CE_key
  S      TS_PC_timeout

  LN     TR_PC_timeout
  AN     TS_PC_timeout
  =      PN_error_PC_timeout
  S      MG_PC_interruption

  L      MG_key_NC_stop
  O      MG_pulse_external_stop
  S      MG_PC_interruption

  L      MG_pulse_internal_stop
  O      MG_auto_manu_mode_change
  ON     MG_control_operational
  CMT    PC_reset

EM

;----------------------------
LBL PC_activate                        ;00

  L      MG_M60_pallet_change
  O      MG_M61_pallet_1_to_machine
  O      MG_M62_pallet_2_to_machine
  A      MG_PC_standby_position
  S      MG_PC_active
  PSL

EM

;----------------------------
LBL PC_release                         ;01

  L      MG_marker_one
  S      TS_PC_timeout

  L      MG_M60_pallet_change
  A      I_PC_position_1_occupy
  A      I_PC_position_2_occupy
  S      PN_M4092_quit_M_function
  JPT    PC_end

  L      MG_M61_pallet_1_to_machine
  AN     I_PC_position_1_occupy
  S      PN_M4092_quit_M_function
  JPT    PC_end

  L      MG_M62_pallet_2_to_machine
  AN     I_PC_position_2_occupy
  S      PN_M4092_quit_M_function
  JPT    PC_end

  L      MG_M61_pallet_1_to_machine
  AN     MG_new_workpiece_table_1
  =      PN_error_enable_table_1
  IFT
    L      MG_clock_1_s
    =      O_lamp_PC_release
  ENDI

  L      MG_M62_pallet_2_to_machine
  AN     MG_new_workpiece_table_2
  =      PN_error_enable_table_2
  IFT
    L      MG_clock_1_s
    =      O_lamp_PC_release
  ENDI

  LN     I_PC_load_door_closed
  S      PN_error_PC_load_door_opened

  L      I_guard_1_closed_locked
  A      I_guard_2_closed_locked
  AN     PN_error_PC_load_door_opened
  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
  PSL

EM

;----------------------------
LBL Pal_unclamp                        ;11

  L      MG_marker_one
  S      O_pallet_in_machine_unclamping
  R      O_pallet_in_machine_clamping
  PSL

EM

;----------------------------
LBL Pal_clamp                          ;20

  L      MG_marker_one
  R      O_pallet_in_machine_unclamping
  S      O_pallet_in_machine_clamping
  PSL

EM

;----------------------------
LBL Pal_unclamped                      ;12

  L      I_pallet_in_machine_unclamped
  AN     I_pallet_in_machine_clamped
  AN     MG_PC_interruption
  PSL

  L      MG_PC_interruption
  =      MG_win_pal_in_machine_unclamping

EM

;----------------------------
LBL Pal_clamped                        ;21

  LN     I_pallet_in_machine_unclamped
  A      I_pallet_in_machine_clamped
  AN     MG_PC_interruption
  PSL

  L      MG_PC_interruption
  =      MG_win_pal_in_machine_clamping

EM

;----------------------------
LBL PC_door_open                       ;03

  L      MG_marker_one
  S      O_PC_door_open
  R      O_PC_door_close
  PSL

EM

;----------------------------
LBL PC_door_opened                     ;08

  L      I_PC_door_opened
  AN     I_PC_door_closed
  AN     MG_PC_interruption
  PSL

  L      MG_PC_interruption
  =      MG_win_PC_door_open

EM

;----------------------------
LBL PC_door_close                      ;24

  L      MG_marker_one
  R      O_PC_door_open
  S      O_PC_door_close
  PSL

EM

;----------------------------
LBL PC_door_closed                     ;25

  LN     I_PC_door_opened
  A      I_PC_door_closed
  AN     MG_PC_interruption
  PSL

  L      MG_PC_interruption
  =      MG_win_PC_door_close

EM

;----------------------------
LBL Jump_machine_is_empty              ;05

  L      I_PC_position_1_occupy
  A      I_PC_position_2_occupy
  IFT
    L      BG_step_pallet_change
    +      K+11
    =      BG_step_pallet_change
  ENDI

  PS     MG_marker_one

EM

;----------------------------
LBL Jump_machine_empty                 ;15

  L      MG_M60_pallet_change
  IFT
    L      BG_step_pallet_change
    +      K+10
    =      BG_step_pallet_change
  ENDI

  PS     MG_marker_one

EM

;----------------------------
LBL PC_load_to_stop                    ;18
LBL PC_to_machine                      ;22

  L      ML_X_in_PC1_position
  R      O_PC_1_to_load_position
  S      O_PC_1_to_machine_position

  L      ML_X_in_PC2_position
  R      O_PC_2_to_load_position
  S      O_PC_2_to_machine_position

  PS     MG_marker_one

EM

;----------------------------
LBL PC_ma_to_stop                      ;09
LBL PC_to_load                         ;13

  L      ML_X_in_PC1_position
  S      O_PC_1_to_load_position
  R      O_PC_1_to_machine_position

  L      ML_X_in_PC2_position
  S      O_PC_2_to_load_position
  R      O_PC_2_to_machine_position

  PS     MG_marker_one

EM

;----------------------------
LBL PC_in_stop_pos                     ;10 ;19

  L      I_PC_1_in_stop_position
  R      O_PC_1_to_load_position
  R      O_PC_1_to_machine_position

  L      I_PC_2_in_stop_position
  R      O_PC_2_to_load_position
  R      O_PC_2_to_machine_position

  LN     O_PC_1_to_load_position
  AN     O_PC_1_to_machine_position
  AN     O_PC_2_to_load_position
  AN     O_PC_2_to_machine_position
  AN     MG_PC_interruption
  PSL

  L      MG_PC_interruption
  =      MG_win_PC_to_stop_position

EM

;----------------------------
LBL PC_in_load_pos                     ;14

  L      I_PC_1_in_load_position
  R      O_PC_1_to_load_position
  R      O_PC_1_to_machine_position

  L      I_PC_2_in_load_position
  R      O_PC_2_to_load_position
  R      O_PC_2_to_machine_position

  LN     O_PC_1_to_load_position
  AN     O_PC_1_to_machine_position
  AN     O_PC_2_to_load_position
  AN     O_PC_2_to_machine_position
  AN     MG_PC_interruption
  PSL

  L      MG_PC_interruption
  =      MG_win_PC_to_load_position

EM

;----------------------------
LBL PC_in_machine                      ;23

  L      I_PC_1_in_machine_position
  R      O_PC_1_to_load_position
  R      O_PC_1_to_machine_position

  L      I_PC_2_in_machine_position
  R      O_PC_2_to_load_position
  R      O_PC_2_to_machine_position

  LN     O_PC_1_to_load_position
  AN     O_PC_1_to_machine_position
  AN     O_PC_2_to_load_position
  AN     O_PC_2_to_machine_position
  AN     MG_PC_interruption
  PSL

  L      MG_PC_interruption
  =      MG_win_PC_to_machine_position

EM

;----------------------------
LBL X_PC_start_out                     ;06

  L      MG_M60_pallet_change
  IFT
    L      I_PC_position_1_occupy
    AN     I_PC_position_2_occupy
    JPT    X_PC2_start

    LN     I_PC_position_1_occupy
    A      I_PC_position_2_occupy
    JPT    X_PC1_start
  ENDI

  L      MG_M61_pallet_1_to_machine
  A      I_PC_position_1_occupy
  AN     I_PC_position_2_occupy
  JPT    X_PC2_start

  L      MG_M62_pallet_2_to_machine
  AN     I_PC_position_1_occupy
  A      I_PC_position_2_occupy
  JPT    X_PC1_start

  L      MG_marker_one
  S      MG_PC_interruption
  S      MG_win_error_PC_position_sensors

  PS     MG_marker_zero

EM

;----------------------------
LBL X_PC_start_in                      ;16

  L      MG_M61_pallet_1_to_machine
  A      I_PC_position_1_occupy
  A      I_PC_position_2_occupy
  JPT    X_PC1_start

  L      MG_M62_pallet_2_to_machine
  A      I_PC_position_1_occupy
  A      I_PC_position_2_occupy
  JPT    X_PC2_start

  L      MG_marker_one
  S      MG_PC_interruption
  S      MG_win_error_PC_position_sensors

  PS     MG_marker_zero

EM

;----------------------------
LBL X_in_PC_pos_out                    ;07

  L      MG_M60_pallet_change
  IFT
    L      I_PC_position_1_occupy
    AN     I_PC_position_2_occupy
    JPT    X_in_PC2_pos

    LN     I_PC_position_1_occupy
    A      I_PC_position_2_occupy
    JPT    X_in_PC1_pos
  ENDI

  L      MG_M61_pallet_1_to_machine
  A      I_PC_position_1_occupy
  AN     I_PC_position_2_occupy
  JPT    X_in_PC2_pos

  L      MG_M62_pallet_2_to_machine
  AN     I_PC_position_1_occupy
  A      I_PC_position_2_occupy
  JPT    X_in_PC1_pos

  L      MG_marker_one
  S      MG_PC_interruption
  S      MG_win_error_PC_position_sensors

  PS     MG_marker_zero

EM

;----------------------------
LBL X_in_PC_pos_in                     ;17

  L      MG_M61_pallet_1_to_machine
  A      I_PC_position_1_occupy
  A      I_PC_position_2_occupy
  JPT    X_in_PC1_pos

  L      MG_M62_pallet_2_to_machine
  A      I_PC_position_1_occupy
  A      I_PC_position_2_occupy
  JPT    X_in_PC2_pos

  L      MG_marker_one
  S      MG_PC_interruption
  S      MG_win_error_PC_position_sensors

  PS     MG_marker_zero

EM

;----------------------------
LBL Z_start                            ;02

  L      MG_PC_interruption
  IFT
    PS     MG_marker_zero
  ELSE
    PS     KG_axis_Z
    PS     DG_pos_Z_alternating_table
    PS     DG_feed_PLC_Z
    PS     KL_absolute_with_software_limit_switch
    CM     9221
    PLW
    ==     K+0
    PSL
    M_display_module_error
  ENDI

EM

;----------------------------
LBL Z_in_PC_pos                        ;03

  L      MG_PC_interruption
  =      MG_win_PC_posit_Z

  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_PC_interruption
  PSL

EM

;----------------------------
LBL B_start                            ;02

  L      MG_PC_interruption
  IFT
    PS     MG_marker_zero
  ELSE
    PS     KG_axis_4
    PS     K+0
    PS     DG_feed_PLC_4
    PS     KL_absolute_with_software_limit_switch
    CM     9221
    PLW
    ==     K+0
    PSL
    M_display_module_error
  ENDI

EM

;----------------------------
LBL B_in_PC_pos                        ;04

  L      MG_PC_interruption
  =      MG_win_PC_posit_4

  L      DG_position_axis_4_ref
  <      DG_MP1030_pos_window_4
  O[
    L      K+3600000
    -      DG_position_axis_4_ref
    <      DG_MP1030_pos_window_4
  ]
  AN     PN_M4123_strobe_PLC_posit_axis_4
  AN     MG_PC_interruption
  PSL

EM

;----------------------------
LBL X_PC1_start

  L      MG_PC_interruption
  IFT
    PS     MG_marker_zero
  ELSE
    PS     KG_axis_X
    PS     DG_pos_1_X_alternating_table
    PS     DG_feed_PLC_X
    PS     KL_absolute_with_software_limit_switch
    CM     9221
    PLW
    ==     K+0
    PSL
      M_display_module_error
  ENDI

EM

;----------------------------
LBL X_PC2_start

  L      MG_PC_interruption
  IFT
    PS     MG_marker_zero
  ELSE
    PS     KG_axis_X
    PS     DG_pos_2_X_alternating_table
    PS     DG_feed_PLC_X
    PS     KL_absolute_with_software_limit_switch
    CM     9221
    PLW
    ==     K+0
    PSL
      M_display_module_error
  ENDI

EM

;----------------------------
LBL X_in_PC1_pos

  L      MG_PC_interruption
  =      MG_win_PC_posit_X

  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_PC_interruption
  S      ML_X_in_PC1_position
  R      ML_X_in_PC2_position
  PSL

EM

;----------------------------
LBL X_in_PC2_pos

  L      MG_PC_interruption
  =      MG_win_PC_posit_X

  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_PC_interruption
  R      ML_X_in_PC1_position
  S      ML_X_in_PC2_position
  PSL

EM

;----------------------------
LBL PC_quit                            ;26

  LN     NP_M4072_strobe_M_function
  AN     MG_PC_interruption
  PSL

  LN     MG_PC_interruption
  S      PN_M4092_quit_M_function

EM

;----------------------------
LBL PC_end                             ;27

  PS     MG_marker_zero

LBL PC_reset

  L      ML_X_in_PC1_position
  R      MG_new_workpiece_table_1

  L      ML_X_in_PC2_position
  R      MG_new_workpiece_table_2

  L      MG_marker_one
  R      O_PC_2_to_load_position
  R      O_PC_2_to_machine_position
  R      ML_X_in_PC1_position
  R      ML_X_in_PC2_position
  R      MG_PC_active
  R      MG_PC_interruption

  L      K+0
  =      BG_step_pallet_change

EM

;----------------------------
LBL PC_time

  LN     TR_PC_timer_universal
  =      TS_PC_timer_universal
  PSL

EM
