.

     

MPI Application Template
template.c
 

home3.c -- Hard stop homing then set position to zero
/* home3.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.
 */

/*
 
: Hard stop homing then set position to zero
 
This application demonstrates how to do hard stop homing by looking at
 the position error limit. It shows how to set error limit event configuration
 then to command velocity move. It will wait until a position error limit is 
 triggered and set the home position to zero.

This application will change your E_STOP deceleration time to 1 seconds. 
 Please change this value accordingly.

It is highly recommended to command a very slow move when doing running
 this application by changing the VELOCITY, ACCELERATION and DECELERATION.
 
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 VELOCITY               (1000.0)
#define ACCELERATION           (10000.0)
#define DECELERATION           (10000.0)
#define POSITION_ERROR_LIMIT   (100.00)


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

    /* Application-specific command line arguments */
    Arg argList[] =
    {
      {   "-errorLimit",   ArgTypeFLOAT,    positionLimit, },
        {   "-motion",     ArgTypeLONG,     motionNumber,  },
        {   "-axis",       ArgTypeLONG,     axisNumber,    },
        {   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  > (MPIXmpMAX_Axes-1)))
    {
        mpiPlatformConsole("usage: %s %s\n"
                           "\t\t[-axis   # (0 .. %d)]\n"
                           "\t\t[-motion # (0 .. %d)]\n",
                            argv[0],
                            ArgUSAGE,
                            MPIXmpMAX_Axes - 1,
                            MPIXmpMAX_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,
                 MPIMotor           *motor,
                 MPIEventMgr        *eventMgr,
                 MPINotify          *notify)
{
    MPIEventMask    eventMask;
    long            returnValue;

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

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

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

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

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

    /* Attach axis */
     returnValue =
            mpiMotionAxisAppend(*motion, *axis);
        msgCHECK(returnValue);

    /* Map axis to motion supervisor on XMP */
    returnValue =
        mpiMotionAction(*motion, MPIActionMAP);
    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);
    mpiEventMaskALL(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,
                    MPIMotor    *motor,
                    MPIEventMgr *eventMgr,
                    MPINotify   *notify)
{
    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 axis objects */
    returnValue =
        mpiAxisDelete(*axis);
    msgCHECK(returnValue);
    *axis = MPIHandleVOID;

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

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


/* Perform a Velocity move */
void velocityMove(MPITrajectory     *trajectory,
                  MPIMotionParams   *params,
                  MPIMotion         *motion)
{
    long    returnValue;

    /* Setup motion parameters */
    trajectory->velocity        = VELOCITY;
    trajectory->acceleration    = ACCELERATION;
    trajectory->deceleration    = DECELERATION;    /* Not used for velocity move */
    trajectory->jerkPercent     = 0.0;          /* Not used for velocity move */
    params->velocity.trajectory = trajectory;

    /* Start a velocity move */
    returnValue =
        mpiMotionStart(*motion,
                       MPIMotionTypeVELOCITY,
                       params);
    msgCHECK(returnValue);
}


/* Wait for events */
void waitForEvent(MPIAxis     axis,
              MPIMotor        motor,
              MPIEventMgr     eventMgr,
              MPIEventType    eventType,
              MPINotify       notify,
              float           positionLimit)
{
    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 == MPIEventTypeLIMIT_ERROR)
            {
            printf("PositionErrorLimit Received\n");
                break;
            }
         else if (eventStatus.type == MPIEventTypeMOTION_DONE)
            {
            printf("MotionDoneEvent Received\n");
                break;
            }
        }
        else
        {
            mpiAssert(returnValue == MPIMessageTIMEOUT);
        }
    }
}


/* Set the current position to be zero by changing the origin */
void zeroPosition(MPIMotion   motion,
              MPIAxis   axis)
{
   double            actualPosition;
   double            commandPosition;
   double            originValue;
   long              returnValue;

   returnValue =
      mpiMotionPositionGet(motion,
                           &actualPosition,
                           &commandPosition);
   msgCHECK(returnValue);

   returnValue =
        mpiAxisOriginGet(axis,
                         &originValue);
   msgCHECK(returnValue);

   returnValue =
        mpiAxisOriginSet(axis,
                         originValue+actualPosition);
    msgCHECK(returnValue);
}


int main(int     argc,
         char   *argv[])
{
    MPIControl            control;
    MPIControlType        controlType;
    MPIControlAddress     controlAddress;
    MPIMotion             motion;
    MPIMotionConfig       config;
    MPIAxis               axis;
    MPIMotor              motor;
    MPIEventMgr           eventMgr;
    MPINotify             notify;
    MPIMotorLimitConfig   eventConfig;
    MPITrajectory         trajectory;
    MPIMotionParams       params;
    double                velocity       = VELOCITY;
    double                acceleration   = ACCELERATION;
    double                deceleration   = DECELERATION;
    float                 positionLimit  = POSITION_ERROR_LIMIT;
    long                  motionNumber   = MOTION_NUMBER;
    long                  axisNumber     = AXIS_NUMBER;
    long                  returnValue;

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

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

   /*Configure estop time*/
   returnValue =
      mpiMotionConfigGet(motion,
                         &config,
                         NULL);
   msgCHECK(returnValue);

   config.decelTime.eStop = 1; /*Estop decel is set to 1 seconds */

   returnValue =
      mpiMotionConfigSet(motion,
                         &config,
                         NULL);
   msgCHECK(returnValue);

   /* Set the action limit trigger to E_STOP_ABORT */
   returnValue =
      mpiMotorEventConfigGet(motor,
                             MPIEventTypeLIMIT_ERROR,
                             &eventConfig,
                             NULL);
   msgCHECK(returnValue);

   eventConfig.action = MPIActionE_STOP_ABORT;
   eventConfig.trigger.error = positionLimit;

   returnValue =
      mpiMotorEventConfigSet(motor,
                             MPIEventTypeLIMIT_ERROR,
                             &eventConfig,
                             NULL);
   msgCHECK(returnValue);

   /* Perform a Velocity move */
    velocityMove(&trajectory,
                 &params,
                 &motion);

   /* Wait for a position error event */
   waitForEvent(axis,
                motor,
                eventMgr,
                MPIEventTypeLIMIT_ERROR,
                notify,
                positionLimit);

   /* Sleep 5 seconds to make sure that axis is in STOP state */
   mpiPlatformSleep(5000);

   /* Set axis in idle state */
   returnValue =
      mpiMotionAction(motion, MPIActionRESET);
   msgCHECK(returnValue);

   /* Axis is homed, then set current position to be zero position */
   zeroPosition(motion, axis);

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

    return MPIMessageOK;
}



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