*-----------------------------------------------------------------------------
*  Tool Manual
*-----------------------------------------------------------------------------
*  Module Description
*
*-----------------------------------------------------------------------------

#define /s    KL_absolute_with_software_limit_switch   K %010
#define /s    KL_tool_without_pocket                   K -1

#define /s    ML_TOOL_CALL_old                         M
#define /s    ML_key_pressed                           M
#define /s    ML_foot_switch_pressed                   M
#define /s    ML_TC_posit_X_necessary                  M
#define /s    ML_TC_posit_Y_necessary                  M

#define /s    BL_step_T_change                         B

#include Mac_Lib.inc

EXTERN Absolute

GLOBAL TC_manual

;----------------------------
LBL TC_manual
;----------------------------

  CM     Sn_Input_read

  L      PN_M4543_T_life_exceeded_TIME1
  R      PN_M4543_T_life_exceeded_TIME1
  S      PN_error_tool_life_max

  LN     NP_M4073_strobe_TOOL_CALL
  R      PN_M4093_quit_TOOL_CALL

  L      MG_TC_manual
  O      MG_TC_automatic
  =      PN_M4539_T_status_inverse

  L      MG_Sn_I_tool_unclamped
  ON     MG_Sn_I_tool_clamped
  O      MG_Sn_O_tool_unclamping
  S      PN_error_tool_not_clamped

  L      PN_M4539_T_status_inverse
  A      MG_automatic_mode
  R      PN_error_tool_not_clamped

  L      PN_M4007_S_M05_nominal_0_and_status
;  O      PN_M4008_S_nominal_off_status_inverse
  A      MG_manual_mode
  R      PN_error_tool_not_clamped

;  L      MG_key_NC_start_p_pulse
;  A      MG_TC_without_Tool_Call
;  A      MG_automatic_mode
;  S      PN_error_tool_spindle_ok
;
;  L      MG_key_NC_start_p_pulse
;  A      PN_error_tool_spindle_ok
;  A      MG_win_window_yes_no
;  O      MG_pulse_internal_stop
;  ON     MG_TC_without_Tool_Call
;  R      PN_error_tool_spindle_ok
;
;  L      PN_error_tool_spindle_ok
;  =      MG_win_window_yes_no

  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      MG_key_release_toolchange
  O      MG_SK_release_toolchange
  A      MG_SK_enable_release_toolchange
  AN     ML_key_pressed
  S      MG_TC_manual
  S      O_lamp_release_toolchange     ;20120926

  L      NP_M4073_strobe_TOOL_CALL
  IFT
    L      NP_W262_tool_pocket_number
    ==     KL_tool_without_pocket
    ON     MG_TC_auto_active
    AN     NP_M4175_control_in_operation_flashes
    AN     MG_TC_automatic
    S      MG_TC_manual
    IFT
      L      NP_W262_tool_pocket_number
      =      WG_TC_pocket_number
      L      NP_W264_tool_number
      =      WG_TC_manu_tool_number
    ENDI
  ENDI

  L      MG_test_lamps
  =      O_lamp_TC_enable

  L      MG_Sn_I_tool_unclamped
  ON     MG_Sn_I_tool_clamped
  O      MG_test_lamps
  =      O_lamp_tool_unclamped

  LN     MG_Sn_O_tool_unclamping
  =      MG_Sn_O_tool_clamping

  CASE   BL_step_T_change
    CM     TC_manual_start             ;00
    CM     Jump_manu_mode              ;01  ---> (1)
    CM     Z_start_safety              ;02
    CM     Z_in_pos_safety             ;03
    CM     X_start_manu                ;04
    CM     X_in_pos_manu               ;05
    CM     Y_start_manu                ;06
    CM     Y_in_pos_manu               ;07
    CM     Z_start_manu                ;08
    CM     Z_in_pos_manu               ;09
    CM     Tool_manu_unclamping        ;10  <--- (1)
    CM     Tool_manu_clamping          ;11
    CM     TC_disable                  ;12
    CM     TC_quit                     ;13
    CM     Restore_necessary           ;14
    CM     Restore_pos_ok              ;15
    CM     Tool_Call_quitt             ;16
  ENDC

  PLL
  IFT
    INC    BL_step_T_change
  ENDI

  L      MG_key_release_toolchange
  O      MG_key_tool_unclamping
  =      ML_key_pressed

  L      I_key_tool_unclamping_foot_switch
  =      ML_foot_switch_pressed
  CM     Sn_Output_write

  LN     MG_TC_manual
  EMT

  L      MG_NC_start_pulse
  R      MG_TC_manu_interruption

  L      MG_key_NC_stop
  S      MG_TC_manu_interruption
  S      PN_error_TC_timeout

  L      MG_key_NC_start
  R      PN_error_TC_timeout

  L      MG_pulse_internal_stop
  O      MG_manu_auto_mode_change
  O      MG_auto_manu_mode_change
  ON     MG_control_operational
  CMT    TC_manual_reset

