*-----------------------------------------------------------------------------
*  Temperature compensation
*-----------------------------------------------------------------------------
*  Module Description
*    - axis Z
*    - temperature in 1/10 degree
*-----------------------------------------------------------------------------

#define /s    KL_temperature_difference_max            K +500
#define /s    KL_temperature_ranges                    K +100
#define /s    KL_number_of_mesuring                    K +10
#define /s    KL_MP_format                             K +10000
#define /s    KL_Pt100_input_1                         K +67
#define /s    KL_Pt100_input_2                         K +68
#define /s    KL_max_T_comp_range                      K +30000
#define /s    KL_fitting_HSCI_resolution               K +10

#define /s    ML_calculate_start_value                 M
#define /s    ML_test_has_been_active                  M

#define /s    BL_average                               B

#define /s    WL_sum_temperature_difference            W
#define /s    WL_start_value_former_range              W

#define /s /c start_value                              W[5]
#define /s /c WL_start_value_10_degree_range           W &start_value + 0
#define /s /c WL_start_value_20_degree_range           W &start_value + 2
#define /s /c WL_start_value_30_degree_range           W &start_value + 4
#define /s /c WL_start_value_40_degree_range           W &start_value + 6
#define /s /c WL_start_value_50_degree_range           W &start_value + 8

#include Mac_Lib.inc

GLOBAL Temperature_comp

;----------------------------
LBL Temperature_comp
;----------------------------

  LN     MG_machine_parameter_are_read
  S      ML_calculate_start_value

  LN     MG_pulse_1_s
  ON     MG_machine_parameter_are_read
  O      PN_error_temperature_compensation
  EMT

  L      ML_calculate_start_value
  JPT    Calc_start_value

  L      MG_T_compensation_test
  IFT
    L      MG_T_compensation_test
    AN     ML_test_has_been_active
    IFT
      L      K+200
      =      WG_temperature_spindle_Pt100
      =      WG_temperature_machine_Pt100
    ENDI

  ELSE
#if CFG_HSCI = "active"
    L      IW_temperature_machine_Pt100
    /      KL_fitting_HSCI_resolution
    =      WG_temperature_machine_Pt100

    L      IW_temperature_spindle_Pt100
    /      KL_fitting_HSCI_resolution
    =      WG_temperature_spindle_Pt100
#else
    PS     KL_Pt100_input_1
    CM     9003
    PL     WG_temperature_machine_Pt100
   M_display_module_error

    PS     KL_Pt100_input_2
    CM     9003
    PL     WG_temperature_spindle_Pt100
  M_display_module_error
#endif
  ENDI

  L      MG_T_compensation_test
  =      ML_test_has_been_active

  L      DG_S1_max_temperature[BG_number_active_spindle]
  <=     WG_temperature_spindle_Pt100
  S      PN_error_spindle_T_max

  L      WG_temperature_spindle_Pt100
  -      WG_temperature_machine_Pt100
  =      WG_temperature_difference

  L      WG_temperature_difference
  +      WL_sum_temperature_difference
  =      WL_sum_temperature_difference

  INC    BL_average

  L      BL_average
  <      KL_number_of_mesuring
  EMT

  L      K+0
  =      BL_average

  L      WL_sum_temperature_difference
  /      KL_number_of_mesuring
  >      KL_temperature_difference_max
  S      PN_error_temperature_compensation
  S      MG_win_temperature_sensor
  JPT    Temp_comp_reset

  L      WL_sum_temperature_difference
  <      K+0
  JPT    Temp_comp_reset

  L      WL_sum_temperature_difference
  /      KL_number_of_mesuring
  /      KL_temperature_ranges
  =X

  LX
  >      K+4
  S      PN_error_temperature_compensation
  S      MG_win_temperature_sensor
  JPT    Temp_comp_reset

  L      WL_sum_temperature_difference
  MOD[
    L      KL_temperature_ranges
    X      KL_number_of_mesuring
  ]
  X      DG_factor_10_deg_range[X]
  /      KL_number_of_mesuring
  /      KL_MP_format
  +      WL_start_value_10_degree_range[X]
  X      K-1
  =      WG_temp_compensation_Z

  L      K+0
  =      WL_sum_temperature_difference

EM

LBL Temp_comp_reset

  L      K+0
  =      WL_sum_temperature_difference
  =      WG_temp_compensation_Z

EM

;----------------------------
LBL Calc_start_value

  L      K+0
  =X
  =      WL_start_value_10_degree_range

  REPEAT
    L      WL_start_value_10_degree_range[X]
    =      WL_start_value_former_range

    PS     DG_factor_10_deg_range[X]
    INCX
    PLW
    X      KL_temperature_ranges
    /      KL_MP_format
    +      WL_start_value_former_range
    =      WL_start_value_10_degree_range[X]
    >      KL_max_T_comp_range
    S      PN_error_temperature_compensation

    LX
    >=     K+4
  UNTILT

  LN     PN_error_temperature_compensation
  R      ML_calculate_start_value

EM
