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.

we must have the capability to regulate current. If a step is issued in a spot in time such that the position is too far off. we need less energy to reach the target position and the vibrations should be minimized. One option is to limit the current to such an amount that it reduces the vibrations considerably. Current regulation is then easily obtained by measuring current flowing through a SENSE resistor as shown in Figure 2. resonance effects can be observed. A better solution is to eliminate the vibrations by decreasing the distance the rotor must cover on a step-bystep basis. if this current is not dynamically modulated with load changes.8 degrees.8 degrees per generated step. it is crucial to eliminate both scenarios from the design. Current regulation To induce multiple microsteps embedded into a single full-step. Motors are built to accommodate a step resolution. A 200-step motor moves 1. an even worse threat. then the distance traveled is less than 1.Click on image to enlarge. A commanded full-step and its angular position oscillations before settling. . The great majority of integrated H-bridges commercially available have some means to achieve this goal. If somehow we can divide each step into several microsteps. Unfortunately. when operating a stepper motor. as the speed and position accuracy are heavily compromised. the system suffers from step loss. Both vibration and loss of torque are highly undesirable traits for any motor actuator. With smaller step motions. Therefore. Figure 1.

If VREF is modulated. When the winding current gives off a voltage such that it is larger than the supplied VREF. which is directly proportional to the winding current. the winding current is then also modulated.9 degrees. If we now reverse the same sequence. the motor moves in the opposing direction. in order to minimize losses.8 degrees.Click on image to enlarge. so we are measuring the actual winding current. LO-HI and LO-LO. so will the stator magnetic field change. . the motor moves in one direction. A SENSE resistor in series with the motor winding gives off the dropped voltage. This reference voltage is provided by the application. But if the same motor is commutated with full current and half current. These combinations are: HI-LO. where HI implies current is regulated to IMAX and LO implies current is regulated to –IMAX. Figure 2. For example. If we follow this sequence. as shown in Figure 3. the H-bridge is enabled again. then each step is 0. as the resistor is fairly small. the H-bridge is disabled for a fixed amount of time. We need to amplify the voltage. which in turn controls the rotor position. 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. HI-HI. The SENSE resistor is in series with the motor winding. is repeated ad infinitum. We can keep subdividing the current by as much as we want and we will always obtain even smaller step sizes or larger resolutions. giving us a regulated current. If the current magnitude changes. then each step is 1. The amplified version is then compared against a reference voltage (VREF). This is how we achieve microstepping. if a 200 step stepper is commutated with full current. The process to disable the H-bridge as current reaches an ITRIP target. we can then control the stator magnetic field strength. After this time elapses. By controlling the winding current. The voltage drop across this resistor is amplified by some known gain.

Figure 3. For example. . which gives us microstepping commutation. you would then coordinate the amount of time in between steps. or HI-LO to move it backwards. you would issue a commutation polarity of LO-HI to move the motor one step forward. The typical quadrature pattern used to full-step commutate a bipolar stepper motor.Click on image to enlarge. Its interrupt service routine (ISR) can then be used to update the phases according to the desired direction of rotation. A way of generating these steps is to use an internal timer resource configured to count time intervals. 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. Figure 4 shows this mechanism. we add a current component on top of the full-step commutation. Speed is then the inverse of this time with the unit of measurement being the step per second (SPS). if the current step is at quadrature position HI-HI. By adding the VREF magnitude modulation to the H-bridge circuitry.

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

