*-----------------------------------------------------------------------------
*  Spindle 1/2
*-----------------------------------------------------------------------------
*  Module description
*    - 2 spindle mode can be activated by MP4020 bit_5
*
*
*-----------------------------------------------------------------------------

#define /s    KL_spindle_1                             K +0
#define /s    KL_spindle_2                             K +1
#define /s    KL_active_spindle                        K +0
#define /s    KL_1_percent                             K +100
#define /s    KL_5_percent                             K +500
#define /s    KL_10_percent                            K +1000
#define /s    KL_100_percent                           K +10000

#define /s    ML_S_actual_rpm_0                        M
#define /s    ML_S_nominal_off_status_inverse          M
#define /s    ML_spindle_delayed_stop                  M
#define /s    ML_NC_stop_2_axes_spindle                M
#define /s    ML_S_rpm_actl_nominal_transfer           M
#define /s    ML_operating_time_on                     M
#define /s    ML_operating_time_was_on                 M
#define /s    ML_spindle_initialized                   M
#define /s    ML_key_pressed                           M
#define /s    ML_S_override_initialized                M

#define /s    BL_S1_S2_shift                           B
#define /s    BL_step_S_C_axis                         B

#define /s    WL_S_override_plc                        W
#define /s    WL_factor_S_override                     W

#define /s    DL_offset                                D

#include Mac_Lib.inc

#if (CFG_override_rapid_traverse_instead_spindle  = "inactive") or (CFG_S_in_axes_mode = "active")
EXTERN Absolute
#endif

GLOBAL Spindle

;----------------------------
LBL Spindle
;----------------------------

  CM     Spindle_shift

  L      MG_spindle_orientation_active
  AN     PN_M4011_S_speed_from_MP3520_0
  O      MG_spindle_reference_active
  O      NP_M4000_S_in_position
  O      NP_M4017_S_controlled_in_motion
  O      NP_M4031_tapping_cycle_controlled
  A      MG_spindle_with_ROD
  A      I_ESB_servo_drive_release
  A      MG_servo_drive_release_spindle
  =N     PN_M4012_S_open_control_loop

  L      MG_key_spindle_M3
  AN     MG_control_in_operation_on
  O      MG_M03_spindle_right_cw_on
  O      MG_M13_Spindle_right_coolant_on
  S      PN_M4005_S_M03_nominal_and_status
  R      PN_M4006_S_M04_nominal_and_status
  R      PN_M4007_S_M05_nominal_0_and_status
  R      ML_S_nominal_off_status_inverse

  L      MG_key_spindle_M4
  AN     MG_control_in_operation_on
  O      MG_M04_spindle_left_ccw_on
  O      MG_M14_Spindle_left_coolant_on
  R      PN_M4005_S_M03_nominal_and_status
  S      PN_M4006_S_M04_nominal_and_status
  R      PN_M4007_S_M05_nominal_0_and_status
  R      ML_S_nominal_off_status_inverse

  L      MG_key_spindle_M5
  O      MG_TC_spindle_orientation_on
  AN     MG_control_in_operation_on
  AN     MG_axes_key_memory_function
  O      MG_pulse_END_PGM_M02_M30
  O      MG_pulse_internal_stop
  O      MG_M05_spindle_off
  O      MG_M15_Spindle_off_coolant_off
  O      MG_M18_spindle_reference
  O      MG_M19_spindle_orientation_0_degree
  O      MG_M20_spindle_orientation_CYCL_13
  O      MG_M41_spindle_1
  O      MG_M42_spindle_2
  O      NP_M4073_strobe_TOOL_CALL
  O      MG_TC_help
  O      MG_touch_probe_active
  ON     MG_control_operational
  O[
    L      MG_auto_manu_mode_change
    O      MG_NC_stop_in_manual_mode
    AN     MG_key_NC_stop_without_spindle_stop
  ]
  R      PN_M4005_S_M03_nominal_and_status
  R      PN_M4006_S_M04_nominal_and_status
  S      PN_M4007_S_M05_nominal_0_and_status

  L      MG_spindle_off
  O      NP_M4030_tapping_cycle_active
  ON     PN_M4012_S_open_control_loop
  A[
    L      MG_M05_spindle_off
    O      MG_M15_Spindle_off_coolant_off
  ]
  O[
    L      MG_M03_spindle_right_cw_on
    O      MG_M04_spindle_left_ccw_on
    O      MG_M13_Spindle_right_coolant_on
    O      MG_M14_Spindle_left_coolant_on
    AN     MG_touch_probe_active
  ]
  S      PN_M4092_quit_M_function

  LN     MG_spindle_active
  EMT

  L      MG_control_in_operation_on
  A      MG_automatic_mode
  AN     NP_M4193_RUNCANCEL_macro_active
  AN     MG_NC_stop_active
  O      MG_key_spindle_M3
  O      PN_M4007_S_M05_nominal_0_and_status
  O      MG_spindle_on_after_gear_change
  R      ML_S_nominal_off_status_inverse

  L      MG_spindle_on_axes
  AN     MG_3D_head_active
  R      MG_spindle_on_axes
  R      ML_S_nominal_off_status_inverse

  L      MG_key_NC_stop_2_axes_spindle
  S      ML_NC_stop_2_axes_spindle

  L      NP_M4221_PET_table_NC_stop
  O      NP_M4223_PET_table_NC_cancel
  O      ML_NC_stop_2_axes_spindle
  ON     MG_key_NC_stop_without_spindle_stop
  IFT
    L      MG_NC_stop_active
