*-----------------------------------------------------------------------------
*  Diagnosis - fuses, temperature, current
*-----------------------------------------------------------------------------
*  Module Description
*
*-----------------------------------------------------------------------------

#define /s    KL_inactive                              K +0
#define /s    KL_no_function                           K +0
#define /s    KL_shutdown_PLC_still_operable           K +0
#define /s    KL_shutdown_mode_defined_in_MP4040       K +2
#define /s    KL_field_angle_reading                   K +0
#define /s    KL_field_angle_writing                   K +1
#define /s    KL_module_exact                          K +0
#define /s    KL_file_access_reading                   K %000
#define /s    KL_file_access_writing_locked            K %011
#define /s    KL_emergency_stop_test_168_hours         K +10080
#define /s    KL_mode_0_start_emergency_stop_test      K +0
#define /s    KL_value_from_MP2230                     K +0
#define /s    KL_value_from_MP2232                     K +0
#define /s    KL_display_without_sign_left             K +0
#define /s    KL_0_digits_after_point                  K +0
#define /s    KL_3_digits_before_point                 K +3
#define /s    KL_not_used                              K +0
#define /s    KL_I2t_pre_warning                       K +2
#define /s    KL_I2t_limitation                        K +3
#define /s    KL_runtime_from_MP                       K -1

#define /s    ML_auto_power_off                        M
#define /s    ML_key_pressed                           M
#define /s    ML_init_control_operational              M
#define /s    ML_emergency_stop_test                   M
#define /s    ML_step_start                            M
#define /s    ML_key_power_on_pressed                  M
#define /s    ML_pneumatic_pressure_low                M

#define /s    ML_working_light                         M

#define /s    BL_identifier                            B
#define /s    BL_identifier_1                          B
#define /s    BL_status_brake_test                     B
#define /s    BL_status_emergency_stop_test            B
#define /s    BL_axis_number                           B
#define /s    BL_error_code                            B
#define /s    BL_step_emergency_stop_test              B

#define /s    BL_status_brake                          B[18]

#define /s    WL_stack_synchronize                     W
#define /s    WL_error_code                            W
#define /s    WL_error_code_2                          W

#define /s    DL_axis_field_angle_initialize           D
#define /s    DL_axes_release_before_emergency_stop_test D
#define /s    DL_file_handle                           D

#include Mac_Lib.inc

GLOBAL Diagnostic

;----------------------------
LBL Diagnostic

  CM     9162
  PLW
  O[
    L      DG_current_speed_loop_on
    A      DG_MP12_simulated_axes
  ]
  =      DG_servo_drives_ready
  D=     MG_servo_drives_ready
  M_display_module_error

  LN     MG_machine_parameter_are_read
  =      PN_error_reading_machine_parameters

#if CFG_emergency_end_position_axes_coded = "active"
  L      I_X_not_overtravelled
  A      I_X_Y_not_overtravelled
  A      I_X_Y_Z_not_overtravelled
#else
  LN     I_end_posit_axis_X
  AN     I_end_posit_axis_Y
  AN     I_end_posit_axis_Z
#endif
  A      I_X_Y_Z_4_not_overtravelled
  A      I_X_Y_Z_4_5_not_overtravelled
  O      O_axes_motion_from_end_position
  AN     I_emergency_button_unlocked
  A      I_SM_1xx_temperature_ok
  =      MG_win_emergency_stop_button

  LN     I_circuit_breaker_ok
  S      PN_error_circuit_breaker

  LN     I_PW_210_temperature_ok
  S      PN_error_PW_210_temperature_max

  LN     I_DA300_pressure_ok
  S      PN_error_DA300_pressure

  LN     MG_pneumatic_source_inactive
  IFT
    LN     I_pneumatic_pressure_ok
    AN     ML_pneumatic_pressure_low
    IFT
      PS     K&TR_pneumatic_pressure_low
      PS     KL_runtime_from_MP
      CM     9006

      L      MG_marker_one
      S      ML_pneumatic_pressure_low
    ENDI

    L      ML_pneumatic_pressure_low
    AN     TR_pneumatic_pressure_low
    A      MG_power_delayed_on
    S      PN_error_pneumatic_pressure

    L      I_pneumatic_pressure_ok
    R      ML_pneumatic_pressure_low
  ENDI

  L      MG_control_operational
  AN     MG_power_delayed_on
  =      MG_test_lamps

  L      I_ESA_control_operational
  =      O_lamp_power_on

  L      NP_M4177_error_message_active
  ON     MG_control_operational
  A      MG_clock_1_s
  O      MG_test_lamps
  =      O_lamp_TNC_alarm

  L      NP_M4170_END_PGM_M02_M30_executed
  O      MG_NC_stop_active
