.

     

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;
    MPIXmpData       *firmware;

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

    long    pointCount;
    void    *point[MPIXmpMaxRecSize];

    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 > MPIXmpMAX_Axes) {
            axisCount = MPIXmpMAX_Axes;
        }

        for (index = 0; index < axisCount; index++) {
            axisNumber[index] = mpiPlatformAtol(argv[argIndex++]);
            mpiAssert((axisNumber[index] >= 0) &&
                      (axisNumber[index] < MPIXmpMAX_Axes));
        }
    }

    if (argIndex < argc) {
        mpiPlatformConsole("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;
        mpiAssert(pointCount <= MPIXmpMaxRecSize);
    }

    record =
        (MPIRecorderRecord *)mpiPlatformAlloc(sizeof(*record) *
                                              recordCount);
    mpiAssert(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;

    mpiPlatformConsole("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;
        }
        mpiAssert(pointIndex == pointCount);
    }

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

    returnValue =
        mpiPlatformFree(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 = mpiPlatformKey(MPIWaitPOLL);

    if (key <= 0) {
        if (key == 0) {
            key = mpiPlatformKey(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) {
            mpiPlatformConsole("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