*-----------------------------------------------------------------------------
*  Gear shift spindle YD - delta wye switchover
*-----------------------------------------------------------------------------
*  Module description
*
*  - YD switchover on spindle stand still
*       MP3010  : 6 - with gear shift signal
*       MP3210.0: n - Y connection motor speed
*       MP3510.0: n - Y connection spindle speed
*       MP3515.0: n - Y connection maximum spindle speed
*       MP3210.1: n - D connection motor speed
*       MP3510.1: n - D connection spindle speed
*       MP3515.1: n - D connection maximum spindle speed
*
*  - YD switchover on turning spindle (34049x 03)
*       MP3010  : 8 - without gear shift signal
*       MP3210.0: n - D connection motor speed
*       MP3510.0: n - D connection spindle speed
*       MP3515.0: n - D connection maximum spindle speed
*       MP3210.1: 0 - inactive
*       MP3510.1: 0 - inactive
*       MP3515.1: 0 - inactive
*       MP_Data.tab:
*         S1 switchover speed Y->D [1/min]: n(Y->D)
*         S2 switchover speed Y->D [1/min]: n(Y->D)
*         S1 switchover speed D->Y [1/min]: n(D->Y)
*         S2 switchover speed D->Y [1/min]: n(D->Y)
*         Note: n(Y->D) > n(D->Y) - about 200 1/min hysteresis (difference)
*
*  - serveral labels also called in module Gear_YD.src
*
*  - Input-/Output interface
*      I_S1_gear_range_1 - (S1 Y connection)
*      I_S1_gear_range_2 - (S1 D connection)
*      I_S2_gear_range_1 - (S2 Y connection)
*      I_S2_gear_range_2 - (S2 D connection)
*      O_S1_wye_delta_switchover - (S1 current pulse relay)
*      O_S2_wye_delta_switchover   (S2 current pulse relay)
*
*-----------------------------------------------------------------------------

#define /s    KL_spindle_Y_mode                        K +0
#define /s    KL_spindle_D_mode                        K +1
#define /s    KL_S_YD_monitoring_inactive              K %000
#define /s    KL_S_YD_fast_monitoring_active           K %111
#define /s    KL_off_delay_YD_switch                   K +0
#define /s    KL_on_delay_YD_switch                    K +0
#define /s    KL_off_delay_DY_switch                   K +0
#define /s    KL_on_delay_DY_switch                    K +0

#define /s    ML_Sn_O_servo_drive_release              M

#define /s    ML_spindle_YD_status                     M [8]
#define /c    ML_spindle_Y_mode                        M &ML_spindle_YD_status + 0
#define /c    ML_spindle_YD_switch                     M &ML_spindle_YD_status + 1
#define /c    ML_spindle_D_mode                        M &ML_spindle_YD_status + 2
#define /c    ML_spindle_DY_switch                     M &ML_spindle_YD_status + 3

#define /s    BL_spindle_YD_status                     B
#define /s    BL_step_gear_YD                          B
#define /s    BL_off_delay_relay_switch                B
#define /s    BL_on_delay_relay_switch                 B

#include Mac_Lib.inc

EXTERN Gear_initialize
EXTERN Backup_G_S
EXTERN Sn_Input_read
EXTERN Sn_Output_write
EXTERN Gear_on
EXTERN Test_gear_sensors
EXTERN Spindle_off
EXTERN Quit

GLOBAL Spindle_YD_gear

