.

     

MPI Application Template
template.c
 

hostService2.c -- Configure board to interrupt host every N samples.
/* hostService2.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.
 */

/*

:Configure board to interrupt host every N samples.

This program starts a hostService thread which configures the board to
 interrupt the host every N (IRQ_PERIOD) samples and runs a user defined
 hostServiceFunction when the interrupt is detected.

The hostService object is created with enableWatchdog == TRUE which will will
 configure the control object to thow an event
 (MEIEventTypeCONTROL_HOST_PROCESS_TIME_EXCEEDED) if the firmware starts SynqNet
 transmission before hostServiceFunction completes.

The watchdog feature should be used if the host is trying to (for example)
 process feedback and calculate a new DAC command -- this will require a hard
 realtime operating system.  enableWatchdog should only be defined if the
 IRQ_PERIOD is 1 (interrupt every sample) to avoid a MPIMessageARG_INVALID
 return code.

NOTE: Attempting to use an IRQ_PERIOD of 1 will only work succesfully for
 real-time operating systems.  Non-real-time operating systems will have
 trouble calling the host service function consistently unless the
 interrupt period (= [controller sample rate] * IRQ_PERIOD) is
 10 milliseconds or more.

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 IRQ_PERIOD              (1)     /* interruptPeriod */
#define ENABLE_WATCHDOG         (TRUE)
#define HOST_SERVICE_PRIORITY   (-1)    /* max priority */

typedef struct HostServiceFunctionParams {
    MPIControl       control;
    MEIXmpData       *firmware;
    long             firstPass;
} HostServiceFunctionParams;

HostServiceFunctionParams  params;


/* 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,
                 MPIEventMgr        *eventMgr,
                 MPINotify          *notify,
                 Service            *service)
{
    MPIEventMask    eventMask;
    long            returnValue;


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

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

    /* Create event manager object */
    *eventMgr =
        mpiEventMgrCreate(*control);
    msgCHECK(mpiEventMgrValidate(*eventMgr));

    /* Request notification of control events */
    mpiEventMaskCLEAR(eventMask);
    meiEventMaskCONTROL(eventMask);

    returnValue = mpiControlEventReset(*control,
                                       eventMask);
    msgCHECK(returnValue);

    returnValue =
        mpiControlEventNotifySet(*control,
                                 eventMask,
                                 NULL);
    msgCHECK(returnValue);

    /* Create event notification object for controller */
    *notify =
        mpiNotifyCreate(eventMask, *control);
    msgCHECK(mpiNotifyValidate(*notify));

    /* Append notify to event manager */
    returnValue =
        mpiEventMgrNotifyAppend(*eventMgr, *notify);
    msgCHECK(returnValue);

    /* Create service thread */
    *service =
        serviceCreate(*eventMgr,
                      -1,   /* default (max) priority */
                      -1);  /* -1 => enable interrupts */
    if (*service == NULL)
    {
        msgCHECK(MPIMessageHANDLE_INVALID);
    }
}

/* Perform certain cleanup actions and delete MPI objects */
void programCleanup(MPIControl  *control,
                    MPIEventMgr *eventMgr,
                    MPINotify   *notify,
                    Service     *service)
{
    long            returnValue;


    /* Delete service thread */
    returnValue =
        serviceDelete(*service);
    msgCHECK(returnValue);
    *service = MPIHandleVOID;

    /* Delete event manager object */
    returnValue =
        mpiEventMgrDelete(*eventMgr);
    msgCHECK(returnValue);
    *eventMgr = MPIHandleVOID;

    /* Delete event notification object */
    returnValue =
        mpiNotifyDelete(*notify);
    msgCHECK(returnValue);
    *notify = MPIHandleVOID;

    /* Delete motion controller object */
    returnValue =
        mpiControlDelete(*control);
    msgCHECK(returnValue);
    *control = MPIHandleVOID;
}

/*
 * Function called by the host service object.
 *
 * This function will print an error message if it is
 * not called every IRQ_PERIOD controller samples.
 */
