*-----------------------------------------------------------------------------
*  Common used functions
*-----------------------------------------------------------------------------
*  Module Description
*
*    To expand the NC/PLC interface further functions will be offered.
*
*
*-----------------------------------------------------------------------------

#define /s    KL_2_seconds                             K +2
#define /s    KL_6_seconds                             K +6
#define /s    KL_60_seconds                            K +60
#define /s    KL_3600_seconds                          K +3600

#define /s    KL_editor_as_main_operating_mode         K +0
#define /s    KL_displayed_screen_window               K +4
#define /s    KL_machine_mode_in_background            K +3
#define /s    KL_editor_overlaid_machine_mode          K +28
#define /s    KL_tool_table                            K +3
#define /s    KL_pocket_table                          K +4
#define /s    KL_reference_value                       K +7
#define /s    KL_actual_value                          K +1
#define /s    KL_monitoring_drives_on_PLC_input        K +0
#define /s    KL_monitoring_drives_off_PLC_input       K +1
#define /s    KL_input_I0                              K +0
#define /s    KL_DCM_status                            K +0
#define /s    KL_MP_programming                        K +95148
#define /s    KL_PLC_programming                       K +807667
#define /s    KL_active                                K +1
#define /s    KL_runtime_from_MP                       K -1
#define /s    KL_NC_macro_active                       K +1
#define /s    KL_NC_macro_canceled                     K -2
#define /s    KL_active_T_axis                         K +100
#define /s    KL_0_test_key_queue                      K +0
#define /s    KL_inactive                              K -1
#define /s    KL_temp_comp_kinematic                   K +8
#define /s    KL_TC_mag_auto                           K +1
#define /s    KL_axis_group_8                          K +7
#define /s    KL_bit_0                                 K +0
#define /s    KL_format_time_h_m                       K +15

#define /s    ML_second_odd                            M
#define /s    ML_second_even                           M
#define /s    ML_second_0                              M
#define /s    ML_END_PGM_M02_M30                       M
#define /s    ML_control_operates_flashes              M
#define /s    ML_edit_test_run_mode                    M
#define /s    ML_mode_restore_position                 M
#define /s    ML_tool_table_has_been_selected          M
#define /s    ML_soft_key_edit_on_off                  M
#define /s    ML_key_pressed                           M
#define /s    ML_clock_250_ms                          M
#define /s    ML_clock_500_ms                          M
#define /s    ML_control_operational                   M
#define /s    ML_edit_tool_table_was_active            M
#define /s    ML_edit_pocket_table_was_active          M
#define /s    ML_additional_axis_mounted               M
#define /s    ML_reset_init_macro_started              M
#define /s    ML_TC_help_has_been_active               M
#define /s    ML_internal_stop                         M
#define /s    ML_manual_mode_interruption              M
#define /s    ML_NC_read_interlock_single_block        M

#define /s    BL_clock_250_ms                          B
#define /s    BL_reset_init_macro                      B
#define /s    BL_number_key_in_queue                   B
#define /s    BL_axes_group_index                      B

#define /s    WL_TC_mag_number_old                     W
#define /s    WL_number_active_NC_macro                W

#include Mac_Lib.inc

GLOBAL Common_function
GLOBAL Absolute
GLOBAL Begin_PLC_cycle
GLOBAL End_PLC_Cycle

;----------------------------
LBL Begin_PLC_cycle
;----------------------------

  L      K+0
  =      BG_module_error_queue

EM

;----------------------------
LBL Common_function
;----------------------------

