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

encoder.c

Go to the documentation of this file.
00001 /*
00002    _encoder.c
00003 
00004    C defs for encoder
00005 
00006    Modification history:
00007 
00008    14-Apr-2000 WPS added _attribute__((unused)) to ident
00009    23-Sep-1999  WPS replaced stdio functions with inet_file functions
00010    which work under CE.
00011    17-Mar-1998  FMP added section arg to encoderIniLoad()
00012    16-Jun-1997  FMP added index init
00013    24-Apr-1997  FMP added encoderIniLoad()
00014    17-Apr-1997  FMP created from C portion of original encoder.c
00015 */
00016 
00017 #ifndef NO_STDIO_SUPPORT
00018 #ifndef UNDER_CE
00019 #include <stdio.h>
00020 #endif
00021 #include "inetfile.hh"          /* inet_file_open() */
00022 #include "inifile.h"
00023 #include "rcs_prnt.hh"          /* rcs_print() */
00024 #endif
00025 #include <math.h>               /* fmod() */
00026 #include "encoder.h"            /* these decls */
00027 
00028 /* ident tag */
00029 #ifndef __GNUC__
00030 #ifndef __attribute__
00031 #define __attribute__(x)
00032 #endif
00033 #endif
00034 
00035 static char __attribute__((unused)) ident[] = "$Id: encoder.c,v 1.2 2000/10/27 20:34:42 terrylr Exp $";
00036 
00037 #define COUNTS_PER_REV_SET 0x01
00038 #define ALL_SET (COUNTS_PER_REV_SET)
00039 
00040 #define TWO_PI (2.0 * 3.1415926535898)
00041 #define CALIBRATION_FUZZ 0.0001
00042 
00043 /*
00044    returns non-zero if a multiple of TWO_PI lies in range
00045 */
00046 static int sawIndex(double pos, double lastPos)
00047 {
00048   if (pos <= 0.0 && lastPos >= 0.0)
00049   {
00050     return 1;
00051   }
00052 
00053   if (pos >= 0.0 && lastPos <= 0.0)
00054   {
00055     return 1;
00056   }
00057 
00058   if (pos - fmod(pos, TWO_PI) != lastPos - fmod(lastPos, TWO_PI))
00059   {
00060     return 1;
00061   }
00062 
00063   return 0;
00064 }
00065 
00066 int encoderInit(ENCODER_STRUCT * enc)
00067 {
00068   if (0 == enc)
00069   {
00070     return -1;
00071   }
00072 
00073   /* parameters */
00074   enc->countsPerRev = 0;
00075 
00076   /* internal vars */
00077   enc->configured = 0;
00078   enc->counts = 0;
00079   enc->calibrating = 0;
00080   enc->offset = 0.0;
00081   enc->lastPosition = 0.0;
00082   enc->index = 0;
00083 
00084   return 0;
00085 }
00086 
00087 int encoderSetCountsPerRev(ENCODER_STRUCT * enc, int countsPerRev)
00088 {
00089   if (0 == enc ||
00090       countsPerRev <= 0)
00091   {
00092     return -1;
00093   }
00094 
00095   enc->countsPerRev = countsPerRev;
00096   enc->configured |= COUNTS_PER_REV_SET;
00097 
00098   return 0;
00099 }
00100 
00101 int encoderGetCountsPerRev(ENCODER_STRUCT * enc)
00102 {
00103   if (0 == enc ||
00104       ! (enc->configured & COUNTS_PER_REV_SET))
00105   {
00106     return 0;
00107   }
00108 
00109   return enc->countsPerRev;
00110 }
00111 
00112 int encoderGetCounts(ENCODER_STRUCT * enc, double position)
00113 {
00114   if (0 == enc ||
00115       enc->configured != ALL_SET)
00116   {
00117     return 0;
00118   }
00119 
00120   if (enc->calibrating)
00121   {
00122     /* see if range [lastPosition, position] includes a multiple of
00123        TWO_PI. If so, we saw an index pulse */
00124     if (sawIndex(position, enc->lastPosition))
00125     {
00126       enc->offset = position - fmod(position, TWO_PI);
00127       enc->calibrating = 0;
00128     }
00129   }
00130 
00131   enc->counts = (int) (((double) enc->countsPerRev) *
00132                        ((position - enc->offset) / TWO_PI));
00133   enc->lastPosition = position;
00134 
00135   return enc->counts;
00136 }
00137 
00138 int encoderCalibrate(ENCODER_STRUCT * enc)
00139 {
00140   if (0 == enc ||
00141       enc->configured != ALL_SET)
00142   {
00143     return -1;
00144   }
00145 
00146   enc->calibrating = 1;
00147 
00148   return 0;
00149 }
00150 
00151 int encoderNoCalibrate(ENCODER_STRUCT * enc)
00152 {
00153   if (0 == enc)
00154   {
00155     return -1;
00156   }
00157 
00158   enc->calibrating = 0;
00159 
00160   return 0;
00161 }
00162 
00163 int encoderIsCalibrating(ENCODER_STRUCT * enc)
00164 {
00165   if (0 == enc)
00166   {
00167     return -1;
00168   }
00169 
00170   return enc->calibrating;
00171 }
00172 
00173 int encoderIniLoad(ENCODER_STRUCT * enc, const char * filename, const char *section)
00174 {
00175 #ifndef NO_STDIO_SUPPORT
00176 
00177   int countsPerRev;
00178   int retval = 0;
00179   const char *inistring;
00180 #ifndef UNDER_CE
00181   FILE *fp;
00182 
00183   if (NULL == (fp = fopen(filename, "r")))
00184   {
00185     rcs_print_error( "can't open ini file %s\n", filename);
00186     return -1;
00187   }
00188 #else
00189   INET_FILE *fp;
00190 
00191   if (NULL == (fp = inet_file_open(filename, "r")))
00192   {
00193     rcs_print_error( "can't open ini file %s\n", filename);
00194     return -1;
00195   }
00196 #endif
00197 
00198   if (NULL != (inistring = iniFind(fp, "COUNTS_PER_REV", section)))
00199   {
00200 #ifndef UNDER_CE
00201     if (1 != sscanf((char *) inistring, "%d", &countsPerRev))
00202     {
00203       /* found, but invalid */
00204       rcs_print_error( "invalid COUNTS_PER_REV: %s\n", inistring);
00205       retval = -1;
00206     }
00207     else
00208     {
00209       encoderSetCountsPerRev(enc, countsPerRev);
00210     }
00211 #else
00212     countsPerRev = atol(inistring);
00213     encoderSetCountsPerRev(enc, countsPerRev);
00214 #endif
00215 
00216   }
00217 
00218   /* close inifile */
00219 #ifndef UNDER_CE
00220   fclose(fp);
00221 #else
00222   inet_file_close(fp);
00223 #endif
00224 
00225   return retval;
00226 
00227 #else
00228 
00229   return -1;
00230 
00231 #endif
00232 }

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