;----------------------------
LBL Spindle_YD_gear
;----------------------------

  L      NP_M4172_1st_PLC_run_after_power_on
  O      NP_M4173_1st_PLC_run_after_compile
  S      MG_gear_initialize

  LN     MG_spindle_active
  EMT

  CM     Sn_Input_read

  L      MG_gear_initialize
  A      MG_machine_parameter_are_read
  A      MG_power_delayed_on
  AN     NP_M4176_control_in_operation_on_or_flashes
  CMT    Gear_initialize
  CMT    Gear_YD_initialize

  L      PN_D756_S_nominal_rpm_PLC
  <>     DG_S_nominal_rpm_PLC
  CMT    Backup_G_S

  L      PN_D756_S_nominal_rpm_PLC
  =      DG_S_nominal_rpm_PLC

  LN     NP_M4070_strobe_G_code
  R      PN_M4090_quit_G_code

  L      MG_spindle_with_gear
  IFT
    CASE   BL_step_gear_YD
      CM     Gear_on                   ;00
      CM     Spindle_off               ;01
      CM     Coupling                  ;02
      CM     Quit                      ;03
      CM     Gear_end                  ;04
    ENDC
  ELSE
    L      K+0
    B=     ML_spindle_YD_status

    CM     9174                        ;Spindle Y/D status
    PL     BL_spindle_YD_status
  M_display_module_error

    L      MG_marker_one
    =      ML_spindle_YD_status[BL_spindle_YD_status]

    CASE   BL_step_gear_YD
      CM     YD_parameter              ;00
      CM     YD_relais                 ;01
      CM     YD_relais_ok              ;02
      CM     Gear_end                  ;03
    ENDC
  ENDI

  PLL
  IFT
    INC    BL_step_gear_YD
  ENDI

  LN     MG_gear_change_active
  AN     MG_YD_switch_rpm_dependend
  =      TS_gear_timeout

  L      PN_error_gear_change
  ON     MG_control_operational
  CMT    Gear_reset

  CM     Sn_Output_write

  L      MG_spindle_with_gear
  CMT    Test_gear_sensors
  CMF    Test_gear_YD_sensors

EM

;----------------------------
LBL YD_parameter

  L      MG_Sn_I_gear_range_1
  AN     MG_Sn_I_gear_range_2
  A      ML_spindle_YD_switch
  O[
    LN     MG_Sn_I_gear_range_1
    A      MG_Sn_I_gear_range_2
    A      ML_spindle_DY_switch
  ]
  A      MG_control_operational
  AN     NP_M4030_tapping_cycle_active
  PSL
  EMF

  L      ML_spindle_YD_switch
  IFT
*$WS debug
    L      KL_off_delay_YD_switch
    =      BL_off_delay_relay_switch

    L      KL_on_delay_YD_switch
    =      BL_on_delay_relay_switch

    PS     KG_spindle
    PS     KL_spindle_D_mode
    CM     9163                        ;Spindle Y/D switchover
      M_display_module_error
  ENDI

  L      ML_spindle_DY_switch
  IFT
*$WS debug
    L      KL_off_delay_DY_switch
    =      BL_off_delay_relay_switch

    L      KL_on_delay_DY_switch
    =      BL_on_delay_relay_switch

    PS     KG_spindle
    PS     KL_spindle_Y_mode
    CM     9163                        ;Spindle Y/D switchover
      M_display_module_error
  ENDI

EM

;----------------------------
LBL YD_relais

  L      MG_marker_one
  S      MG_YD_switch_rpm_dependend

  LN     MG_servo_drive_ready_S
  IFT
    DEC    BL_off_delay_relay_switch

    L      BL_off_delay_relay_switch
    <      K+0
    S      MG_Sn_O_YD_wye_delta_switchover
  ENDI

  L      MG_Sn_O_YD_wye_delta_switchover
  PSL

EM

;----------------------------
LBL YD_relais_ok

  LN     MG_Sn_I_gear_range_1
  A      MG_Sn_I_gear_range_2
  A      ML_spindle_YD_switch
  R      MG_Sn_O_YD_wye_delta_switchover

  L      MG_Sn_I_gear_range_1
  AN     MG_Sn_I_gear_range_2
  A      ML_spindle_DY_switch
  R      MG_Sn_O_YD_wye_delta_switchover

  LN     MG_Sn_O_YD_wye_delta_switchover
  IFT
    DEC    BL_on_delay_relay_switch

    L      BL_on_delay_relay_switch
    <      K+0
    R      MG_YD_switch_rpm_dependend
    S      ML_Sn_O_servo_drive_release
  ENDI

  L      MG_spindle_1_active
  IFT
    L      ML_Sn_O_servo_drive_release
    =      O_servo_drive_release_S1
  ENDI

  L      MG_spindle_2_active
  IFT
    L      ML_Sn_O_servo_drive_release
    =      O_servo_drive_release_S2
  ENDI

  L      ML_Sn_O_servo_drive_release
  PSL

EM

