打印

STMF3电机控制测试

[复制链接]
4085|3
手机看帖
扫描二维码
随时随地手机跟帖
跳转到指定楼层
楼主
5186|  楼主 | 2013-4-5 10:59 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
收到跑堂寄的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刷就直观了

沙发
hawksabre| | 2013-4-5 19:43 | 只看该作者
好的  谢谢了  楼主   谢谢你的共享   很不错   呵呵   有空需要好好玩玩    呵呵

使用特权

评论回复
板凳
hawksabre| | 2013-4-5 19:43 | 只看该作者
这帖子是原创的  必须要加精  呵呵   顶一个

使用特权

评论回复
地板
ichenqiang| | 2013-4-5 23:23 | 只看该作者
顶一个原创贴。
看了感觉对不起21IC啊,拿了100块板子中的一个,一直没空研究。唉。。。。。

使用特权

评论回复
发新帖 我要提问
您需要登录后才可以回帖 登录 | 注册

本版积分规则

11

主题

478

帖子

0

粉丝