In addition, we suggest the following instruments:
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.
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.
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.
Table 8.1. Stepper motor speed control table.
Switch | SW7 | SW6 | SW5 | SW4 | SW3 | SW2 | SW1 | SW0 |
---|---|---|---|---|---|---|---|---|
RPM | 16 | 8 | 4 | 2 | 1 | 0.5 | 0.25 | 0.125 |
This lab should be developed in phases to partition the problem into functions or collection of functions that can be tested separately.
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 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.
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.
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” |
S0_5 | 0x0A | H | L | H | L |
S1 | 0x08 | H | L | L | L |
S1_5 | 0x09 | H | L | L | H |
S2 | 0x01 | L | L | L | H |
S2_5 | 0x05 | L | H | L | H |
S3 | 0x04 | L | H | L | L |
S3_5 | 0x06 | L | H | H | L |
S0 | 0x02 | L | L | H | L |
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.
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 |
---|---|---|---|---|
RB3 | PGED3/AN3/C2INA/RPB3/RB3 | AIN1 | AIN1 | Red |
RE8 | RPE8/RE8 | AIN2 | AIN2 | Orange |
RE9 | RPE9/RE9 | BIN1 | BIN1 | Yellow |
RB5 | AN5/C1INA/RPB5/RB5 | BIN2 | BIN2 | Blue |
RF1 | RPF1/PMD10/RF1 | MODE | ||
No Connection | Pink |
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.
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.
Table C.1. Processor IO Assignments.
CPU pin | Port | ALT | Function | |
---|---|---|---|---|
21 | RB4 | AN4/C1INB/RB4 | A_MIC | Microphone |
43 | RB14 | AN14/RPB14/PMA1/CTED5/RB14 | A_OUT | Speaker |
23 | RB2 | PGEC3/AN2/C2INB/PRB2/CTED13/RB2 | A_POT | Pot |
90 | RG0 | RPGO/PMD8/RG0 | ACL_INT2 | I2C - Accelerometer |
22 | RB3 | PGED3/AN3/C2INA/RPB3/RB3 | AIN1 | Stepper motor |
18 | RE8 | RPE8/RE8 | AIN2 | Stepper motor |
41 | RB12 | AN12/PMA11/RB12 | AN0 | 4 digit 7 segment LED |
42 | RB13 | AN13/RB13 | AN1 | ” |
28 | RA9 | VREF-/CVREF-/PMA7/RA9 | AN2 | “ |
29 | RA10 | VREF+/CVREF+/PMA6/RA10 | AN3 | ” |
19 | RE9 | RPE9/RE9 | BIN1 | Stepper motor |
20 | RB5 | AN5/C1INA/RPB5/RB5 | BIN2 | Stepper motor |
87 | RF0 | RPF0/PMD11/RF0 | BTNC | Push Button |
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 |
66 | RA14 | RPA14/RA14 | CB | “ |
83 | RD6 | RDD6/PMD14/RD6 | CC | ” |
97 | RG13 | TRD0/RG13 | CD | “ |
1 | RG15 | CNG15/RG15 | CE | ” |
84 | RD7 | RPD7/PMD15/RD7 | CF | “ |
80 | RD13 | RPD13/RD13 | CG | ” |
95 | RG14 | TRD2/RD14 | CP | “ |
93 | RE0 | PMD0/RE0 | DB0 | Character LCD data |
94 | RE1 | PMD1/RE1 | DB1 | ” |
98 | RE2 | AN20/PMD2/RE2 | DB2 | “ |
99 | RE3 | RPE3/PMD3/RE3 | DB3 | ” |
100 | RE4 | AN21/PMD4/RE4 | DB4 | “ |
3 | RE5 | AN22/RPE5/PMD5/RE5 | DB5 | ” |
4 | RE6 | AN23/PM6/RE6 | DB6 | “ |
5 | RE7 | AN27/PMD7/RE7 | DB7 | ” |
81 | RD4 | RPD4/PMWR/RD4 | DISP_EN | Character LCD ctrl |
82 | RD5 | RPD5/PMRD/RD5 | DISP_R/W | “ |
44 | RB15 | AN15/RPB15/PMA0/CTED6/RB15 | DISP_RS | ” |
89 | RG1 | RPG1/PMD9/RG1 | IR_PDOWN | IRDA |
26 | RB6 | PGEC2/AN6/RPB6/RB6 | IR_RX | “ |
27 | RB7 | PGED2/AN7/RPB7/CTED3/RB7 | IR_TX | ” |
7 | RC2 | RPC2/RC2 | JA1 | Pmod JA |
6 | RC1 | RPC1/RC1 | JA2 | “ |
9 | RC4 | RPC4/CTED7/RC4 | JA3 | ” |
10 | RG6 | AN16/C1IND/RPG6/SCK2/PMA5/RG6 | JA4 | “ |
8 | RC3 | RPC3/RC3 | JA7 | ” |
11 | RG7 | AN17/C1INC/RPG7/PMA4/RG7 | JA8 | “ |
12 | RG8 | AN18/C2IND/RPG8/PMA3/RG8 | JA9 | ” |
14 | RG9 | AN19/C2INC/RPG9/PMA2/RG9 | JA10 | “ |
69 | RD9 | RPD9/RD9 | JB1 | Pmod JB |
71 | RD11 | RPD11/PMCS1/RD11 | JB2 | ” |
70 | RD10 | RPD10/PMCS2/RD10 | JB3 | “ |
68 | RD8 | RPD8/RTCC/RD8 | JB4 | ” |
74 | RC14 | SOSCO/RPC14/T1CK/RC14 | JB7 | “ |
72 | RD0 | RPD0/RD0 | JB8 | ” |
76 | RD1 | AN24/RPD1/RD1 | JB9 | “ |
73 | RC13 | SOSCI/RPC13/RC13 | JB10 | ” |
17 | RA0 | TMS/CTED1/RA0 | LED0 | LED |
38 | RA1 | TCK/CTED2/RA1 | LED1 | “ |
58 | RA2 | SCL2/RA2 | LED2 | ” |
59 | RA3 | SDA2/RA3 | LED3 | “ |
60 | RA4 | TDI/CTED9/RA4 | LED4 | ” |
61 | RA5 | TD0/RA5 | LED5 | “ |
91 | RA6 | TRCLK/RA6 | LED6 | ” |
92 | RA7 | TD3/CTED8/RA7 | LED7 | “ |
78 | RD3 | AN26/RPD3/RD3 | LED8_B | Tri-color LED |
79 | RD12 | RPD12/PMD12/RD12 | LED8_G | ” |
77 | RD2 | AN25/RPD2/RD2 | LED8_R | “ |
88 | RF1 | RPF1/PMD10/RF1 | MODE | Stepper motor |
25 | RB0 | PGED1/AN0/RPB0/RB0 | P32_PGC/BTNL | Push Button |
24 | RB1 | PGC1/AN1/RPB1/CTED12/RB1 | P32_PGD/BTNU | ” |
57 | RG2 | SCL1/RG2 | SCL | I2C - Accelerometer |
56 | RG3 | SDA1/RG3 | SDA | “ |
53 | RF8 | RPF8/RF8 | SPI_CE | Flash memory |
55 | RF6 | RPF6/SCK1/INT0/RF6 | SPI_SCK | ” |
52 | RF2 | RPF2/RF2 | SPI_SI | “ |
54 | RF7 | RPF7/RF7 | SPI_SO | ” |
51 | RF3 | RPF3/RF3 | SW0 | Slide switch |
50 | RF5 | RPF5/PMA8/RF5 | SW1 | “ |
49 | RF4 | RPF4/PMA9/RF4 | SW2 | ” |
48 | RD15 | RPD15/RD15 | SW3 | “ |
47 | RD14 | RPD14/RD14 | SW4 | ” |
35 | RB11 | AN11/PMA12/RB11 | SW5 | “ |
34 | RB10 | CVREFOUT/AN10/RPB10/PMA13/CTED11/RB10 | SW6 | ” |
33 | RB9 | AN9/RPB9/CTED4/RB9 | SW7 | “ |
39 | RF13 | RPF13/RF13 | UART_RX | FTDI receive |
40 | RF12 | RPF12/RF12 | UART_TX | FTDI transmit |
63 | RC12 | CLKI/RC12/OSC1 | ||
64 | RC15 | CLKO/RC15/OSC2 |