*-----------------------------------------------------------------------------
*  IPC 6110 / IPC 6120
*-----------------------------------------------------------------------------
*  Module Description
*
*    - Four soft keys have been added to integrate the IPC.
*         - Soft_key_IPC_active
*         - Soft_key_override_S_IPC
*         - Soft_key_override_FMAX_IPC
*         - Soft_key_override_F_IPC
*      In addition to <Soft_key_IPC_active> the IPC features a key which
*      also serves to switch the IPC on and off.
*    - On TNC side the IPC can be deactivated by key <Home>.
*    - The overrides are connected to the analog inputs 1/2 X48 the MC 42x
*      via a separate cable. An additional averaging function with
*      hysteresis evaluation ensures a continuous characteristic curve.
*    - When the IPC is active, the switchover to IPC overrides takes
*      place by pressing the corresponding soft key or by changing the
*      IPC overrides by more than 10 % (without HSCI).
*    - On HSCI facilities with MB6xxx exists only one soft key to shift
*      all IPC overrides. This has to be pressed for activation or
*      deactivation in general.
*    - The operation on the TNC side is accordingly after the IP was
*      deactivated.
*    - Settings IPC TNCterminal
*        User: administrator (case sensitive)
*        password: 807667
*        IP address: IP Address of the TNC
*        TNCterminal - ControlPanel\Settings\Edit\PLC Interaction [address]
*          Connect Marker             -> 'IPC_61x0.src'\ML_IPC_61x0_connect
*          Disconnect Marker          -> 'IPC_61x0.src'\ML_IPC_61x0_disconnect
*          Disable Connect Key Marker -> 'IPC_61x0.src'\ML_IPC_61x0_disable
*          Connect Status Marker      -> 'IPC_61x0.src'\ML_IPC_61x0_connected
*          Heart Beat Marker          -> 'IPC_61x0.src'\ML_IPC_61x0_clock_monitoring
*
*-----------------------------------------------------------------------------

#define /s    KL_factor_voltage_range                  K +30
#define /s    KL_hysteresis                            K +1
#define /s    KL_treshold_0_percent                    K +10
#define /s    KL_treshold_150_percent                  K +490
#define /s    KL_voltage_value_0_percent               K +0
#define /s    KL_voltage_value_150_percent             K +500
#define /s    KL_10_percent_override                   K +1000
#define /s    KL_analog_input_1_MC42x                  K +64
#define /s    KL_analog_input_2_MC42x                  K +65
#define /s    KL_time_clock_monitoring_s               K +3
#define /s    KL_MB6xxx_iTNC530                        K +0
#define /s    KL_MB6xxx_IPC61x0                        K +1
#define /s    KL_reserved                              K +0

#define /s    ML_IPC_61x0_connect                      M
#define /s    ML_IPC_61x0_disconnect                   M
#define /s    ML_IPC_61x0_disable                      M
#define /s    ML_IPC_61x0_connected                    M
#define /s    ML_IPC_61x0_clock_monitoring             M

#define /s    ML_SK_S_has_been_active                  M
#define /s    ML_SK_FMAX_has_been_active               M
#define /s    ML_SK_F_has_been_active                  M
#define /s    ML_SK_override_S_F_IPC_was_active        M
#define /s    ML_IPC_61x0_has_been_connected           M
#define /s    ML_IPC_61x0_clock_was_active             M

#define /s    BL_number_summation                      B
#define /s    BL_clock_monitoring                      B

#define /s    WL_factor_spindle_override_IPC           W
#define /s    WL_factor_feed_override_IPC              W
#define /s    WL_factor_rapid_traverse_override_IPC    W
#define /s    WL_factor_spindle_has_been_active        W
#define /s    WL_factor_feed_has_been_active           W
#define /s    WL_factor_rapid_trav_was_active          W
#define /s    WL_DC_analog_input_1_MC42x               W
#define /s    WL_DC_analog_input_2_MC42x               W
#define /s    WL_DC_analog_input_1_before              W
#define /s    WL_DC_analog_input_2_before              W

