.

     

MPI Application Template
template.c
 

ProbeSniffer.c -- Monitor the state of any Probe that has been configured.

/* 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.
 */

/*

P R O B E   S N I F F E R
===================================
This program may be used to monitor the state of any Probe that has been
 configured.

Use to diagnose problems with Probe configuration and confirm captured
 time/position in the registers.


I N S T R U C T I O N S
=======================
1. Compile this code for your MPI
2. Run Program
3. Program will Prompt for a Motor # that the Probe is on, enter the desired
   Motor (with Probe on it) to watch 0-31
4. After selecting a Motor Number, enter the Probe Index(on that Motor) to watch,
   # of available Probes varies by node.
5. Use keyboard to select how to view the information: (Type a letter at any time)
   d = Double, f = Float, h = Hex
6. To monitor a different user limit, type "c", refer to Step 3
7. To clear the selected Probe's configuration, and disable the probe, press "L"
8. To Quit, type "q"


C O M P I L E   N O T E S
=========================
Originally built and compiled under MPI 03.04.08

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 <windows.h> 

#include "stdmpi.h"
#include "stdmei.h"

#include "apputil.h"

typedef struct objProbe {
   long  ynEnabled;
   long  probeSource;
   long  probeDataType;
   long  ynInputFilter;

   long  intMaxRegisterCount;
   long  intProbeIndex;
   long  ioStatus;
   long  arrRegisters[MPIMaxProbeRegisters];

} objProbe;

//Function Declarations
void ProbeSourceToEnglish(int intProbeSource, char *strProbeSource);
void ProbeDataTypeToEnglish(int intProbeDataType, char *strProbeDataType);
void BooleanToEnglish(int ynBoolean, char *strBoolean);
void clearUserLimit(MPIControl control, MPIMotor *motor, int intUserLimit);

// Setup Global Variables
long  intProbeToSniff=-1;  // -1: Nothing Specified, anything greater watches the
                        //    specified probe on specified motor
                        //    # Available varies by node
long  intMotorNumToSniff=-1;  // -1: Nothing Specified, anything greater watches the
                        //    specified Motor 0-31 Available
char  DisplayMode = 'h';      // d = Double, f = Float, h = hex


/* 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[32])
{
    long            returnValue;
   long        i=0;  //Loop incrementor

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

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


   /* Create motor objects */
   for(i=0; i<32; i++){
      motor[i] = mpiMotorCreate(*control, i);
      msgCHECK(mpiMotorValidate(motor[i]));
   }
}

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

   /* Delete Motor Objects */
   for(i=0; i<32; i++){
      returnValue = mpiMotorDelete(motor[i]);
      msgCHECK(returnValue);
   }

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

/* Create Probe Object */
void createProbe(MPIControl control, MPIProbe *probe)
{
   MPIProbeParams    probeParams;

   probeParams.probeIndex = intProbeToSniff;
   probeParams.type = MPIProbeTypeMOTOR;
   probeParams.number.motor = intMotorNumToSniff;

   /* Create probe */
   *probe = mpiProbeCreate(control, &probeParams);
   msgCHECK(mpiProbeValidate(*probe));
}

/* Delete Probe object */
void destroyProbe(MPIProbe *probe)
{
   long returnValue;

   returnValue = mpiProbeDelete(*probe);
   msgCHECK(returnValue);
}

/* Clear Probe configuration and disable */
void clearProbe(MPIProbe *probe)
{
   MPIProbeStatus       probeStatus;
   MPIProbeParams       probeParams;
   MPIProbeConfig       probeConfig;
   long              returnValue;

   probeParams.type = MPIProbeTypeMOTOR;
   probeParams.number.motor = intMotorNumToSniff;
   probeParams.probeIndex = intProbeToSniff;

   msgCHECK(mpiProbeValidate(*probe));

   /* Get probe configuration handle */
   returnValue = mpiProbeConfigGet(*probe, &probeConfig, NULL);
   msgCHECK(returnValue);

   probeConfig.enable = FALSE;            // Disable Probe
   probeConfig.source = 0;
   probeConfig.data = MPIProbeDataTIME;   /* Time mode */
   probeConfig.inputFilter = FALSE;

   /* Set probe configuration */
   returnValue = mpiProbeConfigSet(*probe, &probeConfig, NULL);
   msgCHECK(returnValue);

   returnValue = mpiProbeStatus(*probe, &probeStatus, NULL);
   msgCHECK(returnValue);
}

