.

     

MPI Application Template
template.c
 

UsrLim2.c -- Position comparison using User Limits to generate events.
/* UsrLim2.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.
 */

/*

:Position comparison using User Limits to generate events.

This sample code shows how to configure the XMP controller's User Limits
 to compare an axis' actual position to a specific position value.  If the
 actual position is greater than or equal to the value specified, then
 the controller will generate a User Event to the Host.

The function CompareLimitSet(...) demonstrates how to configure a
 User Limit for position compare.


The XMP-Series controller "User Limit" feature allows the user to program a
 the result of two logical condition to generate an event to the host.  Also,
 a "User Limit" can be configured to write an "output" to any XMP memory
 location, using an AND mask and OR mask.

Here is the "User Limit" block structure:

typedef struct {
    MPIXmpLimitType         Type;
    void                    *SourceAddress;
    long                    Mask;
    MPIXmpGenericValue      LimitValue;
} MPIXmpLimitCondition;

typedef struct {
    long                    AndMask;
    long                    OrMask;
    long                    *OutputPtr;
    long                    Enabled;
} MPIXmpLimitOutput;

typedef struct {
    MPIXmpLimitCondition    Condition[MPIXmpLimitConditions];
    MPIXmpStatus            Status;
    MPIXmpLogic             Logic;
    MPIXmpLimitOutput       Output;
    long                    Count;
    long                    State;
} MPIXmpLimitData;


MPIXmpLimitTypes are the operators that are used for the User Limit's
 Condition[0] and Condition[1].  They are found in xmp.h.

*SourceAddress is a pointer to an XMP memory location.

Mask is ANDed with the value located at the *SourceAddress.

LimitValue is compared with the masked value located at *SourceAddress,
 using the Type operator.

Status defines the status bit used to generate an event to the host.

MPIXmpLogic is the logic applied between the two condition block outputs,
 Condition[0] and Condition[1]:

 MPIXmpLogicNEVER
    Does NOT evaluate Condition[0], Condition[1].  No event is generated.

 MPIXmpLogicSINGLE
    Only evaluates Condition[0].  Event is generated if Condition[0] is TRUE.

 MPIXmpLogicOR
    Evaluates Condition[0], Condition[1].  Event is generated if (Condition[0]
    OR Condition[1]) = TRUE.

 MPIXmpLogicAND
    Evaluates Condition[0], Condition[1].  Event is generated if (Condition[0]
    AND Condition[1]) = TRUE.

The other MPIXmpLogic enums in xmp.h are for internal use only.

*OutputPtr is a pointer to an XMP memory location.

AndMask is ANDed, and OrMask is ORed with the value located at
 *OutputPtr when the resultant MPIXmpLogic applied Condition[0] and
 Condition[1] are TRUE.

Count and State are for internal use only.  Do not write these values.
  The MPI method, mpiMotorEventConfigSet(...) will not write these values.


The XMP supports up to 8 User Limits per motor.  The User Limit processing
 occurs in the firmware background task.  For maximum efficiency, the XMP only
 processes the User Limits (in order 0, 1, 2, etc.), up to the largest User
 Limit number that has (motorEventConfig.Logic ! = MPIXmpLogicNEVER).

For example:
 If UserLimit 0 (motor 0) is configured for MPIXmpLogicSINGLE, then the XMP will
    only process the first UserLimit for each motor.
 If UserLimit 3 (motor 0) is configured for MPIXmpLogicSINGLE, then the XMP will
    process UserLimits number 0, 1, 2, and 3 for each motor.

It's best to use the lower numbered UserLimits for maximum efficiency.  When
 finished using UserLimits, it's a good idea to set the Logic to
 MPIXmpLogicNEVER.

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"

#define MOTION_COUNT        (2)

/* User Limit configurations */
#define EVENT_TYPE          (MPIEventTypeLIMIT_USER0)   /* 0, 1, 2...7 */
#define COMPARE_POSITION    (10000)                     /* Actual Position */

/* Command line arguments and defaults */
long            axisNumber      = 0;
long            motionNumber    = 0;
long            motorNumber     = 0;
MPIMotionType   motionType      = MPIMotionTypeS_CURVE;

Arg argList[] = {
    {   "-axis",    ArgTypeLONG,    &axisNumber,    },
    {   "-motion",  ArgTypeLONG,    &motionNumber,  },
    {   "-motor",   ArgTypeLONG,    &motorNumber,   },
    {   "-type",    ArgTypeLONG,    &motionType,    },

    {   NULL,       ArgTypeINVALID, NULL,   }
};

