MD-2 Dual Stepper Motor System MD2QB2S.TXT Quick-Basic Level 2 Motion Control Subroutines Documentation Copyright (c) 1994 Arrick Robotics FILE NAMES: File names describe their contents. Here are some of the posibilities: MD2QB2S.BAS <---- .BAS = Basic source code. | | || .TXT = (Text) documentation file. | | || .BI = Basic include file. | | || .EXE = Executable program. | | || .MAK = Make file. | | || .FRM = Form. | | || .INI = Configuration & parameters. | | || | | | ------------- S=Subroutine Library. | | | E1=Example program #1, etc. | | | F1=Form #1, etc. | | | T=Programming tools. | | | | | ----------- 1 = Level 1 subroutine library. | | 2 = Level 2 subroutine library, etc. | | | --------- QB = Quick Basic. | QI = Q Basic Interpreter. | VD = Visual Basic for DOS. | VW = Visual Basic for Windows. | BI = Basic Interpreter with line #'s. | QC = Quick C. | -------- MD2 = MD-2 Dual Stepper Motor System. LEVEL 2 SUBROUTINE LIBRARY OVERVIEW: Different levels of motion control subroutines are available to provide different levels of sophistication, features and performance. The level 2 subroutines offer powerful motion control tools to programmers who need to create custom automation programs using the MD-2 dual stepper motor system from Arrick Robotics. The library includes all the subroutines necessary to control up to 6 motors. All motor parameters are global variables that can be used by all modules. The program simply sets the desired parameters such as speed, direction and distance and calls the subroutine. The library is very easy to use and can be easily connected to other subroutine libraries such as those provided with data acquisition systems. These subroutines have the following features: Complete control of all motion parameters. Control up to 6 motors on one computer. Selectable half step or full step mode. Single or dual motor moves. Programmable acceleration/deceleration. Linear interpolation on 2 motors. Circular interpolation on 2 motors. Arc and elipitical moves. Grid coordinate moves. Absolute and relative moves. Motor homing using switches. Conversion of units (inches, degrees, etc) to steps. Backlash compensation for improved accuracy. Programmable limits to prevent over travel. Standby mode conserves power and reduces heat. Control of input/output port available on some units. Saving, loading and editing of motor parameters. Saving, loading and running of motion sequences. THE MD-2 SYSTEM: The MD-2 dual stepper motor system is a complete dual axis motion control system that connects to any IBM style personal computer. The system comes complete with 2 motors, cables, drive electronics, software and documentation. Simply connect the MD-2 to a printer port and load the software. These motors can be used to control a wide range of items including robotic arms, telescopes, cameras and lasers. The operator and programmer have complete control over the speed, direction and other parameters of each motor. Sophisticated motion control programs can be created to accomplish almost any job. PORTS AND MOTOR NUMBERS: The MD-2 system connects to the parallel printer port of any IBM style personal computer using a standard printer cable. There can be as many as 3 parallel printer ports on a single computer. Since each port can be attached to an MD-2, a total of 6 motors can be controlled with a single computer. Each port has its own unique address. The three possible addresses are 3BC, 378 and 278. When adding a new printer port, make sure that no two ports have the same address. The MD-2 software refers to the motors connected to the MD-2 which is connected to port 3BC as 1 & 2, port 378 as 3 & 4, and port 278 as 5 & 6. Your system may only have one or two ports. Since the motor numbers are determined by which port they are connected to, your system may have motors 3 & 4 or 5 & 6 but not 1 & 2. You may wish to keep your 3BC port connected to your printer so it can be refered to as LPT1: or PRN: in which case your MD-2 motors would be 3 & 4 or 5 & 6. Parallel printer port cards are very inexpensive and are normally available at local computer stores or by mail-order. These cards normally have jumpers on them to set the desired address. SUBROUTINE DESCRIPTION: The subroutine source code file contains several subroutines that can be used by the programmer to control the MD-2 system. These subroutines are contained in SUB's to make them easier to use. All motor parameters to and from these SUB's are in the form of global variables. To use the SUB, the programmer simply sets the motor parameter variables to the desired settings and calls the SUB. PUBLIC SUBROUTINES --------------------------------------------------- NAME DESCRIPTION ---- ----------- MD2SETUP Used at the beginning of a program to initialize motor parameters to their default values and checks for parallel port availability. Loads calibration file. Use this SUB before any other. MD2CALIBRATE Calibrates motor speeds with computer speeds and saves to disk. Use this SUB once on your cpu before moving. MD2ON Turns on an MD-2 system. Use before any moves. MD2OFF Turns off an MD-2 system. Use at end of program. MD2HOME Moves a motor to the Home position by watching the home switch. Current position is set to zero. MD2MOVE Moves a motor to the desired target position using the various motor parameters. Also moves two motors together providing linear interpolation. MD2CIRCLE Performs 2-axis circular, arc and ellipise moves. MD2GRID Moves 2 motors using grid coordinates. MD2OUTPUTS Turns on and off output pins to control tools. MD2INPUTS Reads condition of input pins and home switches. MD2STANDBYON Puts MD-2 into standby mode which reduces motor current and heat while maintaining some holding torque. MD2STANDBYOFF Turns off standby mode giving full current to motors. MD2PARLOAD Loads motor parameters from a disk file. MD2PARSAVE Saves motor parameters to a disk file. MD2PAREDIT Allows editing of motor parameters. MD2SEQLOAD Loads a sequence of motion commands from a disk file. MD2SEQSAVE Saves a sequence of motion commands to a disk file. MD2SEQRUN Runs a sequence of motion commands. PUBLIC MOTOR PARAMETERS AND VARIABLES ---------------------------------- (M) indicates motor # 1,2,3,4,5 or 6. NAME TYPE DESCRIPTION ---- ---- ----------- MD2BACKLASH(M) SINGLE Backlash compensation in units. Adds this amount to every move that changes direction. Uses MD2LASTDIR(M). Value is in Units. MD2ENABLED12 INTEGER Port 3BC for motors 1 & 2 enabled status. 0=disabled , -1=enabled. MD2ENABLED34 INTEGER Port 378 for motors 3 & 4 enabled status. 0=disabled , -1=enabled. MD2ENABLED56 INTEGER Port 278 for motors 5 & 6 enabled status. 0=disabled , -1=enabled. MD2HOLD INTEGER -1=Leaves the motor energized after a move to cause the motor to hold its position. 0 causes the motor to turn off after a move which conserves power and reduces heat. MD2HOMEOFFSET(M)SINGLE Distance from home switch to call home. Value is in units. MD2HOMEDIR(M) INTEGER Direction to home switch, 0=rev, -1=fwd. Reverses meaning of forward and reverse target directions. All directions are reversed. MD2INTERRUPTS INTEGER 0=turns COM port interrupts off during moves which prevents mouse & modem interference. This can be important under WINDOWS. -1 leaves interrupts enabled. MD2LIMITF(M) SINGLE Forward soft limit in units. Prevents moves beyond this value in the forward direction. Value is in Units. MD2LIMITR(M) SINGLE Reverse soft limit in units. Prevents moves beyond this value in the reverse direction Value is in Units. MD2MOTOR INTEGER The selected motor to act upon. 1,2,3,4,5 or 6. Set to 12, 34, 56 for dual motor moves. MD2MOTORNAME(M) STRING User definable motor name. MD2MAXSPEED(M) SINGLE Motor fastest speed after accelerating. In Units per Second. MD2MINSPEED(M) SINGLE Motor speed at the beginning and end of ramping. In Units per Second. MD2MOVETYPE STRING Tells the MD2MOVE sub what type of move. R=relative, A=absolute. Affects all motors. With relative moves, the MD2TARGET parameter tells how many steps to move from the current position. Negative numbers are reverse, positive numbers are forward. With absolute moves, the MD2TARGET parameter tells what position to move to. The number of steps and direction are determined by the current position. Negative numbers are reverse from home and positive numbers are forward. negative then reverse, positive then forward. MD2AVAILABLE12 INTEGER Port 3BC availability for motors 1 & 2 set by the MD2SETUP sub. 0=not available, -1=available. MD2AVAILABLE34 INTEGER Port 378 availability for motors 3 & 4 set by the MD2SETUP sub. 0=not available, -1=available. MD2AVAILABLE56 INTEGER Port 278 availability for motors 5 & 6 set by the MD2SETUP sub. 0=not available, -1=available. MD2POSITION(M) SINGLE Current motor position for each motor (M=motor #) relative to home. Negative = reverse from home and positive = forward from home. Value is in units. MD2SLOPE(M) INTEGER The slope of the ramp (acceleration/deceleration) in number Units. A 2 would mean that the motor would accelerate from the MD2MINSPEED to the MD2MAXSPEED in 2 units. MD2STATUS STRING Completion status. O=motion completed OK, K=a keypress stopped the motion, B=bad parameter, L=soft limit was exceeded. E=file error. MD2STEPTYPE STRING H=Half step, D=Double phase full step, S=Single phase full step. Affects all motors. MD2TARGET(M) SINGLE The number of units to move in relative moves, the position to move to in absolute moves. Value is in units which is converted to steps. MD2UNITS(M) LONG Converts user definable units to steps for MD2MOVE subroutine. This is number of steps in each unit. MD2UNITNAME(M) STRING This is the name of the units such as steps, inches, degrees, etc. PUBLIC INPUT/OUTPUT PORT PARAMETERS---------------------------------- MD2OUTPUTCODE INTEGER Tells the MD2OUTPUTS sub which MD-2, which output pin and on/off action. MD2INPUTS(CODE) INTEGER Contains status of input pins and home switches after using MD2INPUTS sub. PUBLIC PARAMETER & SEQUENCE FILE VARIABLES--------------------------- MD2COMMAND STRING Single command to execute. MD2SEQUENCE STRING Contains a motion sequence. MD2SEQFILE STRING Sequence file name. MD2PARFILE STRING Parameter file name. MD2POINTER STRING Sequence file pointer. PUBLIC GRID MOVE PARAMETERS------------------------------------------- MD2GRIDBEGINX SINGLE Grid X start position in units. MD2GRIDBEGINY SINGLE Grid Y start position in units. MD2GRIDSPACEX SINGLE Grid X spacing in units. MD2GRIDSPACEY SINGLE Grid Y spacing in units. MD2GRIDTARGETX INTEGER Grid X target. MD2GRIDTARGETY INTEGER Grid Y target. PUBLIC CIRCLE/ARC MOVE PARAMETERS------------------------------------ MD2CIRCLERADIUSX SINGLE Circle X radius in units. MD2CIRCLERADIUSY SINGLE Circle Y radius in units. MD2CIRCLECENTERX SINGLE Circle X center in units. MD2CIRCLECENTERY SINGLE Circle Y center in units. MD2CIRCLESTART INTEGER Circle start angle in degrees. MD2CIRCLEARC INTEGER Circle arc angle in degrees. -=CW,+=CCW. MD2CIRCLECHORD INTEGER Circle chord angle in degrees. PUBLIC MOTOR SPEED CALIBRATION PARAMETERS----------------------------------- MD2VELOCITY(24) SINGLE Fastest possible motor speed in steps/sec. MD2DELAY(24) SINGLE Slowest possible motor speed in steps/sec. MD2CALFILE STRING Calibration file name. --------------------------------------------------------------------- UNITS CONVERSION: Stepper motors move in small steps, usually .9 degrees. Normally the stepper motors are attached to some device which converts these steps to rotational motion or linear motion. In most cases, the user dosn't care how many motor steps must be given to reach a desired location, but is interested only in the distance traveled. This distance is often feet, inches, millimeters, or degrees. The MD2UNITS(M) and MD2UNITNAME(M) parameters contain user-definable units and are used to convert from the users units to steps. The MD2UNITS(M) parameter contains the number of steps in each unit. The MD2UNITNAME(M) parameter contains the name of the units. For example, if a positioning table has 200 steps for each inch, the user would set the MD2UNITS(M) parameter to 200 and set the MD2UNITNAME(M) parameter to "inches". The programmer can now deal with motor positions in terms of units (inches) instead of steps. This affects the MD2POSITION(M), MD2TARGET(M) and MD2BACKLASH(M) parameters which are set in units. Setting the MD2UNITS(M) parameter to 1 and the MD2UNITNAME(M) parameter to "steps" will allow the programmer to deal with motor steps. Units conversion can reduce confusion during programming and can also allow easy changes to the mechanical system. For example, if you create a program to control a positioning table that has 200 steps per inch and then switch to a table that has 1000 steps per inch, simply change the units parameters and run the program without modification. Adjusting units can also result in a changing size of the motor moves. This can be used to enlarge or reduce motion sequences. For example, if 2 motors are being used to control an XY positioning table that moves a router to cut square shapes out of plastic, changing the MD2UNITS(M) parameter will result in smaller or larger squares without changing the program. Units conversion parameters are set to 1 and "Steps" as defaults by the MD2SETUP subroutine. The MD2UNITNAME(M) parameter is not used by the MD-2 software but can be used by the custom program if needed. HOMING MOTORS: Each motor on the MD-2 system has a home switch associated with it. At the beginning of a program, the software does not accuratly know the positions of the motors. Homing causes the motor to seek the home switch to establish the home position (position zero). All moves are relative to this home position. The programmer or operator can, at any time, home the motors to insure accurate positioning. This is not necessary in most situations where the home function is only used at the beginning of a program once. The MD2HOME subroutine moves the motor reverse (clockwise as viewed from the front of the motor) until the home switch is activated, then forward until the switch is deactivated. This sequence has the effect of preloading the mechanical system in the forward direction which will increase the accuracy of systems which have backlash or belt stretch. A new home position can be established by homing the motor normally, moving to the desired location, then setting the MD2POSITION(M) parameter to zero. All motions thereafter will be relative to this new home position. This can be used to duplicate motion sequences for step and repeat operations without modifications to the fundamental program. The MD2HOMEOFFSET(M) parameter determines how far off of the home switch to move after it is pressed and released. The offset value is in units and can be used to duplicate motion sequences at various places on the positioning system. The MD2HOMEDIR(M) parameter determines the direction of the home switch which then determines the direction of all other moves. Normally the MD2HOMEDIR(M) parameter is set to 0 for reverse. Set it to -1 for forward. This parameter is useful because mechanical systems such as gear boxes and lead screws often change the motor directions. During home moves, the motor will move at the MD2MINSPEED, ramping does not apply. The home switches may also be used as general purpose inputs and can be read by the program through the MD2INPUTS subroutine. MOVING MOTORS: The MD2MOVE subroutine allows both single and dual motor moves. For single motor moves, the programmer simply sets the MD2MOTOR parameter to a valid motor # (1,2,3,4,5 or 6) along with other motor parameters and calls the MD2MOVE subroutine. When the move is complete, the MD2STATUS variable will be set to O for OK, B for bad parameter, K to indicate that the move is stopped by a keypress or L to indicate that the soft limit was exceeded. When the move was stopped by a keypress, the MD2POSITION(M) may not accuratly reflect the motors position since the motor was stopped abruptly and not decelerated. It may be necessary to home the motor if this occurs to reestablish correct positions. For dual motor moves, the programmer must set the MD2MOTOR parameter to a valid pair of motors on an MD-2 system (12, 34 or 56) along with their various parameters. When the MD2MOVE subroutine is called, both motors will begin moving and will end at the same time. This action will result in linear interpolation which means that the motors would move in a straigt line if connected to an XY positioning table or a robotic arm having a rectangular coordinate system. In dual motor moves, the speed parameters (MD2MINSPEED(M), MD2MAXSPEED(M), and MD2SLOPE(M) ) of the motor moving the longest distance will be used. With either single or dual motor moves, the motor(s) will start moving using the MD2MINSPEED(M) parameter and accelerate to the MD2MAXSPEED(M) parameter then decelerate at the end of the move back to the MD2MINSPEED(M). The acceleration/ deceleration ramping is determined by the MD2SLOPE parameter which indicates how many units is required to go from the minimum speed to the maximum speed and back. RELATIVE & ABSOLUTE MOVES: When moving a motor the programmer has two choices, relative or absolute moves. This is set using the MD2MOVETYPE parameter to "R" or "A". In relative moves, the motor will move the number of units in the MD2TARGET(M) parameter, positive numbers are forward and negative numbers are reverse. In absolute moves the motor will move to the position indicated by the MD2TARGET(M) parameter. Position values are in units from home, with positive numbers indicating positions forward from home and negative numbers reverse from home. In absolute moves the motor will take into account its current position and determine which direction and step count is needed to move to the desired position. The direction of forward and reverse can be swapped by using the MD2HOMEDIR(M) parameter. Both linear and circular moves are affected by the move type parameter. MOTOR SPEEDS: Motor speeds are determined by the frequency of the pulses sent to the MD-2 system by the computer and are programmable using the MD2MINSPEED(M), MD2MAXSPEED(M) and MD2SLOPE(M) parameters. The minimum speed and maximum speed values are in units per second. The MD2CALIBRATE subroutine must be used on a particular computer once to insure proper speed calculation. This calibration file is load by the MD2SETUP subroutine at the beginning of each program. The calibration file and its use is transparant to the programmer. The minimum and maximum speed ranges are determined by the processing speed of the computer. Fast computers are capabable of sending faster step pulses to the motors. If pulses are sent to the motor too fast, the result will be erratic motion or simply vibration. This does not harm the motor or MD-2 system. The torque of a stepper motor decreases as the speed increases. This may cause the motor to move a load at slow speeds but miss steps at faster speeds. Homing the motor can always be used to resyncronize the system if this occurs. Experimentation will be necessary to determine the optimum speed for a given motor, mechanical system and load. RAMPING (ACCELERATION/DECELERATION): Ramping means accelerating the motor from a slow speed to a maximum speed gradually then decelerating back to the slow speed when the motion is done. Ramping greatly increases the speed at which a motor can move a load and greatly reduces the amount of vibration and jerk in a system. Three parameters are used to accomplish ramping: MD2MINSPEED(M), MD2MAXSPEED(M) and MD2SLOPE(M). The MD2MINSPEED(M) is the speed at which the motor begins to move at and ultimately ends at. The MD2MAXSPEED(M) parameter is the speed at which the motor moves after ramping up (accelerating) and before ramping down (decelerating). MD2SLOPE(M) is the rate at which the speed changes given in number of units. If the MD2SLOPE(M) parameter is set to 100 then it will take 100 units for the motor to go from the MD2MINSPEED(M) to MD2MAXSPEED(M) during acceleration and also 100 units for the motor to go from MD2MAXSPEED(M) back down to MD2MINSPEED(M) when done. Set the MD2MINSPEED(M) to a value that the motor can always start and stop at without missing steps or overshooting. Set the MD2SLOPE(M) value to a number that allows the motor to ramp quickly without missing steps. Set the MD2MAXSPEED(M) to a speed faster than the MD2MINSPEED(M) parameter but one that does not cause lost steps. Experimentation will provide the best values for these parameters resulting in smooth motion and high speeds. Setting the MD2SLOPE(M) parameter to 0 will cause the motor to move at a constant speed determined by the MD2MAXSPEED(M) parameter without ramping. The MD2HOME subroutine does not ramp the motor to prevent overshoot. The home speed is determined by the MD2MINSPEED(M) parameter only. BACKLASH COMPENSATION: Most mechanical systems include gears, chains, belts or other devices that contain looseness known as backlash. This results from small spaces between gear teeth, belt stretch, etc. Backlash is not a problem as long as the motor moves in the same direction. As soon as the motor changes direction, the backlash results in a positioning error since the motor must move additional distance to take up the looseness. This backlash could be eliminated as a source of positioning error by making sure that the desired position is always approached from the same direction. This may require overshooting the desired position and returning to it from the other direction. Another way to solve the backlash problem is to compensate for it in software by adding steps whenever the motor changes direction. The MD2BACKLASH(M) parameter is used to do just that. Set this parameter to the number of units of backlash in the mechanical system. Every time the motor changes direction, the backlash will be added to the desired distance. This will result in greatly improved bi-directional repeatability which means that the position will be accuratly acquired no matter which direction it is approached from. The MD2LASTDIR(M) variable is maintained by the software to keep track of the motor's last direction. STEP TYPES: Stepper motors operate by energizing their windings (phases) in certain sequences called phase patterns. Changing from one pattern to another causes the motor to move one step. There are 3 different types of phase pattern sequences. In half step mode, the motor alternates between one and two motor phases being energized which results in steps that are half of the typical full step. Half steps are usually .9 degrees but differ depending on the motor. Half step mode is the most common mode since it has twice the resolution of full step modes which reduces vibration. There are two types of full step modes. Double-phase full step mode always energizes two motor phases at a time which results in more torque and motor heat. Single-phase full step mode always energizes one motor phase at a time which results in less torque and less motor heat. Both full step modes normally result in steps that are 1.8 degrees. The MD2STEPTYPE parameter allows the step type to be changed. Set it to "H" for half step mode, "S" for single-phase full step mode and "D" for double-phase full step mode. In most applications half step mode should be used to minimize vibration and increase resolution. HOLDING TORQUE AND STANDBY MODE: When a stepper motor is energized but not moving it produces holding torque which will cause the motor's shaft to resist movement. While the motor is in this holding mode, heat is generated. The programmer can turn off this holding torque and cause the motor to de-energize after each motion using the MD2HOLD parameter. Set MD2HOLD to 0 to remove holding or to -1 to enable holding. If some holding torque is necessary but heat is a problem, standby mode can be enabled which cuts motor torque in half. Simply set the correct motor number or motor pair and call the MD2STANDBYON or MD2STANDBYOFF subroutine to control this feature. Standby mode affects the entire MD-2 system (2 motors). Moving a motor while in standby mode will greatly reduce the available torque. CIRCLES, ARCS, ELIPISES: The MD2CIRCLE subroutine provides 2-axis circular interpolation, arcs and elipises on XY positioning tables. Simply set MD2MOTOR to a valid motor pair (12, 34 or 56), set the circle parameters and other motor parameters the call. The circle parameters are: MD2CIRCLERADIUSX X-axis radius in units. MD2CIRCLERADIUSY Y-axis radius in units. MD2CIRCLECENTERX X-axis center position or distance. MD2CIRCLECENTERY Y-axis center position or distance. MD2CIRCLESTART Starting angle in degrees. MD2CIRCLEARC Arc angle in degrees. MD2CIRCLECHORD Chord angle in degrees. Set both X and Y radius parameters to the same value for circles and to different values to produce an elipse. Set the arc angle parameter to 360 for a complete circle or elipse or some smaller number for partial circles and elipses which creates arcs. A positive arc angle will produce counter-clockwise motion and negative angles will produce clockwise motion. Each circular motion profile is created by moving in straight lines between points on the circle. The resolution of these points is set using the MD2CIRCLECHORD parameter. Setting the chord angle to 1 will result in a line every one degree. Setting the chord angle to 10 degrees will result in a 36 sided circle. If an arc angle can not be evenly divided by the selected chord angle, the last line segment will be shortend so that the desired arc angle results. The MD2CIRCLE subroutine is affected by the MD2MOVETYPE parameter to perform absolute or relative moves. When using absolute moves, set the circle center parameters to absolute circle center positions. When using relative moves, set the circle center parameters to offsets from the current motor positions. Center and radius values are in units. Start, arc and chord angles are in integer degrees. GRID COORDINATE MOVES: Many motion control applications use XY positioning tables and need to position to a matrix (or grid) of workpieces such as a rack of test tubes. The grid consists of a series of positions on the X axis (columns) and a series of positions on the Y axis (rows). Instead of positioning to a certain cell of the grid by sending distances to the MD2MOVE subroutine, you can simply pass X (column) and Y (row) targets to the MD2GRID subroutine which takes care of the positioning for you. To use grid moves, simply define the grid using the following parameters: MD2GRIDBEGINX X-axis beginning position of the grid in units. MD2GRIDBEGINY Y-axis beginning position of the grid in units. MD2GRIDSPACEX X-axis spacing between cells in units. MD2GRIDSPACEY Y-axis spacing between cells in units. MD2GRIDTARGETX Desired X-axis grid cell (column). MD2GRIDTARGETY Desired Y-axis grid cell (row). The MD2MOTOR parameter must be set to a valid motor pair (12, 34 or 56) and all other motor parameters must be set. MOTION SEQUENCES: Subroutines are provided which will allow you to execute a sequence of motion control commands. The maximum length of a sequence file is 32,000 characters. Sequences are stored on disk in the form of a text file and closly resembles a BASIC program. A sequence can be created and modified using any normal text editor that will save text without formatting information. The MD2SEQSAVE subroutine will save a sequence that is stored in the MD2SEQUENCE variable to a disk file whos name is stored in MD2SEQFILE. The MD2SEQLOAD subroutine will load a sequence from disk into the MD2SEQUENCE variable. Sequence files can contain calls to most motion control subroutines and parameter assignments. This allows complex motion applications to be constructed without programming in BASIC or C. The following subroutines can be called: MD2SETUP, MD2ON, MD2OFF, MD2PARLOAD, MD2PARSAVE, MD2PAREDIT, MD2HOME, MD2MOVE, MD2CIRCLE, MD2GRID, MD2OUTPUTS, MD2STANDBYON, MD2STANDBYOFF. Comments can be included just like BASIC programs. Most sequence files can be loaded and run through Quick-Basic or Visual-Basic/DOS. Here is an example of a motion sequence: MD2SETUP 'INITIALIZE MD2 SYSTEM. MD2MOTOR=34 'TURN ON MD-2 FOR MOTORS 3 AND 4. MD2ON MD2PARFILE="MOTOR.PAR" 'LOAD MOTOR PARAMETERS. MD2PARLOAD MD2PAREDIT 'ALLOW EDITING OF PARAMETERS. MD2MOTOR=3 'MOVE MOTOR 3 HOME. MD2HOME MD2MOTOR=4 'MOVE MOTOR 4 HOME. MD2HOME MD2TARGET(3)=3 'MOVE MOTOR 3, 3 UNITS. MD2MOTOR=3 MD2MOVE MD2TARGET(4)=1 'MOVE MOTOR 4, 1 UNIT. MD2MOTOR=4 MD2MOVE MD2MOTOR=34 'TURN OFF MD-2 SYSTEM. MD2OFF PARAMETER SAVING, LOADING, EDITING: All motor parameters can be loaded from a disk file using the MD2PARLOAD subroutine and saved using the MD2PARSAVE subroutine. All parameters are affected including circle and grid parameters but not sequences or calibration information since they have their own special files. Using parameter files eliminates the need to set each motor parameter manually at the beginning of a BASIC program or motion sequence. The MD2PARFILE variable contains the parameter file name. Several parameter files can be saved to disk and loaded when needed for different motion control projects. The MD2PAREDIT subroutine allows the user to edit motor parameters using the screen and keyboard. This eliminates the need for the programmer to write code to allow the viewing and changing of each parameter. INPUT/OUTPUT PORT: Some MD-2 systems have an Input/Ouput port which can be controlled by the programmer. These signals can be used to read digital sensors and switches and can control tooling by energizing relays. A 14 pin header type connector contains 3 digital input bits, 2 digital output bits, 5VDC, 12VDC and ground. The INPUTS subroutine allows the programmer to read any of the 3 inputs along with the 2 home switches (a total of 5 input bits). After calling the INPUTS sub, the MD2INPUTS() array will contain the status of all 5 input bits for all 3 MD-2 systems. Ignore bits that are associated with ports that are not connected to MD-2 systems. Each array element contains a 0 indicating that an input is not active (not tied to ground) or a -1 indicating that it is active (tied to ground). The following table describes the array. ARRAY ELEMENT MD-2 PORT INPUT CONDITION ------------- --------- ----- --------- MD2INPUT(11) 3BC HOME SWITCH #1 -1=SWITCH ACTIVE (LOW). MD2INPUT(12) 3BC HOME SWITCH #2 -1=SWITCH ACTIVE (LOW). MD2INPUT(13) 3BC GEN PURP IN #1 -1=INPUT ACTIVE (LOW). MD2INPUT(14) 3BC GEN PURP IN #2 -1=INPUT ACTIVE (LOW). MD2INPUT(15) 3BC GEN PURP IN #3 -1=INPUT ACTIVE (LOW). MD2INPUT(21) 378 HOME SWITCH #1 -1=SWITCH ACTIVE (LOW). MD2INPUT(22) 378 HOME SWITCH #2 -1=SWITCH ACTIVE (LOW). MD2INPUT(23) 378 GEN PURP IN #1 -1=INPUT ACTIVE (LOW). MD2INPUT(24) 378 GEN PURP IN #2 -1=INPUT ACTIVE (LOW). MD2INPUT(25) 378 GEN PURP IN #3 -1=INPUT ACTIVE (LOW). MD2INPUT(31) 278 HOME SWITCH #1 -1=SWITCH ACTIVE (LOW). MD2INPUT(32) 278 HOME SWITCH #2 -1=SWITCH ACTIVE (LOW). MD2INPUT(33) 278 GEN PURP IN #1 -1=INPUT ACTIVE (LOW). MD2INPUT(34) 278 GEN PURP IN #2 -1=INPUT ACTIVE (LOW). MD2INPUT(35) 278 GEN PURP IN #3 -1=INPUT ACTIVE (LOW). The MD2OUTPUTS subroutine allows the programmer to control the 2 digital outputs for each MD-2 system. The MD-2 must first be turned on using the MD2ON subroutine for the outputs to become active. To control an output bit, set the MD2OUTPUTCODE variable to the desired code and call the sub. The following table shows the codes needed to turn on and off the digital output bits. Notice the first digit of the code selects which MD-2 system, the second digit selects the pin and the third selects ON or OFF (1 or 0). MD2OUTPUTCODE MD-2 PORT OUTPUT ACTION ------------- --------- ------ ------ 110 3BC #1 OFF 111 3BC #1 ON 120 3BC #2 OFF 121 3BC #2 ON 210 378 #1 OFF 211 378 #1 ON 220 378 #2 OFF 221 378 #2 ON 310 278 #1 OFF 311 278 #1 ON 320 278 #2 OFF 321 278 #2 ON See the MD-2 manual for more information on the MD-2 Input/Output port. PROGRAMMING EXAMPLES: Controlling the MD-2 stepper motor system is simply a matter of setting the desired motor parameters via global variables and calling the appropriate SUB. The following example program shows how to use the subs and parameters. It may be necessary to change the MD2MINSPEED(M), MD2MAXSPEED(M), MD2SLOPE(M) and MD2MOTOR parameters for this program to operate on your computer. It may also be necessary for a path to be included in the INCLUDE command depending on where you place this file. '$INCLUDE: 'MD2QB2S.BI' 'DECLARATIONS OF SUBS AND MODULES. START: MD2SETUP 'SET DEFAULTS AND FINDS PORTS. 'DISPLAY AVAILABLE PORTS. IF MD2AVAILABLE12 THEN PRINT "PORT FOR 1 & 2 IS AVAILABLE." IF MD2AVAILABLE34 THEN PRINT "PORT FOR 3 & 4 IS AVAILABLE." IF MD2AVAILABLE56 THEN PRINT "PORT FOR 5 & 6 IS AVAILABLE." 'TURN ON MD-2 FOR MOTORS 3 & 4. MD2MOTOR = 3 '34 OR 4 WOULD ALSO WORK HERE. MD2ON 'TURN ON MD-2. 'SET MOTOR PARAMETERS THAT ARE DIFFERENT THAN DEFAULTS. MD2MINSPEED(3) = 2500 'SET START/STOP SPEED. MD2MAXSPEED(3) = 1000 'SET MAXIMUM SPEED. MD2SLOPE(3) = 150 'SET ACCEL/DECELERATION IN STEPS. MD2MINSPEED(4) = 4000 'SET SPEEDS FOR MOTOR 4. MD2MAXSPEED(4) = 2000 MD2SLOPE(4) = 150 'MOVE MOTORS 3 & 4 HOME. MD2MOTOR = 3 'MOVE 3 HOME. MD2HOME MD2MOTOR = 4 'MOVE 4 HOME. MD2HOME 'MOVE MOTOR 3 TO POSITION 275. MD2MOTOR = 3 'MOTOR 3. MD2MOVETYPE = "A" 'ABSOLUTE MOVE. MD2TARGET(3) = 275 'POSITION 275. MD2MOVE 'MOVE THE MOTOR. PRINT "STATUS IS ";MD2STATUS 'DISPLAY STATUS. 'MOVE MOTOR 4 400 STEPS FORWARD. MD2MOTOR = 4 'MOTOR 4. MD2MOVETYPE = "R" 'RELATIVE MOVE. MD2TARGET(4) = 400 '400 STEPS FORWARD. MD2MOVE PRINT "STATUS IS ";MD2STATUS 'DISPLAY STATUS. 'DUAL AXIS MOVE (LINEAR INTERPOLATION). MD2MOTOR = 34 'BOTH MOTORS. MD2MOVETYPE = "R" 'RELATIVE. MD2TARGET(3) = 400 'MOTOR 3 TARGET. MD2TARGET(4) = 253 'MOTOR 4 TARGET. MD2MOVE 'MOVE. PRINT "STATUS IS ";MD2STATUS 'DISPLAY STATUS. 'DISPLAY MOTOR POSITIONS. PRINT "MOTOR 3 POSITION IS: ";MD2POSITION(3) PRINT "MOTOR 4 POSITION IS: ";MD2POSITION(4) 'TURN OFF MD-2 SYSTEM. MD2MOTOR = 3 'SELECT MOTOR AT THAT PORT. MD2OFF 'TURN OFF MD-2. END 'DONE. USING QUICKBASIC: QuickBasic is a Basic language compiler that is very easy to use. A Basic program normally consists of several program modules, each providing a certain function. The MD2QB2S.BAS subroutine library is provided in the form of a module. Your program will probably consist of two modules: the MD2QB2S.BAS subroutine module and your custom program module along with the MD2QB2S.BI include file which declares the subs and global variables necessary for other modules to have access to MD2QB2S.BAS. The following sequence can be used to create a custom program using the MD2QB2S.BAS module and the MD2QB2S.BI include file. It should be used as a guildline only. Your system may require different commands and steps. Basic knowledge of DOS, your computer, and QuickBasic is required to accomplish this. You must begin Quick-Basic with the /L switch to allow some functions to work. Make sure that FULL MENUS is selected. 1 Make a directory on the hard disk to contain the MD-2 files and programs. Example from the DOS prompt: MD C:\MD2 (Enter) 2 Copy the files from the distribution floppy disk to the hard disk. Example from the DOS prompt: COPY A:*.* C:\MD2 (Enter) 3 Change to the MD-2 directory. Example: CD \MD2 (Enter) 4 Run QuickBasic. Example: QB /L 5 Select 'Load file' from the FILE menu. FULL MENUS must be ON. 6 Enter MD2QB2S.BAS for loading. 7 Select 'Create file' from the FILE menu and enter a new file name. 8 Enter in your program into the new file window. Don't forget to begin your program with the $INCLUDE statement as in the example above. 9 Select 'Set Main Module' from the RUN menu and enter in your new file name as the main module. 10 Select 'Save As' from the FILE menu to save your custom program. 11 Run your program by selecting 'Start' from the RUN menu. 12 Consult your manual or on-line help for more information on QuickBasic. END OF DOCUMENTATION: