.

     

MPI Application Template
template.c
 

pvt1.c -- Simple motion path generation, specified by position/velocity/time points.
/* pvt1.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.
 */

/*

:Simple motion path generation, specified by position/velocity/time points.

This sample code demonstrates the motion type, MPIMotionTypePVT.  A simple
 trapezoidal velocity profile points list is created, and downloaded to
 the controller.  The time delta between each position is constant and
 the positions/velocities are spaced to generate an acceleration, constant
 velocity, and deceleration profile to the final position.

Several motion parameters must be initialized for the PVT motion type:

 params.pvt.pointCount - Specifies the number of points (position/vel./time).

 params.pvt.position - Pointer to a position[...] array.  There is one
    position value per point, per axis.  The length of the array must be
    equal to pointCount multiplied by the number of axes.  The positions
    are interleaved in the array by the axis index.

    For example, a three axis system would have:
        position[0] = position for axis 0
        position[1] = position for axis 1
        position[2] = position for axis 2
        position[3] = position for axis 0
        position[4] = position for axis 1
        etc.

 params.pvt.velocity - Pointer to a velocity[...] array.  There is one
    velocity value per point, per axis.  The length of the array must be
    equal to pointCount multiplied by the number of axes.  The velocities
    are interleaved in the array by the axis index.

    For example, a three axis system would have:
        velocity[0] = velocity for axis 0
        velocity[1] = velocity for axis 1
        velocity[2] = velocity for axis 2
        velocity[3] = velocity for axis 0
        velocity[4] = velocity for axis 1
        etc.

 params.pvt.time - Pointer to a time[...] array.  There is one time value
    per point.  The time specifies the number of seconds between the previous 
    position/velocity (point) and the specified position/velocity.  The length 
    of the time array must be equal to the pointCount.

 params.pvt.point.retain - Specifies whether the MPI and XMP should buffer the
    points after they have executed.  If retain = 0, the buffer will destroy
    the points after they have executed.  If retain = 1, the buffer will keep
    the points after they have executed.  This parameter is useful for future
    backup on path capability.

 params.pvt.point.final - Specifies if more points will be loaded.  If final
    = 1, no more points will be loaded.  If final = 0, more points can be
    loaded with mpiMotionModify(...) using the APPEND attribute.

 params.pvt.point.emptyCount - Specifies how many points the XMP-Series
    controller must have in it's buffer.  If the XMP's point buffer falls
    below the emptyCount, an E-Stop event will be generated by the motion
    supervisor, decelerating all the associated axes to a stop.

When the points are passed to mpiMotionStart(...), the MPI calculates
 accleration/jerk segments to fit exactly through the specified positions at
 the specified times.  The MPI library automatically handles buffering points
 lists that are larger than the XMP's buffer.  Presently, the XMP-Series
 controller can store 128 Frames per axis.  Each point requires one Frame
 per axis.

Special Considerations:

It is not necesary to specify the initial command position as the first
 position point.  The time[...] values can be constant or vary for each point.
 Constant time[...] values make the programmer's calculations easier.  For
 optimum performance, the number of points can be reduced, with no loss of
 resolution, by replacing the constant velocity sections with longer
 time[...] values.

When creating points lists longer than 64 points, you must create an Event
 Manager.  The Event Manager handles point buffering between the MPI and the
 XMP controller.

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.

*/

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

#include "stdmpi.h"
#include "stdmei.h"

#include "apputil.h"

#if defined(ARG_MAIN_RENAME)
#define main    pvt1Main

argMainRENAME(main, pvt1)
#endif

#define AXIS_COUNT      (1)
#define POINT_COUNT     (7)
#define TIME            (.2)    /* Time between points (seconds) */

double position[AXIS_COUNT * POINT_COUNT];
double velocity[AXIS_COUNT * POINT_COUNT];
double ptime[POINT_COUNT];

/* Simple trapezoidal profile velocity path, Positions */
double pointPosition[POINT_COUNT] = {
    0.0,
    400.0,      /* Accel to position */
    1200.0,     /* Constant vel to position */
    1600.0,     /* Decel to position */
   1200.0,     /* Move back */
   400.0,
   0.0 };

double pointVelocity[POINT_COUNT] = {
    0.0,
    4000.0,     /* Accel to vel */
    4000.0,     /* Constant vel */
    0.0,        /* Decel to zero vel */
   -4000.0,    /* Move back */
   -4000.0,
   0.0 };


/* Command line arguments and defaults */
long    axisNumber[AXIS_COUNT] = { 0,  };
long    motionNumber = 0;


