record1.c -- Read/display data recorder records from specified axis (default 0)
/* record1.c */

/* Copyright(c) 1991-2002 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.
 */

#if defined(MEI_RCS)
static const char MEIAppRCS[] =
    "$Header: /MainTree/XMPLib/XMP/app/record1.c 8     7/23/01 2:36p Kevinh $";
#endif

/*

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

Warning!  This is a sample program to assist in the integration of the
 XMP 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    record1Main

argMainRENAME(main, record1)
#endif

#define AXIS_COUNT      (1)
#define RECORD_COUNT    (100)

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

Arg argList[] = {
    {   "-axis",    ArgTypeLONG,    &axisNumber,    },
    {   "-period",  ArgTypeLONG,    &period,        },

    {   NULL,       ArgTypeINVALID, NULL,   }
};

int
    main(int    argc,
         char   *argv[])
{
    MPIControl  control;            /* motion controller handle */
    MPIAxis     axis[AXIS_COUNT];   /* axis handle */
    MPIRecorder recorder;           /* data recorder handle */

    MPIRecorderRecord   records[RECORD_COUNT];
    MEIRecorderRecord   *recordPtr;

    MPIControlType      controlType;
    MPIControlAddress   controlAddress;

    long    recordCountRemaining;
    long    recordIndex;

    long    returnValue;    /* return value from library */

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

    /* Check that the axis number is valid */
    meiASSERT((axisNumber >= 0) &&
              (axisNumber < MEIXmpMAX_Axes));

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

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

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

    /* Create axis object */
    axis[0] =
        mpiAxisCreate(control,
                      axisNumber);
    msgCHECK(mpiAxisValidate(axis[0]));

    /* Create and configure recorder object */
    recorder =
        mpiRecorderCreate(control);
    msgCHECK(mpiRecorderValidate(recorder));

    returnValue =
        mpiRecorderRecordConfig(recorder,
                                (MPIRecorderRecordType)MEIRecorderRecordTypeAXIS,
                                AXIS_COUNT,
                                axis);
    msgCHECK(returnValue);

    /* Start recording the axis */
    returnValue =
        mpiRecorderStart(recorder,
                         RECORD_COUNT,
                         period,    /* period (milliseconds) */
                         0);
    msgCHECK(returnValue);

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

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

        recordCountRemaining -= countGet;
        recordIndex += countGet;
    }

    /* Stop recording */
    returnValue = mpiRecorderStop(recorder);
    if (returnValue == MPIRecorderMessageSTOPPED) {
        returnValue = MPIMessageOK;
    }
    msgCHECK(returnValue);

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

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

        printf("axis %d sample %d\tcommand %d\tactual %d\tdac %.4f\n",
                axisNumber,
                recordPtr->axis[0].sample,
                recordPtr->axis[0].command,
                recordPtr->axis[0].actual,
                recordPtr->axis[0].dac);
    }

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

    returnValue =
        mpiAxisDelete(axis[0]);
    msgCHECK(returnValue);

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

    return ((int)returnValue);
}