But. Take for example our eight degrees of microstepping scenario. If the falling edge is registered. In other words. I often refer to this anding element as the INDEX_MASK. 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). Every time such a transition is registered. whereas bits below the MSB are used as lookup table index to extract current magnitude information. the index increment is set to +1. It can be a timer input capture or a general purpose input output (GPIO) configured to interrupt. the motor then moves counterclockwise. then the motor moves clockwise. falling edge. if we let the INDEX go from 0 to twice the TABLE_DEPTH. then the table space needs to be four times as large as the number of current settings we want per step. TABLE_DEPTH is equal to 16. If a rising edge is registered. if we walk forward through the table. If the table value being fetched for PHASE A is the element #15. In this case. but what about the phase information? Should we store this in the table as well? If so. it can be any form of hardware input which grants an interrupt on a transition. as this would be outside of the table space. we can then use the resulting number’s most significant bit information as the PHASE polarity. 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 don’t care about wrap up on the INDEX value when deriving PHASE information as the meaningful most significant bit (MSB) will always be toggling. Here we describe the logistics behind the code we will implement to use the lookup table. or both. bits above the MSB are completely ignored. we do not want to fetch element #23 for PHASE B. We have discussed how to fetch the current magnitude information from the lookup table. The INDEX value goes from 0 to the TABLE_DEPTH – 1 value. this is not necessary as the PHASE information can be derived from the INDEX variable itself. We want to fetch element #7. A second ISR takes care of the rotation direction request. right? Actually. but we will let index run from zero to 31. except in this case it must react to both rising and falling edges. but if we walk backwards through the table. The truth is there are times when we will not want to increment the index. . Again. but actually want to decrement it.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.#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. Anding the lookup table index by the table size minus one results in the correct normalized value. In other words. You must choose whether you want the step command to be recognized on a rising edge. the code specified above will be executed. #define TABLE_DEPTH 16 #define INDEX_MASK TABLE_DEPTH . The reason for this is that by walking through the lookup table we can also derive direction of rotation information. A hardware ISR is used to acknowledge the step command. then the index increment is set to –1.

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.As you can see. < 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. the levels of flexibility are appealing to this kind of implementation. He received a B. 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.ti.com. . 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. You can use any wave shape the design requires. 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. Because microstepping is achieved with software rather than hardware. with the amount of code executed per microstep being fairly small.S. Degrees of microstepping can grow from small resolution to very high resolution. the level of simplicity to turn your stepper driver into a microstepping commutator is considerably simple.

you would need to step twice as fast than on 256 degrees.com/litv/zip/slvc365 That link contains more information such as actual Gerber Files. 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. to reach faster speeds at 512 degrees of microstepping.ti. whereas the first app note consisted of as much as 256 degrees of microstepping.com/general/docs/lit/getliterature. Why? So that I can decrease the amount of time needed to compute each consecutive step. 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. I placed that information on the actual lookup table. I then made a few changes and recoded the application which was released as our DRV8829EVM module.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. As you can imagine. and EVM schematic. The main difference is that instead of deriving phasing polarity information from the actual microstep index. You can find more information on the DRV8829EVM here: http://www.Thanks for your interest on this topic and reading the article. The second implementation is a little bit different than the algorithm on the first implementation for which this article was tailored. Thanks for reading the article! There is an application note I wrote on this topic you can download from here: http://focus. Why? The other difference is that the DRV8829EVM is programmed to do 512 degrees of microstepping. firmware source code for an MSP430F2617 MCU. .ti. If the links provided in my first reply are still lacking on the content you would like to study further. 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. for each step? It seems more reasonable for me. 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. this is the same as a step function which translates into much more energy than the one needed to actuate the rotor into position. motor inductance. 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. Hope these info helps. . To the eyes of the motor. Depending on the application voltage. Reply JoseQuinones 7/21/2011 8:41 AM EDT Hi Fabio Prudente. winding current and desired motor speed. FULL STEP is like amplifying your sine/cosine waveforms into saturation giving you two square waves with 90 degrees out of phase. The idea behind microstepping is to eliminate the effects of vibration caused by using too much energy on a per step basis. you will start to see vibrations causing less or more problems as you move across the spectrum. Best regards. I’ll try to answer your question as best as possible. Yes. Thanks for taking the time to go through my article. In essence. from 0 to 100% of the full current.

and in practice sine/cosine wave forms can give rather non-uniform micro steps. However. etc. The techniques presented work fine for low speeds though. .Do note that sine/cosine wave patterns are not the only possible wave shapes you can utilize. which may or may not be important in a given application. 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 . Firstly. and regulating the current through such a load. They all offer different responses and effects will vary from application to application. 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.0. Real motors deviate from that ideal.you need to be able to measure the angular position of the shaft to a accuracy of 100th of a full step . square root of sine. trapezoidal. the sine/cosine table is theoretically correct. there are a couple of complications/irritations in practice.018 degrees for a typical motor . Reply cmhicks 7/15/2011 4:11 AM EDT This is a good technique that I have been using for a few years. You could as well fill your lookup table with any other wave shape you desire.or so) and calculate optimised waveforms on a case-by-case basis. to give micro-steps of equal angles for an idealised motor model. I have tried sine squared. The second irritation is that a stepper motor is a highly inductive load. triangular. as long as the 90 degrees out of phase relationship is preserved.

Sign up to vote on this title
UsefulNot useful