Accelstepper 库中的参数计算公式

要完全调试电机的各个参数,例如最大速度,加速度等,就需要了解其计算公式和原理。

以下是代码注释:

/// This code uses speed calculations as described in 
/// "Generate stepper-motor speed profiles in real time" by David Austin 
/// http://fab.cba.mit.edu/classes/MIT/961.09/projects/i0/Stepper_Motor_Speed_Profile.pdf or
/// http://www.embedded.com/design/mcus-processors-and-socs/4006438/Generate-stepper-motor-speed-profiles-in-real-time or
/// http://web.archive.org/web/20140705143928/http://fab.cba.mit.edu/classes/MIT/961.09/projects/i0/Stepper_Motor_Speed_Profile.pdf

/// with the exception that AccelStepper uses steps per second rather than radians per second
/// (because we dont know the step angle of the motor)
/// An initial step interval is calculated for the first step, based on the desired acceleration
/// On subsequent steps, shorter step intervals are calculated based 
/// on the previous step until max speed is achieved.

红色部分内容可以从云盘下载:

链接:https://pan.baidu.com/s/16pFTsVpk6bxtFFilVpFYjw 
提取码:7wgc 
 

其原理来自于

A new algorithm for stepper-motor acceleration allows speed profiles to be parameterized and calculated in real time. This algorithm can run on a low-end microcontroller using only simple fixed-point arithmetic operations and no data tables. It develops an accurate approximation for the timing of a linear ramp with constant acceleration and deceleration.

一种新的步进电机加速算法允许速度分布参数化和实时计算。该算法只需要简单的定点算术运算,不需要数据表,就可以在低端微控制器上运行。它是一个精确的近似恒定速度的线性加速与减速时序。

It's commonly thought that the timing of a linear speed ramp for a stepper motor is too complex to be calculated in real time. The exact formula for the step delay is in Equation 8. The solution has been to store the ramp data in precompiled arrays, but this method is inflexible and wastes memory. The alternative has been to use a more powerful and expensive processor than otherwise needed or a high-level stepper-control IC. This article develops an accurate approximation that has been implemented in C using 24.8 fixed-point arithmetic on a mid-range PIC microcontroller.

人们普遍认为步进电机的线性速度加速的时序太过复杂,无法实时计算。步进延迟的精确公式如式8所示。解决方案是将加速数据存储在预编译的数组中,但是这种方法不灵活,而且会浪费内存。另一种选择是使用更强大、更昂贵的处理器或高级的步进控制芯片。本文开发了一种精确的近似方法,在C语言中,在中程PIC微控制器上使用了24.8的定点算法。

Motor step signals can be generated by a 16-bit timer-comparator module as commonly integrated in microcontrollers. On the PIC, the CCP (capture/compare/pwm) performs this function. It allows steps to be timed to the resolution of one timer period. Each step advances the motor by a constant increment, typically 1.8 degrees on a hybrid stepper motor.

电机步进信号可以由一个16位的时间比较器模块产生,通常集成在微控制器中。在PIC上,CCP(捕获/比较/pwm)执行这个功能。它允许将步数计时到一个计时器周期的分辨率。每一次步进都使电机前进一个恒定的增量,在混合步进电机上通常是1.8度。

The timer frequency should be as high as possible while still allowing long delays as the motor is accelerated from stop. A timer frequency of 1MHz has been used. A maximum motor speed of 300rpm is then equivalent to a delay count of 1,000. It's necessary to have high timer resolution to give smooth acceleration at high speed.

定时器频率应该尽可能高,当电机从停止开始加速时应仍然允许长时间的延迟。定时器的频率为1MHz。最大电机转速为300rpm时,相当于延迟计数为1000。要在高速运转时,以提供平稳的加速度,需要高分辨率定时器。

Notation and basic formulas  符号和基本公式
Delay (sec) programmed by timer count c :

定时器计数c计算延时(秒)

 Equation 1

f = timer frequency (Hz). 定时器频率
Motor speed ω (rad/sec) at fixed timer count c :

定时器计数c一定时计算电机的速度ω(rad/sec) (弧度每秒)(电机的每步走过的弧度/每步时间间隔(延时)=弧度每秒)

 Equation 2

α = motor step angle (radian).电机的步进角
1rad = 180/π = 57.3deg. 1rad/sec = 30/π = 9.55rpm.

Acceleration ω ' (rad/sec2 ) from adjacent timer counts c 1 and c 2:

相邻的定时器计数c c 1和c 2计算加速度ω”(rad / sec2):(加速度=(V1-V2)  /t)

 Equation 3

Equation 3 assumes fixed-count speed (Equation 2) at the midpoint of each step interval (Equation 1), as on a linear ramp, Figure 1. Note that ω ' resolution is inversely proportional to the cube of the speed.

