.

     

MPI Application Template
template.c
 

seqrec.c -- Track status of specified motion supervisors and enable data recorder on any motion.
/* seqrec.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.
 */
/*

:Track status of specified motion supervisors and enable data recorder on any motion.

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"
#define  RECORDER_NUMBER (0)
#if defined(ARG_MAIN_RENAME)
#define main    seqrecMain

argMainRENAME(main, seqrec)
#endif

/* Command line arguments and defaults */
long    sequenceNumber  = 0;

Arg argList[] = {
    {   "-sequence",    ArgTypeLONG,    &sequenceNumber,    },

    {   NULL,       ArgTypeINVALID, NULL,   }
};

int
    main(int    argc,
         char   *argv[])
{
    MPIControl  control;    /* motion controller handle */
    MPISequence sequence;   /* sequence handle */

    MPICommand  command;

    MPIXmpData  *firmware;

    MPICommandParams    params;
   MPIControlConfig  controlConfig;

    long    returnValue;    /* return value from library */
    long    index;

    long            motionCount;
    MPIObjectMap    motionMap;

    long    labelCheckMS;

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

    /* Check for unknown/invalid command line arguments */
    if (sequenceNumber >= MPIXmpMAX_PSs) {
        mpiPlatformConsole("usage: %s %s\n"
                           "\t\t[-sequence # (0 .. %d)]\n"
                           "\t\t[motion# (0) ...]\n",
                            argv[0],
                            ArgUSAGE,
                            MPIXmpMAX_PSs - 1);
        exit(MPIMessageARG_INVALID);
    }

    mpiObjectMapCLEAR(motionMap);

    /* Create motion map */
    if (argIndex >= argc) {
        motionCount = 1;
        mpiObjectMapBitSET(motionMap, 0, 1);    /* MS[0] default */
    }
    else {
        motionCount = 0;

        while (argIndex < argc) {
            char    *arg;
            long    number;

            arg = argv[argIndex++];

            number = mpiPlatformAtol(arg);

            if ((number >= 0) &&
                (number < MPIXmpMAX_MSs)) {
                motionCount++;
                mpiObjectMapBitSET(motionMap, number, 1);
            }
            else {
                fprintf(stderr,
                        "%s: invalid motion number\n",
                        arg);
                exit(1);
            }
        }
    }

    if (argIndex < argc) {
        mpiPlatformConsole("usage: %s %s\n"
                           "\t\t[-sequence # (0 .. %d)]\n"
                           "\t\t[motion# (0) ...]\n",
                            argv[0],
                            ArgUSAGE,
                            MPIXmpMAX_PSs - 1);
        exit(MPIMessageARG_INVALID);
    }

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

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

   /* Enable a sequence engine on the controller */
   returnValue =
      mpiControlConfigGet(control,
                     &controlConfig,
                     NULL);
   msgCHECK(returnValue);

   if (controlConfig.sequenceCount <= sequenceNumber) {
      controlConfig.sequenceCount = (sequenceNumber + 1);
   }

   returnValue =
      mpiControlConfigSet(control,
                     &controlConfig,
                     NULL);
   msgCHECK(returnValue);

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

    /* Create Sequence */
    sequence =
        mpiSequenceCreate(control,
                          sequenceNumber,
                          2 + motionCount + 2); /* sequence size */
    msgCHECK(mpiSequenceValidate(sequence));

    /* Create Commands */

    /* Branch to "CheckMS" if Recorder is ON (!= 0) */
    params.branch.label             = "CheckMS";
    params.branch.expr.address.l    = &firmware->Recorder[RECORDER_NUMBER].Enable;
    params.branch.expr.oper         = MPICommandOperatorNOT_EQUAL;
    params.branch.expr.by.value.l   = 0;

    command =
        mpiCommandCreate(MPICommandTypeBRANCH,
                         &params,
                         "RecorderON");
    msgCHECK(mpiCommandValidate(command));

    returnValue =
        mpiSequenceCommandAppend(sequence,
                                 command);
    msgCHECK(returnValue);

    /* Turn Recorder ON */
    params.assign.dst.l     = &firmware->Recorder[RECORDER_NUMBER].Enable;
    params.assign.value.l   = -1;   /* continuous, else # records */

    command =
        mpiCommandCreate(MPICommandTypeASSIGN,
                         &params,
                         NULL);
    msgCHECK(mpiCommandValidate(command));

    returnValue =
        mpiSequenceCommandAppend(sequence,
                                 command);
    msgCHECK(returnValue);

    labelCheckMS = FALSE;

    for (index = 0; index < MPIXmpMAX_MSs; index++) {
        if (mpiObjectMapBitGET(motionMap, index) == 0) {
            continue;
        }

        /* Branch to "RecorderOn" if motion detected */
        params.branch.label             = "RecorderON";
        params.branch.expr.address.l    = (long *)&firmware->MS[index].Status;
        params.branch.expr.oper         = MPICommandOperatorBIT_CLEAR;
        params.branch.expr.by.value.l   = (0x1 << MPIXmpEventDONE);

        command =
            mpiCommandCreate(MPICommandTypeBRANCH,
                             &params,
                             (labelCheckMS == FALSE) ? "CheckMS" : NULL);
        msgCHECK(mpiCommandValidate(command));

        labelCheckMS = TRUE;

        returnValue =
            mpiSequenceCommandAppend(sequence,
                                     command);
        msgCHECK(returnValue);
    }

    /* Turn Recorder OFF */
    params.assign.dst.l     = &firmware->Recorder[RECORDER_NUMBER].Enable;
    params.assign.value.l   = 0;

    command =
        mpiCommandCreate(MPICommandTypeASSIGN,
                         &params,
                         NULL);
    msgCHECK(mpiCommandValidate(command));

    returnValue =
        mpiSequenceCommandAppend(sequence,
                                 command);
    msgCHECK(returnValue);

    /* Branch to "CheckMS" */
    params.branch.label     = "CheckMS";
    params.branch.expr.oper = MPICommandOperatorALWAYS;

    command =
        mpiCommandCreate(MPICommandTypeBRANCH,
                         &params,
                         NULL);
    msgCHECK(mpiCommandValidate(command));

    returnValue =
        mpiSequenceCommandAppend(sequence,
                                 command);
    msgCHECK(returnValue);

    /* Start sequence */
    returnValue =
        mpiSequenceStart(sequence,
                         MPIHandleVOID);

    if (returnValue != MPIMessageOK) {
        fprintf(stderr, "%s: mpiSequenceStart() returns 0x%x: %s\n",
                        argv[0],
                        returnValue,
                        mpiMessage(returnValue, NULL));
        exit(returnValue);
    }

    mpiPlatformConsole("Press any key to stop sequence\n");
    mpiPlatformKey(MPIWaitFOREVER);

    /* Stop sequence */
    returnValue = mpiSequenceStop(sequence);
    msgCHECK(returnValue);

    returnValue = mpiSequenceDelete(sequence);
    msgCHECK(returnValue);

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

    return ((int)returnValue);
}


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