.

     

MPI Application Template
template.c
 

seqKill.c -- Program Sequence to monitor an I/O bit and abort all axes when active.
/* seqkill.c */

/* Copyright(c) 1991-2006 by Motion Engineering, Inc.  All rights reserved.
 *
 * This software  contains proprietary and  confidential information  of
 * Motion Engineering Inc., and its suppliers.  Except as may be set forth
 * in the license agreement under which  this software is supplied, use,
 * disclosure, or  reproduction is prohibited without the prior express
 * written consent of Motion Engineering, Inc.
 */

/*

:Program Sequence to monitor an I/O bit and abort all axes when active.

This sample program creates a program sequencer that will monitor an I/O bit.
 When this I/O bit is set, it will abort all the axes from 0 to axisCount.  It
 will monitor motor 0's transceiver A to determine when to abort the axes.

The XMP program sequencer controls the execution of a single command or a
 series of commands on the XMP controller.  The program sequencer provides the
 ability to execute programs directly on the XMP controller without host
 intervention.  Examples of individual commands that can be executed by the
 program sequencer are motion, looping, conditional branching, computation,
 reading and writing of memory, time delays, waiting for conditions, setting
 inputs/outputs, and generation of events.  This rich command set provides
 capability similar to, and beyond, that provided by Programmable Logic
 Controller (PLC) programs.

Warning!  This is a sample program to assist in the integration of an
 MEI motion controller with your application.  It may not contain all
 of the logic and safety features that your application requires.

The msgCHECK(...) macros used in the following sample code are intended
 to convey our strong belief that ALL error return codes should be checked.
 Actual application code should use specific error handling techniques (other
 than msgCHECKs) best suited to your internal error recovery methods.

*/

#include <stdlib.h>
#include <stdio.h>

#include "stdmpi.h"
#include "stdmei.h"
#include "mei_rmb.h"
#include "apputil.h"

#define MOTION_NUMBER   (0)
#define MOTOR_NUMBER    (0)
#define AXIS_COUNT      (8)

#define IO_TRIGGER      (RMBGeneralIoXCVR_A)

/*
   Active High: MPICommandOperatorBIT_SET
   Active Low : MPICommandOperatorBIT_CLEAR
*/
#define IO_LEVEL        (MPICommandOperatorBIT_SET)


/* Perform basic command line parsing. (-control -server -port -trace) */
void basicParsing(int                    argc,
                  char                  *argv[],
                  MPIControlType        *controlType,
                  MPIControlAddress     *controlAddress)
{
    long argIndex;

    /* Parse command line for Control type and address */
    argIndex = argControl(argc, argv, controlType, controlAddress);

    /* Check for unknown/invalid command line arguments */
    if (argIndex < argc) {
        fprintf(stderr,"usage: %s %s\n", argv[0], ArgUSAGE);
        exit(MPIMessageARG_INVALID);
    }
}


/* Create and initialize MPI objects */
void programInit(MPIControl         *control,
                 MPIControlType      controlType,
                 MPIControlAddress  *controlAddress,
                 MPIMotion          *motion,
                 long                motionNumber,
                 MPIAxis            *axis,
                 long                axisCount,
                 MPIMotor           *motor,
                 long                motorNumber)
{
    long            index;
    long            returnValue;


    /* Create motion controller object */
    *control =
        mpiControlCreate(controlType,
                         controlAddress);
    msgCHECK(mpiControlValidate(*control));

    /* Initialize motion controller */
    returnValue =
        mpiControlInit(*control);
    msgCHECK(returnValue);

    /* Create axis objects */
    for (index = 0; index < axisCount; index++) {
        axis[index] =
            mpiAxisCreate(*control,
                          index);
        msgCHECK(mpiAxisValidate(axis[index]));
    }

    /* Create motion supervisor object with axis */
    *motion =
        mpiMotionCreate(*control,
                        motionNumber,
                        *axis);
    msgCHECK(mpiMotionValidate(*motion));

    /* Create motor object */
    *motor =
        mpiMotorCreate(*control,
                        motorNumber);
    msgCHECK(mpiMotorValidate(*motor));

    /* Write Motion Supervisor AxisMap[], and clear fault conditions */
    returnValue =
        mpiMotionAction(*motion,
                        MPIActionRESET);
    msgCHECK(returnValue);
}


/* Perform certain cleanup actions and delete MPI objects */
void programCleanup(MPIControl      *control,
                    MPIMotion       *motion,
                    MPIAxis         *axis,
                    long             axisCount,
                    MPIMotor        *motor)
{
    long    index;
    long    returnValue;


    /* Delete motor object */
    returnValue =
        mpiMotorDelete(*motor);
    msgCHECK(returnValue);

    *motor = MPIHandleVOID;

    /* Delete motion supervisor object */
    returnValue =
        mpiMotionDelete(*motion);
    msgCHECK(returnValue);

    *motion = MPIHandleVOID;

    /* Delete axis objects */
    for (index = 0; index < axisCount; index++) {
        returnValue = mpiAxisDelete(axis[index]);
        msgCHECK(returnValue);

        axis[index] = MPIHandleVOID;
    }

    /* Delete motion controller object */
    returnValue =
        mpiControlDelete(*control);
    msgCHECK(returnValue);

    *control = MPIHandleVOID;
}


