.

     

MPI Application Template
template.c
 

home2.c -- Simple Homing routine that captures the hardware position using
                    mpiNotifyEventWait, sets the origin and moves back to home.
/* home2.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 Homing routine that captures the hardware position using 
  mpiNotifyEventWait, sets the origin and moves back to home .

home2.c allows the user to trigger his home off an input or index
 pulse, wait for a home event that is captured by the hardware position, 
 set the origin to the home position, and then move back
 to that home position.

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"

/* MPI Object numbers */
#define MOTION_NUMBER   (0)
#define AXIS_NUMBER     (0)
#define MOTOR_NUMBER    (0)
#define  CAPTURE_COUNT  (1)      /* Enabled Capture count */
#define CAPTURE_NUMBER  (0)     /* Capture object number */
#define CAPTURE_INDEX   (0)     /* Index to capture engine */

/* Motion Parameters */
#define VELOCITY        (5000)  /* Move velocity     */
#define ACCELERATION    (10000) /* Move acceleration */
#define DECELERATION    (10000) /* Move deceleration */

/* CAPTURE_EDGE can be any MPICaptureEdge */
#define CAPTURE_EDGE    (MPICaptureEdgeFALLING)     /* Capture on falling edge */

/* CAPTURE_SOURCE can be any MPICaptureSource */
#define CAPTURE_SOURCE  (MPICaptureSourceHOME)      /* Capture on home input */

/* CAPTURE_TYPE can be any MPICaptureType */
#define CAPTURE_TYPE    (MPICaptureTypePOSITION)    /* hardware position latch */

