# [Reprint] Stepper Motor principle Introduction and STM32-based SPWM Drive stepper motor, the use of software to achieve motor segmentation

Source: Internet
Author: User

I. Structure and driving principle of hybrid stepping motor

The principle of the motor this part does not want to talk about too complicated, open a motor to see the understanding.
The rotor of the motor is a permanent magnet, and it has several poles on it, which are fixed at a certain angle. The stator of the motor is a magnet consisting of several coils in series. The outgoing line is usually four lines marked as a+,a-,b+,b-. A and B phase is not through, with a multimeter is easy to distinguish out, as for each phase of the +-line is actually not considered, any phase of the positive or negative reversal of the motor will be reversed. Another kind of line is six lines only in a phase and b phase of the middle point does not make two out of the difference, six out of the motor through the middle outlet to a + or a-current to simulate a positive or negative current, can be in a circuit without negative phase current control to achieve motor drive, thereby simplifying the drive circuit, But this practice only half-phase with current at any moment, the torque of the motor is a loss. The rotation of the stepper motor is also the result of the electromagnetic pole and the permanent magnet pole force, but the polarity of the electromagnetic pole is realized by the driving circuit control.
We can make an experiment like this to get in the motor and turn it up. 1 Find a battery of positive and negative access to the end of a phase, and then disconnect, (recorded as a positive) 2 and then connect the battery to the B-phase at both ends; Then disconnect; (recorded as B positive) 3 The battery is re-connected to a phase; Then disconnect; (recorded as a negative) 4 to keep the positive and negative switching to the B phase; then disconnect; (recorded as B negative) ... In this cycle you will see that the stepper motor is turning slowly. Note that the phase resistance of the motor is very small when connected to near-short. We will record the direction of the phase current should be: a+b+a-b-a+ ..., if we replace the wiring sequence so that the phase current sequence of a+b-a-b+a+ ... At this point we will see the motor moving in the opposite direction. Over hereevery switch phase current motor will rotate a very small angle, this angle is the motor's step angle。 Step angle is an intrinsic parameter of stepping motor,General two-phase motor step angle of 1.8 degrees, that is, switching 200 times can make the motor turn one turn。 Here we compare the positive and negative current sequence can be seen A + and a-; The order and the reverse order of the B-Plus and B. are identical, that is to say, "any one phase or the reverse of the motor will be reversed." The above is four rows of work, in order to make the phase current smoother can also use eight rows of the working method namely: A +; a+b+; B +; b+a-; A; a-b-; b; B-a+, cycle forward from the rear, and reverse backwards from back to back.
In order to realize the positive and negative flow control of phase current with a single chip microcomputer must have an H bridge drive circuit, this drive module with H Bridge is still many, the cheaper is transistor H bridge such as l298n, transistor switch speed is relatively slow, can not drive the motor high-speed movement. Some modules also include subdivision control circuitry, which we do not use because our segments are controlled by software. In practical applications, the MOS-tube two-bridge drive chip with St L6205 can drive a stepper motor. With the H-bridge, the phase current can be controlled by PWM, changing the state of the input pole IN1 and IN2 (see manual page 8th) to control the direction of the phase current.

Two. Principle of subdivision and output control