#if CFG_pallet_changer = "Alternat_table"
  O[
    L      MG_alternating_table_on
    A      MG_axis_limit_2
    AN     MG_new_workpiece_table_1
  ]
  O[
    L      MG_alternating_table_on
    A      MG_axis_limit_1
    AN     MG_new_workpiece_table_2
  ]
#endif
  A      MG_automatic_mode
  A      MG_clock_1_s
  O      MG_test_lamps
  =      O_lamp_program_end

  L      MG_control_in_operation_on
  A      MG_automatic_mode
  =      O_program_run

;Machine control voltage on

  L      NP_M4192_emergency_stop_test_power_on
  A      NP_M4190_emergency_stop_test_active
  A      ML_init_control_operational
  O[
    L      MG_key_power_on
    AN     ML_key_power_on_pressed
  ]
  O      MG_power_on_AUTOSTART_on
  O      MG_energy_opt_power_on
  S      O_power_on

  L      MG_key_power_on
  =      ML_key_power_on_pressed

  L      I_ESA_control_operational
  S      ML_init_control_operational

  L      I_ESA_control_operational
  ON     I_emergency_button_unlocked
  ON     NP_M4191_control_operational
  R      O_power_on

  L      NP_M4190_emergency_stop_test_active
  IFT
    L      KL_emergency_stop_test_168_hours
    =      WG_time_next_emergency_stop_test
  ENDI

;Emergency stop test - brake test

  L      MG_pulse_1_min
  IFT
    DEC    WG_time_next_emergency_stop_test
  ENDI

  L      WG_time_next_emergency_stop_test
  <=     K+360
  A      MG_pulse_1_h
  S      PN_error_emergency_stop_test

  L      WG_time_next_emergency_stop_test
  <=     K+0
  =      MG_start_blocked_emergency_stop_test

  L      MG_pulse_1_min
  O      MG_key_NC_start
  A      MG_start_blocked_emergency_stop_test
  S      PN_error_emergency_stop_test

  CASE   BL_step_emergency_stop_test
    CM     Emergency_stop_Aktivierung  ;00
    CM     Brake_test_start            ;01
    CM     Brake_test_ok               ;02
    CM     Drives_off                  ;03
    CM     Drives_off_ok               ;04
    CM     Emergency_stop_start        ;05
    CM     Emergency_stop_ok           ;06
    CM     Drives_on                   ;07
    CM     Drives_on_ok                ;08
    CM     Emergency_end               ;09
  ENDC

  PLL
  =      ML_step_start
  IFT
    INC    BL_step_emergency_stop_test
  ENDI

  LN     I_emergency_button_unlocked
  CMT    Emergency_reset

  L      PN_error_emergency_stop_test
  R      PN_error_emergency_stop_test
  IFT
    PS     NP_M4203_error_module_9xxx
    PS     K &PN_error_emergency_stop_test - 4800
    L      WG_time_next_emergency_stop_test
    +      K +30
    /      K +60
    PSW
    PS     KL_display_without_sign_left
    PS     KL_0_digits_after_point
    PS     KL_3_digits_before_point
    PS     K &SG_time_emergency_stop_test
    CM     9051
    M_display_module_error

    PS     K &SG_time_emergency_stop_test
    PS     KL_not_used
    PS     KL_not_used
    CM     9084
    PL     NP_M4203_error_module_9xxx
  ENDI

;Main switch off

  L      MG_auto_power_off_after_END_PGM
  O      STG_energy_opt_control.M_active
  IFT
    L      STG_energy_opt_control.M_active
    AN     MG_control_in_operation_on
    O      MG_pulse_END_PGM_M02_M30
    S      ML_auto_power_off

    L      ML_auto_power_off
    A      MG_spindle_stand_still
    A      MG_axes_in_position
    AN     MG_TC_automatic
    AN     MG_TC_magazine_n_start_active
    R      ML_auto_power_off
    R      MG_auto_power_off_after_END_PGM
    S      PN_error_auto_power_off_after_END_PGM

    L      PN_error_auto_power_off_after_END_PGM
    IFT
      RPLY   BL_identifier_1
      ==     K+0
      IFT
        SUBM   Auto_power_off_submit
        =      BL_identifier_1
        ==     K+0
        S      PN_error_submit_spawn_queue_full
      ENDI
    ENDI
  ENDI

  L      MG_key_working_light
  AN     ML_key_pressed
  O[
    L      MG_SK_working_light
    A      STG_energy_opt_light.M_active
  ]
  XO     MG_SK_working_light
  O      MG_energy_opt_light_on
  =      MG_SK_working_light

  L      MG_SK_working_light
  =      O_working_light

  L      MG_key_working_light
  =      ML_key_pressed

  PS     KG_spindle
  CM     9164
  PL     DG_actual_drive_rpm_S
  M_display_module_error

  PS     KG_spindle
  CM     9166
  PL     WG_drive_utilization_S
  M_display_module_error

  LN     MG_pulse_500_ms
  EMT

  CM     Drive_diagnosis

