/*
 * calc_tempers.c
 *
 *  Created on: 4 δεκ. 2020 γ.
 *      Author: star
 */

#include <adc_tools.h>
#include <calc_tempers.h>
#include <CAN_project.h>
#include <edrk_main.h>
#include <params_temper_p.h>

#include "CAN_Setup.h"
#include "IQmathLib.h"

int calc_max_temper_acdrive_bear(void);
int calc_max_temper_acdrive_winding(void);
int calc_max_temper_edrk_u(void);
int calc_max_temper_edrk_water(void);
int calc_max_temper_edrk_air(void);
int calc_min_temper_edrk_air(void);




////////////////////////////////////////////////////////////////////
int calc_max_temper_acdrive_bear(void)
{
  int i, max_t=0;

  for (i=0;i<edrk.temper_acdrive.bear.max_size;i++)
    if (edrk.temper_acdrive.winding.filter_real_int_temper[i]>max_t)
      max_t = edrk.temper_acdrive.winding.filter_real_int_temper[i];

  return max_t;

}
////////////////////////////////////////////////////////////////////

////////////////////////////////////////////////////////////////////
int calc_max_temper_acdrive_winding(void)
{
  int i, max_t=0;

  for (i=0;i<edrk.temper_acdrive.winding.max_size;i++)
    if (edrk.temper_acdrive.winding.filter_real_int_temper[i]>max_t)
      max_t = edrk.temper_acdrive.winding.filter_real_int_temper[i];

  return max_t;

}
////////////////////////////////////////////////////////////////////

////////////////////////////////////////////////////////////////////
int calc_max_temper_edrk_u(void)
{
  int i, max_t=0;

  for (i=0;i<7;i++)
    if (edrk.temper_edrk.real_int_temper_u[i]>max_t)
      max_t = edrk.temper_edrk.real_int_temper_u[i];

  return max_t;

}
////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////
int calc_max_temper_edrk_water(void)
{
  int i, max_t=0;

  for (i=0;i<2;i++)
    if (edrk.temper_edrk.real_int_temper_water[i]>max_t)
      max_t = edrk.temper_edrk.real_int_temper_water[i];

  return max_t;

}
////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////
int calc_max_temper_edrk_air(void)
{
  int i, max_t=0;

  for (i=0;i<4;i++)
    if (edrk.temper_edrk.real_int_temper_air[i]>max_t)
      max_t = edrk.temper_edrk.real_int_temper_air[i];

  return max_t;

}
//////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////
int calc_min_temper_edrk_air(void)
{
  int i, min_t=1000;

  for (i=0;i<4;i++)
    if (edrk.temper_edrk.real_int_temper_air[i]<min_t)
      min_t = edrk.temper_edrk.real_int_temper_air[i];

  return min_t;

}
//////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////

////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////

//  ACP[V]/61.9[Om]/2.0*10000


#define koef_Temper_ENGINE_filter 1600000
#define INDEX_ACDRIVE_WINDING_CAN_UNITES    24
#define INDEX_ACDRIVE_BEAR_CAN_UNITES       30



