收到跑堂寄的F3套件有些时间了,不过最近忙得一踏糊涂,没有时间研究,不过跑堂交待的作业还是得完成。所以今天就。。。。。
先感谢下跑堂啊,以下省略千言万语。。。。。。
好了,申请的时候就说是用于数控系统的,数控系统中当然得控制电机了,今天玩个用F3控制BLDC吧,跟以前的F1系统比下速度
时间不多,这里只例出两个关键的东西:1、增量式PID控制。 2、BLDC换向控制
有兴趣的同志自己移植一下,肯定可以用哈,比方说自己做个电动车什么之类的哈木有问题的啊。
1、增量式PID控制:
int IncPIDCalc(int NextPoint)
{
int iError, iIncpid; //int32_t
//当前误差
iError = sptr->SetPoint - NextPoint;
//增量计算
iIncpid = sptr->Proportion * iError //E[k]项 比例项
- sptr->Integral * sptr->LastError //E[k-1]项 积分项
+ sptr->Derivative * sptr->PrevError; //E[k-2]项 微分项
//存储误差,用于下次计算
sptr->PrevError = sptr->LastError;
sptr->LastError = iError;
//返回增量值
return(iIncpid);
}
2、
switch(irq_zhuanzi)
{
//=================================================
case 0x05: //W->V 5(正确)
{
/* Next step: Step 2 Configuration ---------------------------- */
/* Channel1 configuration */
TIM_CCxCmd(TIM1, TIM_Channel_1, TIM_CCx_Disable); //
TIM_CCxNCmd(TIM1, TIM_Channel_1, TIM_CCxN_Disable);
/* Channel2 configuration */
TIM_SelectOCxM(TIM1, TIM_Channel_2, TIM_OCMode_PWM1 );
TIM_CCxCmd(TIM1, TIM_Channel_2, TIM_CCx_Disable);
TIM_CCxNCmd(TIM1, TIM_Channel_2, TIM_CCxN_Enable); //V6
/* Channel3 configuration */
TIM_SelectOCxM(TIM1, TIM_Channel_3, TIM_OCMode_PWM1 );
TIM_CCxCmd(TIM1, TIM_Channel_3, TIM_CCx_Enable); //V5
TIM_CCxNCmd(TIM1, TIM_Channel_3, TIM_CCxN_Enable);
//PutChar(40,230,irq_zhuanzi+48,White,Black);
TIM_GenerateEvent(TIM1, TIM_EventSource_COM); //软件设置引起COM事件
};
break;
case 0x01: //U->V 3 1
{
/* Next step: Step 2 Configuration ---------------------------- */
/* Channel1 configuration */
TIM_SelectOCxM(TIM1, TIM_Channel_1, TIM_OCMode_PWM1 );
TIM_CCxCmd(TIM1, TIM_Channel_1, TIM_CCx_Enable); //V1
TIM_CCxNCmd(TIM1, TIM_Channel_1, TIM_CCxN_Enable);
/* Channel2 configuration */
TIM_SelectOCxM(TIM1, TIM_Channel_2, TIM_OCMode_PWM1 );
TIM_CCxCmd(TIM1, TIM_Channel_2, TIM_CCx_Disable);
TIM_CCxNCmd(TIM1, TIM_Channel_2, TIM_CCxN_Enable); //V6
/* Channel3 configuration */
TIM_CCxCmd(TIM1, TIM_Channel_3, TIM_CCx_Disable);
TIM_CCxNCmd(TIM1, TIM_Channel_3, TIM_CCxN_Disable);
//PutChar(40,230,irq_zhuanzi+48,White,Black);
TIM_GenerateEvent(TIM1, TIM_EventSource_COM); //软件设置引起COM事件
};
break;
case 0x03: //U->W 2 3
{
/* Next step: Step 2 Configuration ---------------------------- */
/* Channel1 configuration */
TIM_SelectOCxM(TIM1, TIM_Channel_1, TIM_OCMode_PWM1 );
TIM_CCxCmd(TIM1, TIM_Channel_1, TIM_CCx_Enable); //V1
TIM_CCxNCmd(TIM1, TIM_Channel_1, TIM_CCxN_Enable);
/* Channel2 configuration */
TIM_CCxCmd(TIM1, TIM_Channel_2, TIM_CCx_Disable);
TIM_CCxNCmd(TIM1, TIM_Channel_2, TIM_CCxN_Disable);
/* Channel3 configuration */
TIM_SelectOCxM(TIM1, TIM_Channel_3, TIM_OCMode_PWM1 );
TIM_CCxCmd(TIM1, TIM_Channel_3, TIM_CCx_Disable);
TIM_CCxNCmd(TIM1, TIM_Channel_3, TIM_CCxN_Enable); //V2
//PutChar(40,230,irq_zhuanzi+48,White,Black);
TIM_GenerateEvent(TIM1, TIM_EventSource_COM); //软件设置引起COM事件
};
break;
case 0x02: //V->W 6 2
{
/* Next step: Step 2 Configuration ---------------------------- */
/* Channel1 configuration */
TIM_CCxCmd(TIM1, TIM_Channel_1, TIM_CCx_Disable);
TIM_CCxNCmd(TIM1, TIM_Channel_1, TIM_CCxN_Disable);
/* Channel2 configuration */
TIM_SelectOCxM(TIM1, TIM_Channel_2, TIM_OCMode_PWM1 );
TIM_CCxCmd(TIM1, TIM_Channel_2, TIM_CCx_Enable); //V3
TIM_CCxNCmd(TIM1, TIM_Channel_2, TIM_CCxN_Enable);
/* Channel3 configuration */
TIM_SelectOCxM(TIM1, TIM_Channel_3, TIM_OCMode_PWM1 );
TIM_CCxCmd(TIM1, TIM_Channel_3, TIM_CCx_Disable);
TIM_CCxNCmd(TIM1, TIM_Channel_3, TIM_CCxN_Enable); //V2
//PutChar(40,230,irq_zhuanzi+48,White,Black);
TIM_GenerateEvent(TIM1, TIM_EventSource_COM); //软件设置引起COM事件
};
break;
case 0x06: //V->U 4 6
{
/* Next step: Step 2 Configuration ---------------------------- */
/* Channel1 configuration */
TIM_SelectOCxM(TIM1, TIM_Channel_1, TIM_OCMode_PWM1 );
TIM_CCxCmd(TIM1, TIM_Channel_1, TIM_CCx_Disable);
TIM_CCxNCmd(TIM1, TIM_Channel_1, TIM_CCxN_Enable); //V4
/* Channel2 configuration */
TIM_SelectOCxM(TIM1, TIM_Channel_2, TIM_OCMode_PWM1 );
TIM_CCxCmd(TIM1, TIM_Channel_2, TIM_CCx_Enable); //V3
TIM_CCxNCmd(TIM1, TIM_Channel_2, TIM_CCxN_Enable);
/* Channel3 configuration */
TIM_CCxCmd(TIM1, TIM_Channel_3, TIM_CCx_Disable);
TIM_CCxNCmd(TIM1, TIM_Channel_3, TIM_CCxN_Disable);
//PutChar(40,230,irq_zhuanzi+48,White,Black);
TIM_GenerateEvent(TIM1, TIM_EventSource_COM); //软件设置引起COM事件
};
break;
case 0x04: //W->U 5 4
{
/* Next step: Step 2 Configuration ---------------------------- */
/* Channel1 configuration */
TIM_SelectOCxM(TIM1, TIM_Channel_1, TIM_OCMode_PWM1 );
TIM_CCxCmd(TIM1, TIM_Channel_1, TIM_CCx_Disable);
TIM_CCxNCmd(TIM1, TIM_Channel_1, TIM_CCxN_Enable); //V4
/* Channel2 configuration */
TIM_CCxCmd(TIM1, TIM_Channel_2, TIM_CCx_Disable);
TIM_CCxNCmd(TIM1, TIM_Channel_2, TIM_CCxN_Disable);
/* Channel3 configuration */
TIM_SelectOCxM(TIM1, TIM_Channel_3, TIM_OCMode_PWM1 );
TIM_CCxCmd(TIM1, TIM_Channel_3, TIM_CCx_Enable); //V5
TIM_CCxNCmd(TIM1, TIM_Channel_3, TIM_CCxN_Enable);
//PutChar(40,230,irq_zhuanzi+48,White,Black);
TIM_GenerateEvent(TIM1, TIM_EventSource_COM); //软件设置引起COM事件
};
break;
//================================================
}
好了,关键代码完毕,像接霍尔啊,控制板硬件设计啊网上查吧。希望大家都可以玩转啊
据本人测试,F3比F1要流畅很多啊,当然就控制电机的话不太明显,在系统中多跑些个任务,比方说给他多整点乘除运算,再加个LCD刷就直观了
|
|