Você está na página 1de 10

A simple algorithm for microstepping a bipolar stepper motor

Jose Quinones, Texas Instruments 7/11/2011 10:48 AM EDT

Here's a simple algorithm that uses conventional microcontroller blocks to control commercially available H-bridges to properly commutate a bipolar stepper motor through a microstepping profile. Bipolar stepper motors offer a simple way of achieving position control and accurate speed actuation without the need to close the loop through shaft encoders or similar means. To improve performance, we can employ a technique known as microstepping in which a sine wave current wave shape is embedded into the typical fullstep commutation wave form. This article details a simple algorithm utilizing conventional microcontroller blocks to control commercially available H-bridges to properly commutate a bipolar stepper motor through a microstepping profile. Stepper motors are an excellent motion actuator because they move in steps. This gives us two inherent advantages: 1) position can be easily obtained and maintained by moving a number of steps and then stopping; and 2) an accurate speed can be obtained by properly scheduling the steps in a timely manner. As a result, steppers can stop at a given angular position and hold that position against external load changes, and at the same time the motor speed can be maintained even when the system undergoes changes in power supply voltage. Whereas other motor topologies could not achieve any of these two feats without the proper amount of closed loop control, the stepper excels at both without the need for any form of closed loop. However, stepper motors are not perfect, and there are areas in which their performance is severely affected. The most crucial of these inefficacies is resonance, or a vibration induced by the generation of subsequent steps at a time in which further motion is exacerbated. Figure 1 below illustrates what happens to the angular position as a full-step is generated. As the rotor is scheduled to land on the next step, 1.8 degrees away from the current position, it actually oscillates around this angular region before settling at the target. What if we scheduled a step when the position is farther away or closer to the next step position? When this happens, the distance traveled by the rotor will be much more or much less than what it would have been had the rotor started from the goal position. Actuation at these speeds is what causes the motor to vibrate and loose torque. It is very easy to see where the vibration-inducing speeds lie on a particular motor, if you slowly accelerate the motor from a slow speed to a higher speed. You will notice the regions where vibrations increase and decrease as the speed is ramped up.

Click on image to enlarge.

Figure 1. A commanded full-step and its angular position oscillations before settling. If a step is issued in a spot in time such that the position is too far off, resonance effects can be observed. Both vibration and loss of torque are highly undesirable traits for any motor actuator. Therefore, when operating a stepper motor, it is crucial to eliminate both scenarios from the design. One option is to limit the current to such an amount that it reduces the vibrations considerably. Unfortunately, if this current is not dynamically modulated with load changes, the system suffers from step loss, an even worse threat, as the speed and position accuracy are heavily compromised. A better solution is to eliminate the vibrations by decreasing the distance the rotor must cover on a step-bystep basis. Motors are built to accommodate a step resolution. A 200-step motor moves 1.8 degrees per generated step. If somehow we can divide each step into several microsteps, then the distance traveled is less than 1.8 degrees. With smaller step motions, we need less energy to reach the target position and the vibrations should be minimized.

Current regulation
To induce multiple microsteps embedded into a single full-step, we must have the capability to regulate current. The great majority of integrated H-bridges commercially available have some means to achieve this goal. Current regulation is then easily obtained by measuring current flowing through a SENSE resistor as shown in Figure 2.

Click on image to enlarge.

Figure 2. A SENSE resistor in series with the motor winding gives off the dropped voltage, which is directly proportional to the winding current. The SENSE resistor is in series with the motor winding, so we are measuring the actual winding current. The voltage drop across this resistor is amplified by some known gain. We need to amplify the voltage, as the resistor is fairly small, in order to minimize losses. The amplified version is then compared against a reference voltage (VREF). When the winding current gives off a voltage such that it is larger than the supplied VREF, the H-bridge is disabled for a fixed amount of time. After this time elapses, the H-bridge is enabled again. The process to disable the H-bridge as current reaches an ITRIP target, is repeated ad infinitum, giving us a regulated current. This reference voltage is provided by the application. If VREF is modulated, the winding current is then also modulated. This is how we achieve microstepping. If the current magnitude changes, so will the stator magnetic field change. By controlling the winding current, we can then control the stator magnetic field strength, which in turn controls the rotor position. For example, if a 200 step stepper is commutated with full current, then each step is 1.8 degrees. But if the same motor is commutated with full current and half current, then each step is 0.9 degrees. We can keep subdividing the current by as much as we want and we will always obtain even smaller step sizes or larger resolutions.