;$WS R&D ?
;  RPLY   BL_Identifier
;  <>     K+0
;  EMT
;
;  SUBM   Antrieb_Diagnose
;  =      BL_Identifier
;  ==     K+0
;  S      PN_Fehler_Submit_Spawn_Queue_voll

EM

;----------------------------
LBL Drive_diagnosis

  L      NP_D1100_axes_release
  D=     MG_axes_release_bit

  L      NP_D1108_axes_in_motion
  D=     MG_axes_in_motion_bit

  L      NP_D1116_reference_nesessary
  AN     DG_MP12_simulated_axes
  D=     MG_reference_necessary_bit

  L      DG_drives_digital
  ==     KL_inactive
  EMT

  PS     KL_I2t_pre_warning
  CM     9066
  PLW
  D=     MG_I2t_X_pre_warning
  >      KL_inactive
  =      PN_error_I2t_pre_warning

  M_error_I2t_pre_warning
  M_display_module_error

  PS     KL_I2t_limitation
  CM     9066
  PLW
  D=     MG_I2t_X_limitation
  >      KL_inactive
  S      PN_error_I2t_limitation

  M_error_I2t_limitation
  M_display_module_error

*$WS
;  PS     KL_JH_Umrichter
;  CM     9066
;  PLW
;  B=     MG_JH_Umrichter_Diagnose
; ...
;  ON     I_Temperatur_Leistungsteil_ok
;  S      PN_Fehler_Leistungteil_Tmax
;
;*  M_Fehler_Leistungteil_Tmax
;  M_Modul_Fehler_anzeigen
;
  LN     I_temp_drives_power_stage_ok
  S      PN_error_inverter_temp_max

  L      K+0
  =X
  REPEAT
    L      DG_drives_digital
    BTX
    IFT
      PSX
      PSX
      PSX
      CM     9164
      PL     DG_actual_drive_rpm_X[X]
      M_display_module_error

      CM     9165
      PL     WG_drive_temperature_X[X]
      M_display_module_error

      CM     9166
      PL     WG_drive_utilization_X[X]
      M_display_module_error

      L      WG_drive_temp_max_X[X]
      -      WG_drive_temperature_X[X]
      <      K+10
      A[
        L      WG_drive_temp_max_X[X]
        <>     K+255
      ]
      A[
        L      WG_drive_temp_max_X[X]
        <>     K+0
      ]
      A      MG_power_delayed_on
      S      PN_error_drive_temp_max
      M_error_motor_temp_max

#if KG_field_angle_saved_in_PLC > 0
      L      KG_field_angle_saved_in_PLC
      BTX
      IFT
        L      DG_axis_field_angle_saving
        A      NP_D1104_axes_in_position
        BTX
        IFT
          PSX
          PS     KL_no_function
          PS     KL_field_angle_reading
          CM     9149
          PLW
          =      WL_error_code
          ==     KL_module_exact
          AN     NP_M4203_error_module_9xxx
          IFT
            L      WG_axis_field_angle_significant
            BSX
            =      WG_axis_field_angle_significant
          ELSE
            L      WG_axis_field_angle_significant
            BCX
            =      WG_axis_field_angle_significant
          ENDI
          PL     DG_field_angle_X[X]

          L      WG_axis_field_angle_saving
          BCX
          =      WG_axis_field_angle_saving

          PS     KL_file_access_writing_locked
          PS     S"PLC:\MP\Field_Angle.TAB"
          CM     9240                  ;File open
          PL     DL_file_handle
          M_display_module_error

          PS     DL_file_handle
          PSX
          PS     S"Value"
          PS     DG_field_angle_X[X]
          CM     9256                  ;Table write
          M_display_module_error
        ENDI

        LN     WL_axis_field_angle_initialize
        A      WG_axis_field_angle_significant
        BTX
        IFT
          PS     KL_file_access_reading
          PS     S"PLC:\MP\Field_Angle.TAB"
          CM     9240                  ;File open
          PL     DL_file_handle
          M_display_module_error

          PS     DL_file_handle
          PSX
          PS     S"Value"
          CM     9255                  ;Table read
          PL     DG_field_angle_X[X]
          M_display_module_error

          PSX
          PS     DG_field_angle_X[X]
          PS     KL_field_angle_writing
          CM     9149
          PLW
          =      WL_error_code_2
          <>     KL_module_exact
          O      NP_M4203_error_module_9xxx
          IFT
            L      WG_axis_field_angle_significant
            BCX
            =      WG_axis_field_angle_significant
          ENDI
          PL     WL_stack_synchronize

          LN     WL_axis_field_angle_initialize
          BTX
          IFT
            L      WL_axis_field_angle_initialize
            BSX
            =      WL_axis_field_angle_initialize
          ENDI
          M_display_module_error
        ENDI
      ENDI
