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

encoder.c File Reference

#include <stdio.h>
#include "inetfile.hh"
#include "inifile.h"
#include "rcs_prnt.hh"
#include <math.h>
#include "encoder.h"

Include dependency graph for encoder.c:

Include dependency graph

Go to the source code of this file.

Defines

#define COUNTS_PER_REV_SET   0x01
#define ALL_SET   (COUNTS_PER_REV_SET)
#define TWO_PI   (2.0 * 3.1415926535898)
#define CALIBRATION_FUZZ   0.0001

Functions

char __attribute__ ((unused)) ident[]="$Id
int encoderInit (ENCODER_STRUCT *enc)
int encoderSetCountsPerRev (ENCODER_STRUCT *enc, int countsPerRev)
int encoderGetCountsPerRev (ENCODER_STRUCT *enc)
int encoderGetCounts (ENCODER_STRUCT *enc, double position)
int encoderCalibrate (ENCODER_STRUCT *enc)
int encoderNoCalibrate (ENCODER_STRUCT *enc)
int encoderIsCalibrating (ENCODER_STRUCT *enc)
int encoderIniLoad (ENCODER_STRUCT *enc, const char *filename, const char *section)


Define Documentation

#define ALL_SET   (COUNTS_PER_REV_SET)
 

#define CALIBRATION_FUZZ   0.0001
 

#define COUNTS_PER_REV_SET   0x01
 

#define TWO_PI   (2.0 * 3.1415926535898)
 


Function Documentation

char __attribute__ (unused)    [static]
 

Definition at line 35 of file encoder.c.

00035                                                   : 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 }

int encoderCalibrate ENCODER_STRUCT   enc
 

Definition at line 138 of file encoder.c.

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 }

int encoderGetCounts ENCODER_STRUCT   enc,
double    position
 

Definition at line 112 of file encoder.c.

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 }

int encoderGetCountsPerRev ENCODER_STRUCT   enc
 

Definition at line 101 of file encoder.c.

00102 {
00103   if (0 == enc ||
00104       ! (enc->configured & COUNTS_PER_REV_SET))
00105   {
00106     return 0;
00107   }
00108 
00109   return enc->countsPerRev;
00110 }

int encoderIniLoad ENCODER_STRUCT   enc,
const char *    filename,
const char *    section
 

Definition at line 173 of file encoder.c.

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 }

int encoderInit ENCODER_STRUCT   enc
 

Definition at line 66 of file encoder.c.

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 }

int encoderIsCalibrating ENCODER_STRUCT   enc
 

Definition at line 163 of file encoder.c.

00164 {
00165   if (0 == enc)
00166   {
00167     return -1;
00168   }
00169 
00170   return enc->calibrating;
00171 }

int encoderNoCalibrate ENCODER_STRUCT   enc
 

Definition at line 151 of file encoder.c.

00152 {
00153   if (0 == enc)
00154   {
00155     return -1;
00156   }
00157 
00158   enc->calibrating = 0;
00159 
00160   return 0;
00161 }

int encoderSetCountsPerRev ENCODER_STRUCT   enc,
int    countsPerRev
 

Definition at line 87 of file encoder.c.

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 }


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