qec.h

00001 /*
00002 
00003   @file qec.h
00004   @author Nicolas Williamson and Thomas Craig 
00005   @date June 2009
00006 
00007 */
00008 
00009 #ifndef __QEC_H__
00010 #define __QEC_H__
00011 
00012 //the resolution of the encoder can be 2 or 4
00013 // - QEC_2X: counts interrupts from only one channel; uses state of both channels to infer direciton
00014 // - QEC_4X: counts interrupts from both channels
00016 typedef enum qec_resolution {
00017   QEC_NULL = 0, 
00018   QEC_2X = 2, 
00019   QEC_4X = 4 
00020 } QEC_RESOLUTION;
00021 
00023 typedef enum qec_nums {
00024   QEC_1 = 0, 
00025   QEC_2 
00026 } QEC_ID;
00027 
00028 //the data associated with an encoder
00030 typedef struct qec_data{
00031   volatile int current_pos; 
00032   int prev_pos; 
00033   int delta_pos; 
00034   int delta_time; 
00035   int* pos_buf; 
00036   int* time_buf; 
00037   int index; 
00038   int length; 
00039   float velocity; 
00040   volatile int prev_chA; 
00041   volatile int prev_chB; 
00042   QEC_RESOLUTION resolution; 
00043 } QEC_DATA;
00044 
00045 #define QEC_2_PI (6.283185307179586476925286766559f) //approximately
00046 
00047 // Functions
00048 //Public
00049 int qec_get_abs_pos(QEC_ID id); //returns the absolute position of the encoder
00050 void qec_update_velocity(void);
00051 float qec_get_velocity(QEC_ID id); //calculates and returns the velocity of an encoder
00052 void qec_init(QEC_RESOLUTION enc_res_1, int enc1_avg_time, QEC_RESOLUTION enc_res_2, int enc2_avg_time, VOID_VOID_F function);
00053 int qec_is_stopped(QEC_ID id);
00054 //Private
00055 void qec_isr(void); //__irq; //interrupt called when a new edge or timer overflow is detected
00056 static void qec_init_data(volatile QEC_DATA* data, QEC_RESOLUTION resolution, int* pos_buf, int* time_buf, int length);
00057 static void qec_calc_velocity(volatile QEC_DATA* data, int ticks_elapsed);
00058 //updates the given encoder with given flag and state information
00059 void qec_update(volatile QEC_DATA* data, int flag_chA, int flag_chB, int chA, int chB);
00060 
00061 #endif // __QEC_H__
00062 
Generated on Tue Jun 29 16:36:14 2010 by  doxygen 1.6.3