.

     

MPI Application Template
template.c
 

record1.c -- Read/display data recorder records from specified axis (default 0)
/* record1.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.
 */

/*

:Read/display data recorder records from specified axis (default 0)

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 AXIS_COUNT      (1)
#define RECORD_COUNT    (100)


/* 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,
                 MPIAxis           *axis,
                 long               axisCount,
                 long              *axisNumbers,
                 MPIRecorder       *recorder)
{
    long axisIndex;
    long returnValue;

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

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

    /* Create axis objects */
    for (axisIndex=0; axisIndex<axisCount; axisIndex++) {
        axis[axisIndex] =
            mpiAxisCreate(*control,
                          axisNumbers[axisIndex]);
        msgCHECK(mpiAxisValidate(axis[axisIndex]));
    }

    /* Create recorder object */
    *recorder =
        mpiRecorderCreate(*control,
                    -1);      /* allocate next available recorder */
    msgCHECK(mpiRecorderValidate(*recorder));
}

/* Perform certain cleanup actions and delete MPI objects */
void programCleanup(MPIControl  *control,
                    MPIAxis     *axis,
                    long         axisCount,
                    MPIRecorder *recorder)
{
    long axisIndex;
    long returnValue;

    /* Delete recorder object */
    returnValue =
        mpiRecorderDelete(*recorder);
    msgCHECK(returnValue);

    /* Delete axis objects */
    for (axisIndex=0; axisIndex<axisCount; axisIndex++) {
        returnValue =
            mpiAxisDelete(axis[axisIndex]);
        msgCHECK(returnValue);

        axis[axisIndex] = MPIHandleVOID;
    }

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

    *control = MPIHandleVOID;
}

/* Start recorder, get data, and stop recorder */
void recordData(MPIRecorder         recorder,
                long                numRecords,
                MPIRecorderRecord  *records)
{
    long returnValue;
    long recordIndex;
    long recordCountRemaining;
    long countGet;

    /* Start recording the axis */
    returnValue =
        mpiRecorderStart(recorder,
                         numRecords);
    msgCHECK(returnValue);

    /* Fill the record buffer */
    for (recordIndex = 0, recordCountRemaining = numRecords;
         recordCountRemaining > 0; ) {

        returnValue =
            mpiRecorderRecordGet(recorder,
                                 recordCountRemaining,
                                 &records[recordIndex],
                                 &countGet);
        msgCHECK(returnValue);

        recordCountRemaining -= countGet;
        recordIndex += countGet;
    }

    /* Stop recording */
    returnValue = mpiRecorderStop(recorder);

    /* In case the recorder has already stopped */
    if (returnValue == MPIRecorderMessageSTOPPED) {
        returnValue = MPIMessageOK;
    }
    msgCHECK(returnValue);
}

/* Print the records */
void printRecords(long                numRecords,
                  MPIRecorderRecord  *records,
                  long                numAxes,
                  long               *axisNumbers) {

    MEIRecorderRecord   *recordPtr;

    long recordIndex;
    long axisIndex;

    /* Output record buffer to screen */
    for (recordIndex = 0; recordIndex < numRecords; recordIndex++) {
        printf("\nrecord[%d]:\n",
                recordIndex);

        /* Cast records to MEIRecorderRecord pointer */
        recordPtr = (MEIRecorderRecord *)&records[recordIndex];

        for (axisIndex = 0; axisIndex < numAxes; axisIndex++) {
            printf("axis %d sample %d\tcommand %d\tactual %d\tdac %.4f\n",
                    axisNumbers[axisIndex],
                    recordPtr->axis[axisIndex].sample,
                    recordPtr->axis[axisIndex].command,
                    recordPtr->axis[axisIndex].actual,
                    recordPtr->axis[axisIndex].dac);
        }
    }
}


int main(int    argc,
         char   *argv[])
{
    MPIControl          control;                        /* motion controller handle */
    MPIControlType      controlType;
    MPIControlAddress   controlAddress;
    MPIAxis             axis[AXIS_COUNT];               /* axis handle */
    MPIRecorder         recorder;                       /* data recorder handle */
    MPIRecorderRecord   records[RECORD_COUNT];
    long                axisNumbers[AXIS_COUNT] = {0};
    long                period                  = 0;    /* every sample */
    long                returnValue;                    /* return value from library */


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

    /* Create and initialize MPI objects */
    programInit(&control,
                controlType,
                &controlAddress,
                axis,
                AXIS_COUNT,
                axisNumbers,
                &recorder);

    /* Configure recorder object */
    returnValue =
        mpiRecorderRecordConfig(recorder,
                                (MPIRecorderRecordType) MEIRecorderRecordTypeAXIS,
                                AXIS_COUNT,
                                axis);
    msgCHECK(returnValue);

    /* Start recorder, get data, and stop recorder */
    recordData(recorder,
               RECORD_COUNT,
               records);

    /* Print the records */
    printRecords(RECORD_COUNT,
                 records,
                 AXIS_COUNT,
                 axisNumbers);

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

    return ((int)returnValue);
}


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