EM

;----------------------------
LBL TC_manual_start                    ;00

  L      MG_spindle_stand_still
  ON     MG_spindle_active
  AN     MG_axes_in_motion
  A[
    L      MG_automatic_mode
    AN     NP_M4176_control_in_operation_on_or_flashes
    O      MG_manual_mode
  ]
  AN     MG_TC_help
  =      MG_SK_enable_release_toolchange
  RN     MG_SK_release_toolchange

  L      MG_TC_manual
  AN     MG_TC_automatic
  S      MG_spindle_off_orientation

  L      MG_TC_manual
  AN     MG_TC_automatic
  A      MG_spindle_off
  R      MG_SK_enable_release_toolchange
  S      MG_SK_release_toolchange
  S      MG_TC_manual_active
  PSL
  EMF

  L      NP_M4073_strobe_TOOL_CALL
  S      MG_tool_status_reading
  =N     MG_TC_without_Tool_Call

EM

;----------------------------
LBL Jump_manu_mode                     ;01

  PS     MG_marker_one

  LN     NP_M4073_strobe_TOOL_CALL
  O      MG_TC_position_manual_inactive
  IFT
    L      BL_step_T_change
    +      K+8
    =      BL_step_T_change
  ENDI

EM

;----------------------------
LBL Z_start_safety                     ;02

  L      MG_TC_manu_interruption
  =      MG_win_TC_pos_Z_safety
  IFT
    PS     MG_marker_zero
  ELSE

#if CFG_pos_X_manual = "active"
    L      DG_position_axis_X_ref
    -      DG_TC_pos_X_manual
    PSW
    CM     Absolute
    PLW
    >      DG_MP1030_pos_window_X
    =      ML_TC_posit_X_necessary
#endif

#if CFG_pos_Y_manual = "active"
    L      DG_position_axis_Y_ref
    -      DG_TC_pos_Y_manual
    PSW
    CM     Absolute
    PLW
    >      DG_MP1030_pos_window_Y
    =      ML_TC_posit_Y_necessary
#endif

    LN     ML_TC_posit_X_necessary
    AN     ML_TC_posit_Y_necessary
    IFT
      PS     MG_marker_one
    ELSE
      PS     KG_axis_Z
      PS     DG_TC_pos_Z_safety
      PS     DG_feed_PLC_Z
      PS     KL_absolute_with_software_limit_switch
      CM     9221
      PLW
      ==     K+0
      PSL
      M_display_module_error
    ENDI
  ENDI

EM

