limitSw1.c -- Configure positive and negative hardware limit inputs.
/* limitsw1.c */

/* Copyright(c) 1991-2001 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/limitsw1.c 5     7/18/01 9:32a Kevinh $";
#endif

#if defined(ARG_MAIN_RENAME)
#define main    limitsw1Main
argMainRENAME(main, limitsw1)
#endif

/*

:Configure positive and negative hardware limit inputs.

A motor's positive and negative hardware limit inputs have four configurations:

  1) Event Action   (any MPIAction such as MPIActionE_STOP)
  2) Event Trigger  (a trigger polarity, active HIGH or LOW)
  3) Direction Flag (TRUE will cause the command direction of motion to
                        qualify the events, FALSE will ignore direction,
                        based solely on the limit input state)
  4) Duration       (requires the limit condition to exist for
                        a programmable number of seconds before an
                        event will occur)

Events are configured using MPIMotorEventConfigGet/Set(...).

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"


#define MOTOR           (0)
#define EVENT_DURATION  (0.010)     /* seconds */


/* 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);
    }
}


void limitConfigure(MPIMotor        motor,
                    MPIEventType    eventType,
                    MPIAction       action,
                    long            polarity,
                    long            direction,
                    double          duration)
{
    MPIMotorEventConfig eventConfig;
    long                returnValue;


    /* Get the current limit configuration */
    returnValue =
        mpiMotorEventConfigGet(motor,
                               eventType,
                               &eventConfig,
                               NULL);
    msgCHECK(returnValue);

    /* Set trigger level to Active Low (FALSE) or Active High (TRUE) */
    eventConfig.trigger.polarity = polarity;

    /* Use commanded velocity direction as a trigger qualifier. (TRUE/FALSE) */
    eventConfig.direction = direction;

    /* Wait duration seconds before triggering */
    eventConfig.duration = (float) duration;

    /* Configure motor to perform action upon limit */
    eventConfig.action = action;

    /* Set the new limit configuration */
    returnValue =
        mpiMotorEventConfigSet(motor,
                               eventType,
                               &eventConfig,
                               NULL);
    msgCHECK(returnValue);
}


/* Create and initialize MPI objects */
void programInit(MPIControl         *control,
                 MPIControlType      controlType,
                 MPIControlAddress  *controlAddress,
                 MPIMotor           *motor,
                 long                motorNumber)
{
    long returnValue;


    /* Obtain a Control handle */
    *control =
        mpiControlCreate(controlType,
                         controlAddress);
    msgCHECK(mpiControlValidate(*control));

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

    /* Obtain a Motor handle */
    *motor =
        mpiMotorCreate(*control,
                       motorNumber);
    msgCHECK(mpiMotorValidate(*motor));
}


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

    /* Delete motor object */
    returnValue =
        mpiMotorDelete(*motor);
    msgCHECK(returnValue);

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


int main(int    argc,
         char   *argv[])
{
    MPIControl          control;
    MPIMotor            motor;
    MPIControlAddress   controlAddress;
    MPIControlType      controlType;


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

    /* Create and initialize MPI objects */
    programInit(&control,
                controlType,
                &controlAddress,
                &motor,
                MOTOR);

    /* Change the LIMIT_HW_POS configuration */
    limitConfigure(motor,
                   MPIEventTypeLIMIT_HW_POS,    /* +HW limit        */
                   MPIActionE_STOP,             /* E-STOP on limit  */
                   TRUE,                        /* Active High      */
                   TRUE,     /* Use velocity as a trigger qualifier */
                   EVENT_DURATION);

    /* Change the LIMIT_HW_POS configuration */
    limitConfigure(motor,
                   MPIEventTypeLIMIT_HW_NEG,    /* -HW limit        */
                   MPIActionE_STOP,             /* E-STOP on limit  */
                   TRUE,                        /* Active High      */
                   TRUE,     /* Use velocity as a trigger qualifier */
                   EVENT_DURATION);

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

    return MPIMessageOK;
}