User defined functions, Example 3 - nested functions in a motion program, User defined functions -11 – Rockwell Automation 2098-IPD-xxx Ultra5000 C Programming using the Motion Library User Manual
Page 23

Publication 2098-PM001E-EN-P — July 2002
Programming Motion Control in C
1-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.
#include
#define
INSERTACCDEC 20000
/* counts/second**2 */
#define
INSERTVEL
10000
/* counts/second */
#define
INSERTDIST
8000
/* counts */
#define
CONTINUE
16
/* Input that determines
program continuation
*/
#define
INSERT
1
/* Input that starts the
insertion move */
#define
INMOTION
1
/* Output that is set
while move is in
progress */
long
partcount = 0;
/* Initialize partcount
variable to 0 */
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) {
/* insert returns new partcount */
partcount = insert(INSERTDIST, INSERTVEL, INSERTACCDEC);
}
}
AxisDisable();
return 0;
}
/* Function that does the insertion move and pulses Output 1
while moving */
long insert(long distance, long velocity, long accdec)