392 lines
12 KiB
C
392 lines
12 KiB
C
|
||
#include "task.h"
|
||
#include "inFlash.h"
|
||
#include "parameter.h"
|
||
#include "FM_GPIO.h"
|
||
#include "chargControlEnum.h"
|
||
#include "bl_chargControl.h"
|
||
#include "hy_protocol.h"
|
||
|
||
/* 控制运行指示灯和喂狗 */
|
||
// #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 /* 任务执行间隔 */
|
||
#define wdi_offset 0 /* 任务执行偏移量 */
|
||
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);
|
||
|
||
/* 回路阻抗检测 */
|
||
#define impedanceCalculation_reloadVal 20 /* 任务执行间隔 */
|
||
#define impedanceCalculation_offset 0 /* 任务执行偏移量 */
|
||
static STR_TimeSliceOffset m_impedanceCalculation;
|
||
static void Task_impedanceCalculation(void);
|
||
|
||
/* 开路电压采集 */
|
||
#define collectOpenCircuitVoltage_reloadVal 1000 /* 任务执行间隔 */
|
||
#define collectOpenCircuitVoltage_offset 0 /* 任务执行偏移量 */
|
||
STR_TimeSliceOffset m_collectOpenCircuitVoltage;
|
||
void Task_collectOpenCircuitVoltage(void);
|
||
|
||
/* 限时开启HY协议配置模式 */
|
||
#define beginHYconfigMode_reloadVal 1000 /* 任务执行间隔 */
|
||
#define beginHYconfigMode_offset 0 /* 任务执行偏移量 */
|
||
STR_TimeSliceOffset m_beginHYconfigMode;
|
||
void Task_beginHYconfigMode(void);
|
||
|
||
|
||
|
||
|
||
/**
|
||
* @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);
|
||
TimeSliceOffset_Register(&m_startControl, Task_startControl, startControl_reloadVal, startControl_offset);
|
||
TimeSliceOffset_Register(&m_refreshJudgeData, Task_refreshJudgeData
|
||
, refreshJudgeData_reloadVal, refreshJudgeData_offset);
|
||
TimeSliceOffset_Register(&m_collectOpenCircuitVoltage, Task_collectOpenCircuitVoltage
|
||
, collectOpenCircuitVoltage_reloadVal, collectOpenCircuitVoltage_offset);
|
||
|
||
}
|
||
|
||
/**
|
||
* @brief 运行指示灯任务
|
||
* @param None
|
||
* @retval None
|
||
*
|
||
*/
|
||
void Task_Runled(void)
|
||
{
|
||
RUN_LED();
|
||
}
|
||
|
||
/**
|
||
* @brief 喂狗任务
|
||
* @param None
|
||
* @retval None
|
||
*/
|
||
void Task_wdi(void)
|
||
{
|
||
feedDog();
|
||
|
||
debug_printf("chargCurrent:%f \n", getChargCurrent());
|
||
debug_printf("outputVoltage:%f \n", getOutputVoltage());
|
||
debug_printf("BatteryVoltage:%f \n", getBatteryVoltage());
|
||
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("loopImpedance:%f \n", g_cfgParameter.loopImpedance);
|
||
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();
|
||
setHighSideMosTemperature();
|
||
|
||
/* 判断有无电池 */
|
||
if (getBatteryState() == FALSE && (getChargBatteryCurrent() > 1 || getChargBatteryCurrent() < -1)
|
||
&& getOutputVoltage() < 14.2f) {
|
||
setBatteryState(TRUE);
|
||
}
|
||
|
||
/* 有电池,太阳能输出功率大,同时回路阻抗未测试或需要重新测试 */
|
||
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);
|
||
}
|
||
|
||
/* 温度检测 */
|
||
if ((getMosTemperState() != mosTemperStart)
|
||
&& (getHighSideMosTemperature() < g_cfgParameter.HighSideMosTemperature_start)) {
|
||
/* 状态处于停止运行则打开充电开关 */
|
||
if (getMosTemperState() == mosTemperStop) {
|
||
beginChargWork();
|
||
}
|
||
setMosTemperState(mosTemperStart);
|
||
}
|
||
else if ((getMosTemperState() == mosTemperStart)
|
||
&& getHighSideMosTemperature() > g_cfgParameter.HighSideMosTemperature_end) {
|
||
setMosTemperState(mosTemperEnd);
|
||
}
|
||
else if ((getMosTemperState() == mosTemperEnd)
|
||
&& getHighSideMosTemperature() > g_cfgParameter.HighSideMosTemperature_stop) {
|
||
setMosTemperState(mosTemperStop);
|
||
/* 停止充电 */
|
||
stopChargWork();
|
||
}
|
||
}
|
||
|
||
/**
|
||
* @brief 停止充电或系统启动时后开启该任务,每隔一段时间检测开路电压
|
||
* 检测电池电压是否大于充电电压,大于则开启充电,否则关闭充电
|
||
* 能否达到充电标准,达到后检测电压判断系统中是否有电池同时启
|
||
* 动软启动任务
|
||
* @param None
|
||
* @retval None
|
||
*
|
||
*/
|
||
void Task_startControl(void)
|
||
{
|
||
/* 是否达到启动条件 */
|
||
if (getSolarInCircuitVoltage() > g_cfgParameter.startSolarOpenCircuitV) {
|
||
TimeSliceOffset_Unregister(&m_startControl);
|
||
m_startControl.runFlag = 0;
|
||
|
||
|
||
/* 判断有无电池 */
|
||
if (getOutputVoltage() > 10) {
|
||
setBatteryState(TRUE);
|
||
} else {
|
||
setBatteryState(FALSE);
|
||
}
|
||
|
||
/* 启动软起动任务 */
|
||
TimeSliceOffset_Register(&m_softStart, Task_softStart, softStart_reloadVal, softStart_offset);
|
||
}
|
||
}
|
||
/**
|
||
* @brief 打开启动任务
|
||
* @param
|
||
* @retval
|
||
*/
|
||
void beginStartControlTask(void)
|
||
{
|
||
TimeSliceOffset_Register(&m_startControl, Task_startControl, startControl_reloadVal, startControl_offset);
|
||
m_startControl.runFlag = 1;
|
||
}
|
||
|
||
/**
|
||
* @brief 软启动
|
||
* @param
|
||
* @retval
|
||
*
|
||
*/
|
||
void Task_softStart(void)
|
||
{
|
||
static uint16_t num = 0;
|
||
static float dutyRatio = 0;
|
||
num++;
|
||
|
||
if (num < 5) {
|
||
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;
|
||
setDutyRatio(0.75f);
|
||
|
||
if (getBatteryState() == TRUE) {
|
||
setMPPT_Mode(MPPT);
|
||
} else {
|
||
setMPPT_Mode(floatCharg);
|
||
}
|
||
setChargControlFlag(TRUE);
|
||
}
|
||
|
||
else {
|
||
setDutyRatio(getDutyRatio() + 0.01f);
|
||
}
|
||
}
|
||
/**
|
||
* @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);
|
||
setCheckImpedanceState();
|
||
}
|
||
num = 0;
|
||
|
||
setMPPT_Mode(MPPT);
|
||
setChargControlFlag(TRUE);
|
||
return;
|
||
}
|
||
}
|
||
|
||
/**
|
||
* @brief 开路电压采集
|
||
* @retval
|
||
*/
|
||
void Task_collectOpenCircuitVoltage(void)
|
||
{
|
||
/* 用于无充电控制时获取开路电压 */
|
||
static uint32_t collectOpenCircuitVoltageNoNUM = 0;
|
||
/* 用于有充电控制时获取开路电压 */
|
||
static uint8_t collectOpenCircuitVoltageYesNUM = 0;
|
||
/* 用于有充电控制时当标志位 */
|
||
static BOOL collectOpenCircuitVoltageYesFlag = 0;
|
||
|
||
|
||
/* 未进行充电时,3S采集一次开路电压 */
|
||
if (FALSE == getChargControlFlag()) {
|
||
if (2 <= collectOpenCircuitVoltageNoNUM++) {
|
||
setSolarOpenCircuitVoltage();
|
||
collectOpenCircuitVoltageNoNUM = 0;
|
||
}
|
||
collectOpenCircuitVoltageYesNUM = 0;
|
||
if (collectOpenCircuitVoltageYesFlag == TRUE) {
|
||
setSolarOpenCircuitVoltage();
|
||
beginChargWork();
|
||
collectOpenCircuitVoltageYesFlag = FALSE;
|
||
}
|
||
|
||
}
|
||
|
||
collectOpenCircuitVoltageYesNUM++;
|
||
|
||
/* 到达开路电压检测时间 */
|
||
if (collectOpenCircuitVoltageYesNUM == g_cfgParameter.collectOpenCircuitVoltageTime) {
|
||
/* 有电池才进行开路电压检测 */
|
||
if (getBatteryState()) {
|
||
collectOpenCircuitVoltageYesFlag = TRUE;
|
||
stopChargWork();
|
||
/* 设置延时为1000-500ms */
|
||
m_collectOpenCircuitVoltage.count = 500;
|
||
}
|
||
collectOpenCircuitVoltageYesNUM = 0;
|
||
}
|
||
|
||
/* 检测开路电压 */
|
||
if (collectOpenCircuitVoltageYesNUM == g_cfgParameter.collectOpenCircuitVoltageTime + 1) {
|
||
setSolarOpenCircuitVoltage();
|
||
beginChargWork();
|
||
collectOpenCircuitVoltageYesFlag = FALSE;
|
||
}
|
||
}
|
||
|
||
/**
|
||
* @brief 开启HY配置模式后,配置完成后120S后自动退出
|
||
* @param
|
||
* @retval
|
||
*/
|
||
void Task_beginHYconfigMode(void)
|
||
{
|
||
static uint8_t num = 0;
|
||
num++;
|
||
if (num >= 120) {
|
||
TimeSliceOffset_Unregister(&m_beginHYconfigMode);
|
||
m_beginHYconfigMode.runFlag = 0;
|
||
num = 0;
|
||
setHYconfigModeState(FALSE);
|
||
}
|
||
}
|
||
|
||
/**
|
||
* @brief 开启HY配置模式后启动退出任务
|
||
* @param
|
||
* @retval
|
||
*/
|
||
void beginHYconfigMode(void)
|
||
{
|
||
setHYconfigModeState(TRUE);
|
||
TimeSliceOffset_Register(&m_beginHYconfigMode, Task_beginHYconfigMode
|
||
, beginHYconfigMode_reloadVal, beginHYconfigMode_offset);
|
||
}
|
||
|