void Mclib_API_thread_entry(void* parameter) { /* Main PWM Output Enable */ TIM_CtrlPWMOutputs(TIM1,ENABLE); // Enable the Adv Current Reading during Run state SVPWM_3ShuntAdvCurrentReading(ENABLE); while (1) { switch (State) { case IDLE: // Idle state break; case INIT: MCL_Init(); delay_ms(15); State = START; break; case START: break; case RUN: // motor running if(ENC_ErrorOnFeedback() == RT_TRUE) { //LEDToggle(LED1); MCL_SetFault(SPEED_FEEDBACK); } break; case STOP: // motor stopped // shutdown power /* Main PWM Output Disable */ // TIM_CtrlPWMOutputs(TIM1, DISABLE); F40X_TurnOfflowsides(); State = WAIT; SVPWM_3ShuntAdvCurrentReading(DISABLE); SVPWM_3ShuntCalcDutyCycles(Stat_Volt_alfa_beta); delay_ms(10); break; case WAIT: // wait state if(ENC_Get_Mechanical_Speed() ==0) { State = IDLE; } break; case FAULT: if (MCL_ClearFault() == RT_TRUE) { State = IDLE; } break; default: break; } rt_thread_delay( RT_TICK_PER_SECOND/250 ); } }
/******************************************************************************* * Function Name : main * Description : Main program. * Input : None * Output : None * Return : None *******************************************************************************/ int main(void) { SystemInit(); /*速度反馈初始化*/ ENC_Init( ); /*电流反馈初始化*/ SVPWM_3ShuntInit(); /*时基初始化,并启动TIM6计算速度,初始化PID*/ TB_Init( ); Tim6_Init( ); PID_Init(&PID_Torque_InitStructure, &PID_Flux_InitStructure, &PID_Speed_InitStructure); /*温度,电压数组初始化*/ MCL_Init_Arrays(); /*交互界面初始化*/ KEYS_Init( ); LCD_Display_init(); /*-------------------*/ Res_f=1; //上电完成 //串口示波器初始化 usart_init(115200); while(1) { /*UI显示,以及电源报警,用户管理*/ Display_LCD( ); MCL_ChkPowerStage( ); KEYS_process( ); /*状态机开启运行*/ switch (State) { case IDLE: //通过sel按键进入INIT ,在WAIT和FAULT中进入IDEL break; case INIT: MCL_Init( );//初始化电机控制层 TB_Set_StartUp_Timeout(3000); State = START; break; case START: //passage to state RUN is performed by startup functions; break; case RUN: //电机运行过程中,检测速度反馈是否存在问题 if(ENC_ErrorOnFeedback() == TRUE) { MCL_SetFault(SPEED_FEEDBACK); } break; case STOP: //关闭TIM1的输出,状态转为等待,停止电流检测,设置Valpha和Vbeta为0,计算三相占空比 TIM_CtrlPWMOutputs(TIM1, DISABLE); State = WAIT; SVPWM_3ShuntAdvCurrentReading(DISABLE); Stat_Volt_alfa_beta.qV_Component1 = Stat_Volt_alfa_beta.qV_Component2 = 0; SVPWM_3ShuntCalcDutyCycles(Stat_Volt_alfa_beta); TB_Set_Delay_500us(2000); // 1 sec delay break; case WAIT: // 等待速度为零时,将状态转为IDEL if (TB_Delay_IsElapsed( ) == TRUE) { if(ENC_Get_Mechanical_Speed( ) ==0) { State = IDLE; } } break; case FAULT: //状态变为IDEL,全局变量设为第一次启动 if (MCL_ClearFault( ) == TRUE) { if(wGlobal_Flags & SPEED_CONTROL == SPEED_CONTROL) { //速度控制模式 bMenu_index = CONTROL_MODE_MENU_1; } else { //力矩控制模式 bMenu_index = CONTROL_MODE_MENU_6; } State = IDLE; wGlobal_Flags |= FIRST_START; Hall_Start_flag=0; } break; default: break; } usart_watcher(Speed_Iq_Id_watch); /********End of Usart_watch**************/ } }