.

     

MPI Application Template
template.c
 

stepcfg.c -- Configure a motor as a stepper motor
/* stepcfg.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 a motor as a stepper motor

This program will configure the motor and filter parameters for a motor to allow
 the motor to control a stepper motor.  The motor type is set to stepper, and
 the pulse width is set (default is 25.5 microseconds), and the loop back mode
 is set.  If loop back is set to TRUE, then the stepper pusles are read by the
 encoder input that is attached to the motor.

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 "..\sqNodeLib\include\mei_rmb.h"


#include "apputil.h"

#if defined(ARG_MAIN_RENAME)
#define main    stepcfgMain

argMainRENAME(main, stepcfg)
#endif

#define MOTION_NUMBER   (0)
#define AXIS_NUMBER     (0)
#define FILTER_NUMBER   (0)
#define MOTOR_NUMBER    (0)

/* LOOPBACK_CONFIG : TRUE = enable stepper loopback, FALSE = no loopback */
#define LOOPBACK_CONFIG     TRUE
/* output pulse width  (seconds) */
#define PULSE_WIDTH_VALUE   (2.55e-5)

#undef CW_CCW_STEPPERS

#if defined CW_CCW_STEPPERS
#define IO_CONFIG_A         (MPIMotorStepperPulseTypeCW)
#define IO_CONFIG_B         (MPIMotorStepperPulseTypeCCW)
#else
#define IO_CONFIG_A         (MPIMotorStepperPulseTypeSTEP)
#define IO_CONFIG_B         (MPIMotorStepperPulseTypeDIR)
#endif

#define TRANSCEIVER_ID_A    (RMBMotorIoConfigXCVR_A)
#define TRANSCEIVER_ID_B    (RMBMotorIoConfigXCVR_B)
#define INVERT_BIT          (FALSE)


/* 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,
                 MPIMotion          *motion,
                 long                motionNumber,
                 MPIAxis            *axis,
                 long                axisNumber,
                 MPIFilter          *filter,
                 long                filterNumber,
                 MPIMotor           *motor,
                 long                motorNumber)
{
    long            returnValue;


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

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

    /* Create axis object */
    *axis =
        mpiAxisCreate(*control,
                      axisNumber);
    msgCHECK(mpiAxisValidate(*axis));

    /* Create motion supervisor object with axis */
    *motion =
        mpiMotionCreate(*control,
                        motionNumber,
                        *axis);
    msgCHECK(mpiMotionValidate(*motion));

    /* Create filter object */
    *filter =
        mpiFilterCreate(*control,
                        filterNumber);
    msgCHECK(mpiFilterValidate(*filter));

    /* Create motor object */
    *motor =
        mpiMotorCreate(*control,
                        motorNumber);
    msgCHECK(mpiMotorValidate(*motor));
}


