.

     

MPI Application Template
template.c
 

dualLoop.c -- Dual Loop Configuration
/* dualLoop.c */

/* Copyright(c) 1991-2007 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.
 */

/*

:Dual loop configuration
 
This program demonstrates how to configure a motor for dual loop PIV operation. 
 The secondary feedback is used to close the position loop. The primary encoder
 position and its position delta (velocity) are used to close the velocity loop.

motorEncoderSet(...) swaps primary and secondary encoder pointers in controller 
 memory. Velocity pointer is also changed from primary encoder delta to 
 secondary. controlAddressGet(...) and controlAddrToAddr(...) are helper
 functions used to read and modify controller memory.
 
Note: This configuration is done by directly modifying the controller memory.
 Therefore, any operation such as controller reset, FPGA download, power cycle,
 and changes made in Motion Console's motor summary window will cause the 
 modified pointers reset to default values. This code must be run every time the
 actions above take place.


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 MOTOR_NUMBER    (0)      /* Motor to configure dual loop */

/* 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,
                 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 motor object */
    *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);
    *motor = MPIHandleVOID;

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


/*
Gets controller memory *src and writes it to *dst
*/
long controlAddressGet(MPIControl control, void *dst, const void *src)
{
   MPIPlatform  platform;
   long returnValue;

   platform = mpiControlPlatform(control);
   returnValue = mpiPlatformValidate(platform);
   if(returnValue == MPIMessageOK)
   {
      returnValue =
         mpiPlatformMemoryToFirmware(platform,
                              (void**)src,
                              (void**)dst);
   }
   return returnValue;
}

/*
Sets the address of *dst to the address of *src
*/
long controlAddrToAddr(MPIControl control, void *dst, const void *src)
{
   long returnValue;
   long addr;

   returnValue = controlAddressGet(control,
      &addr,
      src);
   if(returnValue)return returnValue;

   returnValue = mpiControlMemorySet(control,
      (void**)dst,
      &addr,
      sizeof(dst));
   return returnValue;
}

/* motorEncoderSet(...) configures a given motor for PIV dual loop operation.
   This allows the controller to close the position loop using secondary
   encoder feedback, usually a signal from a linear encoder mounted at the load. 
   The primary feedback from the rotary motor is used for drive commutation and 
   closing the velocity loop. To set up dual loop configuration, primary and 
   secondary encoder pointers are swapped in controller memory. Velocity pointer 
   is also changed from primary encoder delta to secondary.
*/

long motorEncoderSet(MPIControl control, MPIMotor  motor, long motorNumber)
{
    MPIXmpData        *firmware;
    MPIXmpBufferData  *buffer;
    MPIXmpMotor       *memory;

    long    returnValue;
    long    encoderAddressPrimary;
    long    encoderAddressSecondary;

   returnValue = mpiControlMemory(control, &firmware, &buffer);

    /* Get motor control memory pointer */
    returnValue =
      mpiMotorMemory(motor,
      (void *)&memory);

   /* Get primary motor encoder address */
    if(returnValue == MPIMessageOK)
    {
        returnValue =
            mpiMotorMemoryGet(motor,
         &encoderAddressPrimary,
         &memory->IO.Encoder[0].Ptr,
         sizeof(memory->IO.Encoder[0].Ptr));
    }

    /* Get secondary motor encoder address */
    if(returnValue == MPIMessageOK)
    {
        returnValue =
            mpiMotorMemoryGet(motor,
         &encoderAddressSecondary,
         &memory->IO.Encoder[1].Ptr,
         sizeof(memory->IO.Encoder[1].Ptr));
    }

    /* Set primary motor encoder address to secondary */
    if(returnValue == MPIMessageOK)
    {
        returnValue =
            mpiMotorMemorySet(motor,
         &memory->IO.Encoder[0].Ptr,
         &encoderAddressSecondary,
         sizeof(memory->IO.Encoder[0].Ptr));
    }

   /* Set secondary motor encoder address to primary */
    if(returnValue == MPIMessageOK)
    {
        returnValue =
            mpiMotorMemorySet(motor,
         &memory->IO.Encoder[1].Ptr,
         &encoderAddressPrimary,
         sizeof(memory->IO.Encoder[1].Ptr));
    }

   /* Set velocity pointer to secondary encoder delta */
   if(returnValue == MPIMessageOK)
    {
      returnValue = controlAddrToAddr(control,
         &firmware->ControlLaw.Standard.Filter[motorNumber].VelPtr,
         &memory->IO.Encoder[1].Delta);
   }
    return returnValue;
}

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

   /* Set up motor 0 for dual loop operation. Change MOTOR_NUMBER declaration
   to configure a different motor. */
   motorEncoderSet(control,
               motor,
               MOTOR_NUMBER);

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

    return MPIMessageOK;
}


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