/* * mppt_control.c * * Created on: 2024年6月29日 * Author: psx */ #include "mppt_control.h" #include "collect_Conversion.h" #include "pwm.h" #include "inflash.h" #include "gpio.h" SL_Mppt_para g_Mppt_Para = {0}; static void TrickleCharge(void); static void ConstantCurrentCharge(void); static void ConstantVoltageCharge(void); static void FloatingCharge(void); /* 占空比 */ float g_duty_ratio = 0.5; /* 用于确定工作模式 */ static uint8_t modeFlag = 0; /** * @brief 得到输出的功率 * @param * @retval OutputPower 输出功率 */ static float Get_OutputPower(void) { static float OutputPower; static float V_out, I_out; V_out = get_PV_VOLT_OUT(); I_out = get_CHG_CURR(); OutputPower = V_out * I_out; printf(" V = %d/100, I = %d/10000, OutputPower = %d/10000 \r\n", (int)(V_out*100), (int)(I_out * 10000), (int)(OutputPower * 10000)); return OutputPower; } /** * @brief 使用的为扰动干扰法,调整输出电压,使功率输出最大 * @param * @retval */ /* pwm占空比调节步长 */ const float step1_pwm = 0.01; const float step2_pwm = 0.005; //#define array_num 10 void mppt_readJust(void) { // static float last_duty_ratio = 0.5; // static float now_duty_ratio; // static float last_OutputPower; // static float now_OutputPower; // static float step_pwm = step1_pwm; // // last_OutputPower = Get_OutputPower(); // // printf(" duty_ratio = %d/1000 \r\n", (int)(last_duty_ratio * 1000)); // // /* 正向调节查看功率是否会变大 */ // now_duty_ratio = last_duty_ratio + step_pwm; // if (now_duty_ratio > 1) { // now_duty_ratio = 1; // } // Set_duty_ratio(now_duty_ratio); // now_OutputPower = Get_OutputPower(); // if (now_OutputPower > last_OutputPower) { // printf(" now_OutputPower > last_OutputPower1 \r\n"); // last_duty_ratio = now_duty_ratio; // return; // } // // /* 负向调节查看功率是否会变大 */ // now_duty_ratio = last_duty_ratio - step_pwm; // if (now_duty_ratio < 0) { // now_duty_ratio = 0; // } // Set_duty_ratio(now_duty_ratio); // now_OutputPower = Get_OutputPower(); // if (now_OutputPower > last_OutputPower) { // printf(" now_OutputPower > last_OutputPower2 \r\n"); // last_duty_ratio = now_duty_ratio; // return; // } // // /* 正负向调节功率均未变大,此时设置功率为原来的点 */ // Set_duty_ratio(last_duty_ratio); // step_pwm = step2_pwm; static float last_duty_ratio = 0.5; static float last_OutputPower; static float now_OutputPower; static float step_pwm = step1_pwm; last_OutputPower = Get_OutputPower(); printf(" duty_ratio = %d/1000 \r\n", (int)(last_duty_ratio * 1000)); /* 正向调节查看功率是否会变大 */ g_duty_ratio = last_duty_ratio + step_pwm; Set_duty_ratio(&g_duty_ratio); now_OutputPower = Get_OutputPower(); if (now_OutputPower > last_OutputPower) { printf(" now_OutputPower > last_OutputPower1 \r\n"); last_duty_ratio = g_duty_ratio; return; } /* 负向调节查看功率是否会变大 */ g_duty_ratio = last_duty_ratio - step_pwm; Set_duty_ratio(&g_duty_ratio); now_OutputPower = Get_OutputPower(); if (now_OutputPower > last_OutputPower) { printf(" now_OutputPower > last_OutputPower2 \r\n"); last_duty_ratio = g_duty_ratio; return; } /* 正负向调节功率均未变大,此时设置功率为原来的点 */ g_duty_ratio = last_duty_ratio; Set_duty_ratio(&g_duty_ratio); step_pwm = step2_pwm; } void printf_data(void) { printf("\n"); get_CHG_CURR(); get_PV_VOLT_OUT(); get_DSG_CURR(); get_PV1_VOLT_IN(); get_PV_VOLT_IN1(); get_MOSFET_Temper(); get_PV2_VOLT_IN(); printf("\n"); } /** * @brief 恒定输入电压 * @param * @retval * */ void mppt_constantVoltage(float InVoltage) { static uint8_t ConstantVoltageFlag = 1; float PV1_V = get_PV1_VOLT_IN(); if (ConstantVoltageFlag) { if (PV1_V > InVoltage) { g_duty_ratio += step1_pwm; Set_duty_ratio(&g_duty_ratio); } else { g_duty_ratio -= step1_pwm; Set_duty_ratio(&g_duty_ratio); } if (PV1_V - InVoltage < 0.1) { ConstantVoltageFlag = 0; } } else { if (PV1_V > InVoltage) { g_duty_ratio += step2_pwm; Set_duty_ratio(&g_duty_ratio); } else { g_duty_ratio -= step2_pwm; Set_duty_ratio(&g_duty_ratio); } if (PV1_V - InVoltage > 0.1) { ConstantVoltageFlag = 1; } } } /** * @brief 恒定输出电压 * @param * @retval * */ void mppt_constantVoltageO(float OutVoltage) { static uint8_t ConstantVoltageFlag = 1; float PV1_V = get_PV_VOLT_OUT(); if (ConstantVoltageFlag) { if (PV1_V > OutVoltage) { g_duty_ratio -= step1_pwm; Set_duty_ratio(&g_duty_ratio); } else { g_duty_ratio += step1_pwm; Set_duty_ratio(&g_duty_ratio); } if (PV1_V - OutVoltage < 0.1) { ConstantVoltageFlag = 0; } } else { if (PV1_V > OutVoltage) { g_duty_ratio -= step2_pwm; Set_duty_ratio(&g_duty_ratio); } else { g_duty_ratio += step2_pwm; Set_duty_ratio(&g_duty_ratio); } if (PV1_V - OutVoltage > 0.1) { ConstantVoltageFlag = 1; } } } /** * @brief 后端电池钳位,恒定输出电流 * @param * @retval * */ void mppt_constantCurrentO(float outCurrent) { static uint8_t ConstantCurrent = 1; float out_I = get_CHG_CURR(); if (ConstantCurrent) { if (out_I > outCurrent) { g_duty_ratio -= step1_pwm; Set_duty_ratio(&g_duty_ratio); } else { g_duty_ratio += step1_pwm; Set_duty_ratio(&g_duty_ratio); } if (out_I - outCurrent < 0.1) { ConstantCurrent = 0; } } else { if (out_I > outCurrent) { g_duty_ratio -= step2_pwm; Set_duty_ratio(&g_duty_ratio); } else { g_duty_ratio += step2_pwm; Set_duty_ratio(&g_duty_ratio); } if (out_I - outCurrent > 0.1) { ConstantCurrent = 1; } } } /** * @brief 涓流充电 * @param * @retval * */ void TrickleCharge(void) { mppt_constantCurrentO(0.7); } /** * @brief 恒流充电(大电流充电),mppt最大功率充电 * @param * @retval * */ void ConstantCurrentCharge(void) { mppt_readJust(); } /** * @brief 恒压充电 * @param * @retval * */ void ConstantVoltageCharge(void) { mppt_constantVoltage(17.5); } /** * @brief 浮充充电 * @param * @retval * */ void FloatingCharge(void) { static uint8_t run_num; if (get_CHG_CURR() > 0.1) { mppt_constantVoltageO(12); if (run_num++ > 100) { if ((get_PV_VOLT_OUT()) < 14) { run_num = 0; modeFlag = CONSTANTVOLTAGE; return; } } } else { TIM_SetCompare4(TIM4, 0); if (run_num++) { if ((get_PV_VOLT_OUT()) < 14) { run_num = 0; modeFlag = CONSTANTVOLTAGE; return; } } if (run_num > 100) { run_num = 0; modeFlag = CONSTANTVOLTAGE; return; } } if (run_num > 200) { run_num = 100; } } void MpptContorl(void) { switch(modeFlag) { case TRICKLE: TrickleCharge(); break; case CONSTANTCURRENT: ConstantCurrentCharge(); break; case CONSTANTVOLTAGE: ConstantVoltageCharge(); break; case FLOAT: FloatingCharge(); break; default: break; } } void MpptMode(void) { static uint8_t temp_flag = 1; static float ConstantCurrentV; static float ConstantVoltageV; static float FloatI; /* 赋值仅执行一次 */ if (temp_flag) { ConstantCurrentV = (float)g_slConfigInfo.ConstantCurrentV / 100; ConstantVoltageV = (float)g_slConfigInfo.ConstantVoltageV / 100; FloatI = (float)g_slConfigInfo.FloatI / 100; temp_flag = 0; } if ((ConstantCurrentV < g_Mppt_Para.Battery_Voltage) && (ConstantVoltageV > g_Mppt_Para.Battery_Voltage)) { modeFlag = CONSTANTCURRENT; return; } if (!(ConstantVoltageV > g_Mppt_Para.Battery_Voltage) && (FloatI < get_CHG_CURR())) { modeFlag = CONSTANTVOLTAGE; return; } if ((!(ConstantVoltageV > g_Mppt_Para.Battery_Voltage) && (FloatI > get_CHG_CURR())) || modeFlag == FLOAT) { modeFlag = FLOAT; return; } modeFlag = TRICKLE; return; } void findMiNDutyRatio(void) { static uint8_t num = 100; if (0.05 < get_CHG_CURR()) { num -= 1; TIM_SetCompare4(TIM4, num); } else { printf("min duty ratio : %d/200 \n", num); } } void test(void) { // mppt_readjust(); // Get_OutputPower(); // mppt_constantVoltage(18); // findMiNDutyRatio(); // MpptContorl(); // printf_data(); // void MpptContorl(); // mppt_constantVoltageO(12); // FloatingCharge(); mppt_readJust(); // mppt_constantCurrentO(0.7); // static uint32_t run_num = 0; // if (1000 < run_num++) { // FloatingCharge(); // run_num = 1200; // printf("in floatcharge \n"); // return; // } // mppt_readJust(); // mppt_constantCurrentO(1.2); }