.


Trace1.c
/* Trace1.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.
 */

/*
 
Trace1 shows how to setup MPI tracing and log the data to a circular file.  A
 circular file is similar in concept to a circular buffer; it allows trace data
 to be continually written without taking up too much space.

Trace1 performs a simple trapezoidal move while logging the function entry
 points for all MPI functions internal and external and is useful when tracking
 down application problems.  Function exit points or status points can be traced
 by changing the TRACE_OPTION macro.

The trace into a circFile called "trace1.txt", but may be renamed by changing
 the CIRC_FILE macro.

To convert the circular files to normal "flat" files, please refer to the
 CircFileRead sample application.

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"
#include "CircFile.h"

#if defined(ARG_MAIN_RENAME)
#define main    trace1Main
argMainRENAME(main, trace1)
#endif

#define MOTION_NUMBER   (0)
#define AXIS_NUMBER     (0)

#define GOAL_POSITION   (100)
#define VELOCITY        (5000)
#define ACCELERATION    (10000)
#define DECELERATION    (20000)

#define LINE_LENGTH     (80)    
#define LINE_COUNT      (100)

#define CIRC_FILE       "trace1.txt"
#define TRACE_OPTION    (MPITraceFUNCTION_ENTRY)

CircFilePtr cfp = NULL;

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

    /* Application-specific command line arguments */
    Arg argList[] =
    {
        {   "-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)
    {
        fprintf(stderr,"usage: %s %s\n"
                       "\t\t[-axis #(0...%ld)]\n",
                       argv[0],
                       ArgUSAGE,
                       MPIControlMAX_AXES);
        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)
{
    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,       /* motion supervisor number */
                        *axis);             /* axis object handle */
    msgCHECK(mpiMotionValidate(*motion));
}


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

    /* Delete motion supervisor object */
    returnValue =
        mpiMotionDelete(*motion);
    msgCHECK(returnValue);

    /* Delete axis object */
    returnValue =
        mpiAxisDelete(*axis);
    msgCHECK(returnValue);

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

}


/*  Select which to trace and open circFile to write */
void setupTrace(char *circFileName,
                long lineLength,
                long lineCount,
                long traceOption)
{
    MPITraceMask rv;

    /* Set trace option of interest, see MPITrace enum for more option */
    rv = mpiTraceSet(traceOption);

    /* Open circFile for writing purpose */
    cfp = circFileOpenForWrite(circFileName, lineLength, lineCount);

    if(cfp == NULL)
    {
        fprintf(stderr,"Unable to open file %s\n", circFileName);
    }

}


/* Close circular file and restore the mpiTraceFunction to default */
void endTrace(void)
{
    /* Set the function back to default */
    mpiTraceFunction(NULL);

    /* Close circFile after use */
    circFileClose(cfp);
}


/* Display or write trace buffer to the circFile */
long traceFunction(const char *buffer)
{
    circFileWrite(cfp, buffer);
    return 0;
}


/* Command simple trapezoidal motion */
long 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);

    return returnValue;
}


int main(int    argc,
         char   *argv[])
{
    MPIControl          control;    /* motion controller object handle */
    MPIControlType      controlType;
    MPIControlAddress   controlAddress;
    MPIMotion           motion;     /* motion object handle */
    MPIAxis             axis;       /* axis object handle */

    long                axisNumber = AXIS_NUMBER;


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

    /* Create and initialize MPI objects */
    programInit(&control,
                controlType,
                &controlAddress,
                &motion,
                MOTION_NUMBER,
                &axis,
                AXIS_NUMBER);

    /* Setup Trace module and Open circFile*/
    setupTrace(CIRC_FILE,
               LINE_LENGTH,
               LINE_COUNT,
               TRACE_OPTION);

    /* Sets the function used to display a trace buffer */
    mpiTraceFunction(traceFunction);

    /* Command simple motion */
    simpleTrapMove(motion,
                   GOAL_POSITION,
                   VELOCITY,
                   ACCELERATION,
                   DECELERATION);

    /* Clean up and return trace settings to the default one */
    endTrace();

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

    return MPIMessageOK;
}

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