*-----------------------------------------------------------------------------
*  Turning - Free Rotation
*-----------------------------------------------------------------------------
*  Module Description
*
* - Soft keys free rotation cw, free rotation ccw, assignment S-override
* - M23 free rotation cw
* - M24 free rotation ccw
* - M25 free rotation off
* - FN19 - feed turn axis
*     Identifier (D280): K+99994
*     Date       (D284): Vorschub [°/min]
*
*-----------------------------------------------------------------------------

#define /s    KL_override_0                            K +0
#define /s    KL_factor_override_resolution            K +10000
#define /s    KL_turning_override_min                  K +4000

#define /s    KL_direction_positive                    K +1
#define /s    KL_direction_negative                    K -1
#define /s    KL_feed_0                                K +0
#define /s    KL_stop                                  K +0
#define /s    KL_active                                K +1
#define /s    KL_mode_free_rotation                    K +1

#define /s    KL_FR_nsoll_difference                   K +25 ;[100/min]
#define /s    KL_FR_n_nominal_percent                  K +80 ;[%]

#define /s    ML_free_rotation_special_function        M
#define /s    ML_free_rotation_was_cw                  M
#define /s    ML_free_rotation_was_ccw                 M
#define /s    ML_free_rotation_on                      M
#define /s    ML_free_rotation_direction_change        M

#define /s    BL_stack_synchronize                     B

#define /s    WL_override_free_rotation                W

#define /s    DL_motor_rpm_actl_turn_axis              D
#define /s    DL_actual_feed_turn_axis                 D
#define /s    DL_nomlinal_feed_turn_axis               D

#define /s    DL_turn_axis_feed                        D

#include Mac_Lib.inc

GLOBAL Turning

;----------------------------
LBL Turning
;----------------------------

  L      DG_turning_axis_feed
  ==     K+0
  A      MG_machine_parameter_are_read
  IFT
    L      DG_feed_PLC_X[KG_turning_rotary_axis_index]
    >      DG_MP1010_rapid_feed_turning_axis
    S      PN_error_value_not_permissible
    IFT
      L      DG_MP1010_rapid_feed_turning_axis
      =      DG_turning_axis_feed
    ELSE
      L      DG_feed_PLC_X[KG_turning_rotary_axis_index]
      =      DG_turning_axis_feed
    ENDI
  ENDI

  LN     MG_control_in_operation_on
  =      MG_SK_enable_free_rotation

  PS     KG_turning_rotary_axis_index
  PS     KL_mode_free_rotation
  CM     9048                          ;Interrogate the operating states of axes
  PLW
  ==     KL_active
  =      MG_free_rotation_active
  =      MG_SK_enable_S_override_turn

  L      MG_M23_turn_cw_on
  A      MG_SK_free_rotation_cw
  O[
    L      MG_M24_turn_ccw_on
    A      MG_SK_free_rotation_ccw
  ]
  A      MG_free_rotation_active
  AN     PN_M4133_strobe_free_rotation
  S      PN_M4092_quit_M_function

  L      MG_M25_turn_off
  AN     MG_free_rotation_active
  AN     PN_M4133_strobe_free_rotation
  S      PN_M4092_quit_M_function

  L      MG_SK_free_rotation_cw
  AN     ML_free_rotation_was_cw
  O      MG_M23_turn_cw_on
  S      MG_SK_free_rotation_cw
  R      MG_SK_free_rotation_ccw

  L      MG_SK_free_rotation_ccw
  AN     ML_free_rotation_was_ccw
  O      MG_M24_turn_ccw_on
  S      MG_SK_free_rotation_ccw
  R      MG_SK_free_rotation_cw

  L      MG_M25_turn_off
  ON     MG_control_operational
  R      MG_SK_free_rotation_cw
  R      MG_SK_free_rotation_ccw

