record3.c -- Interrupt-driven display of data recorder records from
specified axis (default 0)
/* record3.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.
*/
/*
:Interrupt-driven display of 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_NUMBER (0) /* Default axis to record */
#define PERIOD (0) /* milliseconds (0 means every servo cycle) */
#define RECORD_COUNT (2000) /* Number of samples to record */
/*
Recorder will report full when HIGH_COUNT records have been recorded
If HIGH_COUNT < 0, then programInit() will configure HIGH_COUNT to equal
the maximum number of possible records.
*/
#define HIGH_COUNT (1000)
/* 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 axisNumber,
MPIRecorder *recorder,
MPIRecorderRecord **records,
long recordCount,
long *highCount,
MPINotify *notify,
MPIEventMgr *eventMgr,
Service *service)
{
MPIEventMask eventMask;
MPIRecorderStatus recorderStatus;
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 recorder object */
*recorder =
mpiRecorderCreate(*control, -1);
msgCHECK(mpiRecorderValidate(*recorder));
/* Request notification of all events from recorder */
mpiEventMaskCLEAR(eventMask);
mpiEventMaskRECORDER(eventMask); /* macro */
returnValue =
mpiRecorderEventNotifySet(*recorder,
eventMask,
NULL);
msgCHECK(returnValue);
/* Create event notification object for recorder */
*notify =
mpiNotifyCreate(eventMask,
*recorder);
msgCHECK(mpiNotifyValidate(*notify));
/* Create event manager object */
*eventMgr =
mpiEventMgrCreate(*control);
msgCHECK(mpiEventMgrValidate(*eventMgr));
/* Add notify to event manager's list */
returnValue =
mpiEventMgrNotifyAppend(*eventMgr,
*notify);
msgCHECK(returnValue);
/* Create service thread */
*service =
serviceCreate(*eventMgr,
-1, /* default (max) priority */
-1); /* -1 => enable interrupts */
meiAssert(*service != NULL);
returnValue =
mpiRecorderStatus(*recorder,
&recorderStatus,
NULL);
msgCHECK(returnValue);
if (*highCount < 0) {
*highCount =
recorderStatus.recordCountMax -
(recorderStatus.recordCountMax / 4);
}
/* Allocate memory for record buffer */
*records =
(MPIRecorderRecord *)meiPlatformAlloc(sizeof(MPIRecorderRecord) *
recordCount);
meiAssert(*records != NULL);
}
/* Perform certain cleanup actions and delete MPI objects */
void programCleanup(MPIControl *control,
MPIAxis *axis,
MPIRecorder *recorder,
MPIRecorderRecord *records,
long recordCount,
MPIEventMgr *eventMgr,
MPINotify *notify,
Service *service)
{
long returnValue;
/* Free recorder buffer */
returnValue =
meiPlatformFree(records,
(sizeof(MPIRecorderRecord) * recordCount));
meiAssert(returnValue == MPIMessageOK);
/* Delete service */
returnValue =
serviceDelete(*service);
msgCHECK(returnValue);
*service = MPIHandleVOID;
/* Delete eventMgr */
returnValue =
mpiEventMgrDelete(*eventMgr);
msgCHECK(returnValue);
*eventMgr = MPIHandleVOID;
/* Delete notify */
returnValue =
mpiNotifyDelete(*notify);
msgCHECK(returnValue);
*notify = MPIHandleVOID;
/* Delete recorder */
returnValue =
mpiRecorderDelete(*recorder);
msgCHECK(returnValue);
*recorder = MPIHandleVOID;
/* Delete axis */
returnValue =
mpiAxisDelete(*axis);
msgCHECK(returnValue);
*axis = MPIHandleVOID;
/* Delete control */
returnValue =
mpiControlDelete(*control);
msgCHECK(returnValue);
*control = MPIHandleVOID;
}
/* Start recorder, get data, and stop recorder */
void recordData(MPIRecorder recorder,
MPIRecorderRecord *records,
long recordCount,
MPINotify notify)
{
MEIRecorderRecord *recordPtr;
long recordingDone;
long returnValue;
/* Start Recorder and wait for Event */
returnValue =
mpiRecorderStart(recorder,
recordCount);
msgCHECK(returnValue);
recordPtr = (MEIRecorderRecord *)records;
recordingDone = FALSE;
while (recordingDone == FALSE) {
MPIEventStatus eventStatus;
/* Wait for recorder events */
returnValue =
mpiNotifyEventWait(notify,
&eventStatus,
MPIWaitFOREVER);
msgCHECK(returnValue);
switch (eventStatus.type) {
case MPIEventTypeRECORDER_HIGH:
case MPIEventTypeRECORDER_FULL:
case MPIEventTypeRECORDER_DONE: {
long countGet;
returnValue =
mpiRecorderRecordGet(recorder,
recordCount,
(MPIRecorderRecord *)recordPtr,
&countGet);
msgCHECK(returnValue);
recordPtr += countGet;
printf("event: %d\n",eventStatus.type);
if (eventStatus.type == MPIEventTypeRECORDER_DONE) {
recordingDone = TRUE;
}
break;
}
default: {
meiAssert(FALSE);
break;
}
}
}
/* Stop recording */
returnValue = mpiRecorderStop(recorder);
/* In case the recorder has already stopped */
if (returnValue == MPIRecorderMessageSTOPPED) {
returnValue = MPIMessageOK;
}
msgCHECK(returnValue);
}
/* Print the records */
void printRecords(MPIRecorderRecord *records,
long recordCount,
long axisNumber)
{
MEIRecorderRecord *recordPtr;
long recordIndex;
/* Ouput all records to console */
for (recordIndex = 0,
recordPtr = (MEIRecorderRecord *)records;
recordIndex < recordCount;
recordIndex++,
recordPtr++) {
printf("\nrecord[%d]:\n",
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);
}
}
int main(int argc,
char *argv[])
{
MPIControl control; /* motion controller handle */
MPIControlType controlType;
MPIControlAddress controlAddress;
MPIAxis axis; /* axis handle */
MPIRecorder recorder; /* data recorder handle */
MPIRecorderConfig recorderConfig;
MPIRecorderRecord *records;
MPINotify notify; /* event notification handle */
MPIEventMgr eventMgr; /* event manager handle */
Service service; /* service thread */
long axisNumber = AXIS_NUMBER; /* axis number */
long period = PERIOD; /* every sample */
long recordCount = RECORD_COUNT;
long highCount = HIGH_COUNT;
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,
axisNumber,
&recorder,
&records,
recordCount,
&highCount,
¬ify,
&eventMgr,
&service);
/* Configure recorder object */
returnValue =
mpiRecorderRecordConfig(recorder,
(MPIRecorderRecordType)MEIRecorderRecordTypeAXIS,
1,
&axis);
msgCHECK(returnValue);
returnValue =
mpiRecorderConfigGet(recorder,
&recorderConfig,
NULL);
msgCHECK(returnValue);
recorderConfig.period = PERIOD;
recorderConfig.highCount = HIGH_COUNT;
returnValue =
mpiRecorderConfigSet(recorder,
&recorderConfig,
NULL);
msgCHECK(returnValue);
/* Start recorder, get data, and stop recorder */
recordData(recorder,
records,
recordCount,
notify);
/* Print the records */
printRecords(records,
recordCount,
axisNumber);
/* Perform certain cleanup actions and delete MPI objects */
programCleanup(&control,
&axis,
&recorder,
records,
recordCount,
&eventMgr,
¬ify,
&service);
return ((int)returnValue);
}
|