/* This Function Reads the current State and Registers of a specified probe */
void readProbe(MPIControl control, MPIProbe *probe, objProbe *sniffedProbe)
{
   MPIProbeConfig       probeConfig;
   MPIProbeStatus       probeStatus;

   char              strEnabled[MEIMapNameSizeMAX];
   char              strProbeSource[MEIMapNameSizeMAX];
   char              strProbeDataType[MEIMapNameSizeMAX];
   char              strInputFilter[MEIMapNameSizeMAX];
   long              returnValue;
   long              i;

   /* Get probe configuration handle */
   returnValue = mpiProbeConfigGet(*probe, &probeConfig, NULL);
   msgCHECK(returnValue);

   returnValue = mpiProbeStatus(*probe, &probeStatus, NULL);
   msgCHECK(returnValue);

   //Read Info from probeConfig
   sniffedProbe->ynEnabled = probeConfig.enable;
   sniffedProbe->probeSource = probeConfig.source;
   sniffedProbe->probeDataType = probeConfig.data;
   sniffedProbe->ynInputFilter = probeConfig.inputFilter;

   //Read Info from probeStatus
   sniffedProbe->intMaxRegisterCount = probeStatus.maxRegisters;
   sniffedProbe->intProbeIndex = probeStatus.index;
   sniffedProbe->ioStatus = probeStatus.io;
   //Populate Registers
   for(i = 0; i < sniffedProbe->intMaxRegisterCount; i++){
      sniffedProbe->arrRegisters[i] = probeStatus.regs[i];
   }

   // Convert Status values to English
   BooleanToEnglish(sniffedProbe->ynEnabled, strEnabled);
   ProbeSourceToEnglish(sniffedProbe->probeSource, strProbeSource);
   ProbeDataTypeToEnglish(sniffedProbe->probeDataType, strProbeDataType);
   BooleanToEnglish(sniffedProbe->ynInputFilter, strInputFilter);

   // Start Printing to Screen
   printf("PROPERTIES FOR PROBE:\tMotor #:\t%d", intMotorNumToSniff);
   printf("\tEnabled:\t%s\n", strEnabled);
   printf("\t\t\tProbe #:\t%d", intProbeToSniff);
   printf("\tInput Filter:\t%s\n", strInputFilter);
   printf("\t\t\tProbe Source:\t%s\n", strProbeSource);
   printf("\t\t\tProbe DataType:\t%s\n", strProbeDataType);
   printf("\t\t\tMax Registers:\t%d\n", sniffedProbe->intMaxRegisterCount);
   printf("\t\t\tProbe Index:\t%d\n", sniffedProbe->intProbeIndex);

   switch(DisplayMode)
   {
      case 'f':{  //Case Float
               printf("\t\t\tIO State:\t%2.2f\n\n", (float)*((float *)&sniffedProbe->ioStatus));
               for(i = 0; i < sniffedProbe->intMaxRegisterCount; i++){
                  printf("\t\t\tRegister %d:\t%2.2f\n", i, (float)*((float *)&sniffedProbe->arrRegisters[i]));//Separated or else String displays Funky
               }
               break;
            }
      case 'h':{  //Default: Case Hex
               printf("\t\t\tIO State:\t0x%8.8X\n\n", sniffedProbe->ioStatus);
               for(i = 0; i < sniffedProbe->intMaxRegisterCount; i++){
                  printf("\t\t\tRegister %d:\t0x%4.4X\n", i, sniffedProbe->arrRegisters[i]);//Separated or else String displays Funky
               }
               break;
            }

      default:{   //Default => d
               printf("\t\t\tIO State:\t%d\n\n", sniffedProbe->ioStatus);
               for(i = 0; i < sniffedProbe->intMaxRegisterCount; i++){
                  printf("\t\t\tRegister %d:\t%d\n", i, sniffedProbe->arrRegisters[i]);//Separated or else String displays Funky
               }
               break;
            }
   }//end Switch
}

