Sei sulla pagina 1di 14

Computer Control of a Stepper Motor Unit

ACS214/271: Assignment A2: Computer Control of a Stepper Motor Unit


Please read document "Ten basic rules for assignments A1, A2, and A3" (on MOLE). This lab script must be read prior to attending the lab session. Preparation time should be about 7 hours. You need to attend the lab session that you were allocated to and sign the attendance sheet in order for your report to get assessed. Report submission deadlines: Students allocated to group 1: Monday 26.03.2012, at 10:00 am. Students allocated to group 2: Monday 23.04.2012, at 10:00 am. Students allocated to group 3: Monday 19.03.2012, at 10:00 am.

Submission via Turnitin in MOLE. Hardcopy submissions will be discarded. Every student needs to submit her/his own original report. Do not include the entire source code in your report. Instead you should provide - where requested - your own fragments of code. If you define new helper functions or global variables, these must be included too. The report length is restricted to 5 pages (any additional pages will be discarded). The report should have one section per exercise. No introduction is needed. Introduction The aim of this laboratory is for you to become familiar with the programming required to control a stepper motor, read an optical encoder and perform code conversion (Gray to Binary). Objectives Develop software functions written in C language to control the stepping rate, mode and direction of a unipolar type stepper motor. Develop software functions to read the absolute position of a Gray optically-encoded disc. Learn the principles of digital control using an I/O interface. Understand the issues related to stepper motors and encoders. Background Materials Laboratory Script Programming Examples Interfacing routines documentation (see appendix) C language documentation Assessment For this assignment you will need to complete all exercises listed in this script. Icon indicates that you need to demonstrate something to the session advisor. You need to write a report in which you answer the questions from this lab script, only the text in boldface needs to be addressed! If you do not succeed in completing an exercise, comment on your partial solution and/or on the problems you encountered.

Each exercise includes a list of tasks (bullet points) that you need to complete. The tasks which are highlighted in bold indicate what information you should include in your final laboratory report.

P. Eastwood & R. Gross, 2012-02-27

Computer Control of a Stepper Motor Unit

1. The Hardware
The experiment includes the following hardware components:

Stepper Motor Unit with integral Gray-encoded disc. PC-USB interface module, providing digital I/O functionality. Networked PC.
In addition, you will have access to a C/C++ compiler and a complete I/O library i.e. a set of high-level functions that cover all of the common operations of the data acquisition device (e.g. reading and writing data from/to a digital I/O port). The following functions will be of particular use when you start to develop your own code: cbDOut this function writes a byte to one of the two USB modules I/O ports. cbDIn this function reads a byte from one of the two USB modules I/O ports. cbDConfigPort - this function sets the direction i.e. Input or Output of one of the two USB modules I/O ports.

Basic Stepper Motor Operation The stepper motor used in this experiment is a uni-polar type i.e. the operating current in any of the four windings (coils) flows in one direction. To step the motor in a particular direction, a specific switching sequence of the four motor coils (A D) needs to be followed, refer to Figure 1. Full Step Mode The switching sequence shown in Table 1 results in the rotor advancing through one complete step at a time. In this case this would be 7.5 degrees / step. AntiClockwise Step No. (4) 1 2 3 4 5(1) Table 1 Coil A OFF ON ON OFF OFF ON Coil B ON OFF OFF ON ON OFF Coil C ON ON OFF OFF ON ON Coil D OFF OFF ON ON OFF OFF

Start Position (arbitrary) Above sequence repeating

Clockwise

Half Step Mode The sequence shown in Table 2 uses both single and dual phase switching. This results in the rotor advancing through half the step angle (3.75 degrees / step). This mode results in smoother operation and allows faster stepping rates.

P. Eastwood & R. Gross, 2012-02-27

Computer Control of a Stepper Motor Unit AntiClockwise Step No 1 2 3 4 5 6 7 8 Table 2 Coil A ON ON ON OFF OFF OFF OFF OFF ON Coil B OFF OFF OFF OFF ON ON ON OFF OFF Coil C ON OFF OFF OFF OFF OFF ON ON ON Coil D OFF OFF ON ON ON OFF OFF OFF OFF

