.

     

MPI Application Template
template.c
 

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

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)
{
    long            returnValue;

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

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

/* Perform certain cleanup actions and delete MPI objects */
void programCleanup(MPIControl *control)
{
    long    returnValue;

    /* Delete control handle */
    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("\nError %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;
}

int main(int     argc,
         char   *argv[])
{
    MPIControl                control;
    MPIControlType            controlType;
    MPIControlAddress         controlAddress;
    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);

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

    /* Wait for user to press key */
    fprintf(stderr, "Press any key to exit...\n\n");
    while(meiPlatformKey(MPIWaitPOLL) <= 0) {
        meiPlatformSleep(100);
    }

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

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

    return MPIMessageOK;
}


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