;Seconds clock (0.5S, 1S)

  LN     TR_pulse_125_ms
  =      TS_pulse_125_ms
  =      MG_pulse_125_ms

  L      MG_clock_250_ms
  XO     MG_pulse_125_ms
  =      MG_clock_250_ms

  L      MG_clock_250_ms
  AN     ML_clock_250_ms
  =      MG_pulse_250_ms

  L      MG_clock_250_ms
  =      ML_clock_250_ms

  L      MG_clock_250_ms
  A      MG_pulse_125_ms
  XO     MG_clock_500_ms
  =      MG_clock_500_ms

  L      MG_clock_500_ms
  AN     ML_clock_500_ms
  =      MG_pulse_500_ms

  L      MG_clock_500_ms
  =      ML_clock_500_ms

  L      MG_clock_500_ms
  A      MG_pulse_250_ms
  XO     MG_clock_1_s
  =      MG_clock_1_s

  CM     9195
  PL     DG_real_time
  M_display_module_error

  L      DG_real_time
  MOD    KL_2_seconds
  PSW
  ==     K+1
  =      ML_second_odd

  LN     ML_second_odd
  XO     ML_second_even
  =      MG_pulse_1_s

  PLW
  ==     K+0
  =      ML_second_even

  L      DG_real_time
  MOD    KL_6_seconds
  >=     K+3
  =      MG_clock_3_s

  L      DG_real_time
  MOD    KL_3600_seconds
  ==     K+1
  A      ML_second_0
  =      MG_pulse_1_h

  L      DG_real_time
  MOD    KL_60_seconds
  PSW
  ==     K+1
  A      ML_second_0
  =      MG_pulse_1_min

  PLW
  ==     K+0
  =      ML_second_0

  L      MG_pulse_1_min
  IFT
    PS     DG_real_time
    PS     K &SG_Time_H_M
    PS     KL_format_time_h_m
    CM     9055

    L      DG_real_time
    ;  /      K+60
    ;  =      DL_Unix_Systemzeit_Minuten
    ;  /      K+60
    ;  =      DL_Unix_Systemzeit_Stunden
    ;  /      K+24
    ;  =      DL_Unix_Systemzeit_Tage
    /      K+86400
    +      K+3                         * 4:Monday=1 3:Monday=0
    MOD    K+7
    =      DG_day_of_the_week
  ENDI

;External DC voltage off/on -t->

  L      I_ESA_control_operational
  AN     ML_control_operational
  =      MG_p_pulse_control_operational

  LN     I_ESA_control_operational
  A      ML_control_operational
  =      MG_n_pulse_control_operational

  L      I_ESA_control_operational
  =      ML_control_operational

  L      I_ESA_control_operational
  AN     NP_M4178_external_emergency_stop_active
  =      MG_control_operational

  LN     MG_control_operational
  =      TS_extern_DC_voltage_off_on

  L      MG_control_operational
  AN     TR_extern_DC_voltage_off_on
  =      MG_power_delayed_on

  L      I_ESA_control_operational
  =      TS_extern_DC_voltage_on_off

;Operational mode and change

  L      NP_M4150_mode_manual
  O      NP_M4151_mode_electronic_handwheel
  O      NP_M4155_mode_pass_over_reference
  O      NP_M4156_mode_soft_key_manual_operation
  =      MG_manual_mode

  LN     MG_manual_mode
  AN     MG_automatic_mode
  =      MG_manu_auto_mode_change

  L      MG_manual_mode
  A      MG_automatic_mode
  AN     NP_M4156_mode_soft_key_manual_operation
  =      MG_auto_manu_mode_change

  L      NP_M4152_mode_posit_with_manual_input
  O      NP_M4153_mode_program_run_single_block
  O      NP_M4154_mode_program_run_full_sequence
  AN     NP_M4156_mode_soft_key_manual_operation
  =      MG_automatic_mode

  PS     KL_editor_as_main_operating_mode
  CM     9035
  PLW
  BT     K+0
  =      ML_edit_test_run_mode
  M_display_module_error

  PS     KL_displayed_screen_window
  CM     9035
  PLW
  BT     K+0
  =      MG_edit_mode
  M_display_module_error

  L      ML_edit_test_run_mode
  A      MG_edit_mode
  =      MG_test_run_mode

  PS     KL_machine_mode_in_background
  CM     9035
  PLW
  PSW
  ==     KL_tool_table
  =      MG_tool_table_selected

  PLW
  PSW
  ==     KL_pocket_table
  =      MG_pocket_table_selected

  PLW
  >=     KL_tool_table
  =      MG_table_edit_active
  M_display_module_error

  PS     KL_editor_overlaid_machine_mode
  CM     9035
  PLW
  PSW
  ==     KL_pocket_table
  =      MG_edit_pocket_table

  PLW
  ==     KL_tool_table
  =      MG_edit_tool_table
  M_display_module_error

  LN     MG_edit_tool_table
  A      ML_edit_tool_table_was_active
  S      MG_tool_status_reading

  L      MG_edit_tool_table
  =      ML_edit_tool_table_was_active

  LN     MG_edit_pocket_table
  A      ML_edit_pocket_table_was_active
  AN     NP_M4176_control_in_operation_on_or_flashes
  S      MG_tool_status_reading
  S      MG_tool_spindle_activation

  L      MG_edit_pocket_table
  =      ML_edit_pocket_table_was_active

  PS     KL_active_T_axis
  CM     9035
  PL     BG_active_T_axis
  M_display_module_error

  LN     NP_M4157_mode_restore_position
  AN     NP_M4158_mode_block_scan
  R      MG_mode_block_scan_has_been_active

  L      NP_M4158_mode_block_scan
  S      MG_mode_block_scan_has_been_active

  PS     KL_DCM_status
  CM     9064
  PLW
  ==     KL_active
  =      MG_DCM_active
  M_display_module_error

  L      MG_TC_help
  AN     ML_TC_help_has_been_active
  =      MG_pulse_TC_help

  L      MG_TC_help
  =      ML_TC_help_has_been_active