;$WS    O      MG_NC_stop_in_manual_mode
    O      NP_M4221_PET_table_NC_stop
    O      NP_M4223_PET_table_NC_cancel
    A[
      L      MG_axes_in_position
      O      NP_M4151_mode_electronic_handwheel
    ]
    AN     MG_axes_key_memory_function
    =N     TS_delayed_spindle_stop

    LN     TS_delayed_spindle_stop
    AN     TR_delayed_spindle_stop
    S      ML_S_nominal_off_status_inverse

    L      MG_spindle_stand_still
    R      ML_NC_stop_2_axes_spindle
  ENDI

  L      NP_M4070_strobe_G_code
  O      MG_programmed_stop_M00_active
  O      PN_error_guard_open
  O      PN_error_gear_change
  O      PN_error_spindle_1_2_activate
  O      NP_M4158_mode_block_scan
  O      MG_TC_manual
  O      MG_spindle_C_axis_mode_active
  ON     MG_gear_range_ok
  O      MG_Sn_I_tool_unclamped
  ON     MG_Sn_I_tool_clamped
  S      ML_S_nominal_off_status_inverse

  L      PN_M4007_S_M05_nominal_0_and_status
  R      ML_S_nominal_off_status_inverse

  CM     Spindle_lamps
*$WS testen
  CM     Spindle_jogging

;Servo drive release

  LN     PN_M4007_S_M05_nominal_0_and_status
  AN     ML_S_nominal_off_status_inverse
  O      PN_M4009_S_rotation_left
  O      PN_M4010_S_rotation_right
  O      PN_M4011_S_speed_from_MP3520_0
  ON     PN_M4012_S_open_control_loop
  IFT
    S      O_servo_drive_release_S1
    R      O_servo_drive_release_S2

    L      MG_spindle_2_active
    R      O_servo_drive_release_S1
    S      O_servo_drive_release_S2
  ENDI

  L      PN_M4007_S_M05_nominal_0_and_status
  O      ML_S_nominal_off_status_inverse
  A      PN_M4012_S_open_control_loop
  A      MG_spindle_stand_still