方程3假设在每个步进间隔(方程1)的中点处具有固定计数的速度(方程2),就像在线性加速一样,如图1所示。注意,加速度ω”分辨率与速度围成的巨型大小成反比。(分的越细分辨率越高)对速度求导就是加速度,线性速度对应恒定加速度。


Figure 1: Ramp geometry: move of m =12 steps

速度曲线,12步的速度曲线

Linear speed ramp—exact 线性速度渐变


On a linear ramp, acceleration ω ' is constant, and speed ω (t ) = ω '.t . Integration gives the motor shaft angle θ(t ):

在一个线性速度渐变过程总,加速ω”是常数,和速度ω(t) =ω'。t。集成给出了电机轴角θ(t):求积分计算出电机转过的角度。

 Equation 4

n ≥ 0 step number (real). When the shaft is at θ = n .α, (integer n ) it's time for the n th step pulse:

n≥0步数(实时)。当轴θ= n .α(整数n) 刚好是第n次脉冲:

 Equation 5

The exact timer count to program the delay between the n th and (n +1)th pulses (n ≥ 0) is:

精确定时器计数计算第n个脉冲(n≥0)与(n +1)个脉冲之间的延时为:

 Equation 6

The initial count c 0 factorizes out to give Equations 7 and 8:

初始计数c,0分解得到方程7和8:

 Equation 7(将n=0和n=1带入公式5和公式6而得)

 Equation 8

Note that c 0 sets the acceleration, proportional to (1/c 0 )2 .

注意,c0设置加速度,与(1/ c0)的比值为2。

In real-time, Equation 8 would require calculation of a square-root for each step, with the added problem of loss of precision by subtraction.

在实时情况下,方程8需要为每一步计算一个平方根,加上减法导致精度损失的问题。

Approximating linear ramp  近似线性加速
Ratio of successive exact timer counts from Equation 8:

从公式8中连续精确计时器计数的比率:

 Equation 9

Taylor series: 泰勒系列

 Equation 10

Equation 11 is the second-order approximation to Equation 9 using Equation 10:

利用方程10得方程11是方程9的二阶近似,:

 Equation 11

Equation 11 can be rearranged for faster calculation:

公式11可以重新排列,计算速度更快:

 Equation 12

Finally, we can disconnect the physical step number, i , from the step number n on a ramp from zero, to give the general-purpose ramp algorithm shown in Equation 13. Here n determines the acceleration and increments with i for constant acceleration. To ramp up from stop, ni i i =1,2, . . . :

最后,我们可以将物理步长i与一个渐进上的步长n从0断开,从而得到如式13所示的通用渐进算法。这里n决定了加速度与i增量,为恒定加速度。从停止状态开始加速,ni = i, i =1,2,…:

 Equation 13

Negative n -values give deceleration. In particular, Equation 14, with ni i – m , can be used to ramp any speed down to stop in the final steps of a move of m steps:

负n值表示减速。特别是,方程14,ni = i - m,可以用来渐变任何速度在移动m步后速度下降到停止状态:

 Equation 14

Table 1: Accuracy of the step-delay approximation

表1:步进延迟近似的值

Step n

Exact (9)

Approx (11)

Relative error

1

0.4142

0.6000

0.4485

2

0.7673

0.7778

0.0136

3

0.8430

0.8462

0.00370

4

0.8810

0.8824

0.00152

5

0.9041

0.9048

7.66E-4

6

0.9196

0.9200

4.41E-4

10

0.9511

0.9512

9.42E-5

100

0.9950

0.9950

9.38E-8

1,000

0.9995

0.9995

9.37E-11

Accuracy of approximation  

近似精度值

Table 1 shows that the approximation is accurate even at low step number n and relative error decreases with n3 . However, n =1 has a significant inaccuracy. The inaccuracy at n =1 can be handled in two ways:

从表1可以看出,即使在较低步进数n的情况下,该近似也是准确的,相对误差随着n3的增大而减小。然而,n =1有显著的不准确性。n =1处的误差可以用两种方法处理:

  • Treat n =1 as a special case. Using c 1 0.4056 c 0 compensates for the inaccuracies at the start of the ramp and allows Equation 7 to be used to calculate c 0.
  • 将n =1作为特殊情况处理。使用c1 0.4056 c0可以补偿加速开始时的误差,并允许使用公式7来计算c0。
  • Ignore the inaccuracy. In place of Equation 7 use Equation 15:
  • 忽略错误。用方程15代替方程7:

 Equation 15

The first alternative gives an almost perfect linear ramp. The second alternative starts with a fast step. This is to the good, as it helps keep the motor moving between step pulses 0 and 1-and establishes the angle error needed to generate torque. It also allows a wider range of accelerations to be generated with a 16-bit timer and has the advantage of simplicity. It's therefore recommended to ignore the inaccuracy at n =1.