;Number of the active TC magazine

  L      NP_W268_tool_magazine_number
  <>     WL_TC_mag_number_old
  A[
    L      NP_W268_tool_magazine_number
    >=     KL_TC_mag_auto
  ]
  IFT
    L      NP_W268_tool_magazine_number
    DECW
    =      BG_TC_mag_index_Tool_Call

    L      NP_W268_tool_magazine_number
    =      WL_TC_mag_number_old
  ENDI

;M2, M30, END PGM

  L      NP_M4170_END_PGM_M02_M30_executed
  AN     ML_END_PGM_M02_M30
  =      MG_pulse_END_PGM_M02_M30

  L      NP_M4170_END_PGM_M02_M30_executed
  =      ML_END_PGM_M02_M30

;Code number

  L      NP_D276_code_number_entered_via_MOD
  ==     KL_MP_programming
  =      MG_MP_MP_programming

  L      NP_D276_code_number_entered_via_MOD
  ==     KL_PLC_programming
  =      MG_PLC_programming

;Status NC program end

*$WS debug window
  CM     9320
  PL     WG_NC_PGM_end_cause_of_PGM_end
  PL     WG_NC_PGM_end_error_class
  PL     WG_NC_PGM_end_help_number
  PL     WG_NC_PGM_end_block_number_NC_PGM
  PL     WG_NC_PGM_end_additional_info
  M_display_module_error

;External/Internal STOP

  L      NP_M4176_control_in_operation_on_or_flashes
  R      NP_M4185_internal_STOP

  L      NP_M4221_PET_table_NC_stop
  ON     PN_M4560_NC_stop_0_active
  A      NP_M4176_control_in_operation_on_or_flashes
  A      MG_manual_mode
  S      ML_manual_mode_interruption

  L      MG_pulse_internal_stop
  O      MG_automatic_mode
  R      ML_manual_mode_interruption

  L      NP_M4185_internal_STOP
  AN     ML_internal_stop
  O[
    L      ML_manual_mode_interruption
    AN     NP_M4176_control_in_operation_on_or_flashes
  ]
  =      MG_pulse_internal_stop

  L      NP_M4185_internal_STOP
  =      ML_internal_stop

  L      NP_M4175_control_in_operation_flashes
  AN     ML_control_operates_flashes
  AN     NP_M4158_mode_block_scan
  =      MG_pulse_external_stop

  L      NP_M4176_control_in_operation_on_or_flashes
  AN     NP_M4175_control_in_operation_flashes
  =      MG_control_in_operation_on
  =N     MG_release_PLC_strobe

  L      MG_control_in_operation_on
  A      ML_control_operates_flashes
  AN     NP_M4157_mode_restore_position
  AN     NP_M4193_RUNCANCEL_macro_active
  =      MG_NC_start_pulse

  L      NP_M4175_control_in_operation_flashes
  =      ML_control_operates_flashes

  L      ML_mode_restore_position
  AN     MG_key_NC_stop
  AN     NP_M4221_PET_table_NC_stop
  R      MG_pulse_external_stop

  L      NP_M4157_mode_restore_position
  =      ML_mode_restore_position

;NC STOP / NC START

  LN     PN_M4560_NC_stop_0_active
  O      MG_pulse_external_stop
  S      MG_NC_stop_active

  L      PN_M4564_NC_start
  O      MG_manual_mode
  O      MG_pulse_internal_stop
  O      MG_key_spindle_M3
  R      MG_NC_stop_active

  L      MG_key_NC_start
  O      MG_key_spindle_M3
  O      MG_automatic_mode
  R      MG_NC_stop_in_manual_mode

  L      MG_key_NC_stop
  A      MG_manual_mode
  S      MG_NC_stop_in_manual_mode

;CE-key pressed

  L      NP_W274_code_pushed_key
  ==     KG_key_CE
  O      NP_M4668_HR420_key_Ctrl
  AN     MG_CE_key
  =      MG_pulse_CE_key
  S      MG_submit_CE_key

  L      NP_W274_code_pushed_key
  ==     KG_key_CE
  O      NP_M4668_HR420_key_Ctrl
  =      MG_CE_key

