/********************************** * 文件名称: low_task.c * 功能描述: 低优先级任务模块 * 功能: 处理电机启停、速度调整、用户按键输入和定时任务 **********************************/ #include "main.h" #include "low_task.h" #include "led_app.h" // 全局变量 u16 hz_100_cnt = 0; // 100Hz计数器 uint8_t motor_start_stop = 0; // 电机启停状态(0:停止,1:启动) uint8_t motor_start_stop_pre = 1; // 电机启停状态前值,用于检测状态变化 /** * @brief 电机启动函数 * @note 初始化FOC算法,设置速度参考值,使能PWM输出 */ void motor_start(void) { // 初始化FOC算法 foc_algorithm_initialize(); // 设置速度参考值为20.0转/秒 Speed_Ref = 20.0F; // 清除速度闭环标志 speed_close_loop_flag = 0; // 设置q轴电流参考值 Iq_ref = 0.0f; // // 初始化电机角度增量 // hall_angle_add = 0.0005f; // // // 初始化电机速度 // hall_speed = 0.0f; SHUTDOWN_EN(); // 使能PWM输出 TIM_CtrlPWMOutputs(PWM_TIM, ENABLE); // 通知LED应用电机启动 motor_state_callback(1); //motor_run_display_flag = 1; // 显示标志(已注释) } /** * @brief 电机停止函数 * @note 禁用PWM输出,通知LED应用电机停止 */ void motor_stop(void) { // 禁用PWM输出 TIM_CtrlPWMOutputs(PWM_TIM, DISABLE); SHUTDOWN_OFF(); // 通知LED应用电机停止 motor_state_callback(0); //motor_start_stop_pre = motor_start_stop; // 保存状态 /* if(rtY.EKF[2]<140.0f) { GPIO_ResetBits(GPIOC, GPIO_Pin_9); TIM_CtrlPWMOutputs(PWM_TIM, DISABLE); motor_start_stop_pre = motor_start_stop; motor_run_display_flag = 0; } else { rtspeed_ref = 20.0F; if(motor_start_stop == 1) { motor_start_stop_pre = 0; } else { motor_start_stop_pre = 1; } } */ } /** * @brief 低优先级控制任务 * @note 处理电机启停、速度调整和用户按键输入 */ void low_control_task(void) { // 只有电流偏移量获取完成后才执行控制逻辑 if(get_offset_flag == 2) { // 检测电机启停状态变化 if(motor_start_stop_pre != motor_start_stop) { // 保存状态前值 motor_start_stop_pre = motor_start_stop; // 根据状态执行相应操作 if(motor_start_stop == 1) { motor_start(); // 启动电机 } else { motor_stop(); // 停止电机 } } } // 处理KEY1按键事件,切换电机启停状态 if(key1_flag == 1) { if(motor_start_stop == 0) { motor_start_stop = 1; // 启动电机 } else { motor_start_stop = 0; // 停止电机 } key1_flag = 0; // 清除按键标志 } // 处理KEY2按键事件 if(key2_flag == 1) { Speed_Ref += 5.0f; // 速度参考值增加5.0转/秒 Iq_ref += 0.2; key2_flag = 0; // 清除按键标志 } // 处理KEY3按键事件 if(key3_flag == 1) { Speed_Ref -= 5.0f; // 速度参考值减少5.0转/秒 Iq_ref -= 0.2; key3_flag = 0; // 清除按键标志 } } /** * @brief SysTick中断处理函数 * @note 实现100Hz定时任务,每10ms执行一次低优先级控制任务 */ void SysTick_Handler(void) { Temp = calculate_temperature(ADC_TO_RT(ADC3ConvertedValue[ADC3_TEMP_INDEX])); //rtspeed_ref = 20.0F; // 速度参考值(已注释) // 执行速度PID计算 Speed_Pid_Calc(Speed_Ref, Speed_Fdk, &Speed_Pid_Out, &Speed_Pid); // 增加100Hz计数器 hz_100_cnt++; // 每10ms执行一次低优先级控制任务 if(hz_100_cnt == 10) { // 调用LED应用的tick函数,传递10ms的计时信息 led_app_tick(10); // 调用LED应用的update函数,更新LED状态 led_app_update(); // communication_handle(); // 通信处理(已注释) // 执行低优先级控制任务 low_control_task(); // 减少延时计数器 TimingDelay_Decrement(); // 重置计数器 hz_100_cnt = 0; } }