.

     

MPI Application Template
template.c
 

record2.c -- Record Data and Demonstrate How to Start/Stop/Restart Recorder
/* record2.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.
 */

/*

:Record Data and Demonstrate How to Start/Stop/Restart Recorder

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.

*/

#include <stdlib.h>
#include <stdio.h>

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

#include "apputil.h"

#if defined(ARG_MAIN_RENAME)
#define main    record2Main

argMainRENAME(main, record2)
#endif

/* Command line arguments and defaults */
long    period      = 0;    /* every sample */
long    recordCount = 100;

Arg argList[] = {
    {   "-period",  ArgTypeLONG,    &period,        },
    {   "-records", ArgTypeLONG,    &recordCount,   },

    {   NULL,       ArgTypeINVALID, NULL,   }
};

long
    recorderMode(MPIRecorder    recorder,
                 long           mode,
                 long           recordCount);

int
    main(int    argc,
         char   *argv[])
{
    MPIControl       control;    /* motion controller handle */
    MPIRecorder         recorder;      /* data recorder handle */
   MPIRecorderConfig recorderConfig;
    MEIXmpData       *firmware;

    long    axisCount;
    long    axisNumber[MEIXmpMAX_Axes]; /* axis numbers */

    long    pointCount;
    void    *point[MEIXmpMaxRecSize];

    MPIRecorderRecord   *record;
    register MPIRecorderRecord  *recordPtr;

    long    returnValue;    /* return value from library */

    long    index;

    long    recordCountRemaining;
    long    recordIndex;
    long    recordsRead;

    long    mode;

    MPIControlType      controlType;
    MPIControlAddress   controlAddress;

    long    argIndex;

    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;
        }
    }

    if (argIndex >= argc) {
        axisCount       = 1;
        axisNumber[0]   = 0;
    }
    else {
        axisCount = argc - argIndex;

        if (axisCount > MEIXmpMAX_Axes) {
            axisCount = MEIXmpMAX_Axes;
        }

        for (index = 0; index < axisCount; index++) {
            axisNumber[index] = meiPlatformAtol(argv[argIndex++]);
            meiAssert((axisNumber[index] >= 0) &&
                      (axisNumber[index] < MEIXmpMAX_Axes));
        }
    }

    if (argIndex < argc) {
        meiPlatformConsole("usage: %s %s\n"
                           "\t\t[-period (%d)]\n"
                           "\t\t[-records (%d)]\n"
                           "\t\t[axisNumber ...]\n",
                           argv[0],
                           ArgUSAGE,
                           period,
                           recordCount);
        exit(MPIMessageARG_INVALID);
    }

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

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

    returnValue =
        mpiControlMemory(control,
                         (void **)&firmware,
                         NULL);
    msgCHECK(returnValue);

    pointCount = 0;

    point[pointCount++] = &firmware->SystemData.SampleCounter;

    for (index = 0; index < axisCount; index++) {
        point[pointCount++] = &firmware->MS[axisNumber[index]].Status;
        point[pointCount++] = &firmware->Axis[axisNumber[index]].Status;
        meiAssert(pointCount <= MEIXmpMaxRecSize);
    }

    record =
        (MPIRecorderRecord *)meiPlatformAlloc(sizeof(*record) *
                                              recordCount);
    meiAssert(record != NULL);

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

    returnValue =
        mpiRecorderRecordConfig(recorder,
                                MPIRecorderRecordTypePOINT,
                                pointCount,
                                point);
    msgCHECK(returnValue);

   returnValue =
      mpiRecorderConfigGet(recorder,
                      &recorderConfig,
                      NULL);
    msgCHECK(returnValue);

   recorderConfig.period = period;

   returnValue =
      mpiRecorderConfigSet(recorder,
                      &recorderConfig,
                      NULL);
    msgCHECK(returnValue);


    recordPtr = record;

    mode = 0;

    meiPlatformConsole("Press any key to start recording, ESC to quit\n");

    recordCountRemaining = recordCount;

    while (recordCountRemaining > 0) {
        mode =
            recorderMode(recorder,
                         mode,
                         recordCountRemaining);

        if (mode > 0) {
            long    countGet;

            returnValue =
                mpiRecorderRecordGet(recorder,
                                     recordCountRemaining,
                                     recordPtr,
                                     &countGet);
            msgCHECK(returnValue);

            recordCountRemaining -= countGet;
            recordPtr += countGet;
        }
        else if (mode < 0) {
            break;
        }
    }

    recordsRead = recordPtr - record;

    for (recordIndex = 0,
            recordPtr = record;
         recordIndex < recordsRead;
         recordIndex++,
             recordPtr++) {
        long    pointIndex;

        pointIndex = 0;

        printf("record[%d]: sample %d\n",
                recordIndex,
                recordPtr->point[pointIndex++]);

        for (index = 0; index < axisCount; index++) {
            printf("%d: motion status 0x%x axis status 0x%x\n",
                    axisNumber[index],
                    recordPtr->point[pointIndex],
                    recordPtr->point[pointIndex + 1]);

            pointIndex += 2;
        }
        meiAssert(pointIndex == pointCount);
    }

    returnValue = mpiRecorderDelete(recorder);
    msgCHECK(returnValue);

    returnValue =
        meiPlatformFree(record,
                        (sizeof(*record) * recordCount));
    msgCHECK(returnValue);

    returnValue = mpiControlDelete(control);
    msgCHECK(returnValue);

    return ((int)returnValue);
}

long
    recorderMode(MPIRecorder    recorder,
                 long           mode,
                 long           recordCount)
{
    long    returnValue;

    long    modeNew;

    long    key;

    key = meiPlatformKey(MPIWaitPOLL);

    if (key <= 0) {
        if (key == 0) {
            key = meiPlatformKey(MPIWaitPOLL);
        }
        modeNew = mode;
    }
    else if (key == 0x1b) {
        modeNew = -1;
    }
    else {
        modeNew = (mode <= 0) ? 1 : 0;
    }

    if (modeNew != mode) {
        if (modeNew <= 0) {
            if (mode > 0) {
                returnValue =
                    mpiRecorderStop(recorder);
                msgCHECK(returnValue);
            }
        }
        else {
            returnValue =
                mpiRecorderStart(recorder,
                                 recordCount);
            msgCHECK(returnValue);
        }

        if (modeNew >= 0) {
            meiPlatformConsole("Press any key to %s recording, ESC to quit\n",
                                (modeNew == 0) ? "start" : "stop ");
        }
    }

    return (modeNew);
}


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