/* 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,
             MPIAxis       axis[AXIS_COUNT],
             MPIMotion        *motion,
             MPIEventMgr      *eventMgr,
             MPINotify        *notify,
             Service       *service)
{
    MPIEventMask eventMask;
   long returnValue;
   long axisIndex;


     /* Obtain a Control handle */
    *control =
        mpiControlCreate(controlType,
                         controlAddress);
    msgCHECK(mpiControlValidate(*control));

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

    /* Create Axis objects */
    for (axisIndex = 0; axisIndex < AXIS_COUNT; axisIndex++) {
        axis[axisIndex] =
            mpiAxisCreate(*control,
                          axisNumber[axisIndex]);
        msgCHECK(mpiAxisValidate(axis[axisIndex]));
    }

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

    /* Append axis objects to motion object */
    for (axisIndex = 0; axisIndex < AXIS_COUNT; axisIndex++) {
        returnValue =
            mpiMotionAxisAppend(*motion,
                                axis[axisIndex]);
        msgCHECK(returnValue);
    }

    /* Request notification of all events from motion */
    mpiEventMaskCLEAR(eventMask);
    mpiEventMaskALL(eventMask);
    returnValue =
        mpiMotionEventNotifySet(*motion,
                                eventMask,
                                NULL);
    msgCHECK(returnValue);

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

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

    /* Add notify to event manager's list */
    returnValue =
        mpiEventMgrNotifyAppend(*eventMgr,
                                *notify);
    msgCHECK(returnValue);

    /* Create service thread */
    *service =
        serviceCreate(*eventMgr,
                      -1,   /* Default (max) priority */
                      -1);  /* Default sleep (msec) */
    mpiAssert(service != NULL);
}


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

    /* Delete objects */
    returnValue = serviceDelete(service);
    msgCHECK(returnValue);

    returnValue = mpiEventMgrDelete(eventMgr);
    msgCHECK(returnValue);

    returnValue = mpiNotifyDelete(notify);
    msgCHECK(returnValue);

    returnValue = mpiMotionDelete(motion);
    msgCHECK(returnValue);

    for (axisIndex = 0; axisIndex < AXIS_COUNT; axisIndex++) {
        returnValue = mpiAxisDelete(axis[axisIndex]);
        msgCHECK(returnValue);
    }

    returnValue = mpiControlDelete(control);
    msgCHECK(returnValue);

}


int
    main(int    argc,
         char   *argv[])
{
    MPIControl      control;                /* Control object */
    MPIAxis         axis[MPIXmpMAX_Axes];   /* Array of axis objects */
    MPIMotion       motion;                 /* Motion object */
    MPINotify       notify;                 /* Event notification handle */
    MPIEventMgr     eventMgr;               /* Event manager handle */
    MPIMotionParams params;                 /* MPI motion parameters */

    Service         service;                /* Event manager service handle */

    long    returnValue;

    MPIControlType      controlType;
    MPIControlAddress   controlAddress;

    long    pointIndex;
    long    axisIndex;
    long    motionDone;

    double  initialPosition[MPIXmpMAX_Axes];

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

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

   /* Append axis objects to motion object */
    for (axisIndex = 0; axisIndex < AXIS_COUNT; axisIndex++) {
        /* Read initial command position */
        returnValue =
            mpiAxisCommandPositionGet(axis[axisIndex],
                                      &initialPosition[axisIndex]);
        msgCHECK(returnValue);

    }


    /* Initialize motion params structure */
    params.pvt.pointCount = POINT_COUNT;
    params.pvt.position = position;
    params.pvt.velocity = velocity;
    params.pvt.time = ptime;
    params.pvt.point.retain = 0;    /* Flush frame buffer after execution */
    params.pvt.point.final = 1;     /* Last point */
    params.pvt.point.emptyCount = 5;/* Start E-Stop if frames left to execute
                                        is less than this limit. -1 disables */

    /* Create points */
    for (pointIndex = 0; pointIndex < POINT_COUNT; pointIndex++) {

        ptime[pointIndex] = TIME;   /* Delta time between points */

        for (axisIndex = 0; axisIndex < AXIS_COUNT; axisIndex++) {
            /* Position at point */
            position[(pointIndex * AXIS_COUNT) + axisIndex] =
                pointPosition[pointIndex] + initialPosition[axisIndex];

            /* Velocity at point */
            velocity[(pointIndex * AXIS_COUNT) + axisIndex] =
                pointVelocity[pointIndex];
        }
    }

    /* Start motion */
    returnValue =
        mpiMotionStart(motion,
                       MPIMotionTypePVT,
                       &params);
    fprintf(stderr,
        "mpiMotionStart returns 0x%x: %s\n",
        returnValue,
        mpiMessage(returnValue, NULL));
    msgCHECK(returnValue);


    /* Collect motion events */
    motionDone = FALSE;
    while (motionDone != TRUE) {

        MPIEventStatus  eventStatus;

        returnValue =
            mpiNotifyEventWait(notify,
                               &eventStatus,
                               MPIWaitFOREVER);

        switch(eventStatus.type) {

            case MPIEventTypeMOTION_DONE: {
                motionDone = TRUE;
                break;
            }
            default: {
                break;
            }
        }

        fprintf(stderr,
                "mpiNotifyEventWait() returns 0x%x\n"
                "\teventStatus: type %d source 0x%x info 0x%x\n",
                returnValue,
                eventStatus.type,
                eventStatus.source,
                eventStatus.info[0]);
        msgCHECK(returnValue);

    }

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

    return ((int)returnValue);
}


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