chargeController/APP/businessLogic/Src/task.c

338 lines
9.9 KiB
C
Raw Normal View History

#include "task.h"
#include "inFlash.h"
#include "parameter.h"
#include "FM_GPIO.h"
#include "chargControlEnum.h"
#include "bl_chargControl.h"
2024-12-10 14:25:55 +00:00
/* 控制运行指示灯和喂狗 */
// #define runled_reloadVal 1000 /* 任务执行间隔 */
static uint16_t runled_reloadVal = 1000; /* 任务执行间隔 */
#define runled_offset 0 /* 任务执行偏移量 */
static STR_TimeSliceOffset m_runled;
static void Task_Runled(void);
/* 喂狗 */
#define wdi_reloadVal 1000 /* 任务执行间隔 */
2024-12-11 09:51:48 +00:00
#define wdi_offset 0 /* 任务执行偏移量 */
2024-12-10 14:25:55 +00:00
static STR_TimeSliceOffset m_wdi;
static void Task_wdi(void);
/* 刷新寄存器中的数据 */
#define refreshJudgeData_reloadVal 1000 /* 任务执行间隔 */
#define refreshJudgeData_offset 0 /* 任务执行偏移量 */
static STR_TimeSliceOffset m_refreshJudgeData;
static void Task_refreshJudgeData(void);
/* 启动任务 */
#define startControl_reloadVal 5000 /* 任务执行间隔 */
#define startControl_offset 100 /* 任务执行偏移量 */
static STR_TimeSliceOffset m_startControl;
static void Task_startControl(void);
/* 软启动 */
#define softStart_reloadVal 1 /* 任务执行间隔 */
#define softStart_offset 0 /* 任务执行偏移量 */
static STR_TimeSliceOffset m_softStart;
static void Task_softStart(void);
/* 回路阻抗检测 */
2024-12-11 09:51:48 +00:00
#define impedanceCalculation_reloadVal 20 /* 任务执行间隔 */
2024-12-10 14:25:55 +00:00
#define impedanceCalculation_offset 0 /* 任务执行偏移量 */
static STR_TimeSliceOffset m_impedanceCalculation;
static void Task_impedanceCalculation(void);
2024-12-11 14:11:05 +00:00
/* 开路电压采集 */
#define collectOpenCircuitVoltage_reloadVal 1000 /* 任务执行间隔 */
#define collectOpenCircuitVoltage_offset 0 /* 任务执行偏移量 */
STR_TimeSliceOffset g_collectOpenCircuitVoltage;
void Task_collectOpenCircuitVoltage(void);
2024-12-10 14:25:55 +00:00
/**
* @brief
* @param None
* @retval None
*
*/
void task_Init(void)
{
TimeSliceOffset_Register(&m_runled, Task_Runled, runled_reloadVal, runled_offset);
TimeSliceOffset_Register(&m_wdi, Task_wdi, wdi_reloadVal, wdi_offset);
2024-12-11 14:11:05 +00:00
TimeSliceOffset_Register(&m_startControl, Task_startControl, startControl_reloadVal, startControl_offset);
2024-12-11 09:51:48 +00:00
TimeSliceOffset_Register(&m_refreshJudgeData, Task_refreshJudgeData, refreshJudgeData_reloadVal, refreshJudgeData_offset);
}
/**
* @brief
* @param None
* @retval None
*
*/
void Task_Runled(void)
{
RUN_LED();
}
/**
* @brief
* @param None
* @retval None
*/
void Task_wdi(void)
2024-12-11 09:51:48 +00:00
{
feedDog();
debug_printf("chargCurrent:%f \n", getChargCurrent());
debug_printf("outputVoltage:%f \n", getOutputVoltage());
debug_printf("dischargCurrent:%f \n", getDischargCurrent());
debug_printf("solarInCircuitVoltage:%f \n", getSolarInCircuitVoltage());
debug_printf("HighSideMosTemperature:%f \n", getHighSideMosTemperature());
debug_printf("InputVoltage:%f \n", getInputVoltage());
debug_printf("DischargMosState:%d \n", getDischargMosState());
debug_printf("MPPT_Mode:%d \n", getMPPT_Mode());
debug_printf("DutyRatio:%f \n", getDutyRatio());
/* 每天复位一次复位前将电量信息写入flash中 */
static uint32_t temp = 60 * 60 * 24;
if (!(--temp)) {
temp = 0;
float tempF;
tempF = getTotalElectricityConsumption();
savetotalElectricityConsumption(&tempF);
tempF = getTotalChargCapacity();
savetotalChargCapacity(&tempF);
NVIC_SystemReset();
}
}
/**
* @brief
* @param None
* @retval None
*
*/
void Task_refreshJudgeData(void)
{
/* 获取数据 */
setInputVoltage();
2024-12-11 09:51:48 +00:00
setHighSideMosTemperature();
/* 判断有无电池 */
if (getBatteryState() == FALSE && (getChargBatteryCurrent() > 1 || getChargBatteryCurrent() < -1)
&& getOutputVoltage() < 14.2f) {
setBatteryState(TRUE);
}
2024-12-11 09:51:48 +00:00
/* 有电池,太阳能输出功率大,同时回路阻抗未测试或需要重新测试 */
if ((getCheckImpedanceState() == FALSE || g_cfgParameter.loopImpedance == 0.0f)
&& (getBatteryState() == TRUE) && (getChargCurrent() > 3.0f)
&& (getOutputVoltage() > 9) && (getSolarInCircuitVoltage() > 14)) {
TimeSliceOffset_Register(&m_impedanceCalculation, Task_impedanceCalculation
, impedanceCalculation_reloadVal, impedanceCalculation_reloadVal);
}
/* 温度检测 */
2024-12-11 09:51:48 +00:00
if ((getMosTemperState() != mosTemperStart)
&& (getHighSideMosTemperature() < g_cfgParameter.HighSideMosTemperature_start)) {
/* 状态处于停止运行则打开充电开关 */
if (getMosTemperState() == mosTemperStop) {
2024-12-11 14:11:05 +00:00
beginChargWork();
2024-12-11 09:51:48 +00:00
}
setMosTemperState(mosTemperStart);
}
else if ((getMosTemperState() == mosTemperStart)
&& getHighSideMosTemperature() > g_cfgParameter.HighSideMosTemperature_end) {
setMosTemperState(mosTemperEnd);
}
else if ((getMosTemperState() == mosTemperEnd)
&& getHighSideMosTemperature() > g_cfgParameter.HighSideMosTemperature_stop) {
setMosTemperState(mosTemperStop);
/* 停止充电 */
2024-12-11 14:11:05 +00:00
stopChargWork();
}
}
/**
* @brief
*
*
*
* @param None
* @retval None
*
*/
void Task_startControl(void)
{
2024-12-10 14:25:55 +00:00
/* 是否达到启动条件 */
if (getSolarInCircuitVoltage() > g_cfgParameter.startSolarOpenCircuitV) {
2024-12-10 14:25:55 +00:00
TimeSliceOffset_Unregister(&m_startControl);
m_startControl.runFlag = 0;
/* 判断有无电池 */
if (getOutputVoltage() > 10) {
setBatteryState(TRUE);
} else {
setBatteryState(FALSE);
}
2024-12-10 14:25:55 +00:00
/* 启动软起动任务 */
TimeSliceOffset_Register(&m_softStart, Task_softStart, softStart_reloadVal, softStart_offset);
}
}
2024-12-10 14:25:55 +00:00
/**
* @brief
* @param
* @retval
*/
void beginStartControlTask(void)
{
2024-12-11 14:11:05 +00:00
TimeSliceOffset_Register(&m_startControl, Task_startControl, startControl_reloadVal, startControl_offset);
m_startControl.runFlag = 1;
2024-12-10 14:25:55 +00:00
}
/**
* @brief
* @param
* @retval
*
*/
void Task_softStart(void)
{
static uint16_t num = 0;
static float dutyRatio = 0;
num++;
if (num < 5) {
2024-12-11 09:51:48 +00:00
set_pwmDutyRatio(0.1f);
EN_PWMOUT_Eable();
}
else if (num > 70 || dutyRatio > 0.75f) {
TimeSliceOffset_Unregister(&m_softStart);
m_softStart.runFlag = 0;
dutyRatio = 0;
num = 0;
2024-12-11 09:51:48 +00:00
setDutyRatio(0.75f);
if (getBatteryState() == TRUE) {
setMPPT_Mode(MPPT);
} else {
setMPPT_Mode(floatCharg);
}
setChargControlFlag(TRUE);
2024-12-11 09:51:48 +00:00
}
else {
2024-12-10 14:25:55 +00:00
setDutyRatio(getDutyRatio() + 0.01f);
}
}
2024-12-11 09:51:48 +00:00
/**
* @brief
* @param
* @retval
*/
void beginSoftStartTask(void)
{
TimeSliceOffset_Register(&m_softStart, Task_softStart, softStart_reloadVal, softStart_offset);
}
/**
* @brief
* @param
* @retval
*/
void Task_impedanceCalculation(void)
{
static uint8_t num = 0;
static float currOne = 0;
static float voltOne = 0;
static float currTwo = 0;
static float voltTwo = 0;
num++;
if (num == 1) {
setChargControlFlag(FALSE);
setDutyRatio(0.7);
set_pwmDutyRatio(getDutyRatio());
return;
}
if (num == 11) {
currOne = getChargCurrent() - getDischargCurrent();
voltOne = getOutputVoltage();
setDutyRatio(0.85);
set_pwmDutyRatio(getDutyRatio());
return;
}
if (num == 21) {
TimeSliceOffset_Unregister(&m_impedanceCalculation);
m_impedanceCalculation.runFlag = 0;
currTwo = getChargCurrent() - getDischargCurrent();
voltTwo = getOutputVoltage();
float tempLoopImpedance = 0;
tempLoopImpedance = (voltOne - voltTwo) / (currOne - currTwo);
/* 判断回路阻抗是否合理 */
if (tempLoopImpedance < 1.0f && tempLoopImpedance > 0.05f) {
g_cfgParameter.loopImpedance = tempLoopImpedance;
saveLoopImpedance(&g_cfgParameter.loopImpedance);
2024-12-11 09:51:48 +00:00
setCheckImpedanceState();
}
num = 0;
setMPPT_Mode(MPPT);
setChargControlFlag(TRUE);
return;
}
}
2024-12-11 14:11:05 +00:00
/**
* @brief
* @retval
*/
void Task_collectOpenCircuitVoltage(void)
{
/* 用于无充电控制时获取开路电压 */
static uint32_t collectOpenCircuitVoltageNoNUM = 0;
/* 用于有充电控制时获取开路电压 */
static uint8_t collectOpenCircuitVoltageYesNUM = 0;
if (FALSE == getChargControlFlag()) {
if (1 <= collectOpenCircuitVoltageNoNUM++) {
setSolarOpenCircuitVoltage();
collectOpenCircuitVoltageNoNUM = 0;
}
collectOpenCircuitVoltageYesNUM = 0;
}
collectOpenCircuitVoltageYesNUM++;
/* 到达开路电压检测时间 */
if (collectOpenCircuitVoltageYesNUM == g_cfgParameter.collectOpenCircuitVoltageTime) {
/* 有电池才进行开路电压检测 */
if (getBatteryState()) {
stopChargWork();
/* 设置延时为1000-500ms */
g_collectOpenCircuitVoltage.count = 500;
}
collectOpenCircuitVoltageYesNUM = 0;
}
2024-12-11 14:11:05 +00:00
/* 检测开路电压 */
if (collectOpenCircuitVoltageYesNUM == g_cfgParameter.collectOpenCircuitVoltageTime + 1) {
setSolarOpenCircuitVoltage();
beginChargWork();
}
}