*$WS Unterbrechung sollte ausschalten
  A[
    LN     NP_M4030_tapping_cycle_active
    O      MG_NC_stop_active
  ]
  AN     PN_M4009_S_rotation_left
  AN     PN_M4010_S_rotation_right
  ON     MG_power_delayed_on
  ON     I_ESB_servo_drive_release
  ON     MG_servo_drive_release_spindle
  O      MG_YD_switch_rpm_dependend
  R      O_servo_drive_release_S1
  R      O_servo_drive_release_S2

  L      WG_drive_temperature_S
  >=     DG_S1_fan_on_temperature[BG_number_active_spindle]
  A      I_emergency_button_unlocked
  S      O_S1_spindle_fan
  S      O_S2_spindle_fan

  L      WG_drive_temperature_S
  <=     DG_S1_fan_off_temperature[BG_number_active_spindle]
  R      O_S1_spindle_fan
  R      O_S2_spindle_fan

  L      BG_number_active_spindle
  ==     KL_spindle_1
  RN     O_S1_spindle_fan
  R      O_S2_spindle_fan

  L      MG_spindle_digital
  IFT
    LN     PN_M4007_S_M05_nominal_0_and_status
    AN     MG_servo_drive_ready_S
    AN     MG_YD_switch_rpm_dependend
    O      ML_S_nominal_off_status_inverse
    =      PN_M4008_S_nominal_off_status_inverse
  ELSE
    LN     PN_M4007_S_M05_nominal_0_and_status
    A      TR_servo_drive_release_S
    O      ML_S_nominal_off_status_inverse
    =      PN_M4008_S_nominal_off_status_inverse
  ENDI

;Operating time S1/S2

  L      PN_M4012_S_open_control_loop
  O      NP_M4030_tapping_cycle_active
  O      NP_M4031_tapping_cycle_controlled
  A[
    L      O_servo_drive_release_S1
    O      O_servo_drive_release_S2
  ]
  =      ML_operating_time_on

  L      ML_operating_time_on
  AN     ML_operating_time_was_on
  IFT
    LN     MG_spindle_2_active
    IFT
      PS     KG_operating_time_S1
    ELSE
      PS     KG_operating_time_S2
    ENDI
    CM     9190
    M_display_module_error
  ENDI

  LN     ML_operating_time_on
  A      ML_operating_time_was_on
  IFT
    LN     MG_spindle_2_active
    IFT
      PS     KG_operating_time_S1
    ELSE
      PS     KG_operating_time_S2
    ENDI
    CM     9191
    M_display_module_error
  ENDI

  L      ML_operating_time_on
  =      ML_operating_time_was_on

  L      MG_S_actual_equal_nominal_rpm
  AN     ML_S_actual_rpm_0
  AN     NP_M4002_S_nominal_0
  =      MG_spindle_nominal_rpm_reached

  L      ML_S_actual_rpm_0
  O      MG_spindle_C_axis_mode_active
  A      MG_S_actual_equal_nominal_rpm
  A      NP_M4001_S_nominal_not_in_ramp
  A      NP_M4002_S_nominal_0
  =      MG_spindle_stand_still

  L      PN_M4007_S_M05_nominal_0_and_status
  O      PN_M4008_S_nominal_off_status_inverse
  A      MG_spindle_stand_still
  AN     MG_servo_drive_ready_S
  AN     O_servo_drive_release_S1
  AN     O_servo_drive_release_S2
  =      MG_spindle_off

  CM     Spindle_diagnosis
  CM     Error_messages
  CM     Spindle_reduction
  CM     Spindle_override
#if CFG_S_in_axes_mode = "active"
  CM     Spindle_C_axis_mode
#endif

EM

;----------------------------
LBL Spindle_reduction

  L      K+99999999
  =      PN_D604_max_S_rpm_PLC

  LN     MG_axes_in_motion
  R      MG_guard_has_been_opened

  LN     I_guard_1_closed_locked
  ON     I_guard_2_closed_locked
  AN     MG_alternating_table_on
  O[
    LN     I_guard_1_closed_locked
    A      MG_axis_limit_1
    O[
      ON     I_guard_2_closed_locked
      A      MG_axis_limit_2
    ]
  ]
  AN     MG_guard_inactive
  AN     MG_SLS_operating_mode_2_set_up
  AN     MG_SLS_operating_mode_3
  AN     MG_SLS_operating_mode_4
  S      MG_guard_has_been_opened

  L      MG_SLS_operating_mode_4
  IFT
    L      DG_S1_operation_mode_4_rpm
    =      PN_D604_max_S_rpm_PLC
  ENDI

  L      MG_SLS_operating_mode_3
  IFT
    L      DG_S1_operation_mode_3_rpm
    =      PN_D604_max_S_rpm_PLC
  ENDI

  L      MG_SLS_operating_mode_2_set_up
  IFT
    L      DG_S1_operation_mode_2_rpm
    =      PN_D604_max_S_rpm_PLC
  ENDI

  L      MG_guard_has_been_opened
  O      MG_TC_help
  O      MG_PC_help
  O      MG_spindle_chiller_inactive   ;20120803
  O      MG_pneumatic_source_inactive  ;20120808
  IFT
    L      DG_S1_service_rpm[BG_number_active_spindle]
    =      PN_D604_max_S_rpm_PLC
  ENDI