#endif

    ENDI

    INCX

    LX
    >=     KG_max_number_axes
  UNTILT

  LN     MG_spindle_digital
  EMT

  PS     KG_spindle
  CM     9165
  PL     WG_drive_temperature_S
  M_display_module_error

  L      WG_drive_temp_max_S
  -      WG_drive_temperature_S
  <      K+10
  A[
    L      WG_drive_temp_max_S
    <>     K+255
  ]
  A[
    L      WG_drive_temp_max_S
    <>     K+0
  ]
  A      MG_spindle_active
  A      MG_power_delayed_on
  S      PN_error_drive_temp_max
  IFT
    L      KG_spindle
    =X
    M_error_motor_temp_max
  ENDI

EM

;----------------------------
LBL Auto_power_off_submit

  PS     KL_shutdown_mode_defined_in_MP4040
  CM     9279
  M_display_module_error

  L      MG_marker_one
  S      O_auto_power_off_after_END_PGM

EM

;----------------------------
LBL Emergency_stop_Aktivierung

  L      MG_start_blocked_emergency_stop_test
  A      I_guard_1_closed_locked
  A      I_guard_2_closed_locked
  A      MG_axes_in_position
  A      PN_M4007_S_M05_nominal_0_and_status
  S      MG_emergency_stop_test_active
  PSL

EM

;----------------------------
LBL Brake_test_start

  RPLY   BL_status_brake_test
  ==     K+0
  IFT
    SUBM   Brake_test
    =      BL_status_brake_test
    <>     K+0
    S      MG_brake_test
    SN     PN_error_submit_spawn_queue_full
    PSL
  ELSE
    PS     MG_marker_zero
  ENDI

EM

;----------------------------
LBL Brake_test_ok

  LN     MG_brake_test
  PSL

EM

;----------------------------
LBL Drives_off

  PS     MG_marker_one

  L      NP_D1100_axes_release
  =      DL_axes_release_before_emergency_stop_test
  =      DG_clamping_mode_on_external

EM

;----------------------------
LBL Drives_off_ok

  L      DG_servo_drives_ready
  ==     K+0
  PSL

EM

;----------------------------
LBL Emergency_stop_start

  RPLY   BL_status_emergency_stop_test
  ==     K+0
  IFT
    SUBM   Emergency_stop_test
    =      BL_status_emergency_stop_test
    <>     K+0
    S      ML_emergency_stop_test
    SN     PN_error_submit_spawn_queue_full
    PSL
  ELSE
    PS     MG_marker_zero
  ENDI

EM

;----------------------------
LBL Emergency_stop_ok

  LN     ML_emergency_stop_test
  A      NP_M4190_emergency_stop_test_active
  PSL
  EMF

  PS     K &PN_error_emergency_stop_test - 4800
  CM     9086

  M_display_module_error


EM

;----------------------------
LBL Drives_on

  LN     NP_M4190_emergency_stop_test_active
  A      I_ESA_control_operational
  PSL
  EMF

  L      DL_axes_release_before_emergency_stop_test
  =      DG_clamping_mode_off_external

EM

;----------------------------
LBL Drives_on_ok

  L      DG_clamping_mode_off_external
  ==     K+0
  PSL

EM

;----------------------------
LBL Emergency_end

  PS     MG_marker_zero

LBL Emergency_reset

  L      MG_marker_one
  R      MG_emergency_stop_test_active

  L      K+0
  =      BL_step_emergency_stop_test

EM

;----------------------------
LBL Brake_test

  L      K +0
  =      BL_axis_number

  REPEAT
    L      DG_controlled_axes
    A      NP_D1100_axes_release
    A      DG_MP2230_brake_test_active
    AN     KG_axes_with_c_axis_mode
    BT     BL_axis_number
    IFT
      PS     BL_axis_number
      PS     KL_value_from_MP2230
      PS     KL_value_from_MP2232
      CM     9143
      PL     BL_status_brake[BL_axis_number]

      M_display_module_error
    ENDI

    INC    BL_axis_number

    L      BL_axis_number
    >=     KG_max_number_axes
  UNTILT

  L      MG_brake_test
  R      MG_brake_test

EM

;----------------------------
LBL Emergency_stop_test

  PS     KL_mode_0_start_emergency_stop_test
  PS     KL_no_function
  PS     KL_no_function
  CM     9144
  PL     BL_error_code

  M_display_module_error

  L      ML_emergency_stop_test
  R      ML_emergency_stop_test

EM