double position[MOTION_COUNT] = {
    20000.0,
    0.0,
};

MPITrajectory trajectory[MOTION_COUNT] = {
    /* velocity     accel       decel       jerkPercent */
    { 10000.0,      1000000.0,  1000000.0,  0.0,    },
    { 10000.0,      100000.0,   100000.0,   0.0,    },
};

/* Motion Parameters */
MPIMotionSCurve sCurve[MOTION_COUNT] = {
    {   &trajectory[0], &position[0],   },
    {   &trajectory[1], &position[1],   },
};

MPIMotionTrapezoidal    trapezoidal[MOTION_COUNT] = {
    {   &trajectory[0], &position[0],   },
    {   &trajectory[1], &position[1],   },
};

MPIMotionVelocity   velocity[MOTION_COUNT] = {
    {   &trajectory[0], },
    {   &trajectory[1], },
};


/* Function Prototypes */
long CompareLimitSet(MPIAxis axis,
                     MPIMotor motor,
                     long position,
                     MPIEventType eventType);


int
    main(int    argc,
         char   *argv[])
{
    MPIControl  control;    /* motion controller handle */
    MPIAxis     axis;       /* axis object */
    MPIMotion   motion;     /* motion object */
    MPIMotor    motor;      /* motor object */
    MPINotify   notify;     /* event notification object */
    MPIEventMgr eventMgr;   /* event manager handle */

    MPIEventMask    eventMask;

    long    returnValue;    /* return value from library */

    long    index;
    long    motionDone;     /* flag when Done occurs */

    Service service;

    MPIControlType      controlType;
    MPIControlAddress   controlAddress;

    long    argIndex;

    /* 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 >= MPIXmpMAX_Axes) ||
        (motionNumber >= MPIXmpMAX_MSs) ||
        (motorNumber >= MPIXmpMAX_Motors) ||
        (motionType < MPIMotionTypeFIRST) ||
        (motionType >= MPIMotionTypeLAST)) {
        mpiPlatformConsole("usage: %s %s\n"
                           "\t\t[-axis # (0 .. %d)]\n"
                           "\t\t[-motion # (0 .. %d)]\n"
                           "\t\t[-motor # (0 .. %d)]\n"
                           "\t\t[-type # (0 .. %d)]\n",
                            argv[0],
                            ArgUSAGE,
                            MPIXmpMAX_Axes - 1,
                            MPIXmpMAX_MSs - 1,
                            MPIXmpMAX_Motors - 1,
                            MPIMotionTypeLAST - 1);
        exit(MPIMessageARG_INVALID);
    }

    switch (motionType) {
        case MPIMotionTypeS_CURVE:
        case MPIMotionTypeTRAPEZOIDAL:
        case MPIMotionTypeVELOCITY: {
            break;
        }
        default: {
            mpiPlatformConsole("%s: %d: motion type not available\n",
                                argv[0],
                                motionType);
            exit(MPIMessageUNSUPPORTED);
            break;
        }
    }

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

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

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

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

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

    /* Request notification of ALL events from motion */
    mpiEventMaskCLEAR(eventMask);
    mpiEventMaskALL(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);  /* -1 => enable interrupts */
    mpiAssert(service != NULL);

    /* Configure the Position Compare, using a User Limit */
    returnValue =
        CompareLimitSet(axis,
                        motor,
                        COMPARE_POSITION,
                        EVENT_TYPE);
    msgCHECK(returnValue);

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

    /* Loop repeatedly */
    index       = 0;
    motionDone  = TRUE;
    while (mpiPlatformKey(MPIWaitPOLL) <= 0) {
        MPIEventStatus      eventStatus;
        MPIEventStatusInfo  *info;

        if (motionDone) {
            MPIMotionParams     motionParams;       /* motion parameters */

            /* fill in the MPIMotionParams structure */
            switch (motionType) {
                case MPIMotionTypeS_CURVE: {
                    motionParams.sCurve = sCurve[index];
                    break;
                }
                case MPIMotionTypeTRAPEZOIDAL: {
                    motionParams.trapezoidal = trapezoidal[index];
                    break;
                }
                case MPIMotionTypeVELOCITY: {
                    motionParams.velocity = velocity[index];
                    break;
                }
                default: {
                    mpiAssert(FALSE);
                    break;
                }
            }

            printf("\n\nMotion Start...");

            /* Start Motion */
            returnValue =
                mpiMotionStart(motion,
                               motionType,
                               &motionParams);
            msgCHECK(returnValue);

            motionDone = FALSE;
        }

        /* Wait for events */
        returnValue =
            mpiNotifyEventWait(notify,
                               &eventStatus,
                               MPIWaitFOREVER);
        msgCHECK(returnValue);

        info = (MPIEventStatusInfo *)eventStatus.info;

        switch (eventStatus.type) {
            /* In Coarse Event from axis source */
            case MPIEventTypeIN_POSITION_COARSE: {
                printf("\nInCoarse (%ld)",
                       info->data.axis.sampleCounter);
                break;
            }
            /* In Fine Event from axis source */
            case MPIEventTypeIN_POSITION_FINE: {
                printf("\nInFine (%ld)",
                       info->data.axis.sampleCounter);
                break;
            }
            /* In Fine Event from axis source */
            case MPIEventTypeAT_TARGET: {
                printf("\nAtTarget (%ld)",
                       info->data.axis.sampleCounter);
                break;
            }
            /* Motion Done Event from motion source */
            case MPIEventTypeMOTION_DONE: {
                printf("\nDone (%ld)",
                       info->data.motion.sampleCounter);

                motionDone = TRUE;

                if (++index >= MOTION_COUNT) {
                    index = 0;
                }
                break;
            }
            /* User Limit Event from motor source */
            case MPIEventTypeLIMIT_USER0: {
                printf("\nUser Limit #0, type %d source 0x%x info 0x%x",
                        eventStatus.type,
                        eventStatus.source,
                        eventStatus.info[0]);
                break;
            }
            /* User Limit Event from motor source */
            case MPIEventTypeLIMIT_USER1: {
                printf("\nUser Limit #1, type %d source 0x%x info 0x%x",
                        eventStatus.type,
                        eventStatus.source,
                        eventStatus.info[0]);
                break;
            }
            /* User Limit Event from motor source */
            case MPIEventTypeLIMIT_USER2: {
                printf("\nUser Limit #2, type %d source 0x%x info 0x%x",
                        eventStatus.type,
                        eventStatus.source,
                        eventStatus.info[0]);
                break;
            }
            /* User Limit Event from motor source */
            case MPIEventTypeLIMIT_USER3: {
                printf("\nUser Limit #3, type %d source 0x%x info 0x%x",
                        eventStatus.type,
                        eventStatus.source,
                        eventStatus.info[0]);
                break;
            }
            /* User Limit Event from motor source */
            case MPIEventTypeLIMIT_USER4: {
                printf("\nUser Limit #4, type %d source 0x%x info 0x%x",
                        eventStatus.type,
                        eventStatus.source,
                        eventStatus.info[0]);
                break;
            }
            /* User Limit Event from motor source */
            case MPIEventTypeLIMIT_USER5: {
                printf("\nUser Limit #5, type %d source 0x%x info 0x%x",
                        eventStatus.type,
                        eventStatus.source,
                        eventStatus.info[0]);
                break;
            }
            /* User Limit Event from motor source */
            case MPIEventTypeLIMIT_USER6: {
                printf("\nUser Limit #6, type %d source 0x%x info 0x%x",
                        eventStatus.type,
                        eventStatus.source,
                        eventStatus.info[0]);
                break;
            }
            /* User Limit Event from motor source */
            case MPIEventTypeLIMIT_USER7: {
                printf("\nUser Limit #7, type %d source 0x%x info 0x%x",
                        eventStatus.type,
                        eventStatus.source,
                        eventStatus.info[0]);
                break;
            }

            default: {
                printf("mpiNotifyEventWait() returns 0x%x\n"
                    "\teventStatus: type %d source 0x%x info 0x%x\n",
                    returnValue,
                    eventStatus.type,
                    eventStatus.source,
                    eventStatus.info[0]);
                break;
            }
        }
    }

    printf("\n");

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

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

    returnValue = mpiAxisDelete(axis);
    msgCHECK(returnValue);

    returnValue = serviceDelete(service);
    msgCHECK(returnValue);

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

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

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

    return ((int)returnValue);
}