第一种方案提供了一个近乎完美的线性加速。第二种方案以一个快速步进开始。也算还可以,因为它有助于保持电机在步进脉冲0和1之间移动,并建立产生扭矩所需的角度误差。它还允许使用16位定时器生成更大范围的加速,并且具有简单性的优点。因此,建议忽略n =1时的不准确性。


Figure 2: Stepper-motor speed ramp 步进电机速度曲线

 


Figure 3: Start of ramp detail 加速开始时的细节

 


Figure 4: End of ramp detail 加速结束时的细节

Figures 2 through 4 compare the options for a target ramp from 0 to 120rpm in 1sec. For clarity, step changes in speed are shown, calculated from Equation 2. The true profile should be close to a straight line.

图2到图4比较了1秒内从0转到120转的目标加速的选项。为清楚起见,由公式2计算速度的步进变化显示出来。真实的轮廓应该接近一条直线。

2.c /(4.n +1) in Equation 12 could be approximated by c /2.n . Some effects would be:

式12c /(4.n +1)可近似为c /2.n .有如下影响:

  • The algorithm would still produce a linear ramp. 这个算法仍然会产生一个线性渐变。
  • c0 would be closer to the “exact” value shown in Equation 7: 88.6% instead of 67.6% for the same ramp acceleration.
  • c0将更接近公式7:88.6%所示的“精确”值,而不是相同渐变加速度下的67.6%。
  • A single equation like Equation 13 could no longer be used for both acceleration and deceleration.
  • 像方程13这样的单一方程不能再同时用于加速和减速。

Changes of acceleration  加速度的变化
From Equations 4 and 5 we can obtain an expression for the step number n as a function of speed and acceleration:

由式(4)和(5)可以得到步长n作为速度和加速度的函数的表达式:

 Equation 16

Thus the number of steps needed to reach a given speed is inversely proportional to the acceleration:

因此,达到给定速度所需的步数与加速度成反比:

 Equation 17

This makes it possible to change the acceleration at a point on the ramp by changing the step number n in the ramp algorithm Equation 13. Moreover, using signed ω ' values results in signed n -values that behave correctly in the algorithm. Only ω ' = 0 needs special handling.

这使得通过改变加速算法方程13中的步数n来改变加速过程中某一点的加速度成为可能。此外,使用带有ω的值得出带有n值的表达式。只有ω= 0需要特殊处理。

The n -value given by Equation 17 is correct for tn . However cn represents an average for the interval tn .. tn +1 . Equation 17 is usually adequate, but it's more accurate to add a half-step to n -values for use in the ramp algorithm:

方程17给出的n值对tn是正确的。而cn表示区间tn的平均值。tn + 1。方程17通常是足够的,但在加速算法中,向n个值添加半步更准确:

 Equation 18

The numerical example shown in Table 2 changes acceleration from 10 to 5 and to -20rad/sec2 from step 200. Complex speed profiles can be built up piecewise in this way.

表2所示的数值示例将加速度从第10步更改为第5步,并从第200步更改为-20rad/sec2步。复杂的速度轮廓可以用这种方法分段建立。

Table 2: Acceleration changes 加速度变化

Step i

ni

ci (13)

ω ' (3)

notes

198

198

2,813.067

 

199

199
398.5
-100.25

2,806.008

10

10.(199+.5) =
5.(398.5+.5) =
-20.(-100.25+.5)

200

399.5

2,803.498

5

 

201

400.5

2,799.001

5

200

-99.25

2,820.180

-20

 

201

-98.25

2,834.568

-20

Deceleration ramp  加速渐变
For a short move of m steps, where the up-ramp at ω '1 meets the down-ramp at ω '2 before max speed is reached, the step number m at which to start decelerating is, from Equation 17:

短程的m步,在加速ω1满足,加速在ω2之前达到最高速度,开始减速的步骤数m,从方程17可得:

 Equation 19

ω '1 = acceleration, ω '2 = deceleration (positive). Round n to integer and calculate cn .. cm-1 using Equation 14.

ω1 =加速度,ω2 =减速(正值)。将n四舍五入为整数,计算cn ..cm-1用方程14可得。

In other cases, Equations 17 or 18 can be used to calculate the number of steps n 2 needed to stop at deceleration ω '2 , given that the present speed was reached at step n 1 with acceleration ω '1 . Round n 2 to integer and calculate cm-n2 .. cm-1 using Equation 14.

在其他情况下,方程17或18可以用来计算在减速度为ω2时,停下来的步数n 2,鉴于目前的速度与加速度在ω '1时达到n1步。计算cm-n2....cm-1用方程14表示。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章