;----------------------------
LBL Z_in_pos_safety                    ;03

  LN     ML_TC_posit_X_necessary
  AN     ML_TC_posit_Y_necessary
  IFT
    PS     MG_marker_one
  ELSE
    L      DG_position_axis_Z_ref
    -      DG_TC_pos_Z_safety
    PSW
    CM     Absolute
    PLW
    <      DG_MP1030_pos_window_Z
    AN     PN_M4122_strobe_PLC_posit_axis_Z
    AN     MG_TC_manu_interruption
    PSL

    L      MG_TC_manu_interruption
    =      MG_win_TC_pos_Z_safety
  ENDI

EM

;----------------------------
LBL X_start_manu                       ;04

#if CFG_pos_X_manual = "active"
  L      MG_TC_manu_interruption
  =      MG_win_TC_pos_X
  IFT
    PS     MG_marker_zero
  ELSE
    PS     KG_axis_X
    PS     DG_TC_pos_X_manual
    PS     DG_feed_PLC_X
    PS     KL_absolute_with_software_limit_switch
    CM     9221
    PLW
    ==     K+0
    PSL
    M_display_module_error
  ENDI
#else
  PS     MG_marker_one
#endif

EM

;----------------------------
LBL X_in_pos_manu                      ;05

#if CFG_pos_X_manual = "active"
  L      DG_position_axis_X_ref
  -      DG_TC_pos_X_manual
  PSW
  CM     Absolute
  PLW
  <      DG_MP1030_pos_window_X
  AN     PN_M4120_strobe_PLC_posit_axis_X
  AN     MG_TC_manu_interruption
  PSL

  L      MG_TC_manu_interruption
  =      MG_win_TC_pos_X
#else
  PS     MG_marker_one
#endif

EM

;----------------------------
LBL Y_start_manu                       ;06

#if CFG_pos_Y_manual = "active"
  L      MG_TC_manu_interruption
  =      MG_win_TC_pos_Y_manual
  IFT
    PS     MG_marker_zero
  ELSE
    PS     KG_axis_Y
    PS     DG_TC_pos_Y_manual
    PS     DG_feed_PLC_Y
    PS     KL_absolute_with_software_limit_switch
    CM     9221
    PLW
    ==     K+0
    PSL
    M_display_module_error
  ENDI
#else
  PS     MG_marker_one
#endif

EM

;----------------------------
LBL Y_in_pos_manu                      ;07

#if CFG_pos_Y_manual = "active"
  L      DG_position_axis_Y_ref
  -      DG_TC_pos_Y_manual
  PSW
  CM     Absolute
  PLW
  <      DG_MP1030_pos_window_Y
  AN     PN_M4121_strobe_PLC_posit_axis_Y
  AN     MG_TC_manu_interruption
  PSL

  L      MG_TC_manu_interruption
  =      MG_win_TC_pos_Y_manual
#else
  PS     MG_marker_one
#endif

EM

;----------------------------
LBL Z_start_manu                       ;08

#if CFG_pos_Z_manual = "active"
  L      MG_TC_manu_interruption
  =      MG_win_TC_pos_Z_manual
  IFT
    PS     MG_marker_zero
  ELSE
    PS     KG_axis_Z
    PS     DG_TC_pos_Z_manual
    PS     DG_feed_PLC_Z
    PS     KL_absolute_with_software_limit_switch
    CM     9221
    PLW
    ==     K+0
    PSL
    M_display_module_error
  ENDI
#else
  PS     MG_marker_one
#endif

EM

;----------------------------
LBL Z_in_pos_manu                      ;09

#if CFG_pos_Z_manual = "active"
  L      DG_position_axis_Z_ref
  -      DG_TC_pos_Z_manual
  PSW
  CM     Absolute
  PLW
  <      DG_MP1030_pos_window_Z
  AN     PN_M4122_strobe_PLC_posit_axis_Z
  AN     MG_TC_manu_interruption
  PSL

  L      MG_TC_manu_interruption
  =      MG_win_TC_pos_Z_manual
#else
  PS     MG_marker_one
#endif

EM