/* 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,
                 MPIMotion          *motion,
                 long                motionNumber,
                 MPIAxis            *axis,
                 long                axisNumber,
                 MPIMotor           *motor,
                 long                motorNumber,
                 MPICapture         *capture,
                 long                captureNumber,
             long           captureCount,
             MPINotify        *notify,
             MPIEventMgr      *eventMgr)
{
   MPIControlConfig  controlConfig;
   MPIEventMask      eventMask;

    long          returnValue;


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

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

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

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

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

    /* Enable capture objects */
   returnValue =
      mpiControlConfigGet(*control,
                     &controlConfig,
                     NULL);
   msgCHECK(returnValue);

   controlConfig.captureCount = captureCount;

   returnValue =
      mpiControlConfigSet(*control,
                     &controlConfig,
                     NULL);
   msgCHECK(returnValue);

    /* Create capture object */
   *capture =
        mpiCaptureCreate(*control,
                         captureNumber);
    msgCHECK(mpiCaptureValidate(*capture));

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

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

    /* Clear eventMask */
    mpiEventMaskCLEAR(eventMask);

   /* Set all the bits that are associated with MPI Events*/
    mpiEventMaskALL(eventMask);

   /* Set all the bits that are associated with MEI Events*/
    meiEventMaskALL(eventMask);

   /* Request notification of all events (MPI and MEI events) from motion */
    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,
                    MPIMotor        *motor,
                    MPICapture      *capture,
               MPINotify      *notify,
               MPIEventMgr    *eventMgr)
{
    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 capture object */
    returnValue =
        mpiCaptureDelete(*capture);
    msgCHECK(returnValue);

    *capture = MPIHandleVOID;

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

    *motor = 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 home event get captured then set the origin to the home position */
void waitForHomeEvent(MPIAxis    axis,
                 MPICapture   capture,
                 MPIEventMgr  eventMgr,
                      MPINotify     notify)
{
    MPIEventStatus      eventStatus;
    MPICaptureStatus captureStatus;

   long           returnValue;


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

        /* Poll for motion event and wait for 5 seconds */
        returnValue =
            mpiNotifyEventWait(notify,
                               &eventStatus,
                               MPIWaitMSEC*5000);

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

   /* Get the capture status */
    returnValue =  mpiCaptureStatus(capture,
                           &captureStatus,
                           NULL);
    msgCHECK(returnValue);

    printf("CaptureState:0x%x\n", captureStatus.state);

    /* Set origin to home position */
    returnValue =
        mpiAxisOriginSet(axis,
                         captureStatus.latchedValue);
    msgCHECK(returnValue);

}


/* Configure capture object edge: 0 for falling edge, 1 for rising edge */
void configureCapture(MPICapture        capture,
                      MPICaptureSource  source,
                      MPICaptureEdge    edge,
                      MPICaptureType    type,
                      long          motorNumber,
                      long          captureIndex)
{
    MPICaptureConfig    captureConfig;

    long                returnValue;


    /* Disable capture */
    returnValue =
        mpiCaptureArm(capture,
                      FALSE);
    msgCHECK(returnValue);

   /*
      Clear out capture registers before modifying them.
      This keeps from unintentionally combining capture triggers.
   */
   returnValue = mpiCaptureConfigReset(capture);
   msgCHECK(returnValue);

   /* Read capture configuration */
    returnValue =
        mpiCaptureConfigGet(capture,
                            &captureConfig,
                            NULL);
    msgCHECK(returnValue);

    /* Set capture parameters */
    captureConfig.source[source].enabled = TRUE;
    captureConfig.source[source].invert  = FALSE;
    captureConfig.edge = edge;
    captureConfig.type = type;
    captureConfig.captureMotorNumber = motorNumber;
    captureConfig.feedbackMotorNumber = motorNumber;
    captureConfig.encoder = MPIMotorEncoderPRIMARY;
    captureConfig.captureIndex = captureIndex;

    /* Write capture configuration */
    returnValue =
        mpiCaptureConfigSet(capture,
                            &captureConfig,
                            NULL);
    msgCHECK(returnValue);
}


/* Configure Home Event action */
void configureHomeAction(MPIMotor   motor,
                         MPIAction  action,
                         long       activeHigh)
{
    MPIMotorEventConfig eventConfig;
    MPIEventMask        eventMask;

    long                returnValue;


    /* Read home event configuration */
    returnValue =
        mpiMotorEventConfigGet(motor,
                               MPIEventTypeHOME,
                               &eventConfig,
                               NULL);
    msgCHECK(returnValue);

    /* Configure Home Event action */
    eventConfig.action = action;
    eventConfig.trigger.polarity = activeHigh;

    /* Write home event configuration */
    returnValue =
        mpiMotorEventConfigSet(motor,
                               MPIEventTypeHOME,
                               &eventConfig,
                               NULL);
    msgCHECK(returnValue);

    /* Reset Home Event */
    mpiEventMaskCLEAR(eventMask);
    mpiEventMaskSET(eventMask, MPIEventTypeHOME);

   /* Clear previous possible old home events */
   returnValue =
        mpiMotorEventReset(motor,
                           eventMask);
    msgCHECK(returnValue);
}


/* Command simple trapezoidal motion */
void simpleVelocityMove(MPIMotion   motion,
                        double      velocity,
                        double      acceleration,
                        double      deceleration)
{
    MPIMotionParams params;      /* Motion parameters      */
    MPITrajectory   trajectory;  /* Trajectory information */

    long            returnValue; /* MPI library return value */


    /* Setup trajectory structure */
    trajectory.velocity     = velocity;
    trajectory.acceleration = acceleration;
    trajectory.deceleration = deceleration;
    trajectory.jerkPercent  = 0.0;          /* Trapezoidal profile */

    /* Setup parameters structure */
    params.velocity.trajectory  = &trajectory;

    /* Start motion */
    returnValue =
        mpiMotionStart(motion,
                       MPIMotionTypeVELOCITY,
                       &params);
    msgCHECK(returnValue);
}


/* Command simple trapezoidal motion */
void simpleTrapMove(MPIMotion   motion,
                    double      goalPosition,
                    double      velocity,
                    double      acceleration,
                    double      deceleration)
{
    MPIMotionParams params;      /* Motion parameters      */
    MPITrajectory   trajectory;  /* Trajectory information */

    long            returnValue; /* MPI library return value */


    /* Setup trajectory structure */
    trajectory.velocity     = velocity;
    trajectory.acceleration = acceleration;
    trajectory.deceleration = deceleration;

    /* Setup parameters structure */
    params.trapezoidal.trajectory   = &trajectory;
    params.trapezoidal.position     = &goalPosition;

    /* Start motion */
    returnValue =
        mpiMotionStart(motion,
                       MPIMotionTypeTRAPEZOIDAL,
                       &params);
    msgCHECK(returnValue);
}


int main(int     argc,
         char   *argv[])
{
    MPIControl          control;
    MPIControlType      controlType;
    MPIControlAddress   controlAddress;
    MPIMotion           motion;
    MPIAxis             axis;
    MPIMotor            motor;
    MPICapture          capture;
    MPINotify        notify;
   MPIEventMgr       eventMgr;

   long    returnValue;        /* Return value from library */


   /* The number of enabled captures must be greater than the id number of the capture used */
   meiAssert(CAPTURE_COUNT>CAPTURE_NUMBER);

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

    /* Create and initialize MPI objects */
    programInit(&control,
                controlType,
                &controlAddress,
                &motion,
                MOTION_NUMBER,
                &axis,
                AXIS_NUMBER,
                &motor,
                MOTOR_NUMBER,
                &capture,
                CAPTURE_NUMBER,
            CAPTURE_COUNT,
            &notify,
            &eventMgr);

    /* Configure capture */
    configureCapture(capture,
                     CAPTURE_SOURCE,
                     CAPTURE_EDGE,
                     CAPTURE_TYPE,
                     MOTOR_NUMBER,
                     CAPTURE_INDEX);

    /* Configure Home Event action to stop */
    configureHomeAction(motor,
                        MPIActionSTOP,
                        TRUE);          /* Active High */

    /* Arm the capture  */
    returnValue =
        mpiCaptureArm(capture,
                      TRUE);
    msgCHECK(returnValue);

    printf("Looking for home...\n");

    /* Start a velocity move */
    simpleVelocityMove(motion,
                       VELOCITY,
                       ACCELERATION,
                       DECELERATION);

   /* Wait until a Home event is received, then set origin to home position */
   waitForHomeEvent(axis,
                capture,
                eventMgr,
                 notify);

    /* Configure Home Event action to none */
    configureHomeAction(motor,
                        MPIActionNONE,
                        TRUE);

    /* Move back to home */
    simpleTrapMove(motion,
                   0.0,       /* Go back to home position */
                   VELOCITY,
                   ACCELERATION,
                   DECELERATION);

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

    return ((int)returnValue);
}


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