2024-12-10 10:29:05 +00:00
|
|
|
|
|
|
|
|
|
#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-10 10:29:05 +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 09:51:48 +00:00
|
|
|
|
beginStartControlTask();
|
|
|
|
|
TimeSliceOffset_Register(&m_refreshJudgeData, Task_refreshJudgeData, refreshJudgeData_reloadVal, refreshJudgeData_offset);
|
2024-12-10 10:29:05 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @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());
|
|
|
|
|
|
2024-12-10 10:29:05 +00:00
|
|
|
|
/* 每天复位一次,复位前将电量信息写入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();
|
2024-12-10 10:29:05 +00:00
|
|
|
|
|
|
|
|
|
/* 判断有无电池 */
|
|
|
|
|
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-10 10:29:05 +00:00
|
|
|
|
/* 温度检测 */
|
2024-12-11 09:51:48 +00:00
|
|
|
|
if ((getMosTemperState() != mosTemperStart)
|
|
|
|
|
&& (getHighSideMosTemperature() < g_cfgParameter.HighSideMosTemperature_start)) {
|
|
|
|
|
/* 状态处于停止运行则打开充电开关 */
|
|
|
|
|
if (getMosTemperState() == mosTemperStop) {
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
setMosTemperState(mosTemperStart);
|
|
|
|
|
}
|
|
|
|
|
else if ((getMosTemperState() == mosTemperStart)
|
|
|
|
|
&& getHighSideMosTemperature() > g_cfgParameter.HighSideMosTemperature_end) {
|
|
|
|
|
setMosTemperState(mosTemperEnd);
|
|
|
|
|
}
|
|
|
|
|
else if ((getMosTemperState() == mosTemperEnd)
|
|
|
|
|
&& getHighSideMosTemperature() > g_cfgParameter.HighSideMosTemperature_stop) {
|
|
|
|
|
setMosTemperState(mosTemperStop);
|
|
|
|
|
/* 停止充电 */
|
|
|
|
|
EN_PWMOUT_Diseable();
|
|
|
|
|
pwm_Stop();
|
|
|
|
|
setMPPT_Mode(noWork);
|
2024-12-10 10:29:05 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @brief 停止充电或系统启动时后开启该任务,每隔一段时间检测开路电压
|
|
|
|
|
* 检测电池电压是否大于充电电压,大于则开启充电,否则关闭充电
|
|
|
|
|
* 能否达到充电标准,达到后检测电压判断系统中是否有电池同时启
|
|
|
|
|
* 动软启动任务
|
|
|
|
|
* @param None
|
|
|
|
|
* @retval None
|
|
|
|
|
*
|
|
|
|
|
*/
|
|
|
|
|
void Task_startControl(void)
|
|
|
|
|
{
|
2024-12-10 14:25:55 +00:00
|
|
|
|
/* 是否达到启动条件 */
|
2024-12-10 10:29:05 +00:00
|
|
|
|
if (getSolarInCircuitVoltage() > g_cfgParameter.startSolarOpenCircuitV) {
|
2024-12-10 14:25:55 +00:00
|
|
|
|
TimeSliceOffset_Unregister(&m_startControl);
|
|
|
|
|
m_startControl.runFlag = 0;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* 判断有无电池 */
|
2024-12-10 10:29:05 +00:00
|
|
|
|
if (getOutputVoltage() > 10) {
|
|
|
|
|
setBatteryState(TRUE);
|
|
|
|
|
} else {
|
|
|
|
|
setBatteryState(FALSE);
|
|
|
|
|
}
|
2024-12-10 14:25:55 +00:00
|
|
|
|
|
|
|
|
|
/* 启动软起动任务 */
|
2024-12-10 10:29:05 +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 09:51:48 +00:00
|
|
|
|
TimeSliceOffset_Register(&m_startControl, Task_startControl, startControl_reloadVal, startControl_offset);
|
2024-12-10 14:25:55 +00:00
|
|
|
|
}
|
2024-12-10 10:29:05 +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();
|
2024-12-10 10:29:05 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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);
|
2024-12-10 10:29:05 +00:00
|
|
|
|
set_pwmDutyRatio(getDutyRatio());
|
|
|
|
|
|
|
|
|
|
if (getBatteryState() == TRUE) {
|
|
|
|
|
setMPPT_Mode(MPPT);
|
|
|
|
|
} else {
|
|
|
|
|
setMPPT_Mode(floatCharg);
|
|
|
|
|
}
|
|
|
|
|
setChargControlFlag(TRUE);
|
2024-12-11 09:51:48 +00:00
|
|
|
|
}
|
2024-12-10 10:29:05 +00:00
|
|
|
|
|
|
|
|
|
else {
|
2024-12-10 14:25:55 +00:00
|
|
|
|
setDutyRatio(getDutyRatio() + 0.01f);
|
2024-12-10 10:29:05 +00:00
|
|
|
|
set_pwmDutyRatio(getDutyRatio());
|
|
|
|
|
}
|
|
|
|
|
}
|
2024-12-11 09:51:48 +00:00
|
|
|
|
/**
|
|
|
|
|
* @brief 启动软启动任务
|
|
|
|
|
* @param
|
|
|
|
|
* @retval
|
|
|
|
|
*/
|
|
|
|
|
void beginSoftStartTask(void)
|
|
|
|
|
{
|
|
|
|
|
TimeSliceOffset_Register(&m_softStart, Task_softStart, softStart_reloadVal, softStart_offset);
|
|
|
|
|
}
|
2024-12-10 10:29:05 +00:00
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @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();
|
2024-12-10 10:29:05 +00:00
|
|
|
|
}
|
|
|
|
|
num = 0;
|
|
|
|
|
|
|
|
|
|
setMPPT_Mode(MPPT);
|
|
|
|
|
setChargControlFlag(TRUE);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|