;----------------------------
LBL Tool_manu_unclamping               ;10

  L      MG_marker_one
  R      PN_error_quit_tool_change
  R      PN_error_guard_close_quit_tool_change
  R      PN_error_guard_open
  S      O_lamp_TC_enable
  S      MG_SK_enable_release_toolchange
  S      MG_SK_enable_tool_unclamp

  L      MG_key_tool_unclamping
  O      MG_SK_tool_unclamp
  O      I_key_tool_unclamping_foot_switch
  S      PN_error_guard_closed

  LN     MG_alternating_table_on
  IFT
    LN     I_guard_1_closed
    ON     I_guard_2_closed
    O      MG_guard_inactive
    R      PN_error_guard_closed
  ELSE
    L      MG_axis_limit_1
    IFT
      LN     I_guard_1_closed
      R      PN_error_guard_closed
    ENDI

    L      MG_axis_limit_2
    IFT
      LN     I_guard_2_closed
      R      PN_error_guard_closed
    ENDI
  ENDI

  L      PN_error_guard_closed
  R      MG_SK_tool_unclamp
  =N     PN_error_tool_unclamping

  L      MG_clock_1_s
  AN     MG_Sn_O_tool_unclamping
  =      O_lamp_tool_unclamped

  L      MG_key_tool_unclamping
  AN     ML_key_pressed
  O      MG_SK_tool_unclamp
  O      I_key_tool_unclamping_foot_switch
  AN     PN_error_guard_closed
  S      MG_SK_tool_unclamp
  S      MG_Sn_O_tool_unclamping

  L      MG_manual_mode
  O      MG_TOOL_CALL_quit_without_T_unclamping
  =      MG_SK_enable_release_toolchange
  SN     MG_SK_release_toolchange
  IFT
    L      MG_key_release_toolchange
    AN     ML_key_pressed
    ON     MG_SK_release_toolchange
    R      MG_SK_release_toolchange
    IFT
      L      BL_step_T_change
      +      K+3
      =      BL_step_T_change
    ENDI
  ENDI

  PS     MG_Sn_O_tool_unclamping

EM

;----------------------------
LBL Tool_manu_clamping                 ;11

  L      MG_marker_one
  R      PN_error_tool_unclamping
  S      PN_error_tool_clamping
  R      PN_error_quit_tool_change
  R      PN_error_guard_close_quit_tool_change
  R      PN_error_end_tool_change_NC_start
  R      PN_error_guard_open
  R      MG_SK_enable_release_toolchange
  S      MG_SK_release_toolchange
  S      O_lamp_TC_enable

  L      MG_clock_1_s
  A      MG_Sn_O_tool_unclamping
  =      O_lamp_tool_unclamped

  L      MG_key_tool_unclamping
  AN     ML_key_pressed
  ON     MG_SK_tool_unclamp
  O[
    LN     I_key_tool_unclamping_foot_switch
    A      ML_foot_switch_pressed
  ]
  R      MG_SK_tool_unclamp
  R      MG_Sn_O_tool_unclamping

  LN     MG_Sn_O_tool_unclamping
  PSL

EM

;----------------------------
LBL TC_disable                         ;12

  L      MG_marker_one
  R      PN_error_tool_clamping

  L      MG_guard_inactive
  S      PN_error_quit_tool_change
  SN     PN_error_guard_close_quit_tool_change

  L      MG_key_tool_unclamping
  AN     ML_key_pressed
  O      MG_SK_tool_unclamp
  O      I_key_tool_unclamping_foot_switch
  A[
    L      MG_guard_open
    O      MG_guard_inactive
  ]
  S      MG_SK_tool_unclamp
  S      MG_Sn_O_tool_unclamping
  IFT
    DEC    BL_step_T_change
  ENDI

  L      MG_clock_1_s
  =      O_lamp_TC_enable

  L      MG_guard_inactive
  IFT
    =      MG_SK_enable_tool_unclamp
    =      MG_SK_enable_release_toolchange
  ELSE
    L      MG_guard_open
    =      MG_SK_enable_tool_unclamp
    =N     MG_SK_enable_release_toolchange
  ENDI

  LN     MG_SK_enable_release_toolchange
  S      MG_SK_release_toolchange

  L      MG_key_release_toolchange
  AN     ML_key_pressed
  ON     MG_SK_release_toolchange
  AN     MG_guard_open
  R      MG_SK_release_toolchange
  R      MG_SK_enable_release_toolchange
  R      MG_SK_enable_tool_unclamp
  PSL