Microstepping commutation
Bipolar stepper motors are often commutated with full-steps by coordinating each winding phase current in one of the four possible pattern combinations, as shown in Figure 3. These combinations are: HI-LO, HI-HI, LO-HI and LO-LO, where HI implies current is regulated to IMAX and LO implies current is regulated to IMAX. If we follow this sequence, the motor moves in one direction. If we now reverse the same sequence, the motor moves in the opposing direction.

Click on image to enlarge.

Figure 3.

The typical quadrature pattern used to full-step commutate a bipolar stepper motor.

Rotor position is controlled by counting the number of steps being issued with regards to a known starting point. To set up the stepping speed, you would then coordinate the amount of time in between steps. Speed is then the inverse of this time with the unit of measurement being the step per second (SPS). A way of generating these steps is to use an internal timer resource configured to count time intervals. Its interrupt service routine (ISR) can then be used to update the phases according to the desired direction of rotation. For example, if the current step is at quadrature position HI-HI, you would issue a commutation polarity of LO-HI to move the motor one step forward, or HI-LO to move it backwards. By adding the VREF magnitude modulation to the H-bridge circuitry, we add a current component on top of the full-step commutation, which gives us microstepping commutation. Figure 4 shows this mechanism.

Click on image to enlarge.

Figure 4. VREF modulation information is embedded on top of the full-step commutation, resulting in microstepping commutation being applied to the bipolar stepper motor windings. Notice the PHASE information is always a positive value. However, when PHASE is HI, the motor winding current is positive, whereas when PHASE is LO, motor current is negative. Hence, the digital signal PHASE (also called DIRECTION on some H-bridges) gives us current direction, not current magnitude. Current magnitude is obtained by modulating VREF. In the following example, we embedded half of a sine wave shape into the VREF terminal. In essence, you can use any continuous wave shape as long as it offers soft motion. Sine waves are industry standard, but they are not a requirement. Designers are encouraged to try any other wave shape that can offer good results. This level of flexibility is one of the main advantages of using a microcontroller or digital signal processor (DSP) to achieve microstepping. The result of embedding VREF information on top of phase information is an AC signal, in this case a sine wave used to commutate each stepper motor winding. So how do we generate the VREF information? To generate VREF we use some form of DAC module. Since bipolar stepper motors are made of two windings, two DAC channels are required. If a real DAC is not available, a high-speed pulse-width modulation (PWM) output with a low-pass filter can be used to create a crude, but equally useful, programmable analog voltage. The analog magnitudes into this analog output come from an internal lookup table storing the wave shape we have determined to be appropriate for our application. Every time a step is issued, said value is fetched from the lookup table and into the DAC register. There are a few key notes you must have in mind when it comes to generating and using this lookup table. First is the lookup table depth. This table needs to hold as many elements as twice the number of current settings. That is, if you want to divide each full-step eight times (eight microsteps), you need eight current settings, and the table will be 16 steps wide. This takes care of 180 degrees worth of information. The other 180 degrees comes from reusing the table in its entirety, but for the alternate polarity. That is, we use the table for positive current and then the same table for negative current. In actuality, to control any bipolar stepper we need two signals as there are two phases: PHASE A and PHASE B. Luckily, if PHASE A is a sine wave, PHASE B is the same sine wave, but with 90 degrees worth of phasing. In other words, if PHASE A is a sine, PHASE B is the cosine. It may seem like we need two tables, but the very same table can be reused. As the 16-step deep table is used to generate the PHASE A sine wave, we can offset the lookup table index by eight and fetch the values that generate the cosine wave shape. For example, it looks like this in pseudocode:

#define TABLE_DEPTH 16 VREF_PHASEA = LOOKUPTABLE[INDEX] VREF_PHASEB = LOOKUPTABLE[(INDEX + TABLE_DEPTH / 2) & (TABLE_DEPTH-1)] Increase INDEX

