Abstract
A new algorithm for calculating the acceleration of stepper motor. The algorithm can parameterize the motor speed and calculate it in real time. Using a simple fixed-point counting algorithm, the algorithm can run on low-end MCUS and does not require lookup tables. In the case of fixed acceleration, the algorithm controls the acceleration and deceleration of the motor by accurately approximating the linear trend based on time.
Pay attention to
- This is a translation.
- The link to the original article is here.
- Readers are required to understand the basic knowledge of stepper motors, such as stepping, micro-stepping, torque and so on. Please refer to here or Stepper Motor.
The body of the
It is generally considered that the linear trend between speed and time of stepper motor is too complex to be calculated in real time. Equation 8 is the calculation of step delay. One of the traditional solutions was to use arrays to store pre-computed linear relational data. However, this method is inflexible and takes up too much memory. Another solution is to use more powerful devices, such as high-end MCUS or motor control ics, which are of course more expensive. The proposed algorithm provides an implementation that can run on a mid-range PIC controller using C language and fixed-point number (24.8 format) algorithm.
The MCU usually incorporates a 16-bit clock module, which can be used to generate periodic signals to drive the motor to generate stepping (or microstepping). This relates motor stepping (and stepping time) to the clock (and clock resolution). For hybrid stepper motors, the rotation generated by a step is fixed at 1.8°(there are also 0.9, which is finer, smoother, and of course more expensive). This means that the step required for a rotation of the motor is:
That’s 200 stepping pulses that drive the motor around once.
The clock frequency should be as high as possible, and at the same time, the motor should be provided with a long enough time delay (ensure that there is a long enough time for stepping drive signal, if the signal duration is too short, the motor does not have enough energy to make action). For example, you can use a 1MHz clock. Such a motor with a maximum speed of 300 RPM (RPM) requires an equivalent delay of 1000 (i.e., 1000 cumulative clock pulses) to generate a drive signal. A higher clock frequency is necessary in order for the motor to accelerate smoothly at high speed.
Pay attention to
- For the implementation of stepper motor acceleration control, please refer to AccelStepper(C++).
Basic equations
Equation 1: time of pulse
The time required to generate C clock pulses is:
Equation 1
ƒ : Clock frequency, in Hertz (Hz) C: indicates the number of clock pulses.
example ƒ = 1MHz c= 1000, then the required time is:
Equation 2: motor speed
The speed of a motor is usually described in terms of angular velocity (since the motor’s motion mode is rotation). Angular velocity is measured in radians per second: rad/s. Within C clock pulses, the angular velocity ω equation of the motor is:
Equation 2
α: stepping Angle of motor, in radian: rad. 1 rad = 180 / π = 57.3° 1 RAD /s = 30 / π = 9.55 RPM
Equation 3: Acceleration
In the two continuous driving pulses C1 and C2, the acceleration ω ‘(rad/s2) is:
example c1=1000 c2ƒ = 2000 = 1000000 MHZThen the acceleration is:
For a linearly varying speed/time relationship, equation 3 assumes that the motor is rotating uniformly (it is not) within each step pulse interval, and that the speed is taken from the intermediate point of the interval, for the sake of calculation. As shown in Figure 1:
Figure 1: Linear relationship: Movement distance: m =12 steps.
Note that omega ‘is inversely proportional to the velocity histogram.
Equation 4: Accurate calculation of the linear trend of velocity
In a linear trend diagram of velocity, the acceleration ω ‘is constant. The velocity at some point is omega (t)= omega (t). Combined with the Angle θ(t) that the motor has turned at a certain moment, the following equation can be obtained:
Similarly, there are:
Where, n≥0 is the number of steps in the actual situation. At this point, the motor is located at the Angle θ=nα. Combining the two equations, equation 5 can be derived.
Equation 5: Step time
The time used to drive the motor for n step by step is:
Equation 6: The number of clock pulses between steps
Between the NTH (n≥0) and the NTH +1 stepping signal, the number of clock pulses at the interval is:
Equation 7: The clock pulse required for the first step
The initial clock pulse C0 is obtained from equation 7.
Equation 8: clock pulse required for the NTH step
Pay attention to c0I set the acceleration equal to phi
In practice, square root calculations are performed using equation 8 at each step, resulting in a loss of accuracy, and subtraction in the equation leads to a further loss of accuracy.
Equation 9: The linear relationship between two consecutive steps
The linear trend is approximated and the clock pulses required by the two steps before and after are compared according to Equation 8.
Equation 10: Taylor series
We will use Taylor series to approximate equation 9.
Equation 11: Approximation
Quadratic approximation is made to Equation 9 using Equation 10 to obtain Equation 11:
Note that for approximation theory, please refer here.
Equation 12
To facilitate calculation, equation 11 was transposed to obtain Equation 12:
Equation of 13
Finally, equation 12 is sorted out and a general meaning equation irrelevant to the actual physical meaning is established: Equation 13. The equation uses I to represent the NTH step starting from 0. For a given acceleration, n determines the ith change in velocity. When accelerating from rest, ni= I, I =1, 2… .
Equation (14
A negative value of n indicates deceleration. In particular, for equation 14, when ni=i-m, in a total of m steps, can be used to slow down until it stops.
Equation 15: Precision problem of approximate algorithm
The accuracy of the approximation algorithm can be seen in the following table:
Step n | Accuracy (Equation 9) | Approximation (Equation 11) | The 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.66 e-4 |
6 | 0.9196 | 0.9200 | 4.41 e-4 |
10 | 0.9511 | 0.9512 | 9.42 e-5 |
100 | 0.9950 | 0.9950 | 9.38 e-8 |
1000 | 0.9995 | 0.9995 | 9.37 e-11 |
Table 1: Accuracy of the approximation Algorithm Table 1 shows that the approximation algorithm is accurate, even when stepping n is small. And the relative error shrinks at the rate of N3. However, the error is quite large when n=1. Two methods can be used to reduce the error of n=1:
- Treat n=1 as a special case. C1 =0.4056⋅c0 was used as an imprecise starting value and equation 7 was used to calculate c0.
- Ignore the inaccuracy of n=1. Replace equation 7 with equation 15.
How to obtain the two constants 0.4056 and 0.676? Is it through trial and error, comparison, or magic?
The former method yields almost perfect linear trends. The latter method can start up faster. The advantage is that it helps the motor to run: the stepper motor relies on 0-1 pulse to generate step, thus obtaining torque, and of course angular error. It also allows for a wider range of acceleration on a 16-bit clock, and has the advantage of being simple and convenient. Therefore, it is recommended to use this approach and ignore the imprecision of **n=1** (' Is this really a good idea? `).Copy the code
Figure 2: Stepper motor speed trend
Figure 3: When the uptrend begins
Figure 4: At the end of the uptrend
Figure 2 to Figure 4 are the comparison of the acceleration of the target motor from 0 RPM to 120 RPM in 1 second by using the above methods. For clarity, the step-change in velocity is calculated and shown based on equation 2. The ideal trend should be close to a straight line.
In equation 12,Can be approximated as. The result is: – The trend of the algorithm is still linear. –c0Closer to theEquation 7According to the”The real value“: For the same acceleration,c0It was 88.6 percent of the real value, down from 66.7 percent. – Acceleration and deceleration calculations can no longer use single equations such asEquation of 13.
Equation 16: Function that computes the number of steps
From Equation 4 and Equation 5, the function of calculating step number n according to velocity and acceleration can be obtained:
Equation 17
From Equation 16, equation 17 is obtained, and it is inferred that the number of steps required to achieve a given speed is inversely proportional to acceleration:
This makes it possible to change the acceleration at some point in time: by changing the step number n in equation 13. Furthermore, the signed ω ‘value is used so that the signed step value n can be computed correctly. And the only special treatment is omega prime =0. So you can speed up and slow down.
Equation of 18
The step value n provided by equation 17 is correct for TN. However, cn itself is the interval TN… The average of tn+1. Therefore, although equation 17 is accurate enough, a more accurate result can be obtained by adding one half step to the step value n:
Table 2 shows the numerical changes caused by changing the acceleration: from 10 to 5 and then to -20 at step 200. This is a piecewise analysis of complex changes:
Step by step I | ni | ciEquation (13) | Omega ‘equation (3) | note |
---|---|---|---|---|
198 | 198 | 2813067 | ||
199 | 199 398.5 100.25 |
2806008 | 10 | |
200 | 399.5 | 2803498 | 5 | etc |
201 | 400.5 | 2799001 | 5 | Same as above |
200 | 99.25 | 2820180 | – 20 | etc |
201 | 98.25 | 2834568 | – 20 | Same as above |
Equation 19: Slow down
In the linear trend of deceleration, when travel short distances for m step, if acceleration (trend upward) at some point omega ‘1 and deceleration (trend downward) some point omega’ 2 overlap, while the motor has yet to reach maximum speed, namely, accelerate to midway need to slow down, at this point, according to the equation of the 17, steps needed to get the equation to calculate the 19 to slow down:
ω ‘1: acceleration. ω ‘2: deceleration (negative acceleration). By rounding n, cn can be calculated by applying equation 14… Cm – 1.
- For other cases, equation 17 or Equation 18 can be used to calculate: the motor reaches a certain speed after running n1 steps at some acceleration ω ‘1, and then begins to use deceleration ω’ 2 to find the number of steps required by the motor from running to stopping n2. By rounding n2 and applying equation 14, c1… Cm – 1.