From here on the point of view, other places do not see OH.
An ideal stepper motor current profile should be a sine curve with a phase difference of 90 degrees such as:
<ignore_js_op>
The Blue line is a phase a current, and the red line is the B phase current. If the positive and negative extremes of A phase as a a+a-,b phase positive and negative extremum as b+b-, compared to the four-beat way forward a+b+a-b-and inversion a+b-a-b+ it is not difficult to see the four-row method is actually a pulse to replace a sine half-cycle, the phase point from left to right change the motor is turning, from right to left motor reversal. Similar to the eight beat of our way A +; a+b+; B +; b+a-; A; a-b-; b; B-a+, put in the curve can also find the corresponding point, the plot of the phase point of each shot of the ..., it is not difficult to see the use of a+b+ instead of the 2nd beat Point with b+a-instead of the fourth beat point is approximate practice. So where is the difference between this approximate and ideal current? These currents are needlessly consumed and the excess current can cause the motor to spin out of balance. Why subdivide it? The ultimate goal of the actual subdivision is to insert several points in the sine cycle to make the phase current close to the sine change, and the subdivision can improve the positioning accuracy and the smoothness of the motor operation.
So we put aside the subdivision, if you can make two difference to 90 degrees sine wave shape is the ideal stepper motor driver, the modulated sine wave frequency is the stepper motor speed, the sine amplitude is the stepper motor torque. This may not sound like a difficult thing to do, but you should not forget that the sine of the modulation is required. The first to have a certain drive capacity stepper motor power larger drive capacity requirements are also greater. The second to be able to maintain a 90 degree difference under the premise of changing the frequency of the sine, so as to drive the motor at different speeds, the stepper motor rotation is actually two sine wave phase point sequence. The third best can be amplitude modulation, adjust the amplitude can realize the constant torque output of the motor, the actual significance of amplitude modulation is more than these back to talk about. In short a word is through the PWM modulation output can be FM amplitude modulated two-way fixed phase sine wave. (If the three-phase stepper motor should be different from the 120-degree sine wave, the principle is the same.) ）
The relationship between the above figure and the two-phase stepper motor drive may be somewhat confusing. "Is that true?", we'll arrange a trial here. We know that motors and generators are two reversible processes, so we can use stepper motors as generators. Very simple experiment, we put the stepper motor two-phase lead to the double-trace oscilloscope input, and then find a motor with a rotating shaft (I am using a hand drill to clamp the motor shaft, my hand drill can be positive and negative). To maintain a stable speed, you will see the above image on the oscilloscope: two-way fixed perfect sine wave, when the speed increases when the amplitude and frequency changes (linear relationship), and positive and reverse two sinusoidal phase position is different, if you can determine the speed, you can also verify the following cycle, speed, The relationship between the step angle.
Stepper Motor drive than the inverter, servo motor drive complex place is the need for a wide range of frequency conversion, if you can do this stepper motor drive the other two will not be a problem, at least in the waveform modulation absolutely no problems, their basic principle is through.
Below we expand the discussion of sinusoidal modulation, this part is the core part will occupy a lot of space, you rest assured I will not list a lot of math in the tutorial, otherwise how can be called Super invincible? The tutorial is super-invincible, and the driver for this stm32 is Super Invincible (ROAR). But "carrier Poppy, SPWM, dead Zone, unipolar" These words if you are unfamiliar with the words suggest you still have to see the basic concepts of the relevant chapters of the electric power electronics course.

Three. SPWM operation and output

the generation of SPWM can be divided into software methods and hardware methods, hardware method through the hardware to generate a triangular wave all the way through a comparator comparison of sine wave amplitude and triangular amplitude value of the relationship can be obtained SPWM wave. This method is also applied to many SPWM integrated chipshttp://wenku.baidu.com/view/ac55b849767f5acfa1c7cd4e.htmlThe hardware method does not require software participation in waveform generation, and is relatively simple in both frequency modulation and amplitude modulation control. The capabilities and performance of the hardware approach are dependent on the chip itself and are limited for more complex applications. The idea of the software method is that the PWM wave can get sine wave after filtering the pulse width data of SPWM, and calculate the waveform data of duty ratio, and adjust PWM according to the waveform data. In fact, soft and hard methods are not absolute, such as TI's DSP chip internal SPWM generator, his practice is to store a sine table in memory, and then with a timer clock synchronization counter positive and negative count to simulate a triangular wave, Each clock compares the value of the sine table with the trigonometric count value to get SPWM, which can actually be regarded as a semi-software semi-hardware approach. The advantages of the software approach are low cost and more flexibility, low cost needless to say, flexibility, for example: modulated sine wave polarity is achieved by the independent control bit (bipolar), if the output standard sine-wave form hard method requires a triangular wave generator and sine wave generator of the starting point of the precise alignment, This requires a phase-locked loop circuit in the implementation of the hardware circuit to ensure that the software method does not require any additional operations. Now in order to improve the drive performance of stepper motors, we hope that the polarity reversal point behind the output of a few microseconds, to do this hardware method changes is certainly difficult, and the software method only need to increase the timing lag output on the line.