long hostServiceFunction(HostServiceFunctionParams *params)
{
    long returnValue;
    long sampleCounter;

    static long oldSampleCounter;

    returnValue =
        mpiControlMemoryGet(params->control,
                            &sampleCounter,
                            &params->firmware->SystemData.SampleCounter,
                            sizeof(sampleCounter));

    if (params->firstPass == TRUE) {
        oldSampleCounter  = sampleCounter;
        params->firstPass = FALSE;
        return (returnValue);
    }

    if (returnValue == MPIMessageOK) {
        printf("SampleCounter: %d\r", sampleCounter);

        if ((sampleCounter - IRQ_PERIOD) != oldSampleCounter) {
            printf("Error %d samples\n"
                   "sampleCounter %d\n"
                   "oldSampleCounter %d\n"
                   "period %d\n\n",
                   (sampleCounter - oldSampleCounter),
                   sampleCounter,
                   oldSampleCounter,
                   IRQ_PERIOD);
        }

        oldSampleCounter = sampleCounter;
    }

    return returnValue;
}

/* Create and start host service object */
void initHostService(MPIControl   control,
                     HostService* hostService,
                     MPI_BOOL     enableWatchdog,
                     long         hostServicePriority)
{
    long                      returnValue;

    /* initialize the host service function parameters */
    returnValue =
        mpiControlMemory(control, &(params.firmware), NULL);
    msgCHECK(returnValue);

    params.control = control;
    params.firstPass = TRUE;

    /* start host service thread */
    *hostService =
        hostServiceCreate(control,
                          hostServiceFunction,
                          (void*)&params,
                          IRQ_PERIOD,
                          enableWatchdog,
                          hostServicePriority);

    if ((*hostService) == NULL) {
        printf("\nERROR: verify that interruptPeriod == 1 "
                           "if enableWatchdog == TRUE\n");
    }

   /* enable the hostService */
   returnValue =
      hostServiceEnable(*hostService,
                    TRUE);
   msgCHECK(returnValue);
}

/* Delete host service object */
void cleanupHostService(HostService* hostService)
{
    long returnValue = hostServiceDelete(*hostService);
    msgCHECK(returnValue);
    *hostService = MPIHandleVOID;
}

/* Check for "Process Time Exceeded" event until user presses a key to exit */
void checkForProcessTimeExceededEvent(MPIControl control, MPINotify notify)
{
    MPIEventMask   eventMask;
    MPIEventStatus eventStatus;
    long           returnValue;

    mpiEventMaskCLEAR(eventMask);
    meiEventMaskCONTROL(eventMask);

    fprintf(stderr, "Press any key to exit...\n");

    while(meiPlatformKey(MPIWaitPOLL) <= 0) {

        /* Wait for motion event */
        returnValue =
            mpiNotifyEventWait(notify,
                               &eventStatus,
                               1000);

        if (returnValue == MPIMessageOK) {
            if (eventStatus.type == MEIEventTypeCONTROL_HOST_PROCESS_TIME_EXCEEDED) {

                printf("Host Process Time Exceeded\n");
                printf("mpiNotifyEventWait(0x%x, 0x%x, %d) returns 0x%x\n"
                        "\teventStatus: type %d source 0x%x info 0x%x\n",
                        notify,
                        &eventStatus,
                        MPIWaitFOREVER,
                        returnValue,
                        eventStatus.type,
                        eventStatus.source,
                        eventStatus.info[0]);

                returnValue = mpiControlEventReset(control,
                                                   eventMask);
                msgCHECK(returnValue);
            }
        }
        if (returnValue != MPIMessageTIMEOUT) {
            msgCHECK(returnValue);
        }
    }
}

int main(int     argc,
         char   *argv[])
{
    MPIControl          control;
    MPIControlType      controlType;
    MPIControlAddress   controlAddress;
    MPINotify           notify;         /* event notification object */
    MPIEventMgr         eventMgr;       /* event manager handle */
    Service             service;
    HostService         hostService;
    MPI_BOOL            enableWatchdog = ENABLE_WATCHDOG;
    long                hostServicePriority = HOST_SERVICE_PRIORITY;

    /* Perform basic command line parsing. (-control -server -port -trace) */
    basicParsing(argc,
                 argv,
                 &controlType,
                 &controlAddress);

    /* Create and initialize MPI objects */
    programInit(&control,
                controlType,
                &controlAddress,
                &eventMgr,
                &notify,
                &service);

    /* Create and start host service object */
    printf("Starting hostservice thread...\n");
    initHostService(control, &hostService, enableWatchdog, hostServicePriority);

    /* Check for "Process Time Exceeded" event until user presses a key to exit */
    checkForProcessTimeExceededEvent(control, notify);

    /* Delete host service object */
    cleanupHostService(&hostService);
    printf("\n\ndone.\n\n");

    /* Perform certain cleanup actions and delete MPI objects */
    programCleanup(&control,
                   &eventMgr,
                   &notify,
                   &service);

    return MPIMessageOK;
}


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