Notice that for the PHASE B index we must normalize it to the table size. If the table value being fetched for PHASE A is the element #15, we do not want to fetch element #23 for PHASE B, as this would be outside of the table space. We want to fetch element #7. Anding the lookup table index by the table size minus one results in the correct normalized value. I often refer to this anding element as the INDEX_MASK. We have discussed how to fetch the current magnitude information from the lookup table, but what about the phase information? Should we store this in the table as well? If so, then the table space needs to be four times as large as the number of current settings we want per step, right? Actually, this is not necessary as the PHASE information can be derived from the INDEX variable itself. The INDEX value goes from 0 to the TABLE_DEPTH 1 value. But, if we let the INDEX go from 0 to twice the TABLE_DEPTH, we can then use the resulting numbers most significant bit information as the PHASE polarity. Take for example our eight degrees of microstepping scenario. In this case, TABLE_DEPTH is equal to 16, but we will let index run from zero to 31. This changes the index masking strategy as we need a mask for the phase information (PHASE_MASK) and another mask for the lookup table fetching procedure (INDEX_MASK). The pseudo code now looks like this:

#define PHASE_MASK 0x10 PHASEA = INDEX & PHASE_MASK PHASEB = (INDEX + TABLE_DEPTH / 2) & PHASE_MASK
Notice that we dont care about wrap up on the INDEX value when deriving PHASE information as the meaningful most significant bit (MSB) will always be toggling. In other words, bits above the MSB are completely ignored, whereas bits below the MSB are used as lookup table index to extract current magnitude information.

#define TABLE_DEPTH 16 #define INDEX_MASK TABLE_DEPTH - 1 VREF_PHASEA = LOOKUPTABLE[INDEX & INDEX_MASK] VREF_PHASEB = LOOKUPTABLE[(INDEX + TABLE_DEPTH / 2) & (INDEX_MASK)] INDEX = INDEX + IndexIncrement
We changed the way in which the index is incremented. The truth is there are times when we will not want to increment the index, but actually want to decrement it. The reason for this is that by walking through the lookup table we can also derive direction of rotation information. In other words, if we walk forward through the table, then the motor moves clockwise, but if we walk backwards through the table, the motor then moves counterclockwise. Here we describe the logistics behind the code we will implement to use the lookup table. A hardware ISR is used to acknowledge the step command. It can be a timer input capture or a general purpose input output (GPIO) configured to interrupt. You must choose whether you want the step command to be recognized on a rising edge, falling edge, or both. Every time such a transition is registered, the code specified above will be executed. A second ISR takes care of the rotation direction request. Again, it can be any form of hardware input which grants an interrupt on a transition, except in this case it must react to both rising and falling edges. If a rising edge is registered, the index increment is set to +1. If the falling edge is registered, then the index increment is set to 1.

As you can see, the level of simplicity to turn your stepper driver into a microstepping commutator is considerably simple, with the amount of code executed per microstep being fairly small.

Simple fix
Using a microcontroller or a DSP to embed microstepping information on the conventional full-step commutation algorithm is a simple mechanism to fix the resonance problem. Because microstepping is achieved with software rather than hardware, the levels of flexibility are appealing to this kind of implementation. You can use any wave shape the design requires. Degrees of microstepping can grow from small resolution to very high resolution, with up to thousands degrees of microstepping being used on applications requiring a very soft motion profile. Jose Quinones is a motor control applications engineer at Texas Instruments where he is involved in aiding developers in implementing design based on power devices for implementing microstepping such as the DRV8812/8813 and DRV8824/25. He received a B.S. in electrical engineering from the University of Puerto Rico and spent five years developing motion control embedded systems for Xerox Corporation (for which he has two patents) before joining TI. Jose can be reached at ti_josequinones@list.ti.com. < 3 of 3 >

(7)
inShare

Dr DSP

7/14/2011 3:24 PM EDT I like the technique and would hope these is a complete design available for an evaluation board. Why no link to one? Reply

goafrit

7/16/2011 8:40 AM EDT I think it is very fascinating but they need to provide some more links Reply