;Key pulse identification  (e.g. operation > 0,5s)

  L      MG_marker_one
  S      TS_keys_pulse_identifier

;Axes in position

  L      NP_D1104_axes_in_position
  ==     K-1
  =      MG_axes_in_position

  L      NP_D1104_axes_in_position
  A      DG_controlled_NC_axes
  ==     DG_controlled_NC_axes
  =      MG_NC_axes_in_position

;Axes in motion

  L      NP_D1108_axes_in_motion
  >      K+0
  =      MG_axes_in_motion

;Reference traversed

  L      NP_D1116_reference_nesessary
  A      DG_controlled_axes
  AN     DG_MP12_simulated_axes
  AN     DG_slave_axes
  ==     K+0
  AN     MG_reference_active
  A      MG_machine_parameter_are_read
  =      MG_reference_axes_traversed

;Quit FN19

  LN     NP_M4075_strobe_transfer_FN19
  R      PN_M4095_quit_transfer_FN19

  L      NP_M4075_strobe_transfer_FN19
  S      MG_FN19_date_new

;Spindle position reading

  PS     K&DG_position_axis_S_actual
  CM     9044
  M_display_module_error

;Axes position reading

  PS     K&DG_position_axis_X_ref
  PS     KL_reference_value
  CM     9041
  M_display_module_error

  PS     K&DG_position_axis_X_actl
  PS     KL_actual_value
  CM     9041
  M_display_module_error

;Axis error compensation kinematic

  PS     K&DG_temp_comp_kinemat_X
  PS     KL_temp_comp_kinematic
  CM     9041
  M_display_module_error

;Monitoring function off and ACTL/NOM value transfer as long time of MP1150.1 active

  L      MG_axes_in_motion
  =      TS_axes_in_motion

#if CFG_HSCI = "inactive"

; - Case 1 axes release via I32, non HSCI (one axis group)

  L      WG_MP4130_Ixx_monitoring_functions_off
  >=     KL_input_I0
  AN     MG_servo_drive_release_axis_groups_active
  A      TR_axes_in_motion
  AN     MG_TNC_programming_station_with_PLC
  IFT
    L      KL_monitoring_drives_off_PLC_input
    =      PN_W522_fast_PLC_input
  ELSE
    L      KL_monitoring_drives_on_PLC_input
    =      PN_W522_fast_PLC_input
  ENDI

; - Case 2 axes release via X150/1, non HSCI (only 1st group)

  L      MG_servo_drive_release_axis_groups_active
  A      TR_axes_in_motion
  AN     MG_TNC_programming_station_with_PLC
  IFT
    L      PN_W524_servo_drive_release_axes_groups
    BS     KL_bit_0
    =      PN_W524_servo_drive_release_axes_groups
  ELSE
    L      PN_W524_servo_drive_release_axes_groups
    BC     KL_bit_0
    =      PN_W524_servo_drive_release_axes_groups
  ENDI

#else

; - Case 3 axes release via MP4132.x with HSCI (max. 8 axes groups)

  L      K +0
  =      BL_axes_group_index
  REPEAT
    L      DG_MP2040_0_axis_group_1[BL_axes_group_index]
    A      K$7FFFFFFF
    A      NP_D1108_axes_in_motion
    <>     K +0
    IFT
      L      BL_axes_group_index
      +      K &TR_axes_group_01_in_motion
      PSW
      PS     KL_runtime_from_MP
      CM     9006
      M_display_module_error
    ENDI

    L      TR_axes_group_01_in_motion[BL_axes_group_index]
    AN     MG_TNC_programming_station_with_PLC
    IFT
      L      PN_W524_servo_drive_release_axes_groups
      BS     BL_axes_group_index
      =      PN_W524_servo_drive_release_axes_groups
    ELSE
      L      PN_W524_servo_drive_release_axes_groups
      BC     BL_axes_group_index
      =      PN_W524_servo_drive_release_axes_groups
    ENDI

    INC    BL_axes_group_index

    L      BL_axes_group_index
    >      KL_axis_group_8
  UNTILT

#endif

;Automatic activation of additional axes (e.g. index table)

  L      I_additional_axes_mounted
  XO     ML_additional_axis_mounted
  AN     NP_M4172_1st_PLC_run_after_power_on
  AN     NP_M4173_1st_PLC_run_after_compile
  S      PN_error_selection_additional_axis

  L      I_additional_axes_mounted
  =      ML_additional_axis_mounted