*$$WS
;    LN     MG_S1_Test_Smax_bei_WZ_Laenge_inaktiv[BG_Nummer_aktive_Spindel]
;    IFT
;      LN     PN_M4007_S_M05_Sollwert_0_und_Status
;      AN     PN_M4008_S_Sollwert_aus_Status_invers
;      O[
;        L      NP_D356_S_Programmierte_Drehzahl_NC
;        <>     DL_letzte_Programmierte_Drehzahl_NC
;      ]
;      A[
;        L      NP_D356_S_Programmierte_Drehzahl_NC
;        >      DG_max_S_Drehzahl_T_Laenge
;      ]
;      S      PN_Fehler_Smax_bei_T_Laenge
;
;      L      NP_D356_S_Programmierte_Drehzahl_NC
;      <=     DG_max_S_Drehzahl_T_Laenge
;      R      PN_Fehler_Smax_bei_T_Laenge
;
;      L      NP_D356_S_Programmierte_Drehzahl_NC
;      =      DL_letzte_Programmierte_Drehzahl_NC
;
;      L      DG_max_S_Drehzahl_T_Laenge
;      =      PN_D604_Max_S_Drehzahl_PLC
;
;
;    ENDI

;  L      MG_S_Schmierung_Einlauf
;  IFT
;
;    L      DG_S1_Einlauf_max_Drehzahl[BG_Nummer_aktive_Spindel]
;    <      PN_D604_Max_S_Drehzahl_PLC
;    IFT
;      L      DG_S1_Einlauf_max_Drehzahl[BG_Nummer_aktive_Spindel]
;      =      PN_D604_Max_S_Drehzahl_PLC
;    ENDI
;  ENDI

EM

;----------------------------
LBL Spindle_diagnosis

  L      MG_spindle_digital
  O      MG_spindle_with_ROD
  IFT
    L      MG_spindle_in_Uf_control_mode
    IFT
      L      DG_actual_drive_rpm_S
      /      K+1000
      =      DG_spindle_actual_rpm
    ELSE
      L      NP_D368_S_actual_rpm
      =      DG_spindle_actual_rpm
    ENDI

    L      NP_D372_S_end_speed
    <=     K+100
    IFT
      L      NP_D372_S_end_speed
      -      DG_spindle_actual_rpm
      <      DG_S1_rpm_difference_100[BG_number_active_spindle]
      =      MG_S_actual_equal_nominal_rpm
    ELSE
      L      DG_spindle_actual_rpm
      X      K+100
      /      NP_D372_S_end_speed
      >      DG_S1_rpm_nominal[BG_number_active_spindle]
      =      MG_S_actual_equal_nominal_rpm
    ENDI

    L      DG_spindle_actual_rpm
    <      DG_S1_rpm_min[BG_number_active_spindle]
    A[
      L      PN_M4007_S_M05_nominal_0_and_status
      O      ML_S_nominal_off_status_inverse
    ]
    =      ML_S_actual_rpm_0
    S      MG_S_actual_equal_nominal_rpm
  ENDI

  LN     MG_spindle_digital
  AN     MG_spindle_with_ROD
  IFT
    LN     MG_spindle_2_active
    IFT

#if &I_S1_actual_equal_nominal_rpm > 999
      L      NP_M4001_S_nominal_not_in_ramp
      =      I_S1_actual_equal_nominal_rpm
#endif

      L      I_S1_actual_equal_nominal_rpm
      =      MG_S_actual_equal_nominal_rpm

#if &I_S1_actual_lower_nominal_rpm > 999
      L      NP_M4002_S_nominal_0
      =      I_S1_actual_lower_nominal_rpm
#endif

      L      I_S1_actual_lower_nominal_rpm
      =      ML_S_actual_rpm_0

    ELSE

