CMPE 401 - Fall 2008 Laboratory #5

Stepper Motor Control Using the Coldfire Enhanced Time Processor Unit (eTPU)


Lab Dates:

Please refer to the Lab Web Page and Lab Schedule for lab dates.

Report and Demo Due Dates:

Please refer to the Lab Web Page and Lab Schedule for all due dates.

Objectives:

Documentation: Equipment, Parts and Provided Software:

In addition to all hardware and software from prior labs, you will be using: Background:

Microcomputers are sometimes used to control rotating mechanisms, such as shafts and axles as well as attached pulleys, gears and/or wheels. A stepper motor is a motor whose rotor position can be controlled precisely by switching direct current (DC) voltages to two or more stator windings. Each winding can be energized in the forward direction, energized in the reverse direction, or not energized at all. Such a motor is convenient to control using a microcomputer since only buffered digital H-bridge input signals are required. Coldfire's Enhanced Time Processor Unit (eTPU) is a programmable sub-system in the MCF5234 microcontroller that has built-in functions that greatly simplify the generation of the digital waveforms that are required to control a stepper motor.

The stator windings in a stepper motor must be energized in the correct direction and in the correct sequence in order to produce controlled rotation of the rotor. The four steps in the full-step sequence are given in the table below, which assumes the use of the L298 dual H-bridge driver IC. Note that the provided eTPU driver controls the L298 with the built-in stepper motor function. Each step in the full-step sequence advances the rotor of the Jameco motor by 3.6 degrees; thus 100 steps advance the rotor through one full revolution. Each step in the half-step sequence advances the rotor by 1.8 degrees. 200 steps are required for a complete revolution in half-step mode.

Full-Step Sequence
Step Winding A
Current
Direction
Winding B
Current
Direction
L298 EnA = EnB = 1
L298 Outputs L298 Inputs
OUT1 OUT2 OUT3 OUT4 IN1 IN2 IN3 IN4
A #A B #B A #A B #B
#1 forward forward source sink source sink 1 0 1 0
#2 forward reverse source sink sink source 1 0 0 1
#3 reverse reverse sink source sink source 0 1 0 1
#4 reverse forward sink source source sink 0 1 1 0
#1 forward forward source sink source sink 1 0 1 0
#2 forward reverse source sink sink source 1 0 0 1

Note that in the full-step sequence, the total current drawn by the motor from the DC power supply is roughly the same in every step position. If one relaxes this constant-current restriction, then one can double the resolution of the rotor positioning by using the half-step sequence, which is shown in the table below. The additional cost of the half-step sequence is that the control waveforms are slightly more complicated and the total drawn current becomes position-dependant.

In the case of the laboratory set-up, the two L298 enable inputs (EnA and EnB) are controlled separately using two different eTPU timing channels. Thus the recommended hardware configuration assumes that EnA and EnB channels are properly disabled whenever the winding current is supposed to be off.

The Stepper Motor functions for the eTPU have implemented the timing charactoristics for each of the stepper motor configurations described above. They take advantage of the fact that the vertical sequence of 1's and 0's for in the the above IN1 - IN4 are identical, but time shifted versions of eachother. Using this fact, we can make the vertical sequence of one of the four above outputs, and turn it into a number (The "pin seqeuence"). For convienience, if we choose IN2 it will generate the pin seqeuence value of binary 0011 = 0x3. The first 4 bits will set the state of the four outputs. If we do a 4 bit Logical Rotate Right on the number, it will change to binary 1001 = 0x9, and then 1100 = 0xC, 0110 = 0x6, and then back to 0011 = 0x3. How convenient! Note that in the eTPU implementation, you must put in a 24 bit number as the pin sequence. Simply repeat the number 6 times to fill the 24 bits.

e.g. my_pin_seq = 0x333333;

Now, look at page 4 of Coldfire Application Note on Using the eTPU Stepper Motor Function. We will be using this method to control the stepper motor in full step mode. Instead of generating all four signals, a hex inverter is used to provide the inverted A and B signals. In a real world scenario, this technique would be useful in a microcontroller that does not have enough pins to produce all outputs, or the computation time required to generate the two other signals is unacceptable.

Half-Step Sequence
Step Winding A
Current
Winding B
Current
L298 Outputs L298 Inputs
OUT1 OUT2 OUT3 OUT4 EnA EnB IN1 IN2 IN3 IN4
A #A B #B A #A B #B
#1 forward forward source sink source sink 1 1 1 0 1 0
#2 forward off source sink off off 1 0 1 0 X X
#3 forward reverse source sink sink source 1 1 1 0 0 1
#4 off reverse off off sink source 0 1 X X 0 1
#5 reverse reverse sink source sink source 1 1 0 1 0 1
#6 reverse off sink source off off 1 0 0 1 X X
#7 reverse forward sink source source sink 1 1 0 1 1 0
#8 off forward off off source sink 0 1 X X 1 0
#1 forward forward source sink source sink 1 1 1 0 1 0
#2 forward off source sink off off 1 0 1 0 X X
The pin sequence required for half step mode is much more complicated. You will in fact have to initialize two different eTPU stepper motor functions, one for each of A and B channels, and one for the Enable A and Enable B channels. One notable feature about half step mode, is that the output of each channel corresponds to EVERY OTHER bit of the pin sequence. Look at page 6 of Coldfire Application Note on Using the eTPU Stepper Motor Function to see an example of half step mode.