;Evaluation NC macros

  L      WG_number_active_NC_macro
  <>     WL_number_active_NC_macro
  IFT
    L      WL_number_active_NC_macro
    =      WG_number_last_active_NC_macro
  ENDI

  L      WG_number_active_NC_macro
  =      WL_number_active_NC_macro

EM

;----------------------------
LBL Absolute
;----------------------------

  PLW
  PSW
  >=     K+0
  EMT
  PLW
  XON    K+0
  +      K+1
  PSW

EM

;----------------------------
LBL End_PLC_Cycle
;----------------------------

;Soft key keys pressed

  L      NP_W302_number_PLC_soft_key_horizontal
  <>     KL_inactive
  =      MG_SK_horizontal_pressed

  L      NP_W304_number_PLC_soft_key_vertical
  <>     KL_inactive
  =      MG_SK_vertical_pressed

;NC block read interlock

  L      MG_NC_block_read_interlock
  A      NP_M4154_mode_program_run_full_sequence
  A      NP_M4176_control_in_operation_on_or_flashes
  AN     ML_NC_read_interlock_single_block
  CMT    Key_single_block

  L      ML_NC_read_interlock_single_block
  A      NP_M4153_mode_program_run_single_block
  AN     NP_M4176_control_in_operation_on_or_flashes
  CMT    Key_full_sequence

;Axis error compensation

  L      MG_TC_auto_active
  IFT
    L      WG_temp_compensation_X
    =      PN_W576_lag_error_compensation_X

    L      WG_temp_compensation_Y
    =      PN_W578_lag_error_compensation_Y

    L      WG_temp_compensation_Z
    =      PN_W580_lag_error_compensation_Z
  ELSE
    L      WG_temp_compensation_X
    +      WG_axis_compensation_X
    =      PN_W576_lag_error_compensation_X

    L      WG_temp_compensation_Y
    +      WG_axis_compensation_Y
    =      PN_W578_lag_error_compensation_Y

    L      WG_temp_compensation_Z
    +      WG_axis_compensation_Z
    =      PN_W580_lag_error_compensation_Z
  ENDI

#if CFG_timer_MP4111_additional       = "active"
  L      K+0
  =X

  REPEAT
    L      TS_Timer[X]
    IFT
      LX
      +      K &TR_Timer
      PSW
      PS     KL_runtime_from_MP
      CM     9006
      M_display_module_error
    ENDI

    INCX

    LX
    >      K &TS_Timer_end - &TS_Timer
  UNTILT
#endif

  LN     MG_marker_one
  O      MG_marker_zero
  S      PN_error_one_zero_marker_modified

  LW     PN_M4120_strobe_PLC_posit_axis_X
  >      K+0
  O      MG_axes_PLC_posit_active
  =      MG_PLC_strobe_active

  L      ML_reset_init_macro_started
  AN     NP_M4176_control_in_operation_on_or_flashes
  S      MG_reset_init_macro_executed

  LN     ML_reset_init_macro_started
  IFT
    LN     MG_PLC_strobe_active
    A      MG_reference_axes_traversed
    A      MG_power_delayed_on
    AN     MG_gear_initialize
    IFT
      L      MG_spindle_reference_using_sensor
      S      MG_spindle_reference_active

      RPLY   BL_reset_init_macro
      <>     K+0
      EMT

      SUBM   Reset_init_macro
      =      BL_reset_init_macro
      ==     K+0
      S      PN_error_submit_spawn_queue_full

    ENDI
  ENDI

  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 Reset_init_macro

  PS     S"STARTUPCANCEL"
  CM     9291
  M_display_module_error

  L      MG_marker_one
  S      ML_reset_init_macro_started

EM

;----------------------------
LBL Key_single_block

  PS     KL_0_test_key_queue
  CM     9180
  PL     BL_number_key_in_queue

  M_display_module_error

  L      BL_number_key_in_queue
  ==     K+0
  IFT
    PS     KG_key_single_block_mode
    CM     9180
    PL     BL_number_key_in_queue

    M_display_module_error

    LN     NP_M4203_error_module_9xxx
    S      ML_NC_read_interlock_single_block
  ENDI

EM

;----------------------------
LBL Key_full_sequence

  PS     KL_0_test_key_queue
  CM     9180
  PL     BL_number_key_in_queue

  M_display_module_error

  L      BL_number_key_in_queue
  ==     K+0
  IFT
    PS     KG_key_full_sequence_mode
    CM     9180
    PL     BL_number_key_in_queue

    M_display_module_error

    L      MG_marker_one
    R      ML_NC_read_interlock_single_block
  ENDI

EM
