Lab 2b: Dynamic Stepper Motor Control
Download This Document
- Develop an application of foreground-background task scheduling.
- Use change notification interrupts to detect push button presses and releases.
- Eliminate multiple button operations using a timer interrupt to implement a non-blocking delay.
2. Basic Knowledge
3. Equipment List
- Workstation computer running Windows 10 or higher, MAC OS, or Linux
In addition, we suggest the following instruments:
4. Project Takeaways
- The ability to control the speed of rotation of a stepper motor.
- Determine the number of steps required for one rotation of the rotor.
- How to determine the delay time between steps, resulting in a specified rotation speed.
5. Fundamental Concepts
After a search of the internet, one will find that the stepper motor is one of the most frequently used examples of an application of finite state machines (FSM). Applications that use stepper motors include robotics, disk drives, and office products like laser printers and copiers.
6. Problem Statement
Use the eight slide switches to set the speed of rotation of the stepper motor shaft from 0 to 31.875 RPM in steps of 0.125 RPM. BTND will switch the motor operation from FULL step to HALF step when depressed. BTNR will switch the rotational direction of the rotor from CW to CCW when pressed. The speed of rotation will be displayed on the four-digit seven-segment display digits with 25% duty cycle persistence and one ms update rate.
7. Background Information
Stepper motors are electrical mechanical devices used in many robotics applications. In this lab, we will look at how the PIC32 internal timers can be used to implement both cooperative and preemptive scheduling for a real-time system application. We will use time management to transition between states, giving the appearance of continuous rotation of the motor. The position of the stepper motor shaft is a function of four outputs. These outputs must change in a predefined order to cause the motor shaft to move in discrete steps. The outputs to the stepper motor are controlled using a state machine algorithm that generates output patterns and will be used to control position and angular velocity.
Since the PIC32 outputs represent a form of memory, I find it convenient to use state on-entry or on-exit actions to set the processor pins. An on-entry action sets the phase outputs specified for that case whenever a state (case) is entered. An on-exit action sets the phase outputs specified for that case whenever a next state (case) is set. The DIR and MODE inputs define the next state using “if-else” or a sub level of “switch-case” statements.
An alternate implementation uses a table of output codes and an index that becomes the state. It must be remembered that the Basys MX3 platform does not connect consecutive processor pins on a single port to the stepper motor phases. Hence, the state output table must be replaced with a sequence of bit-banging instructions to individually set each phase output pin.
8. Lab 2b
- This make of stepper motor is nominally rated for 1600 steps per revolution +/- 7%. Hence, the range can be from 1488 to 1712 steps per revolution. Determine the number of steps for one complete revolution for the particular motor you are using.
- BTNR controls the direction of rotor rotation (CW or CCW).
- BTND controls the stepper motor step mode (full or half step).
- The speed or rotation must be the same regardless of stepper mode operation.
- The speed of rotation is set by the hexadecimal value set on the eight slide switches. SW7 is the most significant bit and SW0 is the least significant bit with the switch weighting, as shown in Table 8.1.
- The speed of the motor is to be displayed on the four-digit seven-segment LED display in RPM with the decimal point to the right of the third digit, as shown in Fig. A.2.
- The four digits of the Basys MX3 seven-segment display are continually updated with a 1 ms persistence (each digit must be turned on for 1 ms). The four digits will be lit in a round-robin fashion in a foreground operation managed by the Timer 1 ISR.
- Stepper motor outputs are changed in the Timer 1 ISR using the period as determined by the slide switch settings. The period is determined by converting RPM to ms delay between steps.
Table 8.1. Stepper motor speed control table.
8.2. Design Phase
- Concept maps
- Develop a Data flow diagram that relates slide switch inputs, push button inputs, and timer interrupts to stepper motor and seven segment LED outputs.
- Develop a state transition diagram similar to the one described in Unit 2.
- Develop a control flow diagram that describes the total system operation. Background operations are represented separately from each foreground operation.
- Review the pertinent schematic diagrams to determine the relevant IO pin assignments. Refer to Figs. A.3, A.4, and A.5 in Appendix A.
- Make a list of the processor IO pins used, along with the data direction (input or output). Include the information as to whether the pin is analog capable or not.
8.3. Construction Phase
This lab should be developed in phases to partition the problem into functions or collection of functions that can be tested separately.
8.3.1. Phase 1
- Develop an expression that converts rotational speed in RPM to ms per step.
- Write an application that initializes all switches as well as BTNR and BTND as PIC32 inputs. Configure all stepper motor connections as outputs.
- Develop a PIC32 application program that causes the stepper motor to take a single step in a specified direction (CW or CCW) in a specified mode (half or full step).
- The rotational direction of the stepper is such that when BTNR is depressed, the motor rotates in a clockwise (CW) direction, otherwise it rotates in a counterclockwise (CCW) direction.
- This program will be operated with a breakpoint set in the infinite loop function to determine if the stepper motor phase connection is correct.
- Using a software delay, determine the number of steps needed for one complete revolution. (See Requirement 1 above.)
8.3.2. Phase 2
- Initialize Timer 1 to generate a Level 2 interrupt once each 1/10 millisecond.
- Write a Timer 1 interrupt service routine that causes the seven-segment LED to display the RPM (in decimal values) set by the binary encoded switch settings. The LED display is to update one of the four digits once each millisecond.
8.3.3. Phase 3
- Integrate Phase 1 and Phase 2 to implement the following controls in steps two and three.
- The speed of the stepper motor in revolutions per minute (RPM) is set by the binary encoding of the eight slide switch positions. For example, the position of the slide switches shown in Fig. A.1 results in the stepper motor rotating at 60 RPM. 60 RPM results when SW5, SW4, SW3, and SW2 are set high.
- The stepper motor normally operates using half steps. If BTND is held depressed, the stepper motor switches to full step operation without altering the RPM set by the slide switches and returns to half step upon release of the button.
1. Configure the Analog Discovery to display the PIC32 outputs used for the stepper motor control, as shown in Fig. 8.1.
Figure 8.1. Waveforms screen capture showing stepper motor output signals.
a. Measure the step interval for switch settings in the following table and compute the motor speed in RPM using the expression developed for Phase 1, step 1.
Table 8.2. Stepper motor speed control performance log.
|SW7-SW0||BTND||Set Rotor Speed RPM||Step Period – ms||Calculated Rotor Speed RPM|
|0 0 0 0 0 0 0 1||Down||0.125|
|0 0 0 0 0 0 0 1||Up||0.125|
|0 0 0 0 0 0 1 0||Down||0.25|
|0 0 0 0 0 0 1 0||Up||0.25|
|0 0 0 0 0 1 0 0||Down||0.5|
|0 0 0 0 0 1 0 0||Up||0.5|
|0 0 0 0 1 0 0 0||Down||1.0|
|0 0 0 0 1 0 0 0||Up||1.0|
|0 0 0 1 0 0 0 0||Down||2.0|
|0 0 0 1 0 0 0 0||Up||2.0|
|0 0 1 0 0 0 0 0||Down||4.0|
|0 0 1 0 0 0 0 0||Up||4.0|
|0 1 0 0 0 0 0 0||Down||8.0|
|0 1 0 0 0 0 0 0||Up||8.0|
|1 0 0 0 0 0 0 0||Down||16.0|
|1 0 0 0 0 0 0 0||Up||16.0|
b. Capture the WaveForms 2015 screen when slide switches are configured for 0x40 (SW6, and SW4 set high resulting in 10.00 RPM), as shown in Fig. A.1, when BTND push button is depressed.
- How are you able to accurately determine the number of steps per revolution? How many steps occur in the phase period shown in Fig. 8.1?
- From the expression that converts RPM to ms per step, what is the effect of using integer division on the actual stepper motor rotational speed?
- What is the measured stepper motor maximum rotational speed?
- Using the pin designated for LCD DB0 on the Basys MX3 processor board (PIC32 RE0), measure the minimum and maximum ISR execution time. Using the MPLAB stopwatch feature and the assembler code for the Timer 1 ISR prologue and epilogue (saving and restoring) times, what is the worst case ISR execution time?
- Using the pin designated for LCD DB0 on the Basys MX3 processor board, measure the minimum and maximum background loop. What is the worst case response time for a user input measured in microseconds?
- PIC32MX330/350/370/430/450/470 Family Data Sheet.
- Basys MX3 Reference Manual.
- How Stepper Motors Work, https://www.youtube.com/watch?v=bngx2dKl5jU.
- MPLAB X Stopwatch, https://learn.digilentinc.com/Documents/211.
Appendix A: Basys MX3 Schematic Drawings and Equipment Configurations
Figure A.1. Equipment configuration for Lab 2b.
Figure A.2. Basys MX3 slide switch schematic.
Figure A.3. Switch setting for 18.25 RPM.
Figure A.4. Push button schematic diagram.
Figure A.5. Schematic diagram of stepper motor driver.
Figure A.6. Stepper motor connector to Basys MX3 connection. The stepper motor pink wire is not connected.
Appendix B: Introduction to Stepper Motors and Finite State Machines
Stepper motors are variable reluctance electric motors that are designed to control the angular position of the rotor shaft in discrete steps. The stepper motor consists of two sets of field windings positioned around a permanent magnet rotor. The combinations of voltages applied to the four control terminals of the field windings control the magnitude and direction of the current through the windings. The electrical current through the windings create an electromagnet. The motor shaft rotates to a position that minimizes the reluctance path between the field winding electromagnet’s north/south poles and those of the permanent magnet rotor.
Figure B.1. Bipolar (4 wire) Stepper motor diagram.
Figure B.2. Wiring configurations for 5, 6, and 8 wire stepper motor.
Considering the combinations of voltages on the winding terminals as possible control states, there are only eight states that produce current in the field windings, as shown in Table B.1 below. In order to move the rotator shaft from one stable position to the physically adjacent stable position, the control voltages must switch to one of four out of the eight possible combinations of voltages. The action of moving from one stable position to an adjacent stable position is referred to as either a full-step or a half-step. Half-step increments are half the angular rotation of full-steps. Repeating a sequence of full-step or half-step movements at a high speed uniform rate will cause the rotator shaft to appear to rotate at a constant speed albeit in discrete steps.
Table B.1. Stepper motor control codes.
|Step Control||Winding Voltage|
|Step Name||Hex Code||“1a”||“1b”||“2a”||“2b”|
The stepper motor used in Lab 2b is configured as a 5 wire motor, as shown in Fig. B.2, and is designed to require nominally 1600 full steps for the rotor shaft to complete one full revolution, or 0.225 degrees per step. 3200 half-steps are required to make one revolution, or 0.1125 degrees per half-step. The first column in Table II is a label assigned to the state. The second column is the hexadecimal code that will set the processor’s I/O pins to control the voltages on the terminals of the windings. The last four columns in Table B.1 represent the combinations of voltages on the field windings that produce stable rotator shaft positions. The letter “H” denotes a high voltage and the letter “L” denotes a low voltage. As shown in Fig. B.1, current flows through a motor coil when there is a voltage difference across the winding. The voltage combinations for step 3 (S3) in Table B.1 represent the combination to produce the current flow shown in Fig. B.1.
The four winding terminal designations shown in Table B.1 are assigned to I/O pins, as shown in Appendix C. The stepper motor will move to the nearest stable position generated by the voltages associated with the hexadecimal codes shown in the second column. The stepper motor will be held in a fixed position until the voltages on the windings change.
If the motor is to rotate the motor shaft in a clockwise direction using the full-step mode, the sequence of output codes that must be sent to the motor are represented by steps S1, S2, S3, S4, S1, etc. A 1600-step per revolution motor will require the sequence of the four output combinations, S1 through S4, to be repeated 400 times for the rotator shaft to make exactly one revolution. If operating in half-step mode, then the eight-step sequence of S0, S0_5, S1, S1_5, S2, S2_5, etc., must also be repeated 400 times for the rotator shaft to make a complete revolution. Sequencing in one direction (up or down) through the output code found in Table II causes the rotator shaft to rotate in one direction. Reversing this sequence causes the rotator shaft to rotate in the opposite direction.
Connecting the Stepper Motor to the Basys MX3
The Basys MX3 processor platform uses a DRV8835DSSR driver module that interfaces with the PIC32 processor, as shown in Fig. A.5. Referring to Table C.1 of Appendix C, we can generate Table B.2 below to assist in the initialization and operation of the stepper motor. All pins must be set as outputs. The RB3 pin used for AIN1 and the RB5 pin used for BIN2 must also have the analog functionality disabled using the instructions “ANSELBbits.ANSB5 = 0” and “ANSELBbits.ANSB5 = 0.” Based on the data sheet for the DRV8835DSSR driver, the “mode” input must be set low using the output from RF1.
Since an external 5.0 V supply will be used to power the stepper motor, connected to J11, the jumper pin must be set for the VBAR position on the top right corner of the Basys MX3 processor board. The stepper motor is connected to the Basys MX3 board as shown in Table B.2.
Table B.2. PIC32 to Stepper Motor Driver Connections.
|PIC32 PORT||PIC32 PIN||Driver Input||Motor Output||Stepper Motor|
When all phase outputs are either energized or de-energized, there is no holding torque on the motor, which allows it to turn freely. Since all steps are relative to the previous position of the stepper motor, the Basys MX3 outputs should change simultaneously and not one pin at a time. Since the current Basys MX3 trainer board precludes operation in this manner, we must assume that the time to execute the code to individually change the four outputs is much less than the step delay period. This turns out to be a reasonable assumption.
Unknown Wiring Configurations
If no wiring details are provided with the stepper motor, you can determine which wires constitute a phase pair through following the link in the footnote below. Figures B.1 and B.2 show the common wiring configurations. The common wires shown in Fig. B.2 should be left floating, with the exception of each pair of common wires shown in the 8-wire configuration, which must be connected together. Do not connect all four common wires of the 8-wire configuration together.
Appendix C: PIC32MX370 Pin Assignment for the Digilent Basys MX3 Platform
Table C.1. Processor IO Assignments.
|90||RG0||RPGO/PMD8/RG0||ACL_INT2||I2C - Accelerometer|
|41||RB12||AN12/PMA11/RB12||AN0||4 digit 7 segment LED|
|67||RA15||RPA15/RA15||BTND/S1_PWM||Push Button / servo mtr|
|32||RB8||AN8/RPB8/CTED10/RB8||BTNR/SP_PWM||Push Button / servo mtr|
|96||RG12||TRD1/RG12||CA||4 digit 7 segment LED|
|93||RE0||PMD0/RE0||DB0||Character LCD data|
|81||RD4||RPD4/PMWR/RD4||DISP_EN||Character LCD ctrl|
|57||RG2||SCL1/RG2||SCL||I2C - Accelerometer|