/*
 * collect_Conversion.c
 *
 *  Created on: 2024年6月21日
 *      Author: psx
 */

#include "collect_Conversion.h"
#include "gpio.h"
#include "adc.h"
#include <math.h>

#define CHG_CURR        ADC_Channel_1
#define PV_VOLT_OUT     ADC_Channel_2
#define DSG_CURR        ADC_Channel_3
#define PV1_VOLT_IN     ADC_Channel_4
#define PV_VOLT_IN1     ADC_Channel_5
#define MOSFET_Temper   ADC_Channel_6
#define PV2_VOLT_IN     ADC_Channel_7

#define enable_Printf_VI

/* 光伏充电输出电流比例，放大倍数*电阻 */
const float P_CHG_CURR = (1.0 / (50 * 0.005));
/* 光伏充电输出电压比例，分压系数（放电时采集不准） */
const float P_PV_VOLT_OUT = (47 + 4.7) / 4.7;
/* 放电电流采集电流倍数 */
const float P_DSG_CURR = (1.0 / (50 * 0.005));
///* 光伏1开路输出电压比例 */
//const float P_PV1_VOLT_IN = (100 + 4.7) / 4.7;
/* 光伏1开路输出电压比例 */
const float P_PV1_VOLT_IN = (100 + 10) / 10;
/* 系统电源电压比例 */
const float P_PV_VOLT_IN1 = (47 + 4.7) / 4.7;
/* 温度采集比例 */
const float P_MOSFET_TEMPER = 0;
/* 光伏2开路输出电压比例 */
const float P_PV2_VOLT_IN = (100 + 4.7) / 4.7;

static uint16_t get_adc(uint8_t ADC_Channel);

/**
  * @brief  将采集的adc进行校准
  * @param  val 采集的adc数据
  * @retval     校准后的adc数据
  */
uint16_t Get_ConversionVal(int16_t val)
{
    if((val + Calibrattion_Val) < 0 || val == 0)
        return 0;
    if((Calibrattion_Val + val) > 4095 || val == 4095)
        return 4095;
    return (val + Calibrattion_Val);
}

/**
  * @brief  得到adc采集的数据
  * @param  ADC_Channel adc通道
  * @retval val         adc采集的数据
  */
uint16_t get_adc(uint8_t ADC_Channel)
{
    uint16_t val;
    ADC_RegularChannelConfig(ADC1, ADC_Channel, 1, ADC_SampleTime_CyclesMode5);
//    ADC_RegularChannelConfig(ADC1, ADC_Channel, 1, ADC_SampleTime_CyclesMode7);
    ADC_SoftwareStartConvCmd(ADC1, ENABLE);
    while(!ADC_GetFlagStatus(ADC1, ADC_FLAG_EOC));
    val = ADC_GetConversionValue(ADC1);
    return val;
}

/**
  * @brief  得到充电电流
  * @param
  * @retval I   电流值
  */
float get_CHG_CURR(void)
{
    float I;
    uint16_t I_ADC;

    I_ADC = Get_ConversionVal(get_adc(CHG_CURR));
//    I_ADC = get_adc(CHG_CURR);

    I = (float)(I_ADC) / 4095 * 3.3 * P_CHG_CURR;

#ifdef enable_Printf_VI
    printf("\n CHG_CURR ADC : %d \n", I_ADC);
    printf(" CHG_CURR I : %d /10000 \n", (int)(I * 10000));
#endif

    return I;
}

/**
  * @brief  得到光伏输出电压
  * @param
  * @retval V   电压值
  */
float get_PV_VOLT_OUT(void)
{
    float V;
    uint16_t V_ADC;

    V_ADC = Get_ConversionVal(get_adc(PV_VOLT_OUT));
//    V_ADC = get_adc(PV_VOLT_OUT);

    V = (float)(V_ADC) / 4095 * 3.3 * P_PV_VOLT_OUT;

#ifdef enable_Printf_VI
    printf("\n PV_VOLT_OUT ADC : %d \n", V_ADC);
    printf(" PV_VOLT_OUT V : %d /100 \n", (int)(V * 100));
#endif

    return V;
}

/**
  * @brief  得到放电电流
  * @param
  * @retval I   电流值
  */