long CompareLimitSet(MPIAxis axis,
                     MPIMotor motor,
                     long position,
                     MPIEventType eventType)
{
    MPIXmpAxis          *xmpAxis;

    MPIMotorLimitConfig motorEventConfig;
    MPIXmpStatus        status;

    long                returnValue;
    MPIEventMask        resetMask;

    returnValue  =
        mpiAxisMemory(axis,
                      (void **)&xmpAxis);

    mpiEventMaskCLEAR(resetMask);

    if (returnValue == MPIMessageOK) {
        switch (eventType) {
            case MPIEventTypeLIMIT_USER0: {
                status = MPIXmpStatusLIMIT;
                mpiEventMaskSET(resetMask, MPIEventTypeLIMIT_USER0);
                break;
            }
            case MPIEventTypeLIMIT_USER1: {
                status = MPIXmpStatusLIMIT;
                mpiEventMaskSET(resetMask, MPIEventTypeLIMIT_USER1);
                break;
            }
            case MPIEventTypeLIMIT_USER2: {
                status = MPIXmpStatusLIMIT;
                mpiEventMaskSET(resetMask, MPIEventTypeLIMIT_USER2);
                break;
            }
            case MPIEventTypeLIMIT_USER3: {
                status = MPIXmpStatusLIMIT;
                mpiEventMaskSET(resetMask, MPIEventTypeLIMIT_USER3);
                break;
            }
            case MPIEventTypeLIMIT_USER4: {
                status = MPIXmpStatusLIMIT;
                mpiEventMaskSET(resetMask, MPIEventTypeLIMIT_USER4);
                break;
            }
            case MPIEventTypeLIMIT_USER5: {
                status = MPIXmpStatusLIMIT;
                mpiEventMaskSET(resetMask, MPIEventTypeLIMIT_USER5);
                break;
            }
            case MPIEventTypeLIMIT_USER6: {
                status = MPIXmpStatusLIMIT;
                mpiEventMaskSET(resetMask, MPIEventTypeLIMIT_USER6);
                break;
            }
            case MPIEventTypeLIMIT_USER7: {
                status = MPIXmpStatusLIMIT;
                mpiEventMaskSET(resetMask, MPIEventTypeLIMIT_USER7);
                break;
            }
            default:
                printf("\nError, invalid event type\n");
                break;
        }

        returnValue =
            mpiMotorEventConfigGet(motor,
                                   (MPIEventType)eventType,
                                   NULL,
                                   &motorEventConfig);
    }

    if (returnValue == MPIMessageOK) {
        /* Set up the Condition[0] block.  The operator between the value in the
            SourceAddress (masked by the Mask), and LimitValue.l, is
            MPIXmpLimitTypeGT (greater than).  Thus, when the value in
            SourceAddress is "GT" (greater than) the value in LimitValue.l,
            then the Condition[0] block is TRUE.
        */
        motorEventConfig.Condition[0].Type = MPIXmpLimitTypeGT;
        motorEventConfig.Condition[0].SourceAddress = &(xmpAxis->ActPosition);
        motorEventConfig.Condition[0].Mask = 0xffffffff;
        motorEventConfig.Condition[0].LimitValue.g32.l = position;

        /* Set up the Condition[1] block.  The operator is MPIXmpLimitTypeFALSE.
            Therefore, the Condition[1] block is always FALSE.
        */
        motorEventConfig.Condition[1].Type = MPIXmpLimitTypeFALSE;
        motorEventConfig.Condition[1].SourceAddress = NULL;
        motorEventConfig.Condition[1].Mask = 0;
        motorEventConfig.Condition[1].LimitValue.g32.l = 0;

        motorEventConfig.Status = status;
        /* Determine logic result from Condition[0] only */
        motorEventConfig.Logic  = MPIXmpLogicSINGLE;

        /* Disable the output feature */
        motorEventConfig.Output.OutputPtr = NULL;
        motorEventConfig.Output.AndMask = 0x0;
        motorEventConfig.Output.OrMask = 0x0;
        motorEventConfig.Output.Enabled = 0;

        returnValue =
            mpiMotorEventConfigSet(motor,
                                   (MPIEventType)eventType,
                                   NULL,
                                   &motorEventConfig);
    }

    /* Reset event in case conditions have already been satisfied */
    if (returnValue == MPIMessageOK) {
        returnValue =
                mpiMotorEventReset(motor,
                                   resetMask);
    }

    return returnValue;
}


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