JoseQuinones

7/20/2011 8:45 AM EDT Hi Goafrit,

Thanks for your interest on this topic and reading the article. If the links provided in my first reply are still lacking on the content you would like to study further, please let me know and I'll try to point you in the right direction. Reply

JoseQuinones

7/19/2011 5:49 PM EDT Hi Dr DSP,

Thanks for reading the article! There is an application note I wrote on this topic you can download from here:

http://focus.ti.com/general/docs/lit/getliterature.tsp?literatureNumber=slva416&fileType=pdf

This application note details a few more ideas behind this algorithm and provides related hardware suggestions in the form of an schematic and the layout utilized to build a board revolving around these ideas.

I then made a few changes and recoded the application which was released as our DRV8829EVM module. You can find more information on the DRV8829EVM here:

http://www.ti.com/litv/zip/slvc365

That link contains more information such as actual Gerber Files, firmware source code for an MSP430F2617 MCU, and EVM schematic.

The second implementation is a little bit different than the algorithm on the first implementation for which this article was tailored. The main difference is that instead of deriving phasing polarity information from the actual microstep index, I placed that information on the actual lookup table. Why? So that I can decrease the amount of time needed to compute each consecutive step. Why?

The other difference is that the DRV8829EVM is programmed to do 512 degrees of microstepping, whereas the first app note consisted of as much as 256 degrees of microstepping. As you can imagine, to reach faster speeds at 512 degrees of microstepping, you would need to step twice as fast than on 256 degrees. Hence, reducing code execution helps quite a bit. Anybody can argue that at higher speeds there is no need to have so many degrees of microstepping, but am not trying to start a religious discussion here.

Do note whether we employ 512 or 256 degrees of microstepping is all in the code, so the first app note could be made to work with 512 degrees of microstepping as well.

Hope these info helps. Best regards,

Jose Quinones Reply

Fabio.Prudente

7/14/2011 4:53 PM EDT Do I really need a sine/cosine table?

What would happen if I simply implement a linear interpolation, from 0 to 100% of the full current, for each step? It seems more reasonable for me. Reply

JoseQuinones

7/21/2011 8:41 AM EDT Hi Fabio Prudente,

Thanks for taking the time to go through my article. Ill try to answer your question as best as possible.

Yes, you can use a 100% current magnitude and phase commutation information table, but this encompasses a FULL STEP commutation scheme and it is the reason why the resonance effect is exacerbated.

The idea behind microstepping is to eliminate the effects of vibration caused by using too much energy on a per step basis. In essence, FULL STEP is like amplifying your sine/cosine waveforms into saturation giving you two square waves with 90 degrees out of phase. To the eyes of the motor, this is the same as a step function which translates into much more energy than the one needed to actuate the rotor into position. Depending on the application voltage, motor inductance, winding current and desired motor speed, you will start to see vibrations causing less or more problems as you move across the spectrum.

Do note that sine/cosine wave patterns are not the only possible wave shapes you can utilize. You could as well fill your lookup table with any other wave shape you desire, as long as the 90 degrees out of phase relationship is preserved. I have tried sine squared, square root of sine, trapezoidal, triangular, etc. They all offer different responses and effects will vary from application to application. Reply

cmhicks

7/15/2011 4:11 AM EDT This is a good technique that I have been using for a few years. However, there are a couple of complications/irritations in practice, which may or may not be important in a given application.

Firstly, the sine/cosine table is theoretically correct, to give micro-steps of equal angles for an idealised motor model. Real motors deviate from that ideal, and in practice sine/cosine wave forms can give rather non-uniform micro steps. I have found that you can get closer to having micro steps of equal angular size if you measure the motor characteristics (itself non-trivial - you need to be able to measure the angular position of the shaft to a accuracy of 100th of a full step - 0.018 degrees for a typical motor - or so) and calculate optimised waveforms on a case-by-case basis.

The second irritation is that a stepper motor is a highly inductive load, and regulating the current through such a load, with a high enough bandwidth to allow smooth micro-stepped rotation at moderate to high rotational speeds is not as easy as this article makes out. The techniques presented work fine for low speeds though.

Você também pode gostar