1.12 Cubemx_STM32F4 步进电机(四)----S曲线理论

一年前做过的S型曲线加减速算法,再次做的时候竟然犯错,在此总结记录一下,方便以后查阅,同时希望帮助初学者提供简单的参考资料(注:本项目采用的带细分的驱动器,MCU的OC比较输出模块产生50%的PWM方波)。

S型曲线的的方程,在[-5,5]的图形如下图所示:

在这里插入图片描述

如要将此曲线应用在步进电机的加、减速过程中,需要将方程在XY坐标系进行平移,同时对曲线进行拉升变化:
在这里插入图片描述

其中的A分量在y方向进行平移,B分量在y方向进行拉伸,ax+b分量在x方向进行平移和拉伸。

项目中加速过程:从5600Hz加速到64000Hz,采用4细分。输出比较模块所用的定时器驱动频率为10M,采用1000个点进行加速处理。最终根据项目的需要,在加速过程中采用的曲线方程为:
在这里插入图片描述

其中的Fcurrent为length(1000)个点中的单个频率值。Fmin起始频率为5600; Fmax为最大频率64000; -flexible*(i - num)/num是对S型曲线进行拉伸变化,其中flexible代表S曲线区间(越大代表压缩的最厉害,中间(x坐标0点周围)加速度越大;越小越接近匀加速。理想的S曲线的取值为4-6),i是在循环计算过程中的索引,从0开始,num为 length/2 大小(这样可以使得S曲线对称)。在项目中i的区间[0,1000), num=1000/2=500。这些参数均可以修改。提供的计算接口如下。
对应的计算接口code:

/*  calculate the Period and Freq array value, fill the Period value into the Period register during the timer interrupt.

*calculate the acceleration procedure , a totally 1000 elements array.

* parameter    fre[]: point to the array that keeps the freq value.

*   period[]: point to the array that keeps the timer period value.

*   len: the procedure of acceleration length.it is best thing to set the float number, some compile software maybe transfer error if set it as a int

*   fre_max: maximum speed, frequency vale.

*   fre_min: start minimum speed, frequency vale.  mind : 10000000/65535 = 152, so fre_min can't less than 152.

*   flexible:  flexible value. adjust the S curves

*/

void CalculateSModelLine(float fre[],  unsigned short period[],   float  len,  float fre_max,  float fre_min, float flexible)
{
    int i=0;
    float deno ;
    float melo ;
    float delt = fre_max-fre_min;
    for(; i<len; i++)
    {
        melo = flexible * (i-len/2) / (len/2);
        deno = 1.0 / (1 + expf(-melo));   //expf is a library function of exponential(e) 
        fre[i] = delt * deno + fre_min;
        period[i] = (unsigned short)(10000000.0 / fre[i]);    // 10000000 is the timer driver frequency
    }
    return ;
}

通过CalculateSModelLine接口得到如下不同的几条加速曲线:
在这里插入图片描述

黄色:CalculateSModelLine(Freq, Period, 1000, 56000, 16000, 4);
橙色:CalculateSModelLine(Freq, Period, 1000, 64000, 500, 8);
蓝色:CalculateSModelLine(Freq, Period, 1000, 64000, 500, 15);
灰色:CalculateSModelLine(Freq, Period, 1000, 40000, 500, 5);

最后可以估算加速过程的时间和角位移,以橙色曲线为例:CalculateSModelLine(Freq, Period, 1000, 64000, 500, 8)为例(假设在中断中没有 if(CountForAcc++ > 2) 条件限制):

时间:Period第一个点的值为10000000/500 = 20000,最后也点的值 10000000/64000=156,平均值为10000左右,timer中断的平均时间Tn=10000/10000000=1ms, 1000个点,总时间为1s,当然,起始频率大加速时间就越短,比如Fmin=16000Hz,Fmax=64000,则40ms左右即可完成加速过程。

角位移:1.8(单步) * 1000(步数) / 4(细分)= 450°

上述为加速过程,减速同样的道理,只要将方程改为:
在这里插入图片描述

可以得到减速曲线如下所示:
在这里插入图片描述

转载来自CSDN:
https://blog.csdn.net/android_lover2014/article/details/53512143
代码见下一章节

  • 0
    点赞
  • 16
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值