.

     

MPI Application Template
template.c
 

event3.c -- Perform a repeated single-axis motion using command-line-specified axis (default 0).
/* event3.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.
 */

/*
 
:Perform a repeated single-axis motion with service thread events report method

Wait for events to be distributed by the event manager in a separate service 
 thread. Single axis motion with default axis number is 0 and motion 
 supervisor 0, but user is able to change this number through the command line.

Axis will move starting at the start position to the end position then back to
 start position and so forth, until a key is pressed.
 
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 START_POSITION  (0.0)
#define END_POSITION    (20000.0)
#define VELOCITY        (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               *motionNumber)
{
    long    argIndex;

    /* Application-specific command line arguments */
    Arg argList[] =
    {
        {   "-axis",    ArgTypeLONG,    &(*axisNumber),    },
        {   "-motion",  ArgTypeLONG,    &(*motionNumber),  },
        {   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)
    {
        fprintf(stderr,"usage: %s %s\n"
                       "\t      [-axis # (0 .. %d)]\n"
                       "\t      [-motion # (0 .. %d)]\n",
                        argv[0],
                        ArgUSAGE,
                        MEIXmpMAX_Axes,
                        MEIXmpMAX_MSs);
        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,
                 MPIEventMgr        *eventMgr,
                 MPINotify          *notify,
                 Service            *service)
{
    MPIEventMask    eventMask;
   MPIMotor    motor;
    long            returnValue;

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

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

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

    /* Create axis object */
    *axis =
        mpiAxisCreate(*control, axisNumber);
    msgCHECK(mpiAxisValidate(*axis));

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

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

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

   returnValue =
      mpiMotorAmpEnableSet(motor, TRUE);

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

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

    /* Calling mpiMotionEventNotifySet(...) so that events will be reported */
    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);

    /* Create service thread */
    *service =
        serviceCreate(*eventMgr,
                      -1,   /* default (max) priority */
                      -1);  /* -1 => enable interrupts */
    if (*service == NULL)
    {
        msgCHECK(MPIMessageHANDLE_INVALID);
    }
}


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

    /* Delete service object */
    returnValue =
        serviceDelete(*service);
    msgCHECK(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 axis object */
    returnValue =
        mpiAxisDelete(*axis);
    msgCHECK(returnValue);
    *axis = MPIHandleVOID;

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


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

    /* Collect motion events */
    while (TRUE)
    {
        /* Poll for motion event */
        returnValue =
            mpiNotifyEventWait(notify,
                               &eventStatus,
                               MPIWaitFOREVER);
        msgCHECK(returnValue);

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


/* Perform a single-axis trapezoidal move */
void singleAxisTrapMove(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      startPosition,
                  double      endPosition,
                  double      velocity,
                  double      acceleration,
                  double      deceleration,
                  MPIEventMgr eventMgr,
                  MPINotify   notify)
{
    MPITrajectory   trajectory;                 /* trajectory information */
    double          command;                    /* command position */
    double          position;                   /* target position  */
    long            returnValue;                /* MPI return value */

    /* Set up trajectory information */
    trajectory.velocity     = velocity;
    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 != startPosition)
        {
            position = startPosition;
        }
        else
        {
            position = endPosition;
        }

        /* Perform a single axis trapezoidal move until a key is pressed */
        singleAxisTrapMove(motion,
                          &trajectory,
                          &position);

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


/* Main routine */
int main(int     argc,
         char   *argv[])
{
    MPIControl          control;
    MPIControlType      controlType;
    MPIControlAddress   controlAddress;
    MPIMotion           motion;
    MPIAxis             axis;
    MPIEventMgr         eventMgr;
    MPINotify           notify;
    Service             service         = MPIHandleVOID;
    double              startPosition   = START_POSITION;
    double              endPosition     = END_POSITION;
    double              velocity        = VELOCITY;
    double              acceleration    = ACCELERATION;
    double              deceleration    = DECELERATION;
    long                axisNumber      = AXIS_NUMBER;
    long                motionNumber    = MOTION_NUMBER;

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

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

    /* Setup move parameters, start move, wait for motion done event, repeat */
    commandMoves(motion,
                 startPosition,
                 endPosition,
                 velocity,
                 acceleration,
                 deceleration,
                 eventMgr,
                 notify);

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

    return MPIMessageOK;
}


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