In order to reduce the computational overhead, we can use the table-checking method to store the computed SPWM data in ROM and output the values in the table sequentially. This method of data calculation can be done on the PC by matlab software, the data is good to paste into the source program can be. The limitation of tabular method is the contradiction between the parameter change and the storage cost, the more complicated the parameter occupies the larger storage space.

(1) Conversion of triangular wave to sawtooth wave

when the carrier is triangular, the output is an asymmetric PWM waveform which can modulate half-period symmetric sine wave, which is called asymmetric natural sampling method. Other methods (rule sampling equivalent area ... ) are approximate methods that are used to reduce the amount of computation or to be taken. The asymmetric PWM open point does not necessarily relate to the closing point and must be output by a centrally aligned PWM mode with two updates of a cycle. Triangular waves can be seen as a combination of two sawtooth waves, so we can simplify the structure of the program by using the data of the sawtooth wave. We compare the following three images:
<ignore_js_op>
<ignore_js_op>
<ignore_js_op>

Figure 1 is a sawtooth amplitude value of 1, carrier ratio n=16, sine amplitude 0.5, sine and sawtooth wave phase is half a sawtooth wave period; Figure 2 is the result of the horizontal flipping of Figure 1, and Figure 3 is the result of the superposition of Figure 1 and Figure 2. Do you see the SPWM data for the triangular waveform in figure three? That's right, that's it. The sawtooth sine amplitude ratio is 2:1, the half-sawtooth wave period is different, and the computed data are combined into triangular wave data. The algorithm is very simple, assuming that the array is stored in the above sawtooth wave SPWM data, numbering 0~15 a total of 16, then take 0,1,2, ... 15 for triangular waveform open point output data, then reverse take 15,14,13, ... 0 is the triangle waveform close point data can be. In particular, if the carrier ratio is odd, the triangular wave is also odd, and the middle number naturally and its own combination of data is still correct. Note that the method mentioned here can convert the trigonometric waveform calculation to sawtooth waves, but it does not reduce the amount of computation, because if it is an even number of trigonometric waves as long as the calculation of the One-fourth period is enough for the other is symmetrical, and the sawtooth waveform data need to calculate half a cycle. At this point we can use the Sawtooth wave method to calculate the data output by triangle wave.
(2) SPWM iterative operation to calculate the SPWM duty ratio, the sawtooth wave Slash and the sine intersection are required first, i.e. the solution of equation kx+b=y and sin (X) =y. This equation is a transcendental equation that can only be computed by iterative methods. We turn the linear equation into x= (y-b)/k, first take an X value (this is the iteration initial value), take it into sin (X) to find a Y value and then substitute the Y value (y-b)/k for x value, then x into sin (x) for a Y value ... So repeated several times can get a result is the solution of the equation, this is called iterative method. The more iterations, the closer the iteration initial value is to the more accurate the result. Each group of data is calculated with the number of parameters 1: sine amplitude (triangular amplitude and proportional) 2: Carrier ratio n value is half period of triangular wave. In addition, the PWM duty cycle is the timer channel value is related to the PWM period value, so in order to calculate the timer channel value also need a period value, for stm32f This value is the Timer ARR register value, it determines the PWM period (or frequency). The attachment has a matlab_spwm.rar,matlab to calculate the value of the timer SPWM and the drawing of a small tool above some of the figures are drawn with it, the starting part can be set parameters s_m=32768/65536% sine wave amplitude ratio 0~1s_n=16% Half-period triangular wave numbers_pre=16384% single chip microcomputer timer modulus valueThe implementation divides into three parts, calculates the SPWM data, converts the data to the timer setting value according to the period value; drawing;
Calculate timer setting result in timersetting, copy and paste replace tab character literal comma is OK, the following is the calculation result of the above parameter: 1780 5246 8444 11221 13461 15088 16063 16384 16075 15182 13764 11893 9645 7102 4346 1463