void calc_temper_acdrive(void)
{
 int i;
 _iq iqtain,iq_temp;

// Winding
    for (i=0;i<edrk.temper_acdrive.winding.max_size;i++)
    {
        edrk.temper_acdrive.winding.real_temper[i] = Unites[BKSSD_CAN_DEVICE][INDEX_ACDRIVE_WINDING_CAN_UNITES+i]/10.0;
        if (Unites[BKSSD_CAN_DEVICE][INDEX_ACDRIVE_WINDING_CAN_UNITES+i]==0)
        {
            edrk.temper_acdrive.winding.flag_init_filter_temp[i]    = 0;
            edrk.temper_acdrive.winding.filter_real_int_temper[i]   = 0;
            edrk.temper_acdrive.winding.real_int_temper[i]          = 0;
            edrk.temper_acdrive.winding.real_temper[i]              = 0;

            continue;
        }

        edrk.temper_acdrive.winding.real_int_temper[i] = edrk.temper_acdrive.winding.real_temper[i]*K_TEMPER_TO_SVU;

        iqtain = _IQ(edrk.temper_acdrive.winding.real_temper[i]/100.0);
        iq_temp = _IQ(edrk.temper_acdrive.winding.filter_real_temper[i]/100.0);

        if (edrk.temper_acdrive.winding.flag_init_filter_temp[i]==0)
        {
          iq_temp = iqtain;
          edrk.temper_acdrive.winding.flag_init_filter_temp[i]=1;
        }

    //  iq_temp_engine[i] = exp_regul_iq(koef_Temper_ENGINE_filter, iq_temp_engine[i], iqtain);

        iq_temp +=  _IQmpy( (iqtain-iq_temp),  koef_Temper_ENGINE_filter);
        edrk.temper_acdrive.winding.filter_real_temper[i] = _IQtoF(iq_temp)*100.0;
        edrk.temper_acdrive.winding.filter_real_int_temper[i] = edrk.temper_acdrive.winding.filter_real_temper[i]*K_TEMPER_TO_SVU;
    }

    edrk.temper_acdrive.winding.max_real_int_temper = calc_max_temper_acdrive_winding();


// Bearing
    for (i=0;i<edrk.temper_acdrive.bear.max_size;i++)
    {
        edrk.temper_acdrive.bear.real_temper[i] = Unites[BKSSD_CAN_DEVICE][INDEX_ACDRIVE_BEAR_CAN_UNITES+i]/10.0;
        if (Unites[BKSSD_CAN_DEVICE][INDEX_ACDRIVE_BEAR_CAN_UNITES+i]==0)
        {
            edrk.temper_acdrive.bear.flag_init_filter_temp[i]    = 0;
            edrk.temper_acdrive.bear.filter_real_int_temper[i]   = 0;
            edrk.temper_acdrive.bear.real_int_temper[i]          = 0;
            edrk.temper_acdrive.bear.real_temper[i]              = 0;

            continue;

        }

        edrk.temper_acdrive.bear.real_int_temper[i] = edrk.temper_acdrive.bear.real_temper[i]*K_TEMPER_TO_SVU;

        iqtain = _IQ(edrk.temper_acdrive.bear.real_temper[i]/100.0);
        iq_temp = _IQ(edrk.temper_acdrive.bear.filter_real_temper[i]/100.0);

        if (edrk.temper_acdrive.bear.flag_init_filter_temp[i]==0)
        {
          iq_temp = iqtain;
          edrk.temper_acdrive.bear.flag_init_filter_temp[i]=1;
        }

    //  iq_temp_engine[i] = exp_regul_iq(koef_Temper_ENGINE_filter, iq_temp_engine[i], iqtain);

        iq_temp +=  _IQmpy( (iqtain-iq_temp),  koef_Temper_ENGINE_filter);
        edrk.temper_acdrive.bear.filter_real_temper[i] = _IQtoF(iq_temp)*100.0;
        edrk.temper_acdrive.bear.filter_real_int_temper[i] = edrk.temper_acdrive.bear.filter_real_temper[i]*K_TEMPER_TO_SVU;




    }

    edrk.temper_acdrive.bear.max_real_int_temper = calc_max_temper_acdrive_bear();

//////
}


////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////