float get_DSG_CURR(void)
{
    float I;
    uint16_t I_ADC;

    I_ADC = Get_ConversionVal(get_adc(DSG_CURR));
//    I_ADC = get_adc(DSG_CURR);

    I = (float)(I_ADC) / 4095 * 3.3 / P_DSG_CURR;

#ifdef enable_Printf_VI
    printf("\n DSG_CURR ADC : %d \n", I_ADC);
    printf(" DSG_CURR I : %d /100 \n", (int)(I * 100));
#endif

    return I;
}

/**
  * @brief  得到光伏1开路电压
  * @param
  * @retval V   电压值
  */
float get_PV1_VOLT_IN(void)
{
    float V;
    uint16_t V_ADC;

    GPIO_WriteBit(G_FFMOS_CON1_GPIO, G_FFMOS_CON1_PIN, SET);
    V_ADC = Get_ConversionVal(get_adc(PV1_VOLT_IN));
//    V_ADC = get_adc(PV1_VOLT_IN);
    GPIO_WriteBit(G_FFMOS_CON1_GPIO, G_FFMOS_CON1_PIN, RESET);

    V = (float)(V_ADC) / 4095 * 3.3 * P_PV1_VOLT_IN;

#ifdef enable_Printf_VI
    printf("\n PV1_VOLT_IN ADC : %d \n", V_ADC);
    printf(" PV1_VOLT_IN V1 : %d /100 \n", (int)(V * 100));
#endif

    return V;
}

/**
  * @brief  得到系统电压电压
  * @param
  * @retval V   电压值
  */
float get_PV_VOLT_IN1(void)
{
    float V;
    uint16_t V_ADC;

    V_ADC = Get_ConversionVal(get_adc(PV_VOLT_IN1));
//    V_ADC = get_adc(PV_VOLT_IN1);

    V = (float)(V_ADC) / 4095 * 3.3 * P_PV_VOLT_IN1;

#ifdef enable_Printf_VI
    printf("\n PV_VOLT_IN1 ADC : %d \n", V_ADC);
    printf(" PV_VOLT_IN1 V : %d /100 \n", (int)(V * 100));
#endif

    return V;
}


const float Rp=10000.0; //10K
const float T2 = (273.15+25.0);;//T2
//const float Bx = 3950.0;//B
const float Bx = 3435.0;//B
const float Ka = 273.15;
/**
  * @brief  得到温度
  * @param
  * @retval V   电压值
  */
float get_MOSFET_Temper(void)
{
    float T;
    uint16_t T_ADC;

    T_ADC = Get_ConversionVal(get_adc(MOSFET_Temper));
//    T_ADC = get_adc(MOSFET_Temper);

    float V = (float)(T_ADC) / 4095 * 3.3;
    float R = (float)(10 * V) / (3.3 - V);

    T = R / Rp;
    T = log(T);//ln(Rt/Rp)
    T /= Bx;//ln(Rt/Rp)/B
    T += (1.0 / T2);
    T = 1.0 / (T);
    T -= Ka;

//    T = (float)(T_ADC) / 4095 * 3.3 * P_MOSFET_TEMPER;

#ifdef enable_Printf_VI
    printf("\n MOSFET_Temper ADC : %d \n", T_ADC);
    printf(" MOSFET_Temper T : %d /100 \n", (int)(T * 100));
#endif

    return T;
}

/**
  * @brief  得到光伏2开路电压
  * @param
  * @retval V   电压值
  */
float get_PV2_VOLT_IN(void)
{
    float V;
    uint16_t V_ADC;

    GPIO_WriteBit(G_FFMOS_CON2_GPIO, G_FFMOS_CON2_PIN, SET);
    V_ADC = Get_ConversionVal(get_adc(PV2_VOLT_IN));
//    V_ADC = get_adc(PV2_VOLT_IN);
    GPIO_WriteBit(G_FFMOS_CON2_GPIO, G_FFMOS_CON2_PIN, RESET);

    V = (float)(V_ADC) / 4095 * 3.3 * P_PV2_VOLT_IN;

#ifdef enable_Printf_VI
    printf("\n PV2_VOLT_IN ADC : %d \n", V_ADC);
    printf(" PV2_VOLT_IN V : %d /100 \n", (int)(V * 100));
#endif

    return V;
}




