Difference between revisions of "PIC32MX: Driving a Stepper Motor"

From Mech
Jump to navigationJump to search
Line 42: Line 42:


Note: There are limits to how fast the motor can spin. If the user sets the speed too high, the motor will just vibrate. Some motors list their maximum speeds on the data sheets. If the desired motor does not have this listed, a little bit of experimentation should yield the maximum speed.
Note: There are limits to how fast the motor can spin. If the user sets the speed too high, the motor will just vibrate. Some motors list their maximum speeds on the data sheets. If the desired motor does not have this listed, a little bit of experimentation should yield the maximum speed.
[[Media:Example.ogg]]

Revision as of 21:38, 8 February 2010

Original Assignment

Do not erase this section!

Your assignment is to create code that reads a voltage from a potentiometer and controls a stepper motor to rotate at a proportional speed. The middle range of the analog input (i.e., 1.65 V) corresponds to zero speed, the minimum is maximum backward speed, and the maximum is maximum forward speed. You will create an interrupt service routine that is called every 500 microseconds. Each time through the routine, you will add 1 to a counter. When that counter (time, in units of 500 microseconds) exceeds the inverse of the desired speed (time per step), you will change the digital outputs controlling the stepper motor to the next position, depending on the direction of motion, and set the counter back to zero.

To do this, you must create a function that takes the analog input, turns it into a speed, then turns it into an inverse of speed that the ISR uses. Clearly the shortest allowable inverse of speed is 1 unit (500 microseconds per step). But can your motor really do this, or does it just vibrate if you try to rotate that fast? Based on your experiments, you should find the maximum speed that the motor actually tracks and set that as a constant. Then 3.3 V and 0 V map to this speed, in opposite directions.

You will use a geared stepper motor that we provide.

Overview

The goal of this page is to explain several pieces of code and a corresponding circuit that were designed to drive a bipolar stepper motor using a PIC32MX460F512L. Useful information about stepper motors in general can be found here and here. The specific motor that we are using is a Portescap 26M048B1B-V27. This 5V bipolar stepper motor has 48 steps per revolution (7.5 degrees per step), a 20:1 gearhead (for an output step angle of 0.375 degrees), and a resistance per phase of 19.80 ± 10% Ω. The datasheet for this motor can be found here.

In this code, the user executes a processing code, and uses it to communicate with the PIC32. More information about processing, as well as a free download of the program can be found here. The communication is handled with an RS232 cable and the PIC32’s UART peripheral. The processing code provides the user with the ability to send three functionally different sets of information to the PIC32. The three sets of that can be sent are as follows:

1) The ability to transmit some motor speed to the PIC32, and the PIC32 will drive the motor at that speed until the user changes it by transmitting a new speed. They can transmit a positive or a negative speed to change the direction.

2) The ability to transmit a motor speed and number of steps to the PIC32, and the PIC32 will drive the motor at the desired speed until the desired number of steps has been reached, and then it will stop the motor.

3) The ability to send a command to stop the motor right where it is.

The code uses one of the timer modules on the PIC32 to generate the correct pulse train to drive the motor. The desired speed of the motor (in steps/ second) is input into a function that calculates how often the motor should step, and in-turn what value needs to be stored in the timer’s period register such that the timer interrupt service routine is called every time that the motor steps. This ISR is what handles the setting of the phases.

Circuit

General H-Bridge Bipolar Drive Circuit

A bipolar stepper motor has two separate phases. The steps are created by changing the direction of the current flow through each of the phases in the correct sequence. In this circuit, one-half of an h-bridge is connected to each of the four leads of the stepper motor. These h-bridge transistors are used so that the 3.3V available from the PIC32 can be used as control logic alone, and the primary current for the motor can be sourced from somewhere else. These also help to isolate the motor from the PIC32. A general image of this h-bridge setup can be seen at right.


We used an L293D (Digi-Key part number 497-2936-5-ND) chip for the h-bridges. These can be purchased at Digi-Key here.

An image of the full circuit is shown below.
Bipolar Stepper Motor Drive Circuit JSJW.JPG
In the above circuit, pins D0-D3 of the PIC32 are used to as inputs to the h-bridge chip. The timers in the code switch these pins high and low to step the motor. The motor's output leads need to be correctly connected to the h-bridge outputs. Some motor manufacturers specify which color wires correspond to which phase, but some do not. If unsure which wire is which, use a multimeter, and test the resistance of each of the pairs of wires. When one pair shows a resistance that is significant, you know that these two wires are internally connected. This means that they will either need to be connected to outputs 1 and 2 or outputs 3 and 4 of the h-bridge. U2TX and U2RX are connected to an RS232 cable that is correctly grounded and connected to a PC. This cable is used for sending signals to the motor and receiving current motor step counts from the PIC. An interesting note is that this circuit and corresponding code will work for other four-lead bipolar stepper motors as well. If using another motor, the motor supply voltage simply has to be adjusted to match this motor. Be careful not to exceed the limits of the h-bridge chip!

Code

The entire pastable code for the PIC32 is in the text box below. Compiling this code and loading it onto the PIC32 is the bare-minimum that is required for controlling the stepper motor. If this is the case, motor control is handled through some serial communication terminal program (hyperterminal, putty, or realterm). Be sure to match the baudrate (115200), the stop bits (1), the parity bits (none), and the data flow (none). In the main function of the program it is possible for the user to extract the correct format for the motor control data. But, for simplicity, it is summarized here as well. A total set of control data consists of 10, 8-bit (one byte) ascii characters. The first character is either an 's', 'd', or 'p'. An 's' implies that the user simply wants to set a motor speed to some value; a 'p' tells the PIC that the user wants to control speed and position of the motor i.e. step this fast for this many degrees; and a 'd' tells the motor to stop right where it is. The second byte is either an ascii '1' or '0'. This byte sets the motor direction. The next three bytes set the motor speed in steps per second. The first byte is the hundreds digit, then the tens digit, then the ones digit. The last five bytes are the number of steps the user wants the motor to step before stopping; these are decoded in the same way except the most significant byte is the ten-thousands digit. If the user sends an 's' as the first byte, the contents of the last 5 bytes will not affect the motor's behavior; if the user sends a 'd', only the first byte is important. As an example, let's say the user wants to the motor to drive 1000 steps in the positive direction (the actual direction will depend on how the motor is wired up) at a speed of 300 steps/ second. Then the user would send the string 'p130001000' to the PIC.

Note: There are limits to how fast the motor can spin. If the user sets the speed too high, the motor will just vibrate. Some motors list their maximum speeds on the data sheets. If the desired motor does not have this listed, a little bit of experimentation should yield the maximum speed. Media:Example.ogg