#include "app_config.h" #include "interface.h" #include "app_frm_monitor.h" #include "app_frm_signal.h" #include "app_frm_timer.h" #include "app_param_manage.h" #include "app_power.h" // 定义按钮状态枚举 typedef enum { BUTTON_STATE_INITIAL, BUTTON_STATE_SHORT_PRESS, BUTTON_STATE_SHORT_PRESS_DETECTED, BUTTON_STATE_WAIT_FOR_LONG_PRESS, BUTTON_STATE_LONG_PRESS, BUTTON_STATE_LONG_PRESS_WAIT } ButtonState; // 定义按钮结构体 typedef struct { ButtonState state; uint32_t press_start_time; uint32_t release_start_time; uint8_t is_power_on; Timer timer; uint8_t old_is_power_on; Timer timer1; } PowerButton; // 全局变量 PowerSystem power_data; static PowerButton power_button = {BUTTON_STATE_INITIAL, 0, 0, 0, {0},0}; //// 电源按钮处理函数 //static void handlePowerButton(void) //{ // switch (power_button.state) // { // case BUTTON_STATE_INITIAL: // if (power_data.remote_power_switch == app_close()) // { // power_button.state = BUTTON_STATE_SHORT_PRESS_DETECTED; // timerStart(&power_button.timer, 500, 0); // 启动短按定时器,500ms // } // break; // // case BUTTON_STATE_SHORT_PRESS_DETECTED: // if (power_data.remote_power_switch == app_open()) // { // if (power_button.timer.active) // 定时器未到期,短按完成,启动等待长按定时器 // { // power_button.state = BUTTON_STATE_WAIT_FOR_LONG_PRESS; // timerStart(&power_button.timer, 500, 0); // 启动等待长按定时器,500ms // } // } // else if (!power_button.timer.active)// 短按定时器到期,按键仍被按下,视为无效,重置为初始状态 // { // power_button.state = BUTTON_STATE_INITIAL; // } // break; // // case BUTTON_STATE_WAIT_FOR_LONG_PRESS: // if (power_data.remote_power_switch == app_close())// 检测是否在等待时间内进行长按 // { // power_button.state = BUTTON_STATE_LONG_PRESS; // timerStart(&power_button.timer, 1000, 0); // 启动长按定时器,1000ms // } // else if (!power_button.timer.active) // 等待长按超时,重置为初始状态 // { // power_button.state = BUTTON_STATE_INITIAL; // } // break; // // case BUTTON_STATE_LONG_PRESS: // // if (!power_button.timer.active)// 长按完成,切换电源状态 20250423 修改不需要判断松开按键就打开控制器 // { // power_button.is_power_on = !power_button.is_power_on; // printf("PowerButton: is_power_on = %d\n", power_button.is_power_on); // power_button.state = BUTTON_STATE_LONG_PRESS_WAIT; // } // else if(power_data.remote_power_switch == app_open()) // { // power_button.state = BUTTON_STATE_INITIAL; // printf("Long press for short duration"); // } // else // break; // // case BUTTON_STATE_LONG_PRESS_WAIT: // if (power_data.remote_power_switch == app_open())// 检测按键释放 // { // power_button.state = BUTTON_STATE_INITIAL; // printf("Release the button"); // } // default: // power_button.state = BUTTON_STATE_INITIAL; // break; // } //} // 输出处理函数 static void powerOutput(void *signal_id) { (void)signal_id; // 根据当前状态,控制各个设备的电源 switch (power_data.current_state) { case POWER_PRE_CHARGE: publishMessage(&power_data.pre_charge_finish, 1);//发布预充完成信号,100ms发送一次,直到预充完成 un_inf_can_kgf_output1.bit_data.KGF01 = setPowerOff(); // E3 un_inf_can_kgf_output1.bit_data.KGF02 = setPowerOn(); // 导航仪 un_inf_can_kgf_output1.bit_data.KGF03 = setPowerOff(); // 导航仪 un_inf_can_kgf_output1.bit_data.KGF05 = setPowerOn(); // 交换机、路由器 un_inf_can_kgf_output1.bit_data.KGF06 = setPowerOn(); // 交换机、路由器 un_inf_can_kgf_output1.bit_data.KGF07 = setPowerOn(); // 计算机 un_inf_can_kgf_output1.bit_data.KGF08 = setPowerOn(); // 计算机 un_inf_can_kgf_output1.bit_data.KGF09 = setPowerOn(); // 上装 un_inf_can_kgf_output1.bit_data.KGF11 = setPowerOn(); // 上装 un_inf_can_kgf_output1.bit_data.KGF12 = setPowerOn(); // 待定 // un_inf_can_kgf_output1.bit_data.KGF13 = setPowerOn(); // 前左右红灯 // un_inf_can_kgf_output1.bit_data.KGF14 = setPowerOn(); // 后左右红灯 un_inf_can_kgf_output1.bit_data.KGF15 = setPowerOn(); // 网络摄像头 un_inf_can_kgf_output1.bit_data.KGF16 = setPowerOn(); // 遥控器 break; case POWER_NEUTRAL: publishMessage(&power_data.pre_charge_finish, 1);//20250316增加,发送空挡信号,保证电机控制器高压上电后,发送空挡信号 un_inf_can_kgf_output1.bit_data.KGF01 = setPowerOff(); // E3 un_inf_can_kgf_output1.bit_data.KGF02 = setPowerOn(); // 导航仪 un_inf_can_kgf_output1.bit_data.KGF03 = setPowerOff(); // 导航仪 un_inf_can_kgf_output1.bit_data.KGF05 = setPowerOn(); // 交换机、路由器 un_inf_can_kgf_output1.bit_data.KGF06 = setPowerOn(); // 交换机、路由器 un_inf_can_kgf_output1.bit_data.KGF07 = setPowerOn(); // 计算机 un_inf_can_kgf_output1.bit_data.KGF08 = setPowerOn(); // 计算机 un_inf_can_kgf_output1.bit_data.KGF09 = setPowerOn(); // 上装 un_inf_can_kgf_output1.bit_data.KGF11 = setPowerOn(); // 上装 un_inf_can_kgf_output1.bit_data.KGF12 = setPowerOn(); // 待定 // un_inf_can_kgf_output1.bit_data.KGF13 = setPowerOn(); // 前左右红灯 // un_inf_can_kgf_output1.bit_data.KGF14 = setPowerOn(); // 后左右红灯 un_inf_can_kgf_output1.bit_data.KGF15 = setPowerOn(); // 网络摄像头 un_inf_can_kgf_output1.bit_data.KGF16 = setPowerOn(); // 遥控器 break; case POWER_STANDBY: // 初始状态,只开启基本设备 un_inf_can_kgf_output1.bit_data.KGF01 = setPowerOff(); // E3 un_inf_can_kgf_output1.bit_data.KGF02 = setPowerOn(); // 导航仪 un_inf_can_kgf_output1.bit_data.KGF03 = setPowerOff(); // 导航仪 un_inf_can_kgf_output1.bit_data.KGF05 = setPowerOn(); // 交换机、路由器 un_inf_can_kgf_output1.bit_data.KGF06 = setPowerOn(); // 交换机、路由器 un_inf_can_kgf_output1.bit_data.KGF07 = setPowerOn(); // 计算机 un_inf_can_kgf_output1.bit_data.KGF08 = setPowerOn(); // 计算机 un_inf_can_kgf_output1.bit_data.KGF09 = setPowerOn(); // 上装 un_inf_can_kgf_output1.bit_data.KGF11 = setPowerOn(); // 上装 un_inf_can_kgf_output1.bit_data.KGF12 = setPowerOn(); // 待定 // un_inf_can_kgf_output1.bit_data.KGF13 = setPowerOn(); // 前左右红灯 // un_inf_can_kgf_output1.bit_data.KGF14 = setPowerOn(); // 后左右红灯 un_inf_can_kgf_output1.bit_data.KGF15 = setPowerOn(); // 网络摄像头 un_inf_can_kgf_output1.bit_data.KGF16 = setPowerOn(); // 遥控器 break; case POWER_WORKING: // 工作状态,除预充继电器外所有设备开启 un_inf_can_kgf_output1.bit_data.KGF01 = setPowerOff(); // E3 un_inf_can_kgf_output1.bit_data.KGF02 = setPowerOn(); // 导航仪 un_inf_can_kgf_output1.bit_data.KGF03 = setPowerOff(); // 导航仪 un_inf_can_kgf_output1.bit_data.KGF05 = setPowerOn(); // 交换机、路由器 un_inf_can_kgf_output1.bit_data.KGF06 = setPowerOn(); // 交换机、路由器 un_inf_can_kgf_output1.bit_data.KGF07 = setPowerOn(); // 计算机 un_inf_can_kgf_output1.bit_data.KGF08 = setPowerOn(); // 计算机 un_inf_can_kgf_output1.bit_data.KGF09 = setPowerOn(); // 上装 un_inf_can_kgf_output1.bit_data.KGF11 = setPowerOn(); // 上装 un_inf_can_kgf_output1.bit_data.KGF12 = setPowerOn(); // 待定 // un_inf_can_kgf_output1.bit_data.KGF13 = setPowerOn(); // 前左右红灯 // un_inf_can_kgf_output1.bit_data.KGF14 = setPowerOn(); // 后左右红灯 un_inf_can_kgf_output1.bit_data.KGF15 = setPowerOn(); // 网络摄像头 un_inf_can_kgf_output1.bit_data.KGF16 = setPowerOn(); // 遥控器 break; case POWER_EMERGENCY: // 急停状态,断开高压 un_inf_can_kgf_output1.bit_data.KGF01 = setPowerOff(); // E3 un_inf_can_kgf_output1.bit_data.KGF02 = setPowerOn(); // 导航仪 un_inf_can_kgf_output1.bit_data.KGF03 = setPowerOff(); // 导航仪 un_inf_can_kgf_output1.bit_data.KGF05 = setPowerOn(); // 交换机、路由器 un_inf_can_kgf_output1.bit_data.KGF06 = setPowerOn(); // 交换机、路由器 un_inf_can_kgf_output1.bit_data.KGF07 = setPowerOn(); // 计算机 un_inf_can_kgf_output1.bit_data.KGF08 = setPowerOn(); // 计算机 un_inf_can_kgf_output1.bit_data.KGF09 = setPowerOn(); // 上装 un_inf_can_kgf_output1.bit_data.KGF11 = setPowerOn(); // 上装 un_inf_can_kgf_output1.bit_data.KGF12 = setPowerOn(); // 待定 // un_inf_can_kgf_output1.bit_data.KGF13 = setPowerOn(); // 前左右红灯 // un_inf_can_kgf_output1.bit_data.KGF14 = setPowerOn(); // 后左右红灯 un_inf_can_kgf_output1.bit_data.KGF15 = setPowerOn(); // 网络摄像头 un_inf_can_kgf_output1.bit_data.KGF16 = setPowerOn(); // 遥控器 break; case POWER_SLEEP: // 休眠状态,关闭所有设备 un_inf_can_kgf_output1.bit_data.KGF01 = setPowerOff(); // E3 un_inf_can_kgf_output1.bit_data.KGF02 = setPowerOn(); // 导航仪 un_inf_can_kgf_output1.bit_data.KGF03 = setPowerOff(); // 导航仪 un_inf_can_kgf_output1.bit_data.KGF05 = setPowerOn(); // 交换机、路由器 un_inf_can_kgf_output1.bit_data.KGF06 = setPowerOn(); // 交换机、路由器 un_inf_can_kgf_output1.bit_data.KGF07 = setPowerOn(); // 计算机 un_inf_can_kgf_output1.bit_data.KGF08 = setPowerOn(); // 计算机 un_inf_can_kgf_output1.bit_data.KGF09 = setPowerOn(); // 上装 un_inf_can_kgf_output1.bit_data.KGF11 = setPowerOn(); // 上装 un_inf_can_kgf_output1.bit_data.KGF12 = setPowerOn(); // 待定 // un_inf_can_kgf_output1.bit_data.KGF13 = setPowerOn(); // 前左右红灯 // un_inf_can_kgf_output1.bit_data.KGF14 = setPowerOn(); // 后左右红灯 un_inf_can_kgf_output1.bit_data.KGF15 = setPowerOn(); // 网络摄像头 un_inf_can_kgf_output1.bit_data.KGF16 = setPowerOn(); // 遥控器 break; default: break; } publishMessage(&power_data, 1); publishMessage(&un_inf_can_kgf_output1, 1); // publishMessage(&un_inf_can_kgf_output2, 1); } static void wakeupProcess(void *signal_id) { (void)signal_id; un_gather_output.bit_data.sleep_duration = (uint16_t)getParam("sleepTm"); un_gather_output.bit_data.wakeup_interval = (uint16_t)getParam("wakeTm"); if(un_gather_output.bit_data.sleep_duration < 5)//最小值限定 { un_gather_output.bit_data.sleep_duration = 5; } if(un_gather_output.bit_data.wakeup_interval < 5)//最小值限定 { un_gather_output.bit_data.wakeup_interval = 5; } un_gather_output.bit_data.vehicle_mode = power_data.current_state; publishMessage(&un_gather_output, 1); timerStart(&power_data.timer1, 500, 1); //周期调用 } // 定时器处理函数 static void powerTimerProcess(void *signal_id) { (void)signal_id; // 调用电源按钮处理函数 // handlePowerButton(); // 电源按钮状态有变化,保存到参数 // if (power_button.is_power_on != power_button.old_is_power_on) // { // setParam("pwr_btn", (float)power_button.is_power_on); // power_button.old_is_power_on = power_button.is_power_on; // } // 状态转换逻辑 switch (power_data.current_state) { case POWER_PRE_CHARGE: if (!power_data.timer_pre_charge.active) // 预充时间到 { power_data.current_state = POWER_NEUTRAL; // 工作 power_data.pre_charge_finish = 1; // 预充完成 printf("Power: Transitioning from PRE_CHARGE to POWER_NEUTRAL state\n"); } break; case POWER_NEUTRAL://20250316增加,发送空挡信号 if (power_data.neutral_cnt >= 5) // 运行5次 { power_data.neutral_cnt = 0; power_data.current_state = POWER_WORKING; // 工作 power_data.pre_charge_finish = 1; // 预充完成 printf("Power: Transitioning from POWER_NEUTRAL to WORKING state\n"); } else { power_data.neutral_cnt ++; power_data.current_state = POWER_NEUTRAL; // 空挡 power_data.pre_charge_finish = 1; // 预充完成 } break; case POWER_STANDBY: power_data.current_state = POWER_WORKING; // 休眠 20251005 修改不需要休眠,直接上电 // if (power_data.high_voltage_switch == app_open()) // 高压开关断开 // { // power_data.current_state = POWER_SLEEP; // 休眠 // printf("Power: Transitioning from STANDBY to SLEEP state\n"); // } // else if (power_button.is_power_on == app_close() && power_data.emergency_stop == app_close()) // 遥控器电源开关闭合且急停开关闭合 // { // power_data.current_state = POWER_EMERGENCY; // 急停 // printf("Power: Transitioning from STANDBY to EMERGENCY state\n"); // } break; case POWER_WORKING: // if (power_data.high_voltage_switch == app_open()) // 高压开关断开 // { // power_data.current_state = POWER_SLEEP; // 休眠 // printf("Power: Transitioning from STANDBY to SLEEP state\n"); // if (power_data.emergency_stop == app_close()) // 急停开关闭合 { power_data.current_state = POWER_EMERGENCY; // 急停 printf("Power: Transitioning from WORKING to EMERGENCY state\n"); printf("emergency_stop_switch: %d, remote_emergency_stop: %d\n", power_data.emergency_stop_switch, power_data.remote_emergency_stop); //打印状态 printf("remote_stop: %d\n", un_remote_control_input.bit_data.switch_b); } break; case POWER_EMERGENCY: // if (power_data.high_voltage_switch == app_open()) // 高压开关断开 // { // power_data.current_state = POWER_SLEEP; // 休眠 // printf("Power: Transitioning from EMERGENCY to SLEEP state\n"); // } // else if (power_button.is_power_on == app_open()) // 遥控器电源开关断开 //20251005 修改不需要电源开关 // { // power_data.current_state = POWER_STANDBY; // 待机 // printf("Power: Transitioning from EMERGENCY to STANDBY state\n"); // } if (power_data.emergency_stop == app_open()) // 急停断开 { power_data.current_state = POWER_PRE_CHARGE; // 预充 timerStart(&power_data.timer_pre_charge, (uint32_t)(getParam("prCTime") * 1000), 1); // 启动预充定时器 printf("Power: Transitioning from EMERGENCY to PRE_CHARGE state\n"); } break; case POWER_SLEEP: if (power_data.high_voltage_switch == app_close()) // 高压开关闭合 { power_data.current_state = POWER_STANDBY; // 待机 printf("Power: Transitioning from SLEEP to STANDBY state\n"); } break; default: power_data.current_state = POWER_STANDBY; // 待机 break; } powerOutput(NULL); // 输出 // 电源状态有变化,记录到参数 if (power_data.old_state != power_data.current_state) { power_data.old_state = power_data.current_state; setParam("pwr_sta", (float)power_data.current_state); } timerStart(&power_data.timer, 100, 1); //周期调用 } // 处理所有输入信号的函数 static void powerInput(void *signal_id) { //不能直接赋值,用memcpy PowerSystem old_data; memcpy(&old_data, &power_data, sizeof(PowerSystem)); // 填充数据 power_data.emergency_stop_switch = 0;//急停开关 20251005 修改四轮四转车无急停开关以及采集模块 高压 power_data.high_voltage_switch = 1;//高压开关 if (signal_id == &un_sw_sample) { // power_data.emergency_stop_switch = (uint8_t)un_sw_sample.bit_data.emergency_stop_switch;//急停开关 // power_data.high_voltage_switch = (uint8_t)un_sw_sample.bit_data.High_voltage_switch;//高压开关 } else if ( (signal_id == &un_remote_control_input) && (1 == un_remote_control_input.bit_data.enable) )// 遥控器断线,不更新数据 { power_data.remote_power_switch = (uint8_t)un_remote_control_input.bit_data.switch_d; // 遥控器电源开关 power_data.remote_emergency_stop = ((uint8_t)un_remote_control_input.bit_data.switch_b == 1) ? 0 : 1;// 遥控器急停开关 } // 急停开关 power_data.emergency_stop = (uint8_t)( (power_data.emergency_stop_switch == app_close()) || (power_data.remote_emergency_stop == app_close()) ); // 急停开关有变化,记录到参数 if (power_data.old_emergency_stop != power_data.emergency_stop) { power_data.old_emergency_stop = power_data.emergency_stop; setParam("stop_sw", (float)power_data.emergency_stop); } // 高压开关状态有变化,记录到参数 if (power_data.old_high_voltage_switch != power_data.high_voltage_switch) { power_data.old_high_voltage_switch = power_data.high_voltage_switch; setParam("high_sw", (float)power_data.high_voltage_switch); } } // APP模块的初始化 void powerAppInit(void) { // 初始化变量 memset(&power_data, 0, sizeof(PowerSystem)); power_data.current_state = POWER_STANDBY; // 初始化时恢复电源状态 power_data.current_state = (PowerState)getParam("pwr_sta"); power_data.old_state = power_data.current_state; // 恢复电源按钮状态 power_button.is_power_on = (uint8_t)getParam("pwr_btn"); power_button.old_is_power_on = power_button.is_power_on; //恢复高压开关状态 power_data.high_voltage_switch = (uint8_t)getParam("high_sw"); power_data.old_high_voltage_switch = power_data.high_voltage_switch; //恢复急停开关状态 power_data.emergency_stop = (uint8_t)getParam("stop_sw"); power_data.old_emergency_stop = power_data.emergency_stop; un_inf_can_kgf_output1.bit_data.KGF01 = setPowerOff(); // E3 初始化上电默认打开 un_inf_can_kgf_output1.bit_data.KGF02 = setPowerOn(); // 导航仪 un_inf_can_kgf_output1.bit_data.KGF03 = setPowerOff(); // 导航仪 un_inf_can_kgf_output1.bit_data.KGF05 = setPowerOn(); // 交换机、路由器 un_inf_can_kgf_output1.bit_data.KGF06 = setPowerOn(); // 交换机、路由器 un_inf_can_kgf_output1.bit_data.KGF07 = setPowerOn(); // 计算机 un_inf_can_kgf_output1.bit_data.KGF08 = setPowerOn(); // 计算机 un_inf_can_kgf_output1.bit_data.KGF09 = setPowerOn(); // 上装 un_inf_can_kgf_output1.bit_data.KGF11 = setPowerOn(); // 上装 un_inf_can_kgf_output1.bit_data.KGF12 = setPowerOn(); // 待定 un_inf_can_kgf_output1.bit_data.KGF13 = setPowerOn(); // 前左右红灯 un_inf_can_kgf_output1.bit_data.KGF14 = setPowerOn(); // 后左右红灯 un_inf_can_kgf_output1.bit_data.KGF15 = setPowerOn(); // 网络摄像头 un_inf_can_kgf_output1.bit_data.KGF16 = setPowerOn(); // 遥控器 // 订阅输入信号 subscribe(&un_sw_sample, powerInput); // 急停开关、高压开关 subscribe(&un_remote_control_input, powerInput); // 遥控器电源开关 // 定时器 timerInit(&power_data.timer); subscribe(&power_data.timer, powerTimerProcess); timerStart(&power_data.timer, 500, 1); // 周期调用 //定时器唤醒 timerInit(&power_data.timer1); subscribe(&power_data.timer1, wakeupProcess); timerStart(&power_data.timer1, 500, 1); // 周期调用 //预充定时器 timerInit(&power_data.timer_pre_charge); subscribe(&power_data.timer_pre_charge, powerTimerProcess); printf("app_power: initial OK\n"); }