#if &I_S2_actual_equal_nominal_rpm > 999
      L      NP_M4001_S_nominal_not_in_ramp
      =      I_S2_actual_equal_nominal_rpm
#endif

      L      I_S2_actual_equal_nominal_rpm
      =      MG_S_actual_equal_nominal_rpm

#if &I_S2_actual_lower_nominal_rpm > 999
      L      NP_M4002_S_nominal_0
      =      I_S2_actual_lower_nominal_rpm
#endif

      L      I_S2_actual_lower_nominal_rpm
      =      ML_S_actual_rpm_0

    ENDI
  ENDI

  L      MG_spindle_digital
  EMT

  L      NP_W480_voltage_input_0_on_LE[BG_number_active_spindle]
  X      DG_S1_factor_utilization_display[BG_number_active_spindle]
  /      K+10
  =      WG_drive_utilization_S

  LN     I_S1_spindle_temperature_ok
  AN     MG_spindle_2_active
  S      PN_error_drive_temp_max
  IFT
    L      KG_spindle
    =X
    M_error_motor_temp_max
  ENDI

  LN     I_S2_spindle_temperature_ok
  A      MG_spindle_2_active
  S      PN_error_drive_temp_max
  IFT
    L      KG_spindle
    =X
    M_error_motor_temp_max
  ENDI

EM

;----------------------------
LBL Spindle_jogging

  LN     PN_M4007_S_M05_nominal_0_and_status
  AN     PN_M4008_S_nominal_off_status_inverse
  O      MG_spindle_orientation_active
  ON     MG_manual_mode
  EMT

  L      MG_3D_head_axis_1_service_unclamping
  O      MG_3D_head_axis_2_service_unclamping
  IFT
    L      MG_key_S_jog_right_cw
    =      PN_M4010_S_rotation_right

    L      MG_key_S_jog_left_ccw
    =      PN_M4009_S_rotation_left
  ELSE
    L      MG_key_S_jog_right_cw
    O      MG_key_S_jog_left_ccw
    =      PN_M4011_S_speed_from_MP3520_0

    L      MG_key_S_jog_left_ccw
    =      PN_M4014_S_reverse_direction_of_rotation

    L      MG_key_S_jog_right_cw
    =      O_lamp_S_jog_right_cw       ;20120926

    L      MG_key_S_jog_left_ccw
    =      O_lamp_S_jog_left_ccw       ;20120926
  ENDI

EM

;----------------------------
LBL Spindle_lamps

  L      PN_M4005_S_M03_nominal_and_status
  O      MG_test_lamps
  =      O_lamp_spindle_M3

  L      PN_M4006_S_M04_nominal_and_status
  O      MG_test_lamps
  =      O_lamp_spindle_M4

  L      PN_M4008_S_nominal_off_status_inverse
  A      MG_clock_1_s
  R      O_lamp_spindle_M3
  R      O_lamp_spindle_M4

  L      ML_S_actual_rpm_0
  O      MG_test_lamps
  =      O_lamp_spindle_M5

EM

;----------------------------
LBL Error_messages

  LN     MG_power_delayed_on
  S      TS_spindle_rpm_change
  EMT

  L      MG_S_actual_equal_nominal_rpm
  O      MG_spindle_stand_still
  O      MG_key_NC_start
  =      TS_spindle_rpm_change

  LN     TR_spindle_rpm_change
  AN     NP_M4002_S_nominal_0
  S      PN_error_S_actual_equal_nominal_rpm

  LN     TR_spindle_rpm_change
  AN     TS_spindle_rpm_change
  A      NP_M4002_S_nominal_0
  S      PN_error_S_actual_lower_nominal_rpm

  L      MG_spindle_digital
  EMT

  LN     MG_spindle_2_active
  IFT
    L      I_S1_spindle_I_max
    =      PN_error_spindle_I_max

    LN     O_servo_drive_release_S1
    =      TS_servo_drive_release_S

    LN     I_S1_spindle_operational
    AN     TR_servo_drive_release_S
    AN     TS_servo_drive_release_S
    S      PN_error_spindle_operational
  ELSE
    L      I_S2_spindle_I_max
    =      PN_error_spindle_I_max

    LN     O_servo_drive_release_S2
    =      TS_servo_drive_release_S

    LN     I_S2_spindle_operational
    AN     TR_servo_drive_release_S
    AN     TS_servo_drive_release_S
    S      PN_error_spindle_operational
  ENDI