Start Position

Clockwise

Above sequence repeating

A simplified block diagram of the system is shown in Figure 1. Figure 1. Port B DB7 DB6 DB5 DB4 DB3 DB2 DB1 DB0 PB7 PB6 PB5 PB4 PB3 PB2 PB1 PB0 Encoder Disc

PC / USB Link

Port A DA7 DA6 DA5 DA4 DA3 DA2 DA1 DA0 Not used PA7 PA6 PA5 PA4 PA3 PA2 PA1 PA0 Stepper Motor

PMD 1208LS USB Interface Controlling the stepper motor from the PC

Stepper Motor Unit

P. Eastwood & R. Gross, 2012-02-27

Computer Control of a Stepper Motor Unit Port A of the USB interface (Bits DA0 DA3) is used to switch the stepper motor coils ON logic0 state or OFF logic1 state. Port A must be configured as an Output to allow this to occur. Note the use of negative logic. This is a fail-safe method used to prevent the stepper coils from energising when the unit is disconnected from the interface or whilst the interface unit has not been initialised. Tables 3 and 4 include the data bits required to rotate the stepper motor in full and half step modes respectively. Full step CW rotation occurs when the following step sequence 1,2,3,4 (then repeats) is executed. Full step CCW rotation occurs when the following step sequence 1,4,3,2 (then repeats) is executed. Half step mode is similar but with a sequence count of eight instead of four. Table 3 Full Step Mode (!!!please note that this is to be read as DCBA!!!) Step No. (4) 1 2 3 4 5 (1) Coil A DA0 OFF --- HIGH ON ---- LOW ON ---- LOW OFF --- HIGH OFF --- HIGH ON ---- LOW Coil B DA1 ON ---- LOW OFF --- HIGH OFF --- HIGH ON ---- LOW ON ---- LOW OFF --- HIGH Coil C DA2 ON ---- HIGH ON ---- LOW OFF --- HIGH OFF --- HIGH ON ---- LOW ON ---- LOW Coil D DA3 OFF --- HIGH OFF --- HIGH ON ---- LOW ON ---- LOW OFF --- HIGH OFF --- HIGH

Table 4 Half Step Mode (!!!please note that this is to be read as DCBA!!!) Step No (8) 1 2 3 4 5 6 7 8 Coil A DA0 ON ---- LOW ON ---- LOW ON ---- LOW OFF --- HIGH OFF --- HIGH OFF --- HIGH OFF --- HIGH OFF --- HIGH ON ---- LOW Coil B DA1 OFF --- HIGH OFF --- HIGH OFF --- HIGH OFF --- HIGH ON ---- LOW ON ---- LOW ON ---- LOW OFF --- HIGH OFF --- HIGH Coil C DA2 ON ---- LOW OFF --- HIGH OFF --- HIGH OFF --- HIGH OFF --- HIGH OFF --- HIGH ON ---- LOW ON ---- LOW ON ---- LOW Coil D DA3 OFF --- HIGH OFF --- HIGH ON ---- LOW ON ---- LOW ON ---- LOW OFF --- HIGH OFF --- HIGH OFF --- HIGH OFF --- HIGH

Reading the Gray Code Value. Port B of the USB interface (Bits DB0 DB6) is used to read the seven bit Gray-encoder value. Port B must be configured as an Input to allow this to occur. Note: The Stepper Motor has 96 discrete steps / revolution, when used in half-step mode. Seven-bit Gray code has 128 states. Some method is required to detect the transition from step 95 (the last step) to step 0 (the first step) and vice-versa, refer to Figure 2. An index bit (Bit DB7) is provided to enable the software to detect the latter states thus preventing spurious logic transitions from occurring. You will NOT be required to use the index bit in these exercises, but you should notice the occurrence of indeterminate logic values (read via. Port B) when there is a transition through the index position.
P. Eastwood & R. Gross, 2012-02-27

