.

     

MPI Application Template
template.c
 
event1.c -- Perform a repeated two-axis motion while polling an event manager.

/* 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.
 */

/*
 
:Perform a repeated multi-axis motion while polling an for a motion done event.

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 "apputil.h"

#define MOTION_NUMBER   (0)
#define AXIS_NUMBER     (0)
#define AXIS_COUNT      (2)

#define END_POSITION    (20000.0)
#define SPEED           (10000.0)
#define ACCELERATION    (1000000.0)
#define DECELERATION    (1000000.0)


/* Parse command line */
long parseCommandLine(int                 argc,
                      char               *argv[],
                      MPIControlType     *controlType,
                      MPIControlAddress  *controlAddress,
                      long               *axisNumber,
                      long               *axisCount,
                      long               *motionNumber)
{
    long    argIndex;

    /* Application-specific command line arguments */
    Arg argList[] =
    {
        {   "-motion",    ArgTypeLONG,    motionNumber, },
        {   "-axis",      ArgTypeLONG,    axisNumber,   },
        {   "-axisCount", ArgTypeLONG,    axisCount,    },
        {   NULL,         ArgTypeINVALID, NULL,         }
    };

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

    /* Parse command line for application-specific arguments */
    while (argIndex < argc)
    {
        long    argIndexNew;

        argIndexNew = argSet(argList, argIndex, argc, argv);

        if (argIndexNew <= argIndex)
        {
            argIndex = argIndexNew;
            break;
        }
        else
        {
            argIndex = argIndexNew;
        }
    }

    /* Check for unknown/invalid command line arguments */
    if ((argIndex     < argc) ||
        (*axisNumber  > (MEIXmpMAX_Axes - AXIS_COUNT)))
    {
        meiPlatformConsole("usage: %s %s\n"
                           "\t\t[-axis   # (0 .. %d)]\n"
                           "\t\t[-motion # (0 .. %d)]\n",
                            argv[0],
                            ArgUSAGE,
                            MEIXmpMAX_Axes - AXIS_COUNT,
                            MEIXmpMAX_MSs - 1);
        exit(MPIMessageARG_INVALID);
    }

    return 0;
}


/* Create and initialize MPI objects */
void programInit(MPIControl         *control,
                 MPIControlType      controlType,
                 MPIControlAddress  *controlAddress,
                 MPIMotion          *motion,
                 long                motionNumber,
                 MPIAxis            *axis,
                 long                axisNumber,
                 long                numberOfAxes,
                 MPIEventMgr        *eventMgr,
                 MPINotify          *notify)
{
    MPIEventMask    eventMask;
    long            index;
    long            returnValue;


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

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

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

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

    /* Attach axes */
    for (index = 0; index < numberOfAxes; ++index)
    {
        returnValue =
            mpiMotionAxisAppend(*motion, axis[index]);
        msgCHECK(returnValue);
    }

    /* Map axis to motion supervisor on XMP */
    returnValue =
        mpiMotionAction(*motion, MEIActionMAP);
    msgCHECK(returnValue);

    /* Create event manager object */
    *eventMgr =
        mpiEventMgrCreate(*control);
    msgCHECK(mpiEventMgrValidate(*eventMgr));

    returnValue =
        mpiEventMgrFlush(*eventMgr);
    msgCHECK(returnValue);

    /* Request notification of all events from motion */
    mpiEventMaskCLEAR(eventMask);
    mpiEventMaskALL(eventMask);
    meiEventMaskALL(eventMask);

    returnValue =
        mpiMotionEventNotifySet(*motion,
                                 eventMask,
                                 NULL);
    msgCHECK(returnValue);

    /* Create event notification object for NULL (all objects) */
    *notify =
        mpiNotifyCreate(eventMask, NULL);
    msgCHECK(mpiNotifyValidate(*notify));

    /* Append notify to event manager */
    returnValue =
        mpiEventMgrNotifyAppend(*eventMgr, *notify);
    msgCHECK(returnValue);
}


