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表示。

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