Because of the variety of uses for the products described in this
publication, those responsible for the application and use of this
control equipment must satisfy themselves that all necessary steps
have been taken to assure that each application and use meets all
performance and safety requirements, including any applicable laws,
regulations, codes and standards.
The illustrations, charts, sample programs and layout examples shown
in this guide are intended solely for purposes of example. Since there
are many variables and requirements associated with any particular
installation, Allen-Bradley does not assume responsibility or liability
(to include intellectual property liability) for actual use based upon
the examples shown in this publication.
Allen-Bradley publication SGI-1.1, Safety Guidelines for the
Application, Installation and Maintenance of Solid-State Control
(available from your local Allen-Bradley office), describes some
important differences between solid-state equipment and
electromechanical devices that should be taken into consideration
when applying products such as those described in this publication.
Reproduction of the contents of this copyrighted publication, in whole
or part, without written permission of Rockwell Automation, is
prohibited.
Throughout this manual we use notes to make you aware of safety
considerations:
ATTENTION
Identifies information about practices or
circumstances that can lead to personal injury or
death, property damage or economic loss.
!
Attention statements help you to:
• identify a hazard
• avoid a hazard
• recognize the consequences
IMPORTANT
Allen-Bradley is a registered trademark of Rockwell Automation, Inc.
Ultra3000, Ultra5000 and Ultraware are trademarks of Rockwell Automation, Inc.
Identifies information that is critical for successful
application and understanding of the product.
Updates to the Motion Library for Ultraware Release 1.5 . . . A-1
Updates to the Motion Library for Ultraware Release 1.4 . . . A-1
Updates to the Motion Library for Ultraware Release 1.3 . . . A-1
Updates to the Motion Library for Ultraware Release 1.2 . . . A-2
Index
Preface
Introduction
Who Should Use this
Manual
Read this preface to familiarize yourself with this manual. This preface
covers the following topics:
• Introduction
• Who should use this manual
• Contents of this Manual
• Related documentation
• Conventions Used in this Manual
• Using Online Help
®
• Allen-Bradley
Use this manual if you are new to programming in the Ultraware
programming environment, but have a basic understanding of
programming and the C programming language.
If you do not have a basic understanding of C programming, you
should first read one of the many books available or obtain instruction
on C programming.
support
Where to Find Help
You can find help for Ultraware in both this manual and the User
Manual, and in the Online Help. You can also select the Tip of the Day... command from Ultraware’s Help menu, which opens a dialog
displaying helpful hints on using Ultraware.
1Publication 2098-PM001E-EN-P — July 2002
P-2Preface
Contents of this Manual
Related Documentation
This manual contains the following sections:
Chapters:Title:Contents:
PrefaceAn overview of this manual.
Chapter 1Programming Motion
Control in C
Chapter 2Referencing the
Motion Library
Appendix AUpdates to the Motion
Library
An overview of C Programming principles and
practices in the Ultraware environment.
A description of the C functions available in the
Ultraware Motion Library
A list of motion library enhancements at each
release level.
The following documents contain additional information concerning
related Allen-Bradley products or general information that may be
helpful. To obtain a copy, contact your local Rockwell Automation
office or distributor, or access the documents on-line at
Ultra5000™ drive installation
and setup procedures
.
Ultra5000 Hardware
Installation Manual
or
Publication Number
-IN001E-EN-P
2098
1
Conventions Used in this
Manual
Ultraware™
operating instructions
Ultraware installation
instructions
Ultra3000™ and Ultra5000
drive capabilities
1
To ensure correct operation, Allen-Bradley publications referenced should be at or above the revision level
setup and
listed. (e.g., 2098-UM001F-EN-P is revision F of the Ultraware User Manual.)
Ultraware User Manual
Release 1.5
Ultraware CD Installation
Instructions
Ultra Family Brochure2098-BR001x-EN-P
2098-UM001F-EN-P
2098-IN002x-EN-P
The following conventions are used throughout this manual:
• Bulleted lists provide information, not procedural steps.
• Numbered lists provide sequential steps.
• Words you type or select, and keys that you press, appear in bold.
• Programming code appears in a
Courier font.
• C Programming conventions are used as appropriate.
Publication 2098-PM001E-EN-P — July 2002
PrefaceP-3
Using Online Help
Allen-Bradley Support
The following types of online help are available:
To use:Do:Description:
Ultraware HelpSelect Contents and Index
from the Help menu.
Navigate the help file using
the Table of Contents, the
Index and the Search tabs.
Context Sensitive HelpEither:
• Click on a Help
button in the active
window, or
• Select an on-screen
object and press F1.
Descriptions of all on-screen
objects.
Object property
configuration settings.
How-to information.
For help about the selected
object.
Allen-Bradley offers support services worldwide, with over 75
sales/support offices, 512 authorized distributors and 260 authorized
systems integrators located throughout the United States alone, plus
Allen-Bradley representatives in every major country in the world.
Local Product Support
Contact your local Allen-Bradley representative for:
• Sales and order support
• Product technical training
• Warranty support
• Support service agreements
Technical Product Assistance
If you need to contact Allen-Bradley for technical assistance, please
review the information in this manual or in the Online Help file first.
Then call your local Allen-Bradley representative. For the quickest
possible response, we recommend that you have the catalog numbers
of your products available when you call.
Publication 2098-PM001E-EN-P — July 2002
P-4Preface
Publication 2098-PM001E-EN-P — July 2002
Programming Motion Control in C
Chapter
1
Introduction
Before You Begin
The chapter introduces you to the C language and programming with
the Motion Library functions in Ultraware 1.3. The purpose of this
chapter is to:
• Review some of the basic C language constructs.
• Help you gain experience in developing Ultraware programs that
control the motion of a motor.
• Have you create and run motion programs using the Ultraware
Motion Library.
This chapter assumes that you have read, and understand, the
contents of the Ultraware User Manual (2098-UM001F-EN-P). That
means:
• The Ultra5000 system is setup and tuning is complete.
• Controlled motion was initiated through direct mode commands,
such as Jog Forward.
IMPORTANT
If you do not have a basic understanding of C
programming, you should consider reading one of
the many books available or obtain instruction on C
programming. This manual is not intended to
provide you with the instruction necessary to
become a proficient C programmer.
Creating A Basic Motion
Program
1Publication 2098-PM001E-EN-P — July 2002
Until now all motion occurred in what is called the direct mode; you
told the Ultra5000 what to do by selecting direct commands from the
Ultraware Commands menu. It is far more useful to have all the
moves a machine axis will do, along with I/O statements, etc., in a
program. In this chapter you will build several C programs that
automate the control of a motor and drive system.
1-2Programming Motion Control in C
The first short program you will write:
• Moves the axis 1000 counts in the positive direction
• Delays program execution for 1 second
• Returns the axis 1000 counts in the negative direction
While the code is compact, what is important at this point is the
process. You must create the program text in a Project file, build it
successfully, load it, and run it.
Creating and Running a Program
The Ultraware User Manual (2098-UM001F-EN-P) instructs you about
how to create and run a program. The steps involved in creating and
running a program are:
1. Create a Project.
2. Create and edit a Source (.c) file.
3. Use the Program menu’s Build command to compile and create
the executable program (.exe) file.
4. Move the executable program file to the Program branch of an
On-Line drive.
5. Use the Program menu’s Run, Stop and Kill commands to control
the program’s execution.
You may remember an additional step, creating and editing a Header
file. For now that is an unnecessary step.
Example 1 - A Basic Motion Program
The first Ultraware program example starts you programming in C
using Ultraware Motion Library functions. To begin:
1. Insert a project into your program database.
Publication 2098-PM001E-EN-P — July 2002
2. Insert a source file into your newly created project.
Programming Motion Control in C1-3
3. Open the source file and enter the following text with the text
editor:
MoveIncremental(1000);
while (MoveInProgress()) {/* loop */}
Sleep(1000);
MoveIncremental(-1000);
while (MoveInProgress()) {/* loop */}
AxisDisable();
return 0;
}
4. After the text is entered, right-click on the Project in which you
inserted the source file and select Build from the Project menu. If
all of the text was entered correctly, the compilation will proceed
and you will see the following statements sequentially display in
the Ultraware Output window:
Notice the new file created under the project. The file is named the
same as the project but has an .exe extension. This is the program’s
executable file that now may be loaded and run on the Ultra5000.
Note: If the build fails, refer to the Executable Program File section in
the Ultraware User Manual (2098-UM001F-EN-P) for instructions
on how to troubleshoot errors encountered while creating a
program.
5. Drag the .exe file from the Project to the Program branch of the
On-line Ultra5000.
6. After the on-line controller’s Program branch expands, right-click
on your program, and select Run from the menu to execute the
program.
Publication 2098-PM001E-EN-P — July 2002
1-4Programming Motion Control in C
Example 1 Explained
Any C program, whatever its size, consists of functions and variables.
A function contains statements that specify the operations to be done,
and variables that store the values used in those operations. Braces
(opening {, and closing }) enclose the statements in a function.
Example 1 uses a function named main. Normally you are free to give
functions whatever name you like, but main is special – C programs
begin executing at main. This means that every program must have
int main(void)
as its first function.
The main function calls other functions to help perform a task, some
that you write, and others from the motion library. The first line of the
program,
#include <motion.h>
tells the compiler to include information from the motion library. This
line must appear at the beginning of any program wishing to use
motion library functions. The chapter Referencing the Motion Library
describes the functions provided in the Ultraware Motion Library.
One method of communicating data between functions is for the
calling function to provide a list of values, called arguments, to a
function it is calling. Parentheses after a function name surround an
argument list. In our example, main is defined to be a function that
expects no arguments, which is indicated by the empty or void list
[written as (void) or often as a set of empty parentheses ( )].
In the first example, the function main contains several function calls
and a return statement, which are explained later. Each function is
called by naming it, followed by its arguments in parentheses, and is
terminated with a semicolon. The line
MoveSetDec(1000);
calls the function MoveSetDec with the argument 1000. MoveSetDec is
an Ultraware Motion Library function that sets the default move
deceleration value to 1000 encoder counts/sec².
Publication 2098-PM001E-EN-P — July 2002
Programming Motion Control in C1-5
Variables, Loops, and Constants
The next example is a motion program that makes a series of 10 index
moves of 1000 counts each. This program introduces you to:
• Comments
• Declarations
• Variables
• Arithmetic expressions
• Loops
Example 2.1 - A Motion Program Using Variables
Enter the following program into a source file using the same process
as Example 1.
AxisDefinePos(0);/* define current position as
index = 1000;0 */
target = index;/* initialize target to first
index position (1000) */
endpoint = index * 10;/* initialize endpoint to
10000 (1000 * 10) */
while (target <= endpoint) {
MoveAbsolute(target);
while (MoveInProgress()) {/* loop */}
target = target+index;
}
AxisDisable();
return 0;
}
Publication 2098-PM001E-EN-P — July 2002
1-6Programming Motion Control in C
Example 2.1 Explained
The program still consists of a single function main. However, it
introduces several new ideas, including comments, declarations,
variables, arithmetic expressions, and loops. The two lines
/* Increment through the 10 index positions
1000, 2000, etc. until target > endpoint */
are a comment, which in this case briefly explains what the program
does. Any characters between the /* and */ are ignored by the
compiler. Comments of this type may be multi-line and should be
used freely to make a program easier to read and understand.
In C, all variables must be declared before they are used, usually at
the beginning of the function in front of any executable statements. A
declaration announces the properties of variables; it consists of a type
name and a list of variables such as
long target, index, endpoint;
The type long means that the variables listed are long integers, by
contrast the type float means floating point, i.e., numbers that may
have a fractional part. On the Ultra5000, a long integer is a 32-bit
quantity that contains a value in the range ±2,147,483,647; a floating
point is a 32-bit quantity that contains a value in the range 10
+38
10
.
-38
to
Computation begins in the motion program with the assignment
statements
index = 1000;
target = index;
endpoint = index * 10;
which set the variables to their initial values. Semicolons terminate
individual statements.
In the example, each move is calculated and executed the same way,
and the loop repeats once per index; this is the purpose of the while
loop
while (target <= endpoint) {
...
}
Publication 2098-PM001E-EN-P — July 2002
The while loop tests whether a statement is true or false, and then
performs one of two possible paths. The example operates as follows:
The condition in parentheses is tested. If it is true (target is less than
or equal to endpoint), the body of the loop (the statements enclosed
by braces) is executed. Then the condition is re-tested, and if true, the
body is executed again. When the test becomes false (target exceeds
Programming Motion Control in C1-7
endpoint) the loop ends, and execution continues at the statement
that follows the loop.
Statements contained inside the braces of a while loop, and in many
other cases yet to come, should always be indented so you can see at
a glance which statements are inside the loop. The indentation
emphasizes the logical structure of the program. Although the
Ultraware C compiler does not care about how a program looks,
proper indentation and spacing are critical in making a program easy
to read.
Publication 2098-PM001E-EN-P — July 2002
1-8Programming Motion Control in C
Example 2.2 - Varying the Program using Variables
There are many different ways to write a program for a particular task.
This program performs the same tasks as Example 2.1 - A Motion
Program Using Variables, but it uses slightly different software code.
#include <motion.h>
/* increment through the 10 index positions 1000, 2000,...,
MoveAbsolute(target);
while (MoveInProgress()) {/* loop */}
}
AxisDisable();
return 0;
}
Example 2.2 Explained
This program produces the same motion, but looks different than
Example 2.1. One major change is the elimination of most of the
variables; only target remains. The index and endpoint values appear
only as constants in the for statement.
The for statement is a loop. Compare the for loop to the previous
usage of the while command. Within the parentheses, there are three
parts, separated by semicolons. The first part, the initialization
target = 1000
Publication 2098-PM001E-EN-P — July 2002
is done once, before the loop proper is entered. The second part is
the test or condition that controls the loop:
target <= 10000
This condition is evaluated, and if it is true, the body of the loop is
executed. Then the increment step
target = target + 1000
Programming Motion Control in C1-9
is executed, and the condition is re-evaluated. The loop terminates
when the condition becomes false. As with the while, the body of the
loop is enclosed by braces. The initialization, condition, and
increment can be any expressions.
The choice between while and for is arbitrary, based on which seems
clearer. The for is usually appropriate for loops in which the
initialization and increment are single statements and logically related.
It is more compact than while and it keeps the loop control statements
together in one place.
It is generally bad practice to use magic numbers like 1000 and 10000
in a program; they convey little information to someone who might
have to read the program later. One way to deal with magic numbers
is to give them meaningful names. A #define line defines a symbolic
name or symbolic constant to be a particular string of characters:
#definenamereplacement_text
Thereafter, any occurrence of name (that is not part of another name)
will be replaced by the corresponding replacement text. The name
has the same form as a variable name: a sequence of letters and digits
that begins with a letter.
You may have noticed that there is a return statement at the end of
main. Since main is a function like any other, it may return a value to
its caller, which in effect is the Ultra5000 operating system. This return
value is not currently used.
Publication 2098-PM001E-EN-P — July 2002
1-10Programming Motion Control in C
Example 2.3 - Another Variance of the Program using Variables
A third way to achieve the same result is to write the program as
follows:
INDEX) {
MoveAbsolute(target);
while (MoveInProgress()) {/* loop */}
}
AxisDisable();
return 0;
}
Example 2.3 Explained
The quantities ACCEL, DECEL, TARGETVEL, INDEX, and ENDPOINT
are symbolic constants, not variables, so they do not appear in
declarations. C programming conventions write constants in upper
case and variables in lower case, but this is not a requirement. Notice
that there is no semicolon at the end of a #define line.
Publication 2098-PM001E-EN-P — July 2002
Programming Motion Control in C1-11
User Defined Functions
A function provides a convenient way to encapsulate a sequence of
motion commands and computations.
Example 3 - Nested Functions in a Motion Program
This program shows the function insert and the main program to
exercise it. In this example you see the whole structure at once.
long insert(long distance, long velocity, long accdec);
/* This program continue cycling through the while loop
as long as input 16 is on and it has not completed 10 insertion
cycles. Each loop through, the state of input 1 is evaluated
and, if it is on, the insert function is executed.*/
int main (void)
{
InitMotionLibrary();
AxisEnable();
/* Continue operating while Input 16 is active and partcount
has not reached 10 */
while ((InputGetState(CONTINUE) == ON) && (partcount < 10)){
/* do insertion if Input 1 is active */
if (InputGetState(INSERT) == ON) {
The call passes three arguments to insert, which each time returns a
long integer partcount. The first line of insert itself,
long insert(long distance, long velocity, long accdec)
declares the parameter types and names, and the type of the result
that the function returns. The names used by insert for its parameters
are local to insert, and are not visible to any other functions (i.e.,
Other routines can use the same names without conflict.).
The value of partcount is transferred to main by the return statement.
Any expression may follow return:
return expression;
Publication 2098-PM001E-EN-P — July 2002
A function need not return a value; a return with no expression causes
control, but no value, to be returned to the caller. Also, the calling
function can ignore a value returned by a function.
Programming Motion Control in C1-13
The declaration
long insert(long distance, long velocity, long accdec);
just before main says that insert is a function that expects three long
arguments and returns a long value. This declaration, which is called a
function prototype, has to agree with the definition and uses of insert.
It is an error if the definition of the function or any uses of it do not
agree with its prototype.
Parameter names need not agree. In fact, parameter names are
optional in a function prototype, so the prototype could be written
long insert(long, long, long);
Function prototypes allow the compiler to detect errors in the number
of arguments or their types.
Program Stop
Ultraware uses the Program Stop command to request the programs
executing on the Ultra5000 to stop. This command provides a
controlled stop to program execution, unlike the Kill command. When
the Program Stop command is executed, a SystemSignal_Stop flag is
set. The state of this flag is checked through the use of the
StopRequested function.
ATTENTION
!
Examples 4 and 5 are programs that show how the StopRequested
function should be used. The programs also provide examples of the
Jog and Gear functions. Notice the use of the ! operator to modify
StopRequested() in the while loop of both programs. This symbol (!)
is termed the not operator. The loop continues execution while the
StopRequested event has not occurred.
Note: Program comments explain this example and all subsequent
examples in this chapter.
The StopRequested function must be called in your
main program execution loop if the program is to
react to a Stop command. If this function is not
called, the Stop command will be ignored. It is your
responsibility to ensure that all programs check for
and react to this event.
#defineSTOPPED0/* Jog modes */
#defineFORWARD1
#defineREVERSE2
/* This program uses inputs 7 and 8 to Jog the axis in the
reverse and forward direction, respectively. It will
continue to run until a Stop or Kill command is issued from
Ultraware. */
/* This program uses analog input channel 1 to determine
the gearing ratio. The purpose of comparing the old ratio
to new is to avoid resetting the gear ratio when it has
not changed. */
LatchOnIndex(1,1,TRUE);
while (!LatchTriggered(1)) { }
JogStop();
while (JogInProgress()) { }
MoveSetVel(HOME_VEL);
MoveSetAcc(HOME_ACC);
MoveSetDec(HOME_DEC);
MoveAbsolute(LatchGetOutput(1)+HOME_OFFSET);
while (MoveInProgress()) { }
AxisDefinePos(0);
return 0;
}
Global variables are handled through arrays created in Ultraware and
stored in the Ultra5000 non-volatile memory. Refer to the Ultraware User Manual (2098-UM001F-EN-P) for information on creating
non-volatile arrays.
There are two types of arrays that can be created in Ultraware: those
containing long integer values and those containing floating-point
values. You may create as many arrays as you have space for in the
Publication 2098-PM001E-EN-P — July 2002
Programming Motion Control in C1-17
controller’s non-volatile memory but only one of each type may be
accessed at a time. One way to access non-volatile arrays is shown.
Example 7 - A Non-Volatile Array Program
#include <motion.h>
/* This program assumes that two 10-element arrays have been
created through Ultraware. One floating-point array named
"FArray" and one long integer array named "IArray". This
program continuously cycles through the array elements
incrementing the value to be written. */
the expression i/10 has the type in brackets right before it.
Publication 2098-PM001E-EN-P — July 2002
1-18Programming Motion Control in C
Types, Operators, and
Expressions
The C language allows you a great deal of flexibility in deciding what
type of data the Ultra5000 will use. Some of the data is preset or static,
such as (#define ACCEL 1000), while other data changes or is assigned
different values as your program runs (long partcount; is an example).
The preset and unchanging data is called a constant, while the
changing data is called a variable. Expressions combine variables and
constants to produce new values.
Variable Names and Data Types/Sizes
You are already familiar with declaring one variable. The code
long target
declares the variable target as a long integer. This one line of code
covers the rules for C variables:
• Each variable must have a unique name.
• Variables must begin with a letter.
• Before you can use a variable, you must declare or define it by its
type. The following table lists the eight keywords for defining a
variable.
Keyword:Description:Example:
intInteger: A positive or negative value of up to
32-bits in length.
longLong word: An integer of the maximum length,
32- bits with a range of ±2,147,483,647.
shortShort word: An integer smaller than the largest
integer; 32- bits in length.
unsignedA non-negative number (i.e., a number greater
than or equal to 0).
charTypographic symbols, such as A, & and + or small
integers, up to 32-bits in length.
floatA fractional value or a value with a decimal
point. 32-bits in length, but typically with a range
–38
from 10
doubleA longer fractional or decimal value. It allows for
additional significant places and/or larger
exponents than a basic float. 32-bits in length,
but typically with a 10-digit base and an
exponent from –307 to 308.
Note: A long double typically provides an 18-digit
base and an exponent from –4931 to 4932.
signedA number with a positive or a negative value.
to 10
38
.
int main ()
long int ()
short int ()
unsigned int ()
or
unsigned int ()
salary ()
double salary ()
Publication 2098-PM001E-EN-P — July 2002
Loading...
+ 84 hidden pages
You need points to download manuals.
1 point = 1 manual.
You can buy points or you can get point for every manual you upload.