EM

;----------------------------
LBL Spindle_override

#if CFG_override_rapid_traverse_instead_spindle  = "active"

  L      MG_key_override_S_100
  AN     ML_key_pressed
  XO     MG_SK_override_S_100
  =      MG_SK_override_S_100

  L      MG_SK_override_S_100
  ON     ML_S_override_initialized
  IFT
    S      ML_S_override_initialized

    L      KL_100_percent
    =      WL_S_override_plc
  ENDI

  L      MG_key_override_S_plus
  O      MG_SK_override_S_plus
  IFT
    R      MG_SK_override_S_100

    LN     ML_key_pressed
    IFT
      L      WL_S_override_plc
      +      KL_1_percent
      =      WL_S_override_plc
    ENDI

    LN     TR_keys_pulse_identifier
    A      MG_pulse_250_ms
    IFT
      L      WL_S_override_plc
      +      KL_5_percent
      /      KL_5_percent
      X      KL_5_percent
      =      WL_S_override_plc
    ENDI
  ENDI

  L      MG_key_override_S_minus
  O      MG_SK_override_S_minus
  IFT
    R      MG_SK_override_S_100

    LN     ML_key_pressed
    IFT
      L      WL_S_override_plc
      -      KL_1_percent
      =      WL_S_override_plc
    ENDI

    LN     TR_keys_pulse_identifier
    A      MG_pulse_250_ms
    IFT
      L      WL_S_override_plc
      -      KL_1_percent
      /      KL_5_percent
      X      KL_5_percent
      =      WL_S_override_plc
    ENDI
  ENDI

  L      WL_S_override_plc
  =      PN_W764_S_override_PLC

  L      MG_key_override_S_plus
  O      MG_key_override_S_minus
  O      MG_SK_override_S_plus
  O      MG_SK_override_S_minus
  O      MG_key_override_S_100
  =      ML_key_pressed
  R      TS_keys_pulse_identifier
#else
  L      MG_key_override_S_100
  AN     ML_key_pressed
  XO     MG_SK_override_S_100
  =      MG_SK_override_S_100
  IFT
    L      KL_100_percent
    =      PN_W764_S_override_PLC

    L      NP_W492_S_override_NC
    -      WL_factor_S_override
    PSW
    CM     Absolute
    PLW
    >=     KL_10_percent
    R      MG_SK_override_S_100
  ELSE
    L      NP_W492_S_override_NC
    =      WL_factor_S_override
  ENDI

  L      MG_key_override_S_100
  =      ML_key_pressed
#endif

  L      PN_W764_S_override_PLC
  >      WG_MP3310_0_S_override_max[BG_number_active_spindle]
  IFT
    L      WG_MP3310_0_S_override_max[BG_number_active_spindle]
    =      PN_W764_S_override_PLC
    =      WL_S_override_plc
  ENDI

  L      PN_W764_S_override_PLC
  <      WG_MP3310_1_S_override_min[BG_number_active_spindle]
  IFT
    L      WG_MP3310_1_S_override_min[BG_number_active_spindle]
    =      PN_W764_S_override_PLC
    =      WL_S_override_plc
  ENDI

EM

;----------------------------
LBL Spindle_shift

  LN     MG_2_spindle_mode_active
  IFT
    L      MG_M41_spindle_1
    O      MG_M42_spindle_2
    S      PN_error_M_function_not_used

    EM
  ENDI

  CASE   BL_S1_S2_shift
    CM     Activate                    ;00
    CM     Shift                       ;01
    CM     Shift_ok                    ;02
    CM     MP_read                     ;03
    CM     Gear_init                   ;04
    CM     Spindle_S2_on_off           ;05
    CM     M_quit                      ;06
    CM     S_shift_end                 ;07
  ENDC

  PLL
  IFT
    INC    BL_S1_S2_shift
  ENDI

  LN     MG_spindle_1_active
  R      O_servo_drive_release_S1

  LN     MG_spindle_2_active
  R      O_servo_drive_release_S2

  LN     MG_control_operational
  CMT    S_shift_reset

EM

;----------------------------
LBL Activate

  L      MG_spindle_2_active
  XO     I_S2_spindle_active
  S      PN_error_spindle_1_2_activate

  L      BG_number_active_spindle
  <>     KL_spindle_1
  A      MG_spindle_1_active
  A      ML_spindle_initialized
  S      PN_error_spindle_1_2_activate

  L      BG_number_active_spindle
  <>     KL_spindle_2
  A      MG_spindle_2_active
  A      ML_spindle_initialized
  S      PN_error_spindle_1_2_activate

  LN     MG_spindle_1_active
  AN     MG_spindle_2_active
#ifdef CFG_prog_station_OEM or CFG_prog_station_public
  A      ML_spindle_initialized
#endif
  S      PN_error_spindle_1_2_activate

  L      MG_M41_spindle_1
  A      MG_spindle_1_active
  O[
    L      MG_M42_spindle_2
    A      MG_spindle_2_active
  ]
  S      PN_M4092_quit_M_function
  JPT    S_shift_end

  L      MG_M41_spindle_1
  O      MG_M42_spindle_2
  A      PN_M4007_S_M05_nominal_0_and_status
  A      MG_control_operational
  A      MG_spindle_off
  ON     ML_spindle_initialized
  A      MG_machine_parameter_are_read
  S      MG_spindle_change_active
  PSL

EM

;----------------------------
LBL Shift

  PS     K+0
  PS     KL_active_spindle
  CM     9179
  PL     BG_number_active_spindle
  M_display_module_error

  L      BG_number_active_spindle
  <>     KL_spindle_1
  A      MG_M41_spindle_1
  IFT
    PS     KL_spindle_1
    CM     9175

    M_display_module_error
  ENDI

  L      MG_spindle_2_active
  AN     ML_spindle_initialized
  O      MG_M42_spindle_2
  A[
    L      BG_number_active_spindle
    <>     KL_spindle_2
  ]
  IFT
    PS     KL_spindle_2
    CM     9175

    M_display_module_error
  ENDI

  LN     NP_M4203_error_module_9xxx
  PSL

  L      NP_M4203_error_module_9xxx
  S      PN_error_spindle_1_2_activate
  JPT    S_shift_reset

EM

;----------------------------
LBL Shift_ok

  L      MG_marker_one
  R      MG_spindle_1_active
  R      MG_spindle_2_active
  R      MG_spindle_active

  PS     K+0
  PS     KL_active_spindle
  CM     9179
  PL     BG_number_active_spindle
  M_display_module_error

  LN     NP_M4203_error_module_9xxx
  S      PN_M4015_S_reference_reset
  PSL

  L      NP_M4203_error_module_9xxx
  S      PN_error_spindle_1_2_activate
  JPT    S_shift_reset

  L      BG_number_active_spindle
  ==     KL_spindle_1
  S      MG_spindle_1_active

  L      BG_number_active_spindle
  ==     KL_spindle_2
  S      MG_spindle_2_active

EM

;----------------------------
LBL MP_read

  L      MG_marker_one
  R      MG_machine_parameter_are_read
  PSL

EM

;----------------------------
LBL Gear_init

  L      MG_machine_parameter_are_read
  S      MG_gear_initialize
  PSL

EM

;----------------------------
LBL Spindle_S2_on_off

  L      MG_spindle_1_active
  A      I_S2_spindle_active
  =      PN_error_put_down_S2_spindle

  L      MG_spindle_2_active
  AN     I_S2_spindle_active
  =      PN_error_put_in_S2_spindle

  LN     PN_error_put_in_S2_spindle
  AN     PN_error_put_down_S2_spindle
  ON     ML_spindle_initialized
  PSL

EM

;----------------------------
LBL M_quit

  LN     NP_M4072_strobe_M_function
  AN     MG_gear_initialize
  AN     PN_M4134_strobe_G_range_S_speed
  S      ML_spindle_initialized
  PSL

  L      MG_M41_spindle_1
  O      MG_M42_spindle_2
  AN     MG_gear_initialize
  AN     PN_M4134_strobe_G_range_S_speed
  S      PN_M4092_quit_M_function