/* Perform certain cleanup actions and delete MPI objects */
void programCleanup(MPIControl      *control,
                    MPIMotion       *motion,
                    MPIAxis         *axis,
                    MPIFilter       *filter,
                    MPIMotor        *motor)
{
    long    returnValue;


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

    /* Delete filter object */
    returnValue =
        mpiFilterDelete(*filter);
    msgCHECK(returnValue);

    /* Delete motion supervisor object */
    returnValue =
        mpiMotionDelete(*motion);
    msgCHECK(returnValue);

    /* Delete axis object */
    returnValue =
        mpiAxisDelete(*axis);
    msgCHECK(returnValue);

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


/* Disable Filter Algorithm */
void disableFilterAlgorithm(MPIFilter filter)
{
    MPIFilterConfig filterConfigXmp;
    long            returnValue;


    /* Read filter configuration */
    returnValue =
        mpiFilterConfigGet(filter,
                           NULL,
                           &filterConfigXmp);
    msgCHECK(returnValue);

    /* Set filter algorithm to NONE */
    filterConfigXmp.Algorithm = MPIXmpAlgorithmNONE;

    /* Set filter configuration */
    returnValue =
        mpiFilterConfigSet(filter,
                           NULL,
                           &filterConfigXmp);
    msgCHECK(returnValue);
}


/* Configure motor for use as a stepper motor */
void configureStepperMotor(MPIMotor                     motor,
                           MPIMotorStepperPulseType    xcvrAType,
                           long                         xcvrAInvert,
                           MPIMotorStepperPulseType    xcvrBType,
                           long                         xcvrBInvert,
                           float                        pulseWidth,
                           long                         loopBack)
{
    MPIMotorConfig  motorConfigMPI;
    MPIMotorConfig  motorConfigXMP;
    long            returnValue;


    /* Read motor configuration */
    returnValue =
        mpiMotorConfigGet(motor,
                          &motorConfigMPI,
                          &motorConfigXMP);
    msgCHECK(returnValue);

    /* Setup motor as a stepper motor */
    motorConfigMPI.type = (MPIMotorType)MPIXmpMotorTypeSTEPPER;

    motorConfigMPI.event[MPIEventTypeAMP_FAULT].action   = MPIActionNONE;
    motorConfigMPI.event[MPIEventTypeLIMIT_ERROR].action = MPIActionNONE;

    motorConfigXMP.Io[TRANSCEIVER_ID_A].Type = MPIMotorIoTypePULSE_A;
    motorConfigXMP.Io[TRANSCEIVER_ID_B].Type = MPIMotorIoTypePULSE_B;

   motorConfigXMP.Stepper.pulseA.type = xcvrAType;
   motorConfigXMP.Stepper.pulseB.type = xcvrBType;

   motorConfigXMP.Stepper.pulseA.invert = xcvrAInvert;
   motorConfigXMP.Stepper.pulseB.invert = xcvrBInvert;


    motorConfigXMP.Stepper.pulseWidth = pulseWidth;

    motorConfigXMP.Stepper.loopback = loopBack;

   motorConfigXMP.disableAction = MPIMotorDisableActionNONE;

    /* Set motor configuration */
    returnValue =
        mpiMotorConfigSet(motor,
                          &motorConfigMPI,
                          &motorConfigXMP);
    msgCHECK(returnValue);
}


/* Set in position fine tolerance */
void setAxisFineTolerance(MPIAxis   axis,
                          float     fineTolerance)
{
    MPIAxisConfig   axisConfig;
    long            returnValue;


    /* Read axis configuration */
    returnValue =
        mpiAxisConfigGet(axis,
                         &axisConfig,
                         NULL);
    msgCHECK(returnValue);

    /* Set axis in position fine tolerance */
    axisConfig.inPosition.tolerance.positionFine = fineTolerance;

    /* Set axis configuration */
    returnValue =
        mpiAxisConfigSet(axis,
                         &axisConfig,
                         NULL);
    msgCHECK(returnValue);
}

int main(int    argc,
         char   *argv[])
{
    MPIControl          control;
    MPIControlType      controlType;
    MPIControlAddress   controlAddress;
    MPIMotion           motion;
    MPIAxis             axis;
    MPIFilter           filter;
    MPIMotor            motor;


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

    /* Create and initialize MPI objects */
    programInit(&control,
                controlType,
                &controlAddress,
                &motion,
                MOTION_NUMBER,
                &axis,
                AXIS_NUMBER,
                &filter,
                FILTER_NUMBER,
                &motor,
                MOTOR_NUMBER);

    /* Disable Filter Algorithm */
    disableFilterAlgorithm(filter);

    /* Configure motor for use as a stepper motor */
    configureStepperMotor(motor,
                          IO_CONFIG_A,
                          INVERT_BIT,
                          IO_CONFIG_B,
                          INVERT_BIT,
                          (float)PULSE_WIDTH_VALUE,
                          LOOPBACK_CONFIG);

    /* Set in position fine tolerance */
    setAxisFineTolerance(axis,
                         (float)1.E8);

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

    return MPIMessageOK;
}


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