void calc_temper_edrk(void)
{
 int i;

////
    edrk.temper_edrk.adc_temper_u[0]  = ADC_f[1][1];
    edrk.temper_edrk.adc_temper_u[1]  = ADC_f[1][2];
    edrk.temper_edrk.adc_temper_u[2]  = ADC_f[1][3];
    edrk.temper_edrk.adc_temper_u[3]  = ADC_f[1][4];
    edrk.temper_edrk.adc_temper_u[4]  = ADC_f[1][5];
    edrk.temper_edrk.adc_temper_u[5]  = ADC_f[1][6];
    edrk.temper_edrk.adc_temper_u[6]  = ADC_f[1][7];

    edrk.temper_edrk.real_temper_u[0] = (_IQtoF(analog.T_U01)*100.0 - 4.0)/16.0 * NORMA_ACP_TEMPER - DELTA_ACP_TEMPER;
    edrk.temper_edrk.real_temper_u[1] = (_IQtoF(analog.T_U02)*100.0  - 4.0)/16.0 * NORMA_ACP_TEMPER - DELTA_ACP_TEMPER;
    edrk.temper_edrk.real_temper_u[2] = (_IQtoF(analog.T_U03)*100.0  - 4.0)/16.0 * NORMA_ACP_TEMPER - DELTA_ACP_TEMPER;
    edrk.temper_edrk.real_temper_u[3] = (_IQtoF(analog.T_U04)*100.0  - 4.0)/16.0 * NORMA_ACP_TEMPER - DELTA_ACP_TEMPER;
    edrk.temper_edrk.real_temper_u[4] = (_IQtoF(analog.T_U05)*100.0  - 4.0)/16.0 * NORMA_ACP_TEMPER - DELTA_ACP_TEMPER;
    edrk.temper_edrk.real_temper_u[5] = (_IQtoF(analog.T_U06)*100.0  - 4.0)/16.0 * NORMA_ACP_TEMPER - DELTA_ACP_TEMPER;
    edrk.temper_edrk.real_temper_u[6] = (_IQtoF(analog.T_U07)*100.0  - 4.0)/16.0 * NORMA_ACP_TEMPER - DELTA_ACP_TEMPER;


    for (i=0;i<7;i++)
      edrk.temper_edrk.real_int_temper_u[i] = edrk.temper_edrk.real_temper_u[i]*K_TEMPER_TO_SVU;

    edrk.temper_edrk.max_real_int_temper_u = calc_max_temper_edrk_u();
/////

    edrk.temper_edrk.adc_temper_water[0]  = ADC_f[1][8];
    edrk.temper_edrk.adc_temper_water[1]  = ADC_f[1][9];

    edrk.temper_edrk.real_temper_water[0] = (_IQtoF(analog.T_Water_internal)*100.0  - 4.0)/16.0 * NORMA_ACP_TEMPER - DELTA_ACP_TEMPER;
    edrk.temper_edrk.real_temper_water[1] = (_IQtoF(analog.T_Water_external)*100.0  - 4.0)/16.0 * NORMA_ACP_TEMPER - DELTA_ACP_TEMPER;

    for (i=0;i<2;i++)
      edrk.temper_edrk.real_int_temper_water[i] = edrk.temper_edrk.real_temper_water[i]*K_TEMPER_TO_SVU;

    edrk.temper_edrk.max_real_int_temper_water = calc_max_temper_edrk_water();

//////
    edrk.temper_edrk.adc_temper_air[0]  = ADC_f[1][10];
    edrk.temper_edrk.adc_temper_air[1]  = ADC_f[1][11];
    edrk.temper_edrk.adc_temper_air[2]  = ADC_f[1][12];
    edrk.temper_edrk.adc_temper_air[3]  = ADC_f[1][13];


    edrk.temper_edrk.real_temper_air[0] = (_IQtoF(analog.T_Air_01)*100.0  - 4.0)/16.0 * NORMA_ACP_TEMPER - DELTA_ACP_TEMPER;
    edrk.temper_edrk.real_temper_air[1] = (_IQtoF(analog.T_Air_02)*100.0  - 4.0)/16.0 * NORMA_ACP_TEMPER - DELTA_ACP_TEMPER;
    edrk.temper_edrk.real_temper_air[2] = (_IQtoF(analog.T_Air_03)*100.0  - 4.0)/16.0 * NORMA_ACP_TEMPER - DELTA_ACP_TEMPER;
    edrk.temper_edrk.real_temper_air[3] = (_IQtoF(analog.T_Air_04)*100.0  - 4.0)/16.0 * NORMA_ACP_TEMPER - DELTA_ACP_TEMPER;

    for (i=0;i<4;i++)
      edrk.temper_edrk.real_int_temper_air[i] = edrk.temper_edrk.real_temper_air[i]*K_TEMPER_TO_SVU;

    edrk.temper_edrk.max_real_int_temper_air = calc_max_temper_edrk_air();
    edrk.temper_edrk.min_real_int_temper_air = calc_min_temper_edrk_air();



}
//////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////