EM

;----------------------------
LBL TC_quit                            ;13

  L      MG_marker_one
  R      PN_error_quit_tool_change
  R      PN_error_guard_close_quit_tool_change
  R      PN_error_guard_closed
  R      PN_error_tool_unclamping

  L      MG_key_NC_start
  ON     NP_M4176_control_in_operation_on_or_flashes
  =N     PN_error_end_tool_change_NC_start
  PSL

EM

;----------------------------
LBL Restore_necessary                  ;14

  L      MG_control_in_operation_on
  O      NP_M4157_mode_restore_position
  ON     NP_M4176_control_in_operation_on_or_flashes
  PSL

EM

;----------------------------
LBL Restore_pos_ok                     ;15

  L      MG_control_in_operation_on
  AN     NP_M4157_mode_restore_position
  O      MG_mode_block_scan_has_been_active
  ON     NP_M4176_control_in_operation_on_or_flashes
  PSL

EM

;----------------------------
LBL Tool_Call_quitt                    ;16

  PS     MG_marker_zero

  L      NP_M4073_strobe_TOOL_CALL
  S      PN_M4093_quit_TOOL_CALL
  IFT
    L      NP_W264_tool_number
    =      WG_tool_in_spindle
  ENDI

  LN     NP_M4073_strobe_TOOL_CALL
  S      MG_standby_tool_status_reading
  CMT    TC_manual_reset

EM

;----------------------------
LBL TC_manual_reset

  L      MG_marker_one
  R      PN_error_tool_clamping
  R      PN_error_tool_unclamping
  R      PN_error_quit_tool_change
  R      PN_error_guard_close_quit_tool_change
  R      PN_error_end_tool_change_NC_start
  R      MG_TC_manual
  R      MG_TC_manual_active
  R      MG_TC_manu_interruption
  R      MG_SK_release_toolchange
  R      MG_SK_tool_unclamp
  R      MG_SK_enable_tool_unclamp
  R      MG_Sn_O_tool_unclamping
  R      O_lamp_release_toolchange     ;20120926

  L      K+0
  =      BL_step_T_change

EM

;----------------------------
LBL Sn_Input_read

  L      MG_spindle_1_active
  IFT
    L      I_S1_tool_clamped
    =      MG_Sn_I_tool_clamped

    L      I_S1_tool_unclamped
    =      MG_Sn_I_tool_unclamped

    L      I_S1_tool_located_in_spindle
    =      MG_Sn_I_tool_located_in_spindle
  ENDI

  L      MG_spindle_2_active
  IFT
    L      I_S2_tool_clamped
    =      MG_Sn_I_tool_clamped

    L      I_S2_tool_unclamped
    =      MG_Sn_I_tool_unclamped

    L      I_S2_tool_located_in_spindle
    =      MG_Sn_I_tool_located_in_spindle
  ENDI

EM

;----------------------------
LBL Sn_Output_write

  L      MG_spindle_1_active
  IFT
    L      MG_Sn_O_tool_clamping
    =      O_S1_tool_clamping

    L      MG_Sn_O_tool_unclamping
    =      O_S1_tool_unclamping
  ENDI

  L      MG_spindle_2_active
  IFT
    L      MG_Sn_O_tool_clamping
    =      O_S2_tool_clamping

    L      MG_Sn_O_tool_unclamping
    =      O_S2_tool_unclamping
  ENDI

EM

