huyongji1.1-system/App/Irrigation/Irrigation.c

792 lines
23 KiB
C
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#include "Irrigation.h"
// 直排模式时间参数
uint32_t DIRECT_HIGH_PUMP_ON_TIME = 1; // 1分钟
uint32_t DIRECT_HIGH_PUMP_OFF_TIME = 150; // 150分钟
uint32_t DIRECT_HIGH_FAN_ON_TIME = 40; // 40分钟
uint32_t DIRECT_HIGH_FAN_OFF_TIME = 2 ; // 2分钟
uint32_t DIRECT_LOW_FAN_ON_TIME = 10; // 10分钟
uint32_t DIRECT_LOW_FAN_OFF_TIME = 20; // 20分钟
uint32_t DIRECT_FIRST_LOW_FAN_ON = 40; // 40分钟
uint32_t DIRECT_FIRST_LOW_FAN_OFF = 2; // 2分钟
// 冲厕模式时间参数
uint32_t FLUSH_HIGH_PUMP_ON_TIME = 1; // 1分钟
uint32_t FLUSH_HIGH_PUMP_OFF_TIME = 150; // 150分钟
uint32_t FLUSH_HIGH_FAN_ON_TIME = 40; // 40分钟
uint32_t FLUSH_HIGH_FAN_OFF_TIME = 2; // 2分钟
uint32_t FLUSH_LOW_FAN_ON_TIME = 5; // 5分钟
uint32_t FLUSH_LOW_FAN_OFF_TIME = 25; // 25分钟
uint32_t FLUSH_FIRST_LOW_FAN_ON = 40; // 40分钟
uint32_t FLUSH_FIRST_LOW_FAN_OFF = 2; // 2分钟
//灌溉模式时间参数
uint32_t HIGH_LEVEL_PUMP_ON_TIME = 1; // 1分钟
uint32_t HIGH_LEVEL_PUMP_OFF_TIME = 120; // 120分钟
uint32_t HIGH_LEVEL_FAN_ON_TIME = 20; // 20分钟
uint32_t HIGH_LEVEL_FAN_OFF_TIME = 5; // 5分钟
uint32_t FIRST_LOW_FAN_ON_TIME = 10; // 10分钟
uint32_t FIRST_LOW_FAN_OFF_TIME = 10; // 10分钟
uint32_t LOW_LEVEL_FAN_ON_TIME = 5; // 5分钟
uint32_t LOW_LEVEL_FAN_OFF_TIME = 40; // 40分钟
// 特殊模式变量
uint32_t SPECIAL_INITIAL_STOP_TIME = 1440; // 24小时 = 1440分钟 前5分钟
uint32_t SPECIAL_FIRST_PHASE_TIME = 2880; // 48小时 = 2880分钟 后六分钟
uint32_t SPECIAL_PUMP_RUN_TIME = 1; // 前1分钟运行
uint32_t SPECIAL_FIRST_STOP_TIME = 360; // 前359分钟停止
uint32_t SPECIAL_SECOND_PHASE_TIME =1; // 48小时 = 2880分钟
uint32_t SPECIAL_SECOND_STOP_TIME = 240; // 239分钟停止
// 特殊模式的变量
SpecialPhase_t special_phase = SPECIAL_INITIAL_STOP;
uint32_t special_phase_timer = 0; // 阶段计时器
uint32_t special_cycle_timer = 0; // 循环计时器
WorkMode current_mode = MODE_NONE;
// 参数更改标志
uint8_t parameters_changed = 0; // 标志参数是否已更改
// 定义计时变量
uint32_t pump_timer = 0; // 水泵计时器
uint32_t fan_timer = 0; // 风机计时器
static FanState current_fan = FAN_1; // 当前运行的风机
uint8_t is_first_low = 1; // 是否是首次低液位
// 浮球检测相关变量
static uint8_t float_state_count = 0; // 浮球状态计数器
static uint8_t last_float_state = GPIO_PIN_SET; // 记录上一次浮球状态
static uint8_t stable_float_state = 0; // 稳定的浮球状态
volatile uint8_t timer_pause = 0; // 初始化为0表示不暂停
// 添加一个标志位表示是否刚刚修改过时间
volatile uint8_t time_just_modified = 0;
// 读取浮球状态(带防抖)
uint8_t Read_FloatBall_Stable(void)
{
uint8_t current_state = Read_FloatBall();
if(current_state != last_float_state)
{
float_state_count = 0;
last_float_state = current_state;
}
else if(float_state_count < 10)
{
float_state_count++;
if(float_state_count >= 10)
{
stable_float_state = current_state;
float_state_count = 0;
}
}
return stable_float_state;
}
// 在App_Work.c中定义
uint32_t debug_pump_state = 0; // 0:关闭 1:开启
// 初始化函数
void Device_Init(void)
{
// 初始化EEPROM并加载保存的时间参数
Inf_AT24C02_Init();
Time_Load();
// 关闭所有设备
Control_WaterPump(0);
Control_Fan1(0);
Control_Fan2(0);
// 重置计时器和状态
pump_timer = 0;
fan_timer = 0;
current_fan = FAN_1;
is_first_low = 1;
// 初始化浮球检测
float_state_count = 0;
last_float_state = Read_FloatBall();
stable_float_state = last_float_state;
}
// 风机切换控制函数
static void Control_Fan_Switch(uint8_t fan_on)
{
if(fan_on)
{
if(current_fan == FAN_1)
{
Control_Fan1(1);
Control_Fan2(0);
}
else
{
Control_Fan1(0);
Control_Fan2(1);
}
}
else
{
Control_Fan1(0);
Control_Fan2(0);
}
}
// 记录上一次的液位状态
static uint8_t last_floatBallState = GPIO_PIN_SET;
// 直排模式控制函数
void Direct_Mode_Control(void)
{
uint8_t floatBallState = Read_FloatBall_Stable();
// 检查液位状态是否发生变化
if (floatBallState != last_floatBallState)
{
// 液位状态发生变化,重置计时器和状态
pump_timer = 0;
fan_timer = 0;
is_first_low = 1; // 重置首次低液位标志
last_floatBallState = floatBallState; // 更新上一次的液位状态
}
// 高液位状态
if (floatBallState == GPIO_PIN_RESET)
{
if (force_pump_off) {
Control_WaterPump(0);
debug_pump_state = 0;
} else {
// 水泵控制 - 按照自己的时间参数运行
if (pump_timer < DIRECT_HIGH_PUMP_ON_TIME)
{
Control_WaterPump(1);
debug_pump_state = 1;
}
else if (pump_timer < (DIRECT_HIGH_PUMP_ON_TIME + DIRECT_HIGH_PUMP_OFF_TIME))
{
Control_WaterPump(0);
debug_pump_state = 0;
}
else
{
pump_timer = 0; // 只重置水泵计时器
}
}
// 风机控制 - 按照自己的时间参数运行
if (force_fan_off)
{
Control_Fan_Switch(0);
} else {
if (fan_timer < DIRECT_HIGH_FAN_ON_TIME)
{
Control_Fan_Switch(1);
}
else
{
Control_Fan_Switch(0);
if (fan_timer >= (DIRECT_HIGH_FAN_ON_TIME + DIRECT_HIGH_FAN_OFF_TIME))
{
fan_timer = 0; // 只重置风机计时器
current_fan = (current_fan == FAN_1) ? FAN_2 : FAN_1;
}
}
}
}
// 低液位状态
else
{
Control_WaterPump(0);
debug_pump_state = 0;
if (force_fan_off) {
Control_Fan_Switch(0);
}
else
{
if (is_first_low)
{
if (fan_timer < DIRECT_FIRST_LOW_FAN_ON)
{
Control_Fan_Switch(1);
}
else
{
Control_Fan_Switch(0);
if (fan_timer >= (DIRECT_FIRST_LOW_FAN_ON + DIRECT_FIRST_LOW_FAN_OFF))
{
fan_timer = 0;
current_fan = (current_fan == FAN_1) ? FAN_2 : FAN_1;
is_first_low = 0;
}
}
}
else
{
if (fan_timer < DIRECT_LOW_FAN_ON_TIME)
{
Control_Fan_Switch(1);
}
else
{
Control_Fan_Switch(0);
if (fan_timer >= (DIRECT_LOW_FAN_ON_TIME + DIRECT_LOW_FAN_OFF_TIME))
{
fan_timer = 0;
current_fan = (current_fan == FAN_1) ? FAN_2 : FAN_1;
}
}
}
}
}
}
// 当模式切换到直排模式时调用
void Switch_To_Direct_Mode(void)
{
// 重置所有计时器和状态
pump_timer = 0;
fan_timer = 0;
last_floatBallState = Read_FloatBall_Stable(); // 初始化上次液位状态
// 关闭所有设备等待Direct_Mode_Control函数重新控制
Control_WaterPump(0);
Control_Fan1(0);
Control_Fan2(0);
}
// 记录上一次的灌溉模式液位状态
static uint8_t last_irrigationBallState = GPIO_PIN_SET;
// 灌溉模式控制函数
void Irrigation_Mode_Control(void)
{
uint8_t floatBallState = Read_FloatBall_Stable();
// 检查液位状态是否发生变化
if (floatBallState != last_irrigationBallState)
{
// 液位状态发生变化,重置计时器和状态
pump_timer = 0;
fan_timer = 0;
is_first_low = 1; // 重置首次低液位标志
last_irrigationBallState = floatBallState; // 更新上一次的液位状态
}
// 高液位状态
if (floatBallState == GPIO_PIN_RESET)
{
// 水泵控制 - 添加force_pump_off检查
if (force_pump_off) {
Control_WaterPump(0);
debug_pump_state = 0;
} else {
if (pump_timer < HIGH_LEVEL_PUMP_ON_TIME)
{
Control_WaterPump(1);
debug_pump_state = 1;
}
else if (pump_timer < (HIGH_LEVEL_PUMP_ON_TIME + HIGH_LEVEL_PUMP_OFF_TIME))
{
Control_WaterPump(0);
debug_pump_state = 0;
}
else
{
pump_timer = 0; // 只重置水泵计时器
}
}
// 风机控制 - 添加force_fan_off检查
if (force_fan_off) {
Control_Fan_Switch(0);
} else {
if (fan_timer < HIGH_LEVEL_FAN_ON_TIME)
{
Control_Fan_Switch(1);
}
else
{
Control_Fan_Switch(0);
if (fan_timer >= (HIGH_LEVEL_FAN_ON_TIME + HIGH_LEVEL_FAN_OFF_TIME))
{
fan_timer = 0;
current_fan = (current_fan == FAN_1) ? FAN_2 : FAN_1;
}
}
}
}
// 低液位状态
else
{
Control_WaterPump(0);
debug_pump_state = 0;
// 风机控制 - 添加force_fan_off检查
if (force_fan_off) {
Control_Fan_Switch(0);
} else {
if (is_first_low)
{
if (fan_timer < FIRST_LOW_FAN_ON_TIME)
{
Control_Fan_Switch(1);
}
else
{
Control_Fan_Switch(0);
if (fan_timer >= (FIRST_LOW_FAN_ON_TIME + FIRST_LOW_FAN_OFF_TIME))
{
fan_timer = 0;
current_fan = (current_fan == FAN_1) ? FAN_2 : FAN_1;
is_first_low = 0;
}
}
}
else
{
if (fan_timer < LOW_LEVEL_FAN_ON_TIME)
{
Control_Fan_Switch(1);
}
else
{
Control_Fan_Switch(0);
if (fan_timer >= (LOW_LEVEL_FAN_ON_TIME + LOW_LEVEL_FAN_OFF_TIME))
{
fan_timer = 0;
current_fan = (current_fan == FAN_1) ? FAN_2 : FAN_1;
}
}
}
}
}
}
// 当模式切换到灌溉模式时调用
void Switch_To_Irrigation_Mode(void)
{
// 重置所有计时器和状态
pump_timer = 0;
fan_timer = 0;
is_first_low = 1; // 确保进入模式后遇到的第一个低液位是首次
last_irrigationBallState = Read_FloatBall_Stable(); // 初始化上次液位状态
// 关闭所有设备等待Irrigation_Mode_Control函数重新控制
Control_WaterPump(0);
Control_Fan1(0);
Control_Fan2(0);
}
// 记录上一次的冲厕模式液位状态
static uint8_t last_flushBallState = GPIO_PIN_SET;
void Flush_Mode_Control(void)
{
uint8_t floatBallState = Read_FloatBall_Stable();
// 检查液位状态是否发生变化
if (floatBallState != last_flushBallState)
{
// 液位状态发生变化,重置计时器和状态
pump_timer = 0;
fan_timer = 0;
is_first_low = 1; // 重置首次低液位标志
last_flushBallState = floatBallState; // 更新上一次的液位状态
}
// 高液位状态
if (floatBallState == GPIO_PIN_RESET)
{
// 水泵控制 - 添加force_pump_off检查
if (force_pump_off) {
Control_WaterPump(0);
debug_pump_state = 0;
} else {
if (pump_timer < FLUSH_HIGH_PUMP_ON_TIME)
{
Control_WaterPump(1);
debug_pump_state = 1;
}
else if (pump_timer < (FLUSH_HIGH_PUMP_ON_TIME + FLUSH_HIGH_PUMP_OFF_TIME))
{
Control_WaterPump(0);
debug_pump_state = 0;
}
else
{
pump_timer = 0; // 只重置水泵计时器
}
}
// 风机控制 - 添加force_fan_off检查
if (force_fan_off) {
Control_Fan_Switch(0);
} else {
if (fan_timer < FLUSH_HIGH_FAN_ON_TIME)
{
Control_Fan_Switch(1);
}
else if (fan_timer < (FLUSH_HIGH_FAN_ON_TIME + FLUSH_HIGH_FAN_OFF_TIME))
{
Control_Fan_Switch(0);
}
else
{
fan_timer = 0; // 只重置风机计时器
current_fan = (current_fan == FAN_1) ? FAN_2 : FAN_1;
}
}
}
// 低液位状态
else
{
Control_WaterPump(0);
debug_pump_state = 0;
// 风机控制 - 添加force_fan_off检查
if (force_fan_off) {
Control_Fan_Switch(0);
} else {
if (is_first_low)
{
if (fan_timer < FLUSH_FIRST_LOW_FAN_ON)
{
Control_Fan_Switch(1);
}
else if (fan_timer < (FLUSH_FIRST_LOW_FAN_ON + FLUSH_FIRST_LOW_FAN_OFF))
{
Control_Fan_Switch(0);
}
else
{
fan_timer = 0;
current_fan = (current_fan == FAN_1) ? FAN_2 : FAN_1;
is_first_low = 0;
}
}
else
{
if (fan_timer < FLUSH_LOW_FAN_ON_TIME)
{
Control_Fan_Switch(1);
}
else if (fan_timer < (FLUSH_LOW_FAN_ON_TIME + FLUSH_LOW_FAN_OFF_TIME))
{
Control_Fan_Switch(0);
}
else
{
fan_timer = 0;
current_fan = (current_fan == FAN_1) ? FAN_2 : FAN_1;
}
}
}
}
}
// 当模式切换到冲厕模式时调用
void Switch_To_Flush_Mode(void)
{
// 重置所有计时器和状态
pump_timer = 0;
fan_timer = 0;
is_first_low = 1; // 确保进入模式后遇到的第一个低液位是首次
last_flushBallState = Read_FloatBall_Stable(); // 初始化上次液位状态
// 关闭所有设备等待Flush_Mode_Control函数重新控制
Control_WaterPump(0);
Control_Fan1(0);
Control_Fan2(0);
}
// 记录上一次的特殊模式液位状态
static uint8_t last_specialBallState = GPIO_PIN_SET;
void Special_Mode_Control(void)
{
uint8_t floatBallState = Read_FloatBall_Stable();
// 检查液位状态是否发生变化
if (floatBallState != last_specialBallState)
{
// 液位状态发生变化,重置风机计时器和状态
fan_timer = 0;
is_first_low = 1; // 重置首次低液位标志
last_specialBallState = floatBallState; // 更新上一次的液位状态
// 注意不重置pump_timer因为特殊模式水泵控制独立于液位状态
}
// 风机控制逻辑 - 与直排模式相同,但使用统一的计时逻辑
if(floatBallState == GPIO_PIN_RESET) // 高液位
{
if(fan_timer < DIRECT_HIGH_FAN_ON_TIME)
{
Control_Fan_Switch(1);
}
else if(fan_timer < (DIRECT_HIGH_FAN_ON_TIME + DIRECT_HIGH_FAN_OFF_TIME))
{
Control_Fan_Switch(0);
}
else
{
fan_timer = 0;
current_fan = (current_fan == FAN_1) ? FAN_2 : FAN_1;
}
}
else // 低液位
{
if(is_first_low)
{
if(fan_timer < DIRECT_FIRST_LOW_FAN_ON)
{
Control_Fan_Switch(1);
}
else if(fan_timer < (DIRECT_FIRST_LOW_FAN_ON + DIRECT_FIRST_LOW_FAN_OFF))
{
Control_Fan_Switch(0);
}
else
{
fan_timer = 0;
current_fan = (current_fan == FAN_1) ? FAN_2 : FAN_1;
is_first_low = 0;
}
}
else
{
if(fan_timer < DIRECT_LOW_FAN_ON_TIME)
{
Control_Fan_Switch(1);
}
else if(fan_timer < (DIRECT_LOW_FAN_ON_TIME + DIRECT_LOW_FAN_OFF_TIME))
{
Control_Fan_Switch(0);
}
else
{
fan_timer = 0;
current_fan = (current_fan == FAN_1) ? FAN_2 : FAN_1;
}
}
}
// 水泵控制逻辑(特殊模式分阶段控制)
switch(special_phase)
{
case SPECIAL_INITIAL_STOP:
// 初始24小时全部停止
Control_WaterPump(0);
debug_pump_state = 0;
if(special_phase_timer > SPECIAL_INITIAL_STOP_TIME)
{
special_phase = SPECIAL_FIRST_PHASE;
special_phase_timer = 0;
special_cycle_timer = 0;
}
break;
case SPECIAL_FIRST_PHASE:
// 第一阶段48小时每6小时循环1分钟开359分钟关
if(special_phase_timer < SPECIAL_FIRST_PHASE_TIME)
{
if(special_cycle_timer < SPECIAL_PUMP_RUN_TIME)
{
Control_WaterPump(1); // 开1分钟
debug_pump_state = 1;
}
else if(special_cycle_timer < (SPECIAL_PUMP_RUN_TIME + SPECIAL_FIRST_STOP_TIME))
{
Control_WaterPump(0); // 关359分钟
debug_pump_state = 0;
}
else
{
special_cycle_timer = 0; // 重置循环计时器
}
}
else
{
special_phase = SPECIAL_SECOND_PHASE;
special_phase_timer = 0;
special_cycle_timer = 0;
}
break;
case SPECIAL_SECOND_PHASE:
// 第二阶段48小时每4小时循环1分钟开239分钟关
if(special_phase_timer < SPECIAL_SECOND_PHASE_TIME)
{
if(special_cycle_timer < SPECIAL_PUMP_RUN_TIME)
{
Control_WaterPump(1); // 开1分钟
debug_pump_state = 1;
}
else if(special_cycle_timer < (SPECIAL_PUMP_RUN_TIME + SPECIAL_SECOND_STOP_TIME))
{
Control_WaterPump(0); // 关239分钟
debug_pump_state = 0;
}
else
{
special_cycle_timer = 0; // 重置循环计时器
}
}
else
{
special_phase = SPECIAL_TO_DIRECT;
// 直接调用直排模式切换函数
Switch_To_Direct_Mode();
current_mode = MODE_DIRECT; // 更新当前模式
}
break;
case SPECIAL_TO_DIRECT:
// 已经切换到直排模式,但可能还在执行特殊模式
Switch_To_Direct_Mode();
current_mode = MODE_DIRECT; // 更新当前模式
break;
}
}
// 当模式切换到特殊模式时调用
void Switch_To_Special_Mode(void)
{
// 重置相关计时器和状态
fan_timer = 0;
special_phase = SPECIAL_INITIAL_STOP;
special_phase_timer = 0;
special_cycle_timer = 0;
is_first_low = 1; // 确保进入模式后遇到的第一个低液位是首次
last_specialBallState = Read_FloatBall_Stable(); // 初始化上次液位状态
// 关闭所有设备,特殊模式会按照自己的逻辑控制设备
Control_WaterPump(0);
Control_Fan1(0);
Control_Fan2(0);
// 更新当前模式
current_mode = MODE_SPECIAL;
}
// 添加一个停止所有设备的函数
void Stop_All_Devices(void)
{
// 停止所有设备
Control_WaterPump(0);
Control_Fan1(0);
Control_Fan2(0);
// 重置所有计时器
pump_timer = 0;
fan_timer = 0;
special_phase_timer = 0;
special_cycle_timer = 0;
// 重置状态标志
is_first_low = 1;
debug_pump_state = 0;
// 等待一小段时间确保设备完全停止
HAL_Delay(1000); // 延时1秒
}
// 修改模式切换函数
void Switch_Mode(WorkMode new_mode)
{
// 先停止所有设备
Stop_All_Devices();
// 然后切换到新模式
switch(new_mode)
{
case MODE_FLUSH:
// printf("切换到冲厕模式\n");
Switch_To_Flush_Mode();
current_mode = MODE_FLUSH;
break;
case MODE_IRRIGATION:
// printf("切换到灌溉模式\n");
Switch_To_Irrigation_Mode();
current_mode = MODE_IRRIGATION;
break;
case MODE_DIRECT:
// printf("切换到直排模式\n");
Switch_To_Direct_Mode();
current_mode = MODE_DIRECT;
break;
case MODE_SPECIAL:
// printf("切换到特殊模式\n");
Switch_To_Special_Mode();
current_mode = MODE_SPECIAL;
break;
default:
// printf("无效的模式切换请求\n");
break;
}
}
// 添加一个重启当前模式的函数
// 修改Restart_Current_Mode函数
void Restart_Current_Mode(void)
{
// 1. 先禁用定时器中断
HAL_TIM_Base_Stop_IT(&htim2); // 暂停TIM2中断
// 2. 停止所有设备
Stop_All_Devices();
// 3. 确保所有计时器和状态完全重置
pump_timer = 0;
fan_timer = 0;
special_phase_timer = 0;
special_cycle_timer = 0;
is_first_low = 1;
debug_pump_state = 0;
current_fan = FAN_1;
// 4. 添加足够的延时确保系统完全停止
HAL_Delay(1000);
// 5. 根据当前模式重新启动
switch (current_mode)
{
case MODE_FLUSH:
// printf("重启冲厕模式\n");
Switch_To_Flush_Mode();
break;
case MODE_IRRIGATION:
// printf("重启灌溉模式\n");
Switch_To_Irrigation_Mode();
break;
case MODE_DIRECT:
// printf("重启直排模式\n");
Switch_To_Direct_Mode();
break;
}
// 6. 再次确认计时器为0
pump_timer = 0;
fan_timer = 0;
// 7. 重新启用定时器中断
HAL_TIM_Base_Start_IT(&htim2); // 重新启动TIM2中断
// printf("重启完成确认计时器状态pump_timer=%lu\n", pump_timer);
}