/*
   sequenceCommandAdd() creates a command (MPICommand object) and appends it to
   sequence's list of commands.  This function does return MPI error codes so
   that sequence setup code may be more easily debugged.

   Warning:  sequenceCommandAdd() does not keep track of created command objects
   so it is important that before a sequence object is deleted, that each
   command object on sequence's list is itself deleted.  Otherwise, there will
   be memory leaks.  One can use sequenceProgramDelete() to accomplish this.
*/
long sequenceCommandAdd(MPISequence          sequence,
                        MPICommandType       commandType,
                        MPICommandParams    *commandParams,
                        const char          *label)
{
    MPICommand  command;
    long        returnValue;


    command =
        mpiCommandCreate(commandType,
                         commandParams,
                         label);
    returnValue =
        mpiCommandValidate(command);

    if (returnValue == MPIMessageOK) {
        returnValue =
            mpiSequenceCommandAppend(sequence,
                                     command);
    }

    return returnValue;
}


/*
   sequenceKillProgramCreate() creates a program that waits for one of motor's
   io lines (represented by trigger) to turn on then commands an E_STOP_ABORT.
*/
MPISequence sequenceKillProgramCreate(MPIMotion             motion,
                                      MPIMotor              motor,
                                      RMBGeneralIo       trigger,
                                      MPICommandOperator    level)
{
    MPISequence         sequence;
    MPICommandParams    commandParams;
    long                returnValue;


    /* Create sequence object */
    sequence =
        mpiSequenceCreate(mpiMotionControl(motion),
                          -1,   /* Use the next available sequence */
                          3);   /* # of commands in sequence */
    msgCHECK(mpiSequenceValidate(sequence));

    /* Wait for motor 0's bit 0 (transceiver A) to be set */
    commandParams.waitIO.type           = MPIIoTypeMOTOR_GENERAL;
    commandParams.waitIO.source.motor   = motor;
    commandParams.waitIO.oper           = level;
    commandParams.waitIO.mask           = trigger;

    returnValue =
        sequenceCommandAdd(sequence,
                           MPICommandTypeWAIT_IO,
                           &commandParams,
                           NULL);
    msgCHECK(returnValue);

    /* command an E_STOP_ABORT */
    commandParams.motion.motionCommand  = MPICommandMotionE_STOP_ABORT;
    commandParams.motion.motion     = motion;

    returnValue =
        sequenceCommandAdd(sequence,
                           MPICommandTypeMOVE,
                           &commandParams,
                           NULL);
    msgCHECK(returnValue);

    /* Stop sequence */
    commandParams.branch.label      = NULL;
    commandParams.branch.expr.oper  = MPICommandOperatorALWAYS;

    returnValue =
        sequenceCommandAdd(sequence,
                           MPICommandTypeBRANCH,
                           &commandParams,
                           NULL);
    msgCHECK(returnValue);

    /* Return the new sequence handle */
    return sequence;
}


/*
   sequenceProgramDelete() removes and deletes all command objects on sequence's
   command list and then deletes sequence.  This function does return MPI error
   codes so that sequence setup code may be more easily debugged.
*/
long sequenceProgramDelete(MPISequence *sequence)
{
    MPICommand  command;
    long        returnValue = MPIMessageOK;


    /* Remove and delete command objects on sequence list */
    while(TRUE) {
        command = mpiSequenceCommandLast(*sequence);
        if ((returnValue != MPIMessageOK) ||
            (command==MPIHandleVOID)) {
            break;
        }
        returnValue =
            mpiSequenceCommandRemove(*sequence,
                                     command);

        if (returnValue == MPIMessageOK) {
            returnValue =
                mpiCommandDelete(command);
        }
    }

    /* Delete sequence object */
    returnValue =
        mpiSequenceDelete(*sequence);

    if (returnValue == MPIMessageOK) {
        /* Make it obvious that sequence is no longer a valid object */
        *sequence = MPIHandleVOID;
    }

    return returnValue;
}


/*
   Starts a sequence, waits for the user to press a key and then
   stops the sequence.
*/
void sequenceRun(MPISequence sequence)
{
    long returnValue;


    /* Start sequence */
    returnValue =
        mpiSequenceStart(sequence,
                         MPIHandleVOID);
    msgCHECK(returnValue);

    fprintf(stderr,"Press any key to stop sequence.\n");
    mpiPlatformKey(MPIWaitFOREVER);
    fprintf(stderr,"Exiting ...\n");

    /* Stop sequence */
    returnValue = mpiSequenceStop(sequence);
    if ((returnValue != MPIMessageOK) &&
        (returnValue != MPISequenceMessageSTOPPED)) {
        msgCHECK(returnValue);
    }
}


int main(int     argc,
         char   *argv[])
{
    MPIControl          control;
    MPIControlType      controlType;
    MPIControlAddress   controlAddress;
    MPIMotion           motion;
    MPIAxis             axis[MPIXmpMAX_COORD_AXES];     /* axis handles */
    MPIMotor            motor;
    MPISequence         sequence;

    long    returnValue;


    /* Perform basic command line parsing. (-control -server -port -trace) */
    basicParsing(argc,
                 argv,
                 &controlType,
                 &controlAddress);

    /* Create and initialize MPI objects */
    programInit(&control,
                controlType,
                &controlAddress,
                &motion,
                MOTION_NUMBER,
                axis,
                AXIS_COUNT,
                &motor,
                MOTOR_NUMBER);

    /* Create sequence program */
    sequence =
        sequenceKillProgramCreate(motion,
                                  motor,
                                  IO_TRIGGER,
                                  IO_LEVEL);

    /* Run sequence */
    sequenceRun(sequence);

    /* Delete sequence program */
    returnValue =
        sequenceProgramDelete(&sequence);
    msgCHECK(returnValue);

    /* Perform certain cleanup actions and delete MPI objects */
    programCleanup(&control,
                   &motion,
                   axis,
                   AXIS_COUNT,
                   &motor);

    return ((int)returnValue);
}


      
       Legal Notice  |  Tech Email  |  Feedback
      
Copyright ©
2001-2009 Motion Engineering