;----------------------------
LBL Coupling                           ;02

  PS     KG_spindle
  L      MG_gear_range_1_from_3D_head
  IFT
    PS     KL_spindle_Y_mode
  ELSE
    PS     NP_W256_G_code_spindle
  ENDI
  CM     9163
  M_display_module_error

  LN     MG_servo_drive_ready_S
  S      MG_Sn_O_YD_wye_delta_switchover

  L      NP_W256_G_code_spindle
  ==     K+0
  O      MG_gear_range_1_from_3D_head
  A      MG_Sn_I_gear_range_1
  AN     MG_Sn_I_gear_range_2
  O[
    L      NP_W256_G_code_spindle
    ==     K+1
    AN     MG_gear_range_1_from_3D_head
    AN     MG_Sn_I_gear_range_1
    A      MG_Sn_I_gear_range_2
  ]
  R      MG_Sn_O_YD_wye_delta_switchover
  PSL

EM

;----------------------------
LBL Gear_end                           ;04

  PS     MG_marker_zero

LBL Gear_reset

  L      MG_marker_one
  R      MG_gear_change_active
  R      MG_spindle_on_after_gear_change
  R      MG_gear_range_1_from_3D_head
  R      MG_gear_range_from_3D_head
  R      ML_Sn_O_servo_drive_release
  R      PN_M4010_S_rotation_right
  R      PN_M4009_S_rotation_left
  R      MG_YD_switch_rpm_dependend
  R      O_S1_wye_delta_switchover
  R      O_S1_gear_range_1
  R      O_S1_gear_range_2
  R      O_S2_wye_delta_switchover
  R      O_S2_gear_range_1
  R      O_S2_gear_range_2

  L      K+0
  =      BL_step_gear_YD

EM

;----------------------------
LBL Test_gear_YD_sensors

  L      MG_spindle_off
  AN     MG_gear_range_ok
  IFT
    L      MG_p_pulse_control_operational
    O      MG_pulse_CE_key
    IFT
      LN     MG_Sn_I_gear_range_1
      O      MG_Sn_I_gear_range_2
      S      MG_Sn_O_YD_wye_delta_switchover

      PS     KG_spindle
      PS     KL_spindle_Y_mode
      CM     9163                      ;Spindle Y/D switchover
      M_display_module_error
    ENDI

    L      MG_Sn_I_gear_range_1
    AN     MG_Sn_I_gear_range_2
    R      MG_Sn_O_YD_wye_delta_switchover

    L      MG_Sn_I_gear_range_1
    AN     MG_Sn_I_gear_range_2
    A      ML_spindle_Y_mode
    R      PN_error_gear_change
  ENDI

  L      ML_spindle_Y_mode
  O      ML_spindle_D_mode
  R      MG_gear_range_ok

  L      ML_spindle_Y_mode
  A      MG_Sn_I_gear_range_1
  AN     MG_Sn_I_gear_range_2
  S      MG_gear_range_ok

  L      ML_spindle_D_mode
  AN     MG_Sn_I_gear_range_1
  A      MG_Sn_I_gear_range_2
  S      MG_gear_range_ok

  LN     PN_M4007_S_M05_nominal_0_and_status
  ON     PN_M4012_S_open_control_loop
  AN     MG_gear_range_ok
  AN     MG_YD_switch_rpm_dependend
  AN     MG_gear_initialize
  O[
    AN     TS_gear_timeout
    AN     TR_gear_timeout
  ]
  A      MG_power_delayed_on
  S      PN_error_gear_change
  R      MG_gear_range_ok

EM

;----------------------------
LBL Gear_YD_initialize

  L      MG_spindle_with_gear
  IFT
    PS     KG_spindle
    PS     NP_W256_G_code_spindle
    CM     9163
        M_display_module_error
    PS     KL_S_YD_monitoring_inactive
    PS     DG_S1_rpm_YD_switch[BG_number_active_spindle]
    PS     DG_S1_rpm_DY_switch[BG_number_active_spindle]
    CM     9173                        ;Spindle Y/D switchover drehzahlabhängig
        M_display_module_error
  ELSE
    PS     KL_S_YD_fast_monitoring_active
    PS     DG_S1_rpm_YD_switch[BG_number_active_spindle]
    PS     DG_S1_rpm_DY_switch[BG_number_active_spindle]
    CM     9173                        ;Spindle Y/D switchover drehzahlabhängig
        M_display_module_error
  ENDI

EM