/* Perform certain cleanup actions and delete MPI objects */
void programCleanup(MPIControl  *control,
                    MPIMotion   *motion,
                    MPIAxis     *axis,
                    long         axisNumber,
                    long         numberOfAxes,
                    MPIEventMgr *eventMgr,
                    MPINotify   *notify)
{
    long            index;
    long            returnValue;


    /* Delete event manager object */
    returnValue =
        mpiEventMgrDelete(*eventMgr);
    msgCHECK(returnValue);
    *eventMgr = MPIHandleVOID;

    /* Delete event notification object */
    returnValue =
        mpiNotifyDelete(*notify);
    msgCHECK(returnValue);
    *notify = MPIHandleVOID;

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

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

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


/* Wait for motion done event */
void waitForMotionDoneEvent(MPIEventMgr eventMgr,
                            MPINotify   notify)
{
    MPIEventStatus  eventStatus;
    long            returnValue;

    /* Collect motion events */
    while (TRUE)
    {
        /* Obtain firmware event(s) (if any) */
        returnValue =
            mpiEventMgrService(eventMgr,
                               MPIHandleVOID);
        msgCHECK(returnValue);

        /* Poll for motion event */
        returnValue =
            mpiNotifyEventWait(notify,
                               &eventStatus,
                               MPIWaitMSEC*20);

        if (returnValue == MPIMessageOK)
        {
            if (eventStatus.type == MPIEventTypeMOTION_DONE)
            {
                printf("Motion Done event received\n");
                break;
            }
        }
        else
        {
            meiAssert(returnValue == MPIMessageTIMEOUT);
        }
    }
}


/* Perform a multi-axis trapezoidal move */
void multiAxisTrapMove(MPIMotion      motion,
                       MPITrajectory *trajectory,
                       double        *position)
{
    MPIMotionParams params;         /* Motion parameters */
    MPIStatus       status;         /* Status handle */
    long            returnValue;


    params.trapezoidal.trajectory = trajectory;
    params.trapezoidal.position   = position;

    /* Read motion status */
    returnValue =
        mpiMotionStatus(motion,
                        &status,
                        NULL);
    msgCHECK(returnValue);

    switch (status.state)
    {
        case MPIStateIDLE:
        case MPIStateSTOPPED:
        {
            /* Start motion */
            returnValue =
                mpiMotionStart(motion,
                               MPIMotionTypeTRAPEZOIDAL,
                               &params);
            msgCHECK(returnValue);

            break;
        }
        case MPIStateSTOPPING:
        case MPIStateMOVING:
        {
            /* Modify the executing motion profile */
            returnValue =
                mpiMotionModify(motion,
                                MPIMotionTypeTRAPEZOIDAL,
                                &params);
            msgCHECK(returnValue);
            break;
        }
        case MPIStateERROR:
        {
            /* Error State */
            msgCHECK(MPIMotionMessageERROR);
            break;
        }
        default:
        {
            /* Unknown State */
            fprintf(stderr, "Unknown state from mpiMotionStatus.\n");
            msgCHECK(MPIMessageFATAL_ERROR);
            break;
        }
    }
}


/* setup move parameters, start move, wait for motion done event, repeat */
void commandMoves(MPIMotion   motion,
                  double      endPosition,
                  double      speed,
                  double      acceleration,
                  double      deceleration,
                  long        axisCount,
                  MPIEventMgr eventMgr,
                  MPINotify   notify)
{
    MPITrajectory   trajectory;                 /* trajectory information */
    double          command[MEIXmpMAX_Axes];    /* command position */
    double          position[MEIXmpMAX_Axes];   /* target position  */
    long            index;                      /* loop index */
    long            returnValue;                /* MPI return value */


    /* Set up trajectory information */
    trajectory.velocity     = speed;
    trajectory.acceleration = acceleration;
    trajectory.deceleration = deceleration;

    printf("Press any key to exit...\n\n");

    /* Loop until the user presses a key */
    while (meiPlatformKey(MPIWaitPOLL) < 0)
    {
        /* Get command position */
        returnValue =
            mpiMotionPositionGet(motion,
                                 NULL,
                                 command);
        msgCHECK(returnValue);

        if (command[0] != 0.0)
        {
            for (index=0; index<axisCount; ++index)
            {
                position[index] = 0.0;
            }
        }
        else
        {
            for (index=0; index<axisCount; ++index)
            {
                position[index] = endPosition;
            }
        }

        /* Perform a trapezoidal move */
        multiAxisTrapMove(motion,
                          &trajectory,
                          position);

        /* Wait for a motion done event */
        waitForMotionDoneEvent(eventMgr, notify);
    }
    printf("\n");
}


int main(int     argc,
         char   *argv[])
{
    MPIControl          control;
    MPIControlType      controlType;
    MPIControlAddress   controlAddress;
    MPIMotion           motion;
    MPIAxis             axis[AXIS_COUNT];
    MPIEventMgr         eventMgr;
    MPINotify           notify;
    double              endPosition  = END_POSITION;
    double              speed        = SPEED;
    double              acceleration = ACCELERATION;
    double              deceleration = DECELERATION;
    long                motionNumber = MOTION_NUMBER;
    long                axisNumber   = AXIS_NUMBER;
    long                axisCount    = AXIS_COUNT;


    /* Parse command line */
    parseCommandLine(argc,
                     argv,
                     &controlType,
                     &controlAddress,
                     &axisNumber,
                     &axisCount,
                     &motionNumber);

    /* Create and initialize MPI objects */
    programInit(&control,
                controlType,
                &controlAddress,
                &motion,
                motionNumber,
                axis,
                axisNumber,
                axisCount,
                &eventMgr,
                &notify);

    /* setup move parameters, start move, wait for motion done event, repeat */
    commandMoves(motion,
                 endPosition,
                 speed,
                 acceleration,
                 deceleration,
                 axisCount,
                 eventMgr,
                 notify);

    /* Perform certain cleanup actions and delete MPI objects */
    programCleanup(&control,
                   &motion,
                   axis,
                   axisNumber,
                   axisCount,
                   &eventMgr,
                   &notify);

    return MPIMessageOK;
}


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