(3) Optimization of SPWM real-time operation

four. Stepper Motor Operation Control

At this point, we can do real-time iterative operations on the microcontroller quickly. An iterative calculation of a half-period SPWM with its input parameters is only related to three values:
Amplitude of 1.M sine This value determines the phase currents size of the stepper motor, which is the output torque of the stepper motor. One of the advantages of stepper motor is its low-speed performance, when the stepper motor low-speed operation of the rotor is always subject to magnetic force traction rotation, the size of the force directly depends on the magnitude of the excitation current, very small speed can be used with a large force traction rotation. The low-speed movement of the DC motor can only be achieved by reducing the excitation current, in fact, the small torque to achieve low speed, so that control can not be very accurate especially in the start-stop phase especially troublesome. Stepper motor at high speed torque drop quickly this reason is not difficult to understand, because in the stepper motor excitation coil has a number of magnetic poles quickly across the formation of a large inductance of the electromotive force to offset the driving voltage caused by the excitation current small torque smaller. To improve the high-speed performance solution there is only one increase in operating voltage. According to the motor speed automatic adjustment phase current size can achieve a constant torque output, that is, low speed small value high speed large value. 2. The relationship between the carrier ratio N and the control ratio c, the two parameters and the modulation frequency f is:F*2C*2N=TF (TF is the clock frequency of the timer)We slowly explain this formula, modulation frequency is what we actually want the motor speed, from the above formula can be seen to let the motor speed increase there are two ways to reduce C or reduce n (TF can also be changed temporarily not considered);c is actually the modulo value of the timer (ARR), what he means is to use a few timer clock cycles to produce a PWM period, the front 2 is due to the timer working in central alignment mode, the Timer +-count round produces a full triangular wave cycle. The range of arr can not be too small, because a timer interrupt is required to update the value of the channel, too small a value of two times the update time is too short to achieve the calculation and update step count, and other operations. If the value of arr is too large, the output PWM frequency is too low to be effective. 3. N is the number of triangular waves in the carrier-to-half period, and his meaning is to use several PWM cycles to modulate a sine period, which is actually the fine fraction that we often say, which determines the number of positions that can be controlled within a sinusoidal period (a step angle). In a common drive, this value is set in advance by the dial switch, which is a fixed value in the work because it is almost impossible to adjust the granularity of a hardware circuit seamlessly. Software operations do not have this problem, the value of n can be arbitrary, the only affected is the polar control, the above equation n the 2 meaning of the front is the sine positive half period and negative half period. The value of n is also taken into account in memory and computation, and if the initial value of the iterative algorithm is close to the result, the operation efficiency will be greatly improved, therefore, for the operation with the initial value, there should be a storage space for each computing point, the value of the n is too large to consider the memory resource, and if the calculation without the initial values In particular, when the value of n is changed, the initial value will be different from the real one, so the change of n should be minimized. and stepper motor speed has a relationship between the parameters are known at the time of operation, so at any moment the speed of the motor can be calculated, if the motor can be run smoothly (no plugging or dropping the situation) is not required other speed dial device, closed-loop control is even more unnecessary. Then again, if you throw a step or a block turn the closed loop can solve it?
Five. Step value counting produces ab polarity logic and positive and negative rotation
The stepper motor drivers seen on the web are all the same for array storage IO status table output with fewer subdivisions. First to comb what has now been done, there is an array of memory in the entire sine half-cycle of the real-time operation of the SPWM data, this data is based on the current PWM period is converted, Therefore, each PWM cycle sequentially assigns the array contents to the timer channel value, which can be used to output sine-varying PWM at the timer channel PIN. In addition, one (l6205 is two, can also use non-gate) IO port to control the polarity output, such as high-level output sine negative half-week, low-level output sine positive half-week.
The next step is to arrange a logical and simple data structure that combines stepping count, subdivision, and polarity control. First we use a S32 stepcounter global amount to do step count, its numerical value and the stepper motor's real-time position corresponds, this variable is a very important variable, because at any time the AB two-phase SPWM data output point and the polarity control signal is produced by it. Assuming that we think of its low eight bits as the subdivision step count (256 is the largest subdivision), this step value will correspond to the full step position in addition to 256. In addition, a U8 microstep is used to control the subdivision step, its value is related to the current subdivision, if the 256 subdivision is microstep=1,128 subdivision microstep=2, and so on. If the motor is moving forward a micro step then stepcounter+= Microstep, if the reversal of a micro-step is stepcounter-=microstep (micro-stepping this part can be put into the interrupt program), OK positive and negative rotation is very simple, micro-step forward automatically update the whole step. The key point is how to use this count value to generate two phase polarity signal output control and a phase B SPWM data position, here to explain why you want control to be generated by this variable: Because the program is the simplest, although this is a lot of things, but in the programming implementation you see a few lines , not error-prone, most efficient, as you can imagine, if the more variables involved, the more likely it is to be considered, the more prone it is to be mistaken; it is easy to package and function to extend, for example, you want to do an ad sample value and the motor position according to a proportional synchronization of the program that the slip resistance motor to follow the small Change the ad sample value to the step value a little bit.
First of all, the output of the first data, SPWM array 256, if not consider the polarity of the data position is only related to the lower 8 bits of stepcounter, so a-phase data with stepcounter low 8 bits as a pointer from the array can be (A-phase 0 value point is the step 0 value point), B and a phase difference is 90 degrees, so a=0,1,2, ... 255,0 ... Then b=128,129,130, ... 127,128 ... Can you see that? (a data pointer +128)%256 data pointer equivalent to a ^128 is the data pointer of B.
This is written on the program:
A channel value =SPWM array [(U8) stepcounter];
B-Channel value =SPWM array [(U8) stepcounter^128];
Here the array size is 256, so a logical XOR is resolved, if you want to take the size is 100, you have to (POINT+50)%100 to find the B phase point.
The second is the polarity control, assuming that the initial a=0,b=0,c8=0,c7=0,a=0 b=0 represents a phase B positive half-cycle polarity, C8 is the value of the Stepcounter 8th bit (the lowest bit is 0), the reason is 8 is the previously mentioned condition that the lower 8 bits as a micro-step, C7 7th bit, Now Stepcounter count to 0 and 256,a phase should flip once, count to 128 and 128+256,b to flip once, thus listing the truth table to extract the logical relationship:
C8 C7 A B counter Value
0 0 0) 0 0
0 1 0) 1 128
1 0 1) 1 256
1 1 1 0 256+128
0 0 0) 0 512
A phase of logic and C8 the same, B-phase logic can you see? B-phase logic is C8 XOR or C7. The program is simple and executes every time the Stepcounter value changes:
A phase IO control bit value =setpcounter 7th bit value;
B-Phase IO control bit value =a phase IO control bit value ^setpcounter 8th bit value;
The polarity of the two sentences is updated, regardless of the forward, reverse polarity output is always right, here combined with the stm32 of the band operation is better. The above content for the subdivision of 256,128,64 ... when it is established, if n is not a 2 integer power will be more than a number of dislocation, n value (that is, the degree of subdivision) the impact is relatively large, but will not produce a polarity error.

[Reprint] Stepper Motor principle Introduction and STM32-based SPWM Drive stepper motor, the use of software to achieve motor segmentation

Related Keywords:
Related Article

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

## A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

• #### Sales Support

1 on 1 presale consultation

• #### After-Sales Support

24/7 Technical Support 6 Free Tickets per Quarter Faster Response

• Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.