#define /s    DL_DC_analog_input_1_MC42x               D
#define /s    DL_DC_analog_input_2_MC42x               D

#include Mac_Lib.inc

#if CFG_HSCI = "inactive"
EXTERN Absolute
#endif

GLOBAL IPC_61x0

;----------------------------
LBL IPC_61x0
;----------------------------

;Initialization when the IPC is activated

  L      ML_IPC_61x0_connected
  AN     ML_IPC_61x0_has_been_connected
  S      MG_SK_IPC_active
#if CFG_HSCI = "inactive"
  IFT
    L      WL_factor_spindle_override_IPC
    =      WL_factor_spindle_has_been_active

    L      WL_factor_feed_override_IPC
    =      WL_factor_feed_has_been_active

    L      WL_factor_rapid_traverse_override_IPC
    =      WL_factor_rapid_trav_was_active
  ENDI
#endif

;Initialization when the IPC is deactivated

  LN     ML_IPC_61x0_connected
  A      ML_IPC_61x0_has_been_connected
  R      MG_SK_IPC_active
#if CFG_HSCI = "inactive"
  IFT

#if CFG_override_rapid_traverse_instead_spindle  = "active"
    L      NP_W496_rapid_traverse_override_NC
    =      WL_factor_rapid_trav_was_active
#else
    L      NP_W492_S_override_NC
    =      WL_factor_spindle_has_been_active
#endif
    L      NP_W494_factor_feed_override_NC
    =      WL_factor_feed_has_been_active
  ENDI
#endif

  L      ML_IPC_61x0_connected
  =      ML_IPC_61x0_has_been_connected

;PLC interface, marker to (de)activate the IPC

  L      ML_IPC_61x0_clock_monitoring
  AN     ML_IPC_61x0_clock_was_active
  ON     ML_IPC_61x0_connected
  IFT
    L      KL_time_clock_monitoring_s
    =      BL_clock_monitoring
  ENDI

  L      ML_IPC_61x0_clock_monitoring
  =      ML_IPC_61x0_clock_was_active

  L      MG_pulse_1_s
  IFT
    DEC    BL_clock_monitoring
  ENDI

  L      BL_clock_monitoring
  <      K+0
  A      ML_IPC_61x0_connected
  S      PN_errorIPC_61x0_clock_monitoring
  S      ML_IPC_61x0_disconnect

  L      NP_W274_code_pushed_key
  ==     KG_key_home
  A      ML_IPC_61x0_connected
  S      ML_IPC_61x0_disconnect

  LN     MG_SK_IPC_active
  A      ML_IPC_61x0_connected
  S      ML_IPC_61x0_disconnect

  LN     ML_IPC_61x0_connected
  R      ML_IPC_61x0_disconnect

  L      MG_SK_IPC_active
  AN     ML_IPC_61x0_connected
  S      ML_IPC_61x0_connect

  L      ML_IPC_61x0_connected
  R      ML_IPC_61x0_connect

#if CFG_HSCI = "active"

  LN     MG_SK_override_S_F_IPC
  A      ML_SK_override_S_F_IPC_was_active
  IFT
    PS     KL_MB6xxx_iTNC530
    PS     KL_reserved
    PS     KL_reserved
    CM     9132
    M_display_module_error
  ENDI

  L      MG_SK_override_S_F_IPC
  AN     ML_SK_override_S_F_IPC_was_active
  IFT
    PS     KL_MB6xxx_IPC61x0
    PS     KL_reserved
    PS     KL_reserved
    CM     9132
    M_display_module_error
  ENDI

  L      MG_SK_override_S_F_IPC
  =      ML_SK_override_S_F_IPC_was_active

EM