void displayHeaders(){
   // Clear Screen
   system ("cls");

   // Informational Printout
   printf("|     P R O B E    S N I F F E R     |  Change Data Type:  D - Double  |\n|------------------------------------|                     F - Float   |\n");
   printf("|Key Commands:  Q - Quit             |                     H - Hex     |\n|               C - Change Probe #   |                                 |\n");
   printf("|               L - Clear Probe      |                                 |\n");
   printf("|----------------------------------------------------------------------|\n");
}

/* Convert mpiProbeSource value to English */
void ProbeSourceToEnglish(int intProbeSource, char *strProbeSource){
   switch(intProbeSource)
   {
      case (MPIProbeSourceHOME):{
               sprintf(strProbeSource,"Triggers on MPIProbeSourceHOME");
               break;
            }
      case (MPIProbeSourceINDEX):{
               sprintf(strProbeSource,"Triggers on Condition[0]=True");
               break;
            }
      case (MPIProbeSourceLIMIT_HW_NEG):{
               sprintf(strProbeSource,"Triggers on MPIProbeSourceLIMIT_HW_NEG");
               break;
            }
      case (MPIProbeSourceLIMIT_HW_POS):{
               sprintf(strProbeSource,"Triggers on MPIProbeSourceLIMIT_HW_POS");
               break;
            }
      case (MPIProbeSourceINDEX_SECONDARY):{
               sprintf(strProbeSource,"Triggers on MPIProbeSourceINDEX_SECONDARY");
               break;
            }
      case (MPIProbeSourceMOTOR_IO_0):{
               sprintf(strProbeSource,"Triggers on MPIProbeSourceMOTOR_IO_0");
               break;
            }
      case (MPIProbeSourceMOTOR_IO_1):{
               sprintf(strProbeSource,"Triggers on MPIProbeSourceMOTOR_IO_1");
               break;
            }
      case (MPIProbeSourceMOTOR_IO_2):{
               sprintf(strProbeSource,"Triggers on MPIProbeSourceMOTOR_IO_2");
               break;
            }
      case (MPIProbeSourceMOTOR_IO_3):{
               sprintf(strProbeSource,"Triggers on MPIProbeSourceMOTOR_IO_3");
               break;
            }
      case (MPIProbeSourceMOTOR_IO_4):{
               sprintf(strProbeSource,"Triggers on MPIProbeSourceMOTOR_IO_4");
               break;
            }
      case (MPIProbeSourceMOTOR_IO_5):{
               sprintf(strProbeSource,"Triggers on MPIProbeSourceMOTOR_IO_5");
               break;
            }
      case (MPIProbeSourceMOTOR_IO_6):{
               sprintf(strProbeSource,"Triggers on MPIProbeSourceMOTOR_IO_6");
               break;
            }
      case (MPIProbeSourceMOTOR_IO_7):{
               sprintf(strProbeSource,"Triggers on MPIProbeSourceMOTOR_IO_7");
               break;
            }
      case (MPIProbeSourceMOTOR_IO_8):{
               sprintf(strProbeSource,"Triggers on MPIProbeSourceMOTOR_IO_8");
               break;
            }
      case (MPIProbeSourceMOTOR_IO_9):{
               sprintf(strProbeSource,"Triggers on MPIProbeSourceMOTOR_IO_9");
               break;
            }
      case (MPIProbeSourceMOTOR_IO_10):{
               sprintf(strProbeSource,"Triggers on MPIProbeSourceMOTOR_IO_10");
               break;
            }
      case (MPIProbeSourceMOTOR_IO_11):{
               sprintf(strProbeSource,"Triggers on MPIProbeSourceMOTOR_IO_11");
               break;
            }
      case (MPIProbeSourceMOTOR_IO_12):{
               sprintf(strProbeSource,"Triggers on MPIProbeSourceMOTOR_IO_12");
               break;
            }
      case (MPIProbeSourceMOTOR_IO_13):{
               sprintf(strProbeSource,"Triggers on MPIProbeSourceMOTOR_IO_13");
               break;
            }
      case (MPIProbeSourceMOTOR_IO_14):{
               sprintf(strProbeSource,"Triggers on MPIProbeSourceMOTOR_IO_14");
               break;
            }
      case (MPIProbeSourceMOTOR_IO_15):{
               sprintf(strProbeSource,"Triggers on MPIProbeSourceMOTOR_IO_15");
               break;

            }

      default:{
               sprintf(strProbeSource,"** Not Set **");
               break;
            }
   }//End Switch
}

