Example 2.2 - varying the program using variables, Example 2.2 explained – Rockwell Automation 2098-IPD-xxx Ultra5000 C Programming using the Motion Library User Manual

Page 20

Advertising
background image

Publication 2098-PM001E-EN-P — July 2002

1-8

Programming 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,...,

until target > endpoint

*/

int main (void)

{

long target;

InitMotionLibrary();

AxisEnable();

MoveSetAcc(2000);

MoveSetVel(1000);

MoveSetDec(2000);

AxisDefinePos(0);

for (target = 1000; target <= 10000; target = target + 1000) {

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

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

Advertising