Main Page   Class Hierarchy   Alphabetical List   Data Structures   File List   Data Fields   Globals  

alter.c

Go to the documentation of this file.
00001 /*
00002   alter.c
00003 
00004   Sample external process that dynamically alters the input position
00005   of the EMC's 'axis', using a sine wave of frequency 'freq' (Hz), with
00006   amplitude 'range'. Syntax is:
00007 
00008   alter -axis <axis> -freq <freq> -range <range> -ini <inifile>
00009 
00010   Any or all args can be omitted. Default is 0, 1.0, 1.0, and emc.ini,
00011   respectively.
00012 
00013   The alter interface is intended to support dynamic compensations of small
00014   amounts based on a sensor-based machine model, for example from temperature.
00015 */
00016 
00017 #include <stdio.h>              /* fprintf(), stderr */
00018 #include <string.h>             /* strncmp() */
00019 #include <stdlib.h>             /* exit() */
00020 #include <float.h>              /* DBL_MIN */
00021 #include <signal.h>             /* SIGINT, signal() */
00022 #include <math.h>               /* sin() */
00023 #include <unistd.h>             /* usleep() */
00024 #include "emcmotglb.h"          /* EMC_INIFILE */
00025 #include "usrmotintf.h"         /* usrmotIniLoad(), usrmotInit(),
00026                                    usrmotAlter(), usrmotQueryAlter() */
00027 
00028 #define TWO_PI 6.28318530717958647692
00029 #define SLEEP_USECS 10000
00030 
00031 static int done = 0;
00032 static void quit(int sig)
00033 {
00034   done = 1;
00035 }
00036 
00037 int main(int argc, char *argv[])
00038 {
00039   int t;
00040   int axis = 0;
00041   double range = 1.0;
00042   double freq = 1.0;
00043   double freqinv = 1.0;
00044   double alter;
00045   double x;
00046 
00047   /* look for EMC_INIFILE, or leave as default if not found */
00048   for (t = 1; t < argc; t++) {
00049     if (! strcmp(argv[t], "-ini")) {
00050       /* make sure -ini isn't last, since we'll look at next arg */
00051       if (t == argc - 1) {
00052         fprintf(stderr, "can't read -ini parameter; using default %s\n", EMCMOT_INIFILE);
00053       }
00054       else {
00055         /* got it */
00056         strncpy(EMCMOT_INIFILE, argv[t + 1], EMCMOT_INIFILE_LEN - 1);
00057         EMCMOT_INIFILE[EMCMOT_INIFILE_LEN - 1] = 0;
00058         t++;                    /* step over arg */
00059       }
00060       continue;
00061     }
00062     if (! strcmp(argv[t], "-axis")) {
00063       /* make sure -axis isn't last, since we'll look at next arg */
00064       if (t == argc - 1) {
00065         fprintf(stderr, "can't read -axis parameter; using default %d\n", axis);
00066       }
00067       else {
00068         if (1 != sscanf(argv[t + 1], "%d", &axis)) {
00069           fprintf(stderr, "bad value for axis: %s\n", argv[t + 1]);
00070           exit(1);
00071         }
00072         /* got it */
00073         t++;                    /* step over arg */
00074       }
00075       continue;
00076     }
00077     if (! strcmp(argv[t], "-range")) {
00078       /* make sure -range isn't last, since we'll look at next arg */
00079       if (t == argc - 1) {
00080         fprintf(stderr, "can't read -range parameter; using default %f\n", range);
00081       }
00082       else {
00083         if (1 != sscanf(argv[t + 1], "%lf", &range)) {
00084           fprintf(stderr, "bad value for range: %s\n", argv[t + 1]);
00085           exit(1);
00086         }
00087         /* got it */
00088         t++;                    /* step over arg */
00089       }
00090       continue;
00091     }
00092     if (! strcmp(argv[t], "-freq")) {
00093       /* make sure -freq isn't last, since we'll look at next arg */
00094       if (t == argc - 1) {
00095         fprintf(stderr, "can't read -freq parameter; using default %f\n", freq);
00096       }
00097       else {
00098         if (1 != sscanf(argv[t + 1], "%lf", &freq) ||
00099             freq < DBL_MIN) {
00100           fprintf(stderr, "bad value for freq: %s\n", argv[t + 1]);
00101           exit(1);
00102         }
00103         /* got it */
00104         freqinv = 1.0 / freq;   /* already checked freq against DBL_MIN */
00105         t++;                    /* step over arg */
00106       }
00107       continue;
00108     }
00109   }
00110 
00111   if (0 != usrmotIniLoad(EMCMOT_INIFILE)) {
00112     fprintf(stderr, "can't initialize from %s\n", EMCMOT_INIFILE);
00113     exit(1);
00114   }
00115 
00116   if (0 != usrmotInit()) {
00117     fprintf(stderr, "can't connect to motion controller\n");
00118     exit(1);
00119   }
00120 
00121   done = 0;
00122   signal(SIGINT, quit);
00123   x = 0;
00124 
00125   while (! done) {
00126     alter = range * sin(TWO_PI * freq * x);
00127     usrmotAlter(axis, alter);
00128     x += ((double) SLEEP_USECS) / 1000000.0;
00129     if (x > freqinv) {
00130       x = 0.0;
00131     }
00132     usleep(SLEEP_USECS);
00133   }
00134 
00135   usrmotExit();
00136 
00137   exit(0);
00138 }

Generated on Sun Dec 2 15:27:35 2001 for EMC by doxygen1.2.11.1 written by Dimitri van Heesch, © 1997-2001