encRatio.c -- Sets the MEIMotorEncoderRatio parameter
/* encRatio.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.
*/
/*
:Sets the MEIMotorEncoderRatio parameter
This program demonstrates how to get and set the MEIMotorEncoderRatio
parameters A and B. The two values are set to 0 by default.
A/B = 1 is not recommended because it would be the same as having this
functionality disabled while consuming more controller resources.
Encoder resolution will be scaled by A/B.
Actual move will be B/A times of the commanded move. Velocity and
acceleration values will be scaled by the same factor as well.
Please note in order to have the controller remember encoder ratio
settings, the user must either save settings to flash or the user
application must initialize encoder ratio settings to previous values
upon meiReset. Otherwise encoder ratio information will be lost and the
position saved in controller memory will be incorrect.
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 MOTION_NUMBER (0)
#define AXIS_NUMBER (0)
#define FILTER_NUMBER (0)
#define MOTOR_NUMBER (0)
#define ENCODER_NUMBER (0)
#define SQNODE_NUMBER (0)
/* Desired encoder ratio values */
#define EncoderRatioA (1)
#define EncoderRatioB (2)
/* 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,
MEISynqNet *synqNet,
MEISqNode *sqNode,
long sqNodeNumber)
{
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));
/* Map axis to motion supervisor on XMP */
returnValue =
mpiMotionAction(*motion, MEIActionMAP);
msgCHECK(returnValue);
/* 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,
MEISynqNet *synqNet,
MEISqNode *sqNode)
{
long returnValue;
/* Delete motor object */
returnValue =
mpiMotorDelete(*motor);
msgCHECK(returnValue);
*motor = MPIHandleVOID;
/* Delete filter object */
returnValue =
mpiFilterDelete(*filter);
msgCHECK(returnValue);
*filter = MPIHandleVOID;
/* Delete motion supervisor object */
returnValue =
mpiMotionDelete(*motion);
msgCHECK(returnValue);
*motion = MPIHandleVOID;
/* Delete axis object */
returnValue =
mpiAxisDelete(*axis);
msgCHECK(returnValue);
*axis = MPIHandleVOID;
/* Delete motion controller object */
returnValue =
mpiControlDelete(*control);
msgCHECK(returnValue);
*control = MPIHandleVOID;
}
/* Set Encoder Ratio Function */
long setEncoderRatio(MPIControl control,
MPIMotor motor,
long A,
long B)
{
long returnValue = 0;
MEIMotorConfig motorConfig;
/* Get motor configuration handle */
returnValue
= mpiMotorConfigGet(motor,
NULL,
&motorConfig);
msgCHECK(returnValue);
/* Display current encoder ratio values */
printf("Current value of A is %d\n", motorConfig.Encoder->ratio.A);
printf("Current value of B is %d\n", motorConfig.Encoder->ratio.B);
/* Set encoder ratio */
motorConfig.Encoder->ratio.A = A;
motorConfig.Encoder->ratio.B = B;
/* Set motor configuration */
returnValue =
mpiMotorConfigSet(motor,
NULL,
&motorConfig);
msgCHECK(returnValue);
/* Display new encoder ratio values */
printf("New value of A is %d\n", motorConfig.Encoder->ratio.A);
printf("New value of A is %d\n", motorConfig.Encoder->ratio.B);
return returnValue;
}
/* Your main function */
int main(int argc,
char *argv[])
{
MPIControl control;
MPIControlType controlType;
MPIControlAddress controlAddress;
MPIMotion motion;
MPIAxis axis;
MPIFilter filter;
MPIMotor motor;
MEISynqNet synqNet;
MEISqNode sqNode;
long returnValue;
/* 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,
&synqNet,
&sqNode,
SQNODE_NUMBER);
/* set encoder ratio */
returnValue =
setEncoderRatio(control,
motor,
EncoderRatioA,
EncoderRatioB);
msgCHECK(returnValue);
/* Perform certain cleanup actions and delete MPI objects */
programCleanup(&control,
&motion,
&axis,
&filter,
&motor,
&synqNet,
&sqNode);
return MPIMessageOK;
}
|