*-----------------------------------------------------------------------------
*  TC Tool table - PLC status reading
*-----------------------------------------------------------------------------
*  Module Description
*    PLC status (tool table):
*    #Bit 0 (%00000001) - touch probe
*    #Bit 1 (%00000010) - oversized tool
*                       - (no pocket out after TOOL DEF)
*    #Bit 2 (%000001xx) - M07 coolant inside active
*-----------------------------------------------------------------------------

#define /s    KL_tool_table                            K +0
#define /s    KL_pocket_table                          K +2
#define /s    KL_PLC_status                            K +17
#define /s    KL_PLC_value                             K +26
#define /s    KL_element_TC_pocket_locked              K +3
#define /s    KL_TC_pocket_locked                      K +1
#define /s    KL_spindle_pocket_P0                     K +0
#define /s    KL_tool_number                           K +0
#define /s    KL_index_0                               K +0
#define /s    KL_tool_pocket_empty                     K +0
#define /s    KL_no_magazine_number                    K +0
#define /s    KL_bit0                                  K +0
#define /s    KL_bit2                                  K +2
#define /s    KL_TC_magazine                           K +1

#define /s    BL_identifier                            B

#define /s    WL_line_number                           W
#define /s    WL_line_number_tool                      W
#define /s    WL_tool_in_spindle_before                W

#include Mac_Lib.inc

GLOBAL Tool_status

;----------------------------
LBL Tool_status
;----------------------------

  LN     MG_tool_status_reading
  AN     MG_standby_tool_status_reading
  EMT

  LN     MG_tool_table_active
  R      MG_tool_status_reading
  R      MG_standby_tool_status_reading
  EMT

  RPLY   BL_identifier
  <>     K+0
  EMT

  SUBM   Tool_status_subm
  =      BL_identifier
  ==     K+0
  S      PN_error_submit_spawn_queue_full

EM

;----------------------------
LBL Tool_status_subm

  L      MG_tool_status_reading
  CMT    TC_status_read

  L      MG_standby_tool_status_reading
  CMT    TC_standby_status

EM

;----------------------------
LBL TC_status_read

  L      MG_TC_auto_active
  IFT
    L      WG_tool_in_spindle
    =      WL_tool_in_spindle_before

    PS     KL_pocket_table
    PS     KL_spindle_pocket_P0
    PS     KL_tool_number
    CM     9093
    PL     WG_tool_in_spindle
    PLX
    M_display_module_error
  ENDI

  L      MG_tool_spindle_activation
  IFT
    R      MG_tool_spindle_activation

    L      WG_tool_in_spindle
    <>     WL_tool_in_spindle_before
    IFT
      S      PN_M4538_tool_selection_from_PLC_W264

      L      WG_tool_in_spindle
      =      NP_W264_tool_number
    ENDI
  ENDI

  PS     KL_tool_table

  L      NP_M4073_strobe_TOOL_CALL
  IFT
    PS     NP_W264_tool_number
  ELSE
    PS     WG_tool_in_spindle
  ENDI

  PS     KL_index_0
  CM     9091
  PLW
  =      WL_line_number
  PSW
  M_display_module_error

  PS     KL_PLC_status
  CM     9093
  PL     BG_tool_status_new
  PLX
  M_display_module_error

  L      BG_tool_status_new
  BT     KL_bit0
  =      MG_touch_probe_active

  L      BG_tool_status_new
  BT     KL_bit2
  ON     MG_S1_release_M07_by_T_status_bit2[BG_number_active_spindle]
  =      MG_release_M07_coolant_inside_on

#if CFG_tool_specific_torque_monitoring = "active"
  PS     KL_tool_table
  PS     WL_line_number
  PS     KL_PLC_value
  CM     9093
  PLW
  /      K+10000
  =      WG_S_max_torque_tool_table
  PLX
  M_display_module_error

  L      WG_S_max_torque_tool_table
  >      K+0
  =      MG_SK_T_torque_monitoring
  IFT
    L      WG_S_max_torque_tool_table
    <      DG_S1_min_torque[BG_number_active_spindle]
    IFT
      L      DG_S1_min_torque[BG_number_active_spindle]
      =      WG_S_max_torque_tool_table
    ENDI
  ELSE
    L      DG_S1_max_torque[BG_number_active_spindle]
    =      WG_S_max_torque_tool_table
  ENDI
#else
  L      DG_S1_max_torque[BG_number_active_spindle]
  =      WG_S_max_torque_tool_table
#endif

  L      MG_marker_one
  S      MG_clear_screen
  R      MG_tool_status_reading

  LN     MG_commissioning
  EMT

  CM     9019
  PL     BG_bytes_on_stack
  M_display_module_error

  L      BG_bytes_on_stack
  >      K+0
  S      PN_error_stack_not_empty

EM

;----------------------------
LBL TC_standby_status

  L      STG_TC_mag[BG_TC_mag_index_Tool_Call].WG_TC_magazine_actual_pocket
  ==     K+0
  R      MG_standby_tool_status_reading
  EMT

  L      NP_W268_tool_magazine_number
  <=     KL_no_magazine_number
  IFT
    PS     KL_TC_magazine
  ELSE
    PS     NP_W268_tool_magazine_number
  ENDI

  L      MG_SK_TC_mag_load_active
  IFT
    L      STG_TC_mag[BG_TC_mag_index_Tool_Call].WG_TC_magazine_actual_pocket
    +      DG_TC_mag_1_offset_load_position[BG_TC_mag_index_Tool_Call]
    -      K+1
    MOD    STG_TC_mag[BG_TC_mag_index_Tool_Call].WG_MP7261_TC_magazine_max_pocket
    +      K+1
    PSW
  ELSE
    PS     STG_TC_mag[BG_TC_mag_index_Tool_Call].WG_TC_magazine_actual_pocket
  ENDI

  CM     9301
  PL     WL_line_number_tool
  M_display_module_error

  PS     KL_pocket_table
  PS     WL_line_number_tool
  PS     KL_tool_number
  CM     9093
  PL     WG_T_number_standby_tool
  PLX
  M_display_module_error

  PS     KL_pocket_table
  PS     WL_line_number_tool
  PS     KL_element_TC_pocket_locked
  CM     9093
  PLW
  ==     KL_TC_pocket_locked
  =      STG_TC_mag[BG_TC_mag_index_Tool_Call].MG_TC_mag_pocket_locked

  PLX
  M_display_module_error

#if TC_pocket_90_degree_shift = &active
  L      WG_T_number_standby_tool
  >      KL_tool_pocket_empty
  IFT
    PS     KL_tool_table
    PS     WG_T_number_standby_tool
    PS     KL_index_0
    CM     9091
    PLW
    =      WL_line_number
    PSW
  M_display_module_error

    PS     KL_PLC_status
    CM     9093
    PL     BG_standby_tool_status
    PLX
  M_display_module_error
  ENDI
#endif

  L      MG_standby_tool_status_reading
  R      MG_standby_tool_status_reading

  LN     MG_commissioning
  EMT

  CM     9019
  PL     BG_bytes_on_stack
  M_display_module_error

  L      BG_bytes_on_stack
  >      K+0
  S      PN_error_stack_not_empty

EM