Computer Control of a Stepper Motor Unit

Step 0 Index marker Step 95

Figure 2 Getting Started You should login on the computer, using your network username and password. Leave the default workstation username (uos) as it is and type in the password: real. To become familiar with the system and software, you will compile and run the source code to energise one coil of the stepper motor. Exercise 1: Compile and run the program type 1 from the menu 10 minutes

The source code is in the file stepper.c. Before compiling and running the program you need to copy the project files to your own directory as follows: Download and extract usb-stepper.zip from MOLE. Copy the folder usb-stepper to the local hard disk, for example in My Documents. Open My Documents

To compile and run the example, do the following: Double-click on stepper.dsw - this will open the stepper.dsw project in the Visual C++ development environment. Make sure that workspace view is enabled (ALT+0). In the workspace view window select the File View tab and click on stepper.c. This will open the source file for your program. To compile the program use CTRL+F7 . To compile and link the program use F7. To run the program use CTRL+F5.

This program will turn on coil A of the stepper motor. Press the Esc key to exit the program.
/******************************************************************** * Name: Exercise_One * Arguments: none * Returns: nothing * * Turns Stepper Motor Coil 'A' on. * P. Eastwood & R. Gross, 2012-02-27

Computer Control of a Stepper Motor Unit


********************************************************************/ void Exercise_One() { cbDOut(BOARD_NUM_0,FIRSTPORTA,0xfe); }

In your report, explain which command (one line is sufficient per case) is necessary to turn on: Coil C only. Coils C and D (both on) only. Explain the reason why your answer is correct.

2. The Source Code


This section contains a brief overview of the source code for the stepper unit. Pre-processor Directives The source code of a C program is analysed, firstly, by the pre-processor before it is compiled. The following #include pre-processor directives are used in the stepper.c program:
/* Include files */ #include <windows.h> #include <stdio.h> #include <conio.h> #include "..\cbw.h"

These directives instruct the pre-processor to include the contents of the respective filenames into the current file before attempting to compile the program. The statement:
#include "..\cbw.h"

instructs the pre-processor to include all functions definitions (prototypes) which are used by the PMD 1208LS data acquisition device. A set of #define directives are used to allocate a fixed reference or constant e.g.
/* USB device parameters */ #define BOARD_NUM_0 0 // USB device number 0

Local Variables Variables which are declared within a function can only be accessed within the same function e.g.
void Exercise_Four( ) { unsigned short Step_Direction int Step_Rate; }

Global Variables Variables which are defined before the main() function can be accessed from any function. Global variables are used to store or track data which needs to be modified or accessed by more than one function e.g.
P. Eastwood & R. Gross, 2012-02-27

Computer Control of a Stepper Motor Unit


unsigned short variable Stepper_Number; // defines an 8 bit unsigned (0 255)

Functions The program contains a number of functions, some have already been written. You may use these within your code as required. Some functions need to be written during this laboratory session. Each of the exercises is written as a function. Only modify the section of code within these exercise functions. If your program locks up, press the Ctrl and Break keys simultaneously to return to the Visual C++ environment screen. Exercise 2: Compile and run the program type 2 from the menu 10 minutes