The stepper motor that you will be controlling in this set of laboratory exercises will be operated in open loop mode. By this we mean that the microcomputer sends out digital signals to the stepper motor and assumes that the rotor will always rotate through precisely the correct number of steps. The microcomputer lacks the feedback means to verify the actual position of the rotor before and after each step command. Open loop operation is more risky in the sense that the microcomputer cannot verify that it does indeed have accurate control over the rotor's position. The rotor position may in fact deviate from the expected position if, for example, the limited torque of the motor is unable to rotate the loaded shaft fast enough to keep up with the stepping commands. A better alternative to open loop operation would be to operate in closed loop mode.

To do this, one would need to provide feedback through a shaft encoder sensor that would allow the microcomputer to independently verify the actual position of the rotor. But we will not be using feedback in this lab. Instead you will initially set the rotor pointer to the "12 o'clock" position, which the TPU driver software will then associate with position 0.

One last idea to touch upon, is that of the acceleration table. In this eTPU implementation, the stepper motor will accelerate from the Start Period to the Slew Period (Max speed) following these rules:

In this lab, a function is provided to you to create the acceleration table. The derivation is complicated, but is calculated in such a way to provide a constant acceleration and linear velocity, resulting in a 1/n type curve in the period.

Note that when we refer to the period, we are refering to the number of eTPU clock cycles each step takes. The eTPU has its own clock, which for this course is set the the default of 36.864 MHz or appoximately 27ns period. For example, a Slew period of 368640 will step every 368640 * 27ns = 1ms. The output pulse will be twice that length in full step mode, because each signal is a 1 or 0 for two steps in a row.

Prelab:

  1. Wire up the Stepper motor, inverter, L298. Take special care ensuring you connect the proper voltage to the correct chip, as the 12 volt line can (and will) destroy ever other chip on the board if connected incorrectly! Also, confusing the input and output lines of the L298 will cause 12V to be driven back into the Microcontroller!
    Schematic
    L298 Pinout on page 2

  2. Choose a pin sequence for half step mode in our setup. This will require a pin sequence for the enable pins, and one for channels A and B. The resulting pin sequence should be a 24 bit number. Use the above table, and the SM application note to get your result. Hint: The X's mean "don't care", but there is no way to denote that in the pin sequence, so you will have to appropriately assign them as 1 or 0.
  3. Look at the schematic. Specifically, look at the diode and motor section and pretend that the L298 is not attached to it. The motor consists of coils (inductors) from A to #A and B to #B. What will happen when the motor stops receiving a voltage? There will still be energy in the coils and since current cannot instantly change in inductors, it will have to go somewhere. Since there is no path, the voltage will build. Where does the energy go when the voltage is: greater than 12V? less than 0 volts? between 0 and 12?

Execise 1:

  1. Download the lab5 code off cvs. You will also need to import the following files from your lab4 code:

    student.cpp, student.h, and html\index.htm

  2. To make the program compile, we will remove some of the lab 4 specific functions. In student.cpp, student.h, and index.htm, remove the functions, declarations, and calls to:
    RTCSetTimeFromUrl, I_Love_CMPE_401, and GetRandomImage.

  3. Finally, remove the block of libraries for the SD card and file system in student.cpp.

    /*** Begin SD FTP include files ***/
    cardtype.h,
    effs_fat/fat.h,
    effs_fat/mmc_mcf.h,
    http_f.h,
    ftpd.h,
    ftp_f.h,
    effs_time.h,
    FileSystemUtils.h
    /*** End SD FTP include files ***/

  4. Execute the code. The current capability of it is to run in full step mode, and to accept a number of steps in the web form (assuming your printlines function from lab 4 works!!)
  5. When entering data into the form, think about what the limitations on the start period, slew period and steps should be for a mechanical device like a stepper motor. Do some back of the envelope calculations to determine if the values you put in the text fields make sense.

Exercise 2:

  1. in stepper.cpp, and stepper.h, add the functions:
    Stepper::SetStartPeriod(unsigned int start) and Stepper::SetSlewPeriod(unsigned int slew)
    Make sure that:
    • The periods are between the SM_MIN_PERIOD = 1 and SM_MAX_PERIOD = 0x7fffff as defined in stepper.h
    • To write and read from the eTPU ram, use the SM functions as defined in:
      C:\Nburn\MOD5234\include\ETPU\functions_API\etpu_sm.h and
      C:\Nburn\MOD5234\system\ETPU\functions_API\etpu_sm.c
    • The acceleration table is updated to reflect the new period values.
    • All values put into the form fields need to be verifoed. The lab instructor will be placing invalid data into the form fields. Make sure you check them before calling the appropriate stepper motor function.

  2. In main.cpp, finish the two functions FormSetSlewPeriod(), and FormSetStartPeriod().
    • These will take in the "value" string from the form, convert it to an integer using atoi() and use the above stepper motor functions to finalize it.
    • "value" should be updated with the value actually put in, and a message to the user should be placed in the message string. Use sprintf() to convert a number to a string.
    • make sure to return 1 upon success or the webform will not display the message or value on the new page.
    • Display a useful message to the user if the value is correct or incorrect.

Exercise 3:

Goal: Make the motor work in Half step mode.

Report Requirements:

Consult the
Report Writing Guidelines if you have any questions regarding the report format.

Marking Scheme:

Lab #5 is worth 20% of the final lab mark.
Please view the Marking Sheet to ensure that you have completed all of the requirements of the lab. The Marking Sheet also contains a limited test suite in the demo section. Please make use of it.

Last modified November 17, 2008