#else
;AD conversion of the overrides

  L      ML_IPC_61x0_connected
  O      MG_SK_override_S_IPC
  O      MG_SK_override_FMAX_IPC
  O      MG_SK_override_F_IPC
  CMT    AD_Overrides

;Overrides, assignment to the values of IPC potentiometer, detection override turned

  L      ML_IPC_61x0_connected
  IFT
#if CFG_override_rapid_traverse_instead_spindle  = "active"
    L      MG_SK_override_FMAX_IPC
    IFT
      L      WL_factor_rapid_traverse_override_IPC
      =      NP_W496_rapid_traverse_override_NC
      =      NP_W752_rapid_traverse_override_PLC
      =      WL_factor_rapid_trav_was_active
    ELSE
      L      WL_factor_rapid_traverse_override_IPC
      -      WL_factor_rapid_trav_was_active
      PSW
      CM     Absolute
      PLW
      >=     KL_10_percent_override
      S      MG_SK_override_FMAX_IPC
    ENDI
#else
    L      MG_SK_override_S_IPC
    IFT
      L      WL_factor_spindle_override_IPC
      =      NP_W492_S_override_NC
      =      PN_W764_S_override_PLC
      =      WL_factor_spindle_has_been_active
    ELSE
      L      WL_factor_spindle_override_IPC
      -      WL_factor_spindle_has_been_active
      PSW
      CM     Absolute
      PLW
      >=     KL_10_percent_override
      S      MG_SK_override_S_IPC
    ENDI
#endif

    L      MG_SK_override_F_IPC
    IFT
      L      WL_factor_feed_override_IPC
      =      NP_W494_factor_feed_override_NC
      =      PN_W766_factor_feed_override_PLC
      =      WL_factor_feed_has_been_active
    ELSE
      L      WL_factor_feed_override_IPC
      -      WL_factor_feed_has_been_active
      PSW
      CM     Absolute
      PLW
      >=     KL_10_percent_override
      S      MG_SK_override_F_IPC
    ENDI
  ELSE

#if CFG_override_rapid_traverse_instead_spindle  = "active"
    L      MG_SK_override_FMAX_IPC
    AN     ML_SK_FMAX_has_been_active
    IFT
      L      NP_W496_rapid_traverse_override_NC
      =      WL_factor_rapid_trav_was_active
    ENDI

    L      MG_SK_override_FMAX_IPC
    IFT
      L      NP_W492_S_override_NC
      -      WL_factor_rapid_trav_was_active
      PSW
      CM     Absolute
      PLW
      >=     KL_10_percent_override
      R      MG_SK_override_FMAX_IPC

      L      WL_factor_rapid_traverse_override_IPC
      =      NP_W492_S_override_NC
      =      NP_W752_rapid_traverse_override_PLC
    ENDI
#else
    L      MG_SK_override_S_IPC
    AN     ML_SK_S_has_been_active
    IFT
      L      NP_W492_S_override_NC
      =      WL_factor_spindle_has_been_active
    ENDI

    L      MG_SK_override_S_IPC
    IFT
      L      NP_W492_S_override_NC
      -      WL_factor_spindle_has_been_active
      PSW
      CM     Absolute
      PLW
      >=     KL_10_percent_override
      R      MG_SK_override_S_IPC

      L      WL_factor_spindle_override_IPC
      =      NP_W492_S_override_NC
      =      PN_W764_S_override_PLC
    ENDI
#endif

    L      MG_SK_override_F_IPC
    AN     ML_SK_F_has_been_active
    IFT
      L      NP_W494_factor_feed_override_NC
      =      WL_factor_feed_has_been_active
    ENDI

    L      MG_SK_override_F_IPC
    IFT
      L      NP_W494_factor_feed_override_NC
      -      WL_factor_feed_has_been_active
      PSW
      CM     Absolute
      PLW
      >=     KL_10_percent_override
      R      MG_SK_override_F_IPC

      L      WL_factor_feed_override_IPC
      =      NP_W494_factor_feed_override_NC
      =      PN_W766_factor_feed_override_PLC
    ENDI
  ENDI