/* Convert mpiProbeData value to English */
void ProbeDataTypeToEnglish(int intProbeDataType, char *strProbeDataType){
   switch(intProbeDataType)
   {
      case MPIProbeDataPOSITION_PRIMARY:{
               sprintf(strProbeDataType,"MPIProbeDataPOSITION_PRIMARY");
               break;
            }
      case MPIProbeDataPOSITION_SECONDARY:{
               sprintf(strProbeDataType,"MPIProbeDataPOSITION_SECONDARY");
               break;
            }
      case MPIProbeDataTIME:{
               sprintf(strProbeDataType,"MPIProbeDataTIME");
               break;
            }
      default:{
               sprintf(strProbeDataType,"** Not Set **");
               break;
            }
   }//End Switch
}

/* Convert Boolean value to English */
void BooleanToEnglish(int ynBoolean, char *strBoolean){
   switch(ynBoolean)
   {
      case 0:{
               sprintf(strBoolean,"FALSE");
               break;
            }
      case 1:{
               sprintf(strBoolean,"TRUE");
               break;
            }
      default:{   //Default => d
               sprintf(strBoolean,"** Not Set **");
               break;
            }
   }//End Switch
}


int main(int     argc,
         char   *argv[])
{
    MPIControl          control;
    MPIControlType      controlType;
    MPIControlAddress   controlAddress;
   MPIMotor       motor[32];      /* motor object */
   MPIProbe       probe;
   MPI_BOOL            done = FALSE;
   char                key;
   objProbe       sniffedProbe;

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

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

   displayHeaders();

    /* Display in use status and shutdown recorders if requested */
   while (!done)
   {
      key = (char) meiPlatformKey(MPIWaitMSEC*100);

      switch(key)
      {
         case 'q':{
                  done = TRUE;
                  printf("\n\n\n\nExit Sequence Completed\n");
                  break;
               }
         case 'c':{
                  intProbeToSniff = -1;
                  destroyProbe(&probe);
                  displayHeaders();
                  break;
               }
         case 'd':{
                  DisplayMode = 'd';
                  break;
                }
         case 'f':{
                  DisplayMode = 'f';
                  break;
                }
         case 'h':{
                  DisplayMode = 'h';
                  break;
                }
         case 'l':{
                  //Clear Probe Function
                  clearProbe(&probe);
                  break;
                }
         default:{
                  break;
               }
      }


      // Check Value of Probe #
      if(intProbeToSniff > -1)
      {
         //If Probe# is Specified, SNIFF IT!
         displayHeaders();
         readProbe(control, &probe, &sniffedProbe);
         meiPlatformSleep(150);

      }else{
         // Prompt User for new User Limit #, as well as Limit # to sniff;
         printf("\f\rEnter Motor Number With Probe (0-31): ");
         scanf("%d", &intMotorNumToSniff);
         printf("Enter Probe on Motor to Monitor: ");
         scanf("%d", &intProbeToSniff);

         // Create Probe based on motor/probe numbers entered
         createProbe(control, &probe);
      }

   }

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

    return MPIMessageOK;
}

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