.


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

/*
 
This program perform a simple trapezoidal move while tracing any function entry
 (see #define TRACE_OPTION) with a specific input (see #define LOOKUP_ENTRY), 
 write this specified trace into a circFile called trace2.txt (see #define CIRC_FILE) to 
 save disk space. It will also write the time when these trace is written to the file.

For a different TRACE_OPTION, see MEITrace enumeration below.
   typedef enum {
      MEITraceNONE
      MEITraceFIRST
      MEITraceFUNCTION_ENTRY
      MEITraceFUNCTION_RETURN
      MEITraceMEMORY_ALLOC
      MEITraceMEMORY_FREE  
      MEITraceMEMORY_GET   
      MEITraceMEMORY_SET   
      MEITraceVALIDATE  
      MEITraceLOCK_GIVE 
      MEITraceLOCK_TAKE 
      MEITraceEVENT     
      MEITraceLAST 
      MEITraceALL
   } MEITrace;

Example of what is written in the circular file when LOOKUP_ENTRY is
 defined as "mpiMotionStart":
   000000: 17:16:44 mpiMotionStart(0x843d20, 9, 0x12fdb4) 

A circular file is defined in CircFile.c and CircFile.h

  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 <string.h>
#include <time.h>

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

#include "apputil.h"
#include "CircFile.h"

#if defined(ARG_MAIN_RENAME)
#define main    trace2Main
argMainRENAME(main, trace2)
#endif

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

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

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

#define CIRC_FILE    "trace2.txt"
#define LOOKUP_ENTRY "mpiMotionStart"
#define TRACE_OPTION MEITraceFUNCTION_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);
}


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

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


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

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

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

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


/* Display or write trace buffer to the circFile */
long traceFunction(const char *buffer)
{
   char *lookUpEntry = LOOKUP_ENTRY;
   struct tm *myTm;
   time_t myTime;

   /* Write to circular file only when it is the specified entry */
   if(strncmp(buffer,lookUpEntry,strlen(lookUpEntry)) == 0)
   {
      /* Get time */
      time(&myTime);
      myTm = localtime(&myTime);

      /* Write time and entry to circular file */
      circFilePrint(cfp,"%d:%d:%d ", myTm->tm_hour, myTm->tm_min, myTm->tm_sec);
      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 */
   meiTraceFunction(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