黄工无刷电机学习
直播中

赵辉

13年用户 952经验值
私信 关注
[问答]

关于S型曲线加减速算法的知识点不看肯定后悔

关于S型曲线加减速算法的知识点不看肯定后悔

回帖(1)

盛方圆

2021-10-28 11:18:04
一年前做过的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     {
        melo = flexible * (i-len/2) / (len/2);
        deno = 1.0 / (1 + expf(-melo));   //expf is a library function of exponential(e)
        fre = delt * deno + fre_min;
        period = (unsigned short)(10000000.0 / fre);    // 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°
上述为加速过程,减速同样的道理,只要将方程改为:
可以得到减速曲线如下所示:



举报

更多回帖

发帖
×
20
完善资料,
赚取积分