;Free rotation ein, change of rpm

  L      MG_SK_free_rotation_cw
  AN     ML_free_rotation_was_cw
  S      ML_free_rotation_on

  L      MG_SK_free_rotation_ccw
  AN     ML_free_rotation_was_ccw
  S      ML_free_rotation_on

  L      MG_SK_free_rotation_cw
  O      MG_SK_free_rotation_ccw
  AN     MG_free_rotation_active
  S      ML_free_rotation_on

  L      DG_turning_axis_feed
  <>     DL_turn_axis_feed
  A      MG_free_rotation_active
  O      ML_free_rotation_on
  AN[
    L      DG_keys_inhibit
    BT     KG_turning_rotary_axis_index
  ]
  IFT
    R      ML_free_rotation_on
    S      MG_SK_S_override_turn_assigned

    PS     KG_turning_rotary_axis_index
    PS     DG_turning_axis_feed

    L      MG_SK_free_rotation_cw
    IFT
      PS     KL_direction_positive
    ELSE
      PS     KL_direction_negative
    ENDI

    CM     9223                        ;Free rotation activate/deactivate
    PL     BL_stack_synchronize
     M_display_module_error
  ENDI

  L      MG_SK_free_rotation_cw
  A      ML_free_rotation_was_ccw
  O[
    L      MG_SK_free_rotation_ccw
    A      ML_free_rotation_was_cw
  ]
  S      ML_free_rotation_direction_change

  L      MG_SK_free_rotation_cw
  =      ML_free_rotation_was_cw

  L      MG_SK_free_rotation_ccw
  =      ML_free_rotation_was_ccw

  L      DG_turning_axis_feed
  =      DL_turn_axis_feed

;Free rotation off

  LN     MG_SK_free_rotation_cw
  AN     MG_SK_free_rotation_ccw
  O      MG_pulse_END_PGM_M02_M30
  O      MG_pulse_internal_stop
  O      MG_auto_manu_mode_change
  A      MG_free_rotation_active
  IFT
    R      MG_SK_S_override_turn_assigned
    R      MG_SK_free_rotation_ccw
    R      MG_SK_free_rotation_cw

    PS     KG_turning_rotary_axis_index
    PS     KL_feed_0
    PS     KL_stop
    CM     9223                        ;Free rotation activate/deactivate
    PL     BL_stack_synchronize
     M_display_module_error
  ENDI

;Quit FN19 if free rotation active (strobe rpm change NC axis)

  L      NP_D280_FN19_1st_value
  /      K+10000
  ==     K+99994
  AN     PN_M4133_strobe_free_rotation
  A      NP_M4075_strobe_transfer_FN19
  S      PN_M4095_quit_transfer_FN19

;Spindle override for free rotation

  L      MG_SK_S_override_turn_assigned
  O      NP_M4172_1st_PLC_run_after_power_on
  IFT
    L      NP_W492_S_override_NC
    =      WL_override_free_rotation

    L      WL_override_free_rotation
    <=     KL_turning_override_min
    IFT
      L      KL_turning_override_min
      =      WL_override_free_rotation
    ENDI
  ENDI

  L      NP_M4175_control_in_operation_flashes
  IFT
    L      KL_override_0
    =      PN_W754_override_free_rotation
  ELSE
    L      WL_override_free_rotation
    =      PN_W754_override_free_rotation
  ENDI

;Feed turning axis

  PS     KG_turning_rotary_axis_index
  CM     9164                          ;Actual rpm of motor reading
  PL     DL_motor_rpm_actl_turn_axis

  M_display_module_error

  L      DL_motor_rpm_actl_turn_axis
  /      K+100
  X[
    L      DG_MP1054_distance_per_revolution_turning_axis
    /      K+100
  ]
  /      K+1000
  =      DL_actual_feed_turn_axis

;Feed linear axis F0 up to rpm (feed) of the turning axis is reached

  L      DG_turning_axis_feed
  X      PN_W754_override_free_rotation
  /      KL_factor_override_resolution
  =      DL_nomlinal_feed_turn_axis

  L      DL_nomlinal_feed_turn_axis
  <=     K+2000
  IFT
    L      DL_nomlinal_feed_turn_axis
    -      DL_actual_feed_turn_axis
    <      KL_FR_nsoll_difference
    =      MG_free_rotation_nominal_rpm_reached
  ELSE
    L      DL_actual_feed_turn_axis
    X      K+100
    /      DL_nomlinal_feed_turn_axis
    >      KL_FR_n_nominal_percent
    =      MG_free_rotation_nominal_rpm_reached
  ENDI

  LN     MG_free_rotation_nominal_rpm_reached
  R      ML_free_rotation_direction_change

  L      ML_free_rotation_direction_change
  R      MG_free_rotation_nominal_rpm_reached

EM