EM

;----------------------------
LBL S_shift_end

  PS     MG_marker_zero

LBL S_shift_reset

  L      MG_marker_one
  R      MG_spindle_change_active
  R      PN_M4015_S_reference_reset

  L      K+0
  =      BL_S1_S2_shift

EM

#if CFG_S_in_axes_mode = "active"
;----------------------------
LBL Spindle_C_axis_mode

;*--> $WS Minimal-Lösung ohne Synchronisierung Spindel Position
;  L      WG_S_Achs_Aktivierung
;  BT     KG_S_Achs_Index
;  A      MG_Spindel_aus
;  IFT
;    L      WG_S_Achs_Aktivierung
;    BS     KG_S_Achs_Index
;    =      WG_S_Achs_Freigabe
;
;    L      NP_D1104_Achsen_in_Position
;    BT     KG_S_Achs_Index
;    AN     NP_M4075_Strobe_FN19
;    AN     MG_M11_Loesen_Achsen
;    IFT
;      L      WG_S_Achs_Freigabe
;      BC     KG_S_Achs_Index
;      =      WG_S_Achs_Freigabe
;    ENDI
;  ENDI
;
;EM
;*<--

  CASE   BL_step_S_C_axis
    CM     C_axis_activate             ;0
    CM     S_start_C_pos               ;1
    CM     S_posit_ok                  ;2
    CM     C_release_on                ;3
    CM     C_release_off               ;4
    CM     C_axis_end                  ;5
  ENDC

  PLL
  IFT
    INC    BL_step_S_C_axis
  ENDI

  L      MG_spindle_C_axis_mode_active
  R      PN_error_S_actual_equal_nominal_rpm
  R      PN_error_S_actual_lower_nominal_rpm

  LN     I_ESA_control_operational
  CMT    C_axis_reset

EM

;----------------------------
LBL C_axis_activate                    ;0

  L      DG_S_axis_activation
  BT     KG_S_axis_index
  S      MG_spindle_C_axis_release_active
  PSL

EM

;----------------------------
LBL S_start_C_pos                      ;1

  L      MG_marker_one
  S      MG_C_axis_spindle_orientation_on
  PSL

  L      K+3600000
  -      DG_position_axis_X_ref[KG_S_axis_index]
  =      DG_C_axis_S_position

EM

;----------------------------
LBL S_posit_ok                         ;2

  L      MG_C_axis_interruption
  =      MG_win_C_axis_spindle_position

  L      DG_C_axis_S_position
  -      DG_position_axis_S_actual
  PSW
  CM     Absolute
  PLW
  <      DG_pos_window_S
  O[
    L      DG_C_axis_S_position
    ==     K+0
    A[
      L      K+3600000
      -      DG_position_axis_S_actual
      <      DG_pos_window_S
    ]
  ]
  O[
    L      DG_C_axis_S_position
    ==     K+3600000
    A[
      L      K+0
      -      DG_position_axis_S_actual
      <      DG_pos_window_S
    ]
  ]
  AN     MG_C_axis_spindle_orientation_on
  AN     MG_C_axis_interruption
  S      MG_spindle_off_orientation
  PSL

EM

;----------------------------
LBL C_release_on                       ;3

  L      MG_spindle_off
  PSL
  IFT
    L      DG_S_axis_activation
    BS     KG_S_axis_index
    =      DG_S_axis_release
  ENDI

EM

;----------------------------
LBL C_release_off                      ;4

  L      NP_D1104_axes_in_position
  BT     KG_S_axis_index
  AN     NP_M4075_strobe_transfer_FN19
  AN     MG_M11_unclamping_axes
  PSL
  IFT
    L      DG_S_axis_release
    BC     KG_S_axis_index
    =      DG_S_axis_release

    L      DG_S_axis_activation
    BC     KG_S_axis_index
    =      DG_S_axis_activation
  ENDI

EM

;----------------------------
LBL C_axis_end                         ;5

  PS     MG_marker_zero

;----------------------------
LBL C_axis_reset

  L      MG_marker_one
  R      MG_spindle_C_axis_release_active

  L      K+0
  =      BL_step_S_C_axis

EM
#endif

