Lab 6: Intro to Position Control—Stepper Motors

Lab 6: Intro to Position Control—Stepper Motors Objectives •
•
•
Learn what stepper motors are and how they work Learn to interface steppers to the DAQ Board Program the computer to drive the steppers forward, backward and at various speeds. Introduction Stepper motors are a special kind of motor designed to move in discrete steps. This can perhaps best be understood by looking at how they are designed. While there are many variations, you will get the general idea by studying a simple type of stepper called the permanent magnet stepper. Actually, the more common type is the variable reluctance stepper, but the function is basically the same and the permanent magnet version is easier to understand. Consider the diagram below. The stepper rotor is the moving part attached to the shaft. It is a permanent magnet with North (N) and South (S) poles. The stepper stator surrounds the rotor and is the stationary part of the motor. It consists of several coils of wire wound around iron laminations, which make up electromagnets that can be turned on and off. Recall that opposite magnetic poles (i.e. N‐N and S‐S) attract each other while like poles (N‐S) repel. The magnetic polarity of the stator magnets can be controlled, by controlling the current direction through the coils. Let us assume that our stepper is set up so that when we turn a coil “on” it generates a magnetic N pole closest to the rotor. Then, if we turn on coil A and leave all of the others off, it is clear that the rotor will try to line itself up with its S pole aligned with coil A, as shown in Figure 1A. If we leave coil A on, the rotor will come to rest in alignment with coil A and will not move farther. In fact, it will vigorously resist any attempt to manually move it from this position. The amount of external torque the motor can resist is called its holding torque. coil A
coil A
coil B
S
N
coil D
coil D
coil B
S
N
coil C
coil C
Figure 1A: rotor aligned with coil A
Figure 1B: rotor aligned with coil B
If we then turn off coil A and turn on coil B, the rotor will turn ¼ rotation to the right and align with coil B, as shown in Figure 1B. Continuing to rotate the magnetic field around the circle will cause the rotor to align next with coil C then coil D, etc. Each of these locations is a stable position. That is, as long as one of the coils is energized, the rotor will attempt to lock itself in alignment with that coil. Stepper Resolution The motor just described has only four stable positions, when operated in the full step mode. It is easy to envision a need to have more options than this, and steppers are available with many more than four steps per revolution. The ones you will be using in your lab have 100 steps per revolution, for example. Many of the variations on the design of the stepper have been created with an eye to improving the resolution and torque/speed characteristics of the motors. The invention of solid state electronic stepper controllers has made it possible to generate literally thousands of steps per revolution from relatively simple stepper motors. One method for doing this is called half stepping and can be done using your motors. Look again at Figure 1, and imagine that we control the coils in the following way: 1. Energize coil A and wait for the motor to come to equilibrium in alignment with coil A. 2. Without turning off coil A, energize coil B. Assuming that the two coils generate equally powerful magnetic fields, the rotor will come to rest halfway between coils A and B. It has moved exactly ½ step. 3. Now turn off coil A, leaving only coil B energized, and the rotor will move on to align itself with coil B. Microstepping The same idea can be extended by the use of electronic current controls. Suppose instead of having equal currents flowing in coil A and coil B, that the current in coil A were twice as large as the current in coil B. Now the rotor would come to rest a little more than ¼ of the way between A and B (tan‐1(0.5) = 26.5 degrees). By controlling the relative strengths of the currents between two adjacent coils, the rotor can be made to move in even smaller increments than the half steps described above. Unipolar/Bipolar Windings The motors you will use are unipolar. That means that you only have access to one end of each winding individually. Schematically, the motor windings look like Figure 2, with one wire being the common lead for all of the windings. It is possible using split power supplies and special controllers to control the direction of current flow through these motors, but nobody would bother with that. In a unipolar motor, the positive supply voltage is usually fed to the common wire, and each of the windings is then connected to a driver transistor, which acts as an on/off switch. Bipolar motors are available which provide access to both ends of each winding individually. These motors allow relatively simple H‐bridge controllers to switch the direction of the current through the windings, allowing even more flexibility in the control of the motor, and offering the possibility of achieving higher torque from a given frame size. Returning to Figure 1, suppose that while we were energizing coil A to be a North pole, we reversed the current in coil C and caused it to be a South pole. Depending on the geometry of the motor and the state of magnetic saturation of the rotor iron, additional torque might be derived from the motor in this way. Common
Lead
Coil Leads
Figure 2: Stepper coil winding Lab Exercises: Stepper Control Programs Connect your motor to the interface board in the following way. Connect the black wire to +12V. Then connect the four colored wires to four successive digital outputs (“‐“ terminals) in the following order: brown, green, red, white. Recall that each of the digital outputs provides a path to the power supply ground when the output is turned on. To turn on a coil in the stepper motor, you need only to turn on the corresponding motor output. So the command digital_out(A,1) will turn on coil A, where “A” is the output to which you have wired the brown wire. Likewise, digital_out(A+1,1) will turn on coil B through the green wire, etc. Use the digital output commands to turn on and off the coils in the proper sequence to get the stepper to move. Try this using Command Line inputs. Don’t forget to turn off coil A after turning on coil B, etc. When the motor is in a stable position, try to turn the shaft with your hand. The motor will resist surprisingly hard for a little motor. The torque required to force the motor to move away from a stable position is called the holding torque and is one of the primary specifications for a stepper motor. Program 1: Forward and Reverse Motion Knowing that there are 100 full steps per revolution of the motor, write a program that will drive the motor one revolution clockwise and one revolution counterclockwise. Use the full‐ stepping mode. Put a piece of tape on the motor shaft as a “flag” to help you see how far the shaft has gone. Start by making a flow chart of your programming logic for making the program work. Hint: Don’t forget to turn off the coils at the appropriate times. Also, don’t forget that it will take a finite amount of time for the stepper to move from one point to another, and that it will move much slower than the computer can run through the program. You must insert some time delay in the appropriate places or the motor will not be able to keep up. If the motion is erratic or it appears that steps are being skipped, you either have the coils out of sequence or you need to insert more time. You can insert time delay by using the Matlab command pause(t ) where t is the amount of time in seconds you want the program to wait before executing the next step. Hint: You will probably want to use a for loop for this task. The general form of your loop will be A = address; coil = A; for step = 1:100 digital_out(coil,1); pause(p); coil = coil + 1; if coil >= A+4; coil = A; end end % address is the output number for coil A % default step size is 1 unless you specify it otherwise %coil is the output address for thecurrent coil % p is the amount of time to pause between steps %resets coil back to A for the cycle % end if statement % end forward for loop NOTE THAT THIS CODE WILL NOT WORK AS‐IS BECAUSE WE NEVER TURN OFF ANY OF THE COILS. It is just given here to help you get started. Modify it to include turning off the correct coil at the appropriate time. Also note that the code above only counts up. You must add your own second loop or modify this one to make the motor go backwards to its starting point. Also note that there are multiple ways to do this and the one shown here is simple to understand but not particularly elegant. The whole business with the “if” statement can be eliminated by taking modulo 4 of count: coil = mod(count,4); You are certainly free to make other improvements to the program, but be sure to properly comment your code so the T/A’s can understand it when they grade your lab. Program 2: Input from Keyboard For motion control, we want to be able to tell the motor when to stop and which direction to go from a program or perhaps from the keyboard. Let’s use the keyboard as our input interface. Matlab can accept input from the keyboard via the command “input(‘prompt’). Type help input for more information on this command. Modify your first program to prompt the user for a number between ‐100 and 100, and use this number to drive the motor that number of steps in the forward (n>0) or reverse directions, correspondingly. Hint: You can set up a for loop with a variable as the start point, the end point or the step size for that matter. So, for example, it is permissible to write for j = P:Q:R %P, Q and R are integer variables. Q is the step size and can be negative do stuff; end Note that P,Q, and R must have already been assigned values before you can call them in a loop like this. Program 3: Scaling I/O In the real world, we deal in engineering units like millimeters and degrees, not in “steps”. Modify your program so the user is prompted to enter the number of degrees to move, with the limit between ‐170 and +170. Insert a math statement to convert the input to a number of steps for your program. Demonstrate your program to one of the T/A’s before going on. Save the code for your Lab Report. Program 4: Home Position Next, we want to see how to make our robot go to a specific point in space. To do this, we must have a reference position we call “home” or “zero” or “the origin”. For robots, this is usually called “home”. Use a rubber band or some tape or a dab of hot‐melt glue to fasten a Lego “beam” to your motor shaft so it sticks out like an arm. Set up the optical proximity sensor to detect when the end of the beam is in the HOME position—see the diagram below. The optical sensor is wired as follows: Red: +5V Black: 5V Ground Orange: Digital Input For best accuracy, Home should always be approached from the same direction, since the beam has finite thickness >0. Robots always find home as the first thing they do when they are powered up. Modify your program so when it first starts, the motor is driven slowly in the clockwise direction until the optical sensor turns on. When the sensor turns on, the robot should stop moving and set its position counter to zero. We will use the convention that positive is counterclockwise and negative is clockwise. To digital
input
Lego beam
+5V
Stepper
motor
Optical sensor emits
a beam of IR light and
detects it if it reflects
from object
Gnd
F
Final Program Now synthesize everything you have learned into one complete position control program. The program should find home, then ask the user if they want to exit the program. If so, end. If not, ask for an angle between 0‐330o and move the arm to that position. It should then go back and prompt for exit again, ask for a new position and go there, and just keep doing this until the user gives the quit signal. Here is a general “outline” of what your program will look like. Note that each of these boxes has a significant amount of code inside. You should draw a more detailed flow chart for each of the major elements of the program. Demonstrate your program to the T/As before disassembling your setup. Start
Find Home
Position,
disp(‘Ready’)
Prompt for
quit flag
Y
End
Wanna quit?
Hint: You can make your code more compact by using the concept of position error. You know your current position. When you read a new position command from the keyboard, compute the error: N
Get new
position
from
Keyboard
error = desired_angle – current angle; Now the error is a signed value that you can scale to give you the number of steps and the direction you need to go to get to the new commanded location. It can be used directly in your for loop in a number of ways. A simple one would be as the end of the counter: Calculate error
Is error zero?
N
Move motor
to reduce
error
Y
numsteps = floor(0.5 + abs(error)/3.6); % rounds to integer steps = numsteps*sign(error); % sets direction for count = 0:sign(error):steps Move the motor; %you figure this out ☺ end You can do this in a simple, clunky way if you can’t figure this out, but it’s rewarding to write nice compact code when possible (and, it runs faster). Calculate new
error
Lab Report •
•
Write a an introduction summarizing the operation of stepper motors and showing a schematic of how you are controlling yours using the digital outputs. Summarize each of the programs you have written. Describe the purpose of the program and briefly describe the strategy you used to solve the problem. Include formatted and commented source code for each program.