#if CFG_override_rapid_traverse_instead_spindle  = "active"
  L      MG_SK_override_FMAX_IPC
  =      ML_SK_FMAX_has_been_active
#else
  L      MG_SK_override_S_IPC
  =      ML_SK_S_has_been_active
#endif

  L      MG_SK_override_F_IPC
  =      ML_SK_F_has_been_active

EM

;----------------------------
LBL AD_Overrides

;Read analog voltages from S/F potentiometer (0..5 V), resolution 0.01 V (0..500)

  PS     KL_analog_input_1_MC42x
  CM     9003
  PL     WL_DC_analog_input_1_MC42x
  M_display_module_error


  PS     KL_analog_input_2_MC42x
  CM     9003
  PL     WL_DC_analog_input_2_MC42x

  M_display_module_error

;Calibrate digital voltage values to min (0 %) and max (150 %)

  L      WL_DC_analog_input_1_MC42x
  <=     KL_treshold_0_percent
  IFT
    L      KL_voltage_value_0_percent
    =      WL_DC_analog_input_1_MC42x
  ENDI

  L      WL_DC_analog_input_2_MC42x
  <=     KL_treshold_0_percent
  IFT
    L      KL_voltage_value_0_percent
    =      WL_DC_analog_input_2_MC42x
  ENDI

  L      WL_DC_analog_input_1_MC42x
  >=     KL_treshold_150_percent
  IFT
    L      KL_voltage_value_150_percent
    =      WL_DC_analog_input_1_MC42x
  ENDI

  L      WL_DC_analog_input_2_MC42x
  >=     KL_treshold_150_percent
  IFT
    L      KL_voltage_value_150_percent
    =      WL_DC_analog_input_2_MC42x
  ENDI

;Calculate mean value

  L      WL_DC_analog_input_1_MC42x
  +      DL_DC_analog_input_1_MC42x
  =      DL_DC_analog_input_1_MC42x

  L      WL_DC_analog_input_2_MC42x
  +      DL_DC_analog_input_2_MC42x
  =      DL_DC_analog_input_2_MC42x

  INC    BL_number_summation

  L      MG_pulse_250_ms
  IFT
    L      DL_DC_analog_input_1_MC42x
    /      BL_number_summation
    =      WL_DC_analog_input_1_MC42x

    L      DL_DC_analog_input_2_MC42x
    /      BL_number_summation
    =      WL_DC_analog_input_2_MC42x

    L      K+0
    =      BL_number_summation
    =      DL_DC_analog_input_1_MC42x
    =      DL_DC_analog_input_2_MC42x

;Hystersis activate

    L      WL_DC_analog_input_1_MC42x
    -      WL_DC_analog_input_1_before
    PSW
    CM     Absolute
    PLW
    <=     KL_hysteresis
    IFT
      L      WL_DC_analog_input_1_before
      =      WL_DC_analog_input_1_MC42x
    ENDI

    L      WL_DC_analog_input_2_MC42x
    -      WL_DC_analog_input_2_before
    PSW
    CM     Absolute
    PLW
    <=     KL_hysteresis
    IFT
      L      WL_DC_analog_input_2_before
      =      WL_DC_analog_input_2_MC42x
    ENDI

    L      WL_DC_analog_input_1_MC42x
    =      WL_DC_analog_input_1_before

    L      WL_DC_analog_input_2_MC42x
    =      WL_DC_analog_input_2_before
  ENDI

;Scaling digital DC value to override range

  L      WL_DC_analog_input_1_MC42x
  X      KL_factor_voltage_range
  =      WL_factor_spindle_override_IPC

  L      WL_DC_analog_input_2_MC42x
  X      KL_factor_voltage_range
  =      WL_factor_feed_override_IPC

EM
#endif
