ampFlt1.c -- Configure the amp fault input.
/* ampflt1.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 the amp fault input.
The program will configure the amp fault input for a motor.
A motor's amp fault input has four items to configure:
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)
The duration configuration provides filtering of the amp fault input by requiring the input to
remain active for the defined duration. This will effectively keep the amp fault from activating prematurely
due to electrical noise on the amp fault input.
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 ACTIVE_HIGH (1)
#define ACTIVE_LOW (0)
#if defined(ARG_MAIN_RENAME)
#define main ampflt1Main
argMainRENAME(main, ampflt1)
#endif
/* Command line arguments and defaults */
long axisNumber = 0;
long motionNumber = 0;
long motorNumber = 0;
float ampfaultduration= (float)5.0;
Arg argList[] = {
{ "-axis", ArgTypeLONG, &axisNumber, },
{ "-motion", ArgTypeLONG, &motionNumber, },
{ "-motor", ArgTypeLONG, &motorNumber, },
{ "-duration",ArgTypeFLOAT, &faultduration, },
{ NULL, ArgTypeINVALID, NULL, }
};
int
main(int argc,
char *argv[])
{
MPIControl control; /* motion controller handle */
MPIAxis axis; /* axis handle(s) */
MPIMotor motor; /* motor handle(s) */
MPIMotion motion; /* motion supervisor handle */
MPINotify notify; /* event notification handle */
MPIEventMgr eventMgr; /* event manager handle */
Service service;
MEIXmpData *firmware;
MPIMotorEventConfig eventConfig;
long messageCount;
long returnValue; /* return value from library */
MPIControlType controlType;
MPIControlAddress controlAddress;
MPIEventMask eventMask;
long argIndex;
/* Parse command line for Control type and address */
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 ((argIndex < argc) ||
(axisNumber >= MEIXmpMAX_Axes) ||
(motionNumber >= MEIXmpMAX_MSs) ||
(motorNumber >= MEIXmpMAX_Motors) ||
(ampfaultduration < 0)) {
meiPlatformConsole("usage: %s %s\n"
"\t\t[-axis # (0 .. %d)]\n"
"\t\t[-motion # (0 .. %d)]\n"
"\t\t[-motor # (0 .. %d)]\n"
"\t\t[-duration # (>0)]\n",
argv[0],
ArgUSAGE,
MEIXmpMAX_Axes - 1,
MEIXmpMAX_MSs - 1,
MEIXmpMAX_Motors - 1);
exit(MPIMessageARG_INVALID);
}
/* Obtain a Control handle. */
control =
mpiControlCreate(controlType,
&controlAddress);
msgCHECK(mpiControlValidate(control));
/* Initialize the controller */
returnValue = mpiControlInit(control);
msgCHECK(returnValue);
/* Get pointer to XMP firmware */
returnValue =
mpiControlMemory(control,
(void**)&firmware,
(void**)NULL);
msgCHECK(returnValue);
/* Create axis object using axisNumber on controller */
axis =
mpiAxisCreate(control,
axisNumber);
msgCHECK(mpiAxisValidate(axis));
/* Create motor object using axisNumber on controller */
motor =
mpiMotorCreate(control,
motorNumber);
msgCHECK(mpiMotorValidate(motor));
/* Create motion supervisor object using MS number */
motion =
mpiMotionCreate(control,
motionNumber,
axis);
msgCHECK(mpiMotionValidate(motion));
/* Request notification of all events from motion */
mpiEventMaskCLEAR(eventMask);
mpiEventMaskALL(eventMask);
meiEventMaskALL(eventMask);
returnValue =
mpiMotionEventNotifySet(motion,
eventMask,
NULL);
msgCHECK(returnValue);
/* Create event notification object for motion */
notify =
mpiNotifyCreate(eventMask,
motion);
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);
/* Get configuration for ampfault event */
returnValue =
mpiMotorEventConfigGet(motor,
MPIEventTypeAMP_FAULT,
&eventConfig,
NULL);
msgCHECK(returnValue);
/* Event Action, configure to ABORT on Amp Fault Input */
eventConfig.action = MPIActionABORT;
/* Event trigger, set trigger level to Active Low */
eventConfig.trigger.polarity = ACTIVE_LOW;
/* Direction Flag, do not use commanded velocity direction as a trigger qualifier */
eventConfig.direction = FALSE;
/* Duration, wait ampfaultduration seconds before triggering */
eventConfig.duration = ampfaultduration;
returnValue =
mpiMotorEventConfigSet(motor,
MPIEventTypeAMP_FAULT,
&eventConfig,
NULL);
msgCHECK(returnValue);
meiPlatformConsole("amp fault duration and polarity configured: wait for events.\n");
messageCount = 0;
while (meiPlatformKey(MPIWaitPOLL) <= 0) {
MPIEventStatus eventStatus;
long motorNumber;
/* Wait for event */
returnValue =
mpiNotifyEventWait(notify,
&eventStatus,
MPIWaitFOREVER);
msgCHECK(returnValue);
messageCount++;
motorNumber = ((MEIEventStatusInfo *)eventStatus.info)->type.number;
printf("Event! (%d) Type = %d \n",
messageCount,
eventStatus.type);
switch (eventStatus.type) {
case MPIEventTypeAMP_FAULT: {
printf("Amp Fault has occurred on motor # %d\n",motorNumber);
printf("Resetting...\n");
returnValue =
mpiMotionAction(motion,
MPIActionRESET);
meiAssert(returnValue == MPIMessageOK);
break;
}
default: {
break;
}
}
}
returnValue = mpiMotorDelete(motor);
msgCHECK(returnValue);
returnValue = mpiMotionDelete(motion);
msgCHECK(returnValue);
returnValue = mpiAxisDelete(axis);
msgCHECK(returnValue);
returnValue = serviceDelete(service);
msgCHECK(returnValue);
returnValue = mpiEventMgrDelete(eventMgr);
msgCHECK(returnValue);
returnValue = mpiNotifyDelete(notify);
msgCHECK(returnValue);
returnValue = mpiControlDelete(control);
msgCHECK(returnValue);
return ((int)returnValue);
}
|