A section of code is provided to operate the stepper motor CW by 14 steps in Full Step mode. Your task is to step the motor CCW by 14 steps in Full Step mode, so the complete sequence starts and finishes on the same step number.
/******************************************************************** * * Name: Exercise Two * Arguments: none * Returns: nothing * * Step Motor in Full Step Mode: 14 steps CW and 14 steps CCW * ********************************************************************/ void Exercise_Two() { unsigned int a; for(a=0; a<14; a++) // 14 steps CW direction { Full_Step_CW(); Sleep(1000); } // Your code here }

Examine this section of code, in particular the function Full_Step_CW() it uses. Write function Full_Step_CCW() and include it in your report. Explain how function Full_Step_CCW() works in your report. Write the section of code ("// Your code here" above) to use this function and include it in your report. Explain what this section of code does in your report. 10 minutes

Exercise 3: Compile and run the program type 3 from the menu

In this exercise you will write two functions to operate the stepper motor in half step mode using a lookup table of the Stepper Motor Coil Sequences required (refer to Tables 3 and 4 provided in this script). The 'C' format of the CW table, used by the Half_Step_CW Function should be written as follows:
Unsigned short Half_Step_CW_Table[8] = {data1,data2,..data8};

Where data1..data8 represent the 8 stepper motor coil sequence values.


P. Eastwood & R. Gross, 2012-02-27

Computer Control of a Stepper Motor Unit Use an indexing method to select the appropriate data from the tables (CW or CCW) to drive the stepper motor coils. You need to write the following three functions used by this exercise: Half_Step_CW() Half_Step_CCW() Exercise_Three() Include all three functions inside your report. Moreover, explain how function Half_Step_CW() and Exercise_Three() work.
/******************************************************************** * * Name: Exercise_Three * Arguments: none * Returns: nothing * * Step Motor in Half Step Mode 24 steps CW and 24 steps CCW * ********************************************************************/ void Exercise_Three() { // Your code here } /************************************************************************** * * * Name: Half_Step_CW * Arguments: stepper phase number * Returns: nothing * * Rotates Stepper Clockwise using Half step mode * *************************************************************************** / void Half_Step_CW() { // Your code here } /*** Write the following Function: Half_Step_CCW ***/ /************************************************************************** * * * Name: Half_Step_CCW * Arguments: stepper phase number * Returns: nothing * * Rotates Stepper Motor Anti-Clockwise using Half step mode * *************************************************************************** / void Half_Step_CCW() { // Your code here } P. Eastwood & R. Gross, 2012-02-27

Computer Control of a Stepper Motor Unit Exercise 4: Compile and run the program type 4 from the menu 25 minutes

The purpose of this exercise is for you to write the function: Stepper_Control() Three parameters are passed to the latter function via the following Exercise. Analyse the code within this exercise before you start to write any code.
/************************************************************************** * * Name: Exercise_Four * Arguments: none * Returns: nothing * * Call to Function: Stepper_Control() passing the parameters: * * (i) Stepper Direction (CW or CCW) * (ii) Time Between Steps * (iii) Number of Steps * * Stepper_Control() should set the initial time between steps to 500ms. * Include code to increase the step rate by subtracting 10ms after each * successive step, until the desired step rate (time between steps * passed to Stepper_Control) is reached, hence generating an * acceleration-effect of the stepper motor speed. * * Also, include code to start decelerating the stepper motor by 10ms / step * 30 steps before the end of a CW or CCW sequence. * * Display the step rate at location 40(x) 17(y) on the PC display. * * * Note: Use Half-Step Mode. * *************************************************************************** / void Exercise_Four() { unsigned short Step_Direction; int Time_Between_Steps; unsigned int Number_Of_Steps;

// CW or CCW rotation // In milliseconds // Steps required

Step_Direction = CW; // CW Time_Between_Steps = 40;// Corresponding to desired Max. Step Rate Number_Of_Steps = 100; // Steps requested

Stepper_Control(Step_Direction,Time_Between_Steps,Number_Of_Steps); Sleep(1000); // 1 Second delay

Step_Direction = CCW; // CCW Time_Between_Steps = 20; // Corresponding to desired Max. Step Rate Number_Of_Steps = 200; // Steps requested

Stepper_Control(Step_Direction,Time_Between_Steps,Number_Of_Steps); Sleep(1000); P. Eastwood & R. Gross, 2012-02-27 // 1 Second delay

Computer Control of a Stepper Motor Unit


Stepper_Control(CW,50,100); } // Shortened method

Write and test the following Function: Stepper_Control(...). Show your system to the demonstrator who will take notes. Include function Stepper_Control(...) into your report. Explain in detail how it works and how parameters are passed to it.
/******************************************************************** * * Name: Stepper_Control * Arguments: Step_Direction,Time Between Steps,Number_Of_Steps * Returns: nothing * * Controls the stepper motor direction, time between steps & number of steps (half step mode) * ********************************************************************/ void Stepper_Control(unsigned short dir,int time_between_steps, unsigned int number) { // Your Code Here }

Exercise 5: Compile and run the program type 5 from the menu

20 minutes

In this Exercise, a partial function has been provided to convert 7 bit Gray code to 7 bit Binary Code using a BIT manipulation solution. Examine the code and comments, which has been provided within this exercise, to understand the methods used. Write the section of code in function Gray_to_Bin that is missing and include it in your report. Explain this section of code in every detail - what does it perform and why is this necessary.
/******************************************************************** * * Name: Exercise_Five * Arguments: none * Returns: nothing * * Read the Gray-encoded value from the optical disc. * Convert this value to Binary call to function: Gray_to_Bin() * Display the resulting value as an unsigned INT at location 40(x),17(y). * Turn the disc slowly by hand to verify the codes change. * Press ANY key to end this exercise. * ********************************************************************/ void Exercise_Five() { unsigned short Gray_Value_From_Disc; unsigned short Binary_Value_From_Disc; MoveCursor (10,20); // Position cursor printf("Press ANY key to return to menu "); while(!kbhit()) // Press Any key to end loop { cbDIn(BOARD_NUM_0,FIRSTPORTB,&Gray_Value_From_Disc); P. Eastwood & R. Gross, 2012-02-27

10

Computer Control of a Stepper Motor Unit


Binary_Value_From_Disc = Gray_to_Bin(Gray_Value_From_Disc); MoveCursor (40,17); printf(" "); MoveCursor (40,17); printf("%u",Binary_Value_From_Disc); // Display the value } } /******************************************************************** * * Name: Gray_to_Bin * Arguments: 7 bit int Gray Code value * Returns: 7 bit int Binary Code value * * Converts a 7 bit Gray Code to Binary * * Gray Value bits ---- MS bit > G6 G5 G4 G3 G2 G1 G0 * * Binary Value bits -- MS bit > B6 B5 B4 B3 B2 B1 B0 * * B6 = G6 // always the same * B5 = B6 ^ G5 // Exclusive OR (^) the bits together to construct the * B4 = B5 ^ G4 // 7 bit binary value. * B3 = B4 ^ G3 * B2 = B3 ^ G2 * B1 = B2 ^ G1 * B0 = B1 ^ G0 * ********************************************************************/ unsigned { unsigned unsigned unsigned short Gray_to_Bin(unsigned short Gray) // short Bin; short i; short mask; // Initial mask // Mask bit 7 (Index Bit) // Set B6 = G6 // Set B5, B4, ..., B0 solution provided

mask = 0x40; Bin = 0; Gray &= 0x7f; Bin = Gray & mask; for (i=0; i<6; i++) { // Your code here } return Bin; }

Exercise 6: Compile and run the program type 6 from the menu

25 minutes

In this exercise you are required to operate the stepper motor continuously between two locations 28 and 48 and display the step position at location 40(x),17(y) on the PC display. You should include code to allow the exercise to terminate when a non-control or non-shift key is pressed.
/******************************************************************** * P. Eastwood & R. Gross, 2012-02-27

11

Computer Control of a Stepper Motor Unit


* Name: Exercise_Six * Arguments: none * Returns: nothing * * * Operate the stepper motor in half-step mode between two step locations * 28 and 48 continuously at 1 second / step using the values read from the * optical encoder. Think of a solution using closed loop control. * * Convert this value to Binary - call function: Gray_to_Bin() * Display the resulting value as an unsigned INT at location 40(x),17(y). * * Press ANY key to end this exercise. * ********************************************************************/ void Exercise_Six() { // Your Code Here }

Write and test the above section of source code. Show your system to the demonstrator who will take notes. Include the section of source code in your report and explain in detail how it works.

Exercise 7: Answer the following two questions in your report (can be done at home) (a) Explain the advantages and disadvantages of stepper motors for control purposes. (b) Explain why Gray-encoded discs are used in positional control, rather than discs using natural binary code.

P. Eastwood & R. Gross, 2012-02-27

12

Computer Control of a Stepper Motor Unit

cbDIn()
Reads a digital input port. Note that for some port types, such as 8255 ports, if the port is configured for DIGITALOUT, this function will provide readback of the last output value. Function prototype: C/C++: Visual Basic: Delphi: Arguments:
BoardNum The number associated with the board when it was installed with the InstaCal int cbDIn(int BoardNum, int PortNum, unsigned short *DataValue) Function cbDIn(ByVal BoardNum&, ByVal PortNum&, DataValue%) As Long function cbDIn(BoardNum:Integer; PortNum:Integer; var

DataValue:Word):Integer; StdCall;

configuration program. BoardNum may be 0 to 99.


PortNum Specifies which digital I/O port to read. Some hardware does allow readback of the state

of the output using this function. Check the board specific information in the Universal Library Users Guide. Table 6-1 on page 98 shows which ports are in which 82C55 and 8536 digital chips. The most 82C55 chips on a single board is eight, on a CIO-DIO192. The most 8536 chips on a board is two, on the CIO-INT32.
DataValue

Digital input value returned here.

Returns: Error code or 0 if no errors. DataValue - Digital input value returned here. Notes: The size of the ports vary. If it is an eight bit port then the returned value will be in the range 0 - 255. If it is a four bit port the value will be in the range 0 - 15. Refer to the example programs and the board-specific information contained in the Universal Library User's Guide (available on our web site at www.mccdaq.com/PDFmanuals/sm-ul-user-guide.pdf) for clarification of valid PortNum values.

P. Eastwood & R. Gross, 2012-02-27

13

Computer Control of a Stepper Motor Unit

cbDOut()
Writes a byte to a digital output port. If the port type is not AUXPORT, you must use cbDConfigPort() to configure the port for output first. If the port type is AUXPORT, you may need to use cbDConfigPort() to configure the port for output first. Check the board specific information in the Universal Library User's Guide (available on our web site at www.mccdaq.com/PDFmanuals/sm-ul-user-guide.pdf) to determine if AUXPORT should be configured for your hardware. Function prototype: C/C++: Visual Basic: Delphi: Arguments:
BoardNum PortNum int cbDOut(int BoardNum, int PortNum, unsigned short DataValue) Function cbDOut(ByVal BoardNum&, ByVal PortNum&, ByVal DataValue%) As Long function cbDOut(BoardNum:Integer; PortNum:Integer; DataValue:Word):Integer;

The number associated with the board when it was installed with the InstaCal configuration program. BoardNum may be 0 to 99. There are three general types of digital ports - ports that are programmable as input or output, ports that are fixed input or output and ports for which each bit may be programmed as input or output. For the first of these types, set PortNumto FIRSTPORTA. For the latter two types, set PortNumto AUXPORT. Some boards have both types of digital ports (for example the DAS1600 Series). Set PortNumto either FIRSTPORTAor AUXPORT depending on the digital port you want to set. Table 6-1 on page 98 shows which ports are in which 82C55 and 8536 digital chips. The CIO-DIO196 has eight 82C55 chips the most on a single board. The CIOINT32 has two 8536 chips the most on a single board.

DataValue

Digital input value to be written.

Returns: Error code or 0 if no errors. Notes: The size of the ports vary. If it is an eight bit port then the output value should be in the range 0 255. If it is a four bit port the value should be in the range 0 15. Refer to the example programs and the board-specific information in the Universal Library User's Guide for clarification of valid PortNum values.

P. Eastwood & R. Gross, 2012-02-27

14

Potrebbero piacerti anche